11da177e4SLinus Torvalds /* 23e57ecf6SOlaf Weber * Copyright (c) 2000-2006 Silicon Graphics, Inc. 37b718769SNathan Scott * All Rights Reserved. 41da177e4SLinus Torvalds * 57b718769SNathan Scott * This program is free software; you can redistribute it and/or 67b718769SNathan Scott * modify it under the terms of the GNU General Public License as 71da177e4SLinus Torvalds * published by the Free Software Foundation. 81da177e4SLinus Torvalds * 97b718769SNathan Scott * This program is distributed in the hope that it would be useful, 107b718769SNathan Scott * but WITHOUT ANY WARRANTY; without even the implied warranty of 117b718769SNathan Scott * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127b718769SNathan Scott * GNU General Public License for more details. 131da177e4SLinus Torvalds * 147b718769SNathan Scott * You should have received a copy of the GNU General Public License 157b718769SNathan Scott * along with this program; if not, write the Free Software Foundation, 167b718769SNathan Scott * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 171da177e4SLinus Torvalds */ 1840ebd81dSRobert P. J. Day #include <linux/log2.h> 1940ebd81dSRobert P. J. Day 201da177e4SLinus Torvalds #include "xfs.h" 21a844f451SNathan Scott #include "xfs_fs.h" 2270a9883cSDave Chinner #include "xfs_shared.h" 23239880efSDave Chinner #include "xfs_format.h" 24239880efSDave Chinner #include "xfs_log_format.h" 25239880efSDave Chinner #include "xfs_trans_resv.h" 261da177e4SLinus Torvalds #include "xfs_sb.h" 271da177e4SLinus Torvalds #include "xfs_mount.h" 28a4fbe6abSDave Chinner #include "xfs_inode.h" 2957062787SDave Chinner #include "xfs_da_format.h" 30c24b5dfaSDave Chinner #include "xfs_da_btree.h" 31c24b5dfaSDave Chinner #include "xfs_dir2.h" 32a844f451SNathan Scott #include "xfs_attr_sf.h" 33c24b5dfaSDave Chinner #include "xfs_attr.h" 34239880efSDave Chinner #include "xfs_trans_space.h" 35239880efSDave Chinner #include "xfs_trans.h" 361da177e4SLinus Torvalds #include "xfs_buf_item.h" 37a844f451SNathan Scott #include "xfs_inode_item.h" 38a844f451SNathan Scott #include "xfs_ialloc.h" 39a844f451SNathan Scott #include "xfs_bmap.h" 4068988114SDave Chinner #include "xfs_bmap_util.h" 411da177e4SLinus Torvalds #include "xfs_error.h" 421da177e4SLinus Torvalds #include "xfs_quota.h" 432a82b8beSDavid Chinner #include "xfs_filestream.h" 4493848a99SChristoph Hellwig #include "xfs_cksum.h" 450b1b213fSChristoph Hellwig #include "xfs_trace.h" 4633479e05SDave Chinner #include "xfs_icache.h" 47c24b5dfaSDave Chinner #include "xfs_symlink.h" 48239880efSDave Chinner #include "xfs_trans_priv.h" 49239880efSDave Chinner #include "xfs_log.h" 50a4fbe6abSDave Chinner #include "xfs_bmap_btree.h" 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 558f04c47aSChristoph Hellwig * Used in xfs_itruncate_extents(). This is the maximum number of extents 561da177e4SLinus Torvalds * freed from a file in a single transaction. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds #define XFS_ITRUNC_MAX_EXTENTS 2 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); 611da177e4SLinus Torvalds 62ab297431SZhi Yong Wu STATIC int xfs_iunlink_remove(xfs_trans_t *, xfs_inode_t *); 63ab297431SZhi Yong Wu 642a0ec1d9SDave Chinner /* 652a0ec1d9SDave Chinner * helper function to extract extent size hint from inode 662a0ec1d9SDave Chinner */ 672a0ec1d9SDave Chinner xfs_extlen_t 682a0ec1d9SDave Chinner xfs_get_extsz_hint( 692a0ec1d9SDave Chinner struct xfs_inode *ip) 702a0ec1d9SDave Chinner { 712a0ec1d9SDave Chinner if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) 722a0ec1d9SDave Chinner return ip->i_d.di_extsize; 732a0ec1d9SDave Chinner if (XFS_IS_REALTIME_INODE(ip)) 742a0ec1d9SDave Chinner return ip->i_mount->m_sb.sb_rextsize; 752a0ec1d9SDave Chinner return 0; 762a0ec1d9SDave Chinner } 772a0ec1d9SDave Chinner 78fa96acadSDave Chinner /* 79efa70be1SChristoph Hellwig * These two are wrapper routines around the xfs_ilock() routine used to 80efa70be1SChristoph Hellwig * centralize some grungy code. They are used in places that wish to lock the 81efa70be1SChristoph Hellwig * inode solely for reading the extents. The reason these places can't just 82efa70be1SChristoph Hellwig * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to 83efa70be1SChristoph Hellwig * bringing in of the extents from disk for a file in b-tree format. If the 84efa70be1SChristoph Hellwig * inode is in b-tree format, then we need to lock the inode exclusively until 85efa70be1SChristoph Hellwig * the extents are read in. Locking it exclusively all the time would limit 86efa70be1SChristoph Hellwig * our parallelism unnecessarily, though. What we do instead is check to see 87efa70be1SChristoph Hellwig * if the extents have been read in yet, and only lock the inode exclusively 88efa70be1SChristoph Hellwig * if they have not. 89fa96acadSDave Chinner * 90efa70be1SChristoph Hellwig * The functions return a value which should be given to the corresponding 9101f4f327SChristoph Hellwig * xfs_iunlock() call. 92fa96acadSDave Chinner */ 93fa96acadSDave Chinner uint 94309ecac8SChristoph Hellwig xfs_ilock_data_map_shared( 95309ecac8SChristoph Hellwig struct xfs_inode *ip) 96fa96acadSDave Chinner { 97309ecac8SChristoph Hellwig uint lock_mode = XFS_ILOCK_SHARED; 98fa96acadSDave Chinner 99309ecac8SChristoph Hellwig if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE && 100309ecac8SChristoph Hellwig (ip->i_df.if_flags & XFS_IFEXTENTS) == 0) 101fa96acadSDave Chinner lock_mode = XFS_ILOCK_EXCL; 102fa96acadSDave Chinner xfs_ilock(ip, lock_mode); 103fa96acadSDave Chinner return lock_mode; 104fa96acadSDave Chinner } 105fa96acadSDave Chinner 106efa70be1SChristoph Hellwig uint 107efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared( 108efa70be1SChristoph Hellwig struct xfs_inode *ip) 109fa96acadSDave Chinner { 110efa70be1SChristoph Hellwig uint lock_mode = XFS_ILOCK_SHARED; 111efa70be1SChristoph Hellwig 112efa70be1SChristoph Hellwig if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE && 113efa70be1SChristoph Hellwig (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0) 114efa70be1SChristoph Hellwig lock_mode = XFS_ILOCK_EXCL; 115efa70be1SChristoph Hellwig xfs_ilock(ip, lock_mode); 116efa70be1SChristoph Hellwig return lock_mode; 117fa96acadSDave Chinner } 118fa96acadSDave Chinner 119fa96acadSDave Chinner /* 120fa96acadSDave Chinner * The xfs inode contains 2 locks: a multi-reader lock called the 121fa96acadSDave Chinner * i_iolock and a multi-reader lock called the i_lock. This routine 122fa96acadSDave Chinner * allows either or both of the locks to be obtained. 123fa96acadSDave Chinner * 124fa96acadSDave Chinner * The 2 locks should always be ordered so that the IO lock is 125fa96acadSDave Chinner * obtained first in order to prevent deadlock. 126fa96acadSDave Chinner * 127fa96acadSDave Chinner * ip -- the inode being locked 128fa96acadSDave Chinner * lock_flags -- this parameter indicates the inode's locks 129fa96acadSDave Chinner * to be locked. It can be: 130fa96acadSDave Chinner * XFS_IOLOCK_SHARED, 131fa96acadSDave Chinner * XFS_IOLOCK_EXCL, 132fa96acadSDave Chinner * XFS_ILOCK_SHARED, 133fa96acadSDave Chinner * XFS_ILOCK_EXCL, 134fa96acadSDave Chinner * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, 135fa96acadSDave Chinner * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, 136fa96acadSDave Chinner * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, 137fa96acadSDave Chinner * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL 138fa96acadSDave Chinner */ 139fa96acadSDave Chinner void 140fa96acadSDave Chinner xfs_ilock( 141fa96acadSDave Chinner xfs_inode_t *ip, 142fa96acadSDave Chinner uint lock_flags) 143fa96acadSDave Chinner { 144fa96acadSDave Chinner trace_xfs_ilock(ip, lock_flags, _RET_IP_); 145fa96acadSDave Chinner 146fa96acadSDave Chinner /* 147fa96acadSDave Chinner * You can't set both SHARED and EXCL for the same lock, 148fa96acadSDave Chinner * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, 149fa96acadSDave Chinner * and XFS_ILOCK_EXCL are valid values to set in lock_flags. 150fa96acadSDave Chinner */ 151fa96acadSDave Chinner ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != 152fa96acadSDave Chinner (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); 153fa96acadSDave Chinner ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != 154fa96acadSDave Chinner (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); 155fa96acadSDave Chinner ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); 156fa96acadSDave Chinner 157fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) 158fa96acadSDave Chinner mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); 159fa96acadSDave Chinner else if (lock_flags & XFS_IOLOCK_SHARED) 160fa96acadSDave Chinner mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); 161fa96acadSDave Chinner 162fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL) 163fa96acadSDave Chinner mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); 164fa96acadSDave Chinner else if (lock_flags & XFS_ILOCK_SHARED) 165fa96acadSDave Chinner mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); 166fa96acadSDave Chinner } 167fa96acadSDave Chinner 168fa96acadSDave Chinner /* 169fa96acadSDave Chinner * This is just like xfs_ilock(), except that the caller 170fa96acadSDave Chinner * is guaranteed not to sleep. It returns 1 if it gets 171fa96acadSDave Chinner * the requested locks and 0 otherwise. If the IO lock is 172fa96acadSDave Chinner * obtained but the inode lock cannot be, then the IO lock 173fa96acadSDave Chinner * is dropped before returning. 174fa96acadSDave Chinner * 175fa96acadSDave Chinner * ip -- the inode being locked 176fa96acadSDave Chinner * lock_flags -- this parameter indicates the inode's locks to be 177fa96acadSDave Chinner * to be locked. See the comment for xfs_ilock() for a list 178fa96acadSDave Chinner * of valid values. 179fa96acadSDave Chinner */ 180fa96acadSDave Chinner int 181fa96acadSDave Chinner xfs_ilock_nowait( 182fa96acadSDave Chinner xfs_inode_t *ip, 183fa96acadSDave Chinner uint lock_flags) 184fa96acadSDave Chinner { 185fa96acadSDave Chinner trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); 186fa96acadSDave Chinner 187fa96acadSDave Chinner /* 188fa96acadSDave Chinner * You can't set both SHARED and EXCL for the same lock, 189fa96acadSDave Chinner * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, 190fa96acadSDave Chinner * and XFS_ILOCK_EXCL are valid values to set in lock_flags. 191fa96acadSDave Chinner */ 192fa96acadSDave Chinner ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != 193fa96acadSDave Chinner (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); 194fa96acadSDave Chinner ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != 195fa96acadSDave Chinner (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); 196fa96acadSDave Chinner ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); 197fa96acadSDave Chinner 198fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) { 199fa96acadSDave Chinner if (!mrtryupdate(&ip->i_iolock)) 200fa96acadSDave Chinner goto out; 201fa96acadSDave Chinner } else if (lock_flags & XFS_IOLOCK_SHARED) { 202fa96acadSDave Chinner if (!mrtryaccess(&ip->i_iolock)) 203fa96acadSDave Chinner goto out; 204fa96acadSDave Chinner } 205fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL) { 206fa96acadSDave Chinner if (!mrtryupdate(&ip->i_lock)) 207fa96acadSDave Chinner goto out_undo_iolock; 208fa96acadSDave Chinner } else if (lock_flags & XFS_ILOCK_SHARED) { 209fa96acadSDave Chinner if (!mrtryaccess(&ip->i_lock)) 210fa96acadSDave Chinner goto out_undo_iolock; 211fa96acadSDave Chinner } 212fa96acadSDave Chinner return 1; 213fa96acadSDave Chinner 214fa96acadSDave Chinner out_undo_iolock: 215fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) 216fa96acadSDave Chinner mrunlock_excl(&ip->i_iolock); 217fa96acadSDave Chinner else if (lock_flags & XFS_IOLOCK_SHARED) 218fa96acadSDave Chinner mrunlock_shared(&ip->i_iolock); 219fa96acadSDave Chinner out: 220fa96acadSDave Chinner return 0; 221fa96acadSDave Chinner } 222fa96acadSDave Chinner 223fa96acadSDave Chinner /* 224fa96acadSDave Chinner * xfs_iunlock() is used to drop the inode locks acquired with 225fa96acadSDave Chinner * xfs_ilock() and xfs_ilock_nowait(). The caller must pass 226fa96acadSDave Chinner * in the flags given to xfs_ilock() or xfs_ilock_nowait() so 227fa96acadSDave Chinner * that we know which locks to drop. 228fa96acadSDave Chinner * 229fa96acadSDave Chinner * ip -- the inode being unlocked 230fa96acadSDave Chinner * lock_flags -- this parameter indicates the inode's locks to be 231fa96acadSDave Chinner * to be unlocked. See the comment for xfs_ilock() for a list 232fa96acadSDave Chinner * of valid values for this parameter. 233fa96acadSDave Chinner * 234fa96acadSDave Chinner */ 235fa96acadSDave Chinner void 236fa96acadSDave Chinner xfs_iunlock( 237fa96acadSDave Chinner xfs_inode_t *ip, 238fa96acadSDave Chinner uint lock_flags) 239fa96acadSDave Chinner { 240fa96acadSDave Chinner /* 241fa96acadSDave Chinner * You can't set both SHARED and EXCL for the same lock, 242fa96acadSDave Chinner * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, 243fa96acadSDave Chinner * and XFS_ILOCK_EXCL are valid values to set in lock_flags. 244fa96acadSDave Chinner */ 245fa96acadSDave Chinner ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != 246fa96acadSDave Chinner (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); 247fa96acadSDave Chinner ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != 248fa96acadSDave Chinner (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); 249fa96acadSDave Chinner ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); 250fa96acadSDave Chinner ASSERT(lock_flags != 0); 251fa96acadSDave Chinner 252fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) 253fa96acadSDave Chinner mrunlock_excl(&ip->i_iolock); 254fa96acadSDave Chinner else if (lock_flags & XFS_IOLOCK_SHARED) 255fa96acadSDave Chinner mrunlock_shared(&ip->i_iolock); 256fa96acadSDave Chinner 257fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL) 258fa96acadSDave Chinner mrunlock_excl(&ip->i_lock); 259fa96acadSDave Chinner else if (lock_flags & XFS_ILOCK_SHARED) 260fa96acadSDave Chinner mrunlock_shared(&ip->i_lock); 261fa96acadSDave Chinner 262fa96acadSDave Chinner trace_xfs_iunlock(ip, lock_flags, _RET_IP_); 263fa96acadSDave Chinner } 264fa96acadSDave Chinner 265fa96acadSDave Chinner /* 266fa96acadSDave Chinner * give up write locks. the i/o lock cannot be held nested 267fa96acadSDave Chinner * if it is being demoted. 268fa96acadSDave Chinner */ 269fa96acadSDave Chinner void 270fa96acadSDave Chinner xfs_ilock_demote( 271fa96acadSDave Chinner xfs_inode_t *ip, 272fa96acadSDave Chinner uint lock_flags) 273fa96acadSDave Chinner { 274fa96acadSDave Chinner ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); 275fa96acadSDave Chinner ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); 276fa96acadSDave Chinner 277fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL) 278fa96acadSDave Chinner mrdemote(&ip->i_lock); 279fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) 280fa96acadSDave Chinner mrdemote(&ip->i_iolock); 281fa96acadSDave Chinner 282fa96acadSDave Chinner trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); 283fa96acadSDave Chinner } 284fa96acadSDave Chinner 285742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 286fa96acadSDave Chinner int 287fa96acadSDave Chinner xfs_isilocked( 288fa96acadSDave Chinner xfs_inode_t *ip, 289fa96acadSDave Chinner uint lock_flags) 290fa96acadSDave Chinner { 291fa96acadSDave Chinner if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { 292fa96acadSDave Chinner if (!(lock_flags & XFS_ILOCK_SHARED)) 293fa96acadSDave Chinner return !!ip->i_lock.mr_writer; 294fa96acadSDave Chinner return rwsem_is_locked(&ip->i_lock.mr_lock); 295fa96acadSDave Chinner } 296fa96acadSDave Chinner 297fa96acadSDave Chinner if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { 298fa96acadSDave Chinner if (!(lock_flags & XFS_IOLOCK_SHARED)) 299fa96acadSDave Chinner return !!ip->i_iolock.mr_writer; 300fa96acadSDave Chinner return rwsem_is_locked(&ip->i_iolock.mr_lock); 301fa96acadSDave Chinner } 302fa96acadSDave Chinner 303fa96acadSDave Chinner ASSERT(0); 304fa96acadSDave Chinner return 0; 305fa96acadSDave Chinner } 306fa96acadSDave Chinner #endif 307fa96acadSDave Chinner 308c24b5dfaSDave Chinner #ifdef DEBUG 309c24b5dfaSDave Chinner int xfs_locked_n; 310c24b5dfaSDave Chinner int xfs_small_retries; 311c24b5dfaSDave Chinner int xfs_middle_retries; 312c24b5dfaSDave Chinner int xfs_lots_retries; 313c24b5dfaSDave Chinner int xfs_lock_delays; 314c24b5dfaSDave Chinner #endif 315c24b5dfaSDave Chinner 316c24b5dfaSDave Chinner /* 317c24b5dfaSDave Chinner * Bump the subclass so xfs_lock_inodes() acquires each lock with 318c24b5dfaSDave Chinner * a different value 319c24b5dfaSDave Chinner */ 320c24b5dfaSDave Chinner static inline int 321c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass) 322c24b5dfaSDave Chinner { 323c24b5dfaSDave Chinner if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) 324c24b5dfaSDave Chinner lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT; 325c24b5dfaSDave Chinner if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) 326c24b5dfaSDave Chinner lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT; 327c24b5dfaSDave Chinner 328c24b5dfaSDave Chinner return lock_mode; 329c24b5dfaSDave Chinner } 330c24b5dfaSDave Chinner 331c24b5dfaSDave Chinner /* 33295afcf5cSDave Chinner * The following routine will lock n inodes in exclusive mode. We assume the 33395afcf5cSDave Chinner * caller calls us with the inodes in i_ino order. 334c24b5dfaSDave Chinner * 33595afcf5cSDave Chinner * We need to detect deadlock where an inode that we lock is in the AIL and we 33695afcf5cSDave Chinner * start waiting for another inode that is locked by a thread in a long running 33795afcf5cSDave Chinner * transaction (such as truncate). This can result in deadlock since the long 33895afcf5cSDave Chinner * running trans might need to wait for the inode we just locked in order to 33995afcf5cSDave Chinner * push the tail and free space in the log. 340c24b5dfaSDave Chinner */ 341c24b5dfaSDave Chinner void 342c24b5dfaSDave Chinner xfs_lock_inodes( 343c24b5dfaSDave Chinner xfs_inode_t **ips, 344c24b5dfaSDave Chinner int inodes, 345c24b5dfaSDave Chinner uint lock_mode) 346c24b5dfaSDave Chinner { 347c24b5dfaSDave Chinner int attempts = 0, i, j, try_lock; 348c24b5dfaSDave Chinner xfs_log_item_t *lp; 349c24b5dfaSDave Chinner 35095afcf5cSDave Chinner /* currently supports between 2 and 5 inodes */ 35195afcf5cSDave Chinner ASSERT(ips && inodes >= 2 && inodes <= 5); 352c24b5dfaSDave Chinner 353c24b5dfaSDave Chinner try_lock = 0; 354c24b5dfaSDave Chinner i = 0; 355c24b5dfaSDave Chinner again: 356c24b5dfaSDave Chinner for (; i < inodes; i++) { 357c24b5dfaSDave Chinner ASSERT(ips[i]); 358c24b5dfaSDave Chinner 359c24b5dfaSDave Chinner if (i && (ips[i] == ips[i - 1])) /* Already locked */ 360c24b5dfaSDave Chinner continue; 361c24b5dfaSDave Chinner 362c24b5dfaSDave Chinner /* 36395afcf5cSDave Chinner * If try_lock is not set yet, make sure all locked inodes are 36495afcf5cSDave Chinner * not in the AIL. If any are, set try_lock to be used later. 365c24b5dfaSDave Chinner */ 366c24b5dfaSDave Chinner if (!try_lock) { 367c24b5dfaSDave Chinner for (j = (i - 1); j >= 0 && !try_lock; j--) { 368c24b5dfaSDave Chinner lp = (xfs_log_item_t *)ips[j]->i_itemp; 36995afcf5cSDave Chinner if (lp && (lp->li_flags & XFS_LI_IN_AIL)) 370c24b5dfaSDave Chinner try_lock++; 371c24b5dfaSDave Chinner } 372c24b5dfaSDave Chinner } 373c24b5dfaSDave Chinner 374c24b5dfaSDave Chinner /* 375c24b5dfaSDave Chinner * If any of the previous locks we have locked is in the AIL, 376c24b5dfaSDave Chinner * we must TRY to get the second and subsequent locks. If 377c24b5dfaSDave Chinner * we can't get any, we must release all we have 378c24b5dfaSDave Chinner * and try again. 379c24b5dfaSDave Chinner */ 38095afcf5cSDave Chinner if (!try_lock) { 38195afcf5cSDave Chinner xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); 38295afcf5cSDave Chinner continue; 38395afcf5cSDave Chinner } 384c24b5dfaSDave Chinner 38595afcf5cSDave Chinner /* try_lock means we have an inode locked that is in the AIL. */ 386c24b5dfaSDave Chinner ASSERT(i != 0); 38795afcf5cSDave Chinner if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) 38895afcf5cSDave Chinner continue; 38995afcf5cSDave Chinner 39095afcf5cSDave Chinner /* 39195afcf5cSDave Chinner * Unlock all previous guys and try again. xfs_iunlock will try 39295afcf5cSDave Chinner * to push the tail if the inode is in the AIL. 39395afcf5cSDave Chinner */ 394c24b5dfaSDave Chinner attempts++; 395c24b5dfaSDave Chinner for (j = i - 1; j >= 0; j--) { 396c24b5dfaSDave Chinner /* 39795afcf5cSDave Chinner * Check to see if we've already unlocked this one. Not 39895afcf5cSDave Chinner * the first one going back, and the inode ptr is the 39995afcf5cSDave Chinner * same. 400c24b5dfaSDave Chinner */ 40195afcf5cSDave Chinner if (j != (i - 1) && ips[j] == ips[j + 1]) 402c24b5dfaSDave Chinner continue; 403c24b5dfaSDave Chinner 404c24b5dfaSDave Chinner xfs_iunlock(ips[j], lock_mode); 405c24b5dfaSDave Chinner } 406c24b5dfaSDave Chinner 407c24b5dfaSDave Chinner if ((attempts % 5) == 0) { 408c24b5dfaSDave Chinner delay(1); /* Don't just spin the CPU */ 409c24b5dfaSDave Chinner #ifdef DEBUG 410c24b5dfaSDave Chinner xfs_lock_delays++; 411c24b5dfaSDave Chinner #endif 412c24b5dfaSDave Chinner } 413c24b5dfaSDave Chinner i = 0; 414c24b5dfaSDave Chinner try_lock = 0; 415c24b5dfaSDave Chinner goto again; 416c24b5dfaSDave Chinner } 417c24b5dfaSDave Chinner 418c24b5dfaSDave Chinner #ifdef DEBUG 419c24b5dfaSDave Chinner if (attempts) { 420c24b5dfaSDave Chinner if (attempts < 5) xfs_small_retries++; 421c24b5dfaSDave Chinner else if (attempts < 100) xfs_middle_retries++; 422c24b5dfaSDave Chinner else xfs_lots_retries++; 423c24b5dfaSDave Chinner } else { 424c24b5dfaSDave Chinner xfs_locked_n++; 425c24b5dfaSDave Chinner } 426c24b5dfaSDave Chinner #endif 427c24b5dfaSDave Chinner } 428c24b5dfaSDave Chinner 429c24b5dfaSDave Chinner /* 430c24b5dfaSDave Chinner * xfs_lock_two_inodes() can only be used to lock one type of lock 431c24b5dfaSDave Chinner * at a time - the iolock or the ilock, but not both at once. If 432c24b5dfaSDave Chinner * we lock both at once, lockdep will report false positives saying 433c24b5dfaSDave Chinner * we have violated locking orders. 434c24b5dfaSDave Chinner */ 435c24b5dfaSDave Chinner void 436c24b5dfaSDave Chinner xfs_lock_two_inodes( 437c24b5dfaSDave Chinner xfs_inode_t *ip0, 438c24b5dfaSDave Chinner xfs_inode_t *ip1, 439c24b5dfaSDave Chinner uint lock_mode) 440c24b5dfaSDave Chinner { 441c24b5dfaSDave Chinner xfs_inode_t *temp; 442c24b5dfaSDave Chinner int attempts = 0; 443c24b5dfaSDave Chinner xfs_log_item_t *lp; 444c24b5dfaSDave Chinner 445c24b5dfaSDave Chinner if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) 446c24b5dfaSDave Chinner ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0); 447c24b5dfaSDave Chinner ASSERT(ip0->i_ino != ip1->i_ino); 448c24b5dfaSDave Chinner 449c24b5dfaSDave Chinner if (ip0->i_ino > ip1->i_ino) { 450c24b5dfaSDave Chinner temp = ip0; 451c24b5dfaSDave Chinner ip0 = ip1; 452c24b5dfaSDave Chinner ip1 = temp; 453c24b5dfaSDave Chinner } 454c24b5dfaSDave Chinner 455c24b5dfaSDave Chinner again: 456c24b5dfaSDave Chinner xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0)); 457c24b5dfaSDave Chinner 458c24b5dfaSDave Chinner /* 459c24b5dfaSDave Chinner * If the first lock we have locked is in the AIL, we must TRY to get 460c24b5dfaSDave Chinner * the second lock. If we can't get it, we must release the first one 461c24b5dfaSDave Chinner * and try again. 462c24b5dfaSDave Chinner */ 463c24b5dfaSDave Chinner lp = (xfs_log_item_t *)ip0->i_itemp; 464c24b5dfaSDave Chinner if (lp && (lp->li_flags & XFS_LI_IN_AIL)) { 465c24b5dfaSDave Chinner if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) { 466c24b5dfaSDave Chinner xfs_iunlock(ip0, lock_mode); 467c24b5dfaSDave Chinner if ((++attempts % 5) == 0) 468c24b5dfaSDave Chinner delay(1); /* Don't just spin the CPU */ 469c24b5dfaSDave Chinner goto again; 470c24b5dfaSDave Chinner } 471c24b5dfaSDave Chinner } else { 472c24b5dfaSDave Chinner xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1)); 473c24b5dfaSDave Chinner } 474c24b5dfaSDave Chinner } 475c24b5dfaSDave Chinner 476c24b5dfaSDave Chinner 477fa96acadSDave Chinner void 478fa96acadSDave Chinner __xfs_iflock( 479fa96acadSDave Chinner struct xfs_inode *ip) 480fa96acadSDave Chinner { 481fa96acadSDave Chinner wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); 482fa96acadSDave Chinner DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); 483fa96acadSDave Chinner 484fa96acadSDave Chinner do { 485fa96acadSDave Chinner prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 486fa96acadSDave Chinner if (xfs_isiflocked(ip)) 487fa96acadSDave Chinner io_schedule(); 488fa96acadSDave Chinner } while (!xfs_iflock_nowait(ip)); 489fa96acadSDave Chinner 490fa96acadSDave Chinner finish_wait(wq, &wait.wait); 491fa96acadSDave Chinner } 492fa96acadSDave Chinner 4931da177e4SLinus Torvalds STATIC uint 4941da177e4SLinus Torvalds _xfs_dic2xflags( 4951da177e4SLinus Torvalds __uint16_t di_flags) 4961da177e4SLinus Torvalds { 4971da177e4SLinus Torvalds uint flags = 0; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_ANY) { 5001da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_REALTIME) 5011da177e4SLinus Torvalds flags |= XFS_XFLAG_REALTIME; 5021da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_PREALLOC) 5031da177e4SLinus Torvalds flags |= XFS_XFLAG_PREALLOC; 5041da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_IMMUTABLE) 5051da177e4SLinus Torvalds flags |= XFS_XFLAG_IMMUTABLE; 5061da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_APPEND) 5071da177e4SLinus Torvalds flags |= XFS_XFLAG_APPEND; 5081da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_SYNC) 5091da177e4SLinus Torvalds flags |= XFS_XFLAG_SYNC; 5101da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_NOATIME) 5111da177e4SLinus Torvalds flags |= XFS_XFLAG_NOATIME; 5121da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_NODUMP) 5131da177e4SLinus Torvalds flags |= XFS_XFLAG_NODUMP; 5141da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_RTINHERIT) 5151da177e4SLinus Torvalds flags |= XFS_XFLAG_RTINHERIT; 5161da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_PROJINHERIT) 5171da177e4SLinus Torvalds flags |= XFS_XFLAG_PROJINHERIT; 5181da177e4SLinus Torvalds if (di_flags & XFS_DIFLAG_NOSYMLINKS) 5191da177e4SLinus Torvalds flags |= XFS_XFLAG_NOSYMLINKS; 520dd9f438eSNathan Scott if (di_flags & XFS_DIFLAG_EXTSIZE) 521dd9f438eSNathan Scott flags |= XFS_XFLAG_EXTSIZE; 522dd9f438eSNathan Scott if (di_flags & XFS_DIFLAG_EXTSZINHERIT) 523dd9f438eSNathan Scott flags |= XFS_XFLAG_EXTSZINHERIT; 524d3446eacSBarry Naujok if (di_flags & XFS_DIFLAG_NODEFRAG) 525d3446eacSBarry Naujok flags |= XFS_XFLAG_NODEFRAG; 5262a82b8beSDavid Chinner if (di_flags & XFS_DIFLAG_FILESTREAM) 5272a82b8beSDavid Chinner flags |= XFS_XFLAG_FILESTREAM; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds return flags; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds uint 5341da177e4SLinus Torvalds xfs_ip2xflags( 5351da177e4SLinus Torvalds xfs_inode_t *ip) 5361da177e4SLinus Torvalds { 537347d1c01SChristoph Hellwig xfs_icdinode_t *dic = &ip->i_d; 5381da177e4SLinus Torvalds 539a916e2bdSNathan Scott return _xfs_dic2xflags(dic->di_flags) | 54045ba598eSChristoph Hellwig (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds uint 5441da177e4SLinus Torvalds xfs_dic2xflags( 54545ba598eSChristoph Hellwig xfs_dinode_t *dip) 5461da177e4SLinus Torvalds { 54781591fe2SChristoph Hellwig return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | 54845ba598eSChristoph Hellwig (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* 552c24b5dfaSDave Chinner * Lookups up an inode from "name". If ci_name is not NULL, then a CI match 553c24b5dfaSDave Chinner * is allowed, otherwise it has to be an exact match. If a CI match is found, 554c24b5dfaSDave Chinner * ci_name->name will point to a the actual name (caller must free) or 555c24b5dfaSDave Chinner * will be set to NULL if an exact match is found. 556c24b5dfaSDave Chinner */ 557c24b5dfaSDave Chinner int 558c24b5dfaSDave Chinner xfs_lookup( 559c24b5dfaSDave Chinner xfs_inode_t *dp, 560c24b5dfaSDave Chinner struct xfs_name *name, 561c24b5dfaSDave Chinner xfs_inode_t **ipp, 562c24b5dfaSDave Chinner struct xfs_name *ci_name) 563c24b5dfaSDave Chinner { 564c24b5dfaSDave Chinner xfs_ino_t inum; 565c24b5dfaSDave Chinner int error; 566c24b5dfaSDave Chinner uint lock_mode; 567c24b5dfaSDave Chinner 568c24b5dfaSDave Chinner trace_xfs_lookup(dp, name); 569c24b5dfaSDave Chinner 570c24b5dfaSDave Chinner if (XFS_FORCED_SHUTDOWN(dp->i_mount)) 5712451337dSDave Chinner return -EIO; 572c24b5dfaSDave Chinner 573309ecac8SChristoph Hellwig lock_mode = xfs_ilock_data_map_shared(dp); 574c24b5dfaSDave Chinner error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); 57501f4f327SChristoph Hellwig xfs_iunlock(dp, lock_mode); 576c24b5dfaSDave Chinner 577c24b5dfaSDave Chinner if (error) 578c24b5dfaSDave Chinner goto out; 579c24b5dfaSDave Chinner 580c24b5dfaSDave Chinner error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); 581c24b5dfaSDave Chinner if (error) 582c24b5dfaSDave Chinner goto out_free_name; 583c24b5dfaSDave Chinner 584c24b5dfaSDave Chinner return 0; 585c24b5dfaSDave Chinner 586c24b5dfaSDave Chinner out_free_name: 587c24b5dfaSDave Chinner if (ci_name) 588c24b5dfaSDave Chinner kmem_free(ci_name->name); 589c24b5dfaSDave Chinner out: 590c24b5dfaSDave Chinner *ipp = NULL; 591c24b5dfaSDave Chinner return error; 592c24b5dfaSDave Chinner } 593c24b5dfaSDave Chinner 594c24b5dfaSDave Chinner /* 5951da177e4SLinus Torvalds * Allocate an inode on disk and return a copy of its in-core version. 5961da177e4SLinus Torvalds * The in-core inode is locked exclusively. Set mode, nlink, and rdev 5971da177e4SLinus Torvalds * appropriately within the inode. The uid and gid for the inode are 5981da177e4SLinus Torvalds * set according to the contents of the given cred structure. 5991da177e4SLinus Torvalds * 6001da177e4SLinus Torvalds * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() 601cd856db6SCarlos Maiolino * has a free inode available, call xfs_iget() to obtain the in-core 602cd856db6SCarlos Maiolino * version of the allocated inode. Finally, fill in the inode and 603cd856db6SCarlos Maiolino * log its initial contents. In this case, ialloc_context would be 604cd856db6SCarlos Maiolino * set to NULL. 6051da177e4SLinus Torvalds * 606cd856db6SCarlos Maiolino * If xfs_dialloc() does not have an available inode, it will replenish 607cd856db6SCarlos Maiolino * its supply by doing an allocation. Since we can only do one 608cd856db6SCarlos Maiolino * allocation within a transaction without deadlocks, we must commit 609cd856db6SCarlos Maiolino * the current transaction before returning the inode itself. 610cd856db6SCarlos Maiolino * In this case, therefore, we will set ialloc_context and return. 6111da177e4SLinus Torvalds * The caller should then commit the current transaction, start a new 6121da177e4SLinus Torvalds * transaction, and call xfs_ialloc() again to actually get the inode. 6131da177e4SLinus Torvalds * 6141da177e4SLinus Torvalds * To ensure that some other process does not grab the inode that 6151da177e4SLinus Torvalds * was allocated during the first call to xfs_ialloc(), this routine 6161da177e4SLinus Torvalds * also returns the [locked] bp pointing to the head of the freelist 6171da177e4SLinus Torvalds * as ialloc_context. The caller should hold this buffer across 6181da177e4SLinus Torvalds * the commit and pass it back into this routine on the second call. 619b11f94d5SDavid Chinner * 620b11f94d5SDavid Chinner * If we are allocating quota inodes, we do not have a parent inode 621b11f94d5SDavid Chinner * to attach to or associate with (i.e. pip == NULL) because they 622b11f94d5SDavid Chinner * are not linked into the directory structure - they are attached 623b11f94d5SDavid Chinner * directly to the superblock - and so have no parent. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds int 6261da177e4SLinus Torvalds xfs_ialloc( 6271da177e4SLinus Torvalds xfs_trans_t *tp, 6281da177e4SLinus Torvalds xfs_inode_t *pip, 629576b1d67SAl Viro umode_t mode, 63031b084aeSNathan Scott xfs_nlink_t nlink, 6311da177e4SLinus Torvalds xfs_dev_t rdev, 6326743099cSArkadiusz Mi?kiewicz prid_t prid, 6331da177e4SLinus Torvalds int okalloc, 6341da177e4SLinus Torvalds xfs_buf_t **ialloc_context, 6351da177e4SLinus Torvalds xfs_inode_t **ipp) 6361da177e4SLinus Torvalds { 63793848a99SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp; 6381da177e4SLinus Torvalds xfs_ino_t ino; 6391da177e4SLinus Torvalds xfs_inode_t *ip; 6401da177e4SLinus Torvalds uint flags; 6411da177e4SLinus Torvalds int error; 642e076b0f3SDave Chinner struct timespec tv; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* 6451da177e4SLinus Torvalds * Call the space management code to pick 6461da177e4SLinus Torvalds * the on-disk inode to be allocated. 6471da177e4SLinus Torvalds */ 648b11f94d5SDavid Chinner error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, 64908358906SChristoph Hellwig ialloc_context, &ino); 650bf904248SDavid Chinner if (error) 6511da177e4SLinus Torvalds return error; 65208358906SChristoph Hellwig if (*ialloc_context || ino == NULLFSINO) { 6531da177e4SLinus Torvalds *ipp = NULL; 6541da177e4SLinus Torvalds return 0; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds ASSERT(*ialloc_context == NULL); 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* 6591da177e4SLinus Torvalds * Get the in-core inode with the lock held exclusively. 6601da177e4SLinus Torvalds * This is because we're setting fields here we need 6611da177e4SLinus Torvalds * to prevent others from looking at until we're done. 6621da177e4SLinus Torvalds */ 66393848a99SChristoph Hellwig error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, 664ec3ba85fSChristoph Hellwig XFS_ILOCK_EXCL, &ip); 665bf904248SDavid Chinner if (error) 6661da177e4SLinus Torvalds return error; 6671da177e4SLinus Torvalds ASSERT(ip != NULL); 6681da177e4SLinus Torvalds 669263997a6SDave Chinner /* 670263997a6SDave Chinner * We always convert v1 inodes to v2 now - we only support filesystems 671263997a6SDave Chinner * with >= v2 inode capability, so there is no reason for ever leaving 672263997a6SDave Chinner * an inode in v1 format. 673263997a6SDave Chinner */ 674263997a6SDave Chinner if (ip->i_d.di_version == 1) 675263997a6SDave Chinner ip->i_d.di_version = 2; 676263997a6SDave Chinner 677576b1d67SAl Viro ip->i_d.di_mode = mode; 6781da177e4SLinus Torvalds ip->i_d.di_onlink = 0; 6791da177e4SLinus Torvalds ip->i_d.di_nlink = nlink; 6801da177e4SLinus Torvalds ASSERT(ip->i_d.di_nlink == nlink); 6817aab1b28SDwight Engen ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid()); 6827aab1b28SDwight Engen ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid()); 6836743099cSArkadiusz Mi?kiewicz xfs_set_projid(ip, prid); 6841da177e4SLinus Torvalds memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); 6851da177e4SLinus Torvalds 686bd186aa9SChristoph Hellwig if (pip && XFS_INHERIT_GID(pip)) { 6871da177e4SLinus Torvalds ip->i_d.di_gid = pip->i_d.di_gid; 688abbede1bSAl Viro if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { 6891da177e4SLinus Torvalds ip->i_d.di_mode |= S_ISGID; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds /* 6941da177e4SLinus Torvalds * If the group ID of the new file does not match the effective group 6951da177e4SLinus Torvalds * ID or one of the supplementary group IDs, the S_ISGID bit is cleared 6961da177e4SLinus Torvalds * (and only if the irix_sgid_inherit compatibility variable is set). 6971da177e4SLinus Torvalds */ 6981da177e4SLinus Torvalds if ((irix_sgid_inherit) && 6991da177e4SLinus Torvalds (ip->i_d.di_mode & S_ISGID) && 7007aab1b28SDwight Engen (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) { 7011da177e4SLinus Torvalds ip->i_d.di_mode &= ~S_ISGID; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds ip->i_d.di_size = 0; 7051da177e4SLinus Torvalds ip->i_d.di_nextents = 0; 7061da177e4SLinus Torvalds ASSERT(ip->i_d.di_nblocks == 0); 707dff35fd4SChristoph Hellwig 708e076b0f3SDave Chinner tv = current_fs_time(mp->m_super); 709dff35fd4SChristoph Hellwig ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; 710dff35fd4SChristoph Hellwig ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; 711dff35fd4SChristoph Hellwig ip->i_d.di_atime = ip->i_d.di_mtime; 712dff35fd4SChristoph Hellwig ip->i_d.di_ctime = ip->i_d.di_mtime; 713dff35fd4SChristoph Hellwig 7141da177e4SLinus Torvalds /* 7151da177e4SLinus Torvalds * di_gen will have been taken care of in xfs_iread. 7161da177e4SLinus Torvalds */ 7171da177e4SLinus Torvalds ip->i_d.di_extsize = 0; 7181da177e4SLinus Torvalds ip->i_d.di_dmevmask = 0; 7191da177e4SLinus Torvalds ip->i_d.di_dmstate = 0; 7201da177e4SLinus Torvalds ip->i_d.di_flags = 0; 72193848a99SChristoph Hellwig 72293848a99SChristoph Hellwig if (ip->i_d.di_version == 3) { 72393848a99SChristoph Hellwig ASSERT(ip->i_d.di_ino == ino); 72493848a99SChristoph Hellwig ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid)); 72593848a99SChristoph Hellwig ip->i_d.di_crc = 0; 72693848a99SChristoph Hellwig ip->i_d.di_changecount = 1; 72793848a99SChristoph Hellwig ip->i_d.di_lsn = 0; 72893848a99SChristoph Hellwig ip->i_d.di_flags2 = 0; 72993848a99SChristoph Hellwig memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2)); 73093848a99SChristoph Hellwig ip->i_d.di_crtime = ip->i_d.di_mtime; 73193848a99SChristoph Hellwig } 73293848a99SChristoph Hellwig 73393848a99SChristoph Hellwig 7341da177e4SLinus Torvalds flags = XFS_ILOG_CORE; 7351da177e4SLinus Torvalds switch (mode & S_IFMT) { 7361da177e4SLinus Torvalds case S_IFIFO: 7371da177e4SLinus Torvalds case S_IFCHR: 7381da177e4SLinus Torvalds case S_IFBLK: 7391da177e4SLinus Torvalds case S_IFSOCK: 7401da177e4SLinus Torvalds ip->i_d.di_format = XFS_DINODE_FMT_DEV; 7411da177e4SLinus Torvalds ip->i_df.if_u2.if_rdev = rdev; 7421da177e4SLinus Torvalds ip->i_df.if_flags = 0; 7431da177e4SLinus Torvalds flags |= XFS_ILOG_DEV; 7441da177e4SLinus Torvalds break; 7451da177e4SLinus Torvalds case S_IFREG: 7461da177e4SLinus Torvalds case S_IFDIR: 747b11f94d5SDavid Chinner if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { 748365ca83dSNathan Scott uint di_flags = 0; 749365ca83dSNathan Scott 750abbede1bSAl Viro if (S_ISDIR(mode)) { 751365ca83dSNathan Scott if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) 752365ca83dSNathan Scott di_flags |= XFS_DIFLAG_RTINHERIT; 753dd9f438eSNathan Scott if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { 754dd9f438eSNathan Scott di_flags |= XFS_DIFLAG_EXTSZINHERIT; 755dd9f438eSNathan Scott ip->i_d.di_extsize = pip->i_d.di_extsize; 756dd9f438eSNathan Scott } 7579336e3a7SDave Chinner if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) 7589336e3a7SDave Chinner di_flags |= XFS_DIFLAG_PROJINHERIT; 759abbede1bSAl Viro } else if (S_ISREG(mode)) { 760613d7043SChristoph Hellwig if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) 761365ca83dSNathan Scott di_flags |= XFS_DIFLAG_REALTIME; 762dd9f438eSNathan Scott if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { 763dd9f438eSNathan Scott di_flags |= XFS_DIFLAG_EXTSIZE; 764dd9f438eSNathan Scott ip->i_d.di_extsize = pip->i_d.di_extsize; 765dd9f438eSNathan Scott } 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && 7681da177e4SLinus Torvalds xfs_inherit_noatime) 769365ca83dSNathan Scott di_flags |= XFS_DIFLAG_NOATIME; 7701da177e4SLinus Torvalds if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && 7711da177e4SLinus Torvalds xfs_inherit_nodump) 772365ca83dSNathan Scott di_flags |= XFS_DIFLAG_NODUMP; 7731da177e4SLinus Torvalds if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && 7741da177e4SLinus Torvalds xfs_inherit_sync) 775365ca83dSNathan Scott di_flags |= XFS_DIFLAG_SYNC; 7761da177e4SLinus Torvalds if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && 7771da177e4SLinus Torvalds xfs_inherit_nosymlinks) 778365ca83dSNathan Scott di_flags |= XFS_DIFLAG_NOSYMLINKS; 779d3446eacSBarry Naujok if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && 780d3446eacSBarry Naujok xfs_inherit_nodefrag) 781d3446eacSBarry Naujok di_flags |= XFS_DIFLAG_NODEFRAG; 7822a82b8beSDavid Chinner if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) 7832a82b8beSDavid Chinner di_flags |= XFS_DIFLAG_FILESTREAM; 784365ca83dSNathan Scott ip->i_d.di_flags |= di_flags; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds /* FALLTHROUGH */ 7871da177e4SLinus Torvalds case S_IFLNK: 7881da177e4SLinus Torvalds ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; 7891da177e4SLinus Torvalds ip->i_df.if_flags = XFS_IFEXTENTS; 7901da177e4SLinus Torvalds ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; 7911da177e4SLinus Torvalds ip->i_df.if_u1.if_extents = NULL; 7921da177e4SLinus Torvalds break; 7931da177e4SLinus Torvalds default: 7941da177e4SLinus Torvalds ASSERT(0); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Attribute fork settings for new inode. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; 8001da177e4SLinus Torvalds ip->i_d.di_anextents = 0; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* 8031da177e4SLinus Torvalds * Log the new values stuffed into the inode. 8041da177e4SLinus Torvalds */ 805ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 8061da177e4SLinus Torvalds xfs_trans_log_inode(tp, ip, flags); 8071da177e4SLinus Torvalds 80858c90473SDave Chinner /* now that we have an i_mode we can setup the inode structure */ 80941be8bedSChristoph Hellwig xfs_setup_inode(ip); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds *ipp = ip; 8121da177e4SLinus Torvalds return 0; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 815e546cb79SDave Chinner /* 816e546cb79SDave Chinner * Allocates a new inode from disk and return a pointer to the 817e546cb79SDave Chinner * incore copy. This routine will internally commit the current 818e546cb79SDave Chinner * transaction and allocate a new one if the Space Manager needed 819e546cb79SDave Chinner * to do an allocation to replenish the inode free-list. 820e546cb79SDave Chinner * 821e546cb79SDave Chinner * This routine is designed to be called from xfs_create and 822e546cb79SDave Chinner * xfs_create_dir. 823e546cb79SDave Chinner * 824e546cb79SDave Chinner */ 825e546cb79SDave Chinner int 826e546cb79SDave Chinner xfs_dir_ialloc( 827e546cb79SDave Chinner xfs_trans_t **tpp, /* input: current transaction; 828e546cb79SDave Chinner output: may be a new transaction. */ 829e546cb79SDave Chinner xfs_inode_t *dp, /* directory within whose allocate 830e546cb79SDave Chinner the inode. */ 831e546cb79SDave Chinner umode_t mode, 832e546cb79SDave Chinner xfs_nlink_t nlink, 833e546cb79SDave Chinner xfs_dev_t rdev, 834e546cb79SDave Chinner prid_t prid, /* project id */ 835e546cb79SDave Chinner int okalloc, /* ok to allocate new space */ 836e546cb79SDave Chinner xfs_inode_t **ipp, /* pointer to inode; it will be 837e546cb79SDave Chinner locked. */ 838e546cb79SDave Chinner int *committed) 839e546cb79SDave Chinner 840e546cb79SDave Chinner { 841e546cb79SDave Chinner xfs_trans_t *tp; 842e546cb79SDave Chinner xfs_trans_t *ntp; 843e546cb79SDave Chinner xfs_inode_t *ip; 844e546cb79SDave Chinner xfs_buf_t *ialloc_context = NULL; 845e546cb79SDave Chinner int code; 846e546cb79SDave Chinner void *dqinfo; 847e546cb79SDave Chinner uint tflags; 848e546cb79SDave Chinner 849e546cb79SDave Chinner tp = *tpp; 850e546cb79SDave Chinner ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 851e546cb79SDave Chinner 852e546cb79SDave Chinner /* 853e546cb79SDave Chinner * xfs_ialloc will return a pointer to an incore inode if 854e546cb79SDave Chinner * the Space Manager has an available inode on the free 855e546cb79SDave Chinner * list. Otherwise, it will do an allocation and replenish 856e546cb79SDave Chinner * the freelist. Since we can only do one allocation per 857e546cb79SDave Chinner * transaction without deadlocks, we will need to commit the 858e546cb79SDave Chinner * current transaction and start a new one. We will then 859e546cb79SDave Chinner * need to call xfs_ialloc again to get the inode. 860e546cb79SDave Chinner * 861e546cb79SDave Chinner * If xfs_ialloc did an allocation to replenish the freelist, 862e546cb79SDave Chinner * it returns the bp containing the head of the freelist as 863e546cb79SDave Chinner * ialloc_context. We will hold a lock on it across the 864e546cb79SDave Chinner * transaction commit so that no other process can steal 865e546cb79SDave Chinner * the inode(s) that we've just allocated. 866e546cb79SDave Chinner */ 867e546cb79SDave Chinner code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc, 868e546cb79SDave Chinner &ialloc_context, &ip); 869e546cb79SDave Chinner 870e546cb79SDave Chinner /* 871e546cb79SDave Chinner * Return an error if we were unable to allocate a new inode. 872e546cb79SDave Chinner * This should only happen if we run out of space on disk or 873e546cb79SDave Chinner * encounter a disk error. 874e546cb79SDave Chinner */ 875e546cb79SDave Chinner if (code) { 876e546cb79SDave Chinner *ipp = NULL; 877e546cb79SDave Chinner return code; 878e546cb79SDave Chinner } 879e546cb79SDave Chinner if (!ialloc_context && !ip) { 880e546cb79SDave Chinner *ipp = NULL; 8812451337dSDave Chinner return -ENOSPC; 882e546cb79SDave Chinner } 883e546cb79SDave Chinner 884e546cb79SDave Chinner /* 885e546cb79SDave Chinner * If the AGI buffer is non-NULL, then we were unable to get an 886e546cb79SDave Chinner * inode in one operation. We need to commit the current 887e546cb79SDave Chinner * transaction and call xfs_ialloc() again. It is guaranteed 888e546cb79SDave Chinner * to succeed the second time. 889e546cb79SDave Chinner */ 890e546cb79SDave Chinner if (ialloc_context) { 8913d3c8b52SJie Liu struct xfs_trans_res tres; 8923d3c8b52SJie Liu 893e546cb79SDave Chinner /* 894e546cb79SDave Chinner * Normally, xfs_trans_commit releases all the locks. 895e546cb79SDave Chinner * We call bhold to hang on to the ialloc_context across 896e546cb79SDave Chinner * the commit. Holding this buffer prevents any other 897e546cb79SDave Chinner * processes from doing any allocations in this 898e546cb79SDave Chinner * allocation group. 899e546cb79SDave Chinner */ 900e546cb79SDave Chinner xfs_trans_bhold(tp, ialloc_context); 901e546cb79SDave Chinner /* 902e546cb79SDave Chinner * Save the log reservation so we can use 903e546cb79SDave Chinner * them in the next transaction. 904e546cb79SDave Chinner */ 9053d3c8b52SJie Liu tres.tr_logres = xfs_trans_get_log_res(tp); 9063d3c8b52SJie Liu tres.tr_logcount = xfs_trans_get_log_count(tp); 907e546cb79SDave Chinner 908e546cb79SDave Chinner /* 909e546cb79SDave Chinner * We want the quota changes to be associated with the next 910e546cb79SDave Chinner * transaction, NOT this one. So, detach the dqinfo from this 911e546cb79SDave Chinner * and attach it to the next transaction. 912e546cb79SDave Chinner */ 913e546cb79SDave Chinner dqinfo = NULL; 914e546cb79SDave Chinner tflags = 0; 915e546cb79SDave Chinner if (tp->t_dqinfo) { 916e546cb79SDave Chinner dqinfo = (void *)tp->t_dqinfo; 917e546cb79SDave Chinner tp->t_dqinfo = NULL; 918e546cb79SDave Chinner tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY; 919e546cb79SDave Chinner tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY); 920e546cb79SDave Chinner } 921e546cb79SDave Chinner 922e546cb79SDave Chinner ntp = xfs_trans_dup(tp); 923e546cb79SDave Chinner code = xfs_trans_commit(tp, 0); 924e546cb79SDave Chinner tp = ntp; 925e546cb79SDave Chinner if (committed != NULL) { 926e546cb79SDave Chinner *committed = 1; 927e546cb79SDave Chinner } 928e546cb79SDave Chinner /* 929e546cb79SDave Chinner * If we get an error during the commit processing, 930e546cb79SDave Chinner * release the buffer that is still held and return 931e546cb79SDave Chinner * to the caller. 932e546cb79SDave Chinner */ 933e546cb79SDave Chinner if (code) { 934e546cb79SDave Chinner xfs_buf_relse(ialloc_context); 935e546cb79SDave Chinner if (dqinfo) { 936e546cb79SDave Chinner tp->t_dqinfo = dqinfo; 937e546cb79SDave Chinner xfs_trans_free_dqinfo(tp); 938e546cb79SDave Chinner } 939e546cb79SDave Chinner *tpp = ntp; 940e546cb79SDave Chinner *ipp = NULL; 941e546cb79SDave Chinner return code; 942e546cb79SDave Chinner } 943e546cb79SDave Chinner 944e546cb79SDave Chinner /* 945e546cb79SDave Chinner * transaction commit worked ok so we can drop the extra ticket 946e546cb79SDave Chinner * reference that we gained in xfs_trans_dup() 947e546cb79SDave Chinner */ 948e546cb79SDave Chinner xfs_log_ticket_put(tp->t_ticket); 9493d3c8b52SJie Liu tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; 9503d3c8b52SJie Liu code = xfs_trans_reserve(tp, &tres, 0, 0); 9513d3c8b52SJie Liu 952e546cb79SDave Chinner /* 953e546cb79SDave Chinner * Re-attach the quota info that we detached from prev trx. 954e546cb79SDave Chinner */ 955e546cb79SDave Chinner if (dqinfo) { 956e546cb79SDave Chinner tp->t_dqinfo = dqinfo; 957e546cb79SDave Chinner tp->t_flags |= tflags; 958e546cb79SDave Chinner } 959e546cb79SDave Chinner 960e546cb79SDave Chinner if (code) { 961e546cb79SDave Chinner xfs_buf_relse(ialloc_context); 962e546cb79SDave Chinner *tpp = ntp; 963e546cb79SDave Chinner *ipp = NULL; 964e546cb79SDave Chinner return code; 965e546cb79SDave Chinner } 966e546cb79SDave Chinner xfs_trans_bjoin(tp, ialloc_context); 967e546cb79SDave Chinner 968e546cb79SDave Chinner /* 969e546cb79SDave Chinner * Call ialloc again. Since we've locked out all 970e546cb79SDave Chinner * other allocations in this allocation group, 971e546cb79SDave Chinner * this call should always succeed. 972e546cb79SDave Chinner */ 973e546cb79SDave Chinner code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, 974e546cb79SDave Chinner okalloc, &ialloc_context, &ip); 975e546cb79SDave Chinner 976e546cb79SDave Chinner /* 977e546cb79SDave Chinner * If we get an error at this point, return to the caller 978e546cb79SDave Chinner * so that the current transaction can be aborted. 979e546cb79SDave Chinner */ 980e546cb79SDave Chinner if (code) { 981e546cb79SDave Chinner *tpp = tp; 982e546cb79SDave Chinner *ipp = NULL; 983e546cb79SDave Chinner return code; 984e546cb79SDave Chinner } 985e546cb79SDave Chinner ASSERT(!ialloc_context && ip); 986e546cb79SDave Chinner 987e546cb79SDave Chinner } else { 988e546cb79SDave Chinner if (committed != NULL) 989e546cb79SDave Chinner *committed = 0; 990e546cb79SDave Chinner } 991e546cb79SDave Chinner 992e546cb79SDave Chinner *ipp = ip; 993e546cb79SDave Chinner *tpp = tp; 994e546cb79SDave Chinner 995e546cb79SDave Chinner return 0; 996e546cb79SDave Chinner } 997e546cb79SDave Chinner 998e546cb79SDave Chinner /* 999e546cb79SDave Chinner * Decrement the link count on an inode & log the change. 1000e546cb79SDave Chinner * If this causes the link count to go to zero, initiate the 1001e546cb79SDave Chinner * logging activity required to truncate a file. 1002e546cb79SDave Chinner */ 1003e546cb79SDave Chinner int /* error */ 1004e546cb79SDave Chinner xfs_droplink( 1005e546cb79SDave Chinner xfs_trans_t *tp, 1006e546cb79SDave Chinner xfs_inode_t *ip) 1007e546cb79SDave Chinner { 1008e546cb79SDave Chinner int error; 1009e546cb79SDave Chinner 1010e546cb79SDave Chinner xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); 1011e546cb79SDave Chinner 1012e546cb79SDave Chinner ASSERT (ip->i_d.di_nlink > 0); 1013e546cb79SDave Chinner ip->i_d.di_nlink--; 1014e546cb79SDave Chinner drop_nlink(VFS_I(ip)); 1015e546cb79SDave Chinner xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1016e546cb79SDave Chinner 1017e546cb79SDave Chinner error = 0; 1018e546cb79SDave Chinner if (ip->i_d.di_nlink == 0) { 1019e546cb79SDave Chinner /* 1020e546cb79SDave Chinner * We're dropping the last link to this file. 1021e546cb79SDave Chinner * Move the on-disk inode to the AGI unlinked list. 1022e546cb79SDave Chinner * From xfs_inactive() we will pull the inode from 1023e546cb79SDave Chinner * the list and free it. 1024e546cb79SDave Chinner */ 1025e546cb79SDave Chinner error = xfs_iunlink(tp, ip); 1026e546cb79SDave Chinner } 1027e546cb79SDave Chinner return error; 1028e546cb79SDave Chinner } 1029e546cb79SDave Chinner 1030e546cb79SDave Chinner /* 1031e546cb79SDave Chinner * Increment the link count on an inode & log the change. 1032e546cb79SDave Chinner */ 1033e546cb79SDave Chinner int 1034e546cb79SDave Chinner xfs_bumplink( 1035e546cb79SDave Chinner xfs_trans_t *tp, 1036e546cb79SDave Chinner xfs_inode_t *ip) 1037e546cb79SDave Chinner { 1038e546cb79SDave Chinner xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); 1039e546cb79SDave Chinner 1040263997a6SDave Chinner ASSERT(ip->i_d.di_version > 1); 1041ab297431SZhi Yong Wu ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE)); 1042e546cb79SDave Chinner ip->i_d.di_nlink++; 1043e546cb79SDave Chinner inc_nlink(VFS_I(ip)); 1044e546cb79SDave Chinner xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1045e546cb79SDave Chinner return 0; 1046e546cb79SDave Chinner } 1047e546cb79SDave Chinner 1048c24b5dfaSDave Chinner int 1049c24b5dfaSDave Chinner xfs_create( 1050c24b5dfaSDave Chinner xfs_inode_t *dp, 1051c24b5dfaSDave Chinner struct xfs_name *name, 1052c24b5dfaSDave Chinner umode_t mode, 1053c24b5dfaSDave Chinner xfs_dev_t rdev, 1054c24b5dfaSDave Chinner xfs_inode_t **ipp) 1055c24b5dfaSDave Chinner { 1056c24b5dfaSDave Chinner int is_dir = S_ISDIR(mode); 1057c24b5dfaSDave Chinner struct xfs_mount *mp = dp->i_mount; 1058c24b5dfaSDave Chinner struct xfs_inode *ip = NULL; 1059c24b5dfaSDave Chinner struct xfs_trans *tp = NULL; 1060c24b5dfaSDave Chinner int error; 1061c24b5dfaSDave Chinner xfs_bmap_free_t free_list; 1062c24b5dfaSDave Chinner xfs_fsblock_t first_block; 1063c24b5dfaSDave Chinner bool unlock_dp_on_error = false; 1064c24b5dfaSDave Chinner uint cancel_flags; 1065c24b5dfaSDave Chinner int committed; 1066c24b5dfaSDave Chinner prid_t prid; 1067c24b5dfaSDave Chinner struct xfs_dquot *udqp = NULL; 1068c24b5dfaSDave Chinner struct xfs_dquot *gdqp = NULL; 1069c24b5dfaSDave Chinner struct xfs_dquot *pdqp = NULL; 1070062647a8SBrian Foster struct xfs_trans_res *tres; 1071c24b5dfaSDave Chinner uint resblks; 1072c24b5dfaSDave Chinner 1073c24b5dfaSDave Chinner trace_xfs_create(dp, name); 1074c24b5dfaSDave Chinner 1075c24b5dfaSDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 10762451337dSDave Chinner return -EIO; 1077c24b5dfaSDave Chinner 1078163467d3SZhi Yong Wu prid = xfs_get_initial_prid(dp); 1079c24b5dfaSDave Chinner 1080c24b5dfaSDave Chinner /* 1081c24b5dfaSDave Chinner * Make sure that we have allocated dquot(s) on disk. 1082c24b5dfaSDave Chinner */ 10837aab1b28SDwight Engen error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()), 10847aab1b28SDwight Engen xfs_kgid_to_gid(current_fsgid()), prid, 1085c24b5dfaSDave Chinner XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, 1086c24b5dfaSDave Chinner &udqp, &gdqp, &pdqp); 1087c24b5dfaSDave Chinner if (error) 1088c24b5dfaSDave Chinner return error; 1089c24b5dfaSDave Chinner 1090c24b5dfaSDave Chinner if (is_dir) { 1091c24b5dfaSDave Chinner rdev = 0; 1092c24b5dfaSDave Chinner resblks = XFS_MKDIR_SPACE_RES(mp, name->len); 1093062647a8SBrian Foster tres = &M_RES(mp)->tr_mkdir; 1094c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR); 1095c24b5dfaSDave Chinner } else { 1096c24b5dfaSDave Chinner resblks = XFS_CREATE_SPACE_RES(mp, name->len); 1097062647a8SBrian Foster tres = &M_RES(mp)->tr_create; 1098c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE); 1099c24b5dfaSDave Chinner } 1100c24b5dfaSDave Chinner 1101c24b5dfaSDave Chinner cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1102c24b5dfaSDave Chinner 1103c24b5dfaSDave Chinner /* 1104c24b5dfaSDave Chinner * Initially assume that the file does not exist and 1105c24b5dfaSDave Chinner * reserve the resources for that case. If that is not 1106c24b5dfaSDave Chinner * the case we'll drop the one we have and get a more 1107c24b5dfaSDave Chinner * appropriate transaction later. 1108c24b5dfaSDave Chinner */ 1109062647a8SBrian Foster error = xfs_trans_reserve(tp, tres, resblks, 0); 11102451337dSDave Chinner if (error == -ENOSPC) { 1111c24b5dfaSDave Chinner /* flush outstanding delalloc blocks and retry */ 1112c24b5dfaSDave Chinner xfs_flush_inodes(mp); 1113062647a8SBrian Foster error = xfs_trans_reserve(tp, tres, resblks, 0); 1114c24b5dfaSDave Chinner } 11152451337dSDave Chinner if (error == -ENOSPC) { 1116c24b5dfaSDave Chinner /* No space at all so try a "no-allocation" reservation */ 1117c24b5dfaSDave Chinner resblks = 0; 1118062647a8SBrian Foster error = xfs_trans_reserve(tp, tres, 0, 0); 1119c24b5dfaSDave Chinner } 1120c24b5dfaSDave Chinner if (error) { 1121c24b5dfaSDave Chinner cancel_flags = 0; 1122c24b5dfaSDave Chinner goto out_trans_cancel; 1123c24b5dfaSDave Chinner } 1124c24b5dfaSDave Chinner 1125c24b5dfaSDave Chinner xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); 1126c24b5dfaSDave Chinner unlock_dp_on_error = true; 1127c24b5dfaSDave Chinner 1128c24b5dfaSDave Chinner xfs_bmap_init(&free_list, &first_block); 1129c24b5dfaSDave Chinner 1130c24b5dfaSDave Chinner /* 1131c24b5dfaSDave Chinner * Reserve disk quota and the inode. 1132c24b5dfaSDave Chinner */ 1133c24b5dfaSDave Chinner error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, 1134c24b5dfaSDave Chinner pdqp, resblks, 1, 0); 1135c24b5dfaSDave Chinner if (error) 1136c24b5dfaSDave Chinner goto out_trans_cancel; 1137c24b5dfaSDave Chinner 113894f3cad5SEric Sandeen if (!resblks) { 113994f3cad5SEric Sandeen error = xfs_dir_canenter(tp, dp, name); 1140c24b5dfaSDave Chinner if (error) 1141c24b5dfaSDave Chinner goto out_trans_cancel; 114294f3cad5SEric Sandeen } 1143c24b5dfaSDave Chinner 1144c24b5dfaSDave Chinner /* 1145c24b5dfaSDave Chinner * A newly created regular or special file just has one directory 1146c24b5dfaSDave Chinner * entry pointing to them, but a directory also the "." entry 1147c24b5dfaSDave Chinner * pointing to itself. 1148c24b5dfaSDave Chinner */ 1149c24b5dfaSDave Chinner error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, 1150c24b5dfaSDave Chinner prid, resblks > 0, &ip, &committed); 1151c24b5dfaSDave Chinner if (error) { 11522451337dSDave Chinner if (error == -ENOSPC) 1153c24b5dfaSDave Chinner goto out_trans_cancel; 1154c24b5dfaSDave Chinner goto out_trans_abort; 1155c24b5dfaSDave Chinner } 1156c24b5dfaSDave Chinner 1157c24b5dfaSDave Chinner /* 1158c24b5dfaSDave Chinner * Now we join the directory inode to the transaction. We do not do it 1159c24b5dfaSDave Chinner * earlier because xfs_dir_ialloc might commit the previous transaction 1160c24b5dfaSDave Chinner * (and release all the locks). An error from here on will result in 1161c24b5dfaSDave Chinner * the transaction cancel unlocking dp so don't do it explicitly in the 1162c24b5dfaSDave Chinner * error path. 1163c24b5dfaSDave Chinner */ 1164c24b5dfaSDave Chinner xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); 1165c24b5dfaSDave Chinner unlock_dp_on_error = false; 1166c24b5dfaSDave Chinner 1167c24b5dfaSDave Chinner error = xfs_dir_createname(tp, dp, name, ip->i_ino, 1168c24b5dfaSDave Chinner &first_block, &free_list, resblks ? 1169c24b5dfaSDave Chinner resblks - XFS_IALLOC_SPACE_RES(mp) : 0); 1170c24b5dfaSDave Chinner if (error) { 11712451337dSDave Chinner ASSERT(error != -ENOSPC); 1172c24b5dfaSDave Chinner goto out_trans_abort; 1173c24b5dfaSDave Chinner } 1174c24b5dfaSDave Chinner xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1175c24b5dfaSDave Chinner xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 1176c24b5dfaSDave Chinner 1177c24b5dfaSDave Chinner if (is_dir) { 1178c24b5dfaSDave Chinner error = xfs_dir_init(tp, ip, dp); 1179c24b5dfaSDave Chinner if (error) 1180c24b5dfaSDave Chinner goto out_bmap_cancel; 1181c24b5dfaSDave Chinner 1182c24b5dfaSDave Chinner error = xfs_bumplink(tp, dp); 1183c24b5dfaSDave Chinner if (error) 1184c24b5dfaSDave Chinner goto out_bmap_cancel; 1185c24b5dfaSDave Chinner } 1186c24b5dfaSDave Chinner 1187c24b5dfaSDave Chinner /* 1188c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the 1189c24b5dfaSDave Chinner * create transaction goes to disk before returning to 1190c24b5dfaSDave Chinner * the user. 1191c24b5dfaSDave Chinner */ 1192c24b5dfaSDave Chinner if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) 1193c24b5dfaSDave Chinner xfs_trans_set_sync(tp); 1194c24b5dfaSDave Chinner 1195c24b5dfaSDave Chinner /* 1196c24b5dfaSDave Chinner * Attach the dquot(s) to the inodes and modify them incore. 1197c24b5dfaSDave Chinner * These ids of the inode couldn't have changed since the new 1198c24b5dfaSDave Chinner * inode has been locked ever since it was created. 1199c24b5dfaSDave Chinner */ 1200c24b5dfaSDave Chinner xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); 1201c24b5dfaSDave Chinner 1202c24b5dfaSDave Chinner error = xfs_bmap_finish(&tp, &free_list, &committed); 1203c24b5dfaSDave Chinner if (error) 1204c24b5dfaSDave Chinner goto out_bmap_cancel; 1205c24b5dfaSDave Chinner 1206c24b5dfaSDave Chinner error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1207c24b5dfaSDave Chinner if (error) 1208c24b5dfaSDave Chinner goto out_release_inode; 1209c24b5dfaSDave Chinner 1210c24b5dfaSDave Chinner xfs_qm_dqrele(udqp); 1211c24b5dfaSDave Chinner xfs_qm_dqrele(gdqp); 1212c24b5dfaSDave Chinner xfs_qm_dqrele(pdqp); 1213c24b5dfaSDave Chinner 1214c24b5dfaSDave Chinner *ipp = ip; 1215c24b5dfaSDave Chinner return 0; 1216c24b5dfaSDave Chinner 1217c24b5dfaSDave Chinner out_bmap_cancel: 1218c24b5dfaSDave Chinner xfs_bmap_cancel(&free_list); 1219c24b5dfaSDave Chinner out_trans_abort: 1220c24b5dfaSDave Chinner cancel_flags |= XFS_TRANS_ABORT; 1221c24b5dfaSDave Chinner out_trans_cancel: 1222c24b5dfaSDave Chinner xfs_trans_cancel(tp, cancel_flags); 1223c24b5dfaSDave Chinner out_release_inode: 1224c24b5dfaSDave Chinner /* 122558c90473SDave Chinner * Wait until after the current transaction is aborted to finish the 122658c90473SDave Chinner * setup of the inode and release the inode. This prevents recursive 122758c90473SDave Chinner * transactions and deadlocks from xfs_inactive. 1228c24b5dfaSDave Chinner */ 122958c90473SDave Chinner if (ip) { 123058c90473SDave Chinner xfs_finish_inode_setup(ip); 1231c24b5dfaSDave Chinner IRELE(ip); 123258c90473SDave Chinner } 1233c24b5dfaSDave Chinner 1234c24b5dfaSDave Chinner xfs_qm_dqrele(udqp); 1235c24b5dfaSDave Chinner xfs_qm_dqrele(gdqp); 1236c24b5dfaSDave Chinner xfs_qm_dqrele(pdqp); 1237c24b5dfaSDave Chinner 1238c24b5dfaSDave Chinner if (unlock_dp_on_error) 1239c24b5dfaSDave Chinner xfs_iunlock(dp, XFS_ILOCK_EXCL); 1240c24b5dfaSDave Chinner return error; 1241c24b5dfaSDave Chinner } 1242c24b5dfaSDave Chinner 1243c24b5dfaSDave Chinner int 124499b6436bSZhi Yong Wu xfs_create_tmpfile( 124599b6436bSZhi Yong Wu struct xfs_inode *dp, 124699b6436bSZhi Yong Wu struct dentry *dentry, 1247330033d6SBrian Foster umode_t mode, 1248330033d6SBrian Foster struct xfs_inode **ipp) 124999b6436bSZhi Yong Wu { 125099b6436bSZhi Yong Wu struct xfs_mount *mp = dp->i_mount; 125199b6436bSZhi Yong Wu struct xfs_inode *ip = NULL; 125299b6436bSZhi Yong Wu struct xfs_trans *tp = NULL; 125399b6436bSZhi Yong Wu int error; 125499b6436bSZhi Yong Wu uint cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 125599b6436bSZhi Yong Wu prid_t prid; 125699b6436bSZhi Yong Wu struct xfs_dquot *udqp = NULL; 125799b6436bSZhi Yong Wu struct xfs_dquot *gdqp = NULL; 125899b6436bSZhi Yong Wu struct xfs_dquot *pdqp = NULL; 125999b6436bSZhi Yong Wu struct xfs_trans_res *tres; 126099b6436bSZhi Yong Wu uint resblks; 126199b6436bSZhi Yong Wu 126299b6436bSZhi Yong Wu if (XFS_FORCED_SHUTDOWN(mp)) 12632451337dSDave Chinner return -EIO; 126499b6436bSZhi Yong Wu 126599b6436bSZhi Yong Wu prid = xfs_get_initial_prid(dp); 126699b6436bSZhi Yong Wu 126799b6436bSZhi Yong Wu /* 126899b6436bSZhi Yong Wu * Make sure that we have allocated dquot(s) on disk. 126999b6436bSZhi Yong Wu */ 127099b6436bSZhi Yong Wu error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()), 127199b6436bSZhi Yong Wu xfs_kgid_to_gid(current_fsgid()), prid, 127299b6436bSZhi Yong Wu XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, 127399b6436bSZhi Yong Wu &udqp, &gdqp, &pdqp); 127499b6436bSZhi Yong Wu if (error) 127599b6436bSZhi Yong Wu return error; 127699b6436bSZhi Yong Wu 127799b6436bSZhi Yong Wu resblks = XFS_IALLOC_SPACE_RES(mp); 127899b6436bSZhi Yong Wu tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE); 127999b6436bSZhi Yong Wu 128099b6436bSZhi Yong Wu tres = &M_RES(mp)->tr_create_tmpfile; 128199b6436bSZhi Yong Wu error = xfs_trans_reserve(tp, tres, resblks, 0); 12822451337dSDave Chinner if (error == -ENOSPC) { 128399b6436bSZhi Yong Wu /* No space at all so try a "no-allocation" reservation */ 128499b6436bSZhi Yong Wu resblks = 0; 128599b6436bSZhi Yong Wu error = xfs_trans_reserve(tp, tres, 0, 0); 128699b6436bSZhi Yong Wu } 128799b6436bSZhi Yong Wu if (error) { 128899b6436bSZhi Yong Wu cancel_flags = 0; 128999b6436bSZhi Yong Wu goto out_trans_cancel; 129099b6436bSZhi Yong Wu } 129199b6436bSZhi Yong Wu 129299b6436bSZhi Yong Wu error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, 129399b6436bSZhi Yong Wu pdqp, resblks, 1, 0); 129499b6436bSZhi Yong Wu if (error) 129599b6436bSZhi Yong Wu goto out_trans_cancel; 129699b6436bSZhi Yong Wu 129799b6436bSZhi Yong Wu error = xfs_dir_ialloc(&tp, dp, mode, 1, 0, 129899b6436bSZhi Yong Wu prid, resblks > 0, &ip, NULL); 129999b6436bSZhi Yong Wu if (error) { 13002451337dSDave Chinner if (error == -ENOSPC) 130199b6436bSZhi Yong Wu goto out_trans_cancel; 130299b6436bSZhi Yong Wu goto out_trans_abort; 130399b6436bSZhi Yong Wu } 130499b6436bSZhi Yong Wu 130599b6436bSZhi Yong Wu if (mp->m_flags & XFS_MOUNT_WSYNC) 130699b6436bSZhi Yong Wu xfs_trans_set_sync(tp); 130799b6436bSZhi Yong Wu 130899b6436bSZhi Yong Wu /* 130999b6436bSZhi Yong Wu * Attach the dquot(s) to the inodes and modify them incore. 131099b6436bSZhi Yong Wu * These ids of the inode couldn't have changed since the new 131199b6436bSZhi Yong Wu * inode has been locked ever since it was created. 131299b6436bSZhi Yong Wu */ 131399b6436bSZhi Yong Wu xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); 131499b6436bSZhi Yong Wu 131599b6436bSZhi Yong Wu ip->i_d.di_nlink--; 131699b6436bSZhi Yong Wu error = xfs_iunlink(tp, ip); 131799b6436bSZhi Yong Wu if (error) 131899b6436bSZhi Yong Wu goto out_trans_abort; 131999b6436bSZhi Yong Wu 132099b6436bSZhi Yong Wu error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 132199b6436bSZhi Yong Wu if (error) 132299b6436bSZhi Yong Wu goto out_release_inode; 132399b6436bSZhi Yong Wu 132499b6436bSZhi Yong Wu xfs_qm_dqrele(udqp); 132599b6436bSZhi Yong Wu xfs_qm_dqrele(gdqp); 132699b6436bSZhi Yong Wu xfs_qm_dqrele(pdqp); 132799b6436bSZhi Yong Wu 1328330033d6SBrian Foster *ipp = ip; 132999b6436bSZhi Yong Wu return 0; 133099b6436bSZhi Yong Wu 133199b6436bSZhi Yong Wu out_trans_abort: 133299b6436bSZhi Yong Wu cancel_flags |= XFS_TRANS_ABORT; 133399b6436bSZhi Yong Wu out_trans_cancel: 133499b6436bSZhi Yong Wu xfs_trans_cancel(tp, cancel_flags); 133599b6436bSZhi Yong Wu out_release_inode: 133699b6436bSZhi Yong Wu /* 133758c90473SDave Chinner * Wait until after the current transaction is aborted to finish the 133858c90473SDave Chinner * setup of the inode and release the inode. This prevents recursive 133958c90473SDave Chinner * transactions and deadlocks from xfs_inactive. 134099b6436bSZhi Yong Wu */ 134158c90473SDave Chinner if (ip) { 134258c90473SDave Chinner xfs_finish_inode_setup(ip); 134399b6436bSZhi Yong Wu IRELE(ip); 134458c90473SDave Chinner } 134599b6436bSZhi Yong Wu 134699b6436bSZhi Yong Wu xfs_qm_dqrele(udqp); 134799b6436bSZhi Yong Wu xfs_qm_dqrele(gdqp); 134899b6436bSZhi Yong Wu xfs_qm_dqrele(pdqp); 134999b6436bSZhi Yong Wu 135099b6436bSZhi Yong Wu return error; 135199b6436bSZhi Yong Wu } 135299b6436bSZhi Yong Wu 135399b6436bSZhi Yong Wu int 1354c24b5dfaSDave Chinner xfs_link( 1355c24b5dfaSDave Chinner xfs_inode_t *tdp, 1356c24b5dfaSDave Chinner xfs_inode_t *sip, 1357c24b5dfaSDave Chinner struct xfs_name *target_name) 1358c24b5dfaSDave Chinner { 1359c24b5dfaSDave Chinner xfs_mount_t *mp = tdp->i_mount; 1360c24b5dfaSDave Chinner xfs_trans_t *tp; 1361c24b5dfaSDave Chinner int error; 1362c24b5dfaSDave Chinner xfs_bmap_free_t free_list; 1363c24b5dfaSDave Chinner xfs_fsblock_t first_block; 1364c24b5dfaSDave Chinner int cancel_flags; 1365c24b5dfaSDave Chinner int committed; 1366c24b5dfaSDave Chinner int resblks; 1367c24b5dfaSDave Chinner 1368c24b5dfaSDave Chinner trace_xfs_link(tdp, target_name); 1369c24b5dfaSDave Chinner 1370c24b5dfaSDave Chinner ASSERT(!S_ISDIR(sip->i_d.di_mode)); 1371c24b5dfaSDave Chinner 1372c24b5dfaSDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 13732451337dSDave Chinner return -EIO; 1374c24b5dfaSDave Chinner 1375c24b5dfaSDave Chinner error = xfs_qm_dqattach(sip, 0); 1376c24b5dfaSDave Chinner if (error) 1377c24b5dfaSDave Chinner goto std_return; 1378c24b5dfaSDave Chinner 1379c24b5dfaSDave Chinner error = xfs_qm_dqattach(tdp, 0); 1380c24b5dfaSDave Chinner if (error) 1381c24b5dfaSDave Chinner goto std_return; 1382c24b5dfaSDave Chinner 1383c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_LINK); 1384c24b5dfaSDave Chinner cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 1385c24b5dfaSDave Chinner resblks = XFS_LINK_SPACE_RES(mp, target_name->len); 13863d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0); 13872451337dSDave Chinner if (error == -ENOSPC) { 1388c24b5dfaSDave Chinner resblks = 0; 13893d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0); 1390c24b5dfaSDave Chinner } 1391c24b5dfaSDave Chinner if (error) { 1392c24b5dfaSDave Chinner cancel_flags = 0; 1393c24b5dfaSDave Chinner goto error_return; 1394c24b5dfaSDave Chinner } 1395c24b5dfaSDave Chinner 1396c24b5dfaSDave Chinner xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL); 1397c24b5dfaSDave Chinner 1398c24b5dfaSDave Chinner xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL); 1399c24b5dfaSDave Chinner xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL); 1400c24b5dfaSDave Chinner 1401c24b5dfaSDave Chinner /* 1402c24b5dfaSDave Chinner * If we are using project inheritance, we only allow hard link 1403c24b5dfaSDave Chinner * creation in our tree when the project IDs are the same; else 1404c24b5dfaSDave Chinner * the tree quota mechanism could be circumvented. 1405c24b5dfaSDave Chinner */ 1406c24b5dfaSDave Chinner if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && 1407c24b5dfaSDave Chinner (xfs_get_projid(tdp) != xfs_get_projid(sip)))) { 14082451337dSDave Chinner error = -EXDEV; 1409c24b5dfaSDave Chinner goto error_return; 1410c24b5dfaSDave Chinner } 1411c24b5dfaSDave Chinner 141294f3cad5SEric Sandeen if (!resblks) { 141394f3cad5SEric Sandeen error = xfs_dir_canenter(tp, tdp, target_name); 1414c24b5dfaSDave Chinner if (error) 1415c24b5dfaSDave Chinner goto error_return; 141694f3cad5SEric Sandeen } 1417c24b5dfaSDave Chinner 1418c24b5dfaSDave Chinner xfs_bmap_init(&free_list, &first_block); 1419c24b5dfaSDave Chinner 1420ab297431SZhi Yong Wu if (sip->i_d.di_nlink == 0) { 1421ab297431SZhi Yong Wu error = xfs_iunlink_remove(tp, sip); 1422ab297431SZhi Yong Wu if (error) 1423ab297431SZhi Yong Wu goto abort_return; 1424ab297431SZhi Yong Wu } 1425ab297431SZhi Yong Wu 1426c24b5dfaSDave Chinner error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, 1427c24b5dfaSDave Chinner &first_block, &free_list, resblks); 1428c24b5dfaSDave Chinner if (error) 1429c24b5dfaSDave Chinner goto abort_return; 1430c24b5dfaSDave Chinner xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1431c24b5dfaSDave Chinner xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); 1432c24b5dfaSDave Chinner 1433c24b5dfaSDave Chinner error = xfs_bumplink(tp, sip); 1434c24b5dfaSDave Chinner if (error) 1435c24b5dfaSDave Chinner goto abort_return; 1436c24b5dfaSDave Chinner 1437c24b5dfaSDave Chinner /* 1438c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the 1439c24b5dfaSDave Chinner * link transaction goes to disk before returning to 1440c24b5dfaSDave Chinner * the user. 1441c24b5dfaSDave Chinner */ 1442c24b5dfaSDave Chinner if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { 1443c24b5dfaSDave Chinner xfs_trans_set_sync(tp); 1444c24b5dfaSDave Chinner } 1445c24b5dfaSDave Chinner 1446c24b5dfaSDave Chinner error = xfs_bmap_finish (&tp, &free_list, &committed); 1447c24b5dfaSDave Chinner if (error) { 1448c24b5dfaSDave Chinner xfs_bmap_cancel(&free_list); 1449c24b5dfaSDave Chinner goto abort_return; 1450c24b5dfaSDave Chinner } 1451c24b5dfaSDave Chinner 1452c24b5dfaSDave Chinner return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1453c24b5dfaSDave Chinner 1454c24b5dfaSDave Chinner abort_return: 1455c24b5dfaSDave Chinner cancel_flags |= XFS_TRANS_ABORT; 1456c24b5dfaSDave Chinner error_return: 1457c24b5dfaSDave Chinner xfs_trans_cancel(tp, cancel_flags); 1458c24b5dfaSDave Chinner std_return: 1459c24b5dfaSDave Chinner return error; 1460c24b5dfaSDave Chinner } 1461c24b5dfaSDave Chinner 14621da177e4SLinus Torvalds /* 14638f04c47aSChristoph Hellwig * Free up the underlying blocks past new_size. The new size must be smaller 14648f04c47aSChristoph Hellwig * than the current size. This routine can be used both for the attribute and 14658f04c47aSChristoph Hellwig * data fork, and does not modify the inode size, which is left to the caller. 14661da177e4SLinus Torvalds * 1467f6485057SDavid Chinner * The transaction passed to this routine must have made a permanent log 1468f6485057SDavid Chinner * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the 1469f6485057SDavid Chinner * given transaction and start new ones, so make sure everything involved in 1470f6485057SDavid Chinner * the transaction is tidy before calling here. Some transaction will be 1471f6485057SDavid Chinner * returned to the caller to be committed. The incoming transaction must 1472f6485057SDavid Chinner * already include the inode, and both inode locks must be held exclusively. 1473f6485057SDavid Chinner * The inode must also be "held" within the transaction. On return the inode 1474f6485057SDavid Chinner * will be "held" within the returned transaction. This routine does NOT 1475f6485057SDavid Chinner * require any disk space to be reserved for it within the transaction. 14761da177e4SLinus Torvalds * 1477f6485057SDavid Chinner * If we get an error, we must return with the inode locked and linked into the 1478f6485057SDavid Chinner * current transaction. This keeps things simple for the higher level code, 1479f6485057SDavid Chinner * because it always knows that the inode is locked and held in the transaction 1480f6485057SDavid Chinner * that returns to it whether errors occur or not. We don't mark the inode 1481f6485057SDavid Chinner * dirty on error so that transactions can be easily aborted if possible. 14821da177e4SLinus Torvalds */ 14831da177e4SLinus Torvalds int 14848f04c47aSChristoph Hellwig xfs_itruncate_extents( 14858f04c47aSChristoph Hellwig struct xfs_trans **tpp, 14868f04c47aSChristoph Hellwig struct xfs_inode *ip, 14878f04c47aSChristoph Hellwig int whichfork, 14888f04c47aSChristoph Hellwig xfs_fsize_t new_size) 14891da177e4SLinus Torvalds { 14908f04c47aSChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 14918f04c47aSChristoph Hellwig struct xfs_trans *tp = *tpp; 14928f04c47aSChristoph Hellwig struct xfs_trans *ntp; 14938f04c47aSChristoph Hellwig xfs_bmap_free_t free_list; 14941da177e4SLinus Torvalds xfs_fsblock_t first_block; 14951da177e4SLinus Torvalds xfs_fileoff_t first_unmap_block; 14961da177e4SLinus Torvalds xfs_fileoff_t last_block; 14978f04c47aSChristoph Hellwig xfs_filblks_t unmap_len; 14981da177e4SLinus Torvalds int committed; 14998f04c47aSChristoph Hellwig int error = 0; 15008f04c47aSChristoph Hellwig int done = 0; 15011da177e4SLinus Torvalds 15020b56185bSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 15030b56185bSChristoph Hellwig ASSERT(!atomic_read(&VFS_I(ip)->i_count) || 15040b56185bSChristoph Hellwig xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 1505ce7ae151SChristoph Hellwig ASSERT(new_size <= XFS_ISIZE(ip)); 15068f04c47aSChristoph Hellwig ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 15071da177e4SLinus Torvalds ASSERT(ip->i_itemp != NULL); 1508898621d5SChristoph Hellwig ASSERT(ip->i_itemp->ili_lock_flags == 0); 15091da177e4SLinus Torvalds ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); 15101da177e4SLinus Torvalds 1511673e8e59SChristoph Hellwig trace_xfs_itruncate_extents_start(ip, new_size); 1512673e8e59SChristoph Hellwig 15131da177e4SLinus Torvalds /* 15141da177e4SLinus Torvalds * Since it is possible for space to become allocated beyond 15151da177e4SLinus Torvalds * the end of the file (in a crash where the space is allocated 15161da177e4SLinus Torvalds * but the inode size is not yet updated), simply remove any 15171da177e4SLinus Torvalds * blocks which show up between the new EOF and the maximum 15181da177e4SLinus Torvalds * possible file size. If the first block to be removed is 15191da177e4SLinus Torvalds * beyond the maximum file size (ie it is the same as last_block), 15201da177e4SLinus Torvalds * then there is nothing to do. 15211da177e4SLinus Torvalds */ 15228f04c47aSChristoph Hellwig first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); 152332972383SDave Chinner last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); 15248f04c47aSChristoph Hellwig if (first_unmap_block == last_block) 15258f04c47aSChristoph Hellwig return 0; 15268f04c47aSChristoph Hellwig 15278f04c47aSChristoph Hellwig ASSERT(first_unmap_block < last_block); 15281da177e4SLinus Torvalds unmap_len = last_block - first_unmap_block + 1; 15291da177e4SLinus Torvalds while (!done) { 15309d87c319SEric Sandeen xfs_bmap_init(&free_list, &first_block); 15318f04c47aSChristoph Hellwig error = xfs_bunmapi(tp, ip, 15323e57ecf6SOlaf Weber first_unmap_block, unmap_len, 15338f04c47aSChristoph Hellwig xfs_bmapi_aflag(whichfork), 15341da177e4SLinus Torvalds XFS_ITRUNC_MAX_EXTENTS, 15353e57ecf6SOlaf Weber &first_block, &free_list, 1536b4e9181eSChristoph Hellwig &done); 15378f04c47aSChristoph Hellwig if (error) 15388f04c47aSChristoph Hellwig goto out_bmap_cancel; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds /* 15411da177e4SLinus Torvalds * Duplicate the transaction that has the permanent 15421da177e4SLinus Torvalds * reservation and commit the old transaction. 15431da177e4SLinus Torvalds */ 15448f04c47aSChristoph Hellwig error = xfs_bmap_finish(&tp, &free_list, &committed); 1545898621d5SChristoph Hellwig if (committed) 1546ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, 0); 15478f04c47aSChristoph Hellwig if (error) 15488f04c47aSChristoph Hellwig goto out_bmap_cancel; 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds if (committed) { 15511da177e4SLinus Torvalds /* 1552f6485057SDavid Chinner * Mark the inode dirty so it will be logged and 1553e5720eecSDavid Chinner * moved forward in the log as part of every commit. 15541da177e4SLinus Torvalds */ 15558f04c47aSChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 15561da177e4SLinus Torvalds } 1557f6485057SDavid Chinner 15588f04c47aSChristoph Hellwig ntp = xfs_trans_dup(tp); 15598f04c47aSChristoph Hellwig error = xfs_trans_commit(tp, 0); 15608f04c47aSChristoph Hellwig tp = ntp; 1561f6485057SDavid Chinner 1562ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, 0); 1563f6485057SDavid Chinner 1564cc09c0dcSDave Chinner if (error) 15658f04c47aSChristoph Hellwig goto out; 15668f04c47aSChristoph Hellwig 1567cc09c0dcSDave Chinner /* 15688f04c47aSChristoph Hellwig * Transaction commit worked ok so we can drop the extra ticket 1569cc09c0dcSDave Chinner * reference that we gained in xfs_trans_dup() 1570cc09c0dcSDave Chinner */ 15718f04c47aSChristoph Hellwig xfs_log_ticket_put(tp->t_ticket); 15723d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); 15731da177e4SLinus Torvalds if (error) 15748f04c47aSChristoph Hellwig goto out; 15751da177e4SLinus Torvalds } 15768f04c47aSChristoph Hellwig 1577673e8e59SChristoph Hellwig /* 1578673e8e59SChristoph Hellwig * Always re-log the inode so that our permanent transaction can keep 1579673e8e59SChristoph Hellwig * on rolling it forward in the log. 1580673e8e59SChristoph Hellwig */ 1581673e8e59SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1582673e8e59SChristoph Hellwig 1583673e8e59SChristoph Hellwig trace_xfs_itruncate_extents_end(ip, new_size); 1584673e8e59SChristoph Hellwig 15858f04c47aSChristoph Hellwig out: 15868f04c47aSChristoph Hellwig *tpp = tp; 15878f04c47aSChristoph Hellwig return error; 15888f04c47aSChristoph Hellwig out_bmap_cancel: 15891da177e4SLinus Torvalds /* 15908f04c47aSChristoph Hellwig * If the bunmapi call encounters an error, return to the caller where 15918f04c47aSChristoph Hellwig * the transaction can be properly aborted. We just need to make sure 15928f04c47aSChristoph Hellwig * we're not holding any resources that we were not when we came in. 15931da177e4SLinus Torvalds */ 15948f04c47aSChristoph Hellwig xfs_bmap_cancel(&free_list); 15958f04c47aSChristoph Hellwig goto out; 15968f04c47aSChristoph Hellwig } 15978f04c47aSChristoph Hellwig 1598c24b5dfaSDave Chinner int 1599c24b5dfaSDave Chinner xfs_release( 1600c24b5dfaSDave Chinner xfs_inode_t *ip) 1601c24b5dfaSDave Chinner { 1602c24b5dfaSDave Chinner xfs_mount_t *mp = ip->i_mount; 1603c24b5dfaSDave Chinner int error; 1604c24b5dfaSDave Chinner 1605c24b5dfaSDave Chinner if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0)) 1606c24b5dfaSDave Chinner return 0; 1607c24b5dfaSDave Chinner 1608c24b5dfaSDave Chinner /* If this is a read-only mount, don't do this (would generate I/O) */ 1609c24b5dfaSDave Chinner if (mp->m_flags & XFS_MOUNT_RDONLY) 1610c24b5dfaSDave Chinner return 0; 1611c24b5dfaSDave Chinner 1612c24b5dfaSDave Chinner if (!XFS_FORCED_SHUTDOWN(mp)) { 1613c24b5dfaSDave Chinner int truncated; 1614c24b5dfaSDave Chinner 1615c24b5dfaSDave Chinner /* 1616c24b5dfaSDave Chinner * If we previously truncated this file and removed old data 1617c24b5dfaSDave Chinner * in the process, we want to initiate "early" writeout on 1618c24b5dfaSDave Chinner * the last close. This is an attempt to combat the notorious 1619c24b5dfaSDave Chinner * NULL files problem which is particularly noticeable from a 1620c24b5dfaSDave Chinner * truncate down, buffered (re-)write (delalloc), followed by 1621c24b5dfaSDave Chinner * a crash. What we are effectively doing here is 1622c24b5dfaSDave Chinner * significantly reducing the time window where we'd otherwise 1623c24b5dfaSDave Chinner * be exposed to that problem. 1624c24b5dfaSDave Chinner */ 1625c24b5dfaSDave Chinner truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); 1626c24b5dfaSDave Chinner if (truncated) { 1627c24b5dfaSDave Chinner xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); 1628eac152b4SDave Chinner if (ip->i_delayed_blks > 0) { 16292451337dSDave Chinner error = filemap_flush(VFS_I(ip)->i_mapping); 1630c24b5dfaSDave Chinner if (error) 1631c24b5dfaSDave Chinner return error; 1632c24b5dfaSDave Chinner } 1633c24b5dfaSDave Chinner } 1634c24b5dfaSDave Chinner } 1635c24b5dfaSDave Chinner 1636c24b5dfaSDave Chinner if (ip->i_d.di_nlink == 0) 1637c24b5dfaSDave Chinner return 0; 1638c24b5dfaSDave Chinner 1639c24b5dfaSDave Chinner if (xfs_can_free_eofblocks(ip, false)) { 1640c24b5dfaSDave Chinner 1641c24b5dfaSDave Chinner /* 1642c24b5dfaSDave Chinner * If we can't get the iolock just skip truncating the blocks 1643c24b5dfaSDave Chinner * past EOF because we could deadlock with the mmap_sem 1644c24b5dfaSDave Chinner * otherwise. We'll get another chance to drop them once the 1645c24b5dfaSDave Chinner * last reference to the inode is dropped, so we'll never leak 1646c24b5dfaSDave Chinner * blocks permanently. 1647c24b5dfaSDave Chinner * 1648c24b5dfaSDave Chinner * Further, check if the inode is being opened, written and 1649c24b5dfaSDave Chinner * closed frequently and we have delayed allocation blocks 1650c24b5dfaSDave Chinner * outstanding (e.g. streaming writes from the NFS server), 1651c24b5dfaSDave Chinner * truncating the blocks past EOF will cause fragmentation to 1652c24b5dfaSDave Chinner * occur. 1653c24b5dfaSDave Chinner * 1654c24b5dfaSDave Chinner * In this case don't do the truncation, either, but we have to 1655c24b5dfaSDave Chinner * be careful how we detect this case. Blocks beyond EOF show 1656c24b5dfaSDave Chinner * up as i_delayed_blks even when the inode is clean, so we 1657c24b5dfaSDave Chinner * need to truncate them away first before checking for a dirty 1658c24b5dfaSDave Chinner * release. Hence on the first dirty close we will still remove 1659c24b5dfaSDave Chinner * the speculative allocation, but after that we will leave it 1660c24b5dfaSDave Chinner * in place. 1661c24b5dfaSDave Chinner */ 1662c24b5dfaSDave Chinner if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) 1663c24b5dfaSDave Chinner return 0; 1664c24b5dfaSDave Chinner 1665c24b5dfaSDave Chinner error = xfs_free_eofblocks(mp, ip, true); 16662451337dSDave Chinner if (error && error != -EAGAIN) 1667c24b5dfaSDave Chinner return error; 1668c24b5dfaSDave Chinner 1669c24b5dfaSDave Chinner /* delalloc blocks after truncation means it really is dirty */ 1670c24b5dfaSDave Chinner if (ip->i_delayed_blks) 1671c24b5dfaSDave Chinner xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); 1672c24b5dfaSDave Chinner } 1673c24b5dfaSDave Chinner return 0; 1674c24b5dfaSDave Chinner } 1675c24b5dfaSDave Chinner 1676c24b5dfaSDave Chinner /* 1677f7be2d7fSBrian Foster * xfs_inactive_truncate 1678f7be2d7fSBrian Foster * 1679f7be2d7fSBrian Foster * Called to perform a truncate when an inode becomes unlinked. 1680f7be2d7fSBrian Foster */ 1681f7be2d7fSBrian Foster STATIC int 1682f7be2d7fSBrian Foster xfs_inactive_truncate( 1683f7be2d7fSBrian Foster struct xfs_inode *ip) 1684f7be2d7fSBrian Foster { 1685f7be2d7fSBrian Foster struct xfs_mount *mp = ip->i_mount; 1686f7be2d7fSBrian Foster struct xfs_trans *tp; 1687f7be2d7fSBrian Foster int error; 1688f7be2d7fSBrian Foster 1689f7be2d7fSBrian Foster tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 1690f7be2d7fSBrian Foster error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); 1691f7be2d7fSBrian Foster if (error) { 1692f7be2d7fSBrian Foster ASSERT(XFS_FORCED_SHUTDOWN(mp)); 1693f7be2d7fSBrian Foster xfs_trans_cancel(tp, 0); 1694f7be2d7fSBrian Foster return error; 1695f7be2d7fSBrian Foster } 1696f7be2d7fSBrian Foster 1697f7be2d7fSBrian Foster xfs_ilock(ip, XFS_ILOCK_EXCL); 1698f7be2d7fSBrian Foster xfs_trans_ijoin(tp, ip, 0); 1699f7be2d7fSBrian Foster 1700f7be2d7fSBrian Foster /* 1701f7be2d7fSBrian Foster * Log the inode size first to prevent stale data exposure in the event 1702f7be2d7fSBrian Foster * of a system crash before the truncate completes. See the related 1703f7be2d7fSBrian Foster * comment in xfs_setattr_size() for details. 1704f7be2d7fSBrian Foster */ 1705f7be2d7fSBrian Foster ip->i_d.di_size = 0; 1706f7be2d7fSBrian Foster xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1707f7be2d7fSBrian Foster 1708f7be2d7fSBrian Foster error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); 1709f7be2d7fSBrian Foster if (error) 1710f7be2d7fSBrian Foster goto error_trans_cancel; 1711f7be2d7fSBrian Foster 1712f7be2d7fSBrian Foster ASSERT(ip->i_d.di_nextents == 0); 1713f7be2d7fSBrian Foster 1714f7be2d7fSBrian Foster error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1715f7be2d7fSBrian Foster if (error) 1716f7be2d7fSBrian Foster goto error_unlock; 1717f7be2d7fSBrian Foster 1718f7be2d7fSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL); 1719f7be2d7fSBrian Foster return 0; 1720f7be2d7fSBrian Foster 1721f7be2d7fSBrian Foster error_trans_cancel: 1722f7be2d7fSBrian Foster xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 1723f7be2d7fSBrian Foster error_unlock: 1724f7be2d7fSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL); 1725f7be2d7fSBrian Foster return error; 1726f7be2d7fSBrian Foster } 1727f7be2d7fSBrian Foster 1728f7be2d7fSBrian Foster /* 172988877d2bSBrian Foster * xfs_inactive_ifree() 173088877d2bSBrian Foster * 173188877d2bSBrian Foster * Perform the inode free when an inode is unlinked. 173288877d2bSBrian Foster */ 173388877d2bSBrian Foster STATIC int 173488877d2bSBrian Foster xfs_inactive_ifree( 173588877d2bSBrian Foster struct xfs_inode *ip) 173688877d2bSBrian Foster { 173788877d2bSBrian Foster xfs_bmap_free_t free_list; 173888877d2bSBrian Foster xfs_fsblock_t first_block; 173988877d2bSBrian Foster int committed; 174088877d2bSBrian Foster struct xfs_mount *mp = ip->i_mount; 174188877d2bSBrian Foster struct xfs_trans *tp; 174288877d2bSBrian Foster int error; 174388877d2bSBrian Foster 174488877d2bSBrian Foster tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 17459d43b180SBrian Foster 17469d43b180SBrian Foster /* 17479d43b180SBrian Foster * The ifree transaction might need to allocate blocks for record 17489d43b180SBrian Foster * insertion to the finobt. We don't want to fail here at ENOSPC, so 17499d43b180SBrian Foster * allow ifree to dip into the reserved block pool if necessary. 17509d43b180SBrian Foster * 17519d43b180SBrian Foster * Freeing large sets of inodes generally means freeing inode chunks, 17529d43b180SBrian Foster * directory and file data blocks, so this should be relatively safe. 17539d43b180SBrian Foster * Only under severe circumstances should it be possible to free enough 17549d43b180SBrian Foster * inodes to exhaust the reserve block pool via finobt expansion while 17559d43b180SBrian Foster * at the same time not creating free space in the filesystem. 17569d43b180SBrian Foster * 17579d43b180SBrian Foster * Send a warning if the reservation does happen to fail, as the inode 17589d43b180SBrian Foster * now remains allocated and sits on the unlinked list until the fs is 17599d43b180SBrian Foster * repaired. 17609d43b180SBrian Foster */ 17619d43b180SBrian Foster tp->t_flags |= XFS_TRANS_RESERVE; 17629d43b180SBrian Foster error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 17639d43b180SBrian Foster XFS_IFREE_SPACE_RES(mp), 0); 176488877d2bSBrian Foster if (error) { 17652451337dSDave Chinner if (error == -ENOSPC) { 17669d43b180SBrian Foster xfs_warn_ratelimited(mp, 17679d43b180SBrian Foster "Failed to remove inode(s) from unlinked list. " 17689d43b180SBrian Foster "Please free space, unmount and run xfs_repair."); 17699d43b180SBrian Foster } else { 177088877d2bSBrian Foster ASSERT(XFS_FORCED_SHUTDOWN(mp)); 17719d43b180SBrian Foster } 177288877d2bSBrian Foster xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); 177388877d2bSBrian Foster return error; 177488877d2bSBrian Foster } 177588877d2bSBrian Foster 177688877d2bSBrian Foster xfs_ilock(ip, XFS_ILOCK_EXCL); 177788877d2bSBrian Foster xfs_trans_ijoin(tp, ip, 0); 177888877d2bSBrian Foster 177988877d2bSBrian Foster xfs_bmap_init(&free_list, &first_block); 178088877d2bSBrian Foster error = xfs_ifree(tp, ip, &free_list); 178188877d2bSBrian Foster if (error) { 178288877d2bSBrian Foster /* 178388877d2bSBrian Foster * If we fail to free the inode, shut down. The cancel 178488877d2bSBrian Foster * might do that, we need to make sure. Otherwise the 178588877d2bSBrian Foster * inode might be lost for a long time or forever. 178688877d2bSBrian Foster */ 178788877d2bSBrian Foster if (!XFS_FORCED_SHUTDOWN(mp)) { 178888877d2bSBrian Foster xfs_notice(mp, "%s: xfs_ifree returned error %d", 178988877d2bSBrian Foster __func__, error); 179088877d2bSBrian Foster xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 179188877d2bSBrian Foster } 179288877d2bSBrian Foster xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); 179388877d2bSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL); 179488877d2bSBrian Foster return error; 179588877d2bSBrian Foster } 179688877d2bSBrian Foster 179788877d2bSBrian Foster /* 179888877d2bSBrian Foster * Credit the quota account(s). The inode is gone. 179988877d2bSBrian Foster */ 180088877d2bSBrian Foster xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); 180188877d2bSBrian Foster 180288877d2bSBrian Foster /* 180388877d2bSBrian Foster * Just ignore errors at this point. There is nothing we can 180488877d2bSBrian Foster * do except to try to keep going. Make sure it's not a silent 180588877d2bSBrian Foster * error. 180688877d2bSBrian Foster */ 180788877d2bSBrian Foster error = xfs_bmap_finish(&tp, &free_list, &committed); 180888877d2bSBrian Foster if (error) 180988877d2bSBrian Foster xfs_notice(mp, "%s: xfs_bmap_finish returned error %d", 181088877d2bSBrian Foster __func__, error); 181188877d2bSBrian Foster error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 181288877d2bSBrian Foster if (error) 181388877d2bSBrian Foster xfs_notice(mp, "%s: xfs_trans_commit returned error %d", 181488877d2bSBrian Foster __func__, error); 181588877d2bSBrian Foster 181688877d2bSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL); 181788877d2bSBrian Foster return 0; 181888877d2bSBrian Foster } 181988877d2bSBrian Foster 182088877d2bSBrian Foster /* 1821c24b5dfaSDave Chinner * xfs_inactive 1822c24b5dfaSDave Chinner * 1823c24b5dfaSDave Chinner * This is called when the vnode reference count for the vnode 1824c24b5dfaSDave Chinner * goes to zero. If the file has been unlinked, then it must 1825c24b5dfaSDave Chinner * now be truncated. Also, we clear all of the read-ahead state 1826c24b5dfaSDave Chinner * kept for the inode here since the file is now closed. 1827c24b5dfaSDave Chinner */ 182874564fb4SBrian Foster void 1829c24b5dfaSDave Chinner xfs_inactive( 1830c24b5dfaSDave Chinner xfs_inode_t *ip) 1831c24b5dfaSDave Chinner { 18323d3c8b52SJie Liu struct xfs_mount *mp; 1833c24b5dfaSDave Chinner int error; 1834c24b5dfaSDave Chinner int truncate = 0; 1835c24b5dfaSDave Chinner 1836c24b5dfaSDave Chinner /* 1837c24b5dfaSDave Chinner * If the inode is already free, then there can be nothing 1838c24b5dfaSDave Chinner * to clean up here. 1839c24b5dfaSDave Chinner */ 1840d948709bSBen Myers if (ip->i_d.di_mode == 0) { 1841c24b5dfaSDave Chinner ASSERT(ip->i_df.if_real_bytes == 0); 1842c24b5dfaSDave Chinner ASSERT(ip->i_df.if_broot_bytes == 0); 184374564fb4SBrian Foster return; 1844c24b5dfaSDave Chinner } 1845c24b5dfaSDave Chinner 1846c24b5dfaSDave Chinner mp = ip->i_mount; 1847c24b5dfaSDave Chinner 1848c24b5dfaSDave Chinner /* If this is a read-only mount, don't do this (would generate I/O) */ 1849c24b5dfaSDave Chinner if (mp->m_flags & XFS_MOUNT_RDONLY) 185074564fb4SBrian Foster return; 1851c24b5dfaSDave Chinner 1852c24b5dfaSDave Chinner if (ip->i_d.di_nlink != 0) { 1853c24b5dfaSDave Chinner /* 1854c24b5dfaSDave Chinner * force is true because we are evicting an inode from the 1855c24b5dfaSDave Chinner * cache. Post-eof blocks must be freed, lest we end up with 1856c24b5dfaSDave Chinner * broken free space accounting. 1857c24b5dfaSDave Chinner */ 185874564fb4SBrian Foster if (xfs_can_free_eofblocks(ip, true)) 185974564fb4SBrian Foster xfs_free_eofblocks(mp, ip, false); 186074564fb4SBrian Foster 186174564fb4SBrian Foster return; 1862c24b5dfaSDave Chinner } 1863c24b5dfaSDave Chinner 1864c24b5dfaSDave Chinner if (S_ISREG(ip->i_d.di_mode) && 1865c24b5dfaSDave Chinner (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 || 1866c24b5dfaSDave Chinner ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0)) 1867c24b5dfaSDave Chinner truncate = 1; 1868c24b5dfaSDave Chinner 1869c24b5dfaSDave Chinner error = xfs_qm_dqattach(ip, 0); 1870c24b5dfaSDave Chinner if (error) 187174564fb4SBrian Foster return; 1872c24b5dfaSDave Chinner 1873f7be2d7fSBrian Foster if (S_ISLNK(ip->i_d.di_mode)) 187436b21ddeSBrian Foster error = xfs_inactive_symlink(ip); 1875f7be2d7fSBrian Foster else if (truncate) 1876f7be2d7fSBrian Foster error = xfs_inactive_truncate(ip); 187736b21ddeSBrian Foster if (error) 187874564fb4SBrian Foster return; 1879c24b5dfaSDave Chinner 1880c24b5dfaSDave Chinner /* 1881c24b5dfaSDave Chinner * If there are attributes associated with the file then blow them away 1882c24b5dfaSDave Chinner * now. The code calls a routine that recursively deconstructs the 1883c24b5dfaSDave Chinner * attribute fork. We need to just commit the current transaction 1884c24b5dfaSDave Chinner * because we can't use it for xfs_attr_inactive(). 1885c24b5dfaSDave Chinner */ 1886c24b5dfaSDave Chinner if (ip->i_d.di_anextents > 0) { 1887c24b5dfaSDave Chinner ASSERT(ip->i_d.di_forkoff != 0); 1888c24b5dfaSDave Chinner 1889c24b5dfaSDave Chinner error = xfs_attr_inactive(ip); 1890c24b5dfaSDave Chinner if (error) 189174564fb4SBrian Foster return; 1892c24b5dfaSDave Chinner } 1893c24b5dfaSDave Chinner 1894c24b5dfaSDave Chinner if (ip->i_afp) 1895c24b5dfaSDave Chinner xfs_idestroy_fork(ip, XFS_ATTR_FORK); 1896c24b5dfaSDave Chinner 1897c24b5dfaSDave Chinner ASSERT(ip->i_d.di_anextents == 0); 1898c24b5dfaSDave Chinner 1899c24b5dfaSDave Chinner /* 1900c24b5dfaSDave Chinner * Free the inode. 1901c24b5dfaSDave Chinner */ 190288877d2bSBrian Foster error = xfs_inactive_ifree(ip); 1903c24b5dfaSDave Chinner if (error) 190474564fb4SBrian Foster return; 1905c24b5dfaSDave Chinner 1906c24b5dfaSDave Chinner /* 1907c24b5dfaSDave Chinner * Release the dquots held by inode, if any. 1908c24b5dfaSDave Chinner */ 1909c24b5dfaSDave Chinner xfs_qm_dqdetach(ip); 1910c24b5dfaSDave Chinner } 1911c24b5dfaSDave Chinner 19121da177e4SLinus Torvalds /* 19131da177e4SLinus Torvalds * This is called when the inode's link count goes to 0. 19141da177e4SLinus Torvalds * We place the on-disk inode on a list in the AGI. It 19151da177e4SLinus Torvalds * will be pulled from this list when the inode is freed. 19161da177e4SLinus Torvalds */ 19171da177e4SLinus Torvalds int 19181da177e4SLinus Torvalds xfs_iunlink( 19191da177e4SLinus Torvalds xfs_trans_t *tp, 19201da177e4SLinus Torvalds xfs_inode_t *ip) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds xfs_mount_t *mp; 19231da177e4SLinus Torvalds xfs_agi_t *agi; 19241da177e4SLinus Torvalds xfs_dinode_t *dip; 19251da177e4SLinus Torvalds xfs_buf_t *agibp; 19261da177e4SLinus Torvalds xfs_buf_t *ibp; 19271da177e4SLinus Torvalds xfs_agino_t agino; 19281da177e4SLinus Torvalds short bucket_index; 19291da177e4SLinus Torvalds int offset; 19301da177e4SLinus Torvalds int error; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds ASSERT(ip->i_d.di_nlink == 0); 19331da177e4SLinus Torvalds ASSERT(ip->i_d.di_mode != 0); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds mp = tp->t_mountp; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /* 19381da177e4SLinus Torvalds * Get the agi buffer first. It ensures lock ordering 19391da177e4SLinus Torvalds * on the list. 19401da177e4SLinus Torvalds */ 19415e1be0fbSChristoph Hellwig error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp); 1942859d7182SVlad Apostolov if (error) 19431da177e4SLinus Torvalds return error; 19441da177e4SLinus Torvalds agi = XFS_BUF_TO_AGI(agibp); 19455e1be0fbSChristoph Hellwig 19461da177e4SLinus Torvalds /* 19471da177e4SLinus Torvalds * Get the index into the agi hash table for the 19481da177e4SLinus Torvalds * list this inode will go on. 19491da177e4SLinus Torvalds */ 19501da177e4SLinus Torvalds agino = XFS_INO_TO_AGINO(mp, ip->i_ino); 19511da177e4SLinus Torvalds ASSERT(agino != 0); 19521da177e4SLinus Torvalds bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; 19531da177e4SLinus Torvalds ASSERT(agi->agi_unlinked[bucket_index]); 195416259e7dSChristoph Hellwig ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); 19551da177e4SLinus Torvalds 195669ef921bSChristoph Hellwig if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * There is already another inode in the bucket we need 19591da177e4SLinus Torvalds * to add ourselves to. Add us at the front of the list. 19601da177e4SLinus Torvalds * Here we put the head pointer into our next pointer, 19611da177e4SLinus Torvalds * and then we fall through to point the head at us. 19621da177e4SLinus Torvalds */ 1963475ee413SChristoph Hellwig error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 1964475ee413SChristoph Hellwig 0, 0); 1965c319b58bSVlad Apostolov if (error) 1966c319b58bSVlad Apostolov return error; 1967c319b58bSVlad Apostolov 196869ef921bSChristoph Hellwig ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); 19691da177e4SLinus Torvalds dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; 197092bfc6e7SChristoph Hellwig offset = ip->i_imap.im_boffset + 19711da177e4SLinus Torvalds offsetof(xfs_dinode_t, di_next_unlinked); 19720a32c26eSDave Chinner 19730a32c26eSDave Chinner /* need to recalc the inode CRC if appropriate */ 19740a32c26eSDave Chinner xfs_dinode_calc_crc(mp, dip); 19750a32c26eSDave Chinner 19761da177e4SLinus Torvalds xfs_trans_inode_buf(tp, ibp); 19771da177e4SLinus Torvalds xfs_trans_log_buf(tp, ibp, offset, 19781da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 19791da177e4SLinus Torvalds xfs_inobp_check(mp, ibp); 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds /* 19831da177e4SLinus Torvalds * Point the bucket head pointer at the inode being inserted. 19841da177e4SLinus Torvalds */ 19851da177e4SLinus Torvalds ASSERT(agino != 0); 198616259e7dSChristoph Hellwig agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); 19871da177e4SLinus Torvalds offset = offsetof(xfs_agi_t, agi_unlinked) + 19881da177e4SLinus Torvalds (sizeof(xfs_agino_t) * bucket_index); 1989f19b872bSDave Chinner xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF); 19901da177e4SLinus Torvalds xfs_trans_log_buf(tp, agibp, offset, 19911da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 19921da177e4SLinus Torvalds return 0; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /* 19961da177e4SLinus Torvalds * Pull the on-disk inode from the AGI unlinked list. 19971da177e4SLinus Torvalds */ 19981da177e4SLinus Torvalds STATIC int 19991da177e4SLinus Torvalds xfs_iunlink_remove( 20001da177e4SLinus Torvalds xfs_trans_t *tp, 20011da177e4SLinus Torvalds xfs_inode_t *ip) 20021da177e4SLinus Torvalds { 20031da177e4SLinus Torvalds xfs_ino_t next_ino; 20041da177e4SLinus Torvalds xfs_mount_t *mp; 20051da177e4SLinus Torvalds xfs_agi_t *agi; 20061da177e4SLinus Torvalds xfs_dinode_t *dip; 20071da177e4SLinus Torvalds xfs_buf_t *agibp; 20081da177e4SLinus Torvalds xfs_buf_t *ibp; 20091da177e4SLinus Torvalds xfs_agnumber_t agno; 20101da177e4SLinus Torvalds xfs_agino_t agino; 20111da177e4SLinus Torvalds xfs_agino_t next_agino; 20121da177e4SLinus Torvalds xfs_buf_t *last_ibp; 20136fdf8cccSNathan Scott xfs_dinode_t *last_dip = NULL; 20141da177e4SLinus Torvalds short bucket_index; 20156fdf8cccSNathan Scott int offset, last_offset = 0; 20161da177e4SLinus Torvalds int error; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds mp = tp->t_mountp; 20191da177e4SLinus Torvalds agno = XFS_INO_TO_AGNO(mp, ip->i_ino); 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* 20221da177e4SLinus Torvalds * Get the agi buffer first. It ensures lock ordering 20231da177e4SLinus Torvalds * on the list. 20241da177e4SLinus Torvalds */ 20255e1be0fbSChristoph Hellwig error = xfs_read_agi(mp, tp, agno, &agibp); 20265e1be0fbSChristoph Hellwig if (error) 20271da177e4SLinus Torvalds return error; 20285e1be0fbSChristoph Hellwig 20291da177e4SLinus Torvalds agi = XFS_BUF_TO_AGI(agibp); 20305e1be0fbSChristoph Hellwig 20311da177e4SLinus Torvalds /* 20321da177e4SLinus Torvalds * Get the index into the agi hash table for the 20331da177e4SLinus Torvalds * list this inode will go on. 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds agino = XFS_INO_TO_AGINO(mp, ip->i_ino); 20361da177e4SLinus Torvalds ASSERT(agino != 0); 20371da177e4SLinus Torvalds bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; 203869ef921bSChristoph Hellwig ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); 20391da177e4SLinus Torvalds ASSERT(agi->agi_unlinked[bucket_index]); 20401da177e4SLinus Torvalds 204116259e7dSChristoph Hellwig if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { 20421da177e4SLinus Torvalds /* 2043475ee413SChristoph Hellwig * We're at the head of the list. Get the inode's on-disk 2044475ee413SChristoph Hellwig * buffer to see if there is anyone after us on the list. 2045475ee413SChristoph Hellwig * Only modify our next pointer if it is not already NULLAGINO. 2046475ee413SChristoph Hellwig * This saves us the overhead of dealing with the buffer when 2047475ee413SChristoph Hellwig * there is no need to change it. 20481da177e4SLinus Torvalds */ 2049475ee413SChristoph Hellwig error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 2050475ee413SChristoph Hellwig 0, 0); 20511da177e4SLinus Torvalds if (error) { 2052475ee413SChristoph Hellwig xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.", 20530b932cccSDave Chinner __func__, error); 20541da177e4SLinus Torvalds return error; 20551da177e4SLinus Torvalds } 2056347d1c01SChristoph Hellwig next_agino = be32_to_cpu(dip->di_next_unlinked); 20571da177e4SLinus Torvalds ASSERT(next_agino != 0); 20581da177e4SLinus Torvalds if (next_agino != NULLAGINO) { 2059347d1c01SChristoph Hellwig dip->di_next_unlinked = cpu_to_be32(NULLAGINO); 206092bfc6e7SChristoph Hellwig offset = ip->i_imap.im_boffset + 20611da177e4SLinus Torvalds offsetof(xfs_dinode_t, di_next_unlinked); 20620a32c26eSDave Chinner 20630a32c26eSDave Chinner /* need to recalc the inode CRC if appropriate */ 20640a32c26eSDave Chinner xfs_dinode_calc_crc(mp, dip); 20650a32c26eSDave Chinner 20661da177e4SLinus Torvalds xfs_trans_inode_buf(tp, ibp); 20671da177e4SLinus Torvalds xfs_trans_log_buf(tp, ibp, offset, 20681da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 20691da177e4SLinus Torvalds xfs_inobp_check(mp, ibp); 20701da177e4SLinus Torvalds } else { 20711da177e4SLinus Torvalds xfs_trans_brelse(tp, ibp); 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds /* 20741da177e4SLinus Torvalds * Point the bucket head pointer at the next inode. 20751da177e4SLinus Torvalds */ 20761da177e4SLinus Torvalds ASSERT(next_agino != 0); 20771da177e4SLinus Torvalds ASSERT(next_agino != agino); 207816259e7dSChristoph Hellwig agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); 20791da177e4SLinus Torvalds offset = offsetof(xfs_agi_t, agi_unlinked) + 20801da177e4SLinus Torvalds (sizeof(xfs_agino_t) * bucket_index); 2081f19b872bSDave Chinner xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF); 20821da177e4SLinus Torvalds xfs_trans_log_buf(tp, agibp, offset, 20831da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 20841da177e4SLinus Torvalds } else { 20851da177e4SLinus Torvalds /* 20861da177e4SLinus Torvalds * We need to search the list for the inode being freed. 20871da177e4SLinus Torvalds */ 208816259e7dSChristoph Hellwig next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); 20891da177e4SLinus Torvalds last_ibp = NULL; 20901da177e4SLinus Torvalds while (next_agino != agino) { 2091129dbc9aSChristoph Hellwig struct xfs_imap imap; 2092129dbc9aSChristoph Hellwig 2093129dbc9aSChristoph Hellwig if (last_ibp) 20941da177e4SLinus Torvalds xfs_trans_brelse(tp, last_ibp); 2095129dbc9aSChristoph Hellwig 2096129dbc9aSChristoph Hellwig imap.im_blkno = 0; 20971da177e4SLinus Torvalds next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); 2098129dbc9aSChristoph Hellwig 2099129dbc9aSChristoph Hellwig error = xfs_imap(mp, tp, next_ino, &imap, 0); 21001da177e4SLinus Torvalds if (error) { 21010b932cccSDave Chinner xfs_warn(mp, 2102129dbc9aSChristoph Hellwig "%s: xfs_imap returned error %d.", 21030b932cccSDave Chinner __func__, error); 21041da177e4SLinus Torvalds return error; 21051da177e4SLinus Torvalds } 2106129dbc9aSChristoph Hellwig 2107129dbc9aSChristoph Hellwig error = xfs_imap_to_bp(mp, tp, &imap, &last_dip, 2108129dbc9aSChristoph Hellwig &last_ibp, 0, 0); 2109129dbc9aSChristoph Hellwig if (error) { 2110129dbc9aSChristoph Hellwig xfs_warn(mp, 2111129dbc9aSChristoph Hellwig "%s: xfs_imap_to_bp returned error %d.", 2112129dbc9aSChristoph Hellwig __func__, error); 2113129dbc9aSChristoph Hellwig return error; 2114129dbc9aSChristoph Hellwig } 2115129dbc9aSChristoph Hellwig 2116129dbc9aSChristoph Hellwig last_offset = imap.im_boffset; 2117347d1c01SChristoph Hellwig next_agino = be32_to_cpu(last_dip->di_next_unlinked); 21181da177e4SLinus Torvalds ASSERT(next_agino != NULLAGINO); 21191da177e4SLinus Torvalds ASSERT(next_agino != 0); 21201da177e4SLinus Torvalds } 2121475ee413SChristoph Hellwig 21221da177e4SLinus Torvalds /* 2123475ee413SChristoph Hellwig * Now last_ibp points to the buffer previous to us on the 2124475ee413SChristoph Hellwig * unlinked list. Pull us from the list. 21251da177e4SLinus Torvalds */ 2126475ee413SChristoph Hellwig error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 2127475ee413SChristoph Hellwig 0, 0); 21281da177e4SLinus Torvalds if (error) { 2129475ee413SChristoph Hellwig xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.", 21300b932cccSDave Chinner __func__, error); 21311da177e4SLinus Torvalds return error; 21321da177e4SLinus Torvalds } 2133347d1c01SChristoph Hellwig next_agino = be32_to_cpu(dip->di_next_unlinked); 21341da177e4SLinus Torvalds ASSERT(next_agino != 0); 21351da177e4SLinus Torvalds ASSERT(next_agino != agino); 21361da177e4SLinus Torvalds if (next_agino != NULLAGINO) { 2137347d1c01SChristoph Hellwig dip->di_next_unlinked = cpu_to_be32(NULLAGINO); 213892bfc6e7SChristoph Hellwig offset = ip->i_imap.im_boffset + 21391da177e4SLinus Torvalds offsetof(xfs_dinode_t, di_next_unlinked); 21400a32c26eSDave Chinner 21410a32c26eSDave Chinner /* need to recalc the inode CRC if appropriate */ 21420a32c26eSDave Chinner xfs_dinode_calc_crc(mp, dip); 21430a32c26eSDave Chinner 21441da177e4SLinus Torvalds xfs_trans_inode_buf(tp, ibp); 21451da177e4SLinus Torvalds xfs_trans_log_buf(tp, ibp, offset, 21461da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 21471da177e4SLinus Torvalds xfs_inobp_check(mp, ibp); 21481da177e4SLinus Torvalds } else { 21491da177e4SLinus Torvalds xfs_trans_brelse(tp, ibp); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds /* 21521da177e4SLinus Torvalds * Point the previous inode on the list to the next inode. 21531da177e4SLinus Torvalds */ 2154347d1c01SChristoph Hellwig last_dip->di_next_unlinked = cpu_to_be32(next_agino); 21551da177e4SLinus Torvalds ASSERT(next_agino != 0); 21561da177e4SLinus Torvalds offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); 21570a32c26eSDave Chinner 21580a32c26eSDave Chinner /* need to recalc the inode CRC if appropriate */ 21590a32c26eSDave Chinner xfs_dinode_calc_crc(mp, last_dip); 21600a32c26eSDave Chinner 21611da177e4SLinus Torvalds xfs_trans_inode_buf(tp, last_ibp); 21621da177e4SLinus Torvalds xfs_trans_log_buf(tp, last_ibp, offset, 21631da177e4SLinus Torvalds (offset + sizeof(xfs_agino_t) - 1)); 21641da177e4SLinus Torvalds xfs_inobp_check(mp, last_ibp); 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds return 0; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 21695b3eed75SDave Chinner /* 21700b8182dbSZhi Yong Wu * A big issue when freeing the inode cluster is that we _cannot_ skip any 21715b3eed75SDave Chinner * inodes that are in memory - they all must be marked stale and attached to 21725b3eed75SDave Chinner * the cluster buffer. 21735b3eed75SDave Chinner */ 21742a30f36dSChandra Seetharaman STATIC int 21751da177e4SLinus Torvalds xfs_ifree_cluster( 21761da177e4SLinus Torvalds xfs_inode_t *free_ip, 21771da177e4SLinus Torvalds xfs_trans_t *tp, 21781da177e4SLinus Torvalds xfs_ino_t inum) 21791da177e4SLinus Torvalds { 21801da177e4SLinus Torvalds xfs_mount_t *mp = free_ip->i_mount; 21811da177e4SLinus Torvalds int blks_per_cluster; 2182982e939eSJie Liu int inodes_per_cluster; 21831da177e4SLinus Torvalds int nbufs; 21845b257b4aSDave Chinner int i, j; 21851da177e4SLinus Torvalds xfs_daddr_t blkno; 21861da177e4SLinus Torvalds xfs_buf_t *bp; 21875b257b4aSDave Chinner xfs_inode_t *ip; 21881da177e4SLinus Torvalds xfs_inode_log_item_t *iip; 21891da177e4SLinus Torvalds xfs_log_item_t *lip; 21905017e97dSDave Chinner struct xfs_perag *pag; 21911da177e4SLinus Torvalds 21925017e97dSDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); 2193982e939eSJie Liu blks_per_cluster = xfs_icluster_size_fsb(mp); 2194982e939eSJie Liu inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog; 2195126cd105SJie Liu nbufs = mp->m_ialloc_blks / blks_per_cluster; 21961da177e4SLinus Torvalds 2197982e939eSJie Liu for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) { 21981da177e4SLinus Torvalds blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), 21991da177e4SLinus Torvalds XFS_INO_TO_AGBNO(mp, inum)); 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds /* 22025b257b4aSDave Chinner * We obtain and lock the backing buffer first in the process 22035b257b4aSDave Chinner * here, as we have to ensure that any dirty inode that we 22045b257b4aSDave Chinner * can't get the flush lock on is attached to the buffer. 22055b257b4aSDave Chinner * If we scan the in-memory inodes first, then buffer IO can 22065b257b4aSDave Chinner * complete before we get a lock on it, and hence we may fail 22075b257b4aSDave Chinner * to mark all the active inodes on the buffer stale. 22081da177e4SLinus Torvalds */ 22091da177e4SLinus Torvalds bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, 2210b6aff29fSDave Chinner mp->m_bsize * blks_per_cluster, 2211b6aff29fSDave Chinner XBF_UNMAPPED); 22121da177e4SLinus Torvalds 22132a30f36dSChandra Seetharaman if (!bp) 22142451337dSDave Chinner return -ENOMEM; 2215b0f539deSDave Chinner 2216b0f539deSDave Chinner /* 2217b0f539deSDave Chinner * This buffer may not have been correctly initialised as we 2218b0f539deSDave Chinner * didn't read it from disk. That's not important because we are 2219b0f539deSDave Chinner * only using to mark the buffer as stale in the log, and to 2220b0f539deSDave Chinner * attach stale cached inodes on it. That means it will never be 2221b0f539deSDave Chinner * dispatched for IO. If it is, we want to know about it, and we 2222b0f539deSDave Chinner * want it to fail. We can acheive this by adding a write 2223b0f539deSDave Chinner * verifier to the buffer. 2224b0f539deSDave Chinner */ 22251813dd64SDave Chinner bp->b_ops = &xfs_inode_buf_ops; 2226b0f539deSDave Chinner 22275b257b4aSDave Chinner /* 22285b257b4aSDave Chinner * Walk the inodes already attached to the buffer and mark them 22295b257b4aSDave Chinner * stale. These will all have the flush locks held, so an 22305b3eed75SDave Chinner * in-memory inode walk can't lock them. By marking them all 22315b3eed75SDave Chinner * stale first, we will not attempt to lock them in the loop 22325b3eed75SDave Chinner * below as the XFS_ISTALE flag will be set. 22335b257b4aSDave Chinner */ 2234adadbeefSChristoph Hellwig lip = bp->b_fspriv; 22351da177e4SLinus Torvalds while (lip) { 22361da177e4SLinus Torvalds if (lip->li_type == XFS_LI_INODE) { 22371da177e4SLinus Torvalds iip = (xfs_inode_log_item_t *)lip; 22381da177e4SLinus Torvalds ASSERT(iip->ili_logged == 1); 2239ca30b2a7SChristoph Hellwig lip->li_cb = xfs_istale_done; 22407b2e2a31SDavid Chinner xfs_trans_ail_copy_lsn(mp->m_ail, 22417b2e2a31SDavid Chinner &iip->ili_flush_lsn, 22427b2e2a31SDavid Chinner &iip->ili_item.li_lsn); 2243e5ffd2bbSDavid Chinner xfs_iflags_set(iip->ili_inode, XFS_ISTALE); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds lip = lip->li_bio_list; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22485b3eed75SDave Chinner 22495b257b4aSDave Chinner /* 22505b257b4aSDave Chinner * For each inode in memory attempt to add it to the inode 22515b257b4aSDave Chinner * buffer and set it up for being staled on buffer IO 22525b257b4aSDave Chinner * completion. This is safe as we've locked out tail pushing 22535b257b4aSDave Chinner * and flushing by locking the buffer. 22545b257b4aSDave Chinner * 22555b257b4aSDave Chinner * We have already marked every inode that was part of a 22565b257b4aSDave Chinner * transaction stale above, which means there is no point in 22575b257b4aSDave Chinner * even trying to lock them. 22585b257b4aSDave Chinner */ 2259982e939eSJie Liu for (i = 0; i < inodes_per_cluster; i++) { 22605b3eed75SDave Chinner retry: 22611a3e8f3dSDave Chinner rcu_read_lock(); 22625b257b4aSDave Chinner ip = radix_tree_lookup(&pag->pag_ici_root, 22635b257b4aSDave Chinner XFS_INO_TO_AGINO(mp, (inum + i))); 22641da177e4SLinus Torvalds 22651a3e8f3dSDave Chinner /* Inode not in memory, nothing to do */ 22661a3e8f3dSDave Chinner if (!ip) { 22671a3e8f3dSDave Chinner rcu_read_unlock(); 22685b257b4aSDave Chinner continue; 22695b257b4aSDave Chinner } 22705b257b4aSDave Chinner 22715b3eed75SDave Chinner /* 22721a3e8f3dSDave Chinner * because this is an RCU protected lookup, we could 22731a3e8f3dSDave Chinner * find a recently freed or even reallocated inode 22741a3e8f3dSDave Chinner * during the lookup. We need to check under the 22751a3e8f3dSDave Chinner * i_flags_lock for a valid inode here. Skip it if it 22761a3e8f3dSDave Chinner * is not valid, the wrong inode or stale. 22771a3e8f3dSDave Chinner */ 22781a3e8f3dSDave Chinner spin_lock(&ip->i_flags_lock); 22791a3e8f3dSDave Chinner if (ip->i_ino != inum + i || 22801a3e8f3dSDave Chinner __xfs_iflags_test(ip, XFS_ISTALE)) { 22811a3e8f3dSDave Chinner spin_unlock(&ip->i_flags_lock); 22821a3e8f3dSDave Chinner rcu_read_unlock(); 22831a3e8f3dSDave Chinner continue; 22841a3e8f3dSDave Chinner } 22851a3e8f3dSDave Chinner spin_unlock(&ip->i_flags_lock); 22861a3e8f3dSDave Chinner 22871a3e8f3dSDave Chinner /* 22885b3eed75SDave Chinner * Don't try to lock/unlock the current inode, but we 22895b3eed75SDave Chinner * _cannot_ skip the other inodes that we did not find 22905b3eed75SDave Chinner * in the list attached to the buffer and are not 22915b3eed75SDave Chinner * already marked stale. If we can't lock it, back off 22925b3eed75SDave Chinner * and retry. 22935b3eed75SDave Chinner */ 22945b257b4aSDave Chinner if (ip != free_ip && 22955b257b4aSDave Chinner !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { 22961a3e8f3dSDave Chinner rcu_read_unlock(); 22975b3eed75SDave Chinner delay(1); 22985b3eed75SDave Chinner goto retry; 22995b257b4aSDave Chinner } 23001a3e8f3dSDave Chinner rcu_read_unlock(); 23015b257b4aSDave Chinner 23025b3eed75SDave Chinner xfs_iflock(ip); 23035b257b4aSDave Chinner xfs_iflags_set(ip, XFS_ISTALE); 23045b257b4aSDave Chinner 23055b3eed75SDave Chinner /* 23065b3eed75SDave Chinner * we don't need to attach clean inodes or those only 23075b3eed75SDave Chinner * with unlogged changes (which we throw away, anyway). 23085b3eed75SDave Chinner */ 23095b257b4aSDave Chinner iip = ip->i_itemp; 23105b3eed75SDave Chinner if (!iip || xfs_inode_clean(ip)) { 23115b257b4aSDave Chinner ASSERT(ip != free_ip); 23121da177e4SLinus Torvalds xfs_ifunlock(ip); 23131da177e4SLinus Torvalds xfs_iunlock(ip, XFS_ILOCK_EXCL); 23141da177e4SLinus Torvalds continue; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 2317f5d8d5c4SChristoph Hellwig iip->ili_last_fields = iip->ili_fields; 2318f5d8d5c4SChristoph Hellwig iip->ili_fields = 0; 23191da177e4SLinus Torvalds iip->ili_logged = 1; 23207b2e2a31SDavid Chinner xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, 23217b2e2a31SDavid Chinner &iip->ili_item.li_lsn); 23221da177e4SLinus Torvalds 2323ca30b2a7SChristoph Hellwig xfs_buf_attach_iodone(bp, xfs_istale_done, 2324ca30b2a7SChristoph Hellwig &iip->ili_item); 23255b257b4aSDave Chinner 23265b257b4aSDave Chinner if (ip != free_ip) 23271da177e4SLinus Torvalds xfs_iunlock(ip, XFS_ILOCK_EXCL); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds xfs_trans_stale_inode_buf(tp, bp); 23311da177e4SLinus Torvalds xfs_trans_binval(tp, bp); 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23345017e97dSDave Chinner xfs_perag_put(pag); 23352a30f36dSChandra Seetharaman return 0; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds /* 23391da177e4SLinus Torvalds * This is called to return an inode to the inode free list. 23401da177e4SLinus Torvalds * The inode should already be truncated to 0 length and have 23411da177e4SLinus Torvalds * no pages associated with it. This routine also assumes that 23421da177e4SLinus Torvalds * the inode is already a part of the transaction. 23431da177e4SLinus Torvalds * 23441da177e4SLinus Torvalds * The on-disk copy of the inode will have been added to the list 23451da177e4SLinus Torvalds * of unlinked inodes in the AGI. We need to remove the inode from 23461da177e4SLinus Torvalds * that list atomically with respect to freeing it here. 23471da177e4SLinus Torvalds */ 23481da177e4SLinus Torvalds int 23491da177e4SLinus Torvalds xfs_ifree( 23501da177e4SLinus Torvalds xfs_trans_t *tp, 23511da177e4SLinus Torvalds xfs_inode_t *ip, 23521da177e4SLinus Torvalds xfs_bmap_free_t *flist) 23531da177e4SLinus Torvalds { 23541da177e4SLinus Torvalds int error; 23551da177e4SLinus Torvalds int delete; 23561da177e4SLinus Torvalds xfs_ino_t first_ino; 23571da177e4SLinus Torvalds 2358579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 23591da177e4SLinus Torvalds ASSERT(ip->i_d.di_nlink == 0); 23601da177e4SLinus Torvalds ASSERT(ip->i_d.di_nextents == 0); 23611da177e4SLinus Torvalds ASSERT(ip->i_d.di_anextents == 0); 2362ce7ae151SChristoph Hellwig ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode)); 23631da177e4SLinus Torvalds ASSERT(ip->i_d.di_nblocks == 0); 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds /* 23661da177e4SLinus Torvalds * Pull the on-disk inode from the AGI unlinked list. 23671da177e4SLinus Torvalds */ 23681da177e4SLinus Torvalds error = xfs_iunlink_remove(tp, ip); 23691baaed8fSDave Chinner if (error) 23701da177e4SLinus Torvalds return error; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); 23731baaed8fSDave Chinner if (error) 23741da177e4SLinus Torvalds return error; 23751baaed8fSDave Chinner 23761da177e4SLinus Torvalds ip->i_d.di_mode = 0; /* mark incore inode as free */ 23771da177e4SLinus Torvalds ip->i_d.di_flags = 0; 23781da177e4SLinus Torvalds ip->i_d.di_dmevmask = 0; 23791da177e4SLinus Torvalds ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ 23801da177e4SLinus Torvalds ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; 23811da177e4SLinus Torvalds ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; 23821da177e4SLinus Torvalds /* 23831da177e4SLinus Torvalds * Bump the generation count so no one will be confused 23841da177e4SLinus Torvalds * by reincarnations of this inode. 23851da177e4SLinus Torvalds */ 23861da177e4SLinus Torvalds ip->i_d.di_gen++; 23871da177e4SLinus Torvalds xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 23881da177e4SLinus Torvalds 23891baaed8fSDave Chinner if (delete) 23902a30f36dSChandra Seetharaman error = xfs_ifree_cluster(ip, tp, first_ino); 23911da177e4SLinus Torvalds 23922a30f36dSChandra Seetharaman return error; 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds /* 239660ec6783SChristoph Hellwig * This is called to unpin an inode. The caller must have the inode locked 239760ec6783SChristoph Hellwig * in at least shared mode so that the buffer cannot be subsequently pinned 239860ec6783SChristoph Hellwig * once someone is waiting for it to be unpinned. 23991da177e4SLinus Torvalds */ 240060ec6783SChristoph Hellwig static void 2401f392e631SChristoph Hellwig xfs_iunpin( 240260ec6783SChristoph Hellwig struct xfs_inode *ip) 2403a3f74ffbSDavid Chinner { 2404579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 2405a3f74ffbSDavid Chinner 24064aaf15d1SDave Chinner trace_xfs_inode_unpin_nowait(ip, _RET_IP_); 24074aaf15d1SDave Chinner 2408a3f74ffbSDavid Chinner /* Give the log a push to start the unpinning I/O */ 240960ec6783SChristoph Hellwig xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0); 2410a14a348bSChristoph Hellwig 2411a3f74ffbSDavid Chinner } 2412a3f74ffbSDavid Chinner 2413f392e631SChristoph Hellwig static void 2414f392e631SChristoph Hellwig __xfs_iunpin_wait( 2415f392e631SChristoph Hellwig struct xfs_inode *ip) 2416f392e631SChristoph Hellwig { 2417f392e631SChristoph Hellwig wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); 2418f392e631SChristoph Hellwig DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); 2419f392e631SChristoph Hellwig 2420f392e631SChristoph Hellwig xfs_iunpin(ip); 2421f392e631SChristoph Hellwig 2422f392e631SChristoph Hellwig do { 2423f392e631SChristoph Hellwig prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 2424f392e631SChristoph Hellwig if (xfs_ipincount(ip)) 2425f392e631SChristoph Hellwig io_schedule(); 2426f392e631SChristoph Hellwig } while (xfs_ipincount(ip)); 2427f392e631SChristoph Hellwig finish_wait(wq, &wait.wait); 2428f392e631SChristoph Hellwig } 2429f392e631SChristoph Hellwig 2430777df5afSDave Chinner void 24311da177e4SLinus Torvalds xfs_iunpin_wait( 243260ec6783SChristoph Hellwig struct xfs_inode *ip) 24331da177e4SLinus Torvalds { 2434f392e631SChristoph Hellwig if (xfs_ipincount(ip)) 2435f392e631SChristoph Hellwig __xfs_iunpin_wait(ip); 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 243827320369SDave Chinner /* 243927320369SDave Chinner * Removing an inode from the namespace involves removing the directory entry 244027320369SDave Chinner * and dropping the link count on the inode. Removing the directory entry can 244127320369SDave Chinner * result in locking an AGF (directory blocks were freed) and removing a link 244227320369SDave Chinner * count can result in placing the inode on an unlinked list which results in 244327320369SDave Chinner * locking an AGI. 244427320369SDave Chinner * 244527320369SDave Chinner * The big problem here is that we have an ordering constraint on AGF and AGI 244627320369SDave Chinner * locking - inode allocation locks the AGI, then can allocate a new extent for 244727320369SDave Chinner * new inodes, locking the AGF after the AGI. Similarly, freeing the inode 244827320369SDave Chinner * removes the inode from the unlinked list, requiring that we lock the AGI 244927320369SDave Chinner * first, and then freeing the inode can result in an inode chunk being freed 245027320369SDave Chinner * and hence freeing disk space requiring that we lock an AGF. 245127320369SDave Chinner * 245227320369SDave Chinner * Hence the ordering that is imposed by other parts of the code is AGI before 245327320369SDave Chinner * AGF. This means we cannot remove the directory entry before we drop the inode 245427320369SDave Chinner * reference count and put it on the unlinked list as this results in a lock 245527320369SDave Chinner * order of AGF then AGI, and this can deadlock against inode allocation and 245627320369SDave Chinner * freeing. Therefore we must drop the link counts before we remove the 245727320369SDave Chinner * directory entry. 245827320369SDave Chinner * 245927320369SDave Chinner * This is still safe from a transactional point of view - it is not until we 246027320369SDave Chinner * get to xfs_bmap_finish() that we have the possibility of multiple 246127320369SDave Chinner * transactions in this operation. Hence as long as we remove the directory 246227320369SDave Chinner * entry and drop the link count in the first transaction of the remove 246327320369SDave Chinner * operation, there are no transactional constraints on the ordering here. 246427320369SDave Chinner */ 2465c24b5dfaSDave Chinner int 2466c24b5dfaSDave Chinner xfs_remove( 2467c24b5dfaSDave Chinner xfs_inode_t *dp, 2468c24b5dfaSDave Chinner struct xfs_name *name, 2469c24b5dfaSDave Chinner xfs_inode_t *ip) 2470c24b5dfaSDave Chinner { 2471c24b5dfaSDave Chinner xfs_mount_t *mp = dp->i_mount; 2472c24b5dfaSDave Chinner xfs_trans_t *tp = NULL; 2473c24b5dfaSDave Chinner int is_dir = S_ISDIR(ip->i_d.di_mode); 2474c24b5dfaSDave Chinner int error = 0; 2475c24b5dfaSDave Chinner xfs_bmap_free_t free_list; 2476c24b5dfaSDave Chinner xfs_fsblock_t first_block; 2477c24b5dfaSDave Chinner int cancel_flags; 2478c24b5dfaSDave Chinner int committed; 2479c24b5dfaSDave Chinner uint resblks; 2480c24b5dfaSDave Chinner 2481c24b5dfaSDave Chinner trace_xfs_remove(dp, name); 2482c24b5dfaSDave Chinner 2483c24b5dfaSDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 24842451337dSDave Chinner return -EIO; 2485c24b5dfaSDave Chinner 2486c24b5dfaSDave Chinner error = xfs_qm_dqattach(dp, 0); 2487c24b5dfaSDave Chinner if (error) 2488c24b5dfaSDave Chinner goto std_return; 2489c24b5dfaSDave Chinner 2490c24b5dfaSDave Chinner error = xfs_qm_dqattach(ip, 0); 2491c24b5dfaSDave Chinner if (error) 2492c24b5dfaSDave Chinner goto std_return; 2493c24b5dfaSDave Chinner 249432296f86SDave Chinner if (is_dir) 2495c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR); 249632296f86SDave Chinner else 2497c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE); 2498c24b5dfaSDave Chinner cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 2499c24b5dfaSDave Chinner 2500c24b5dfaSDave Chinner /* 2501c24b5dfaSDave Chinner * We try to get the real space reservation first, 2502c24b5dfaSDave Chinner * allowing for directory btree deletion(s) implying 2503c24b5dfaSDave Chinner * possible bmap insert(s). If we can't get the space 2504c24b5dfaSDave Chinner * reservation then we use 0 instead, and avoid the bmap 2505c24b5dfaSDave Chinner * btree insert(s) in the directory code by, if the bmap 2506c24b5dfaSDave Chinner * insert tries to happen, instead trimming the LAST 2507c24b5dfaSDave Chinner * block from the directory. 2508c24b5dfaSDave Chinner */ 2509c24b5dfaSDave Chinner resblks = XFS_REMOVE_SPACE_RES(mp); 25103d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0); 25112451337dSDave Chinner if (error == -ENOSPC) { 2512c24b5dfaSDave Chinner resblks = 0; 25133d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0); 2514c24b5dfaSDave Chinner } 2515c24b5dfaSDave Chinner if (error) { 25162451337dSDave Chinner ASSERT(error != -ENOSPC); 2517c24b5dfaSDave Chinner cancel_flags = 0; 2518c24b5dfaSDave Chinner goto out_trans_cancel; 2519c24b5dfaSDave Chinner } 2520c24b5dfaSDave Chinner 2521c24b5dfaSDave Chinner xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL); 2522c24b5dfaSDave Chinner 2523c24b5dfaSDave Chinner xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); 2524c24b5dfaSDave Chinner xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 2525c24b5dfaSDave Chinner 2526c24b5dfaSDave Chinner /* 2527c24b5dfaSDave Chinner * If we're removing a directory perform some additional validation. 2528c24b5dfaSDave Chinner */ 252927320369SDave Chinner cancel_flags |= XFS_TRANS_ABORT; 2530c24b5dfaSDave Chinner if (is_dir) { 2531c24b5dfaSDave Chinner ASSERT(ip->i_d.di_nlink >= 2); 2532c24b5dfaSDave Chinner if (ip->i_d.di_nlink != 2) { 25332451337dSDave Chinner error = -ENOTEMPTY; 2534c24b5dfaSDave Chinner goto out_trans_cancel; 2535c24b5dfaSDave Chinner } 2536c24b5dfaSDave Chinner if (!xfs_dir_isempty(ip)) { 25372451337dSDave Chinner error = -ENOTEMPTY; 2538c24b5dfaSDave Chinner goto out_trans_cancel; 2539c24b5dfaSDave Chinner } 2540c24b5dfaSDave Chinner 254127320369SDave Chinner /* Drop the link from ip's "..". */ 2542c24b5dfaSDave Chinner error = xfs_droplink(tp, dp); 2543c24b5dfaSDave Chinner if (error) 254427320369SDave Chinner goto out_trans_cancel; 2545c24b5dfaSDave Chinner 254627320369SDave Chinner /* Drop the "." link from ip to self. */ 2547c24b5dfaSDave Chinner error = xfs_droplink(tp, ip); 2548c24b5dfaSDave Chinner if (error) 254927320369SDave Chinner goto out_trans_cancel; 2550c24b5dfaSDave Chinner } else { 2551c24b5dfaSDave Chinner /* 2552c24b5dfaSDave Chinner * When removing a non-directory we need to log the parent 2553c24b5dfaSDave Chinner * inode here. For a directory this is done implicitly 2554c24b5dfaSDave Chinner * by the xfs_droplink call for the ".." entry. 2555c24b5dfaSDave Chinner */ 2556c24b5dfaSDave Chinner xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 2557c24b5dfaSDave Chinner } 255827320369SDave Chinner xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2559c24b5dfaSDave Chinner 256027320369SDave Chinner /* Drop the link from dp to ip. */ 2561c24b5dfaSDave Chinner error = xfs_droplink(tp, ip); 2562c24b5dfaSDave Chinner if (error) 256327320369SDave Chinner goto out_trans_cancel; 2564c24b5dfaSDave Chinner 256527320369SDave Chinner xfs_bmap_init(&free_list, &first_block); 256627320369SDave Chinner error = xfs_dir_removename(tp, dp, name, ip->i_ino, 256727320369SDave Chinner &first_block, &free_list, resblks); 256827320369SDave Chinner if (error) { 25692451337dSDave Chinner ASSERT(error != -ENOENT); 257027320369SDave Chinner goto out_bmap_cancel; 257127320369SDave Chinner } 257227320369SDave Chinner 2573c24b5dfaSDave Chinner /* 2574c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the 2575c24b5dfaSDave Chinner * remove transaction goes to disk before returning to 2576c24b5dfaSDave Chinner * the user. 2577c24b5dfaSDave Chinner */ 2578c24b5dfaSDave Chinner if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) 2579c24b5dfaSDave Chinner xfs_trans_set_sync(tp); 2580c24b5dfaSDave Chinner 2581c24b5dfaSDave Chinner error = xfs_bmap_finish(&tp, &free_list, &committed); 2582c24b5dfaSDave Chinner if (error) 2583c24b5dfaSDave Chinner goto out_bmap_cancel; 2584c24b5dfaSDave Chinner 2585c24b5dfaSDave Chinner error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 2586c24b5dfaSDave Chinner if (error) 2587c24b5dfaSDave Chinner goto std_return; 2588c24b5dfaSDave Chinner 25892cd2ef6aSChristoph Hellwig if (is_dir && xfs_inode_is_filestream(ip)) 2590c24b5dfaSDave Chinner xfs_filestream_deassociate(ip); 2591c24b5dfaSDave Chinner 2592c24b5dfaSDave Chinner return 0; 2593c24b5dfaSDave Chinner 2594c24b5dfaSDave Chinner out_bmap_cancel: 2595c24b5dfaSDave Chinner xfs_bmap_cancel(&free_list); 2596c24b5dfaSDave Chinner out_trans_cancel: 2597c24b5dfaSDave Chinner xfs_trans_cancel(tp, cancel_flags); 2598c24b5dfaSDave Chinner std_return: 2599c24b5dfaSDave Chinner return error; 2600c24b5dfaSDave Chinner } 2601c24b5dfaSDave Chinner 2602f6bba201SDave Chinner /* 2603f6bba201SDave Chinner * Enter all inodes for a rename transaction into a sorted array. 2604f6bba201SDave Chinner */ 260595afcf5cSDave Chinner #define __XFS_SORT_INODES 5 2606f6bba201SDave Chinner STATIC void 2607f6bba201SDave Chinner xfs_sort_for_rename( 260895afcf5cSDave Chinner struct xfs_inode *dp1, /* in: old (source) directory inode */ 260995afcf5cSDave Chinner struct xfs_inode *dp2, /* in: new (target) directory inode */ 261095afcf5cSDave Chinner struct xfs_inode *ip1, /* in: inode of old entry */ 261195afcf5cSDave Chinner struct xfs_inode *ip2, /* in: inode of new entry */ 261295afcf5cSDave Chinner struct xfs_inode *wip, /* in: whiteout inode */ 261395afcf5cSDave Chinner struct xfs_inode **i_tab,/* out: sorted array of inodes */ 261495afcf5cSDave Chinner int *num_inodes) /* in/out: inodes in array */ 2615f6bba201SDave Chinner { 2616f6bba201SDave Chinner int i, j; 2617f6bba201SDave Chinner 261895afcf5cSDave Chinner ASSERT(*num_inodes == __XFS_SORT_INODES); 261995afcf5cSDave Chinner memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *)); 262095afcf5cSDave Chinner 2621f6bba201SDave Chinner /* 2622f6bba201SDave Chinner * i_tab contains a list of pointers to inodes. We initialize 2623f6bba201SDave Chinner * the table here & we'll sort it. We will then use it to 2624f6bba201SDave Chinner * order the acquisition of the inode locks. 2625f6bba201SDave Chinner * 2626f6bba201SDave Chinner * Note that the table may contain duplicates. e.g., dp1 == dp2. 2627f6bba201SDave Chinner */ 262895afcf5cSDave Chinner i = 0; 262995afcf5cSDave Chinner i_tab[i++] = dp1; 263095afcf5cSDave Chinner i_tab[i++] = dp2; 263195afcf5cSDave Chinner i_tab[i++] = ip1; 263295afcf5cSDave Chinner if (ip2) 263395afcf5cSDave Chinner i_tab[i++] = ip2; 263495afcf5cSDave Chinner if (wip) 263595afcf5cSDave Chinner i_tab[i++] = wip; 263695afcf5cSDave Chinner *num_inodes = i; 2637f6bba201SDave Chinner 2638f6bba201SDave Chinner /* 2639f6bba201SDave Chinner * Sort the elements via bubble sort. (Remember, there are at 264095afcf5cSDave Chinner * most 5 elements to sort, so this is adequate.) 2641f6bba201SDave Chinner */ 2642f6bba201SDave Chinner for (i = 0; i < *num_inodes; i++) { 2643f6bba201SDave Chinner for (j = 1; j < *num_inodes; j++) { 2644f6bba201SDave Chinner if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { 264595afcf5cSDave Chinner struct xfs_inode *temp = i_tab[j]; 2646f6bba201SDave Chinner i_tab[j] = i_tab[j-1]; 2647f6bba201SDave Chinner i_tab[j-1] = temp; 2648f6bba201SDave Chinner } 2649f6bba201SDave Chinner } 2650f6bba201SDave Chinner } 2651f6bba201SDave Chinner } 2652f6bba201SDave Chinner 2653310606b0SDave Chinner static int 2654310606b0SDave Chinner xfs_finish_rename( 2655310606b0SDave Chinner struct xfs_trans *tp, 2656310606b0SDave Chinner struct xfs_bmap_free *free_list) 2657310606b0SDave Chinner { 2658310606b0SDave Chinner int committed = 0; 2659310606b0SDave Chinner int error; 2660310606b0SDave Chinner 2661310606b0SDave Chinner /* 2662310606b0SDave Chinner * If this is a synchronous mount, make sure that the rename transaction 2663310606b0SDave Chinner * goes to disk before returning to the user. 2664310606b0SDave Chinner */ 2665310606b0SDave Chinner if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) 2666310606b0SDave Chinner xfs_trans_set_sync(tp); 2667310606b0SDave Chinner 2668310606b0SDave Chinner error = xfs_bmap_finish(&tp, free_list, &committed); 2669310606b0SDave Chinner if (error) { 2670310606b0SDave Chinner xfs_bmap_cancel(free_list); 2671310606b0SDave Chinner xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); 2672310606b0SDave Chinner return error; 2673310606b0SDave Chinner } 2674310606b0SDave Chinner 2675310606b0SDave Chinner return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 2676310606b0SDave Chinner } 2677310606b0SDave Chinner 2678f6bba201SDave Chinner /* 2679d31a1825SCarlos Maiolino * xfs_cross_rename() 2680d31a1825SCarlos Maiolino * 2681d31a1825SCarlos Maiolino * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall 2682d31a1825SCarlos Maiolino */ 2683d31a1825SCarlos Maiolino STATIC int 2684d31a1825SCarlos Maiolino xfs_cross_rename( 2685d31a1825SCarlos Maiolino struct xfs_trans *tp, 2686d31a1825SCarlos Maiolino struct xfs_inode *dp1, 2687d31a1825SCarlos Maiolino struct xfs_name *name1, 2688d31a1825SCarlos Maiolino struct xfs_inode *ip1, 2689d31a1825SCarlos Maiolino struct xfs_inode *dp2, 2690d31a1825SCarlos Maiolino struct xfs_name *name2, 2691d31a1825SCarlos Maiolino struct xfs_inode *ip2, 2692d31a1825SCarlos Maiolino struct xfs_bmap_free *free_list, 2693d31a1825SCarlos Maiolino xfs_fsblock_t *first_block, 2694d31a1825SCarlos Maiolino int spaceres) 2695d31a1825SCarlos Maiolino { 2696d31a1825SCarlos Maiolino int error = 0; 2697d31a1825SCarlos Maiolino int ip1_flags = 0; 2698d31a1825SCarlos Maiolino int ip2_flags = 0; 2699d31a1825SCarlos Maiolino int dp2_flags = 0; 2700d31a1825SCarlos Maiolino 2701d31a1825SCarlos Maiolino /* Swap inode number for dirent in first parent */ 2702d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, dp1, name1, 2703d31a1825SCarlos Maiolino ip2->i_ino, 2704d31a1825SCarlos Maiolino first_block, free_list, spaceres); 2705d31a1825SCarlos Maiolino if (error) 2706*eeacd321SDave Chinner goto out_trans_abort; 2707d31a1825SCarlos Maiolino 2708d31a1825SCarlos Maiolino /* Swap inode number for dirent in second parent */ 2709d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, dp2, name2, 2710d31a1825SCarlos Maiolino ip1->i_ino, 2711d31a1825SCarlos Maiolino first_block, free_list, spaceres); 2712d31a1825SCarlos Maiolino if (error) 2713*eeacd321SDave Chinner goto out_trans_abort; 2714d31a1825SCarlos Maiolino 2715d31a1825SCarlos Maiolino /* 2716d31a1825SCarlos Maiolino * If we're renaming one or more directories across different parents, 2717d31a1825SCarlos Maiolino * update the respective ".." entries (and link counts) to match the new 2718d31a1825SCarlos Maiolino * parents. 2719d31a1825SCarlos Maiolino */ 2720d31a1825SCarlos Maiolino if (dp1 != dp2) { 2721d31a1825SCarlos Maiolino dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2722d31a1825SCarlos Maiolino 2723d31a1825SCarlos Maiolino if (S_ISDIR(ip2->i_d.di_mode)) { 2724d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot, 2725d31a1825SCarlos Maiolino dp1->i_ino, first_block, 2726d31a1825SCarlos Maiolino free_list, spaceres); 2727d31a1825SCarlos Maiolino if (error) 2728*eeacd321SDave Chinner goto out_trans_abort; 2729d31a1825SCarlos Maiolino 2730d31a1825SCarlos Maiolino /* transfer ip2 ".." reference to dp1 */ 2731d31a1825SCarlos Maiolino if (!S_ISDIR(ip1->i_d.di_mode)) { 2732d31a1825SCarlos Maiolino error = xfs_droplink(tp, dp2); 2733d31a1825SCarlos Maiolino if (error) 2734*eeacd321SDave Chinner goto out_trans_abort; 2735d31a1825SCarlos Maiolino error = xfs_bumplink(tp, dp1); 2736d31a1825SCarlos Maiolino if (error) 2737*eeacd321SDave Chinner goto out_trans_abort; 2738d31a1825SCarlos Maiolino } 2739d31a1825SCarlos Maiolino 2740d31a1825SCarlos Maiolino /* 2741d31a1825SCarlos Maiolino * Although ip1 isn't changed here, userspace needs 2742d31a1825SCarlos Maiolino * to be warned about the change, so that applications 2743d31a1825SCarlos Maiolino * relying on it (like backup ones), will properly 2744d31a1825SCarlos Maiolino * notify the change 2745d31a1825SCarlos Maiolino */ 2746d31a1825SCarlos Maiolino ip1_flags |= XFS_ICHGTIME_CHG; 2747d31a1825SCarlos Maiolino ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2748d31a1825SCarlos Maiolino } 2749d31a1825SCarlos Maiolino 2750d31a1825SCarlos Maiolino if (S_ISDIR(ip1->i_d.di_mode)) { 2751d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot, 2752d31a1825SCarlos Maiolino dp2->i_ino, first_block, 2753d31a1825SCarlos Maiolino free_list, spaceres); 2754d31a1825SCarlos Maiolino if (error) 2755*eeacd321SDave Chinner goto out_trans_abort; 2756d31a1825SCarlos Maiolino 2757d31a1825SCarlos Maiolino /* transfer ip1 ".." reference to dp2 */ 2758d31a1825SCarlos Maiolino if (!S_ISDIR(ip2->i_d.di_mode)) { 2759d31a1825SCarlos Maiolino error = xfs_droplink(tp, dp1); 2760d31a1825SCarlos Maiolino if (error) 2761*eeacd321SDave Chinner goto out_trans_abort; 2762d31a1825SCarlos Maiolino error = xfs_bumplink(tp, dp2); 2763d31a1825SCarlos Maiolino if (error) 2764*eeacd321SDave Chinner goto out_trans_abort; 2765d31a1825SCarlos Maiolino } 2766d31a1825SCarlos Maiolino 2767d31a1825SCarlos Maiolino /* 2768d31a1825SCarlos Maiolino * Although ip2 isn't changed here, userspace needs 2769d31a1825SCarlos Maiolino * to be warned about the change, so that applications 2770d31a1825SCarlos Maiolino * relying on it (like backup ones), will properly 2771d31a1825SCarlos Maiolino * notify the change 2772d31a1825SCarlos Maiolino */ 2773d31a1825SCarlos Maiolino ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2774d31a1825SCarlos Maiolino ip2_flags |= XFS_ICHGTIME_CHG; 2775d31a1825SCarlos Maiolino } 2776d31a1825SCarlos Maiolino } 2777d31a1825SCarlos Maiolino 2778d31a1825SCarlos Maiolino if (ip1_flags) { 2779d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, ip1, ip1_flags); 2780d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE); 2781d31a1825SCarlos Maiolino } 2782d31a1825SCarlos Maiolino if (ip2_flags) { 2783d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, ip2, ip2_flags); 2784d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE); 2785d31a1825SCarlos Maiolino } 2786d31a1825SCarlos Maiolino if (dp2_flags) { 2787d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, dp2, dp2_flags); 2788d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE); 2789d31a1825SCarlos Maiolino } 2790d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2791d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE); 2792*eeacd321SDave Chinner return xfs_finish_rename(tp, free_list); 2793*eeacd321SDave Chinner 2794*eeacd321SDave Chinner out_trans_abort: 2795*eeacd321SDave Chinner xfs_bmap_cancel(free_list); 2796*eeacd321SDave Chinner xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); 2797d31a1825SCarlos Maiolino return error; 2798d31a1825SCarlos Maiolino } 2799d31a1825SCarlos Maiolino 2800d31a1825SCarlos Maiolino /* 2801f6bba201SDave Chinner * xfs_rename 2802f6bba201SDave Chinner */ 2803f6bba201SDave Chinner int 2804f6bba201SDave Chinner xfs_rename( 2805f6bba201SDave Chinner xfs_inode_t *src_dp, 2806f6bba201SDave Chinner struct xfs_name *src_name, 2807f6bba201SDave Chinner xfs_inode_t *src_ip, 2808f6bba201SDave Chinner xfs_inode_t *target_dp, 2809f6bba201SDave Chinner struct xfs_name *target_name, 2810d31a1825SCarlos Maiolino xfs_inode_t *target_ip, 2811d31a1825SCarlos Maiolino unsigned int flags) 2812f6bba201SDave Chinner { 2813f6bba201SDave Chinner xfs_trans_t *tp = NULL; 2814f6bba201SDave Chinner xfs_mount_t *mp = src_dp->i_mount; 2815f6bba201SDave Chinner int new_parent; /* moving to a new dir */ 2816f6bba201SDave Chinner int src_is_directory; /* src_name is a directory */ 2817f6bba201SDave Chinner int error; 2818f6bba201SDave Chinner xfs_bmap_free_t free_list; 2819f6bba201SDave Chinner xfs_fsblock_t first_block; 2820445883e8SDave Chinner int cancel_flags = 0; 282195afcf5cSDave Chinner xfs_inode_t *inodes[__XFS_SORT_INODES]; 282295afcf5cSDave Chinner int num_inodes = __XFS_SORT_INODES; 2823f6bba201SDave Chinner int spaceres; 2824f6bba201SDave Chinner 2825f6bba201SDave Chinner trace_xfs_rename(src_dp, target_dp, src_name, target_name); 2826f6bba201SDave Chinner 2827*eeacd321SDave Chinner if ((flags & RENAME_EXCHANGE) && !target_ip) 2828*eeacd321SDave Chinner return -EINVAL; 2829*eeacd321SDave Chinner 2830f6bba201SDave Chinner new_parent = (src_dp != target_dp); 2831f6bba201SDave Chinner src_is_directory = S_ISDIR(src_ip->i_d.di_mode); 2832f6bba201SDave Chinner 283395afcf5cSDave Chinner xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, NULL, 2834f6bba201SDave Chinner inodes, &num_inodes); 2835f6bba201SDave Chinner 2836f6bba201SDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME); 2837f6bba201SDave Chinner spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); 28383d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0); 28392451337dSDave Chinner if (error == -ENOSPC) { 2840f6bba201SDave Chinner spaceres = 0; 28413d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0); 2842f6bba201SDave Chinner } 2843445883e8SDave Chinner if (error) 2844445883e8SDave Chinner goto out_trans_cancel; 2845445883e8SDave Chinner cancel_flags = XFS_TRANS_RELEASE_LOG_RES; 2846f6bba201SDave Chinner 2847f6bba201SDave Chinner /* 2848f6bba201SDave Chinner * Attach the dquots to the inodes 2849f6bba201SDave Chinner */ 2850f6bba201SDave Chinner error = xfs_qm_vop_rename_dqattach(inodes); 2851445883e8SDave Chinner if (error) 2852445883e8SDave Chinner goto out_trans_cancel; 2853f6bba201SDave Chinner 2854f6bba201SDave Chinner /* 2855f6bba201SDave Chinner * Lock all the participating inodes. Depending upon whether 2856f6bba201SDave Chinner * the target_name exists in the target directory, and 2857f6bba201SDave Chinner * whether the target directory is the same as the source 2858f6bba201SDave Chinner * directory, we can lock from 2 to 4 inodes. 2859f6bba201SDave Chinner */ 2860f6bba201SDave Chinner xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); 2861f6bba201SDave Chinner 2862f6bba201SDave Chinner /* 2863f6bba201SDave Chinner * Join all the inodes to the transaction. From this point on, 2864f6bba201SDave Chinner * we can rely on either trans_commit or trans_cancel to unlock 2865f6bba201SDave Chinner * them. 2866f6bba201SDave Chinner */ 2867f6bba201SDave Chinner xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); 2868f6bba201SDave Chinner if (new_parent) 2869f6bba201SDave Chinner xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); 2870f6bba201SDave Chinner xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); 2871f6bba201SDave Chinner if (target_ip) 2872f6bba201SDave Chinner xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); 2873f6bba201SDave Chinner 2874f6bba201SDave Chinner /* 2875f6bba201SDave Chinner * If we are using project inheritance, we only allow renames 2876f6bba201SDave Chinner * into our tree when the project IDs are the same; else the 2877f6bba201SDave Chinner * tree quota mechanism would be circumvented. 2878f6bba201SDave Chinner */ 2879f6bba201SDave Chinner if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && 2880f6bba201SDave Chinner (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) { 28812451337dSDave Chinner error = -EXDEV; 2882445883e8SDave Chinner goto out_trans_cancel; 2883f6bba201SDave Chinner } 2884f6bba201SDave Chinner 2885445883e8SDave Chinner xfs_bmap_init(&free_list, &first_block); 2886445883e8SDave Chinner 2887*eeacd321SDave Chinner /* RENAME_EXCHANGE is unique from here on. */ 2888*eeacd321SDave Chinner if (flags & RENAME_EXCHANGE) 2889*eeacd321SDave Chinner return xfs_cross_rename(tp, src_dp, src_name, src_ip, 2890d31a1825SCarlos Maiolino target_dp, target_name, target_ip, 2891d31a1825SCarlos Maiolino &free_list, &first_block, spaceres); 2892d31a1825SCarlos Maiolino 2893d31a1825SCarlos Maiolino /* 2894f6bba201SDave Chinner * Set up the target. 2895f6bba201SDave Chinner */ 2896f6bba201SDave Chinner if (target_ip == NULL) { 2897f6bba201SDave Chinner /* 2898f6bba201SDave Chinner * If there's no space reservation, check the entry will 2899f6bba201SDave Chinner * fit before actually inserting it. 2900f6bba201SDave Chinner */ 290194f3cad5SEric Sandeen if (!spaceres) { 290294f3cad5SEric Sandeen error = xfs_dir_canenter(tp, target_dp, target_name); 2903f6bba201SDave Chinner if (error) 2904445883e8SDave Chinner goto out_trans_cancel; 290594f3cad5SEric Sandeen } 2906f6bba201SDave Chinner /* 2907f6bba201SDave Chinner * If target does not exist and the rename crosses 2908f6bba201SDave Chinner * directories, adjust the target directory link count 2909f6bba201SDave Chinner * to account for the ".." reference from the new entry. 2910f6bba201SDave Chinner */ 2911f6bba201SDave Chinner error = xfs_dir_createname(tp, target_dp, target_name, 2912f6bba201SDave Chinner src_ip->i_ino, &first_block, 2913f6bba201SDave Chinner &free_list, spaceres); 29142451337dSDave Chinner if (error == -ENOSPC) 2915445883e8SDave Chinner goto out_bmap_cancel; 2916f6bba201SDave Chinner if (error) 2917445883e8SDave Chinner goto out_trans_abort; 2918f6bba201SDave Chinner 2919f6bba201SDave Chinner xfs_trans_ichgtime(tp, target_dp, 2920f6bba201SDave Chinner XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2921f6bba201SDave Chinner 2922f6bba201SDave Chinner if (new_parent && src_is_directory) { 2923f6bba201SDave Chinner error = xfs_bumplink(tp, target_dp); 2924f6bba201SDave Chinner if (error) 2925445883e8SDave Chinner goto out_trans_abort; 2926f6bba201SDave Chinner } 2927f6bba201SDave Chinner } else { /* target_ip != NULL */ 2928f6bba201SDave Chinner /* 2929f6bba201SDave Chinner * If target exists and it's a directory, check that both 2930f6bba201SDave Chinner * target and source are directories and that target can be 2931f6bba201SDave Chinner * destroyed, or that neither is a directory. 2932f6bba201SDave Chinner */ 2933f6bba201SDave Chinner if (S_ISDIR(target_ip->i_d.di_mode)) { 2934f6bba201SDave Chinner /* 2935f6bba201SDave Chinner * Make sure target dir is empty. 2936f6bba201SDave Chinner */ 2937f6bba201SDave Chinner if (!(xfs_dir_isempty(target_ip)) || 2938f6bba201SDave Chinner (target_ip->i_d.di_nlink > 2)) { 29392451337dSDave Chinner error = -EEXIST; 2940445883e8SDave Chinner goto out_trans_cancel; 2941f6bba201SDave Chinner } 2942f6bba201SDave Chinner } 2943f6bba201SDave Chinner 2944f6bba201SDave Chinner /* 2945f6bba201SDave Chinner * Link the source inode under the target name. 2946f6bba201SDave Chinner * If the source inode is a directory and we are moving 2947f6bba201SDave Chinner * it across directories, its ".." entry will be 2948f6bba201SDave Chinner * inconsistent until we replace that down below. 2949f6bba201SDave Chinner * 2950f6bba201SDave Chinner * In case there is already an entry with the same 2951f6bba201SDave Chinner * name at the destination directory, remove it first. 2952f6bba201SDave Chinner */ 2953f6bba201SDave Chinner error = xfs_dir_replace(tp, target_dp, target_name, 2954f6bba201SDave Chinner src_ip->i_ino, 2955f6bba201SDave Chinner &first_block, &free_list, spaceres); 2956f6bba201SDave Chinner if (error) 2957445883e8SDave Chinner goto out_trans_abort; 2958f6bba201SDave Chinner 2959f6bba201SDave Chinner xfs_trans_ichgtime(tp, target_dp, 2960f6bba201SDave Chinner XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2961f6bba201SDave Chinner 2962f6bba201SDave Chinner /* 2963f6bba201SDave Chinner * Decrement the link count on the target since the target 2964f6bba201SDave Chinner * dir no longer points to it. 2965f6bba201SDave Chinner */ 2966f6bba201SDave Chinner error = xfs_droplink(tp, target_ip); 2967f6bba201SDave Chinner if (error) 2968445883e8SDave Chinner goto out_trans_abort; 2969f6bba201SDave Chinner 2970f6bba201SDave Chinner if (src_is_directory) { 2971f6bba201SDave Chinner /* 2972f6bba201SDave Chinner * Drop the link from the old "." entry. 2973f6bba201SDave Chinner */ 2974f6bba201SDave Chinner error = xfs_droplink(tp, target_ip); 2975f6bba201SDave Chinner if (error) 2976445883e8SDave Chinner goto out_trans_abort; 2977f6bba201SDave Chinner } 2978f6bba201SDave Chinner } /* target_ip != NULL */ 2979f6bba201SDave Chinner 2980f6bba201SDave Chinner /* 2981f6bba201SDave Chinner * Remove the source. 2982f6bba201SDave Chinner */ 2983f6bba201SDave Chinner if (new_parent && src_is_directory) { 2984f6bba201SDave Chinner /* 2985f6bba201SDave Chinner * Rewrite the ".." entry to point to the new 2986f6bba201SDave Chinner * directory. 2987f6bba201SDave Chinner */ 2988f6bba201SDave Chinner error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, 2989f6bba201SDave Chinner target_dp->i_ino, 2990f6bba201SDave Chinner &first_block, &free_list, spaceres); 29912451337dSDave Chinner ASSERT(error != -EEXIST); 2992f6bba201SDave Chinner if (error) 2993445883e8SDave Chinner goto out_trans_abort; 2994f6bba201SDave Chinner } 2995f6bba201SDave Chinner 2996f6bba201SDave Chinner /* 2997f6bba201SDave Chinner * We always want to hit the ctime on the source inode. 2998f6bba201SDave Chinner * 2999f6bba201SDave Chinner * This isn't strictly required by the standards since the source 3000f6bba201SDave Chinner * inode isn't really being changed, but old unix file systems did 3001f6bba201SDave Chinner * it and some incremental backup programs won't work without it. 3002f6bba201SDave Chinner */ 3003f6bba201SDave Chinner xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); 3004f6bba201SDave Chinner xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); 3005f6bba201SDave Chinner 3006f6bba201SDave Chinner /* 3007f6bba201SDave Chinner * Adjust the link count on src_dp. This is necessary when 3008f6bba201SDave Chinner * renaming a directory, either within one parent when 3009f6bba201SDave Chinner * the target existed, or across two parent directories. 3010f6bba201SDave Chinner */ 3011f6bba201SDave Chinner if (src_is_directory && (new_parent || target_ip != NULL)) { 3012f6bba201SDave Chinner 3013f6bba201SDave Chinner /* 3014f6bba201SDave Chinner * Decrement link count on src_directory since the 3015f6bba201SDave Chinner * entry that's moved no longer points to it. 3016f6bba201SDave Chinner */ 3017f6bba201SDave Chinner error = xfs_droplink(tp, src_dp); 3018f6bba201SDave Chinner if (error) 3019445883e8SDave Chinner goto out_trans_abort; 3020f6bba201SDave Chinner } 3021f6bba201SDave Chinner 3022f6bba201SDave Chinner error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, 3023f6bba201SDave Chinner &first_block, &free_list, spaceres); 3024f6bba201SDave Chinner if (error) 3025445883e8SDave Chinner goto out_trans_abort; 3026f6bba201SDave Chinner 3027f6bba201SDave Chinner xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3028f6bba201SDave Chinner xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); 3029f6bba201SDave Chinner if (new_parent) 3030f6bba201SDave Chinner xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); 3031f6bba201SDave Chinner 3032310606b0SDave Chinner return xfs_finish_rename(tp, &free_list); 3033f6bba201SDave Chinner 3034445883e8SDave Chinner out_trans_abort: 3035f6bba201SDave Chinner cancel_flags |= XFS_TRANS_ABORT; 3036445883e8SDave Chinner out_bmap_cancel: 3037f6bba201SDave Chinner xfs_bmap_cancel(&free_list); 3038445883e8SDave Chinner out_trans_cancel: 3039f6bba201SDave Chinner xfs_trans_cancel(tp, cancel_flags); 3040f6bba201SDave Chinner return error; 3041f6bba201SDave Chinner } 3042f6bba201SDave Chinner 3043bad55843SDavid Chinner STATIC int 3044bad55843SDavid Chinner xfs_iflush_cluster( 3045bad55843SDavid Chinner xfs_inode_t *ip, 3046bad55843SDavid Chinner xfs_buf_t *bp) 3047bad55843SDavid Chinner { 3048bad55843SDavid Chinner xfs_mount_t *mp = ip->i_mount; 30495017e97dSDave Chinner struct xfs_perag *pag; 3050bad55843SDavid Chinner unsigned long first_index, mask; 3051c8f5f12eSDavid Chinner unsigned long inodes_per_cluster; 3052bad55843SDavid Chinner int ilist_size; 3053bad55843SDavid Chinner xfs_inode_t **ilist; 3054bad55843SDavid Chinner xfs_inode_t *iq; 3055bad55843SDavid Chinner int nr_found; 3056bad55843SDavid Chinner int clcount = 0; 3057bad55843SDavid Chinner int bufwasdelwri; 3058bad55843SDavid Chinner int i; 3059bad55843SDavid Chinner 30605017e97dSDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); 3061bad55843SDavid Chinner 30620f49efd8SJie Liu inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog; 3063c8f5f12eSDavid Chinner ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); 306449383b0eSDavid Chinner ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); 3065bad55843SDavid Chinner if (!ilist) 306644b56e0aSDave Chinner goto out_put; 3067bad55843SDavid Chinner 30680f49efd8SJie Liu mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1); 3069bad55843SDavid Chinner first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; 30701a3e8f3dSDave Chinner rcu_read_lock(); 3071bad55843SDavid Chinner /* really need a gang lookup range call here */ 3072bad55843SDavid Chinner nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, 3073c8f5f12eSDavid Chinner first_index, inodes_per_cluster); 3074bad55843SDavid Chinner if (nr_found == 0) 3075bad55843SDavid Chinner goto out_free; 3076bad55843SDavid Chinner 3077bad55843SDavid Chinner for (i = 0; i < nr_found; i++) { 3078bad55843SDavid Chinner iq = ilist[i]; 3079bad55843SDavid Chinner if (iq == ip) 3080bad55843SDavid Chinner continue; 30811a3e8f3dSDave Chinner 30821a3e8f3dSDave Chinner /* 30831a3e8f3dSDave Chinner * because this is an RCU protected lookup, we could find a 30841a3e8f3dSDave Chinner * recently freed or even reallocated inode during the lookup. 30851a3e8f3dSDave Chinner * We need to check under the i_flags_lock for a valid inode 30861a3e8f3dSDave Chinner * here. Skip it if it is not valid or the wrong inode. 30871a3e8f3dSDave Chinner */ 30881a3e8f3dSDave Chinner spin_lock(&ip->i_flags_lock); 30891a3e8f3dSDave Chinner if (!ip->i_ino || 30901a3e8f3dSDave Chinner (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { 30911a3e8f3dSDave Chinner spin_unlock(&ip->i_flags_lock); 30921a3e8f3dSDave Chinner continue; 30931a3e8f3dSDave Chinner } 30941a3e8f3dSDave Chinner spin_unlock(&ip->i_flags_lock); 30951a3e8f3dSDave Chinner 3096bad55843SDavid Chinner /* 3097bad55843SDavid Chinner * Do an un-protected check to see if the inode is dirty and 3098bad55843SDavid Chinner * is a candidate for flushing. These checks will be repeated 3099bad55843SDavid Chinner * later after the appropriate locks are acquired. 3100bad55843SDavid Chinner */ 310133540408SDavid Chinner if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) 3102bad55843SDavid Chinner continue; 3103bad55843SDavid Chinner 3104bad55843SDavid Chinner /* 3105bad55843SDavid Chinner * Try to get locks. If any are unavailable or it is pinned, 3106bad55843SDavid Chinner * then this inode cannot be flushed and is skipped. 3107bad55843SDavid Chinner */ 3108bad55843SDavid Chinner 3109bad55843SDavid Chinner if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) 3110bad55843SDavid Chinner continue; 3111bad55843SDavid Chinner if (!xfs_iflock_nowait(iq)) { 3112bad55843SDavid Chinner xfs_iunlock(iq, XFS_ILOCK_SHARED); 3113bad55843SDavid Chinner continue; 3114bad55843SDavid Chinner } 3115bad55843SDavid Chinner if (xfs_ipincount(iq)) { 3116bad55843SDavid Chinner xfs_ifunlock(iq); 3117bad55843SDavid Chinner xfs_iunlock(iq, XFS_ILOCK_SHARED); 3118bad55843SDavid Chinner continue; 3119bad55843SDavid Chinner } 3120bad55843SDavid Chinner 3121bad55843SDavid Chinner /* 3122bad55843SDavid Chinner * arriving here means that this inode can be flushed. First 3123bad55843SDavid Chinner * re-check that it's dirty before flushing. 3124bad55843SDavid Chinner */ 312533540408SDavid Chinner if (!xfs_inode_clean(iq)) { 3126bad55843SDavid Chinner int error; 3127bad55843SDavid Chinner error = xfs_iflush_int(iq, bp); 3128bad55843SDavid Chinner if (error) { 3129bad55843SDavid Chinner xfs_iunlock(iq, XFS_ILOCK_SHARED); 3130bad55843SDavid Chinner goto cluster_corrupt_out; 3131bad55843SDavid Chinner } 3132bad55843SDavid Chinner clcount++; 3133bad55843SDavid Chinner } else { 3134bad55843SDavid Chinner xfs_ifunlock(iq); 3135bad55843SDavid Chinner } 3136bad55843SDavid Chinner xfs_iunlock(iq, XFS_ILOCK_SHARED); 3137bad55843SDavid Chinner } 3138bad55843SDavid Chinner 3139bad55843SDavid Chinner if (clcount) { 3140bad55843SDavid Chinner XFS_STATS_INC(xs_icluster_flushcnt); 3141bad55843SDavid Chinner XFS_STATS_ADD(xs_icluster_flushinode, clcount); 3142bad55843SDavid Chinner } 3143bad55843SDavid Chinner 3144bad55843SDavid Chinner out_free: 31451a3e8f3dSDave Chinner rcu_read_unlock(); 3146f0e2d93cSDenys Vlasenko kmem_free(ilist); 314744b56e0aSDave Chinner out_put: 314844b56e0aSDave Chinner xfs_perag_put(pag); 3149bad55843SDavid Chinner return 0; 3150bad55843SDavid Chinner 3151bad55843SDavid Chinner 3152bad55843SDavid Chinner cluster_corrupt_out: 3153bad55843SDavid Chinner /* 3154bad55843SDavid Chinner * Corruption detected in the clustering loop. Invalidate the 3155bad55843SDavid Chinner * inode buffer and shut down the filesystem. 3156bad55843SDavid Chinner */ 31571a3e8f3dSDave Chinner rcu_read_unlock(); 3158bad55843SDavid Chinner /* 315943ff2122SChristoph Hellwig * Clean up the buffer. If it was delwri, just release it -- 3160bad55843SDavid Chinner * brelse can handle it with no problems. If not, shut down the 3161bad55843SDavid Chinner * filesystem before releasing the buffer. 3162bad55843SDavid Chinner */ 316343ff2122SChristoph Hellwig bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); 3164bad55843SDavid Chinner if (bufwasdelwri) 3165bad55843SDavid Chinner xfs_buf_relse(bp); 3166bad55843SDavid Chinner 3167bad55843SDavid Chinner xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 3168bad55843SDavid Chinner 3169bad55843SDavid Chinner if (!bufwasdelwri) { 3170bad55843SDavid Chinner /* 3171bad55843SDavid Chinner * Just like incore_relse: if we have b_iodone functions, 3172bad55843SDavid Chinner * mark the buffer as an error and call them. Otherwise 3173bad55843SDavid Chinner * mark it as stale and brelse. 3174bad55843SDavid Chinner */ 3175cb669ca5SChristoph Hellwig if (bp->b_iodone) { 3176bad55843SDavid Chinner XFS_BUF_UNDONE(bp); 3177c867cb61SChristoph Hellwig xfs_buf_stale(bp); 31782451337dSDave Chinner xfs_buf_ioerror(bp, -EIO); 3179e8aaba9aSDave Chinner xfs_buf_ioend(bp); 3180bad55843SDavid Chinner } else { 3181c867cb61SChristoph Hellwig xfs_buf_stale(bp); 3182bad55843SDavid Chinner xfs_buf_relse(bp); 3183bad55843SDavid Chinner } 3184bad55843SDavid Chinner } 3185bad55843SDavid Chinner 3186bad55843SDavid Chinner /* 3187bad55843SDavid Chinner * Unlocks the flush lock 3188bad55843SDavid Chinner */ 318904913fddSDave Chinner xfs_iflush_abort(iq, false); 3190f0e2d93cSDenys Vlasenko kmem_free(ilist); 319144b56e0aSDave Chinner xfs_perag_put(pag); 31922451337dSDave Chinner return -EFSCORRUPTED; 3193bad55843SDavid Chinner } 3194bad55843SDavid Chinner 31951da177e4SLinus Torvalds /* 31964c46819aSChristoph Hellwig * Flush dirty inode metadata into the backing buffer. 31974c46819aSChristoph Hellwig * 31984c46819aSChristoph Hellwig * The caller must have the inode lock and the inode flush lock held. The 31994c46819aSChristoph Hellwig * inode lock will still be held upon return to the caller, and the inode 32004c46819aSChristoph Hellwig * flush lock will be released after the inode has reached the disk. 32014c46819aSChristoph Hellwig * 32024c46819aSChristoph Hellwig * The caller must write out the buffer returned in *bpp and release it. 32031da177e4SLinus Torvalds */ 32041da177e4SLinus Torvalds int 32051da177e4SLinus Torvalds xfs_iflush( 32064c46819aSChristoph Hellwig struct xfs_inode *ip, 32074c46819aSChristoph Hellwig struct xfs_buf **bpp) 32081da177e4SLinus Torvalds { 32094c46819aSChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 32104c46819aSChristoph Hellwig struct xfs_buf *bp; 32114c46819aSChristoph Hellwig struct xfs_dinode *dip; 32121da177e4SLinus Torvalds int error; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds XFS_STATS_INC(xs_iflush_count); 32151da177e4SLinus Torvalds 3216579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 3217474fce06SChristoph Hellwig ASSERT(xfs_isiflocked(ip)); 32181da177e4SLinus Torvalds ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || 32198096b1ebSChristoph Hellwig ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); 32201da177e4SLinus Torvalds 32214c46819aSChristoph Hellwig *bpp = NULL; 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds xfs_iunpin_wait(ip); 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds /* 32264b6a4688SDave Chinner * For stale inodes we cannot rely on the backing buffer remaining 32274b6a4688SDave Chinner * stale in cache for the remaining life of the stale inode and so 3228475ee413SChristoph Hellwig * xfs_imap_to_bp() below may give us a buffer that no longer contains 32294b6a4688SDave Chinner * inodes below. We have to check this after ensuring the inode is 32304b6a4688SDave Chinner * unpinned so that it is safe to reclaim the stale inode after the 32314b6a4688SDave Chinner * flush call. 32324b6a4688SDave Chinner */ 32334b6a4688SDave Chinner if (xfs_iflags_test(ip, XFS_ISTALE)) { 32344b6a4688SDave Chinner xfs_ifunlock(ip); 32354b6a4688SDave Chinner return 0; 32364b6a4688SDave Chinner } 32374b6a4688SDave Chinner 32384b6a4688SDave Chinner /* 32391da177e4SLinus Torvalds * This may have been unpinned because the filesystem is shutting 32401da177e4SLinus Torvalds * down forcibly. If that's the case we must not write this inode 324132ce90a4SChristoph Hellwig * to disk, because the log record didn't make it to disk. 324232ce90a4SChristoph Hellwig * 324332ce90a4SChristoph Hellwig * We also have to remove the log item from the AIL in this case, 324432ce90a4SChristoph Hellwig * as we wait for an empty AIL as part of the unmount process. 32451da177e4SLinus Torvalds */ 32461da177e4SLinus Torvalds if (XFS_FORCED_SHUTDOWN(mp)) { 32472451337dSDave Chinner error = -EIO; 324832ce90a4SChristoph Hellwig goto abort_out; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds /* 3252a3f74ffbSDavid Chinner * Get the buffer containing the on-disk inode. 3253a3f74ffbSDavid Chinner */ 3254475ee413SChristoph Hellwig error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK, 3255475ee413SChristoph Hellwig 0); 3256a3f74ffbSDavid Chinner if (error || !bp) { 3257a3f74ffbSDavid Chinner xfs_ifunlock(ip); 3258a3f74ffbSDavid Chinner return error; 3259a3f74ffbSDavid Chinner } 3260a3f74ffbSDavid Chinner 3261a3f74ffbSDavid Chinner /* 32621da177e4SLinus Torvalds * First flush out the inode that xfs_iflush was called with. 32631da177e4SLinus Torvalds */ 32641da177e4SLinus Torvalds error = xfs_iflush_int(ip, bp); 3265bad55843SDavid Chinner if (error) 32661da177e4SLinus Torvalds goto corrupt_out; 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds /* 3269a3f74ffbSDavid Chinner * If the buffer is pinned then push on the log now so we won't 3270a3f74ffbSDavid Chinner * get stuck waiting in the write for too long. 3271a3f74ffbSDavid Chinner */ 3272811e64c7SChandra Seetharaman if (xfs_buf_ispinned(bp)) 3273a14a348bSChristoph Hellwig xfs_log_force(mp, 0); 3274a3f74ffbSDavid Chinner 3275a3f74ffbSDavid Chinner /* 32761da177e4SLinus Torvalds * inode clustering: 32771da177e4SLinus Torvalds * see if other inodes can be gathered into this write 32781da177e4SLinus Torvalds */ 3279bad55843SDavid Chinner error = xfs_iflush_cluster(ip, bp); 3280bad55843SDavid Chinner if (error) 32811da177e4SLinus Torvalds goto cluster_corrupt_out; 32821da177e4SLinus Torvalds 32834c46819aSChristoph Hellwig *bpp = bp; 32844c46819aSChristoph Hellwig return 0; 32851da177e4SLinus Torvalds 32861da177e4SLinus Torvalds corrupt_out: 32871da177e4SLinus Torvalds xfs_buf_relse(bp); 32887d04a335SNathan Scott xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 32891da177e4SLinus Torvalds cluster_corrupt_out: 32902451337dSDave Chinner error = -EFSCORRUPTED; 329132ce90a4SChristoph Hellwig abort_out: 32921da177e4SLinus Torvalds /* 32931da177e4SLinus Torvalds * Unlocks the flush lock 32941da177e4SLinus Torvalds */ 329504913fddSDave Chinner xfs_iflush_abort(ip, false); 329632ce90a4SChristoph Hellwig return error; 32971da177e4SLinus Torvalds } 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds STATIC int 33001da177e4SLinus Torvalds xfs_iflush_int( 330193848a99SChristoph Hellwig struct xfs_inode *ip, 330293848a99SChristoph Hellwig struct xfs_buf *bp) 33031da177e4SLinus Torvalds { 330493848a99SChristoph Hellwig struct xfs_inode_log_item *iip = ip->i_itemp; 330593848a99SChristoph Hellwig struct xfs_dinode *dip; 330693848a99SChristoph Hellwig struct xfs_mount *mp = ip->i_mount; 33071da177e4SLinus Torvalds 3308579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 3309474fce06SChristoph Hellwig ASSERT(xfs_isiflocked(ip)); 33101da177e4SLinus Torvalds ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || 33118096b1ebSChristoph Hellwig ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); 331293848a99SChristoph Hellwig ASSERT(iip != NULL && iip->ili_fields != 0); 3313263997a6SDave Chinner ASSERT(ip->i_d.di_version > 1); 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds /* set *dip = inode's place in the buffer */ 331692bfc6e7SChristoph Hellwig dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); 33171da177e4SLinus Torvalds 331869ef921bSChristoph Hellwig if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), 33191da177e4SLinus Torvalds mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { 33206a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33216a19d939SDave Chinner "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", 33226a19d939SDave Chinner __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); 33231da177e4SLinus Torvalds goto corrupt_out; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, 33261da177e4SLinus Torvalds mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { 33276a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33286a19d939SDave Chinner "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", 33296a19d939SDave Chinner __func__, ip->i_ino, ip, ip->i_d.di_magic); 33301da177e4SLinus Torvalds goto corrupt_out; 33311da177e4SLinus Torvalds } 3332abbede1bSAl Viro if (S_ISREG(ip->i_d.di_mode)) { 33331da177e4SLinus Torvalds if (XFS_TEST_ERROR( 33341da177e4SLinus Torvalds (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && 33351da177e4SLinus Torvalds (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), 33361da177e4SLinus Torvalds mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { 33376a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33386a19d939SDave Chinner "%s: Bad regular inode %Lu, ptr 0x%p", 33396a19d939SDave Chinner __func__, ip->i_ino, ip); 33401da177e4SLinus Torvalds goto corrupt_out; 33411da177e4SLinus Torvalds } 3342abbede1bSAl Viro } else if (S_ISDIR(ip->i_d.di_mode)) { 33431da177e4SLinus Torvalds if (XFS_TEST_ERROR( 33441da177e4SLinus Torvalds (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && 33451da177e4SLinus Torvalds (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && 33461da177e4SLinus Torvalds (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), 33471da177e4SLinus Torvalds mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { 33486a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33496a19d939SDave Chinner "%s: Bad directory inode %Lu, ptr 0x%p", 33506a19d939SDave Chinner __func__, ip->i_ino, ip); 33511da177e4SLinus Torvalds goto corrupt_out; 33521da177e4SLinus Torvalds } 33531da177e4SLinus Torvalds } 33541da177e4SLinus Torvalds if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > 33551da177e4SLinus Torvalds ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, 33561da177e4SLinus Torvalds XFS_RANDOM_IFLUSH_5)) { 33576a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33586a19d939SDave Chinner "%s: detected corrupt incore inode %Lu, " 33596a19d939SDave Chinner "total extents = %d, nblocks = %Ld, ptr 0x%p", 33606a19d939SDave Chinner __func__, ip->i_ino, 33611da177e4SLinus Torvalds ip->i_d.di_nextents + ip->i_d.di_anextents, 33626a19d939SDave Chinner ip->i_d.di_nblocks, ip); 33631da177e4SLinus Torvalds goto corrupt_out; 33641da177e4SLinus Torvalds } 33651da177e4SLinus Torvalds if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, 33661da177e4SLinus Torvalds mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { 33676a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 33686a19d939SDave Chinner "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", 33696a19d939SDave Chinner __func__, ip->i_ino, ip->i_d.di_forkoff, ip); 33701da177e4SLinus Torvalds goto corrupt_out; 33711da177e4SLinus Torvalds } 3372e60896d8SDave Chinner 33731da177e4SLinus Torvalds /* 3374263997a6SDave Chinner * Inode item log recovery for v2 inodes are dependent on the 3375e60896d8SDave Chinner * di_flushiter count for correct sequencing. We bump the flush 3376e60896d8SDave Chinner * iteration count so we can detect flushes which postdate a log record 3377e60896d8SDave Chinner * during recovery. This is redundant as we now log every change and 3378e60896d8SDave Chinner * hence this can't happen but we need to still do it to ensure 3379e60896d8SDave Chinner * backwards compatibility with old kernels that predate logging all 3380e60896d8SDave Chinner * inode changes. 33811da177e4SLinus Torvalds */ 3382e60896d8SDave Chinner if (ip->i_d.di_version < 3) 33831da177e4SLinus Torvalds ip->i_d.di_flushiter++; 33841da177e4SLinus Torvalds 33851da177e4SLinus Torvalds /* 33861da177e4SLinus Torvalds * Copy the dirty parts of the inode into the on-disk 33871da177e4SLinus Torvalds * inode. We always copy out the core of the inode, 33881da177e4SLinus Torvalds * because if the inode is dirty at all the core must 33891da177e4SLinus Torvalds * be. 33901da177e4SLinus Torvalds */ 339181591fe2SChristoph Hellwig xfs_dinode_to_disk(dip, &ip->i_d); 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds /* Wrap, we never let the log put out DI_MAX_FLUSH */ 33941da177e4SLinus Torvalds if (ip->i_d.di_flushiter == DI_MAX_FLUSH) 33951da177e4SLinus Torvalds ip->i_d.di_flushiter = 0; 33961da177e4SLinus Torvalds 3397fd9fdba6SEric Sandeen xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK); 3398e4ac967bSDavid Chinner if (XFS_IFORK_Q(ip)) 3399fd9fdba6SEric Sandeen xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK); 34001da177e4SLinus Torvalds xfs_inobp_check(mp, bp); 34011da177e4SLinus Torvalds 34021da177e4SLinus Torvalds /* 3403f5d8d5c4SChristoph Hellwig * We've recorded everything logged in the inode, so we'd like to clear 3404f5d8d5c4SChristoph Hellwig * the ili_fields bits so we don't log and flush things unnecessarily. 3405f5d8d5c4SChristoph Hellwig * However, we can't stop logging all this information until the data 3406f5d8d5c4SChristoph Hellwig * we've copied into the disk buffer is written to disk. If we did we 3407f5d8d5c4SChristoph Hellwig * might overwrite the copy of the inode in the log with all the data 3408f5d8d5c4SChristoph Hellwig * after re-logging only part of it, and in the face of a crash we 3409f5d8d5c4SChristoph Hellwig * wouldn't have all the data we need to recover. 34101da177e4SLinus Torvalds * 3411f5d8d5c4SChristoph Hellwig * What we do is move the bits to the ili_last_fields field. When 3412f5d8d5c4SChristoph Hellwig * logging the inode, these bits are moved back to the ili_fields field. 3413f5d8d5c4SChristoph Hellwig * In the xfs_iflush_done() routine we clear ili_last_fields, since we 3414f5d8d5c4SChristoph Hellwig * know that the information those bits represent is permanently on 3415f5d8d5c4SChristoph Hellwig * disk. As long as the flush completes before the inode is logged 3416f5d8d5c4SChristoph Hellwig * again, then both ili_fields and ili_last_fields will be cleared. 34171da177e4SLinus Torvalds * 3418f5d8d5c4SChristoph Hellwig * We can play with the ili_fields bits here, because the inode lock 3419f5d8d5c4SChristoph Hellwig * must be held exclusively in order to set bits there and the flush 3420f5d8d5c4SChristoph Hellwig * lock protects the ili_last_fields bits. Set ili_logged so the flush 3421f5d8d5c4SChristoph Hellwig * done routine can tell whether or not to look in the AIL. Also, store 3422f5d8d5c4SChristoph Hellwig * the current LSN of the inode so that we can tell whether the item has 3423f5d8d5c4SChristoph Hellwig * moved in the AIL from xfs_iflush_done(). In order to read the lsn we 3424f5d8d5c4SChristoph Hellwig * need the AIL lock, because it is a 64 bit value that cannot be read 3425f5d8d5c4SChristoph Hellwig * atomically. 34261da177e4SLinus Torvalds */ 3427f5d8d5c4SChristoph Hellwig iip->ili_last_fields = iip->ili_fields; 3428f5d8d5c4SChristoph Hellwig iip->ili_fields = 0; 34291da177e4SLinus Torvalds iip->ili_logged = 1; 34301da177e4SLinus Torvalds 34317b2e2a31SDavid Chinner xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, 34327b2e2a31SDavid Chinner &iip->ili_item.li_lsn); 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds /* 34351da177e4SLinus Torvalds * Attach the function xfs_iflush_done to the inode's 34361da177e4SLinus Torvalds * buffer. This will remove the inode from the AIL 34371da177e4SLinus Torvalds * and unlock the inode's flush lock when the inode is 34381da177e4SLinus Torvalds * completely written to disk. 34391da177e4SLinus Torvalds */ 3440ca30b2a7SChristoph Hellwig xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); 34411da177e4SLinus Torvalds 344293848a99SChristoph Hellwig /* update the lsn in the on disk inode if required */ 344393848a99SChristoph Hellwig if (ip->i_d.di_version == 3) 344493848a99SChristoph Hellwig dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn); 344593848a99SChristoph Hellwig 344693848a99SChristoph Hellwig /* generate the checksum. */ 344793848a99SChristoph Hellwig xfs_dinode_calc_crc(mp, dip); 344893848a99SChristoph Hellwig 3449adadbeefSChristoph Hellwig ASSERT(bp->b_fspriv != NULL); 3450cb669ca5SChristoph Hellwig ASSERT(bp->b_iodone != NULL); 34511da177e4SLinus Torvalds return 0; 34521da177e4SLinus Torvalds 34531da177e4SLinus Torvalds corrupt_out: 34542451337dSDave Chinner return -EFSCORRUPTED; 34551da177e4SLinus Torvalds } 3456