10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
33e57ecf6SOlaf Weber * Copyright (c) 2000-2006 Silicon Graphics, Inc.
47b718769SNathan Scott * All Rights Reserved.
51da177e4SLinus Torvalds */
6f0e28280SJeff Layton #include <linux/iversion.h>
740ebd81dSRobert P. J. Day
81da177e4SLinus Torvalds #include "xfs.h"
9a844f451SNathan Scott #include "xfs_fs.h"
1070a9883cSDave Chinner #include "xfs_shared.h"
11239880efSDave Chinner #include "xfs_format.h"
12239880efSDave Chinner #include "xfs_log_format.h"
13239880efSDave Chinner #include "xfs_trans_resv.h"
141da177e4SLinus Torvalds #include "xfs_mount.h"
153ab78df2SDarrick J. Wong #include "xfs_defer.h"
16a4fbe6abSDave Chinner #include "xfs_inode.h"
17c24b5dfaSDave Chinner #include "xfs_dir2.h"
18c24b5dfaSDave Chinner #include "xfs_attr.h"
19239880efSDave Chinner #include "xfs_trans_space.h"
20239880efSDave Chinner #include "xfs_trans.h"
211da177e4SLinus Torvalds #include "xfs_buf_item.h"
22a844f451SNathan Scott #include "xfs_inode_item.h"
23784eb7d8SDave Chinner #include "xfs_iunlink_item.h"
24a844f451SNathan Scott #include "xfs_ialloc.h"
25a844f451SNathan Scott #include "xfs_bmap.h"
2668988114SDave Chinner #include "xfs_bmap_util.h"
27e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
281da177e4SLinus Torvalds #include "xfs_error.h"
291da177e4SLinus Torvalds #include "xfs_quota.h"
302a82b8beSDavid Chinner #include "xfs_filestream.h"
310b1b213fSChristoph Hellwig #include "xfs_trace.h"
3233479e05SDave Chinner #include "xfs_icache.h"
33c24b5dfaSDave Chinner #include "xfs_symlink.h"
34239880efSDave Chinner #include "xfs_trans_priv.h"
35239880efSDave Chinner #include "xfs_log.h"
36a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
37aa8968f2SDarrick J. Wong #include "xfs_reflink.h"
389bbafc71SDave Chinner #include "xfs_ag.h"
3901728b44SDave Chinner #include "xfs_log_priv.h"
401da177e4SLinus Torvalds
41182696fbSDarrick J. Wong struct kmem_cache *xfs_inode_cache;
421da177e4SLinus Torvalds
431da177e4SLinus Torvalds /*
448f04c47aSChristoph Hellwig * Used in xfs_itruncate_extents(). This is the maximum number of extents
451da177e4SLinus Torvalds * freed from a file in a single transaction.
461da177e4SLinus Torvalds */
471da177e4SLinus Torvalds #define XFS_ITRUNC_MAX_EXTENTS 2
481da177e4SLinus Torvalds
4954d7b5c1SDave Chinner STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
50f40aadb2SDave Chinner STATIC int xfs_iunlink_remove(struct xfs_trans *tp, struct xfs_perag *pag,
51f40aadb2SDave Chinner struct xfs_inode *);
52ab297431SZhi Yong Wu
532a0ec1d9SDave Chinner /*
542a0ec1d9SDave Chinner * helper function to extract extent size hint from inode
552a0ec1d9SDave Chinner */
562a0ec1d9SDave Chinner xfs_extlen_t
xfs_get_extsz_hint(struct xfs_inode * ip)572a0ec1d9SDave Chinner xfs_get_extsz_hint(
582a0ec1d9SDave Chinner struct xfs_inode *ip)
592a0ec1d9SDave Chinner {
60bdb2ed2dSChristoph Hellwig /*
61bdb2ed2dSChristoph Hellwig * No point in aligning allocations if we need to COW to actually
62bdb2ed2dSChristoph Hellwig * write to them.
63bdb2ed2dSChristoph Hellwig */
64bdb2ed2dSChristoph Hellwig if (xfs_is_always_cow_inode(ip))
65bdb2ed2dSChristoph Hellwig return 0;
66db07349dSChristoph Hellwig if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
67031474c2SChristoph Hellwig return ip->i_extsize;
682a0ec1d9SDave Chinner if (XFS_IS_REALTIME_INODE(ip))
692a0ec1d9SDave Chinner return ip->i_mount->m_sb.sb_rextsize;
702a0ec1d9SDave Chinner return 0;
712a0ec1d9SDave Chinner }
722a0ec1d9SDave Chinner
73fa96acadSDave Chinner /*
74f7ca3522SDarrick J. Wong * Helper function to extract CoW extent size hint from inode.
75f7ca3522SDarrick J. Wong * Between the extent size hint and the CoW extent size hint, we
76e153aa79SDarrick J. Wong * return the greater of the two. If the value is zero (automatic),
77e153aa79SDarrick J. Wong * use the default size.
78f7ca3522SDarrick J. Wong */
79f7ca3522SDarrick J. Wong xfs_extlen_t
xfs_get_cowextsz_hint(struct xfs_inode * ip)80f7ca3522SDarrick J. Wong xfs_get_cowextsz_hint(
81f7ca3522SDarrick J. Wong struct xfs_inode *ip)
82f7ca3522SDarrick J. Wong {
83f7ca3522SDarrick J. Wong xfs_extlen_t a, b;
84f7ca3522SDarrick J. Wong
85f7ca3522SDarrick J. Wong a = 0;
863e09ab8fSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
87b33ce57dSChristoph Hellwig a = ip->i_cowextsize;
88f7ca3522SDarrick J. Wong b = xfs_get_extsz_hint(ip);
89f7ca3522SDarrick J. Wong
90e153aa79SDarrick J. Wong a = max(a, b);
91e153aa79SDarrick J. Wong if (a == 0)
92e153aa79SDarrick J. Wong return XFS_DEFAULT_COWEXTSZ_HINT;
93f7ca3522SDarrick J. Wong return a;
94f7ca3522SDarrick J. Wong }
95f7ca3522SDarrick J. Wong
96f7ca3522SDarrick J. Wong /*
97efa70be1SChristoph Hellwig * These two are wrapper routines around the xfs_ilock() routine used to
98efa70be1SChristoph Hellwig * centralize some grungy code. They are used in places that wish to lock the
99efa70be1SChristoph Hellwig * inode solely for reading the extents. The reason these places can't just
100efa70be1SChristoph Hellwig * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
101efa70be1SChristoph Hellwig * bringing in of the extents from disk for a file in b-tree format. If the
102efa70be1SChristoph Hellwig * inode is in b-tree format, then we need to lock the inode exclusively until
103efa70be1SChristoph Hellwig * the extents are read in. Locking it exclusively all the time would limit
104efa70be1SChristoph Hellwig * our parallelism unnecessarily, though. What we do instead is check to see
105efa70be1SChristoph Hellwig * if the extents have been read in yet, and only lock the inode exclusively
106efa70be1SChristoph Hellwig * if they have not.
107fa96acadSDave Chinner *
108efa70be1SChristoph Hellwig * The functions return a value which should be given to the corresponding
10901f4f327SChristoph Hellwig * xfs_iunlock() call.
110fa96acadSDave Chinner */
111fa96acadSDave Chinner uint
xfs_ilock_data_map_shared(struct xfs_inode * ip)112309ecac8SChristoph Hellwig xfs_ilock_data_map_shared(
113309ecac8SChristoph Hellwig struct xfs_inode *ip)
114fa96acadSDave Chinner {
115309ecac8SChristoph Hellwig uint lock_mode = XFS_ILOCK_SHARED;
116fa96acadSDave Chinner
117b2197a36SChristoph Hellwig if (xfs_need_iread_extents(&ip->i_df))
118fa96acadSDave Chinner lock_mode = XFS_ILOCK_EXCL;
119fa96acadSDave Chinner xfs_ilock(ip, lock_mode);
120fa96acadSDave Chinner return lock_mode;
121fa96acadSDave Chinner }
122fa96acadSDave Chinner
123efa70be1SChristoph Hellwig uint
xfs_ilock_attr_map_shared(struct xfs_inode * ip)124efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared(
125efa70be1SChristoph Hellwig struct xfs_inode *ip)
126fa96acadSDave Chinner {
127efa70be1SChristoph Hellwig uint lock_mode = XFS_ILOCK_SHARED;
128efa70be1SChristoph Hellwig
129932b42c6SDarrick J. Wong if (xfs_inode_has_attr_fork(ip) && xfs_need_iread_extents(&ip->i_af))
130efa70be1SChristoph Hellwig lock_mode = XFS_ILOCK_EXCL;
131efa70be1SChristoph Hellwig xfs_ilock(ip, lock_mode);
132efa70be1SChristoph Hellwig return lock_mode;
133fa96acadSDave Chinner }
134fa96acadSDave Chinner
135fa96acadSDave Chinner /*
136ca76a761SKaixu Xia * You can't set both SHARED and EXCL for the same lock,
137ca76a761SKaixu Xia * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_MMAPLOCK_SHARED,
138ca76a761SKaixu Xia * XFS_MMAPLOCK_EXCL, XFS_ILOCK_SHARED, XFS_ILOCK_EXCL are valid values
139ca76a761SKaixu Xia * to set in lock_flags.
140ca76a761SKaixu Xia */
141ca76a761SKaixu Xia static inline void
xfs_lock_flags_assert(uint lock_flags)142ca76a761SKaixu Xia xfs_lock_flags_assert(
143ca76a761SKaixu Xia uint lock_flags)
144ca76a761SKaixu Xia {
145ca76a761SKaixu Xia ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
146ca76a761SKaixu Xia (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
147ca76a761SKaixu Xia ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
148ca76a761SKaixu Xia (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
149ca76a761SKaixu Xia ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
150ca76a761SKaixu Xia (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
151ca76a761SKaixu Xia ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
152ca76a761SKaixu Xia ASSERT(lock_flags != 0);
153ca76a761SKaixu Xia }
154ca76a761SKaixu Xia
155ca76a761SKaixu Xia /*
15665523218SChristoph Hellwig * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
1572433480aSJan Kara * multi-reader locks: invalidate_lock and the i_lock. This routine allows
15865523218SChristoph Hellwig * various combinations of the locks to be obtained.
159fa96acadSDave Chinner *
160653c60b6SDave Chinner * The 3 locks should always be ordered so that the IO lock is obtained first,
161653c60b6SDave Chinner * the mmap lock second and the ilock last in order to prevent deadlock.
162fa96acadSDave Chinner *
163653c60b6SDave Chinner * Basic locking order:
164653c60b6SDave Chinner *
1652433480aSJan Kara * i_rwsem -> invalidate_lock -> page_lock -> i_ilock
166653c60b6SDave Chinner *
167c1e8d7c6SMichel Lespinasse * mmap_lock locking order:
168653c60b6SDave Chinner *
169c1e8d7c6SMichel Lespinasse * i_rwsem -> page lock -> mmap_lock
1702433480aSJan Kara * mmap_lock -> invalidate_lock -> page_lock
171653c60b6SDave Chinner *
172c1e8d7c6SMichel Lespinasse * The difference in mmap_lock locking order mean that we cannot hold the
1732433480aSJan Kara * invalidate_lock over syscall based read(2)/write(2) based IO. These IO paths
1742433480aSJan Kara * can fault in pages during copy in/out (for buffered IO) or require the
1752433480aSJan Kara * mmap_lock in get_user_pages() to map the user pages into the kernel address
1762433480aSJan Kara * space for direct IO. Similarly the i_rwsem cannot be taken inside a page
1772433480aSJan Kara * fault because page faults already hold the mmap_lock.
178653c60b6SDave Chinner *
179653c60b6SDave Chinner * Hence to serialise fully against both syscall and mmap based IO, we need to
1802433480aSJan Kara * take both the i_rwsem and the invalidate_lock. These locks should *only* be
1812433480aSJan Kara * both taken in places where we need to invalidate the page cache in a race
182653c60b6SDave Chinner * free manner (e.g. truncate, hole punch and other extent manipulation
183653c60b6SDave Chinner * functions).
184fa96acadSDave Chinner */
185fa96acadSDave Chinner void
xfs_ilock(xfs_inode_t * ip,uint lock_flags)186fa96acadSDave Chinner xfs_ilock(
187fa96acadSDave Chinner xfs_inode_t *ip,
188fa96acadSDave Chinner uint lock_flags)
189fa96acadSDave Chinner {
190fa96acadSDave Chinner trace_xfs_ilock(ip, lock_flags, _RET_IP_);
191fa96acadSDave Chinner
192ca76a761SKaixu Xia xfs_lock_flags_assert(lock_flags);
193fa96acadSDave Chinner
19465523218SChristoph Hellwig if (lock_flags & XFS_IOLOCK_EXCL) {
19565523218SChristoph Hellwig down_write_nested(&VFS_I(ip)->i_rwsem,
19665523218SChristoph Hellwig XFS_IOLOCK_DEP(lock_flags));
19765523218SChristoph Hellwig } else if (lock_flags & XFS_IOLOCK_SHARED) {
19865523218SChristoph Hellwig down_read_nested(&VFS_I(ip)->i_rwsem,
19965523218SChristoph Hellwig XFS_IOLOCK_DEP(lock_flags));
20065523218SChristoph Hellwig }
201fa96acadSDave Chinner
2022433480aSJan Kara if (lock_flags & XFS_MMAPLOCK_EXCL) {
2032433480aSJan Kara down_write_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
2042433480aSJan Kara XFS_MMAPLOCK_DEP(lock_flags));
2052433480aSJan Kara } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
2062433480aSJan Kara down_read_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
2072433480aSJan Kara XFS_MMAPLOCK_DEP(lock_flags));
2082433480aSJan Kara }
209653c60b6SDave Chinner
210fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL)
211fa96acadSDave Chinner mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
212fa96acadSDave Chinner else if (lock_flags & XFS_ILOCK_SHARED)
213fa96acadSDave Chinner mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
214fa96acadSDave Chinner }
215fa96acadSDave Chinner
216fa96acadSDave Chinner /*
217fa96acadSDave Chinner * This is just like xfs_ilock(), except that the caller
218fa96acadSDave Chinner * is guaranteed not to sleep. It returns 1 if it gets
219fa96acadSDave Chinner * the requested locks and 0 otherwise. If the IO lock is
220fa96acadSDave Chinner * obtained but the inode lock cannot be, then the IO lock
221fa96acadSDave Chinner * is dropped before returning.
222fa96acadSDave Chinner *
223fa96acadSDave Chinner * ip -- the inode being locked
224fa96acadSDave Chinner * lock_flags -- this parameter indicates the inode's locks to be
225fa96acadSDave Chinner * to be locked. See the comment for xfs_ilock() for a list
226fa96acadSDave Chinner * of valid values.
227fa96acadSDave Chinner */
228fa96acadSDave Chinner int
xfs_ilock_nowait(xfs_inode_t * ip,uint lock_flags)229fa96acadSDave Chinner xfs_ilock_nowait(
230fa96acadSDave Chinner xfs_inode_t *ip,
231fa96acadSDave Chinner uint lock_flags)
232fa96acadSDave Chinner {
233fa96acadSDave Chinner trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
234fa96acadSDave Chinner
235ca76a761SKaixu Xia xfs_lock_flags_assert(lock_flags);
236fa96acadSDave Chinner
237fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL) {
23865523218SChristoph Hellwig if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
239fa96acadSDave Chinner goto out;
240fa96acadSDave Chinner } else if (lock_flags & XFS_IOLOCK_SHARED) {
24165523218SChristoph Hellwig if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
242fa96acadSDave Chinner goto out;
243fa96acadSDave Chinner }
244653c60b6SDave Chinner
245653c60b6SDave Chinner if (lock_flags & XFS_MMAPLOCK_EXCL) {
2462433480aSJan Kara if (!down_write_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
247653c60b6SDave Chinner goto out_undo_iolock;
248653c60b6SDave Chinner } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
2492433480aSJan Kara if (!down_read_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
250653c60b6SDave Chinner goto out_undo_iolock;
251653c60b6SDave Chinner }
252653c60b6SDave Chinner
253fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL) {
254fa96acadSDave Chinner if (!mrtryupdate(&ip->i_lock))
255653c60b6SDave Chinner goto out_undo_mmaplock;
256fa96acadSDave Chinner } else if (lock_flags & XFS_ILOCK_SHARED) {
257fa96acadSDave Chinner if (!mrtryaccess(&ip->i_lock))
258653c60b6SDave Chinner goto out_undo_mmaplock;
259fa96acadSDave Chinner }
260fa96acadSDave Chinner return 1;
261fa96acadSDave Chinner
262653c60b6SDave Chinner out_undo_mmaplock:
263653c60b6SDave Chinner if (lock_flags & XFS_MMAPLOCK_EXCL)
2642433480aSJan Kara up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
265653c60b6SDave Chinner else if (lock_flags & XFS_MMAPLOCK_SHARED)
2662433480aSJan Kara up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
267fa96acadSDave Chinner out_undo_iolock:
268fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL)
26965523218SChristoph Hellwig up_write(&VFS_I(ip)->i_rwsem);
270fa96acadSDave Chinner else if (lock_flags & XFS_IOLOCK_SHARED)
27165523218SChristoph Hellwig up_read(&VFS_I(ip)->i_rwsem);
272fa96acadSDave Chinner out:
273fa96acadSDave Chinner return 0;
274fa96acadSDave Chinner }
275fa96acadSDave Chinner
276fa96acadSDave Chinner /*
277fa96acadSDave Chinner * xfs_iunlock() is used to drop the inode locks acquired with
278fa96acadSDave Chinner * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
279fa96acadSDave Chinner * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
280fa96acadSDave Chinner * that we know which locks to drop.
281fa96acadSDave Chinner *
282fa96acadSDave Chinner * ip -- the inode being unlocked
283fa96acadSDave Chinner * lock_flags -- this parameter indicates the inode's locks to be
284fa96acadSDave Chinner * to be unlocked. See the comment for xfs_ilock() for a list
285fa96acadSDave Chinner * of valid values for this parameter.
286fa96acadSDave Chinner *
287fa96acadSDave Chinner */
288fa96acadSDave Chinner void
xfs_iunlock(xfs_inode_t * ip,uint lock_flags)289fa96acadSDave Chinner xfs_iunlock(
290fa96acadSDave Chinner xfs_inode_t *ip,
291fa96acadSDave Chinner uint lock_flags)
292fa96acadSDave Chinner {
293ca76a761SKaixu Xia xfs_lock_flags_assert(lock_flags);
294fa96acadSDave Chinner
295fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL)
29665523218SChristoph Hellwig up_write(&VFS_I(ip)->i_rwsem);
297fa96acadSDave Chinner else if (lock_flags & XFS_IOLOCK_SHARED)
29865523218SChristoph Hellwig up_read(&VFS_I(ip)->i_rwsem);
299fa96acadSDave Chinner
300653c60b6SDave Chinner if (lock_flags & XFS_MMAPLOCK_EXCL)
3012433480aSJan Kara up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
302653c60b6SDave Chinner else if (lock_flags & XFS_MMAPLOCK_SHARED)
3032433480aSJan Kara up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
304653c60b6SDave Chinner
305fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL)
306fa96acadSDave Chinner mrunlock_excl(&ip->i_lock);
307fa96acadSDave Chinner else if (lock_flags & XFS_ILOCK_SHARED)
308fa96acadSDave Chinner mrunlock_shared(&ip->i_lock);
309fa96acadSDave Chinner
310fa96acadSDave Chinner trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
311fa96acadSDave Chinner }
312fa96acadSDave Chinner
313fa96acadSDave Chinner /*
314fa96acadSDave Chinner * give up write locks. the i/o lock cannot be held nested
315fa96acadSDave Chinner * if it is being demoted.
316fa96acadSDave Chinner */
317fa96acadSDave Chinner void
xfs_ilock_demote(xfs_inode_t * ip,uint lock_flags)318fa96acadSDave Chinner xfs_ilock_demote(
319fa96acadSDave Chinner xfs_inode_t *ip,
320fa96acadSDave Chinner uint lock_flags)
321fa96acadSDave Chinner {
322653c60b6SDave Chinner ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
323653c60b6SDave Chinner ASSERT((lock_flags &
324653c60b6SDave Chinner ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
325fa96acadSDave Chinner
326fa96acadSDave Chinner if (lock_flags & XFS_ILOCK_EXCL)
327fa96acadSDave Chinner mrdemote(&ip->i_lock);
328653c60b6SDave Chinner if (lock_flags & XFS_MMAPLOCK_EXCL)
3292433480aSJan Kara downgrade_write(&VFS_I(ip)->i_mapping->invalidate_lock);
330fa96acadSDave Chinner if (lock_flags & XFS_IOLOCK_EXCL)
33165523218SChristoph Hellwig downgrade_write(&VFS_I(ip)->i_rwsem);
332fa96acadSDave Chinner
333fa96acadSDave Chinner trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
334fa96acadSDave Chinner }
335fa96acadSDave Chinner
336742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
337e31cbde7SPavel Reichl static inline bool
__xfs_rwsem_islocked(struct rw_semaphore * rwsem,bool shared)338e31cbde7SPavel Reichl __xfs_rwsem_islocked(
339e31cbde7SPavel Reichl struct rw_semaphore *rwsem,
340e31cbde7SPavel Reichl bool shared)
341e31cbde7SPavel Reichl {
342e31cbde7SPavel Reichl if (!debug_locks)
343e31cbde7SPavel Reichl return rwsem_is_locked(rwsem);
344e31cbde7SPavel Reichl
345e31cbde7SPavel Reichl if (!shared)
346e31cbde7SPavel Reichl return lockdep_is_held_type(rwsem, 0);
347e31cbde7SPavel Reichl
348e31cbde7SPavel Reichl /*
349e31cbde7SPavel Reichl * We are checking that the lock is held at least in shared
350e31cbde7SPavel Reichl * mode but don't care that it might be held exclusively
351e31cbde7SPavel Reichl * (i.e. shared | excl). Hence we check if the lock is held
352e31cbde7SPavel Reichl * in any mode rather than an explicit shared mode.
353e31cbde7SPavel Reichl */
354e31cbde7SPavel Reichl return lockdep_is_held_type(rwsem, -1);
355e31cbde7SPavel Reichl }
356e31cbde7SPavel Reichl
357e31cbde7SPavel Reichl bool
xfs_isilocked(struct xfs_inode * ip,uint lock_flags)358fa96acadSDave Chinner xfs_isilocked(
359e31cbde7SPavel Reichl struct xfs_inode *ip,
360fa96acadSDave Chinner uint lock_flags)
361fa96acadSDave Chinner {
362fa96acadSDave Chinner if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
363fa96acadSDave Chinner if (!(lock_flags & XFS_ILOCK_SHARED))
364fa96acadSDave Chinner return !!ip->i_lock.mr_writer;
365fa96acadSDave Chinner return rwsem_is_locked(&ip->i_lock.mr_lock);
366fa96acadSDave Chinner }
367fa96acadSDave Chinner
368653c60b6SDave Chinner if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
36982af8806SKaixu Xia return __xfs_rwsem_islocked(&VFS_I(ip)->i_mapping->invalidate_lock,
37082af8806SKaixu Xia (lock_flags & XFS_MMAPLOCK_SHARED));
371653c60b6SDave Chinner }
372653c60b6SDave Chinner
373fa96acadSDave Chinner if (lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) {
374e31cbde7SPavel Reichl return __xfs_rwsem_islocked(&VFS_I(ip)->i_rwsem,
375e31cbde7SPavel Reichl (lock_flags & XFS_IOLOCK_SHARED));
376fa96acadSDave Chinner }
377fa96acadSDave Chinner
378fa96acadSDave Chinner ASSERT(0);
379e31cbde7SPavel Reichl return false;
380fa96acadSDave Chinner }
381fa96acadSDave Chinner #endif
382fa96acadSDave Chinner
383b6a9947eSDave Chinner /*
384b6a9947eSDave Chinner * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
385b6a9947eSDave Chinner * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
386b6a9947eSDave Chinner * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
387b6a9947eSDave Chinner * errors and warnings.
388b6a9947eSDave Chinner */
389b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3903403ccc0SDave Chinner static bool
xfs_lockdep_subclass_ok(int subclass)3913403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3923403ccc0SDave Chinner int subclass)
3933403ccc0SDave Chinner {
3943403ccc0SDave Chinner return subclass < MAX_LOCKDEP_SUBCLASSES;
3953403ccc0SDave Chinner }
3963403ccc0SDave Chinner #else
3973403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass) (true)
3983403ccc0SDave Chinner #endif
3993403ccc0SDave Chinner
400c24b5dfaSDave Chinner /*
401653c60b6SDave Chinner * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
4020952c818SDave Chinner * value. This can be called for any type of inode lock combination, including
4030952c818SDave Chinner * parent locking. Care must be taken to ensure we don't overrun the subclass
4040952c818SDave Chinner * storage fields in the class mask we build.
405c24b5dfaSDave Chinner */
406a1033753SDave Chinner static inline uint
xfs_lock_inumorder(uint lock_mode,uint subclass)407a1033753SDave Chinner xfs_lock_inumorder(
408a1033753SDave Chinner uint lock_mode,
409a1033753SDave Chinner uint subclass)
410c24b5dfaSDave Chinner {
411a1033753SDave Chinner uint class = 0;
4120952c818SDave Chinner
4130952c818SDave Chinner ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
4140952c818SDave Chinner XFS_ILOCK_RTSUM)));
4153403ccc0SDave Chinner ASSERT(xfs_lockdep_subclass_ok(subclass));
4160952c818SDave Chinner
417653c60b6SDave Chinner if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
4180952c818SDave Chinner ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
4190952c818SDave Chinner class += subclass << XFS_IOLOCK_SHIFT;
420653c60b6SDave Chinner }
421653c60b6SDave Chinner
422653c60b6SDave Chinner if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4230952c818SDave Chinner ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4240952c818SDave Chinner class += subclass << XFS_MMAPLOCK_SHIFT;
425653c60b6SDave Chinner }
426653c60b6SDave Chinner
4270952c818SDave Chinner if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4280952c818SDave Chinner ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4290952c818SDave Chinner class += subclass << XFS_ILOCK_SHIFT;
4300952c818SDave Chinner }
431c24b5dfaSDave Chinner
4320952c818SDave Chinner return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
433c24b5dfaSDave Chinner }
434c24b5dfaSDave Chinner
435c24b5dfaSDave Chinner /*
43695afcf5cSDave Chinner * The following routine will lock n inodes in exclusive mode. We assume the
43795afcf5cSDave Chinner * caller calls us with the inodes in i_ino order.
438c24b5dfaSDave Chinner *
43995afcf5cSDave Chinner * We need to detect deadlock where an inode that we lock is in the AIL and we
44095afcf5cSDave Chinner * start waiting for another inode that is locked by a thread in a long running
44195afcf5cSDave Chinner * transaction (such as truncate). This can result in deadlock since the long
44295afcf5cSDave Chinner * running trans might need to wait for the inode we just locked in order to
44395afcf5cSDave Chinner * push the tail and free space in the log.
4440952c818SDave Chinner *
4450952c818SDave Chinner * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4460952c818SDave Chinner * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4470952c818SDave Chinner * lock more than one at a time, lockdep will report false positives saying we
4480952c818SDave Chinner * have violated locking orders.
449c24b5dfaSDave Chinner */
4500d5a75e9SEric Sandeen static void
xfs_lock_inodes(struct xfs_inode ** ips,int inodes,uint lock_mode)451c24b5dfaSDave Chinner xfs_lock_inodes(
452efe2330fSChristoph Hellwig struct xfs_inode **ips,
453c24b5dfaSDave Chinner int inodes,
454c24b5dfaSDave Chinner uint lock_mode)
455c24b5dfaSDave Chinner {
456a1033753SDave Chinner int attempts = 0;
457a1033753SDave Chinner uint i;
458a1033753SDave Chinner int j;
459a1033753SDave Chinner bool try_lock;
460efe2330fSChristoph Hellwig struct xfs_log_item *lp;
461c24b5dfaSDave Chinner
4620952c818SDave Chinner /*
4630952c818SDave Chinner * Currently supports between 2 and 5 inodes with exclusive locking. We
4640952c818SDave Chinner * support an arbitrary depth of locking here, but absolute limits on
465b63da6c8SRandy Dunlap * inodes depend on the type of locking and the limits placed by
4660952c818SDave Chinner * lockdep annotations in xfs_lock_inumorder. These are all checked by
4670952c818SDave Chinner * the asserts.
4680952c818SDave Chinner */
46995afcf5cSDave Chinner ASSERT(ips && inodes >= 2 && inodes <= 5);
4700952c818SDave Chinner ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4710952c818SDave Chinner XFS_ILOCK_EXCL));
4720952c818SDave Chinner ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4730952c818SDave Chinner XFS_ILOCK_SHARED)));
4740952c818SDave Chinner ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4750952c818SDave Chinner inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4760952c818SDave Chinner ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4770952c818SDave Chinner inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4780952c818SDave Chinner
4790952c818SDave Chinner if (lock_mode & XFS_IOLOCK_EXCL) {
4800952c818SDave Chinner ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4810952c818SDave Chinner } else if (lock_mode & XFS_MMAPLOCK_EXCL)
4820952c818SDave Chinner ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
483c24b5dfaSDave Chinner
484c24b5dfaSDave Chinner again:
485a1033753SDave Chinner try_lock = false;
486a1033753SDave Chinner i = 0;
487c24b5dfaSDave Chinner for (; i < inodes; i++) {
488c24b5dfaSDave Chinner ASSERT(ips[i]);
489c24b5dfaSDave Chinner
490c24b5dfaSDave Chinner if (i && (ips[i] == ips[i - 1])) /* Already locked */
491c24b5dfaSDave Chinner continue;
492c24b5dfaSDave Chinner
493c24b5dfaSDave Chinner /*
49495afcf5cSDave Chinner * If try_lock is not set yet, make sure all locked inodes are
49595afcf5cSDave Chinner * not in the AIL. If any are, set try_lock to be used later.
496c24b5dfaSDave Chinner */
497c24b5dfaSDave Chinner if (!try_lock) {
498c24b5dfaSDave Chinner for (j = (i - 1); j >= 0 && !try_lock; j--) {
499b3b14aacSChristoph Hellwig lp = &ips[j]->i_itemp->ili_item;
50022525c17SDave Chinner if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
501a1033753SDave Chinner try_lock = true;
502c24b5dfaSDave Chinner }
503c24b5dfaSDave Chinner }
504c24b5dfaSDave Chinner
505c24b5dfaSDave Chinner /*
506c24b5dfaSDave Chinner * If any of the previous locks we have locked is in the AIL,
507c24b5dfaSDave Chinner * we must TRY to get the second and subsequent locks. If
508c24b5dfaSDave Chinner * we can't get any, we must release all we have
509c24b5dfaSDave Chinner * and try again.
510c24b5dfaSDave Chinner */
51195afcf5cSDave Chinner if (!try_lock) {
51295afcf5cSDave Chinner xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
51395afcf5cSDave Chinner continue;
51495afcf5cSDave Chinner }
515c24b5dfaSDave Chinner
51695afcf5cSDave Chinner /* try_lock means we have an inode locked that is in the AIL. */
517c24b5dfaSDave Chinner ASSERT(i != 0);
51895afcf5cSDave Chinner if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
51995afcf5cSDave Chinner continue;
52095afcf5cSDave Chinner
52195afcf5cSDave Chinner /*
52295afcf5cSDave Chinner * Unlock all previous guys and try again. xfs_iunlock will try
52395afcf5cSDave Chinner * to push the tail if the inode is in the AIL.
52495afcf5cSDave Chinner */
525c24b5dfaSDave Chinner attempts++;
526c24b5dfaSDave Chinner for (j = i - 1; j >= 0; j--) {
527c24b5dfaSDave Chinner /*
52895afcf5cSDave Chinner * Check to see if we've already unlocked this one. Not
52995afcf5cSDave Chinner * the first one going back, and the inode ptr is the
53095afcf5cSDave Chinner * same.
531c24b5dfaSDave Chinner */
53295afcf5cSDave Chinner if (j != (i - 1) && ips[j] == ips[j + 1])
533c24b5dfaSDave Chinner continue;
534c24b5dfaSDave Chinner
535c24b5dfaSDave Chinner xfs_iunlock(ips[j], lock_mode);
536c24b5dfaSDave Chinner }
537c24b5dfaSDave Chinner
538c24b5dfaSDave Chinner if ((attempts % 5) == 0) {
539c24b5dfaSDave Chinner delay(1); /* Don't just spin the CPU */
540c24b5dfaSDave Chinner }
541c24b5dfaSDave Chinner goto again;
542c24b5dfaSDave Chinner }
543c24b5dfaSDave Chinner }
544c24b5dfaSDave Chinner
545c24b5dfaSDave Chinner /*
546d2c292d8SJan Kara * xfs_lock_two_inodes() can only be used to lock ilock. The iolock and
547d2c292d8SJan Kara * mmaplock must be double-locked separately since we use i_rwsem and
548d2c292d8SJan Kara * invalidate_lock for that. We now support taking one lock EXCL and the
549d2c292d8SJan Kara * other SHARED.
550c24b5dfaSDave Chinner */
551c24b5dfaSDave Chinner void
xfs_lock_two_inodes(struct xfs_inode * ip0,uint ip0_mode,struct xfs_inode * ip1,uint ip1_mode)552c24b5dfaSDave Chinner xfs_lock_two_inodes(
5537c2d238aSDarrick J. Wong struct xfs_inode *ip0,
5547c2d238aSDarrick J. Wong uint ip0_mode,
5557c2d238aSDarrick J. Wong struct xfs_inode *ip1,
5567c2d238aSDarrick J. Wong uint ip1_mode)
557c24b5dfaSDave Chinner {
558c24b5dfaSDave Chinner int attempts = 0;
559efe2330fSChristoph Hellwig struct xfs_log_item *lp;
560c24b5dfaSDave Chinner
5617c2d238aSDarrick J. Wong ASSERT(hweight32(ip0_mode) == 1);
5627c2d238aSDarrick J. Wong ASSERT(hweight32(ip1_mode) == 1);
5637c2d238aSDarrick J. Wong ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5647c2d238aSDarrick J. Wong ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
565d2c292d8SJan Kara ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
566d2c292d8SJan Kara ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
567c24b5dfaSDave Chinner ASSERT(ip0->i_ino != ip1->i_ino);
568c24b5dfaSDave Chinner
569c24b5dfaSDave Chinner if (ip0->i_ino > ip1->i_ino) {
5702a09b575SChangcheng Deng swap(ip0, ip1);
5712a09b575SChangcheng Deng swap(ip0_mode, ip1_mode);
572c24b5dfaSDave Chinner }
573c24b5dfaSDave Chinner
574c24b5dfaSDave Chinner again:
5757c2d238aSDarrick J. Wong xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
576c24b5dfaSDave Chinner
577c24b5dfaSDave Chinner /*
578c24b5dfaSDave Chinner * If the first lock we have locked is in the AIL, we must TRY to get
579c24b5dfaSDave Chinner * the second lock. If we can't get it, we must release the first one
580c24b5dfaSDave Chinner * and try again.
581c24b5dfaSDave Chinner */
582b3b14aacSChristoph Hellwig lp = &ip0->i_itemp->ili_item;
58322525c17SDave Chinner if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
5847c2d238aSDarrick J. Wong if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
5857c2d238aSDarrick J. Wong xfs_iunlock(ip0, ip0_mode);
586c24b5dfaSDave Chinner if ((++attempts % 5) == 0)
587c24b5dfaSDave Chinner delay(1); /* Don't just spin the CPU */
588c24b5dfaSDave Chinner goto again;
589c24b5dfaSDave Chinner }
590c24b5dfaSDave Chinner } else {
5917c2d238aSDarrick J. Wong xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
592c24b5dfaSDave Chinner }
593c24b5dfaSDave Chinner }
594c24b5dfaSDave Chinner
5951da177e4SLinus Torvalds uint
xfs_ip2xflags(struct xfs_inode * ip)5961da177e4SLinus Torvalds xfs_ip2xflags(
59758f88ca2SDave Chinner struct xfs_inode *ip)
5981da177e4SLinus Torvalds {
5994422501dSChristoph Hellwig uint flags = 0;
6001da177e4SLinus Torvalds
6014422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_ANY) {
6024422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_REALTIME)
6034422501dSChristoph Hellwig flags |= FS_XFLAG_REALTIME;
6044422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_PREALLOC)
6054422501dSChristoph Hellwig flags |= FS_XFLAG_PREALLOC;
6064422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
6074422501dSChristoph Hellwig flags |= FS_XFLAG_IMMUTABLE;
6084422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_APPEND)
6094422501dSChristoph Hellwig flags |= FS_XFLAG_APPEND;
6104422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_SYNC)
6114422501dSChristoph Hellwig flags |= FS_XFLAG_SYNC;
6124422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NOATIME)
6134422501dSChristoph Hellwig flags |= FS_XFLAG_NOATIME;
6144422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NODUMP)
6154422501dSChristoph Hellwig flags |= FS_XFLAG_NODUMP;
6164422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_RTINHERIT)
6174422501dSChristoph Hellwig flags |= FS_XFLAG_RTINHERIT;
6184422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_PROJINHERIT)
6194422501dSChristoph Hellwig flags |= FS_XFLAG_PROJINHERIT;
6204422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NOSYMLINKS)
6214422501dSChristoph Hellwig flags |= FS_XFLAG_NOSYMLINKS;
6224422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
6234422501dSChristoph Hellwig flags |= FS_XFLAG_EXTSIZE;
6244422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT)
6254422501dSChristoph Hellwig flags |= FS_XFLAG_EXTSZINHERIT;
6264422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NODEFRAG)
6274422501dSChristoph Hellwig flags |= FS_XFLAG_NODEFRAG;
6284422501dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_FILESTREAM)
6294422501dSChristoph Hellwig flags |= FS_XFLAG_FILESTREAM;
6304422501dSChristoph Hellwig }
6314422501dSChristoph Hellwig
6324422501dSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_ANY) {
6334422501dSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
6344422501dSChristoph Hellwig flags |= FS_XFLAG_DAX;
6354422501dSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6364422501dSChristoph Hellwig flags |= FS_XFLAG_COWEXTSIZE;
6374422501dSChristoph Hellwig }
6384422501dSChristoph Hellwig
639932b42c6SDarrick J. Wong if (xfs_inode_has_attr_fork(ip))
6404422501dSChristoph Hellwig flags |= FS_XFLAG_HASATTR;
6414422501dSChristoph Hellwig return flags;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds
6441da177e4SLinus Torvalds /*
645c24b5dfaSDave Chinner * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
646c24b5dfaSDave Chinner * is allowed, otherwise it has to be an exact match. If a CI match is found,
647c24b5dfaSDave Chinner * ci_name->name will point to a the actual name (caller must free) or
648c24b5dfaSDave Chinner * will be set to NULL if an exact match is found.
649c24b5dfaSDave Chinner */
650c24b5dfaSDave Chinner int
xfs_lookup(struct xfs_inode * dp,const struct xfs_name * name,struct xfs_inode ** ipp,struct xfs_name * ci_name)651c24b5dfaSDave Chinner xfs_lookup(
652996b2329SDarrick J. Wong struct xfs_inode *dp,
653996b2329SDarrick J. Wong const struct xfs_name *name,
654996b2329SDarrick J. Wong struct xfs_inode **ipp,
655c24b5dfaSDave Chinner struct xfs_name *ci_name)
656c24b5dfaSDave Chinner {
657c24b5dfaSDave Chinner xfs_ino_t inum;
658c24b5dfaSDave Chinner int error;
659c24b5dfaSDave Chinner
660c24b5dfaSDave Chinner trace_xfs_lookup(dp, name);
661c24b5dfaSDave Chinner
66275c8c50fSDave Chinner if (xfs_is_shutdown(dp->i_mount))
6632451337dSDave Chinner return -EIO;
664c24b5dfaSDave Chinner
665c24b5dfaSDave Chinner error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
666c24b5dfaSDave Chinner if (error)
667dbad7c99SDave Chinner goto out_unlock;
668c24b5dfaSDave Chinner
669c24b5dfaSDave Chinner error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
670c24b5dfaSDave Chinner if (error)
671c24b5dfaSDave Chinner goto out_free_name;
672c24b5dfaSDave Chinner
673c24b5dfaSDave Chinner return 0;
674c24b5dfaSDave Chinner
675c24b5dfaSDave Chinner out_free_name:
676c24b5dfaSDave Chinner if (ci_name)
677c24b5dfaSDave Chinner kmem_free(ci_name->name);
678dbad7c99SDave Chinner out_unlock:
679c24b5dfaSDave Chinner *ipp = NULL;
680c24b5dfaSDave Chinner return error;
681c24b5dfaSDave Chinner }
682c24b5dfaSDave Chinner
6838a569d71SDarrick J. Wong /* Propagate di_flags from a parent inode to a child inode. */
6848a569d71SDarrick J. Wong static void
xfs_inode_inherit_flags(struct xfs_inode * ip,const struct xfs_inode * pip)6858a569d71SDarrick J. Wong xfs_inode_inherit_flags(
6868a569d71SDarrick J. Wong struct xfs_inode *ip,
6878a569d71SDarrick J. Wong const struct xfs_inode *pip)
6888a569d71SDarrick J. Wong {
6898a569d71SDarrick J. Wong unsigned int di_flags = 0;
690603f000bSDarrick J. Wong xfs_failaddr_t failaddr;
6918a569d71SDarrick J. Wong umode_t mode = VFS_I(ip)->i_mode;
6928a569d71SDarrick J. Wong
6938a569d71SDarrick J. Wong if (S_ISDIR(mode)) {
694db07349dSChristoph Hellwig if (pip->i_diflags & XFS_DIFLAG_RTINHERIT)
6958a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_RTINHERIT;
696db07349dSChristoph Hellwig if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
6978a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_EXTSZINHERIT;
698031474c2SChristoph Hellwig ip->i_extsize = pip->i_extsize;
6998a569d71SDarrick J. Wong }
700db07349dSChristoph Hellwig if (pip->i_diflags & XFS_DIFLAG_PROJINHERIT)
7018a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_PROJINHERIT;
7028a569d71SDarrick J. Wong } else if (S_ISREG(mode)) {
703db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
70438c26bfdSDave Chinner xfs_has_realtime(ip->i_mount))
7058a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_REALTIME;
706db07349dSChristoph Hellwig if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
7078a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_EXTSIZE;
708031474c2SChristoph Hellwig ip->i_extsize = pip->i_extsize;
7098a569d71SDarrick J. Wong }
7108a569d71SDarrick J. Wong }
711db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_NOATIME) &&
7128a569d71SDarrick J. Wong xfs_inherit_noatime)
7138a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_NOATIME;
714db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_NODUMP) &&
7158a569d71SDarrick J. Wong xfs_inherit_nodump)
7168a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_NODUMP;
717db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_SYNC) &&
7188a569d71SDarrick J. Wong xfs_inherit_sync)
7198a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_SYNC;
720db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_NOSYMLINKS) &&
7218a569d71SDarrick J. Wong xfs_inherit_nosymlinks)
7228a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_NOSYMLINKS;
723db07349dSChristoph Hellwig if ((pip->i_diflags & XFS_DIFLAG_NODEFRAG) &&
7248a569d71SDarrick J. Wong xfs_inherit_nodefrag)
7258a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_NODEFRAG;
726db07349dSChristoph Hellwig if (pip->i_diflags & XFS_DIFLAG_FILESTREAM)
7278a569d71SDarrick J. Wong di_flags |= XFS_DIFLAG_FILESTREAM;
7288a569d71SDarrick J. Wong
729db07349dSChristoph Hellwig ip->i_diflags |= di_flags;
730603f000bSDarrick J. Wong
731603f000bSDarrick J. Wong /*
732603f000bSDarrick J. Wong * Inode verifiers on older kernels only check that the extent size
733603f000bSDarrick J. Wong * hint is an integer multiple of the rt extent size on realtime files.
734603f000bSDarrick J. Wong * They did not check the hint alignment on a directory with both
735603f000bSDarrick J. Wong * rtinherit and extszinherit flags set. If the misaligned hint is
736603f000bSDarrick J. Wong * propagated from a directory into a new realtime file, new file
737603f000bSDarrick J. Wong * allocations will fail due to math errors in the rt allocator and/or
738603f000bSDarrick J. Wong * trip the verifiers. Validate the hint settings in the new file so
739603f000bSDarrick J. Wong * that we don't let broken hints propagate.
740603f000bSDarrick J. Wong */
741603f000bSDarrick J. Wong failaddr = xfs_inode_validate_extsize(ip->i_mount, ip->i_extsize,
742603f000bSDarrick J. Wong VFS_I(ip)->i_mode, ip->i_diflags);
743603f000bSDarrick J. Wong if (failaddr) {
744603f000bSDarrick J. Wong ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
745603f000bSDarrick J. Wong XFS_DIFLAG_EXTSZINHERIT);
746603f000bSDarrick J. Wong ip->i_extsize = 0;
747603f000bSDarrick J. Wong }
7488a569d71SDarrick J. Wong }
7498a569d71SDarrick J. Wong
7508a569d71SDarrick J. Wong /* Propagate di_flags2 from a parent inode to a child inode. */
7518a569d71SDarrick J. Wong static void
xfs_inode_inherit_flags2(struct xfs_inode * ip,const struct xfs_inode * pip)7528a569d71SDarrick J. Wong xfs_inode_inherit_flags2(
7538a569d71SDarrick J. Wong struct xfs_inode *ip,
7548a569d71SDarrick J. Wong const struct xfs_inode *pip)
7558a569d71SDarrick J. Wong {
756603f000bSDarrick J. Wong xfs_failaddr_t failaddr;
757603f000bSDarrick J. Wong
7583e09ab8fSChristoph Hellwig if (pip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) {
7593e09ab8fSChristoph Hellwig ip->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
760b33ce57dSChristoph Hellwig ip->i_cowextsize = pip->i_cowextsize;
7618a569d71SDarrick J. Wong }
7623e09ab8fSChristoph Hellwig if (pip->i_diflags2 & XFS_DIFLAG2_DAX)
7633e09ab8fSChristoph Hellwig ip->i_diflags2 |= XFS_DIFLAG2_DAX;
764603f000bSDarrick J. Wong
765603f000bSDarrick J. Wong /* Don't let invalid cowextsize hints propagate. */
766603f000bSDarrick J. Wong failaddr = xfs_inode_validate_cowextsize(ip->i_mount, ip->i_cowextsize,
767603f000bSDarrick J. Wong VFS_I(ip)->i_mode, ip->i_diflags, ip->i_diflags2);
768603f000bSDarrick J. Wong if (failaddr) {
769603f000bSDarrick J. Wong ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
770603f000bSDarrick J. Wong ip->i_cowextsize = 0;
771603f000bSDarrick J. Wong }
7728a569d71SDarrick J. Wong }
7738a569d71SDarrick J. Wong
774c24b5dfaSDave Chinner /*
7751abcf261SDave Chinner * Initialise a newly allocated inode and return the in-core inode to the
7761abcf261SDave Chinner * caller locked exclusively.
7771da177e4SLinus Torvalds */
778b652afd9SDave Chinner int
xfs_init_new_inode(struct mnt_idmap * idmap,struct xfs_trans * tp,struct xfs_inode * pip,xfs_ino_t ino,umode_t mode,xfs_nlink_t nlink,dev_t rdev,prid_t prid,bool init_xattrs,struct xfs_inode ** ipp)7791abcf261SDave Chinner xfs_init_new_inode(
780f2d40141SChristian Brauner struct mnt_idmap *idmap,
7811abcf261SDave Chinner struct xfs_trans *tp,
7821abcf261SDave Chinner struct xfs_inode *pip,
7831abcf261SDave Chinner xfs_ino_t ino,
784576b1d67SAl Viro umode_t mode,
78531b084aeSNathan Scott xfs_nlink_t nlink,
78666f36464SChristoph Hellwig dev_t rdev,
7876743099cSArkadiusz Mi?kiewicz prid_t prid,
788e6a688c3SDave Chinner bool init_xattrs,
7891abcf261SDave Chinner struct xfs_inode **ipp)
7901da177e4SLinus Torvalds {
79101ea173eSChristoph Hellwig struct inode *dir = pip ? VFS_I(pip) : NULL;
79293848a99SChristoph Hellwig struct xfs_mount *mp = tp->t_mountp;
7931abcf261SDave Chinner struct xfs_inode *ip;
7941abcf261SDave Chinner unsigned int flags;
7951da177e4SLinus Torvalds int error;
79695582b00SDeepa Dinamani struct timespec64 tv;
7973987848cSDave Chinner struct inode *inode;
7981da177e4SLinus Torvalds
7991da177e4SLinus Torvalds /*
8008b26984dSDave Chinner * Protect against obviously corrupt allocation btree records. Later
8018b26984dSDave Chinner * xfs_iget checks will catch re-allocation of other active in-memory
8028b26984dSDave Chinner * and on-disk inodes. If we don't catch reallocating the parent inode
8038b26984dSDave Chinner * here we will deadlock in xfs_iget() so we have to do these checks
8048b26984dSDave Chinner * first.
8058b26984dSDave Chinner */
8068b26984dSDave Chinner if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
8078b26984dSDave Chinner xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
8088b26984dSDave Chinner return -EFSCORRUPTED;
8098b26984dSDave Chinner }
8108b26984dSDave Chinner
8118b26984dSDave Chinner /*
8121abcf261SDave Chinner * Get the in-core inode with the lock held exclusively to prevent
8131abcf261SDave Chinner * others from looking at until we're done.
8141da177e4SLinus Torvalds */
8151abcf261SDave Chinner error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
816bf904248SDavid Chinner if (error)
8171da177e4SLinus Torvalds return error;
8181abcf261SDave Chinner
8191da177e4SLinus Torvalds ASSERT(ip != NULL);
8203987848cSDave Chinner inode = VFS_I(ip);
82154d7b5c1SDave Chinner set_nlink(inode, nlink);
82266f36464SChristoph Hellwig inode->i_rdev = rdev;
823ceaf603cSChristoph Hellwig ip->i_projid = prid;
8241da177e4SLinus Torvalds
8250560f31aSDave Chinner if (dir && !(dir->i_mode & S_ISGID) && xfs_has_grpid(mp)) {
826c14329d3SChristian Brauner inode_fsuid_set(inode, idmap);
82701ea173eSChristoph Hellwig inode->i_gid = dir->i_gid;
82801ea173eSChristoph Hellwig inode->i_mode = mode;
8293d8f2821SChristoph Hellwig } else {
830f2d40141SChristian Brauner inode_init_owner(idmap, inode, dir, mode);
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds
8331da177e4SLinus Torvalds /*
8341da177e4SLinus Torvalds * If the group ID of the new file does not match the effective group
8351da177e4SLinus Torvalds * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8361da177e4SLinus Torvalds * (and only if the irix_sgid_inherit compatibility variable is set).
8371da177e4SLinus Torvalds */
83842b7cc11SChristian Brauner if (irix_sgid_inherit && (inode->i_mode & S_ISGID) &&
839e67fe633SChristian Brauner !vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)))
840c19b3b05SDave Chinner inode->i_mode &= ~S_ISGID;
8411da177e4SLinus Torvalds
84213d2c10bSChristoph Hellwig ip->i_disk_size = 0;
843daf83964SChristoph Hellwig ip->i_df.if_nextents = 0;
8446e73a545SChristoph Hellwig ASSERT(ip->i_nblocks == 0);
845dff35fd4SChristoph Hellwig
846a0a415e3SJeff Layton tv = inode_set_ctime_current(inode);
8473987848cSDave Chinner inode->i_mtime = tv;
8483987848cSDave Chinner inode->i_atime = tv;
849dff35fd4SChristoph Hellwig
850031474c2SChristoph Hellwig ip->i_extsize = 0;
851db07349dSChristoph Hellwig ip->i_diflags = 0;
85293848a99SChristoph Hellwig
85338c26bfdSDave Chinner if (xfs_has_v3inodes(mp)) {
854f0e28280SJeff Layton inode_set_iversion(inode, 1);
855b33ce57dSChristoph Hellwig ip->i_cowextsize = 0;
856e98d5e88SChristoph Hellwig ip->i_crtime = tv;
85793848a99SChristoph Hellwig }
85893848a99SChristoph Hellwig
8591da177e4SLinus Torvalds flags = XFS_ILOG_CORE;
8601da177e4SLinus Torvalds switch (mode & S_IFMT) {
8611da177e4SLinus Torvalds case S_IFIFO:
8621da177e4SLinus Torvalds case S_IFCHR:
8631da177e4SLinus Torvalds case S_IFBLK:
8641da177e4SLinus Torvalds case S_IFSOCK:
865f7e67b20SChristoph Hellwig ip->i_df.if_format = XFS_DINODE_FMT_DEV;
8661da177e4SLinus Torvalds flags |= XFS_ILOG_DEV;
8671da177e4SLinus Torvalds break;
8681da177e4SLinus Torvalds case S_IFREG:
8691da177e4SLinus Torvalds case S_IFDIR:
870db07349dSChristoph Hellwig if (pip && (pip->i_diflags & XFS_DIFLAG_ANY))
8718a569d71SDarrick J. Wong xfs_inode_inherit_flags(ip, pip);
8723e09ab8fSChristoph Hellwig if (pip && (pip->i_diflags2 & XFS_DIFLAG2_ANY))
8738a569d71SDarrick J. Wong xfs_inode_inherit_flags2(ip, pip);
87453004ee7SGustavo A. R. Silva fallthrough;
8751da177e4SLinus Torvalds case S_IFLNK:
876f7e67b20SChristoph Hellwig ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
877fcacbc3fSChristoph Hellwig ip->i_df.if_bytes = 0;
8786bdcf26aSChristoph Hellwig ip->i_df.if_u1.if_root = NULL;
8791da177e4SLinus Torvalds break;
8801da177e4SLinus Torvalds default:
8811da177e4SLinus Torvalds ASSERT(0);
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds
8841da177e4SLinus Torvalds /*
885e6a688c3SDave Chinner * If we need to create attributes immediately after allocating the
886e6a688c3SDave Chinner * inode, initialise an empty attribute fork right now. We use the
887e6a688c3SDave Chinner * default fork offset for attributes here as we don't know exactly what
888e6a688c3SDave Chinner * size or how many attributes we might be adding. We can do this
889e6a688c3SDave Chinner * safely here because we know the data fork is completely empty and
890e6a688c3SDave Chinner * this saves us from needing to run a separate transaction to set the
891e6a688c3SDave Chinner * fork offset in the immediate future.
892e6a688c3SDave Chinner */
89338c26bfdSDave Chinner if (init_xattrs && xfs_has_attr(mp)) {
8947821ea30SChristoph Hellwig ip->i_forkoff = xfs_default_attroffset(ip) >> 3;
8952ed5b09bSDarrick J. Wong xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
896e6a688c3SDave Chinner }
897e6a688c3SDave Chinner
898e6a688c3SDave Chinner /*
8991da177e4SLinus Torvalds * Log the new values stuffed into the inode.
9001da177e4SLinus Torvalds */
901ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9021da177e4SLinus Torvalds xfs_trans_log_inode(tp, ip, flags);
9031da177e4SLinus Torvalds
90458c90473SDave Chinner /* now that we have an i_mode we can setup the inode structure */
90541be8bedSChristoph Hellwig xfs_setup_inode(ip);
9061da177e4SLinus Torvalds
9071da177e4SLinus Torvalds *ipp = ip;
9081da177e4SLinus Torvalds return 0;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds
911e546cb79SDave Chinner /*
91254d7b5c1SDave Chinner * Decrement the link count on an inode & log the change. If this causes the
91354d7b5c1SDave Chinner * link count to go to zero, move the inode to AGI unlinked list so that it can
91454d7b5c1SDave Chinner * be freed when the last active reference goes away via xfs_inactive().
915e546cb79SDave Chinner */
9160d5a75e9SEric Sandeen static int /* error */
xfs_droplink(xfs_trans_t * tp,xfs_inode_t * ip)917e546cb79SDave Chinner xfs_droplink(
918e546cb79SDave Chinner xfs_trans_t *tp,
919e546cb79SDave Chinner xfs_inode_t *ip)
920e546cb79SDave Chinner {
92147b07e51SCheng Lin if (VFS_I(ip)->i_nlink == 0) {
92247b07e51SCheng Lin xfs_alert(ip->i_mount,
92347b07e51SCheng Lin "%s: Attempt to drop inode (%llu) with nlink zero.",
92447b07e51SCheng Lin __func__, ip->i_ino);
92547b07e51SCheng Lin return -EFSCORRUPTED;
92647b07e51SCheng Lin }
92747b07e51SCheng Lin
928e546cb79SDave Chinner xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
929e546cb79SDave Chinner
930e546cb79SDave Chinner drop_nlink(VFS_I(ip));
931e546cb79SDave Chinner xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
932e546cb79SDave Chinner
93354d7b5c1SDave Chinner if (VFS_I(ip)->i_nlink)
93454d7b5c1SDave Chinner return 0;
93554d7b5c1SDave Chinner
93654d7b5c1SDave Chinner return xfs_iunlink(tp, ip);
937e546cb79SDave Chinner }
938e546cb79SDave Chinner
939e546cb79SDave Chinner /*
940e546cb79SDave Chinner * Increment the link count on an inode & log the change.
941e546cb79SDave Chinner */
94291083269SEric Sandeen static void
xfs_bumplink(xfs_trans_t * tp,xfs_inode_t * ip)943e546cb79SDave Chinner xfs_bumplink(
944e546cb79SDave Chinner xfs_trans_t *tp,
945e546cb79SDave Chinner xfs_inode_t *ip)
946e546cb79SDave Chinner {
947e546cb79SDave Chinner xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
948e546cb79SDave Chinner
949e546cb79SDave Chinner inc_nlink(VFS_I(ip));
950e546cb79SDave Chinner xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
951e546cb79SDave Chinner }
952e546cb79SDave Chinner
953c24b5dfaSDave Chinner int
xfs_create(struct mnt_idmap * idmap,xfs_inode_t * dp,struct xfs_name * name,umode_t mode,dev_t rdev,bool init_xattrs,xfs_inode_t ** ipp)954c24b5dfaSDave Chinner xfs_create(
955f2d40141SChristian Brauner struct mnt_idmap *idmap,
956c24b5dfaSDave Chinner xfs_inode_t *dp,
957c24b5dfaSDave Chinner struct xfs_name *name,
958c24b5dfaSDave Chinner umode_t mode,
95966f36464SChristoph Hellwig dev_t rdev,
960e6a688c3SDave Chinner bool init_xattrs,
961c24b5dfaSDave Chinner xfs_inode_t **ipp)
962c24b5dfaSDave Chinner {
963c24b5dfaSDave Chinner int is_dir = S_ISDIR(mode);
964c24b5dfaSDave Chinner struct xfs_mount *mp = dp->i_mount;
965c24b5dfaSDave Chinner struct xfs_inode *ip = NULL;
966c24b5dfaSDave Chinner struct xfs_trans *tp = NULL;
967c24b5dfaSDave Chinner int error;
968c24b5dfaSDave Chinner bool unlock_dp_on_error = false;
969c24b5dfaSDave Chinner prid_t prid;
970c24b5dfaSDave Chinner struct xfs_dquot *udqp = NULL;
971c24b5dfaSDave Chinner struct xfs_dquot *gdqp = NULL;
972c24b5dfaSDave Chinner struct xfs_dquot *pdqp = NULL;
973062647a8SBrian Foster struct xfs_trans_res *tres;
974c24b5dfaSDave Chinner uint resblks;
975b652afd9SDave Chinner xfs_ino_t ino;
976c24b5dfaSDave Chinner
977c24b5dfaSDave Chinner trace_xfs_create(dp, name);
978c24b5dfaSDave Chinner
97975c8c50fSDave Chinner if (xfs_is_shutdown(mp))
9802451337dSDave Chinner return -EIO;
981c24b5dfaSDave Chinner
982163467d3SZhi Yong Wu prid = xfs_get_initial_prid(dp);
983c24b5dfaSDave Chinner
984c24b5dfaSDave Chinner /*
985ff627196SDarrick J. Wong * Make sure that we have allocated dquot(s) on disk. The uid/gid
986ff627196SDarrick J. Wong * computation code must match what the VFS uses to assign i_[ug]id.
987ff627196SDarrick J. Wong * INHERIT adjusts the gid computation for setgid/grpid systems.
988c24b5dfaSDave Chinner */
989ff627196SDarrick J. Wong error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, i_user_ns(VFS_I(dp))),
990ff627196SDarrick J. Wong mapped_fsgid(idmap, i_user_ns(VFS_I(dp))), prid,
991c24b5dfaSDave Chinner XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
992c24b5dfaSDave Chinner &udqp, &gdqp, &pdqp);
993c24b5dfaSDave Chinner if (error)
994c24b5dfaSDave Chinner return error;
995c24b5dfaSDave Chinner
996c24b5dfaSDave Chinner if (is_dir) {
997c24b5dfaSDave Chinner resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
998062647a8SBrian Foster tres = &M_RES(mp)->tr_mkdir;
999c24b5dfaSDave Chinner } else {
1000c24b5dfaSDave Chinner resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1001062647a8SBrian Foster tres = &M_RES(mp)->tr_create;
1002c24b5dfaSDave Chinner }
1003c24b5dfaSDave Chinner
1004c24b5dfaSDave Chinner /*
1005c24b5dfaSDave Chinner * Initially assume that the file does not exist and
1006c24b5dfaSDave Chinner * reserve the resources for that case. If that is not
1007c24b5dfaSDave Chinner * the case we'll drop the one we have and get a more
1008c24b5dfaSDave Chinner * appropriate transaction later.
1009c24b5dfaSDave Chinner */
1010f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1011f2f7b9ffSDarrick J. Wong &tp);
10122451337dSDave Chinner if (error == -ENOSPC) {
1013c24b5dfaSDave Chinner /* flush outstanding delalloc blocks and retry */
1014c24b5dfaSDave Chinner xfs_flush_inodes(mp);
1015f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
1016f2f7b9ffSDarrick J. Wong resblks, &tp);
1017c24b5dfaSDave Chinner }
10184906e215SChristoph Hellwig if (error)
1019f2f7b9ffSDarrick J. Wong goto out_release_dquots;
1020c24b5dfaSDave Chinner
102165523218SChristoph Hellwig xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1022c24b5dfaSDave Chinner unlock_dp_on_error = true;
1023c24b5dfaSDave Chinner
1024c24b5dfaSDave Chinner /*
1025c24b5dfaSDave Chinner * A newly created regular or special file just has one directory
1026c24b5dfaSDave Chinner * entry pointing to them, but a directory also the "." entry
1027c24b5dfaSDave Chinner * pointing to itself.
1028c24b5dfaSDave Chinner */
1029b652afd9SDave Chinner error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1030b652afd9SDave Chinner if (!error)
1031f2d40141SChristian Brauner error = xfs_init_new_inode(idmap, tp, dp, ino, mode,
1032b652afd9SDave Chinner is_dir ? 2 : 1, rdev, prid, init_xattrs, &ip);
1033d6077aa3SJan Kara if (error)
1034c24b5dfaSDave Chinner goto out_trans_cancel;
1035c24b5dfaSDave Chinner
1036c24b5dfaSDave Chinner /*
1037c24b5dfaSDave Chinner * Now we join the directory inode to the transaction. We do not do it
1038b652afd9SDave Chinner * earlier because xfs_dialloc might commit the previous transaction
1039c24b5dfaSDave Chinner * (and release all the locks). An error from here on will result in
1040c24b5dfaSDave Chinner * the transaction cancel unlocking dp so don't do it explicitly in the
1041c24b5dfaSDave Chinner * error path.
1042c24b5dfaSDave Chinner */
104365523218SChristoph Hellwig xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1044c24b5dfaSDave Chinner unlock_dp_on_error = false;
1045c24b5dfaSDave Chinner
1046381eee69SBrian Foster error = xfs_dir_createname(tp, dp, name, ip->i_ino,
104763337b63SKaixu Xia resblks - XFS_IALLOC_SPACE_RES(mp));
1048c24b5dfaSDave Chinner if (error) {
10492451337dSDave Chinner ASSERT(error != -ENOSPC);
10504906e215SChristoph Hellwig goto out_trans_cancel;
1051c24b5dfaSDave Chinner }
1052c24b5dfaSDave Chinner xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1053c24b5dfaSDave Chinner xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1054c24b5dfaSDave Chinner
1055c24b5dfaSDave Chinner if (is_dir) {
1056c24b5dfaSDave Chinner error = xfs_dir_init(tp, ip, dp);
1057c24b5dfaSDave Chinner if (error)
1058c8eac49eSBrian Foster goto out_trans_cancel;
1059c24b5dfaSDave Chinner
106091083269SEric Sandeen xfs_bumplink(tp, dp);
1061c24b5dfaSDave Chinner }
1062c24b5dfaSDave Chinner
1063c24b5dfaSDave Chinner /*
1064c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the
1065c24b5dfaSDave Chinner * create transaction goes to disk before returning to
1066c24b5dfaSDave Chinner * the user.
1067c24b5dfaSDave Chinner */
10680560f31aSDave Chinner if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
1069c24b5dfaSDave Chinner xfs_trans_set_sync(tp);
1070c24b5dfaSDave Chinner
1071c24b5dfaSDave Chinner /*
1072c24b5dfaSDave Chinner * Attach the dquot(s) to the inodes and modify them incore.
1073c24b5dfaSDave Chinner * These ids of the inode couldn't have changed since the new
1074c24b5dfaSDave Chinner * inode has been locked ever since it was created.
1075c24b5dfaSDave Chinner */
1076c24b5dfaSDave Chinner xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1077c24b5dfaSDave Chinner
107870393313SChristoph Hellwig error = xfs_trans_commit(tp);
1079c24b5dfaSDave Chinner if (error)
1080c24b5dfaSDave Chinner goto out_release_inode;
1081c24b5dfaSDave Chinner
1082c24b5dfaSDave Chinner xfs_qm_dqrele(udqp);
1083c24b5dfaSDave Chinner xfs_qm_dqrele(gdqp);
1084c24b5dfaSDave Chinner xfs_qm_dqrele(pdqp);
1085c24b5dfaSDave Chinner
1086c24b5dfaSDave Chinner *ipp = ip;
1087c24b5dfaSDave Chinner return 0;
1088c24b5dfaSDave Chinner
1089c24b5dfaSDave Chinner out_trans_cancel:
10904906e215SChristoph Hellwig xfs_trans_cancel(tp);
1091c24b5dfaSDave Chinner out_release_inode:
1092c24b5dfaSDave Chinner /*
109358c90473SDave Chinner * Wait until after the current transaction is aborted to finish the
109458c90473SDave Chinner * setup of the inode and release the inode. This prevents recursive
109558c90473SDave Chinner * transactions and deadlocks from xfs_inactive.
1096c24b5dfaSDave Chinner */
109758c90473SDave Chinner if (ip) {
109858c90473SDave Chinner xfs_finish_inode_setup(ip);
109944a8736bSDarrick J. Wong xfs_irele(ip);
110058c90473SDave Chinner }
1101f2f7b9ffSDarrick J. Wong out_release_dquots:
1102c24b5dfaSDave Chinner xfs_qm_dqrele(udqp);
1103c24b5dfaSDave Chinner xfs_qm_dqrele(gdqp);
1104c24b5dfaSDave Chinner xfs_qm_dqrele(pdqp);
1105c24b5dfaSDave Chinner
1106c24b5dfaSDave Chinner if (unlock_dp_on_error)
110765523218SChristoph Hellwig xfs_iunlock(dp, XFS_ILOCK_EXCL);
1108c24b5dfaSDave Chinner return error;
1109c24b5dfaSDave Chinner }
1110c24b5dfaSDave Chinner
1111c24b5dfaSDave Chinner int
xfs_create_tmpfile(struct mnt_idmap * idmap,struct xfs_inode * dp,umode_t mode,struct xfs_inode ** ipp)111299b6436bSZhi Yong Wu xfs_create_tmpfile(
1113f2d40141SChristian Brauner struct mnt_idmap *idmap,
111499b6436bSZhi Yong Wu struct xfs_inode *dp,
1115330033d6SBrian Foster umode_t mode,
1116330033d6SBrian Foster struct xfs_inode **ipp)
111799b6436bSZhi Yong Wu {
111899b6436bSZhi Yong Wu struct xfs_mount *mp = dp->i_mount;
111999b6436bSZhi Yong Wu struct xfs_inode *ip = NULL;
112099b6436bSZhi Yong Wu struct xfs_trans *tp = NULL;
112199b6436bSZhi Yong Wu int error;
112299b6436bSZhi Yong Wu prid_t prid;
112399b6436bSZhi Yong Wu struct xfs_dquot *udqp = NULL;
112499b6436bSZhi Yong Wu struct xfs_dquot *gdqp = NULL;
112599b6436bSZhi Yong Wu struct xfs_dquot *pdqp = NULL;
112699b6436bSZhi Yong Wu struct xfs_trans_res *tres;
112799b6436bSZhi Yong Wu uint resblks;
1128b652afd9SDave Chinner xfs_ino_t ino;
112999b6436bSZhi Yong Wu
113075c8c50fSDave Chinner if (xfs_is_shutdown(mp))
11312451337dSDave Chinner return -EIO;
113299b6436bSZhi Yong Wu
113399b6436bSZhi Yong Wu prid = xfs_get_initial_prid(dp);
113499b6436bSZhi Yong Wu
113599b6436bSZhi Yong Wu /*
1136ff627196SDarrick J. Wong * Make sure that we have allocated dquot(s) on disk. The uid/gid
1137ff627196SDarrick J. Wong * computation code must match what the VFS uses to assign i_[ug]id.
1138ff627196SDarrick J. Wong * INHERIT adjusts the gid computation for setgid/grpid systems.
113999b6436bSZhi Yong Wu */
1140ff627196SDarrick J. Wong error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, i_user_ns(VFS_I(dp))),
1141ff627196SDarrick J. Wong mapped_fsgid(idmap, i_user_ns(VFS_I(dp))), prid,
114299b6436bSZhi Yong Wu XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
114399b6436bSZhi Yong Wu &udqp, &gdqp, &pdqp);
114499b6436bSZhi Yong Wu if (error)
114599b6436bSZhi Yong Wu return error;
114699b6436bSZhi Yong Wu
114799b6436bSZhi Yong Wu resblks = XFS_IALLOC_SPACE_RES(mp);
114899b6436bSZhi Yong Wu tres = &M_RES(mp)->tr_create_tmpfile;
1149253f4911SChristoph Hellwig
1150f2f7b9ffSDarrick J. Wong error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1151f2f7b9ffSDarrick J. Wong &tp);
11524906e215SChristoph Hellwig if (error)
1153f2f7b9ffSDarrick J. Wong goto out_release_dquots;
115499b6436bSZhi Yong Wu
1155b652afd9SDave Chinner error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1156b652afd9SDave Chinner if (!error)
1157f2d40141SChristian Brauner error = xfs_init_new_inode(idmap, tp, dp, ino, mode,
1158b652afd9SDave Chinner 0, 0, prid, false, &ip);
1159d6077aa3SJan Kara if (error)
116099b6436bSZhi Yong Wu goto out_trans_cancel;
116199b6436bSZhi Yong Wu
11620560f31aSDave Chinner if (xfs_has_wsync(mp))
116399b6436bSZhi Yong Wu xfs_trans_set_sync(tp);
116499b6436bSZhi Yong Wu
116599b6436bSZhi Yong Wu /*
116699b6436bSZhi Yong Wu * Attach the dquot(s) to the inodes and modify them incore.
116799b6436bSZhi Yong Wu * These ids of the inode couldn't have changed since the new
116899b6436bSZhi Yong Wu * inode has been locked ever since it was created.
116999b6436bSZhi Yong Wu */
117099b6436bSZhi Yong Wu xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
117199b6436bSZhi Yong Wu
117299b6436bSZhi Yong Wu error = xfs_iunlink(tp, ip);
117399b6436bSZhi Yong Wu if (error)
11744906e215SChristoph Hellwig goto out_trans_cancel;
117599b6436bSZhi Yong Wu
117670393313SChristoph Hellwig error = xfs_trans_commit(tp);
117799b6436bSZhi Yong Wu if (error)
117899b6436bSZhi Yong Wu goto out_release_inode;
117999b6436bSZhi Yong Wu
118099b6436bSZhi Yong Wu xfs_qm_dqrele(udqp);
118199b6436bSZhi Yong Wu xfs_qm_dqrele(gdqp);
118299b6436bSZhi Yong Wu xfs_qm_dqrele(pdqp);
118399b6436bSZhi Yong Wu
1184330033d6SBrian Foster *ipp = ip;
118599b6436bSZhi Yong Wu return 0;
118699b6436bSZhi Yong Wu
118799b6436bSZhi Yong Wu out_trans_cancel:
11884906e215SChristoph Hellwig xfs_trans_cancel(tp);
118999b6436bSZhi Yong Wu out_release_inode:
119099b6436bSZhi Yong Wu /*
119158c90473SDave Chinner * Wait until after the current transaction is aborted to finish the
119258c90473SDave Chinner * setup of the inode and release the inode. This prevents recursive
119358c90473SDave Chinner * transactions and deadlocks from xfs_inactive.
119499b6436bSZhi Yong Wu */
119558c90473SDave Chinner if (ip) {
119658c90473SDave Chinner xfs_finish_inode_setup(ip);
119744a8736bSDarrick J. Wong xfs_irele(ip);
119858c90473SDave Chinner }
1199f2f7b9ffSDarrick J. Wong out_release_dquots:
120099b6436bSZhi Yong Wu xfs_qm_dqrele(udqp);
120199b6436bSZhi Yong Wu xfs_qm_dqrele(gdqp);
120299b6436bSZhi Yong Wu xfs_qm_dqrele(pdqp);
120399b6436bSZhi Yong Wu
120499b6436bSZhi Yong Wu return error;
120599b6436bSZhi Yong Wu }
120699b6436bSZhi Yong Wu
120799b6436bSZhi Yong Wu int
xfs_link(xfs_inode_t * tdp,xfs_inode_t * sip,struct xfs_name * target_name)1208c24b5dfaSDave Chinner xfs_link(
1209c24b5dfaSDave Chinner xfs_inode_t *tdp,
1210c24b5dfaSDave Chinner xfs_inode_t *sip,
1211c24b5dfaSDave Chinner struct xfs_name *target_name)
1212c24b5dfaSDave Chinner {
1213c24b5dfaSDave Chinner xfs_mount_t *mp = tdp->i_mount;
1214c24b5dfaSDave Chinner xfs_trans_t *tp;
1215871b9316SDarrick J. Wong int error, nospace_error = 0;
1216c24b5dfaSDave Chinner int resblks;
1217c24b5dfaSDave Chinner
1218c24b5dfaSDave Chinner trace_xfs_link(tdp, target_name);
1219c24b5dfaSDave Chinner
1220c19b3b05SDave Chinner ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1221c24b5dfaSDave Chinner
122275c8c50fSDave Chinner if (xfs_is_shutdown(mp))
12232451337dSDave Chinner return -EIO;
1224c24b5dfaSDave Chinner
1225c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(sip);
1226c24b5dfaSDave Chinner if (error)
1227c24b5dfaSDave Chinner goto std_return;
1228c24b5dfaSDave Chinner
1229c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(tdp);
1230c24b5dfaSDave Chinner if (error)
1231c24b5dfaSDave Chinner goto std_return;
1232c24b5dfaSDave Chinner
1233c24b5dfaSDave Chinner resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1234871b9316SDarrick J. Wong error = xfs_trans_alloc_dir(tdp, &M_RES(mp)->tr_link, sip, &resblks,
1235871b9316SDarrick J. Wong &tp, &nospace_error);
12364906e215SChristoph Hellwig if (error)
1237253f4911SChristoph Hellwig goto std_return;
1238c24b5dfaSDave Chinner
1239c24b5dfaSDave Chinner /*
1240c24b5dfaSDave Chinner * If we are using project inheritance, we only allow hard link
1241c24b5dfaSDave Chinner * creation in our tree when the project IDs are the same; else
1242c24b5dfaSDave Chinner * the tree quota mechanism could be circumvented.
1243c24b5dfaSDave Chinner */
1244db07349dSChristoph Hellwig if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
1245ceaf603cSChristoph Hellwig tdp->i_projid != sip->i_projid)) {
12469f205010SAndrey Albershteyn /*
12479f205010SAndrey Albershteyn * Project quota setup skips special files which can
12489f205010SAndrey Albershteyn * leave inodes in a PROJINHERIT directory without a
12499f205010SAndrey Albershteyn * project ID set. We need to allow links to be made
12509f205010SAndrey Albershteyn * to these "project-less" inodes because userspace
12519f205010SAndrey Albershteyn * expects them to succeed after project ID setup,
12529f205010SAndrey Albershteyn * but everything else should be rejected.
12539f205010SAndrey Albershteyn */
12549f205010SAndrey Albershteyn if (!special_file(VFS_I(sip)->i_mode) ||
12559f205010SAndrey Albershteyn sip->i_projid != 0) {
12562451337dSDave Chinner error = -EXDEV;
1257c24b5dfaSDave Chinner goto error_return;
1258c24b5dfaSDave Chinner }
12599f205010SAndrey Albershteyn }
1260c24b5dfaSDave Chinner
126194f3cad5SEric Sandeen if (!resblks) {
126294f3cad5SEric Sandeen error = xfs_dir_canenter(tp, tdp, target_name);
1263c24b5dfaSDave Chinner if (error)
1264c24b5dfaSDave Chinner goto error_return;
126594f3cad5SEric Sandeen }
1266c24b5dfaSDave Chinner
126754d7b5c1SDave Chinner /*
126854d7b5c1SDave Chinner * Handle initial link state of O_TMPFILE inode
126954d7b5c1SDave Chinner */
127054d7b5c1SDave Chinner if (VFS_I(sip)->i_nlink == 0) {
1271f40aadb2SDave Chinner struct xfs_perag *pag;
1272f40aadb2SDave Chinner
1273f40aadb2SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sip->i_ino));
1274f40aadb2SDave Chinner error = xfs_iunlink_remove(tp, pag, sip);
1275f40aadb2SDave Chinner xfs_perag_put(pag);
1276ab297431SZhi Yong Wu if (error)
12774906e215SChristoph Hellwig goto error_return;
1278ab297431SZhi Yong Wu }
1279ab297431SZhi Yong Wu
1280c24b5dfaSDave Chinner error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1281381eee69SBrian Foster resblks);
1282c24b5dfaSDave Chinner if (error)
12834906e215SChristoph Hellwig goto error_return;
1284c24b5dfaSDave Chinner xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1285c24b5dfaSDave Chinner xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1286c24b5dfaSDave Chinner
128791083269SEric Sandeen xfs_bumplink(tp, sip);
1288c24b5dfaSDave Chinner
1289c24b5dfaSDave Chinner /*
1290c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the
1291c24b5dfaSDave Chinner * link transaction goes to disk before returning to
1292c24b5dfaSDave Chinner * the user.
1293c24b5dfaSDave Chinner */
12940560f31aSDave Chinner if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
1295c24b5dfaSDave Chinner xfs_trans_set_sync(tp);
1296c24b5dfaSDave Chinner
129770393313SChristoph Hellwig return xfs_trans_commit(tp);
1298c24b5dfaSDave Chinner
1299c24b5dfaSDave Chinner error_return:
13004906e215SChristoph Hellwig xfs_trans_cancel(tp);
1301c24b5dfaSDave Chinner std_return:
1302871b9316SDarrick J. Wong if (error == -ENOSPC && nospace_error)
1303871b9316SDarrick J. Wong error = nospace_error;
1304c24b5dfaSDave Chinner return error;
1305c24b5dfaSDave Chinner }
1306c24b5dfaSDave Chinner
1307363e59baSDarrick J. Wong /* Clear the reflink flag and the cowblocks tag if possible. */
1308363e59baSDarrick J. Wong static void
xfs_itruncate_clear_reflink_flags(struct xfs_inode * ip)1309363e59baSDarrick J. Wong xfs_itruncate_clear_reflink_flags(
1310363e59baSDarrick J. Wong struct xfs_inode *ip)
1311363e59baSDarrick J. Wong {
1312363e59baSDarrick J. Wong struct xfs_ifork *dfork;
1313363e59baSDarrick J. Wong struct xfs_ifork *cfork;
1314363e59baSDarrick J. Wong
1315363e59baSDarrick J. Wong if (!xfs_is_reflink_inode(ip))
1316363e59baSDarrick J. Wong return;
1317732436efSDarrick J. Wong dfork = xfs_ifork_ptr(ip, XFS_DATA_FORK);
1318732436efSDarrick J. Wong cfork = xfs_ifork_ptr(ip, XFS_COW_FORK);
1319363e59baSDarrick J. Wong if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
13203e09ab8fSChristoph Hellwig ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
1321363e59baSDarrick J. Wong if (cfork->if_bytes == 0)
1322363e59baSDarrick J. Wong xfs_inode_clear_cowblocks_tag(ip);
1323363e59baSDarrick J. Wong }
1324363e59baSDarrick J. Wong
13251da177e4SLinus Torvalds /*
13268f04c47aSChristoph Hellwig * Free up the underlying blocks past new_size. The new size must be smaller
13278f04c47aSChristoph Hellwig * than the current size. This routine can be used both for the attribute and
13288f04c47aSChristoph Hellwig * data fork, and does not modify the inode size, which is left to the caller.
13291da177e4SLinus Torvalds *
1330f6485057SDavid Chinner * The transaction passed to this routine must have made a permanent log
1331f6485057SDavid Chinner * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1332f6485057SDavid Chinner * given transaction and start new ones, so make sure everything involved in
1333f6485057SDavid Chinner * the transaction is tidy before calling here. Some transaction will be
1334f6485057SDavid Chinner * returned to the caller to be committed. The incoming transaction must
1335f6485057SDavid Chinner * already include the inode, and both inode locks must be held exclusively.
1336f6485057SDavid Chinner * The inode must also be "held" within the transaction. On return the inode
1337f6485057SDavid Chinner * will be "held" within the returned transaction. This routine does NOT
1338f6485057SDavid Chinner * require any disk space to be reserved for it within the transaction.
13391da177e4SLinus Torvalds *
1340f6485057SDavid Chinner * If we get an error, we must return with the inode locked and linked into the
1341f6485057SDavid Chinner * current transaction. This keeps things simple for the higher level code,
1342f6485057SDavid Chinner * because it always knows that the inode is locked and held in the transaction
1343f6485057SDavid Chinner * that returns to it whether errors occur or not. We don't mark the inode
1344f6485057SDavid Chinner * dirty on error so that transactions can be easily aborted if possible.
13451da177e4SLinus Torvalds */
13461da177e4SLinus Torvalds int
xfs_itruncate_extents_flags(struct xfs_trans ** tpp,struct xfs_inode * ip,int whichfork,xfs_fsize_t new_size,int flags)13474e529339SBrian Foster xfs_itruncate_extents_flags(
13488f04c47aSChristoph Hellwig struct xfs_trans **tpp,
13498f04c47aSChristoph Hellwig struct xfs_inode *ip,
13508f04c47aSChristoph Hellwig int whichfork,
135113b86fc3SBrian Foster xfs_fsize_t new_size,
13524e529339SBrian Foster int flags)
13531da177e4SLinus Torvalds {
13548f04c47aSChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
13558f04c47aSChristoph Hellwig struct xfs_trans *tp = *tpp;
13561da177e4SLinus Torvalds xfs_fileoff_t first_unmap_block;
13578f04c47aSChristoph Hellwig xfs_filblks_t unmap_len;
13588f04c47aSChristoph Hellwig int error = 0;
13591da177e4SLinus Torvalds
13600b56185bSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
13610b56185bSChristoph Hellwig ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
13620b56185bSChristoph Hellwig xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1363ce7ae151SChristoph Hellwig ASSERT(new_size <= XFS_ISIZE(ip));
13648f04c47aSChristoph Hellwig ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
13651da177e4SLinus Torvalds ASSERT(ip->i_itemp != NULL);
1366898621d5SChristoph Hellwig ASSERT(ip->i_itemp->ili_lock_flags == 0);
13671da177e4SLinus Torvalds ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
13681da177e4SLinus Torvalds
1369673e8e59SChristoph Hellwig trace_xfs_itruncate_extents_start(ip, new_size);
1370673e8e59SChristoph Hellwig
13714e529339SBrian Foster flags |= xfs_bmapi_aflag(whichfork);
137213b86fc3SBrian Foster
13731da177e4SLinus Torvalds /*
13741da177e4SLinus Torvalds * Since it is possible for space to become allocated beyond
13751da177e4SLinus Torvalds * the end of the file (in a crash where the space is allocated
13761da177e4SLinus Torvalds * but the inode size is not yet updated), simply remove any
13771da177e4SLinus Torvalds * blocks which show up between the new EOF and the maximum
13784bbb04abSDarrick J. Wong * possible file size.
13794bbb04abSDarrick J. Wong *
13804bbb04abSDarrick J. Wong * We have to free all the blocks to the bmbt maximum offset, even if
13814bbb04abSDarrick J. Wong * the page cache can't scale that far.
13821da177e4SLinus Torvalds */
13838f04c47aSChristoph Hellwig first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
138433005fd0SDarrick J. Wong if (!xfs_verify_fileoff(mp, first_unmap_block)) {
13854bbb04abSDarrick J. Wong WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
13868f04c47aSChristoph Hellwig return 0;
13874bbb04abSDarrick J. Wong }
13888f04c47aSChristoph Hellwig
13894bbb04abSDarrick J. Wong unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
13904bbb04abSDarrick J. Wong while (unmap_len > 0) {
1391692b6cddSDave Chinner ASSERT(tp->t_highest_agno == NULLAGNUMBER);
13924bbb04abSDarrick J. Wong error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
13934bbb04abSDarrick J. Wong flags, XFS_ITRUNC_MAX_EXTENTS);
13948f04c47aSChristoph Hellwig if (error)
1395d5a2e289SBrian Foster goto out;
13961da177e4SLinus Torvalds
13976dd379c7SBrian Foster /* free the just unmapped extents */
13989e28a242SBrian Foster error = xfs_defer_finish(&tp);
13998f04c47aSChristoph Hellwig if (error)
14009b1f4e98SBrian Foster goto out;
14011da177e4SLinus Torvalds }
14028f04c47aSChristoph Hellwig
14034919d42aSDarrick J. Wong if (whichfork == XFS_DATA_FORK) {
1404aa8968f2SDarrick J. Wong /* Remove all pending CoW reservations. */
14054919d42aSDarrick J. Wong error = xfs_reflink_cancel_cow_blocks(ip, &tp,
14064bbb04abSDarrick J. Wong first_unmap_block, XFS_MAX_FILEOFF, true);
1407aa8968f2SDarrick J. Wong if (error)
1408aa8968f2SDarrick J. Wong goto out;
1409aa8968f2SDarrick J. Wong
1410363e59baSDarrick J. Wong xfs_itruncate_clear_reflink_flags(ip);
14114919d42aSDarrick J. Wong }
1412aa8968f2SDarrick J. Wong
1413673e8e59SChristoph Hellwig /*
1414673e8e59SChristoph Hellwig * Always re-log the inode so that our permanent transaction can keep
1415673e8e59SChristoph Hellwig * on rolling it forward in the log.
1416673e8e59SChristoph Hellwig */
1417673e8e59SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1418673e8e59SChristoph Hellwig
1419673e8e59SChristoph Hellwig trace_xfs_itruncate_extents_end(ip, new_size);
1420673e8e59SChristoph Hellwig
14218f04c47aSChristoph Hellwig out:
14228f04c47aSChristoph Hellwig *tpp = tp;
14238f04c47aSChristoph Hellwig return error;
14248f04c47aSChristoph Hellwig }
14258f04c47aSChristoph Hellwig
1426c24b5dfaSDave Chinner int
xfs_release(xfs_inode_t * ip)1427c24b5dfaSDave Chinner xfs_release(
1428c24b5dfaSDave Chinner xfs_inode_t *ip)
1429c24b5dfaSDave Chinner {
1430c24b5dfaSDave Chinner xfs_mount_t *mp = ip->i_mount;
14317d88329eSDarrick J. Wong int error = 0;
1432c24b5dfaSDave Chinner
1433c19b3b05SDave Chinner if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1434c24b5dfaSDave Chinner return 0;
1435c24b5dfaSDave Chinner
1436c24b5dfaSDave Chinner /* If this is a read-only mount, don't do this (would generate I/O) */
14372e973b2cSDave Chinner if (xfs_is_readonly(mp))
1438c24b5dfaSDave Chinner return 0;
1439c24b5dfaSDave Chinner
144075c8c50fSDave Chinner if (!xfs_is_shutdown(mp)) {
1441c24b5dfaSDave Chinner int truncated;
1442c24b5dfaSDave Chinner
1443c24b5dfaSDave Chinner /*
1444c24b5dfaSDave Chinner * If we previously truncated this file and removed old data
1445c24b5dfaSDave Chinner * in the process, we want to initiate "early" writeout on
1446c24b5dfaSDave Chinner * the last close. This is an attempt to combat the notorious
1447c24b5dfaSDave Chinner * NULL files problem which is particularly noticeable from a
1448c24b5dfaSDave Chinner * truncate down, buffered (re-)write (delalloc), followed by
1449c24b5dfaSDave Chinner * a crash. What we are effectively doing here is
1450c24b5dfaSDave Chinner * significantly reducing the time window where we'd otherwise
1451c24b5dfaSDave Chinner * be exposed to that problem.
1452c24b5dfaSDave Chinner */
1453c24b5dfaSDave Chinner truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1454c24b5dfaSDave Chinner if (truncated) {
1455c24b5dfaSDave Chinner xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1456eac152b4SDave Chinner if (ip->i_delayed_blks > 0) {
14572451337dSDave Chinner error = filemap_flush(VFS_I(ip)->i_mapping);
1458c24b5dfaSDave Chinner if (error)
1459c24b5dfaSDave Chinner return error;
1460c24b5dfaSDave Chinner }
1461c24b5dfaSDave Chinner }
1462c24b5dfaSDave Chinner }
1463c24b5dfaSDave Chinner
146454d7b5c1SDave Chinner if (VFS_I(ip)->i_nlink == 0)
1465c24b5dfaSDave Chinner return 0;
1466c24b5dfaSDave Chinner
14677d88329eSDarrick J. Wong /*
14687d88329eSDarrick J. Wong * If we can't get the iolock just skip truncating the blocks past EOF
14697d88329eSDarrick J. Wong * because we could deadlock with the mmap_lock otherwise. We'll get
14707d88329eSDarrick J. Wong * another chance to drop them once the last reference to the inode is
14717d88329eSDarrick J. Wong * dropped, so we'll never leak blocks permanently.
14727d88329eSDarrick J. Wong */
14737d88329eSDarrick J. Wong if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL))
14747d88329eSDarrick J. Wong return 0;
1475c24b5dfaSDave Chinner
14762bc2d49cSChristoph Hellwig if (xfs_can_free_eofblocks(ip)) {
1477c24b5dfaSDave Chinner /*
1478a36b9261SBrian Foster * Check if the inode is being opened, written and closed
1479a36b9261SBrian Foster * frequently and we have delayed allocation blocks outstanding
1480a36b9261SBrian Foster * (e.g. streaming writes from the NFS server), truncating the
1481a36b9261SBrian Foster * blocks past EOF will cause fragmentation to occur.
1482a36b9261SBrian Foster *
1483a36b9261SBrian Foster * In this case don't do the truncation, but we have to be
1484a36b9261SBrian Foster * careful how we detect this case. Blocks beyond EOF show up as
1485a36b9261SBrian Foster * i_delayed_blks even when the inode is clean, so we need to
1486a36b9261SBrian Foster * truncate them away first before checking for a dirty release.
1487a36b9261SBrian Foster * Hence on the first dirty close we will still remove the
1488a36b9261SBrian Foster * speculative allocation, but after that we will leave it in
1489a36b9261SBrian Foster * place.
1490a36b9261SBrian Foster */
1491a36b9261SBrian Foster if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
14927d88329eSDarrick J. Wong goto out_unlock;
14937d88329eSDarrick J. Wong
1494a36b9261SBrian Foster error = xfs_free_eofblocks(ip);
1495a36b9261SBrian Foster if (error)
14967d88329eSDarrick J. Wong goto out_unlock;
1497c24b5dfaSDave Chinner
1498c24b5dfaSDave Chinner /* delalloc blocks after truncation means it really is dirty */
1499c24b5dfaSDave Chinner if (ip->i_delayed_blks)
1500c24b5dfaSDave Chinner xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1501c24b5dfaSDave Chinner }
15027d88329eSDarrick J. Wong
15037d88329eSDarrick J. Wong out_unlock:
15047d88329eSDarrick J. Wong xfs_iunlock(ip, XFS_IOLOCK_EXCL);
15057d88329eSDarrick J. Wong return error;
1506c24b5dfaSDave Chinner }
1507c24b5dfaSDave Chinner
1508c24b5dfaSDave Chinner /*
1509f7be2d7fSBrian Foster * xfs_inactive_truncate
1510f7be2d7fSBrian Foster *
1511f7be2d7fSBrian Foster * Called to perform a truncate when an inode becomes unlinked.
1512f7be2d7fSBrian Foster */
1513f7be2d7fSBrian Foster STATIC int
xfs_inactive_truncate(struct xfs_inode * ip)1514f7be2d7fSBrian Foster xfs_inactive_truncate(
1515f7be2d7fSBrian Foster struct xfs_inode *ip)
1516f7be2d7fSBrian Foster {
1517f7be2d7fSBrian Foster struct xfs_mount *mp = ip->i_mount;
1518f7be2d7fSBrian Foster struct xfs_trans *tp;
1519f7be2d7fSBrian Foster int error;
1520f7be2d7fSBrian Foster
1521253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1522f7be2d7fSBrian Foster if (error) {
152375c8c50fSDave Chinner ASSERT(xfs_is_shutdown(mp));
1524f7be2d7fSBrian Foster return error;
1525f7be2d7fSBrian Foster }
1526f7be2d7fSBrian Foster xfs_ilock(ip, XFS_ILOCK_EXCL);
1527f7be2d7fSBrian Foster xfs_trans_ijoin(tp, ip, 0);
1528f7be2d7fSBrian Foster
1529f7be2d7fSBrian Foster /*
1530f7be2d7fSBrian Foster * Log the inode size first to prevent stale data exposure in the event
1531f7be2d7fSBrian Foster * of a system crash before the truncate completes. See the related
153269bca807SJan Kara * comment in xfs_vn_setattr_size() for details.
1533f7be2d7fSBrian Foster */
153413d2c10bSChristoph Hellwig ip->i_disk_size = 0;
1535f7be2d7fSBrian Foster xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1536f7be2d7fSBrian Foster
1537f7be2d7fSBrian Foster error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1538f7be2d7fSBrian Foster if (error)
1539f7be2d7fSBrian Foster goto error_trans_cancel;
1540f7be2d7fSBrian Foster
1541daf83964SChristoph Hellwig ASSERT(ip->i_df.if_nextents == 0);
1542f7be2d7fSBrian Foster
154370393313SChristoph Hellwig error = xfs_trans_commit(tp);
1544f7be2d7fSBrian Foster if (error)
1545f7be2d7fSBrian Foster goto error_unlock;
1546f7be2d7fSBrian Foster
1547f7be2d7fSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL);
1548f7be2d7fSBrian Foster return 0;
1549f7be2d7fSBrian Foster
1550f7be2d7fSBrian Foster error_trans_cancel:
15514906e215SChristoph Hellwig xfs_trans_cancel(tp);
1552f7be2d7fSBrian Foster error_unlock:
1553f7be2d7fSBrian Foster xfs_iunlock(ip, XFS_ILOCK_EXCL);
1554f7be2d7fSBrian Foster return error;
1555f7be2d7fSBrian Foster }
1556f7be2d7fSBrian Foster
1557f7be2d7fSBrian Foster /*
155888877d2bSBrian Foster * xfs_inactive_ifree()
155988877d2bSBrian Foster *
156088877d2bSBrian Foster * Perform the inode free when an inode is unlinked.
156188877d2bSBrian Foster */
156288877d2bSBrian Foster STATIC int
xfs_inactive_ifree(struct xfs_inode * ip)156388877d2bSBrian Foster xfs_inactive_ifree(
156488877d2bSBrian Foster struct xfs_inode *ip)
156588877d2bSBrian Foster {
156688877d2bSBrian Foster struct xfs_mount *mp = ip->i_mount;
156788877d2bSBrian Foster struct xfs_trans *tp;
156888877d2bSBrian Foster int error;
156988877d2bSBrian Foster
15709d43b180SBrian Foster /*
157176d771b4SChristoph Hellwig * We try to use a per-AG reservation for any block needed by the finobt
157276d771b4SChristoph Hellwig * tree, but as the finobt feature predates the per-AG reservation
157376d771b4SChristoph Hellwig * support a degraded file system might not have enough space for the
157476d771b4SChristoph Hellwig * reservation at mount time. In that case try to dip into the reserved
157576d771b4SChristoph Hellwig * pool and pray.
15769d43b180SBrian Foster *
15779d43b180SBrian Foster * Send a warning if the reservation does happen to fail, as the inode
15789d43b180SBrian Foster * now remains allocated and sits on the unlinked list until the fs is
15799d43b180SBrian Foster * repaired.
15809d43b180SBrian Foster */
1581e1f6ca11SDarrick J. Wong if (unlikely(mp->m_finobt_nores)) {
1582253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
158376d771b4SChristoph Hellwig XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
158476d771b4SChristoph Hellwig &tp);
158576d771b4SChristoph Hellwig } else {
158676d771b4SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
158776d771b4SChristoph Hellwig }
158888877d2bSBrian Foster if (error) {
15892451337dSDave Chinner if (error == -ENOSPC) {
15909d43b180SBrian Foster xfs_warn_ratelimited(mp,
15919d43b180SBrian Foster "Failed to remove inode(s) from unlinked list. "
15929d43b180SBrian Foster "Please free space, unmount and run xfs_repair.");
15939d43b180SBrian Foster } else {
159475c8c50fSDave Chinner ASSERT(xfs_is_shutdown(mp));
15959d43b180SBrian Foster }
159688877d2bSBrian Foster return error;
159788877d2bSBrian Foster }
159888877d2bSBrian Foster
159996355d5aSDave Chinner /*
160096355d5aSDave Chinner * We do not hold the inode locked across the entire rolling transaction
160196355d5aSDave Chinner * here. We only need to hold it for the first transaction that
160296355d5aSDave Chinner * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
160396355d5aSDave Chinner * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
160496355d5aSDave Chinner * here breaks the relationship between cluster buffer invalidation and
160596355d5aSDave Chinner * stale inode invalidation on cluster buffer item journal commit
160696355d5aSDave Chinner * completion, and can result in leaving dirty stale inodes hanging
160796355d5aSDave Chinner * around in memory.
160896355d5aSDave Chinner *
160996355d5aSDave Chinner * We have no need for serialising this inode operation against other
161096355d5aSDave Chinner * operations - we freed the inode and hence reallocation is required
161196355d5aSDave Chinner * and that will serialise on reallocating the space the deferops need
161296355d5aSDave Chinner * to free. Hence we can unlock the inode on the first commit of
161396355d5aSDave Chinner * the transaction rather than roll it right through the deferops. This
161496355d5aSDave Chinner * avoids relogging the XFS_ISTALE inode.
161596355d5aSDave Chinner *
161696355d5aSDave Chinner * We check that xfs_ifree() hasn't grown an internal transaction roll
161796355d5aSDave Chinner * by asserting that the inode is still locked when it returns.
161896355d5aSDave Chinner */
161988877d2bSBrian Foster xfs_ilock(ip, XFS_ILOCK_EXCL);
162096355d5aSDave Chinner xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
162188877d2bSBrian Foster
16220e0417f3SBrian Foster error = xfs_ifree(tp, ip);
162396355d5aSDave Chinner ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
162488877d2bSBrian Foster if (error) {
162588877d2bSBrian Foster /*
162688877d2bSBrian Foster * If we fail to free the inode, shut down. The cancel
162788877d2bSBrian Foster * might do that, we need to make sure. Otherwise the
162888877d2bSBrian Foster * inode might be lost for a long time or forever.
162988877d2bSBrian Foster */
163075c8c50fSDave Chinner if (!xfs_is_shutdown(mp)) {
163188877d2bSBrian Foster xfs_notice(mp, "%s: xfs_ifree returned error %d",
163288877d2bSBrian Foster __func__, error);
163388877d2bSBrian Foster xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
163488877d2bSBrian Foster }
16354906e215SChristoph Hellwig xfs_trans_cancel(tp);
163688877d2bSBrian Foster return error;
163788877d2bSBrian Foster }
163888877d2bSBrian Foster
163988877d2bSBrian Foster /*
164088877d2bSBrian Foster * Credit the quota account(s). The inode is gone.
164188877d2bSBrian Foster */
164288877d2bSBrian Foster xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
164388877d2bSBrian Foster
1644d4d12c02SDave Chinner return xfs_trans_commit(tp);
164588877d2bSBrian Foster }
164688877d2bSBrian Foster
164788877d2bSBrian Foster /*
164862af7d54SDarrick J. Wong * Returns true if we need to update the on-disk metadata before we can free
164962af7d54SDarrick J. Wong * the memory used by this inode. Updates include freeing post-eof
165062af7d54SDarrick J. Wong * preallocations; freeing COW staging extents; and marking the inode free in
165162af7d54SDarrick J. Wong * the inobt if it is on the unlinked list.
165262af7d54SDarrick J. Wong */
165362af7d54SDarrick J. Wong bool
xfs_inode_needs_inactive(struct xfs_inode * ip)165462af7d54SDarrick J. Wong xfs_inode_needs_inactive(
165562af7d54SDarrick J. Wong struct xfs_inode *ip)
165662af7d54SDarrick J. Wong {
165762af7d54SDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
1658732436efSDarrick J. Wong struct xfs_ifork *cow_ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
165962af7d54SDarrick J. Wong
166062af7d54SDarrick J. Wong /*
166162af7d54SDarrick J. Wong * If the inode is already free, then there can be nothing
166262af7d54SDarrick J. Wong * to clean up here.
166362af7d54SDarrick J. Wong */
166462af7d54SDarrick J. Wong if (VFS_I(ip)->i_mode == 0)
166562af7d54SDarrick J. Wong return false;
166662af7d54SDarrick J. Wong
166776e58901SDarrick J. Wong /*
166876e58901SDarrick J. Wong * If this is a read-only mount, don't do this (would generate I/O)
166976e58901SDarrick J. Wong * unless we're in log recovery and cleaning the iunlinked list.
167076e58901SDarrick J. Wong */
167176e58901SDarrick J. Wong if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log))
167262af7d54SDarrick J. Wong return false;
167362af7d54SDarrick J. Wong
167462af7d54SDarrick J. Wong /* If the log isn't running, push inodes straight to reclaim. */
167575c8c50fSDave Chinner if (xfs_is_shutdown(mp) || xfs_has_norecovery(mp))
167662af7d54SDarrick J. Wong return false;
167762af7d54SDarrick J. Wong
167862af7d54SDarrick J. Wong /* Metadata inodes require explicit resource cleanup. */
167962af7d54SDarrick J. Wong if (xfs_is_metadata_inode(ip))
168062af7d54SDarrick J. Wong return false;
168162af7d54SDarrick J. Wong
168262af7d54SDarrick J. Wong /* Want to clean out the cow blocks if there are any. */
168362af7d54SDarrick J. Wong if (cow_ifp && cow_ifp->if_bytes > 0)
168462af7d54SDarrick J. Wong return true;
168562af7d54SDarrick J. Wong
168662af7d54SDarrick J. Wong /* Unlinked files must be freed. */
168762af7d54SDarrick J. Wong if (VFS_I(ip)->i_nlink == 0)
168862af7d54SDarrick J. Wong return true;
168962af7d54SDarrick J. Wong
169062af7d54SDarrick J. Wong /*
169162af7d54SDarrick J. Wong * This file isn't being freed, so check if there are post-eof blocks
16922bc2d49cSChristoph Hellwig * to free.
169362af7d54SDarrick J. Wong *
169462af7d54SDarrick J. Wong * Note: don't bother with iolock here since lockdep complains about
169562af7d54SDarrick J. Wong * acquiring it in reclaim context. We have the only reference to the
169662af7d54SDarrick J. Wong * inode at this point anyways.
169762af7d54SDarrick J. Wong */
16982bc2d49cSChristoph Hellwig return xfs_can_free_eofblocks(ip);
169962af7d54SDarrick J. Wong }
170062af7d54SDarrick J. Wong
170162af7d54SDarrick J. Wong /*
1702c24b5dfaSDave Chinner * xfs_inactive
1703c24b5dfaSDave Chinner *
1704c24b5dfaSDave Chinner * This is called when the vnode reference count for the vnode
1705c24b5dfaSDave Chinner * goes to zero. If the file has been unlinked, then it must
1706c24b5dfaSDave Chinner * now be truncated. Also, we clear all of the read-ahead state
1707c24b5dfaSDave Chinner * kept for the inode here since the file is now closed.
1708c24b5dfaSDave Chinner */
1709d4d12c02SDave Chinner int
xfs_inactive(xfs_inode_t * ip)1710c24b5dfaSDave Chinner xfs_inactive(
1711c24b5dfaSDave Chinner xfs_inode_t *ip)
1712c24b5dfaSDave Chinner {
17133d3c8b52SJie Liu struct xfs_mount *mp;
1714d4d12c02SDave Chinner int error = 0;
1715c24b5dfaSDave Chinner int truncate = 0;
1716c24b5dfaSDave Chinner
1717c24b5dfaSDave Chinner /*
1718c24b5dfaSDave Chinner * If the inode is already free, then there can be nothing
1719c24b5dfaSDave Chinner * to clean up here.
1720c24b5dfaSDave Chinner */
1721c19b3b05SDave Chinner if (VFS_I(ip)->i_mode == 0) {
1722c24b5dfaSDave Chinner ASSERT(ip->i_df.if_broot_bytes == 0);
17233ea06d73SDarrick J. Wong goto out;
1724c24b5dfaSDave Chinner }
1725c24b5dfaSDave Chinner
1726c24b5dfaSDave Chinner mp = ip->i_mount;
172717c12bcdSDarrick J. Wong ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1728c24b5dfaSDave Chinner
172976e58901SDarrick J. Wong /*
173076e58901SDarrick J. Wong * If this is a read-only mount, don't do this (would generate I/O)
173176e58901SDarrick J. Wong * unless we're in log recovery and cleaning the iunlinked list.
173276e58901SDarrick J. Wong */
173376e58901SDarrick J. Wong if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log))
17343ea06d73SDarrick J. Wong goto out;
1735c24b5dfaSDave Chinner
1736383e32b0SDarrick J. Wong /* Metadata inodes require explicit resource cleanup. */
1737383e32b0SDarrick J. Wong if (xfs_is_metadata_inode(ip))
17383ea06d73SDarrick J. Wong goto out;
1739383e32b0SDarrick J. Wong
17406231848cSDarrick J. Wong /* Try to clean out the cow blocks if there are any. */
1741970e92caSWentao Liang if (xfs_inode_has_cow_data(ip)) {
1742970e92caSWentao Liang error = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
1743970e92caSWentao Liang if (error)
1744970e92caSWentao Liang goto out;
1745970e92caSWentao Liang }
17466231848cSDarrick J. Wong
174754d7b5c1SDave Chinner if (VFS_I(ip)->i_nlink != 0) {
1748c24b5dfaSDave Chinner /*
17493b4683c2SBrian Foster * Note: don't bother with iolock here since lockdep complains
17503b4683c2SBrian Foster * about acquiring it in reclaim context. We have the only
17513b4683c2SBrian Foster * reference to the inode at this point anyways.
1752c24b5dfaSDave Chinner */
17532bc2d49cSChristoph Hellwig if (xfs_can_free_eofblocks(ip))
1754d4d12c02SDave Chinner error = xfs_free_eofblocks(ip);
175574564fb4SBrian Foster
17563ea06d73SDarrick J. Wong goto out;
1757c24b5dfaSDave Chinner }
1758c24b5dfaSDave Chinner
1759c19b3b05SDave Chinner if (S_ISREG(VFS_I(ip)->i_mode) &&
176013d2c10bSChristoph Hellwig (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
1761*b887d2feSOjaswin Mujoo xfs_inode_has_filedata(ip)))
1762c24b5dfaSDave Chinner truncate = 1;
1763c24b5dfaSDave Chinner
176449813a21SDarrick J. Wong if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) {
1765537c013bSDarrick J. Wong /*
1766537c013bSDarrick J. Wong * If this inode is being inactivated during a quotacheck and
1767537c013bSDarrick J. Wong * has not yet been scanned by quotacheck, we /must/ remove
1768537c013bSDarrick J. Wong * the dquots from the inode before inactivation changes the
1769537c013bSDarrick J. Wong * block and inode counts. Most probably this is a result of
1770537c013bSDarrick J. Wong * reloading the incore iunlinked list to purge unrecovered
1771537c013bSDarrick J. Wong * unlinked inodes.
1772537c013bSDarrick J. Wong */
177349813a21SDarrick J. Wong xfs_qm_dqdetach(ip);
177449813a21SDarrick J. Wong } else {
1775c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(ip);
1776c24b5dfaSDave Chinner if (error)
17773ea06d73SDarrick J. Wong goto out;
177849813a21SDarrick J. Wong }
1779c24b5dfaSDave Chinner
1780c19b3b05SDave Chinner if (S_ISLNK(VFS_I(ip)->i_mode))
178136b21ddeSBrian Foster error = xfs_inactive_symlink(ip);
1782f7be2d7fSBrian Foster else if (truncate)
1783f7be2d7fSBrian Foster error = xfs_inactive_truncate(ip);
178436b21ddeSBrian Foster if (error)
17853ea06d73SDarrick J. Wong goto out;
1786c24b5dfaSDave Chinner
1787c24b5dfaSDave Chinner /*
1788c24b5dfaSDave Chinner * If there are attributes associated with the file then blow them away
1789c24b5dfaSDave Chinner * now. The code calls a routine that recursively deconstructs the
17906dfe5a04SDave Chinner * attribute fork. If also blows away the in-core attribute fork.
1791c24b5dfaSDave Chinner */
1792932b42c6SDarrick J. Wong if (xfs_inode_has_attr_fork(ip)) {
1793c24b5dfaSDave Chinner error = xfs_attr_inactive(ip);
1794c24b5dfaSDave Chinner if (error)
17953ea06d73SDarrick J. Wong goto out;
1796c24b5dfaSDave Chinner }
1797c24b5dfaSDave Chinner
17987821ea30SChristoph Hellwig ASSERT(ip->i_forkoff == 0);
1799c24b5dfaSDave Chinner
1800c24b5dfaSDave Chinner /*
1801c24b5dfaSDave Chinner * Free the inode.
1802c24b5dfaSDave Chinner */
1803d4d12c02SDave Chinner error = xfs_inactive_ifree(ip);
1804c24b5dfaSDave Chinner
18053ea06d73SDarrick J. Wong out:
1806c24b5dfaSDave Chinner /*
18073ea06d73SDarrick J. Wong * We're done making metadata updates for this inode, so we can release
18083ea06d73SDarrick J. Wong * the attached dquots.
1809c24b5dfaSDave Chinner */
1810c24b5dfaSDave Chinner xfs_qm_dqdetach(ip);
1811d4d12c02SDave Chinner return error;
1812c24b5dfaSDave Chinner }
1813c24b5dfaSDave Chinner
18141da177e4SLinus Torvalds /*
18159b247179SDarrick J. Wong * In-Core Unlinked List Lookups
18169b247179SDarrick J. Wong * =============================
18179b247179SDarrick J. Wong *
18189b247179SDarrick J. Wong * Every inode is supposed to be reachable from some other piece of metadata
18199b247179SDarrick J. Wong * with the exception of the root directory. Inodes with a connection to a
18209b247179SDarrick J. Wong * file descriptor but not linked from anywhere in the on-disk directory tree
18219b247179SDarrick J. Wong * are collectively known as unlinked inodes, though the filesystem itself
18229b247179SDarrick J. Wong * maintains links to these inodes so that on-disk metadata are consistent.
18239b247179SDarrick J. Wong *
18249b247179SDarrick J. Wong * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
18259b247179SDarrick J. Wong * header contains a number of buckets that point to an inode, and each inode
18269b247179SDarrick J. Wong * record has a pointer to the next inode in the hash chain. This
18279b247179SDarrick J. Wong * singly-linked list causes scaling problems in the iunlink remove function
18289b247179SDarrick J. Wong * because we must walk that list to find the inode that points to the inode
18299b247179SDarrick J. Wong * being removed from the unlinked hash bucket list.
18309b247179SDarrick J. Wong *
18312fd26cc0SDave Chinner * Hence we keep an in-memory double linked list to link each inode on an
18322fd26cc0SDave Chinner * unlinked list. Because there are 64 unlinked lists per AGI, keeping pointer
18332fd26cc0SDave Chinner * based lists would require having 64 list heads in the perag, one for each
18342fd26cc0SDave Chinner * list. This is expensive in terms of memory (think millions of AGs) and cache
18352fd26cc0SDave Chinner * misses on lookups. Instead, use the fact that inodes on the unlinked list
18362fd26cc0SDave Chinner * must be referenced at the VFS level to keep them on the list and hence we
18372fd26cc0SDave Chinner * have an existence guarantee for inodes on the unlinked list.
18389b247179SDarrick J. Wong *
18392fd26cc0SDave Chinner * Given we have an existence guarantee, we can use lockless inode cache lookups
18402fd26cc0SDave Chinner * to resolve aginos to xfs inodes. This means we only need 8 bytes per inode
18412fd26cc0SDave Chinner * for the double linked unlinked list, and we don't need any extra locking to
18422fd26cc0SDave Chinner * keep the list safe as all manipulations are done under the AGI buffer lock.
18432fd26cc0SDave Chinner * Keeping the list up to date does not require memory allocation, just finding
18442fd26cc0SDave Chinner * the XFS inode and updating the next/prev unlinked list aginos.
18459b247179SDarrick J. Wong */
18469b247179SDarrick J. Wong
18479b247179SDarrick J. Wong /*
1848a83d5a8bSDave Chinner * Find an inode on the unlinked list. This does not take references to the
1849a83d5a8bSDave Chinner * inode as we have existence guarantees by holding the AGI buffer lock and that
1850a83d5a8bSDave Chinner * only unlinked, referenced inodes can be on the unlinked inode list. If we
1851a83d5a8bSDave Chinner * don't find the inode in cache, then let the caller handle the situation.
18529b247179SDarrick J. Wong */
1853a83d5a8bSDave Chinner static struct xfs_inode *
xfs_iunlink_lookup(struct xfs_perag * pag,xfs_agino_t agino)1854a83d5a8bSDave Chinner xfs_iunlink_lookup(
18559b247179SDarrick J. Wong struct xfs_perag *pag,
18569b247179SDarrick J. Wong xfs_agino_t agino)
18579b247179SDarrick J. Wong {
1858a83d5a8bSDave Chinner struct xfs_inode *ip;
18599b247179SDarrick J. Wong
1860a83d5a8bSDave Chinner rcu_read_lock();
1861a83d5a8bSDave Chinner ip = radix_tree_lookup(&pag->pag_ici_root, agino);
186268b957f6SDarrick J. Wong if (!ip) {
186368b957f6SDarrick J. Wong /* Caller can handle inode not being in memory. */
186468b957f6SDarrick J. Wong rcu_read_unlock();
186568b957f6SDarrick J. Wong return NULL;
186668b957f6SDarrick J. Wong }
18679b247179SDarrick J. Wong
18689b247179SDarrick J. Wong /*
186968b957f6SDarrick J. Wong * Inode in RCU freeing limbo should not happen. Warn about this and
187068b957f6SDarrick J. Wong * let the caller handle the failure.
18719b247179SDarrick J. Wong */
187268b957f6SDarrick J. Wong if (WARN_ON_ONCE(!ip->i_ino)) {
1873a83d5a8bSDave Chinner rcu_read_unlock();
1874a83d5a8bSDave Chinner return NULL;
1875a83d5a8bSDave Chinner }
1876a83d5a8bSDave Chinner ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM));
1877a83d5a8bSDave Chinner rcu_read_unlock();
1878a83d5a8bSDave Chinner return ip;
1879a83d5a8bSDave Chinner }
1880a83d5a8bSDave Chinner
188168b957f6SDarrick J. Wong /*
188268b957f6SDarrick J. Wong * Update the prev pointer of the next agino. Returns -ENOLINK if the inode
188368b957f6SDarrick J. Wong * is not in cache.
188468b957f6SDarrick J. Wong */
18859b247179SDarrick J. Wong static int
xfs_iunlink_update_backref(struct xfs_perag * pag,xfs_agino_t prev_agino,xfs_agino_t next_agino)18862fd26cc0SDave Chinner xfs_iunlink_update_backref(
18879b247179SDarrick J. Wong struct xfs_perag *pag,
18889b247179SDarrick J. Wong xfs_agino_t prev_agino,
18892fd26cc0SDave Chinner xfs_agino_t next_agino)
18909b247179SDarrick J. Wong {
18912fd26cc0SDave Chinner struct xfs_inode *ip;
18929b247179SDarrick J. Wong
18932fd26cc0SDave Chinner /* No update necessary if we are at the end of the list. */
18942fd26cc0SDave Chinner if (next_agino == NULLAGINO)
18959b247179SDarrick J. Wong return 0;
18969b247179SDarrick J. Wong
18972fd26cc0SDave Chinner ip = xfs_iunlink_lookup(pag, next_agino);
18982fd26cc0SDave Chinner if (!ip)
189968b957f6SDarrick J. Wong return -ENOLINK;
190068b957f6SDarrick J. Wong
19012fd26cc0SDave Chinner ip->i_prev_unlinked = prev_agino;
19029b247179SDarrick J. Wong return 0;
19039b247179SDarrick J. Wong }
19049b247179SDarrick J. Wong
19059b247179SDarrick J. Wong /*
19069a4a5118SDarrick J. Wong * Point the AGI unlinked bucket at an inode and log the results. The caller
19079a4a5118SDarrick J. Wong * is responsible for validating the old value.
19089a4a5118SDarrick J. Wong */
19099a4a5118SDarrick J. Wong STATIC int
xfs_iunlink_update_bucket(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_buf * agibp,unsigned int bucket_index,xfs_agino_t new_agino)19109a4a5118SDarrick J. Wong xfs_iunlink_update_bucket(
19119a4a5118SDarrick J. Wong struct xfs_trans *tp,
1912f40aadb2SDave Chinner struct xfs_perag *pag,
19139a4a5118SDarrick J. Wong struct xfs_buf *agibp,
19149a4a5118SDarrick J. Wong unsigned int bucket_index,
19159a4a5118SDarrick J. Wong xfs_agino_t new_agino)
19169a4a5118SDarrick J. Wong {
1917370c782bSChristoph Hellwig struct xfs_agi *agi = agibp->b_addr;
19189a4a5118SDarrick J. Wong xfs_agino_t old_value;
19199a4a5118SDarrick J. Wong int offset;
19209a4a5118SDarrick J. Wong
19212d6ca832SDave Chinner ASSERT(xfs_verify_agino_or_null(pag, new_agino));
19229a4a5118SDarrick J. Wong
19239a4a5118SDarrick J. Wong old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1924f40aadb2SDave Chinner trace_xfs_iunlink_update_bucket(tp->t_mountp, pag->pag_agno, bucket_index,
19259a4a5118SDarrick J. Wong old_value, new_agino);
19269a4a5118SDarrick J. Wong
19279a4a5118SDarrick J. Wong /*
19289a4a5118SDarrick J. Wong * We should never find the head of the list already set to the value
19299a4a5118SDarrick J. Wong * passed in because either we're adding or removing ourselves from the
19309a4a5118SDarrick J. Wong * head of the list.
19319a4a5118SDarrick J. Wong */
1932a5155b87SDarrick J. Wong if (old_value == new_agino) {
19338d57c216SDarrick J. Wong xfs_buf_mark_corrupt(agibp);
19349a4a5118SDarrick J. Wong return -EFSCORRUPTED;
1935a5155b87SDarrick J. Wong }
19369a4a5118SDarrick J. Wong
19379a4a5118SDarrick J. Wong agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
19389a4a5118SDarrick J. Wong offset = offsetof(struct xfs_agi, agi_unlinked) +
19399a4a5118SDarrick J. Wong (sizeof(xfs_agino_t) * bucket_index);
19409a4a5118SDarrick J. Wong xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
19419a4a5118SDarrick J. Wong return 0;
19429a4a5118SDarrick J. Wong }
19439a4a5118SDarrick J. Wong
194468b957f6SDarrick J. Wong /*
194568b957f6SDarrick J. Wong * Load the inode @next_agino into the cache and set its prev_unlinked pointer
194668b957f6SDarrick J. Wong * to @prev_agino. Caller must hold the AGI to synchronize with other changes
194768b957f6SDarrick J. Wong * to the unlinked list.
194868b957f6SDarrick J. Wong */
194968b957f6SDarrick J. Wong STATIC int
xfs_iunlink_reload_next(struct xfs_trans * tp,struct xfs_buf * agibp,xfs_agino_t prev_agino,xfs_agino_t next_agino)195068b957f6SDarrick J. Wong xfs_iunlink_reload_next(
195168b957f6SDarrick J. Wong struct xfs_trans *tp,
195268b957f6SDarrick J. Wong struct xfs_buf *agibp,
195368b957f6SDarrick J. Wong xfs_agino_t prev_agino,
195468b957f6SDarrick J. Wong xfs_agino_t next_agino)
195568b957f6SDarrick J. Wong {
195668b957f6SDarrick J. Wong struct xfs_perag *pag = agibp->b_pag;
195768b957f6SDarrick J. Wong struct xfs_mount *mp = pag->pag_mount;
195868b957f6SDarrick J. Wong struct xfs_inode *next_ip = NULL;
195968b957f6SDarrick J. Wong xfs_ino_t ino;
196068b957f6SDarrick J. Wong int error;
196168b957f6SDarrick J. Wong
196268b957f6SDarrick J. Wong ASSERT(next_agino != NULLAGINO);
196368b957f6SDarrick J. Wong
196468b957f6SDarrick J. Wong #ifdef DEBUG
196568b957f6SDarrick J. Wong rcu_read_lock();
196668b957f6SDarrick J. Wong next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino);
196768b957f6SDarrick J. Wong ASSERT(next_ip == NULL);
196868b957f6SDarrick J. Wong rcu_read_unlock();
196968b957f6SDarrick J. Wong #endif
197068b957f6SDarrick J. Wong
197168b957f6SDarrick J. Wong xfs_info_ratelimited(mp,
197268b957f6SDarrick J. Wong "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating recovery.",
197368b957f6SDarrick J. Wong next_agino, pag->pag_agno);
197468b957f6SDarrick J. Wong
197568b957f6SDarrick J. Wong /*
197668b957f6SDarrick J. Wong * Use an untrusted lookup just to be cautious in case the AGI has been
197768b957f6SDarrick J. Wong * corrupted and now points at a free inode. That shouldn't happen,
197868b957f6SDarrick J. Wong * but we'd rather shut down now since we're already running in a weird
197968b957f6SDarrick J. Wong * situation.
198068b957f6SDarrick J. Wong */
198168b957f6SDarrick J. Wong ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino);
198268b957f6SDarrick J. Wong error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip);
198368b957f6SDarrick J. Wong if (error)
198468b957f6SDarrick J. Wong return error;
198568b957f6SDarrick J. Wong
198668b957f6SDarrick J. Wong /* If this is not an unlinked inode, something is very wrong. */
198768b957f6SDarrick J. Wong if (VFS_I(next_ip)->i_nlink != 0) {
198868b957f6SDarrick J. Wong error = -EFSCORRUPTED;
198968b957f6SDarrick J. Wong goto rele;
199068b957f6SDarrick J. Wong }
199168b957f6SDarrick J. Wong
199268b957f6SDarrick J. Wong next_ip->i_prev_unlinked = prev_agino;
199368b957f6SDarrick J. Wong trace_xfs_iunlink_reload_next(next_ip);
199468b957f6SDarrick J. Wong rele:
199568b957f6SDarrick J. Wong ASSERT(!(VFS_I(next_ip)->i_state & I_DONTCACHE));
199649813a21SDarrick J. Wong if (xfs_is_quotacheck_running(mp) && next_ip)
199749813a21SDarrick J. Wong xfs_iflags_set(next_ip, XFS_IQUOTAUNCHECKED);
199868b957f6SDarrick J. Wong xfs_irele(next_ip);
199968b957f6SDarrick J. Wong return error;
200068b957f6SDarrick J. Wong }
200168b957f6SDarrick J. Wong
2002a4454cd6SDave Chinner static int
xfs_iunlink_insert_inode(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_buf * agibp,struct xfs_inode * ip)2003a4454cd6SDave Chinner xfs_iunlink_insert_inode(
2004f2fc16a3SDarrick J. Wong struct xfs_trans *tp,
2005f40aadb2SDave Chinner struct xfs_perag *pag,
2006a4454cd6SDave Chinner struct xfs_buf *agibp,
2007a4454cd6SDave Chinner struct xfs_inode *ip)
2008f2fc16a3SDarrick J. Wong {
2009f2fc16a3SDarrick J. Wong struct xfs_mount *mp = tp->t_mountp;
2010a4454cd6SDave Chinner struct xfs_agi *agi = agibp->b_addr;
2011a4454cd6SDave Chinner xfs_agino_t next_agino;
2012a4454cd6SDave Chinner xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2013a4454cd6SDave Chinner short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2014f2fc16a3SDarrick J. Wong int error;
2015f2fc16a3SDarrick J. Wong
2016a4454cd6SDave Chinner /*
2017a4454cd6SDave Chinner * Get the index into the agi hash table for the list this inode will
2018a4454cd6SDave Chinner * go on. Make sure the pointer isn't garbage and that this inode
2019a4454cd6SDave Chinner * isn't already on the list.
2020a4454cd6SDave Chinner */
2021a4454cd6SDave Chinner next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2022a4454cd6SDave Chinner if (next_agino == agino ||
2023a4454cd6SDave Chinner !xfs_verify_agino_or_null(pag, next_agino)) {
2024a4454cd6SDave Chinner xfs_buf_mark_corrupt(agibp);
2025a4454cd6SDave Chinner return -EFSCORRUPTED;
2026f2fc16a3SDarrick J. Wong }
2027f2fc16a3SDarrick J. Wong
2028f2fc16a3SDarrick J. Wong /*
20292fd26cc0SDave Chinner * Update the prev pointer in the next inode to point back to this
20302fd26cc0SDave Chinner * inode.
2031f2fc16a3SDarrick J. Wong */
20322fd26cc0SDave Chinner error = xfs_iunlink_update_backref(pag, agino, next_agino);
203368b957f6SDarrick J. Wong if (error == -ENOLINK)
203468b957f6SDarrick J. Wong error = xfs_iunlink_reload_next(tp, agibp, agino, next_agino);
20352fd26cc0SDave Chinner if (error)
20362fd26cc0SDave Chinner return error;
20372fd26cc0SDave Chinner
2038a5155b87SDarrick J. Wong if (next_agino != NULLAGINO) {
2039a4454cd6SDave Chinner /*
2040a4454cd6SDave Chinner * There is already another inode in the bucket, so point this
2041a4454cd6SDave Chinner * inode to the current head of the list.
2042a4454cd6SDave Chinner */
2043062efdb0SDave Chinner error = xfs_iunlink_log_inode(tp, ip, pag, next_agino);
2044a4454cd6SDave Chinner if (error)
2045a4454cd6SDave Chinner return error;
20464fcc94d6SDave Chinner ip->i_next_unlinked = next_agino;
2047f2fc16a3SDarrick J. Wong }
2048f2fc16a3SDarrick J. Wong
2049a4454cd6SDave Chinner /* Point the head of the list to point to this inode. */
2050f12b9668SDarrick J. Wong ip->i_prev_unlinked = NULLAGINO;
2051a4454cd6SDave Chinner return xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index, agino);
2052f2fc16a3SDarrick J. Wong }
2053f2fc16a3SDarrick J. Wong
20549a4a5118SDarrick J. Wong /*
2055c4a6bf7fSDarrick J. Wong * This is called when the inode's link count has gone to 0 or we are creating
2056c4a6bf7fSDarrick J. Wong * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
205754d7b5c1SDave Chinner *
205854d7b5c1SDave Chinner * We place the on-disk inode on a list in the AGI. It will be pulled from this
205954d7b5c1SDave Chinner * list when the inode is freed.
20601da177e4SLinus Torvalds */
206154d7b5c1SDave Chinner STATIC int
xfs_iunlink(struct xfs_trans * tp,struct xfs_inode * ip)20621da177e4SLinus Torvalds xfs_iunlink(
206354d7b5c1SDave Chinner struct xfs_trans *tp,
206454d7b5c1SDave Chinner struct xfs_inode *ip)
20651da177e4SLinus Torvalds {
20665837f625SDarrick J. Wong struct xfs_mount *mp = tp->t_mountp;
2067f40aadb2SDave Chinner struct xfs_perag *pag;
20685837f625SDarrick J. Wong struct xfs_buf *agibp;
20691da177e4SLinus Torvalds int error;
20701da177e4SLinus Torvalds
2071c4a6bf7fSDarrick J. Wong ASSERT(VFS_I(ip)->i_nlink == 0);
2072c19b3b05SDave Chinner ASSERT(VFS_I(ip)->i_mode != 0);
20734664c66cSDarrick J. Wong trace_xfs_iunlink(ip);
20741da177e4SLinus Torvalds
2075f40aadb2SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2076f40aadb2SDave Chinner
20775837f625SDarrick J. Wong /* Get the agi buffer first. It ensures lock ordering on the list. */
207861021debSDave Chinner error = xfs_read_agi(pag, tp, &agibp);
2079859d7182SVlad Apostolov if (error)
2080f40aadb2SDave Chinner goto out;
20815e1be0fbSChristoph Hellwig
2082a4454cd6SDave Chinner error = xfs_iunlink_insert_inode(tp, pag, agibp, ip);
2083f40aadb2SDave Chinner out:
2084f40aadb2SDave Chinner xfs_perag_put(pag);
2085f40aadb2SDave Chinner return error;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds
2088a4454cd6SDave Chinner static int
xfs_iunlink_remove_inode(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_buf * agibp,struct xfs_inode * ip)2089a4454cd6SDave Chinner xfs_iunlink_remove_inode(
209023ffa52cSDarrick J. Wong struct xfs_trans *tp,
2091f40aadb2SDave Chinner struct xfs_perag *pag,
2092a4454cd6SDave Chinner struct xfs_buf *agibp,
20935837f625SDarrick J. Wong struct xfs_inode *ip)
20941da177e4SLinus Torvalds {
20955837f625SDarrick J. Wong struct xfs_mount *mp = tp->t_mountp;
2096a4454cd6SDave Chinner struct xfs_agi *agi = agibp->b_addr;
20975837f625SDarrick J. Wong xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2098b1d2a068SDarrick J. Wong xfs_agino_t head_agino;
20995837f625SDarrick J. Wong short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
21001da177e4SLinus Torvalds int error;
21011da177e4SLinus Torvalds
21024664c66cSDarrick J. Wong trace_xfs_iunlink_remove(ip);
21034664c66cSDarrick J. Wong
21041da177e4SLinus Torvalds /*
210586bfd375SDarrick J. Wong * Get the index into the agi hash table for the list this inode will
210686bfd375SDarrick J. Wong * go on. Make sure the head pointer isn't garbage.
21071da177e4SLinus Torvalds */
2108b1d2a068SDarrick J. Wong head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21092d6ca832SDave Chinner if (!xfs_verify_agino(pag, head_agino)) {
2110d2e73665SDarrick J. Wong XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2111d2e73665SDarrick J. Wong agi, sizeof(*agi));
2112d2e73665SDarrick J. Wong return -EFSCORRUPTED;
2113d2e73665SDarrick J. Wong }
21141da177e4SLinus Torvalds
21151da177e4SLinus Torvalds /*
2116b1d2a068SDarrick J. Wong * Set our inode's next_unlinked pointer to NULL and then return
2117b1d2a068SDarrick J. Wong * the old pointer value so that we can update whatever was previous
2118b1d2a068SDarrick J. Wong * to us in the list to point to whatever was next in the list.
21191da177e4SLinus Torvalds */
2120062efdb0SDave Chinner error = xfs_iunlink_log_inode(tp, ip, pag, NULLAGINO);
2121f2fc16a3SDarrick J. Wong if (error)
21221da177e4SLinus Torvalds return error;
21239a4a5118SDarrick J. Wong
21249b247179SDarrick J. Wong /*
21252fd26cc0SDave Chinner * Update the prev pointer in the next inode to point back to previous
21262fd26cc0SDave Chinner * inode in the chain.
21279b247179SDarrick J. Wong */
21282fd26cc0SDave Chinner error = xfs_iunlink_update_backref(pag, ip->i_prev_unlinked,
21292fd26cc0SDave Chinner ip->i_next_unlinked);
213068b957f6SDarrick J. Wong if (error == -ENOLINK)
213168b957f6SDarrick J. Wong error = xfs_iunlink_reload_next(tp, agibp, ip->i_prev_unlinked,
213268b957f6SDarrick J. Wong ip->i_next_unlinked);
21339b247179SDarrick J. Wong if (error)
213492a00544SGao Xiang return error;
21359b247179SDarrick J. Wong
213692a00544SGao Xiang if (head_agino != agino) {
2137a83d5a8bSDave Chinner struct xfs_inode *prev_ip;
2138f2fc16a3SDarrick J. Wong
21392fd26cc0SDave Chinner prev_ip = xfs_iunlink_lookup(pag, ip->i_prev_unlinked);
21402fd26cc0SDave Chinner if (!prev_ip)
21412fd26cc0SDave Chinner return -EFSCORRUPTED;
2142475ee413SChristoph Hellwig
2143062efdb0SDave Chinner error = xfs_iunlink_log_inode(tp, prev_ip, pag,
21445301f870SDave Chinner ip->i_next_unlinked);
2145a83d5a8bSDave Chinner prev_ip->i_next_unlinked = ip->i_next_unlinked;
21462fd26cc0SDave Chinner } else {
21472fd26cc0SDave Chinner /* Point the head of the list to the next unlinked inode. */
21482fd26cc0SDave Chinner error = xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index,
21492fd26cc0SDave Chinner ip->i_next_unlinked);
21501da177e4SLinus Torvalds }
21519b247179SDarrick J. Wong
2152a83d5a8bSDave Chinner ip->i_next_unlinked = NULLAGINO;
2153f12b9668SDarrick J. Wong ip->i_prev_unlinked = 0;
21542fd26cc0SDave Chinner return error;
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds
21575b3eed75SDave Chinner /*
2158a4454cd6SDave Chinner * Pull the on-disk inode from the AGI unlinked list.
2159a4454cd6SDave Chinner */
2160a4454cd6SDave Chinner STATIC int
xfs_iunlink_remove(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_inode * ip)2161a4454cd6SDave Chinner xfs_iunlink_remove(
2162a4454cd6SDave Chinner struct xfs_trans *tp,
2163a4454cd6SDave Chinner struct xfs_perag *pag,
2164a4454cd6SDave Chinner struct xfs_inode *ip)
2165a4454cd6SDave Chinner {
2166a4454cd6SDave Chinner struct xfs_buf *agibp;
2167a4454cd6SDave Chinner int error;
2168a4454cd6SDave Chinner
2169a4454cd6SDave Chinner trace_xfs_iunlink_remove(ip);
2170a4454cd6SDave Chinner
2171a4454cd6SDave Chinner /* Get the agi buffer first. It ensures lock ordering on the list. */
2172a4454cd6SDave Chinner error = xfs_read_agi(pag, tp, &agibp);
21731da177e4SLinus Torvalds if (error)
21741baaed8fSDave Chinner return error;
21751da177e4SLinus Torvalds
2176a4454cd6SDave Chinner return xfs_iunlink_remove_inode(tp, pag, agibp, ip);
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds
21791da177e4SLinus Torvalds /*
218071e3e356SDave Chinner * Look up the inode number specified and if it is not already marked XFS_ISTALE
218171e3e356SDave Chinner * mark it stale. We should only find clean inodes in this lookup that aren't
218271e3e356SDave Chinner * already stale.
21835806165aSDave Chinner */
218471e3e356SDave Chinner static void
xfs_ifree_mark_inode_stale(struct xfs_perag * pag,struct xfs_inode * free_ip,xfs_ino_t inum)218571e3e356SDave Chinner xfs_ifree_mark_inode_stale(
2186f40aadb2SDave Chinner struct xfs_perag *pag,
21875806165aSDave Chinner struct xfs_inode *free_ip,
2188d9fdd0adSBrian Foster xfs_ino_t inum)
21895806165aSDave Chinner {
2190f40aadb2SDave Chinner struct xfs_mount *mp = pag->pag_mount;
219171e3e356SDave Chinner struct xfs_inode_log_item *iip;
21925806165aSDave Chinner struct xfs_inode *ip;
21935806165aSDave Chinner
21945806165aSDave Chinner retry:
21955806165aSDave Chinner rcu_read_lock();
21965806165aSDave Chinner ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
21975806165aSDave Chinner
21985806165aSDave Chinner /* Inode not in memory, nothing to do */
219971e3e356SDave Chinner if (!ip) {
220071e3e356SDave Chinner rcu_read_unlock();
220171e3e356SDave Chinner return;
220271e3e356SDave Chinner }
22035806165aSDave Chinner
22045806165aSDave Chinner /*
22055806165aSDave Chinner * because this is an RCU protected lookup, we could find a recently
22065806165aSDave Chinner * freed or even reallocated inode during the lookup. We need to check
22075806165aSDave Chinner * under the i_flags_lock for a valid inode here. Skip it if it is not
22085806165aSDave Chinner * valid, the wrong inode or stale.
22095806165aSDave Chinner */
22105806165aSDave Chinner spin_lock(&ip->i_flags_lock);
2211718ecc50SDave Chinner if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
2212718ecc50SDave Chinner goto out_iflags_unlock;
22135806165aSDave Chinner
22145806165aSDave Chinner /*
22155806165aSDave Chinner * Don't try to lock/unlock the current inode, but we _cannot_ skip the
22165806165aSDave Chinner * other inodes that we did not find in the list attached to the buffer
22175806165aSDave Chinner * and are not already marked stale. If we can't lock it, back off and
22185806165aSDave Chinner * retry.
22195806165aSDave Chinner */
22205806165aSDave Chinner if (ip != free_ip) {
22215806165aSDave Chinner if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
222271e3e356SDave Chinner spin_unlock(&ip->i_flags_lock);
22235806165aSDave Chinner rcu_read_unlock();
22245806165aSDave Chinner delay(1);
22255806165aSDave Chinner goto retry;
22265806165aSDave Chinner }
22275806165aSDave Chinner }
222871e3e356SDave Chinner ip->i_flags |= XFS_ISTALE;
22295806165aSDave Chinner
223071e3e356SDave Chinner /*
2231718ecc50SDave Chinner * If the inode is flushing, it is already attached to the buffer. All
223271e3e356SDave Chinner * we needed to do here is mark the inode stale so buffer IO completion
223371e3e356SDave Chinner * will remove it from the AIL.
223471e3e356SDave Chinner */
223571e3e356SDave Chinner iip = ip->i_itemp;
2236718ecc50SDave Chinner if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
223771e3e356SDave Chinner ASSERT(!list_empty(&iip->ili_item.li_bio_list));
223871e3e356SDave Chinner ASSERT(iip->ili_last_fields);
223971e3e356SDave Chinner goto out_iunlock;
224071e3e356SDave Chinner }
22415806165aSDave Chinner
22425806165aSDave Chinner /*
224348d55e2aSDave Chinner * Inodes not attached to the buffer can be released immediately.
224448d55e2aSDave Chinner * Everything else has to go through xfs_iflush_abort() on journal
224548d55e2aSDave Chinner * commit as the flock synchronises removal of the inode from the
224648d55e2aSDave Chinner * cluster buffer against inode reclaim.
22475806165aSDave Chinner */
2248718ecc50SDave Chinner if (!iip || list_empty(&iip->ili_item.li_bio_list))
224971e3e356SDave Chinner goto out_iunlock;
2250718ecc50SDave Chinner
2251718ecc50SDave Chinner __xfs_iflags_set(ip, XFS_IFLUSHING);
2252718ecc50SDave Chinner spin_unlock(&ip->i_flags_lock);
2253718ecc50SDave Chinner rcu_read_unlock();
22545806165aSDave Chinner
225571e3e356SDave Chinner /* we have a dirty inode in memory that has not yet been flushed. */
225671e3e356SDave Chinner spin_lock(&iip->ili_lock);
225771e3e356SDave Chinner iip->ili_last_fields = iip->ili_fields;
225871e3e356SDave Chinner iip->ili_fields = 0;
225971e3e356SDave Chinner iip->ili_fsync_fields = 0;
226071e3e356SDave Chinner spin_unlock(&iip->ili_lock);
226171e3e356SDave Chinner ASSERT(iip->ili_last_fields);
226271e3e356SDave Chinner
2263718ecc50SDave Chinner if (ip != free_ip)
2264718ecc50SDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL);
2265718ecc50SDave Chinner return;
2266718ecc50SDave Chinner
226771e3e356SDave Chinner out_iunlock:
226871e3e356SDave Chinner if (ip != free_ip)
226971e3e356SDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL);
2270718ecc50SDave Chinner out_iflags_unlock:
2271718ecc50SDave Chinner spin_unlock(&ip->i_flags_lock);
2272718ecc50SDave Chinner rcu_read_unlock();
22735806165aSDave Chinner }
22745806165aSDave Chinner
22755806165aSDave Chinner /*
22761da177e4SLinus Torvalds * A big issue when freeing the inode cluster is that we _cannot_ skip any
22771da177e4SLinus Torvalds * inodes that are in memory - they all must be marked stale and attached to
22781da177e4SLinus Torvalds * the cluster buffer.
22791da177e4SLinus Torvalds */
2280f40aadb2SDave Chinner static int
xfs_ifree_cluster(struct xfs_trans * tp,struct xfs_perag * pag,struct xfs_inode * free_ip,struct xfs_icluster * xic)22811da177e4SLinus Torvalds xfs_ifree_cluster(
228271e3e356SDave Chinner struct xfs_trans *tp,
2283f40aadb2SDave Chinner struct xfs_perag *pag,
2284f40aadb2SDave Chinner struct xfs_inode *free_ip,
22851da177e4SLinus Torvalds struct xfs_icluster *xic)
22861da177e4SLinus Torvalds {
228771e3e356SDave Chinner struct xfs_mount *mp = free_ip->i_mount;
228871e3e356SDave Chinner struct xfs_ino_geometry *igeo = M_IGEO(mp);
228971e3e356SDave Chinner struct xfs_buf *bp;
229071e3e356SDave Chinner xfs_daddr_t blkno;
229171e3e356SDave Chinner xfs_ino_t inum = xic->first_ino;
22921da177e4SLinus Torvalds int nbufs;
22931da177e4SLinus Torvalds int i, j;
22941da177e4SLinus Torvalds int ioffset;
2295ce92464cSDarrick J. Wong int error;
22961da177e4SLinus Torvalds
2297ef325959SDarrick J. Wong nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
22981da177e4SLinus Torvalds
2299ef325959SDarrick J. Wong for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
23001da177e4SLinus Torvalds /*
23011da177e4SLinus Torvalds * The allocation bitmap tells us which inodes of the chunk were
23021da177e4SLinus Torvalds * physically allocated. Skip the cluster if an inode falls into
23031da177e4SLinus Torvalds * a sparse region.
23041da177e4SLinus Torvalds */
23051da177e4SLinus Torvalds ioffset = inum - xic->first_ino;
23061da177e4SLinus Torvalds if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
2307ef325959SDarrick J. Wong ASSERT(ioffset % igeo->inodes_per_cluster == 0);
23081da177e4SLinus Torvalds continue;
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds
23111da177e4SLinus Torvalds blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
23121da177e4SLinus Torvalds XFS_INO_TO_AGBNO(mp, inum));
23131da177e4SLinus Torvalds
23141da177e4SLinus Torvalds /*
23151da177e4SLinus Torvalds * We obtain and lock the backing buffer first in the process
2316718ecc50SDave Chinner * here to ensure dirty inodes attached to the buffer remain in
2317718ecc50SDave Chinner * the flushing state while we mark them stale.
2318718ecc50SDave Chinner *
23191da177e4SLinus Torvalds * If we scan the in-memory inodes first, then buffer IO can
23201da177e4SLinus Torvalds * complete before we get a lock on it, and hence we may fail
23211da177e4SLinus Torvalds * to mark all the active inodes on the buffer stale.
23221da177e4SLinus Torvalds */
2323ce92464cSDarrick J. Wong error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2324ef325959SDarrick J. Wong mp->m_bsize * igeo->blocks_per_cluster,
2325ce92464cSDarrick J. Wong XBF_UNMAPPED, &bp);
232671e3e356SDave Chinner if (error)
2327ce92464cSDarrick J. Wong return error;
23281da177e4SLinus Torvalds
23291da177e4SLinus Torvalds /*
23301da177e4SLinus Torvalds * This buffer may not have been correctly initialised as we
23311da177e4SLinus Torvalds * didn't read it from disk. That's not important because we are
23321da177e4SLinus Torvalds * only using to mark the buffer as stale in the log, and to
2333740a427eSDave Chinner * attach stale cached inodes on it.
2334740a427eSDave Chinner *
2335740a427eSDave Chinner * For the inode that triggered the cluster freeing, this
2336740a427eSDave Chinner * attachment may occur in xfs_inode_item_precommit() after we
2337740a427eSDave Chinner * have marked this buffer stale. If this buffer was not in
2338740a427eSDave Chinner * memory before xfs_ifree_cluster() started, it will not be
2339740a427eSDave Chinner * marked XBF_DONE and this will cause problems later in
2340740a427eSDave Chinner * xfs_inode_item_precommit() when we trip over a (stale, !done)
2341740a427eSDave Chinner * buffer to attached to the transaction.
2342740a427eSDave Chinner *
2343740a427eSDave Chinner * Hence we have to mark the buffer as XFS_DONE here. This is
2344740a427eSDave Chinner * safe because we are also marking the buffer as XBF_STALE and
2345740a427eSDave Chinner * XFS_BLI_STALE. That means it will never be dispatched for
2346740a427eSDave Chinner * IO and it won't be unlocked until the cluster freeing has
2347740a427eSDave Chinner * been committed to the journal and the buffer unpinned. If it
2348740a427eSDave Chinner * is written, we want to know about it, and we want it to
2349740a427eSDave Chinner * fail. We can acheive this by adding a write verifier to the
2350740a427eSDave Chinner * buffer.
23511da177e4SLinus Torvalds */
2352740a427eSDave Chinner bp->b_flags |= XBF_DONE;
23531da177e4SLinus Torvalds bp->b_ops = &xfs_inode_buf_ops;
23541da177e4SLinus Torvalds
23551da177e4SLinus Torvalds /*
235671e3e356SDave Chinner * Now we need to set all the cached clean inodes as XFS_ISTALE,
235771e3e356SDave Chinner * too. This requires lookups, and will skip inodes that we've
235871e3e356SDave Chinner * already marked XFS_ISTALE.
23591da177e4SLinus Torvalds */
236071e3e356SDave Chinner for (i = 0; i < igeo->inodes_per_cluster; i++)
2361f40aadb2SDave Chinner xfs_ifree_mark_inode_stale(pag, free_ip, inum + i);
23621da177e4SLinus Torvalds
23631da177e4SLinus Torvalds xfs_trans_stale_inode_buf(tp, bp);
23641da177e4SLinus Torvalds xfs_trans_binval(tp, bp);
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds return 0;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds
23691da177e4SLinus Torvalds /*
23709a5280b3SDave Chinner * This is called to return an inode to the inode free list. The inode should
23719a5280b3SDave Chinner * already be truncated to 0 length and have no pages associated with it. This
23729a5280b3SDave Chinner * routine also assumes that the inode is already a part of the transaction.
23731da177e4SLinus Torvalds *
23749a5280b3SDave Chinner * The on-disk copy of the inode will have been added to the list of unlinked
23759a5280b3SDave Chinner * inodes in the AGI. We need to remove the inode from that list atomically with
23769a5280b3SDave Chinner * respect to freeing it here.
23771da177e4SLinus Torvalds */
23781da177e4SLinus Torvalds int
xfs_ifree(struct xfs_trans * tp,struct xfs_inode * ip)23791da177e4SLinus Torvalds xfs_ifree(
23801da177e4SLinus Torvalds struct xfs_trans *tp,
23811da177e4SLinus Torvalds struct xfs_inode *ip)
23821da177e4SLinus Torvalds {
2383f40aadb2SDave Chinner struct xfs_mount *mp = ip->i_mount;
2384f40aadb2SDave Chinner struct xfs_perag *pag;
23851da177e4SLinus Torvalds struct xfs_icluster xic = { 0 };
23861319ebefSDave Chinner struct xfs_inode_log_item *iip = ip->i_itemp;
2387f40aadb2SDave Chinner int error;
23881da177e4SLinus Torvalds
23891da177e4SLinus Torvalds ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
23901da177e4SLinus Torvalds ASSERT(VFS_I(ip)->i_nlink == 0);
2391daf83964SChristoph Hellwig ASSERT(ip->i_df.if_nextents == 0);
239213d2c10bSChristoph Hellwig ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
23936e73a545SChristoph Hellwig ASSERT(ip->i_nblocks == 0);
23941da177e4SLinus Torvalds
2395f40aadb2SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2396f40aadb2SDave Chinner
23971da177e4SLinus Torvalds /*
23989a5280b3SDave Chinner * Free the inode first so that we guarantee that the AGI lock is going
23999a5280b3SDave Chinner * to be taken before we remove the inode from the unlinked list. This
24009a5280b3SDave Chinner * makes the AGI lock -> unlinked list modification order the same as
24019a5280b3SDave Chinner * used in O_TMPFILE creation.
24021da177e4SLinus Torvalds */
2403f40aadb2SDave Chinner error = xfs_difree(tp, pag, ip->i_ino, &xic);
24041baaed8fSDave Chinner if (error)
24056f5097e3SBrian Foster goto out;
24069a5280b3SDave Chinner
24079a5280b3SDave Chinner error = xfs_iunlink_remove(tp, pag, ip);
24089a5280b3SDave Chinner if (error)
2409f40aadb2SDave Chinner goto out;
24101baaed8fSDave Chinner
2411b2c20045SChristoph Hellwig /*
2412b2c20045SChristoph Hellwig * Free any local-format data sitting around before we reset the
2413b2c20045SChristoph Hellwig * data fork to extents format. Note that the attr fork data has
2414b2c20045SChristoph Hellwig * already been freed by xfs_attr_inactive.
2415b2c20045SChristoph Hellwig */
2416f7e67b20SChristoph Hellwig if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
2417b2c20045SChristoph Hellwig kmem_free(ip->i_df.if_u1.if_data);
2418b2c20045SChristoph Hellwig ip->i_df.if_u1.if_data = NULL;
2419b2c20045SChristoph Hellwig ip->i_df.if_bytes = 0;
2420b2c20045SChristoph Hellwig }
242198c4f78dSDarrick J. Wong
2422c19b3b05SDave Chinner VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
2423db07349dSChristoph Hellwig ip->i_diflags = 0;
2424f40aadb2SDave Chinner ip->i_diflags2 = mp->m_ino_geo.new_diflags2;
24257821ea30SChristoph Hellwig ip->i_forkoff = 0; /* mark the attr fork not in use */
2426f7e67b20SChristoph Hellwig ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
24279b3beb02SChristoph Hellwig if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS))
24289b3beb02SChristoph Hellwig xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS);
2429dc1baa71SEric Sandeen
2430dc1baa71SEric Sandeen /* Don't attempt to replay owner changes for a deleted inode */
24311319ebefSDave Chinner spin_lock(&iip->ili_lock);
24321319ebefSDave Chinner iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
24331319ebefSDave Chinner spin_unlock(&iip->ili_lock);
2434dc1baa71SEric Sandeen
24351da177e4SLinus Torvalds /*
24361da177e4SLinus Torvalds * Bump the generation count so no one will be confused
24371da177e4SLinus Torvalds * by reincarnations of this inode.
24381da177e4SLinus Torvalds */
24399e9a2674SDave Chinner VFS_I(ip)->i_generation++;
24401da177e4SLinus Torvalds xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
24411da177e4SLinus Torvalds
244209b56604SBrian Foster if (xic.deleted)
2443f40aadb2SDave Chinner error = xfs_ifree_cluster(tp, pag, ip, &xic);
2444f40aadb2SDave Chinner out:
2445f40aadb2SDave Chinner xfs_perag_put(pag);
24462a30f36dSChandra Seetharaman return error;
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds
24491da177e4SLinus Torvalds /*
245060ec6783SChristoph Hellwig * This is called to unpin an inode. The caller must have the inode locked
245160ec6783SChristoph Hellwig * in at least shared mode so that the buffer cannot be subsequently pinned
245260ec6783SChristoph Hellwig * once someone is waiting for it to be unpinned.
24531da177e4SLinus Torvalds */
245460ec6783SChristoph Hellwig static void
xfs_iunpin(struct xfs_inode * ip)2455f392e631SChristoph Hellwig xfs_iunpin(
245660ec6783SChristoph Hellwig struct xfs_inode *ip)
2457a3f74ffbSDavid Chinner {
2458579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2459a3f74ffbSDavid Chinner
24604aaf15d1SDave Chinner trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
24614aaf15d1SDave Chinner
2462a3f74ffbSDavid Chinner /* Give the log a push to start the unpinning I/O */
24635f9b4b0dSDave Chinner xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL);
2464a14a348bSChristoph Hellwig
2465a3f74ffbSDavid Chinner }
2466a3f74ffbSDavid Chinner
2467f392e631SChristoph Hellwig static void
__xfs_iunpin_wait(struct xfs_inode * ip)2468f392e631SChristoph Hellwig __xfs_iunpin_wait(
2469f392e631SChristoph Hellwig struct xfs_inode *ip)
2470f392e631SChristoph Hellwig {
2471f392e631SChristoph Hellwig wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2472f392e631SChristoph Hellwig DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2473f392e631SChristoph Hellwig
2474f392e631SChristoph Hellwig xfs_iunpin(ip);
2475f392e631SChristoph Hellwig
2476f392e631SChristoph Hellwig do {
247721417136SIngo Molnar prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2478f392e631SChristoph Hellwig if (xfs_ipincount(ip))
2479f392e631SChristoph Hellwig io_schedule();
2480f392e631SChristoph Hellwig } while (xfs_ipincount(ip));
248121417136SIngo Molnar finish_wait(wq, &wait.wq_entry);
2482f392e631SChristoph Hellwig }
2483f392e631SChristoph Hellwig
2484777df5afSDave Chinner void
xfs_iunpin_wait(struct xfs_inode * ip)24851da177e4SLinus Torvalds xfs_iunpin_wait(
248660ec6783SChristoph Hellwig struct xfs_inode *ip)
24871da177e4SLinus Torvalds {
2488f392e631SChristoph Hellwig if (xfs_ipincount(ip))
2489f392e631SChristoph Hellwig __xfs_iunpin_wait(ip);
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds
249227320369SDave Chinner /*
249327320369SDave Chinner * Removing an inode from the namespace involves removing the directory entry
249427320369SDave Chinner * and dropping the link count on the inode. Removing the directory entry can
249527320369SDave Chinner * result in locking an AGF (directory blocks were freed) and removing a link
249627320369SDave Chinner * count can result in placing the inode on an unlinked list which results in
249727320369SDave Chinner * locking an AGI.
249827320369SDave Chinner *
249927320369SDave Chinner * The big problem here is that we have an ordering constraint on AGF and AGI
250027320369SDave Chinner * locking - inode allocation locks the AGI, then can allocate a new extent for
250127320369SDave Chinner * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
250227320369SDave Chinner * removes the inode from the unlinked list, requiring that we lock the AGI
250327320369SDave Chinner * first, and then freeing the inode can result in an inode chunk being freed
250427320369SDave Chinner * and hence freeing disk space requiring that we lock an AGF.
250527320369SDave Chinner *
250627320369SDave Chinner * Hence the ordering that is imposed by other parts of the code is AGI before
250727320369SDave Chinner * AGF. This means we cannot remove the directory entry before we drop the inode
250827320369SDave Chinner * reference count and put it on the unlinked list as this results in a lock
250927320369SDave Chinner * order of AGF then AGI, and this can deadlock against inode allocation and
251027320369SDave Chinner * freeing. Therefore we must drop the link counts before we remove the
251127320369SDave Chinner * directory entry.
251227320369SDave Chinner *
251327320369SDave Chinner * This is still safe from a transactional point of view - it is not until we
2514310a75a3SDarrick J. Wong * get to xfs_defer_finish() that we have the possibility of multiple
251527320369SDave Chinner * transactions in this operation. Hence as long as we remove the directory
251627320369SDave Chinner * entry and drop the link count in the first transaction of the remove
251727320369SDave Chinner * operation, there are no transactional constraints on the ordering here.
251827320369SDave Chinner */
2519c24b5dfaSDave Chinner int
xfs_remove(xfs_inode_t * dp,struct xfs_name * name,xfs_inode_t * ip)2520c24b5dfaSDave Chinner xfs_remove(
2521c24b5dfaSDave Chinner xfs_inode_t *dp,
2522c24b5dfaSDave Chinner struct xfs_name *name,
2523c24b5dfaSDave Chinner xfs_inode_t *ip)
2524c24b5dfaSDave Chinner {
2525c24b5dfaSDave Chinner xfs_mount_t *mp = dp->i_mount;
2526c24b5dfaSDave Chinner xfs_trans_t *tp = NULL;
2527c19b3b05SDave Chinner int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2528871b9316SDarrick J. Wong int dontcare;
2529c24b5dfaSDave Chinner int error = 0;
2530c24b5dfaSDave Chinner uint resblks;
2531c24b5dfaSDave Chinner
2532c24b5dfaSDave Chinner trace_xfs_remove(dp, name);
2533c24b5dfaSDave Chinner
253475c8c50fSDave Chinner if (xfs_is_shutdown(mp))
25352451337dSDave Chinner return -EIO;
2536c24b5dfaSDave Chinner
2537c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(dp);
2538c24b5dfaSDave Chinner if (error)
2539c24b5dfaSDave Chinner goto std_return;
2540c24b5dfaSDave Chinner
2541c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(ip);
2542c24b5dfaSDave Chinner if (error)
2543c24b5dfaSDave Chinner goto std_return;
2544c24b5dfaSDave Chinner
2545c24b5dfaSDave Chinner /*
2546871b9316SDarrick J. Wong * We try to get the real space reservation first, allowing for
2547871b9316SDarrick J. Wong * directory btree deletion(s) implying possible bmap insert(s). If we
2548871b9316SDarrick J. Wong * can't get the space reservation then we use 0 instead, and avoid the
2549871b9316SDarrick J. Wong * bmap btree insert(s) in the directory code by, if the bmap insert
2550871b9316SDarrick J. Wong * tries to happen, instead trimming the LAST block from the directory.
2551871b9316SDarrick J. Wong *
2552871b9316SDarrick J. Wong * Ignore EDQUOT and ENOSPC being returned via nospace_error because
2553871b9316SDarrick J. Wong * the directory code can handle a reservationless update and we don't
2554871b9316SDarrick J. Wong * want to prevent a user from trying to free space by deleting things.
2555c24b5dfaSDave Chinner */
2556c24b5dfaSDave Chinner resblks = XFS_REMOVE_SPACE_RES(mp);
2557871b9316SDarrick J. Wong error = xfs_trans_alloc_dir(dp, &M_RES(mp)->tr_remove, ip, &resblks,
2558871b9316SDarrick J. Wong &tp, &dontcare);
2559c24b5dfaSDave Chinner if (error) {
25602451337dSDave Chinner ASSERT(error != -ENOSPC);
2561253f4911SChristoph Hellwig goto std_return;
2562c24b5dfaSDave Chinner }
2563c24b5dfaSDave Chinner
2564c24b5dfaSDave Chinner /*
2565c24b5dfaSDave Chinner * If we're removing a directory perform some additional validation.
2566c24b5dfaSDave Chinner */
2567c24b5dfaSDave Chinner if (is_dir) {
256854d7b5c1SDave Chinner ASSERT(VFS_I(ip)->i_nlink >= 2);
256954d7b5c1SDave Chinner if (VFS_I(ip)->i_nlink != 2) {
25702451337dSDave Chinner error = -ENOTEMPTY;
2571c24b5dfaSDave Chinner goto out_trans_cancel;
2572c24b5dfaSDave Chinner }
2573c24b5dfaSDave Chinner if (!xfs_dir_isempty(ip)) {
25742451337dSDave Chinner error = -ENOTEMPTY;
2575c24b5dfaSDave Chinner goto out_trans_cancel;
2576c24b5dfaSDave Chinner }
2577c24b5dfaSDave Chinner
257827320369SDave Chinner /* Drop the link from ip's "..". */
2579c24b5dfaSDave Chinner error = xfs_droplink(tp, dp);
2580c24b5dfaSDave Chinner if (error)
258127320369SDave Chinner goto out_trans_cancel;
2582c24b5dfaSDave Chinner
258327320369SDave Chinner /* Drop the "." link from ip to self. */
2584c24b5dfaSDave Chinner error = xfs_droplink(tp, ip);
2585c24b5dfaSDave Chinner if (error)
258627320369SDave Chinner goto out_trans_cancel;
25875838d035SDarrick J. Wong
25885838d035SDarrick J. Wong /*
25895838d035SDarrick J. Wong * Point the unlinked child directory's ".." entry to the root
25905838d035SDarrick J. Wong * directory to eliminate back-references to inodes that may
25915838d035SDarrick J. Wong * get freed before the child directory is closed. If the fs
25925838d035SDarrick J. Wong * gets shrunk, this can lead to dirent inode validation errors.
25935838d035SDarrick J. Wong */
25945838d035SDarrick J. Wong if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
25955838d035SDarrick J. Wong error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
25965838d035SDarrick J. Wong tp->t_mountp->m_sb.sb_rootino, 0);
25975838d035SDarrick J. Wong if (error)
25982653d533SDarrick J. Wong goto out_trans_cancel;
25995838d035SDarrick J. Wong }
2600c24b5dfaSDave Chinner } else {
2601c24b5dfaSDave Chinner /*
2602c24b5dfaSDave Chinner * When removing a non-directory we need to log the parent
2603c24b5dfaSDave Chinner * inode here. For a directory this is done implicitly
2604c24b5dfaSDave Chinner * by the xfs_droplink call for the ".." entry.
2605c24b5dfaSDave Chinner */
2606c24b5dfaSDave Chinner xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2607c24b5dfaSDave Chinner }
260827320369SDave Chinner xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2609c24b5dfaSDave Chinner
261027320369SDave Chinner /* Drop the link from dp to ip. */
2611c24b5dfaSDave Chinner error = xfs_droplink(tp, ip);
2612c24b5dfaSDave Chinner if (error)
261327320369SDave Chinner goto out_trans_cancel;
2614c24b5dfaSDave Chinner
2615381eee69SBrian Foster error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
261627320369SDave Chinner if (error) {
26172451337dSDave Chinner ASSERT(error != -ENOENT);
2618c8eac49eSBrian Foster goto out_trans_cancel;
261927320369SDave Chinner }
262027320369SDave Chinner
2621c24b5dfaSDave Chinner /*
2622c24b5dfaSDave Chinner * If this is a synchronous mount, make sure that the
2623c24b5dfaSDave Chinner * remove transaction goes to disk before returning to
2624c24b5dfaSDave Chinner * the user.
2625c24b5dfaSDave Chinner */
26260560f31aSDave Chinner if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
2627c24b5dfaSDave Chinner xfs_trans_set_sync(tp);
2628c24b5dfaSDave Chinner
262970393313SChristoph Hellwig error = xfs_trans_commit(tp);
2630c24b5dfaSDave Chinner if (error)
2631c24b5dfaSDave Chinner goto std_return;
2632c24b5dfaSDave Chinner
26332cd2ef6aSChristoph Hellwig if (is_dir && xfs_inode_is_filestream(ip))
2634c24b5dfaSDave Chinner xfs_filestream_deassociate(ip);
2635c24b5dfaSDave Chinner
2636c24b5dfaSDave Chinner return 0;
2637c24b5dfaSDave Chinner
2638c24b5dfaSDave Chinner out_trans_cancel:
26394906e215SChristoph Hellwig xfs_trans_cancel(tp);
2640c24b5dfaSDave Chinner std_return:
2641c24b5dfaSDave Chinner return error;
2642c24b5dfaSDave Chinner }
2643c24b5dfaSDave Chinner
2644f6bba201SDave Chinner /*
2645f6bba201SDave Chinner * Enter all inodes for a rename transaction into a sorted array.
2646f6bba201SDave Chinner */
264795afcf5cSDave Chinner #define __XFS_SORT_INODES 5
2648f6bba201SDave Chinner STATIC void
xfs_sort_for_rename(struct xfs_inode * dp1,struct xfs_inode * dp2,struct xfs_inode * ip1,struct xfs_inode * ip2,struct xfs_inode * wip,struct xfs_inode ** i_tab,int * num_inodes)2649f6bba201SDave Chinner xfs_sort_for_rename(
265095afcf5cSDave Chinner struct xfs_inode *dp1, /* in: old (source) directory inode */
265195afcf5cSDave Chinner struct xfs_inode *dp2, /* in: new (target) directory inode */
265295afcf5cSDave Chinner struct xfs_inode *ip1, /* in: inode of old entry */
265395afcf5cSDave Chinner struct xfs_inode *ip2, /* in: inode of new entry */
265495afcf5cSDave Chinner struct xfs_inode *wip, /* in: whiteout inode */
265595afcf5cSDave Chinner struct xfs_inode **i_tab,/* out: sorted array of inodes */
265695afcf5cSDave Chinner int *num_inodes) /* in/out: inodes in array */
2657f6bba201SDave Chinner {
2658f6bba201SDave Chinner int i, j;
2659f6bba201SDave Chinner
266095afcf5cSDave Chinner ASSERT(*num_inodes == __XFS_SORT_INODES);
266195afcf5cSDave Chinner memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
266295afcf5cSDave Chinner
2663f6bba201SDave Chinner /*
2664f6bba201SDave Chinner * i_tab contains a list of pointers to inodes. We initialize
2665f6bba201SDave Chinner * the table here & we'll sort it. We will then use it to
2666f6bba201SDave Chinner * order the acquisition of the inode locks.
2667f6bba201SDave Chinner *
2668f6bba201SDave Chinner * Note that the table may contain duplicates. e.g., dp1 == dp2.
2669f6bba201SDave Chinner */
267095afcf5cSDave Chinner i = 0;
267195afcf5cSDave Chinner i_tab[i++] = dp1;
267295afcf5cSDave Chinner i_tab[i++] = dp2;
267395afcf5cSDave Chinner i_tab[i++] = ip1;
267495afcf5cSDave Chinner if (ip2)
267595afcf5cSDave Chinner i_tab[i++] = ip2;
267695afcf5cSDave Chinner if (wip)
267795afcf5cSDave Chinner i_tab[i++] = wip;
267895afcf5cSDave Chinner *num_inodes = i;
2679f6bba201SDave Chinner
2680f6bba201SDave Chinner /*
2681f6bba201SDave Chinner * Sort the elements via bubble sort. (Remember, there are at
268295afcf5cSDave Chinner * most 5 elements to sort, so this is adequate.)
2683f6bba201SDave Chinner */
2684f6bba201SDave Chinner for (i = 0; i < *num_inodes; i++) {
2685f6bba201SDave Chinner for (j = 1; j < *num_inodes; j++) {
2686f6bba201SDave Chinner if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
268795afcf5cSDave Chinner struct xfs_inode *temp = i_tab[j];
2688f6bba201SDave Chinner i_tab[j] = i_tab[j-1];
2689f6bba201SDave Chinner i_tab[j-1] = temp;
2690f6bba201SDave Chinner }
2691f6bba201SDave Chinner }
2692f6bba201SDave Chinner }
2693f6bba201SDave Chinner }
2694f6bba201SDave Chinner
2695310606b0SDave Chinner static int
xfs_finish_rename(struct xfs_trans * tp)2696310606b0SDave Chinner xfs_finish_rename(
2697c9cfdb38SBrian Foster struct xfs_trans *tp)
2698310606b0SDave Chinner {
2699310606b0SDave Chinner /*
2700310606b0SDave Chinner * If this is a synchronous mount, make sure that the rename transaction
2701310606b0SDave Chinner * goes to disk before returning to the user.
2702310606b0SDave Chinner */
27030560f31aSDave Chinner if (xfs_has_wsync(tp->t_mountp) || xfs_has_dirsync(tp->t_mountp))
2704310606b0SDave Chinner xfs_trans_set_sync(tp);
2705310606b0SDave Chinner
270670393313SChristoph Hellwig return xfs_trans_commit(tp);
2707310606b0SDave Chinner }
2708310606b0SDave Chinner
2709f6bba201SDave Chinner /*
2710d31a1825SCarlos Maiolino * xfs_cross_rename()
2711d31a1825SCarlos Maiolino *
27120145225eSBhaskar Chowdhury * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall
2713d31a1825SCarlos Maiolino */
2714d31a1825SCarlos Maiolino STATIC int
xfs_cross_rename(struct xfs_trans * tp,struct xfs_inode * dp1,struct xfs_name * name1,struct xfs_inode * ip1,struct xfs_inode * dp2,struct xfs_name * name2,struct xfs_inode * ip2,int spaceres)2715d31a1825SCarlos Maiolino xfs_cross_rename(
2716d31a1825SCarlos Maiolino struct xfs_trans *tp,
2717d31a1825SCarlos Maiolino struct xfs_inode *dp1,
2718d31a1825SCarlos Maiolino struct xfs_name *name1,
2719d31a1825SCarlos Maiolino struct xfs_inode *ip1,
2720d31a1825SCarlos Maiolino struct xfs_inode *dp2,
2721d31a1825SCarlos Maiolino struct xfs_name *name2,
2722d31a1825SCarlos Maiolino struct xfs_inode *ip2,
2723d31a1825SCarlos Maiolino int spaceres)
2724d31a1825SCarlos Maiolino {
2725d31a1825SCarlos Maiolino int error = 0;
2726d31a1825SCarlos Maiolino int ip1_flags = 0;
2727d31a1825SCarlos Maiolino int ip2_flags = 0;
2728d31a1825SCarlos Maiolino int dp2_flags = 0;
2729d31a1825SCarlos Maiolino
2730d31a1825SCarlos Maiolino /* Swap inode number for dirent in first parent */
2731381eee69SBrian Foster error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
2732d31a1825SCarlos Maiolino if (error)
2733eeacd321SDave Chinner goto out_trans_abort;
2734d31a1825SCarlos Maiolino
2735d31a1825SCarlos Maiolino /* Swap inode number for dirent in second parent */
2736381eee69SBrian Foster error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
2737d31a1825SCarlos Maiolino if (error)
2738eeacd321SDave Chinner goto out_trans_abort;
2739d31a1825SCarlos Maiolino
2740d31a1825SCarlos Maiolino /*
2741d31a1825SCarlos Maiolino * If we're renaming one or more directories across different parents,
2742d31a1825SCarlos Maiolino * update the respective ".." entries (and link counts) to match the new
2743d31a1825SCarlos Maiolino * parents.
2744d31a1825SCarlos Maiolino */
2745d31a1825SCarlos Maiolino if (dp1 != dp2) {
2746d31a1825SCarlos Maiolino dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2747d31a1825SCarlos Maiolino
2748c19b3b05SDave Chinner if (S_ISDIR(VFS_I(ip2)->i_mode)) {
2749d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2750381eee69SBrian Foster dp1->i_ino, spaceres);
2751d31a1825SCarlos Maiolino if (error)
2752eeacd321SDave Chinner goto out_trans_abort;
2753d31a1825SCarlos Maiolino
2754d31a1825SCarlos Maiolino /* transfer ip2 ".." reference to dp1 */
2755c19b3b05SDave Chinner if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
2756d31a1825SCarlos Maiolino error = xfs_droplink(tp, dp2);
2757d31a1825SCarlos Maiolino if (error)
2758eeacd321SDave Chinner goto out_trans_abort;
275991083269SEric Sandeen xfs_bumplink(tp, dp1);
2760d31a1825SCarlos Maiolino }
2761d31a1825SCarlos Maiolino
2762d31a1825SCarlos Maiolino /*
2763d31a1825SCarlos Maiolino * Although ip1 isn't changed here, userspace needs
2764d31a1825SCarlos Maiolino * to be warned about the change, so that applications
2765d31a1825SCarlos Maiolino * relying on it (like backup ones), will properly
2766d31a1825SCarlos Maiolino * notify the change
2767d31a1825SCarlos Maiolino */
2768d31a1825SCarlos Maiolino ip1_flags |= XFS_ICHGTIME_CHG;
2769d31a1825SCarlos Maiolino ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2770d31a1825SCarlos Maiolino }
2771d31a1825SCarlos Maiolino
2772c19b3b05SDave Chinner if (S_ISDIR(VFS_I(ip1)->i_mode)) {
2773d31a1825SCarlos Maiolino error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2774381eee69SBrian Foster dp2->i_ino, spaceres);
2775d31a1825SCarlos Maiolino if (error)
2776eeacd321SDave Chinner goto out_trans_abort;
2777d31a1825SCarlos Maiolino
2778d31a1825SCarlos Maiolino /* transfer ip1 ".." reference to dp2 */
2779c19b3b05SDave Chinner if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
2780d31a1825SCarlos Maiolino error = xfs_droplink(tp, dp1);
2781d31a1825SCarlos Maiolino if (error)
2782eeacd321SDave Chinner goto out_trans_abort;
278391083269SEric Sandeen xfs_bumplink(tp, dp2);
2784d31a1825SCarlos Maiolino }
2785d31a1825SCarlos Maiolino
2786d31a1825SCarlos Maiolino /*
2787d31a1825SCarlos Maiolino * Although ip2 isn't changed here, userspace needs
2788d31a1825SCarlos Maiolino * to be warned about the change, so that applications
2789d31a1825SCarlos Maiolino * relying on it (like backup ones), will properly
2790d31a1825SCarlos Maiolino * notify the change
2791d31a1825SCarlos Maiolino */
2792d31a1825SCarlos Maiolino ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2793d31a1825SCarlos Maiolino ip2_flags |= XFS_ICHGTIME_CHG;
2794d31a1825SCarlos Maiolino }
2795d31a1825SCarlos Maiolino }
2796d31a1825SCarlos Maiolino
2797d31a1825SCarlos Maiolino if (ip1_flags) {
2798d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, ip1, ip1_flags);
2799d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2800d31a1825SCarlos Maiolino }
2801d31a1825SCarlos Maiolino if (ip2_flags) {
2802d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, ip2, ip2_flags);
2803d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2804d31a1825SCarlos Maiolino }
2805d31a1825SCarlos Maiolino if (dp2_flags) {
2806d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, dp2, dp2_flags);
2807d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2808d31a1825SCarlos Maiolino }
2809d31a1825SCarlos Maiolino xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2810d31a1825SCarlos Maiolino xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2811c9cfdb38SBrian Foster return xfs_finish_rename(tp);
2812eeacd321SDave Chinner
2813eeacd321SDave Chinner out_trans_abort:
28144906e215SChristoph Hellwig xfs_trans_cancel(tp);
2815d31a1825SCarlos Maiolino return error;
2816d31a1825SCarlos Maiolino }
2817d31a1825SCarlos Maiolino
2818d31a1825SCarlos Maiolino /*
28197dcf5c3eSDave Chinner * xfs_rename_alloc_whiteout()
28207dcf5c3eSDave Chinner *
2821b63da6c8SRandy Dunlap * Return a referenced, unlinked, unlocked inode that can be used as a
28227dcf5c3eSDave Chinner * whiteout in a rename transaction. We use a tmpfile inode here so that if we
28237dcf5c3eSDave Chinner * crash between allocating the inode and linking it into the rename transaction
28247dcf5c3eSDave Chinner * recovery will free the inode and we won't leak it.
28257dcf5c3eSDave Chinner */
28267dcf5c3eSDave Chinner static int
xfs_rename_alloc_whiteout(struct mnt_idmap * idmap,struct xfs_name * src_name,struct xfs_inode * dp,struct xfs_inode ** wip)28277dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
2828f2d40141SChristian Brauner struct mnt_idmap *idmap,
282970b589a3SEric Sandeen struct xfs_name *src_name,
28307dcf5c3eSDave Chinner struct xfs_inode *dp,
28317dcf5c3eSDave Chinner struct xfs_inode **wip)
28327dcf5c3eSDave Chinner {
28337dcf5c3eSDave Chinner struct xfs_inode *tmpfile;
283470b589a3SEric Sandeen struct qstr name;
28357dcf5c3eSDave Chinner int error;
28367dcf5c3eSDave Chinner
2837f2d40141SChristian Brauner error = xfs_create_tmpfile(idmap, dp, S_IFCHR | WHITEOUT_MODE,
2838f736d93dSChristoph Hellwig &tmpfile);
28397dcf5c3eSDave Chinner if (error)
28407dcf5c3eSDave Chinner return error;
28417dcf5c3eSDave Chinner
284270b589a3SEric Sandeen name.name = src_name->name;
284370b589a3SEric Sandeen name.len = src_name->len;
284470b589a3SEric Sandeen error = xfs_inode_init_security(VFS_I(tmpfile), VFS_I(dp), &name);
284570b589a3SEric Sandeen if (error) {
284670b589a3SEric Sandeen xfs_finish_inode_setup(tmpfile);
284770b589a3SEric Sandeen xfs_irele(tmpfile);
284870b589a3SEric Sandeen return error;
284970b589a3SEric Sandeen }
285070b589a3SEric Sandeen
285122419ac9SBrian Foster /*
285222419ac9SBrian Foster * Prepare the tmpfile inode as if it were created through the VFS.
2853c4a6bf7fSDarrick J. Wong * Complete the inode setup and flag it as linkable. nlink is already
2854c4a6bf7fSDarrick J. Wong * zero, so we can skip the drop_nlink.
285522419ac9SBrian Foster */
28562b3d1d41SChristoph Hellwig xfs_setup_iops(tmpfile);
28577dcf5c3eSDave Chinner xfs_finish_inode_setup(tmpfile);
28587dcf5c3eSDave Chinner VFS_I(tmpfile)->i_state |= I_LINKABLE;
28597dcf5c3eSDave Chinner
28607dcf5c3eSDave Chinner *wip = tmpfile;
28617dcf5c3eSDave Chinner return 0;
28627dcf5c3eSDave Chinner }
28637dcf5c3eSDave Chinner
28647dcf5c3eSDave Chinner /*
2865f6bba201SDave Chinner * xfs_rename
2866f6bba201SDave Chinner */
2867f6bba201SDave Chinner int
xfs_rename(struct mnt_idmap * idmap,struct xfs_inode * src_dp,struct xfs_name * src_name,struct xfs_inode * src_ip,struct xfs_inode * target_dp,struct xfs_name * target_name,struct xfs_inode * target_ip,unsigned int flags)2868f6bba201SDave Chinner xfs_rename(
2869f2d40141SChristian Brauner struct mnt_idmap *idmap,
28707dcf5c3eSDave Chinner struct xfs_inode *src_dp,
2871f6bba201SDave Chinner struct xfs_name *src_name,
28727dcf5c3eSDave Chinner struct xfs_inode *src_ip,
28737dcf5c3eSDave Chinner struct xfs_inode *target_dp,
2874f6bba201SDave Chinner struct xfs_name *target_name,
28757dcf5c3eSDave Chinner struct xfs_inode *target_ip,
2876d31a1825SCarlos Maiolino unsigned int flags)
2877f6bba201SDave Chinner {
28787dcf5c3eSDave Chinner struct xfs_mount *mp = src_dp->i_mount;
28797dcf5c3eSDave Chinner struct xfs_trans *tp;
28807dcf5c3eSDave Chinner struct xfs_inode *wip = NULL; /* whiteout inode */
28817dcf5c3eSDave Chinner struct xfs_inode *inodes[__XFS_SORT_INODES];
28826da1b4b1SDarrick J. Wong int i;
288395afcf5cSDave Chinner int num_inodes = __XFS_SORT_INODES;
28842b93681fSDave Chinner bool new_parent = (src_dp != target_dp);
2885c19b3b05SDave Chinner bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
2886f6bba201SDave Chinner int spaceres;
288741667260SDarrick J. Wong bool retried = false;
288841667260SDarrick J. Wong int error, nospace_error = 0;
2889f6bba201SDave Chinner
2890f6bba201SDave Chinner trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2891f6bba201SDave Chinner
2892eeacd321SDave Chinner if ((flags & RENAME_EXCHANGE) && !target_ip)
2893eeacd321SDave Chinner return -EINVAL;
2894f6bba201SDave Chinner
28957dcf5c3eSDave Chinner /*
28967dcf5c3eSDave Chinner * If we are doing a whiteout operation, allocate the whiteout inode
28977dcf5c3eSDave Chinner * we will be placing at the target and ensure the type is set
28987dcf5c3eSDave Chinner * appropriately.
28997dcf5c3eSDave Chinner */
29007dcf5c3eSDave Chinner if (flags & RENAME_WHITEOUT) {
2901f2d40141SChristian Brauner error = xfs_rename_alloc_whiteout(idmap, src_name,
290270b589a3SEric Sandeen target_dp, &wip);
29037dcf5c3eSDave Chinner if (error)
29047dcf5c3eSDave Chinner return error;
2905f6bba201SDave Chinner
29067dcf5c3eSDave Chinner /* setup target dirent info as whiteout */
29077dcf5c3eSDave Chinner src_name->type = XFS_DIR3_FT_CHRDEV;
29087dcf5c3eSDave Chinner }
29097dcf5c3eSDave Chinner
29107dcf5c3eSDave Chinner xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2911f6bba201SDave Chinner inodes, &num_inodes);
2912f6bba201SDave Chinner
291341667260SDarrick J. Wong retry:
291441667260SDarrick J. Wong nospace_error = 0;
2915f6bba201SDave Chinner spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
2916253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
29172451337dSDave Chinner if (error == -ENOSPC) {
291841667260SDarrick J. Wong nospace_error = error;
2919f6bba201SDave Chinner spaceres = 0;
2920253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
2921253f4911SChristoph Hellwig &tp);
2922f6bba201SDave Chinner }
2923445883e8SDave Chinner if (error)
2924253f4911SChristoph Hellwig goto out_release_wip;
2925f6bba201SDave Chinner
2926f6bba201SDave Chinner /*
2927f6bba201SDave Chinner * Attach the dquots to the inodes
2928f6bba201SDave Chinner */
2929f6bba201SDave Chinner error = xfs_qm_vop_rename_dqattach(inodes);
2930445883e8SDave Chinner if (error)
2931445883e8SDave Chinner goto out_trans_cancel;
2932f6bba201SDave Chinner
2933f6bba201SDave Chinner /*
2934f6bba201SDave Chinner * Lock all the participating inodes. Depending upon whether
2935f6bba201SDave Chinner * the target_name exists in the target directory, and
2936f6bba201SDave Chinner * whether the target directory is the same as the source
2937e07ee6feSAllison Henderson * directory, we can lock from 2 to 5 inodes.
2938f6bba201SDave Chinner */
2939f6bba201SDave Chinner xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2940f6bba201SDave Chinner
2941f6bba201SDave Chinner /*
2942f6bba201SDave Chinner * Join all the inodes to the transaction. From this point on,
2943f6bba201SDave Chinner * we can rely on either trans_commit or trans_cancel to unlock
2944f6bba201SDave Chinner * them.
2945f6bba201SDave Chinner */
294665523218SChristoph Hellwig xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2947f6bba201SDave Chinner if (new_parent)
294865523218SChristoph Hellwig xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2949f6bba201SDave Chinner xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2950f6bba201SDave Chinner if (target_ip)
2951f6bba201SDave Chinner xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
29527dcf5c3eSDave Chinner if (wip)
29537dcf5c3eSDave Chinner xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2954f6bba201SDave Chinner
2955f6bba201SDave Chinner /*
2956f6bba201SDave Chinner * If we are using project inheritance, we only allow renames
2957f6bba201SDave Chinner * into our tree when the project IDs are the same; else the
2958f6bba201SDave Chinner * tree quota mechanism would be circumvented.
2959f6bba201SDave Chinner */
2960db07349dSChristoph Hellwig if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
2961ceaf603cSChristoph Hellwig target_dp->i_projid != src_ip->i_projid)) {
29622451337dSDave Chinner error = -EXDEV;
2963445883e8SDave Chinner goto out_trans_cancel;
2964f6bba201SDave Chinner }
2965f6bba201SDave Chinner
2966eeacd321SDave Chinner /* RENAME_EXCHANGE is unique from here on. */
2967eeacd321SDave Chinner if (flags & RENAME_EXCHANGE)
2968eeacd321SDave Chinner return xfs_cross_rename(tp, src_dp, src_name, src_ip,
2969d31a1825SCarlos Maiolino target_dp, target_name, target_ip,
2970f16dea54SBrian Foster spaceres);
2971d31a1825SCarlos Maiolino
2972d31a1825SCarlos Maiolino /*
297341667260SDarrick J. Wong * Try to reserve quota to handle an expansion of the target directory.
297441667260SDarrick J. Wong * We'll allow the rename to continue in reservationless mode if we hit
297541667260SDarrick J. Wong * a space usage constraint. If we trigger reservationless mode, save
297641667260SDarrick J. Wong * the errno if there isn't any free space in the target directory.
297741667260SDarrick J. Wong */
297841667260SDarrick J. Wong if (spaceres != 0) {
297941667260SDarrick J. Wong error = xfs_trans_reserve_quota_nblks(tp, target_dp, spaceres,
298041667260SDarrick J. Wong 0, false);
298141667260SDarrick J. Wong if (error == -EDQUOT || error == -ENOSPC) {
298241667260SDarrick J. Wong if (!retried) {
298341667260SDarrick J. Wong xfs_trans_cancel(tp);
298441667260SDarrick J. Wong xfs_blockgc_free_quota(target_dp, 0);
298541667260SDarrick J. Wong retried = true;
298641667260SDarrick J. Wong goto retry;
298741667260SDarrick J. Wong }
298841667260SDarrick J. Wong
298941667260SDarrick J. Wong nospace_error = error;
299041667260SDarrick J. Wong spaceres = 0;
299141667260SDarrick J. Wong error = 0;
299241667260SDarrick J. Wong }
299341667260SDarrick J. Wong if (error)
299441667260SDarrick J. Wong goto out_trans_cancel;
299541667260SDarrick J. Wong }
299641667260SDarrick J. Wong
299741667260SDarrick J. Wong /*
2998bc56ad8cSkaixuxia * Check for expected errors before we dirty the transaction
2999bc56ad8cSkaixuxia * so we can return an error without a transaction abort.
3000f6bba201SDave Chinner */
3001f6bba201SDave Chinner if (target_ip == NULL) {
3002f6bba201SDave Chinner /*
3003f6bba201SDave Chinner * If there's no space reservation, check the entry will
3004f6bba201SDave Chinner * fit before actually inserting it.
3005f6bba201SDave Chinner */
300694f3cad5SEric Sandeen if (!spaceres) {
300794f3cad5SEric Sandeen error = xfs_dir_canenter(tp, target_dp, target_name);
3008f6bba201SDave Chinner if (error)
3009445883e8SDave Chinner goto out_trans_cancel;
301094f3cad5SEric Sandeen }
3011bc56ad8cSkaixuxia } else {
3012bc56ad8cSkaixuxia /*
3013bc56ad8cSkaixuxia * If target exists and it's a directory, check that whether
3014bc56ad8cSkaixuxia * it can be destroyed.
3015bc56ad8cSkaixuxia */
3016bc56ad8cSkaixuxia if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3017bc56ad8cSkaixuxia (!xfs_dir_isempty(target_ip) ||
3018bc56ad8cSkaixuxia (VFS_I(target_ip)->i_nlink > 2))) {
3019bc56ad8cSkaixuxia error = -EEXIST;
3020bc56ad8cSkaixuxia goto out_trans_cancel;
3021bc56ad8cSkaixuxia }
3022bc56ad8cSkaixuxia }
3023bc56ad8cSkaixuxia
3024bc56ad8cSkaixuxia /*
30256da1b4b1SDarrick J. Wong * Lock the AGI buffers we need to handle bumping the nlink of the
30266da1b4b1SDarrick J. Wong * whiteout inode off the unlinked list and to handle dropping the
30276da1b4b1SDarrick J. Wong * nlink of the target inode. Per locking order rules, do this in
30286da1b4b1SDarrick J. Wong * increasing AG order and before directory block allocation tries to
30296da1b4b1SDarrick J. Wong * grab AGFs because we grab AGIs before AGFs.
30306da1b4b1SDarrick J. Wong *
30316da1b4b1SDarrick J. Wong * The (vfs) caller must ensure that if src is a directory then
30326da1b4b1SDarrick J. Wong * target_ip is either null or an empty directory.
30336da1b4b1SDarrick J. Wong */
30346da1b4b1SDarrick J. Wong for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
30356da1b4b1SDarrick J. Wong if (inodes[i] == wip ||
30366da1b4b1SDarrick J. Wong (inodes[i] == target_ip &&
30376da1b4b1SDarrick J. Wong (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
303861021debSDave Chinner struct xfs_perag *pag;
30396da1b4b1SDarrick J. Wong struct xfs_buf *bp;
30406da1b4b1SDarrick J. Wong
304161021debSDave Chinner pag = xfs_perag_get(mp,
304261021debSDave Chinner XFS_INO_TO_AGNO(mp, inodes[i]->i_ino));
304361021debSDave Chinner error = xfs_read_agi(pag, tp, &bp);
304461021debSDave Chinner xfs_perag_put(pag);
30456da1b4b1SDarrick J. Wong if (error)
30466da1b4b1SDarrick J. Wong goto out_trans_cancel;
30476da1b4b1SDarrick J. Wong }
30486da1b4b1SDarrick J. Wong }
30496da1b4b1SDarrick J. Wong
30506da1b4b1SDarrick J. Wong /*
3051bc56ad8cSkaixuxia * Directory entry creation below may acquire the AGF. Remove
3052bc56ad8cSkaixuxia * the whiteout from the unlinked list first to preserve correct
3053bc56ad8cSkaixuxia * AGI/AGF locking order. This dirties the transaction so failures
3054bc56ad8cSkaixuxia * after this point will abort and log recovery will clean up the
3055bc56ad8cSkaixuxia * mess.
3056bc56ad8cSkaixuxia *
3057bc56ad8cSkaixuxia * For whiteouts, we need to bump the link count on the whiteout
3058bc56ad8cSkaixuxia * inode. After this point, we have a real link, clear the tmpfile
3059bc56ad8cSkaixuxia * state flag from the inode so it doesn't accidentally get misused
3060bc56ad8cSkaixuxia * in future.
3061bc56ad8cSkaixuxia */
3062bc56ad8cSkaixuxia if (wip) {
3063f40aadb2SDave Chinner struct xfs_perag *pag;
3064f40aadb2SDave Chinner
3065bc56ad8cSkaixuxia ASSERT(VFS_I(wip)->i_nlink == 0);
3066f40aadb2SDave Chinner
3067f40aadb2SDave Chinner pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, wip->i_ino));
3068f40aadb2SDave Chinner error = xfs_iunlink_remove(tp, pag, wip);
3069f40aadb2SDave Chinner xfs_perag_put(pag);
3070bc56ad8cSkaixuxia if (error)
3071bc56ad8cSkaixuxia goto out_trans_cancel;
3072bc56ad8cSkaixuxia
3073bc56ad8cSkaixuxia xfs_bumplink(tp, wip);
3074bc56ad8cSkaixuxia VFS_I(wip)->i_state &= ~I_LINKABLE;
3075bc56ad8cSkaixuxia }
3076bc56ad8cSkaixuxia
3077bc56ad8cSkaixuxia /*
3078bc56ad8cSkaixuxia * Set up the target.
3079bc56ad8cSkaixuxia */
3080bc56ad8cSkaixuxia if (target_ip == NULL) {
3081f6bba201SDave Chinner /*
3082f6bba201SDave Chinner * If target does not exist and the rename crosses
3083f6bba201SDave Chinner * directories, adjust the target directory link count
3084f6bba201SDave Chinner * to account for the ".." reference from the new entry.
3085f6bba201SDave Chinner */
3086f6bba201SDave Chinner error = xfs_dir_createname(tp, target_dp, target_name,
3087381eee69SBrian Foster src_ip->i_ino, spaceres);
3088f6bba201SDave Chinner if (error)
3089c8eac49eSBrian Foster goto out_trans_cancel;
3090f6bba201SDave Chinner
3091f6bba201SDave Chinner xfs_trans_ichgtime(tp, target_dp,
3092f6bba201SDave Chinner XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3093f6bba201SDave Chinner
3094f6bba201SDave Chinner if (new_parent && src_is_directory) {
309591083269SEric Sandeen xfs_bumplink(tp, target_dp);
3096f6bba201SDave Chinner }
3097f6bba201SDave Chinner } else { /* target_ip != NULL */
3098f6bba201SDave Chinner /*
3099f6bba201SDave Chinner * Link the source inode under the target name.
3100f6bba201SDave Chinner * If the source inode is a directory and we are moving
3101f6bba201SDave Chinner * it across directories, its ".." entry will be
3102f6bba201SDave Chinner * inconsistent until we replace that down below.
3103f6bba201SDave Chinner *
3104f6bba201SDave Chinner * In case there is already an entry with the same
3105f6bba201SDave Chinner * name at the destination directory, remove it first.
3106f6bba201SDave Chinner */
3107f6bba201SDave Chinner error = xfs_dir_replace(tp, target_dp, target_name,
3108381eee69SBrian Foster src_ip->i_ino, spaceres);
3109f6bba201SDave Chinner if (error)
3110c8eac49eSBrian Foster goto out_trans_cancel;
3111f6bba201SDave Chinner
3112f6bba201SDave Chinner xfs_trans_ichgtime(tp, target_dp,
3113f6bba201SDave Chinner XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3114f6bba201SDave Chinner
3115f6bba201SDave Chinner /*
3116f6bba201SDave Chinner * Decrement the link count on the target since the target
3117f6bba201SDave Chinner * dir no longer points to it.
3118f6bba201SDave Chinner */
3119f6bba201SDave Chinner error = xfs_droplink(tp, target_ip);
3120f6bba201SDave Chinner if (error)
3121c8eac49eSBrian Foster goto out_trans_cancel;
3122f6bba201SDave Chinner
3123f6bba201SDave Chinner if (src_is_directory) {
3124f6bba201SDave Chinner /*
3125f6bba201SDave Chinner * Drop the link from the old "." entry.
3126f6bba201SDave Chinner */
3127f6bba201SDave Chinner error = xfs_droplink(tp, target_ip);
3128f6bba201SDave Chinner if (error)
3129c8eac49eSBrian Foster goto out_trans_cancel;
3130f6bba201SDave Chinner }
3131f6bba201SDave Chinner } /* target_ip != NULL */
3132f6bba201SDave Chinner
3133f6bba201SDave Chinner /*
3134f6bba201SDave Chinner * Remove the source.
3135f6bba201SDave Chinner */
3136f6bba201SDave Chinner if (new_parent && src_is_directory) {
3137f6bba201SDave Chinner /*
3138f6bba201SDave Chinner * Rewrite the ".." entry to point to the new
3139f6bba201SDave Chinner * directory.
3140f6bba201SDave Chinner */
3141f6bba201SDave Chinner error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3142381eee69SBrian Foster target_dp->i_ino, spaceres);
31432451337dSDave Chinner ASSERT(error != -EEXIST);
3144f6bba201SDave Chinner if (error)
3145c8eac49eSBrian Foster goto out_trans_cancel;
3146f6bba201SDave Chinner }
3147f6bba201SDave Chinner
3148f6bba201SDave Chinner /*
3149f6bba201SDave Chinner * We always want to hit the ctime on the source inode.
3150f6bba201SDave Chinner *
3151f6bba201SDave Chinner * This isn't strictly required by the standards since the source
3152f6bba201SDave Chinner * inode isn't really being changed, but old unix file systems did
3153f6bba201SDave Chinner * it and some incremental backup programs won't work without it.
3154f6bba201SDave Chinner */
3155f6bba201SDave Chinner xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3156f6bba201SDave Chinner xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3157f6bba201SDave Chinner
3158f6bba201SDave Chinner /*
3159f6bba201SDave Chinner * Adjust the link count on src_dp. This is necessary when
3160f6bba201SDave Chinner * renaming a directory, either within one parent when
3161f6bba201SDave Chinner * the target existed, or across two parent directories.
3162f6bba201SDave Chinner */
3163f6bba201SDave Chinner if (src_is_directory && (new_parent || target_ip != NULL)) {
3164f6bba201SDave Chinner
3165f6bba201SDave Chinner /*
3166f6bba201SDave Chinner * Decrement link count on src_directory since the
3167f6bba201SDave Chinner * entry that's moved no longer points to it.
3168f6bba201SDave Chinner */
3169f6bba201SDave Chinner error = xfs_droplink(tp, src_dp);
3170f6bba201SDave Chinner if (error)
3171c8eac49eSBrian Foster goto out_trans_cancel;
3172f6bba201SDave Chinner }
3173f6bba201SDave Chinner
31747dcf5c3eSDave Chinner /*
31757dcf5c3eSDave Chinner * For whiteouts, we only need to update the source dirent with the
31767dcf5c3eSDave Chinner * inode number of the whiteout inode rather than removing it
31777dcf5c3eSDave Chinner * altogether.
31787dcf5c3eSDave Chinner */
317983a21c18SChandan Babu R if (wip)
31807dcf5c3eSDave Chinner error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3181381eee69SBrian Foster spaceres);
318283a21c18SChandan Babu R else
3183f6bba201SDave Chinner error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3184381eee69SBrian Foster spaceres);
318502092a2fSChandan Babu R
3186f6bba201SDave Chinner if (error)
3187c8eac49eSBrian Foster goto out_trans_cancel;
3188f6bba201SDave Chinner
3189f6bba201SDave Chinner xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3190f6bba201SDave Chinner xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3191f6bba201SDave Chinner if (new_parent)
3192f6bba201SDave Chinner xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3193f6bba201SDave Chinner
3194c9cfdb38SBrian Foster error = xfs_finish_rename(tp);
31957dcf5c3eSDave Chinner if (wip)
319644a8736bSDarrick J. Wong xfs_irele(wip);
31977dcf5c3eSDave Chinner return error;
3198f6bba201SDave Chinner
3199445883e8SDave Chinner out_trans_cancel:
32004906e215SChristoph Hellwig xfs_trans_cancel(tp);
3201253f4911SChristoph Hellwig out_release_wip:
32027dcf5c3eSDave Chinner if (wip)
320344a8736bSDarrick J. Wong xfs_irele(wip);
320441667260SDarrick J. Wong if (error == -ENOSPC && nospace_error)
320541667260SDarrick J. Wong error = nospace_error;
3206f6bba201SDave Chinner return error;
3207f6bba201SDave Chinner }
3208f6bba201SDave Chinner
3209e6187b34SDave Chinner static int
xfs_iflush(struct xfs_inode * ip,struct xfs_buf * bp)3210e6187b34SDave Chinner xfs_iflush(
321193848a99SChristoph Hellwig struct xfs_inode *ip,
321293848a99SChristoph Hellwig struct xfs_buf *bp)
32131da177e4SLinus Torvalds {
321493848a99SChristoph Hellwig struct xfs_inode_log_item *iip = ip->i_itemp;
321593848a99SChristoph Hellwig struct xfs_dinode *dip;
321693848a99SChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
3217f2019299SBrian Foster int error;
32181da177e4SLinus Torvalds
3219579aa9caSChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3220718ecc50SDave Chinner ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
3221f7e67b20SChristoph Hellwig ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
3222daf83964SChristoph Hellwig ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
322390c60e16SDave Chinner ASSERT(iip->ili_item.li_buf == bp);
32241da177e4SLinus Torvalds
322588ee2df7SChristoph Hellwig dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
32261da177e4SLinus Torvalds
3227f2019299SBrian Foster /*
3228f2019299SBrian Foster * We don't flush the inode if any of the following checks fail, but we
3229f2019299SBrian Foster * do still update the log item and attach to the backing buffer as if
3230f2019299SBrian Foster * the flush happened. This is a formality to facilitate predictable
3231f2019299SBrian Foster * error handling as the caller will shutdown and fail the buffer.
3232f2019299SBrian Foster */
3233f2019299SBrian Foster error = -EFSCORRUPTED;
323469ef921bSChristoph Hellwig if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
32359e24cfd0SDarrick J. Wong mp, XFS_ERRTAG_IFLUSH_1)) {
32366a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
323778b0f58bSZeng Heng "%s: Bad inode %llu magic number 0x%x, ptr "PTR_FMT,
32386a19d939SDave Chinner __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
3239f2019299SBrian Foster goto flush_out;
32401da177e4SLinus Torvalds }
3241c19b3b05SDave Chinner if (S_ISREG(VFS_I(ip)->i_mode)) {
32421da177e4SLinus Torvalds if (XFS_TEST_ERROR(
3243f7e67b20SChristoph Hellwig ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3244f7e67b20SChristoph Hellwig ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
32459e24cfd0SDarrick J. Wong mp, XFS_ERRTAG_IFLUSH_3)) {
32466a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
324778b0f58bSZeng Heng "%s: Bad regular inode %llu, ptr "PTR_FMT,
32486a19d939SDave Chinner __func__, ip->i_ino, ip);
3249f2019299SBrian Foster goto flush_out;
32501da177e4SLinus Torvalds }
3251c19b3b05SDave Chinner } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
32521da177e4SLinus Torvalds if (XFS_TEST_ERROR(
3253f7e67b20SChristoph Hellwig ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3254f7e67b20SChristoph Hellwig ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3255f7e67b20SChristoph Hellwig ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
32569e24cfd0SDarrick J. Wong mp, XFS_ERRTAG_IFLUSH_4)) {
32576a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
325878b0f58bSZeng Heng "%s: Bad directory inode %llu, ptr "PTR_FMT,
32596a19d939SDave Chinner __func__, ip->i_ino, ip);
3260f2019299SBrian Foster goto flush_out;
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds }
32632ed5b09bSDarrick J. Wong if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af) >
32646e73a545SChristoph Hellwig ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
32656a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3266755c38ffSChandan Babu R "%s: detected corrupt incore inode %llu, "
3267755c38ffSChandan Babu R "total extents = %llu nblocks = %lld, ptr "PTR_FMT,
32686a19d939SDave Chinner __func__, ip->i_ino,
32692ed5b09bSDarrick J. Wong ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af),
32706e73a545SChristoph Hellwig ip->i_nblocks, ip);
3271f2019299SBrian Foster goto flush_out;
32721da177e4SLinus Torvalds }
32737821ea30SChristoph Hellwig if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize,
32749e24cfd0SDarrick J. Wong mp, XFS_ERRTAG_IFLUSH_6)) {
32756a19d939SDave Chinner xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
327678b0f58bSZeng Heng "%s: bad inode %llu, forkoff 0x%x, ptr "PTR_FMT,
32777821ea30SChristoph Hellwig __func__, ip->i_ino, ip->i_forkoff, ip);
3278f2019299SBrian Foster goto flush_out;
32791da177e4SLinus Torvalds }
3280e60896d8SDave Chinner
32811da177e4SLinus Torvalds /*
3282965e0a1aSChristoph Hellwig * Inode item log recovery for v2 inodes are dependent on the flushiter
3283965e0a1aSChristoph Hellwig * count for correct sequencing. We bump the flush iteration count so
3284965e0a1aSChristoph Hellwig * we can detect flushes which postdate a log record during recovery.
3285965e0a1aSChristoph Hellwig * This is redundant as we now log every change and hence this can't
3286965e0a1aSChristoph Hellwig * happen but we need to still do it to ensure backwards compatibility
3287965e0a1aSChristoph Hellwig * with old kernels that predate logging all inode changes.
32881da177e4SLinus Torvalds */
328938c26bfdSDave Chinner if (!xfs_has_v3inodes(mp))
3290965e0a1aSChristoph Hellwig ip->i_flushiter++;
32911da177e4SLinus Torvalds
32920f45a1b2SChristoph Hellwig /*
32930f45a1b2SChristoph Hellwig * If there are inline format data / attr forks attached to this inode,
32940f45a1b2SChristoph Hellwig * make sure they are not corrupt.
32950f45a1b2SChristoph Hellwig */
3296f7e67b20SChristoph Hellwig if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
32970f45a1b2SChristoph Hellwig xfs_ifork_verify_local_data(ip))
32980f45a1b2SChristoph Hellwig goto flush_out;
3299932b42c6SDarrick J. Wong if (xfs_inode_has_attr_fork(ip) &&
33002ed5b09bSDarrick J. Wong ip->i_af.if_format == XFS_DINODE_FMT_LOCAL &&
33010f45a1b2SChristoph Hellwig xfs_ifork_verify_local_attr(ip))
3302f2019299SBrian Foster goto flush_out;
3303005c5db8SDarrick J. Wong
33041da177e4SLinus Torvalds /*
33053987848cSDave Chinner * Copy the dirty parts of the inode into the on-disk inode. We always
33063987848cSDave Chinner * copy out the core of the inode, because if the inode is dirty at all
33073987848cSDave Chinner * the core must be.
33081da177e4SLinus Torvalds */
330993f958f9SDave Chinner xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
33101da177e4SLinus Torvalds
33111da177e4SLinus Torvalds /* Wrap, we never let the log put out DI_MAX_FLUSH */
331238c26bfdSDave Chinner if (!xfs_has_v3inodes(mp)) {
3313965e0a1aSChristoph Hellwig if (ip->i_flushiter == DI_MAX_FLUSH)
3314965e0a1aSChristoph Hellwig ip->i_flushiter = 0;
3315ee7b83fdSChristoph Hellwig }
33161da177e4SLinus Torvalds
3317005c5db8SDarrick J. Wong xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3318932b42c6SDarrick J. Wong if (xfs_inode_has_attr_fork(ip))
3319005c5db8SDarrick J. Wong xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
33201da177e4SLinus Torvalds
33211da177e4SLinus Torvalds /*
3322f5d8d5c4SChristoph Hellwig * We've recorded everything logged in the inode, so we'd like to clear
3323f5d8d5c4SChristoph Hellwig * the ili_fields bits so we don't log and flush things unnecessarily.
3324f5d8d5c4SChristoph Hellwig * However, we can't stop logging all this information until the data
3325f5d8d5c4SChristoph Hellwig * we've copied into the disk buffer is written to disk. If we did we
3326f5d8d5c4SChristoph Hellwig * might overwrite the copy of the inode in the log with all the data
3327f5d8d5c4SChristoph Hellwig * after re-logging only part of it, and in the face of a crash we
3328f5d8d5c4SChristoph Hellwig * wouldn't have all the data we need to recover.
33291da177e4SLinus Torvalds *
3330f5d8d5c4SChristoph Hellwig * What we do is move the bits to the ili_last_fields field. When
3331f5d8d5c4SChristoph Hellwig * logging the inode, these bits are moved back to the ili_fields field.
3332664ffb8aSChristoph Hellwig * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
3333664ffb8aSChristoph Hellwig * we know that the information those bits represent is permanently on
3334f5d8d5c4SChristoph Hellwig * disk. As long as the flush completes before the inode is logged
3335f5d8d5c4SChristoph Hellwig * again, then both ili_fields and ili_last_fields will be cleared.
33361da177e4SLinus Torvalds */
3337f2019299SBrian Foster error = 0;
3338f2019299SBrian Foster flush_out:
33391319ebefSDave Chinner spin_lock(&iip->ili_lock);
3340f5d8d5c4SChristoph Hellwig iip->ili_last_fields = iip->ili_fields;
3341f5d8d5c4SChristoph Hellwig iip->ili_fields = 0;
3342fc0561ceSDave Chinner iip->ili_fsync_fields = 0;
33431319ebefSDave Chinner spin_unlock(&iip->ili_lock);
33441da177e4SLinus Torvalds
33451319ebefSDave Chinner /*
33461319ebefSDave Chinner * Store the current LSN of the inode so that we can tell whether the
3347664ffb8aSChristoph Hellwig * item has moved in the AIL from xfs_buf_inode_iodone().
33481319ebefSDave Chinner */
33497b2e2a31SDavid Chinner xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
33507b2e2a31SDavid Chinner &iip->ili_item.li_lsn);
33511da177e4SLinus Torvalds
335293848a99SChristoph Hellwig /* generate the checksum. */
335393848a99SChristoph Hellwig xfs_dinode_calc_crc(mp, dip);
3354f2019299SBrian Foster return error;
33551da177e4SLinus Torvalds }
335644a8736bSDarrick J. Wong
3357e6187b34SDave Chinner /*
3358e6187b34SDave Chinner * Non-blocking flush of dirty inode metadata into the backing buffer.
3359e6187b34SDave Chinner *
3360e6187b34SDave Chinner * The caller must have a reference to the inode and hold the cluster buffer
3361e6187b34SDave Chinner * locked. The function will walk across all the inodes on the cluster buffer it
3362e6187b34SDave Chinner * can find and lock without blocking, and flush them to the cluster buffer.
3363e6187b34SDave Chinner *
33645717ea4dSDave Chinner * On successful flushing of at least one inode, the caller must write out the
33655717ea4dSDave Chinner * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
33665717ea4dSDave Chinner * the caller needs to release the buffer. On failure, the filesystem will be
33675717ea4dSDave Chinner * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
33685717ea4dSDave Chinner * will be returned.
3369e6187b34SDave Chinner */
3370e6187b34SDave Chinner int
xfs_iflush_cluster(struct xfs_buf * bp)3371e6187b34SDave Chinner xfs_iflush_cluster(
3372e6187b34SDave Chinner struct xfs_buf *bp)
3373e6187b34SDave Chinner {
33745717ea4dSDave Chinner struct xfs_mount *mp = bp->b_mount;
33755717ea4dSDave Chinner struct xfs_log_item *lip, *n;
33765717ea4dSDave Chinner struct xfs_inode *ip;
33775717ea4dSDave Chinner struct xfs_inode_log_item *iip;
3378e6187b34SDave Chinner int clcount = 0;
33795717ea4dSDave Chinner int error = 0;
3380e6187b34SDave Chinner
3381e6187b34SDave Chinner /*
33825717ea4dSDave Chinner * We must use the safe variant here as on shutdown xfs_iflush_abort()
3383d2d7c047SDave Chinner * will remove itself from the list.
3384e6187b34SDave Chinner */
33855717ea4dSDave Chinner list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
33865717ea4dSDave Chinner iip = (struct xfs_inode_log_item *)lip;
33875717ea4dSDave Chinner ip = iip->ili_inode;
33885717ea4dSDave Chinner
33895717ea4dSDave Chinner /*
33905717ea4dSDave Chinner * Quick and dirty check to avoid locks if possible.
33915717ea4dSDave Chinner */
3392718ecc50SDave Chinner if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
33935717ea4dSDave Chinner continue;
33945717ea4dSDave Chinner if (xfs_ipincount(ip))
33955717ea4dSDave Chinner continue;
33965717ea4dSDave Chinner
33975717ea4dSDave Chinner /*
33985717ea4dSDave Chinner * The inode is still attached to the buffer, which means it is
33995717ea4dSDave Chinner * dirty but reclaim might try to grab it. Check carefully for
34005717ea4dSDave Chinner * that, and grab the ilock while still holding the i_flags_lock
34015717ea4dSDave Chinner * to guarantee reclaim will not be able to reclaim this inode
34025717ea4dSDave Chinner * once we drop the i_flags_lock.
34035717ea4dSDave Chinner */
34045717ea4dSDave Chinner spin_lock(&ip->i_flags_lock);
34055717ea4dSDave Chinner ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
3406718ecc50SDave Chinner if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
34075717ea4dSDave Chinner spin_unlock(&ip->i_flags_lock);
3408e6187b34SDave Chinner continue;
3409e6187b34SDave Chinner }
3410e6187b34SDave Chinner
3411e6187b34SDave Chinner /*
34125717ea4dSDave Chinner * ILOCK will pin the inode against reclaim and prevent
34135717ea4dSDave Chinner * concurrent transactions modifying the inode while we are
3414718ecc50SDave Chinner * flushing the inode. If we get the lock, set the flushing
3415718ecc50SDave Chinner * state before we drop the i_flags_lock.
3416e6187b34SDave Chinner */
34175717ea4dSDave Chinner if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
34185717ea4dSDave Chinner spin_unlock(&ip->i_flags_lock);
34195717ea4dSDave Chinner continue;
34205717ea4dSDave Chinner }
3421718ecc50SDave Chinner __xfs_iflags_set(ip, XFS_IFLUSHING);
34225717ea4dSDave Chinner spin_unlock(&ip->i_flags_lock);
34235717ea4dSDave Chinner
34245717ea4dSDave Chinner /*
34255717ea4dSDave Chinner * Abort flushing this inode if we are shut down because the
34265717ea4dSDave Chinner * inode may not currently be in the AIL. This can occur when
34275717ea4dSDave Chinner * log I/O failure unpins the inode without inserting into the
34285717ea4dSDave Chinner * AIL, leaving a dirty/unpinned inode attached to the buffer
34295717ea4dSDave Chinner * that otherwise looks like it should be flushed.
34305717ea4dSDave Chinner */
343101728b44SDave Chinner if (xlog_is_shutdown(mp->m_log)) {
34325717ea4dSDave Chinner xfs_iunpin_wait(ip);
34335717ea4dSDave Chinner xfs_iflush_abort(ip);
34345717ea4dSDave Chinner xfs_iunlock(ip, XFS_ILOCK_SHARED);
34355717ea4dSDave Chinner error = -EIO;
34365717ea4dSDave Chinner continue;
34375717ea4dSDave Chinner }
34385717ea4dSDave Chinner
34395717ea4dSDave Chinner /* don't block waiting on a log force to unpin dirty inodes */
34405717ea4dSDave Chinner if (xfs_ipincount(ip)) {
3441718ecc50SDave Chinner xfs_iflags_clear(ip, XFS_IFLUSHING);
34425717ea4dSDave Chinner xfs_iunlock(ip, XFS_ILOCK_SHARED);
34435717ea4dSDave Chinner continue;
34445717ea4dSDave Chinner }
34455717ea4dSDave Chinner
34465717ea4dSDave Chinner if (!xfs_inode_clean(ip))
34475717ea4dSDave Chinner error = xfs_iflush(ip, bp);
34485717ea4dSDave Chinner else
3449718ecc50SDave Chinner xfs_iflags_clear(ip, XFS_IFLUSHING);
34505717ea4dSDave Chinner xfs_iunlock(ip, XFS_ILOCK_SHARED);
34515717ea4dSDave Chinner if (error)
3452e6187b34SDave Chinner break;
3453e6187b34SDave Chinner clcount++;
3454e6187b34SDave Chinner }
3455e6187b34SDave Chinner
3456e6187b34SDave Chinner if (error) {
345701728b44SDave Chinner /*
345801728b44SDave Chinner * Shutdown first so we kill the log before we release this
345901728b44SDave Chinner * buffer. If it is an INODE_ALLOC buffer and pins the tail
346001728b44SDave Chinner * of the log, failing it before the _log_ is shut down can
346101728b44SDave Chinner * result in the log tail being moved forward in the journal
346201728b44SDave Chinner * on disk because log writes can still be taking place. Hence
346301728b44SDave Chinner * unpinning the tail will allow the ICREATE intent to be
346401728b44SDave Chinner * removed from the log an recovery will fail with uninitialised
346501728b44SDave Chinner * inode cluster buffers.
346601728b44SDave Chinner */
346701728b44SDave Chinner xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3468e6187b34SDave Chinner bp->b_flags |= XBF_ASYNC;
3469e6187b34SDave Chinner xfs_buf_ioend_fail(bp);
3470e6187b34SDave Chinner return error;
3471e6187b34SDave Chinner }
3472e6187b34SDave Chinner
34735717ea4dSDave Chinner if (!clcount)
34745717ea4dSDave Chinner return -EAGAIN;
34755717ea4dSDave Chinner
34765717ea4dSDave Chinner XFS_STATS_INC(mp, xs_icluster_flushcnt);
34775717ea4dSDave Chinner XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
34785717ea4dSDave Chinner return 0;
34795717ea4dSDave Chinner
34805717ea4dSDave Chinner }
34815717ea4dSDave Chinner
348244a8736bSDarrick J. Wong /* Release an inode. */
348344a8736bSDarrick J. Wong void
xfs_irele(struct xfs_inode * ip)348444a8736bSDarrick J. Wong xfs_irele(
348544a8736bSDarrick J. Wong struct xfs_inode *ip)
348644a8736bSDarrick J. Wong {
348744a8736bSDarrick J. Wong trace_xfs_irele(ip, _RET_IP_);
348844a8736bSDarrick J. Wong iput(VFS_I(ip));
348944a8736bSDarrick J. Wong }
349054fbdd10SChristoph Hellwig
349154fbdd10SChristoph Hellwig /*
349254fbdd10SChristoph Hellwig * Ensure all commited transactions touching the inode are written to the log.
349354fbdd10SChristoph Hellwig */
349454fbdd10SChristoph Hellwig int
xfs_log_force_inode(struct xfs_inode * ip)349554fbdd10SChristoph Hellwig xfs_log_force_inode(
349654fbdd10SChristoph Hellwig struct xfs_inode *ip)
349754fbdd10SChristoph Hellwig {
34985f9b4b0dSDave Chinner xfs_csn_t seq = 0;
349954fbdd10SChristoph Hellwig
350054fbdd10SChristoph Hellwig xfs_ilock(ip, XFS_ILOCK_SHARED);
350154fbdd10SChristoph Hellwig if (xfs_ipincount(ip))
35025f9b4b0dSDave Chinner seq = ip->i_itemp->ili_commit_seq;
350354fbdd10SChristoph Hellwig xfs_iunlock(ip, XFS_ILOCK_SHARED);
350454fbdd10SChristoph Hellwig
35055f9b4b0dSDave Chinner if (!seq)
350654fbdd10SChristoph Hellwig return 0;
35075f9b4b0dSDave Chinner return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL);
350854fbdd10SChristoph Hellwig }
3509e2aaee9cSDarrick J. Wong
3510e2aaee9cSDarrick J. Wong /*
3511e2aaee9cSDarrick J. Wong * Grab the exclusive iolock for a data copy from src to dest, making sure to
3512e2aaee9cSDarrick J. Wong * abide vfs locking order (lowest pointer value goes first) and breaking the
3513e2aaee9cSDarrick J. Wong * layout leases before proceeding. The loop is needed because we cannot call
3514e2aaee9cSDarrick J. Wong * the blocking break_layout() with the iolocks held, and therefore have to
3515e2aaee9cSDarrick J. Wong * back out both locks.
3516e2aaee9cSDarrick J. Wong */
3517e2aaee9cSDarrick J. Wong static int
xfs_iolock_two_inodes_and_break_layout(struct inode * src,struct inode * dest)3518e2aaee9cSDarrick J. Wong xfs_iolock_two_inodes_and_break_layout(
3519e2aaee9cSDarrick J. Wong struct inode *src,
3520e2aaee9cSDarrick J. Wong struct inode *dest)
3521e2aaee9cSDarrick J. Wong {
3522e2aaee9cSDarrick J. Wong int error;
3523e2aaee9cSDarrick J. Wong
3524e2aaee9cSDarrick J. Wong if (src > dest)
3525e2aaee9cSDarrick J. Wong swap(src, dest);
3526e2aaee9cSDarrick J. Wong
3527e2aaee9cSDarrick J. Wong retry:
3528e2aaee9cSDarrick J. Wong /* Wait to break both inodes' layouts before we start locking. */
3529e2aaee9cSDarrick J. Wong error = break_layout(src, true);
3530e2aaee9cSDarrick J. Wong if (error)
3531e2aaee9cSDarrick J. Wong return error;
3532e2aaee9cSDarrick J. Wong if (src != dest) {
3533e2aaee9cSDarrick J. Wong error = break_layout(dest, true);
3534e2aaee9cSDarrick J. Wong if (error)
3535e2aaee9cSDarrick J. Wong return error;
3536e2aaee9cSDarrick J. Wong }
3537e2aaee9cSDarrick J. Wong
3538e2aaee9cSDarrick J. Wong /* Lock one inode and make sure nobody got in and leased it. */
3539e2aaee9cSDarrick J. Wong inode_lock(src);
3540e2aaee9cSDarrick J. Wong error = break_layout(src, false);
3541e2aaee9cSDarrick J. Wong if (error) {
3542e2aaee9cSDarrick J. Wong inode_unlock(src);
3543e2aaee9cSDarrick J. Wong if (error == -EWOULDBLOCK)
3544e2aaee9cSDarrick J. Wong goto retry;
3545e2aaee9cSDarrick J. Wong return error;
3546e2aaee9cSDarrick J. Wong }
3547e2aaee9cSDarrick J. Wong
3548e2aaee9cSDarrick J. Wong if (src == dest)
3549e2aaee9cSDarrick J. Wong return 0;
3550e2aaee9cSDarrick J. Wong
3551e2aaee9cSDarrick J. Wong /* Lock the other inode and make sure nobody got in and leased it. */
3552e2aaee9cSDarrick J. Wong inode_lock_nested(dest, I_MUTEX_NONDIR2);
3553e2aaee9cSDarrick J. Wong error = break_layout(dest, false);
3554e2aaee9cSDarrick J. Wong if (error) {
3555e2aaee9cSDarrick J. Wong inode_unlock(src);
3556e2aaee9cSDarrick J. Wong inode_unlock(dest);
3557e2aaee9cSDarrick J. Wong if (error == -EWOULDBLOCK)
3558e2aaee9cSDarrick J. Wong goto retry;
3559e2aaee9cSDarrick J. Wong return error;
3560e2aaee9cSDarrick J. Wong }
3561e2aaee9cSDarrick J. Wong
3562e2aaee9cSDarrick J. Wong return 0;
3563e2aaee9cSDarrick J. Wong }
3564e2aaee9cSDarrick J. Wong
356513f9e267SShiyang Ruan static int
xfs_mmaplock_two_inodes_and_break_dax_layout(struct xfs_inode * ip1,struct xfs_inode * ip2)356613f9e267SShiyang Ruan xfs_mmaplock_two_inodes_and_break_dax_layout(
356713f9e267SShiyang Ruan struct xfs_inode *ip1,
356813f9e267SShiyang Ruan struct xfs_inode *ip2)
356913f9e267SShiyang Ruan {
357013f9e267SShiyang Ruan int error;
357113f9e267SShiyang Ruan bool retry;
357213f9e267SShiyang Ruan struct page *page;
357313f9e267SShiyang Ruan
357413f9e267SShiyang Ruan if (ip1->i_ino > ip2->i_ino)
357513f9e267SShiyang Ruan swap(ip1, ip2);
357613f9e267SShiyang Ruan
357713f9e267SShiyang Ruan again:
357813f9e267SShiyang Ruan retry = false;
357913f9e267SShiyang Ruan /* Lock the first inode */
358013f9e267SShiyang Ruan xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
358113f9e267SShiyang Ruan error = xfs_break_dax_layouts(VFS_I(ip1), &retry);
358213f9e267SShiyang Ruan if (error || retry) {
358313f9e267SShiyang Ruan xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
358413f9e267SShiyang Ruan if (error == 0 && retry)
358513f9e267SShiyang Ruan goto again;
358613f9e267SShiyang Ruan return error;
358713f9e267SShiyang Ruan }
358813f9e267SShiyang Ruan
358913f9e267SShiyang Ruan if (ip1 == ip2)
359013f9e267SShiyang Ruan return 0;
359113f9e267SShiyang Ruan
359213f9e267SShiyang Ruan /* Nested lock the second inode */
359313f9e267SShiyang Ruan xfs_ilock(ip2, xfs_lock_inumorder(XFS_MMAPLOCK_EXCL, 1));
359413f9e267SShiyang Ruan /*
359513f9e267SShiyang Ruan * We cannot use xfs_break_dax_layouts() directly here because it may
359613f9e267SShiyang Ruan * need to unlock & lock the XFS_MMAPLOCK_EXCL which is not suitable
359713f9e267SShiyang Ruan * for this nested lock case.
359813f9e267SShiyang Ruan */
359913f9e267SShiyang Ruan page = dax_layout_busy_page(VFS_I(ip2)->i_mapping);
360013f9e267SShiyang Ruan if (page && page_ref_count(page) != 1) {
360113f9e267SShiyang Ruan xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
360213f9e267SShiyang Ruan xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
360313f9e267SShiyang Ruan goto again;
360413f9e267SShiyang Ruan }
360513f9e267SShiyang Ruan
360613f9e267SShiyang Ruan return 0;
360713f9e267SShiyang Ruan }
360813f9e267SShiyang Ruan
3609e2aaee9cSDarrick J. Wong /*
3610e2aaee9cSDarrick J. Wong * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3611e2aaee9cSDarrick J. Wong * mmap activity.
3612e2aaee9cSDarrick J. Wong */
3613e2aaee9cSDarrick J. Wong int
xfs_ilock2_io_mmap(struct xfs_inode * ip1,struct xfs_inode * ip2)3614e2aaee9cSDarrick J. Wong xfs_ilock2_io_mmap(
3615e2aaee9cSDarrick J. Wong struct xfs_inode *ip1,
3616e2aaee9cSDarrick J. Wong struct xfs_inode *ip2)
3617e2aaee9cSDarrick J. Wong {
3618e2aaee9cSDarrick J. Wong int ret;
3619e2aaee9cSDarrick J. Wong
3620e2aaee9cSDarrick J. Wong ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3621e2aaee9cSDarrick J. Wong if (ret)
3622e2aaee9cSDarrick J. Wong return ret;
362313f9e267SShiyang Ruan
362413f9e267SShiyang Ruan if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) {
362513f9e267SShiyang Ruan ret = xfs_mmaplock_two_inodes_and_break_dax_layout(ip1, ip2);
362613f9e267SShiyang Ruan if (ret) {
362713f9e267SShiyang Ruan inode_unlock(VFS_I(ip2));
362813f9e267SShiyang Ruan if (ip1 != ip2)
362913f9e267SShiyang Ruan inode_unlock(VFS_I(ip1));
363013f9e267SShiyang Ruan return ret;
363113f9e267SShiyang Ruan }
363213f9e267SShiyang Ruan } else
3633d2c292d8SJan Kara filemap_invalidate_lock_two(VFS_I(ip1)->i_mapping,
3634d2c292d8SJan Kara VFS_I(ip2)->i_mapping);
363513f9e267SShiyang Ruan
3636e2aaee9cSDarrick J. Wong return 0;
3637e2aaee9cSDarrick J. Wong }
3638e2aaee9cSDarrick J. Wong
3639e2aaee9cSDarrick J. Wong /* Unlock both inodes to allow IO and mmap activity. */
3640e2aaee9cSDarrick J. Wong void
xfs_iunlock2_io_mmap(struct xfs_inode * ip1,struct xfs_inode * ip2)3641e2aaee9cSDarrick J. Wong xfs_iunlock2_io_mmap(
3642e2aaee9cSDarrick J. Wong struct xfs_inode *ip1,
3643e2aaee9cSDarrick J. Wong struct xfs_inode *ip2)
3644e2aaee9cSDarrick J. Wong {
364513f9e267SShiyang Ruan if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) {
364613f9e267SShiyang Ruan xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
364713f9e267SShiyang Ruan if (ip1 != ip2)
364813f9e267SShiyang Ruan xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
364913f9e267SShiyang Ruan } else
3650d2c292d8SJan Kara filemap_invalidate_unlock_two(VFS_I(ip1)->i_mapping,
3651d2c292d8SJan Kara VFS_I(ip2)->i_mapping);
365213f9e267SShiyang Ruan
3653e2aaee9cSDarrick J. Wong inode_unlock(VFS_I(ip2));
3654d2c292d8SJan Kara if (ip1 != ip2)
3655e2aaee9cSDarrick J. Wong inode_unlock(VFS_I(ip1));
3656e2aaee9cSDarrick J. Wong }
365783771c50SDarrick J. Wong
3658d7d84772SCatherine Hoang /* Drop the MMAPLOCK and the IOLOCK after a remap completes. */
3659d7d84772SCatherine Hoang void
xfs_iunlock2_remapping(struct xfs_inode * ip1,struct xfs_inode * ip2)3660d7d84772SCatherine Hoang xfs_iunlock2_remapping(
3661d7d84772SCatherine Hoang struct xfs_inode *ip1,
3662d7d84772SCatherine Hoang struct xfs_inode *ip2)
3663d7d84772SCatherine Hoang {
3664d7d84772SCatherine Hoang xfs_iflags_clear(ip1, XFS_IREMAPPING);
3665d7d84772SCatherine Hoang
3666d7d84772SCatherine Hoang if (ip1 != ip2)
3667d7d84772SCatherine Hoang xfs_iunlock(ip1, XFS_MMAPLOCK_SHARED);
3668d7d84772SCatherine Hoang xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
3669d7d84772SCatherine Hoang
3670d7d84772SCatherine Hoang if (ip1 != ip2)
3671d7d84772SCatherine Hoang inode_unlock_shared(VFS_I(ip1));
3672d7d84772SCatherine Hoang inode_unlock(VFS_I(ip2));
3673d7d84772SCatherine Hoang }
3674d7d84772SCatherine Hoang
367583771c50SDarrick J. Wong /*
367683771c50SDarrick J. Wong * Reload the incore inode list for this inode. Caller should ensure that
367783771c50SDarrick J. Wong * the link count cannot change, either by taking ILOCK_SHARED or otherwise
367883771c50SDarrick J. Wong * preventing other threads from executing.
367983771c50SDarrick J. Wong */
368083771c50SDarrick J. Wong int
xfs_inode_reload_unlinked_bucket(struct xfs_trans * tp,struct xfs_inode * ip)368183771c50SDarrick J. Wong xfs_inode_reload_unlinked_bucket(
368283771c50SDarrick J. Wong struct xfs_trans *tp,
368383771c50SDarrick J. Wong struct xfs_inode *ip)
368483771c50SDarrick J. Wong {
368583771c50SDarrick J. Wong struct xfs_mount *mp = tp->t_mountp;
368683771c50SDarrick J. Wong struct xfs_buf *agibp;
368783771c50SDarrick J. Wong struct xfs_agi *agi;
368883771c50SDarrick J. Wong struct xfs_perag *pag;
368983771c50SDarrick J. Wong xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
369083771c50SDarrick J. Wong xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
369183771c50SDarrick J. Wong xfs_agino_t prev_agino, next_agino;
369283771c50SDarrick J. Wong unsigned int bucket;
369383771c50SDarrick J. Wong bool foundit = false;
369483771c50SDarrick J. Wong int error;
369583771c50SDarrick J. Wong
369683771c50SDarrick J. Wong /* Grab the first inode in the list */
369783771c50SDarrick J. Wong pag = xfs_perag_get(mp, agno);
369883771c50SDarrick J. Wong error = xfs_ialloc_read_agi(pag, tp, &agibp);
369983771c50SDarrick J. Wong xfs_perag_put(pag);
370083771c50SDarrick J. Wong if (error)
370183771c50SDarrick J. Wong return error;
370283771c50SDarrick J. Wong
3703537c013bSDarrick J. Wong /*
3704537c013bSDarrick J. Wong * We've taken ILOCK_SHARED and the AGI buffer lock to stabilize the
3705537c013bSDarrick J. Wong * incore unlinked list pointers for this inode. Check once more to
3706537c013bSDarrick J. Wong * see if we raced with anyone else to reload the unlinked list.
3707537c013bSDarrick J. Wong */
3708537c013bSDarrick J. Wong if (!xfs_inode_unlinked_incomplete(ip)) {
3709537c013bSDarrick J. Wong foundit = true;
3710537c013bSDarrick J. Wong goto out_agibp;
3711537c013bSDarrick J. Wong }
3712537c013bSDarrick J. Wong
371383771c50SDarrick J. Wong bucket = agino % XFS_AGI_UNLINKED_BUCKETS;
371483771c50SDarrick J. Wong agi = agibp->b_addr;
371583771c50SDarrick J. Wong
371683771c50SDarrick J. Wong trace_xfs_inode_reload_unlinked_bucket(ip);
371783771c50SDarrick J. Wong
371883771c50SDarrick J. Wong xfs_info_ratelimited(mp,
371983771c50SDarrick J. Wong "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating list recovery.",
372083771c50SDarrick J. Wong agino, agno);
372183771c50SDarrick J. Wong
372283771c50SDarrick J. Wong prev_agino = NULLAGINO;
372383771c50SDarrick J. Wong next_agino = be32_to_cpu(agi->agi_unlinked[bucket]);
372483771c50SDarrick J. Wong while (next_agino != NULLAGINO) {
372583771c50SDarrick J. Wong struct xfs_inode *next_ip = NULL;
372683771c50SDarrick J. Wong
3727537c013bSDarrick J. Wong /* Found this caller's inode, set its backlink. */
372883771c50SDarrick J. Wong if (next_agino == agino) {
372983771c50SDarrick J. Wong next_ip = ip;
373083771c50SDarrick J. Wong next_ip->i_prev_unlinked = prev_agino;
373183771c50SDarrick J. Wong foundit = true;
3732537c013bSDarrick J. Wong goto next_inode;
373383771c50SDarrick J. Wong }
3734537c013bSDarrick J. Wong
3735537c013bSDarrick J. Wong /* Try in-memory lookup first. */
373683771c50SDarrick J. Wong next_ip = xfs_iunlink_lookup(pag, next_agino);
3737537c013bSDarrick J. Wong if (next_ip)
3738537c013bSDarrick J. Wong goto next_inode;
3739537c013bSDarrick J. Wong
3740537c013bSDarrick J. Wong /* Inode not in memory, try reloading it. */
374183771c50SDarrick J. Wong error = xfs_iunlink_reload_next(tp, agibp, prev_agino,
374283771c50SDarrick J. Wong next_agino);
374383771c50SDarrick J. Wong if (error)
374483771c50SDarrick J. Wong break;
374583771c50SDarrick J. Wong
3746537c013bSDarrick J. Wong /* Grab the reloaded inode. */
374783771c50SDarrick J. Wong next_ip = xfs_iunlink_lookup(pag, next_agino);
374883771c50SDarrick J. Wong if (!next_ip) {
374983771c50SDarrick J. Wong /* No incore inode at all? We reloaded it... */
375083771c50SDarrick J. Wong ASSERT(next_ip != NULL);
375183771c50SDarrick J. Wong error = -EFSCORRUPTED;
375283771c50SDarrick J. Wong break;
375383771c50SDarrick J. Wong }
375483771c50SDarrick J. Wong
3755537c013bSDarrick J. Wong next_inode:
375683771c50SDarrick J. Wong prev_agino = next_agino;
375783771c50SDarrick J. Wong next_agino = next_ip->i_next_unlinked;
375883771c50SDarrick J. Wong }
375983771c50SDarrick J. Wong
3760537c013bSDarrick J. Wong out_agibp:
376183771c50SDarrick J. Wong xfs_trans_brelse(tp, agibp);
376283771c50SDarrick J. Wong /* Should have found this inode somewhere in the iunlinked bucket. */
376383771c50SDarrick J. Wong if (!error && !foundit)
376483771c50SDarrick J. Wong error = -EFSCORRUPTED;
376583771c50SDarrick J. Wong return error;
376683771c50SDarrick J. Wong }
376783771c50SDarrick J. Wong
376883771c50SDarrick J. Wong /* Decide if this inode is missing its unlinked list and reload it. */
376983771c50SDarrick J. Wong int
xfs_inode_reload_unlinked(struct xfs_inode * ip)377083771c50SDarrick J. Wong xfs_inode_reload_unlinked(
377183771c50SDarrick J. Wong struct xfs_inode *ip)
377283771c50SDarrick J. Wong {
377383771c50SDarrick J. Wong struct xfs_trans *tp;
377483771c50SDarrick J. Wong int error;
377583771c50SDarrick J. Wong
377683771c50SDarrick J. Wong error = xfs_trans_alloc_empty(ip->i_mount, &tp);
377783771c50SDarrick J. Wong if (error)
377883771c50SDarrick J. Wong return error;
377983771c50SDarrick J. Wong
378083771c50SDarrick J. Wong xfs_ilock(ip, XFS_ILOCK_SHARED);
378183771c50SDarrick J. Wong if (xfs_inode_unlinked_incomplete(ip))
378283771c50SDarrick J. Wong error = xfs_inode_reload_unlinked_bucket(tp, ip);
378383771c50SDarrick J. Wong xfs_iunlock(ip, XFS_ILOCK_SHARED);
378483771c50SDarrick J. Wong xfs_trans_cancel(tp);
378583771c50SDarrick J. Wong
378683771c50SDarrick J. Wong return error;
378783771c50SDarrick J. Wong }
3788c070b880SDarrick J. Wong
3789c070b880SDarrick J. Wong /* Returns the size of fundamental allocation unit for a file, in bytes. */
3790c070b880SDarrick J. Wong unsigned int
xfs_inode_alloc_unitsize(struct xfs_inode * ip)3791c070b880SDarrick J. Wong xfs_inode_alloc_unitsize(
3792c070b880SDarrick J. Wong struct xfs_inode *ip)
3793c070b880SDarrick J. Wong {
3794c070b880SDarrick J. Wong unsigned int blocks = 1;
3795c070b880SDarrick J. Wong
3796c070b880SDarrick J. Wong if (XFS_IS_REALTIME_INODE(ip))
3797c070b880SDarrick J. Wong blocks = ip->i_mount->m_sb.sb_rextsize;
3798c070b880SDarrick J. Wong
3799c070b880SDarrick J. Wong return XFS_FSB_TO_B(ip->i_mount, blocks);
3800c070b880SDarrick J. Wong }
3801