blob: ac24818f7b2d5a62703056411f71d6d0f1678873 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Robert P. J. Day40ebd812007-11-23 16:30:51 +110018#include <linux/log2.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110028#include "xfs_inode.h"
Dave Chinner57062782013-10-15 09:17:51 +110029#include "xfs_da_format.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100030#include "xfs_da_btree.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100031#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100033#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans_space.h"
35#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_ialloc.h"
39#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100040#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100043#include "xfs_filestream.h"
Christoph Hellwig93848a92013-04-03 16:11:17 +110044#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110046#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100047#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110048#include "xfs_trans_priv.h"
49#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110050#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020055 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * freed from a file in a single transaction.
57 */
58#define XFS_ITRUNC_MAX_EXTENTS 2
59
60STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Zhi Yong Wuab297432013-12-18 08:22:41 +080062STATIC int xfs_iunlink_remove(xfs_trans_t *, xfs_inode_t *);
63
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100064/*
65 * helper function to extract extent size hint from inode
66 */
67xfs_extlen_t
68xfs_get_extsz_hint(
69 struct xfs_inode *ip)
70{
71 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
72 return ip->i_d.di_extsize;
73 if (XFS_IS_REALTIME_INODE(ip))
74 return ip->i_mount->m_sb.sb_rextsize;
75 return 0;
76}
77
Dave Chinnerfa96aca2012-10-08 21:56:10 +110078/*
Christoph Hellwigefa70be2013-12-18 02:14:39 -080079 * These two are wrapper routines around the xfs_ilock() routine used to
80 * centralize some grungy code. They are used in places that wish to lock the
81 * inode solely for reading the extents. The reason these places can't just
82 * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
83 * bringing in of the extents from disk for a file in b-tree format. If the
84 * inode is in b-tree format, then we need to lock the inode exclusively until
85 * the extents are read in. Locking it exclusively all the time would limit
86 * our parallelism unnecessarily, though. What we do instead is check to see
87 * if the extents have been read in yet, and only lock the inode exclusively
88 * if they have not.
Dave Chinnerfa96aca2012-10-08 21:56:10 +110089 *
Christoph Hellwigefa70be2013-12-18 02:14:39 -080090 * The functions return a value which should be given to the corresponding
Christoph Hellwig01f4f322013-12-06 12:30:08 -080091 * xfs_iunlock() call.
Dave Chinnerfa96aca2012-10-08 21:56:10 +110092 */
93uint
Christoph Hellwig309ecac82013-12-06 12:30:09 -080094xfs_ilock_data_map_shared(
95 struct xfs_inode *ip)
Dave Chinnerfa96aca2012-10-08 21:56:10 +110096{
Christoph Hellwig309ecac82013-12-06 12:30:09 -080097 uint lock_mode = XFS_ILOCK_SHARED;
Dave Chinnerfa96aca2012-10-08 21:56:10 +110098
Christoph Hellwig309ecac82013-12-06 12:30:09 -080099 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
100 (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100101 lock_mode = XFS_ILOCK_EXCL;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100102 xfs_ilock(ip, lock_mode);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100103 return lock_mode;
104}
105
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800106uint
107xfs_ilock_attr_map_shared(
108 struct xfs_inode *ip)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100109{
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800110 uint lock_mode = XFS_ILOCK_SHARED;
111
112 if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
113 (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
114 lock_mode = XFS_ILOCK_EXCL;
115 xfs_ilock(ip, lock_mode);
116 return lock_mode;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100117}
118
119/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100120 * The xfs inode contains 3 multi-reader locks: the i_iolock the i_mmap_lock and
121 * the i_lock. This routine allows various combinations of the locks to be
122 * obtained.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100123 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100124 * The 3 locks should always be ordered so that the IO lock is obtained first,
125 * the mmap lock second and the ilock last in order to prevent deadlock.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100126 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100127 * Basic locking order:
128 *
129 * i_iolock -> i_mmap_lock -> page_lock -> i_ilock
130 *
131 * mmap_sem locking order:
132 *
133 * i_iolock -> page lock -> mmap_sem
134 * mmap_sem -> i_mmap_lock -> page_lock
135 *
136 * The difference in mmap_sem locking order mean that we cannot hold the
137 * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
138 * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
139 * in get_user_pages() to map the user pages into the kernel address space for
140 * direct IO. Similarly the i_iolock cannot be taken inside a page fault because
141 * page faults already hold the mmap_sem.
142 *
143 * Hence to serialise fully against both syscall and mmap based IO, we need to
144 * take both the i_iolock and the i_mmap_lock. These locks should *only* be both
145 * taken in places where we need to invalidate the page cache in a race
146 * free manner (e.g. truncate, hole punch and other extent manipulation
147 * functions).
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100148 */
149void
150xfs_ilock(
151 xfs_inode_t *ip,
152 uint lock_flags)
153{
154 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
155
156 /*
157 * You can't set both SHARED and EXCL for the same lock,
158 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
159 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
160 */
161 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
162 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100163 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
164 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100165 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
166 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
167 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
168
169 if (lock_flags & XFS_IOLOCK_EXCL)
170 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
171 else if (lock_flags & XFS_IOLOCK_SHARED)
172 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
173
Dave Chinner653c60b2015-02-23 21:43:37 +1100174 if (lock_flags & XFS_MMAPLOCK_EXCL)
175 mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
176 else if (lock_flags & XFS_MMAPLOCK_SHARED)
177 mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
178
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100179 if (lock_flags & XFS_ILOCK_EXCL)
180 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
181 else if (lock_flags & XFS_ILOCK_SHARED)
182 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
183}
184
185/*
186 * This is just like xfs_ilock(), except that the caller
187 * is guaranteed not to sleep. It returns 1 if it gets
188 * the requested locks and 0 otherwise. If the IO lock is
189 * obtained but the inode lock cannot be, then the IO lock
190 * is dropped before returning.
191 *
192 * ip -- the inode being locked
193 * lock_flags -- this parameter indicates the inode's locks to be
194 * to be locked. See the comment for xfs_ilock() for a list
195 * of valid values.
196 */
197int
198xfs_ilock_nowait(
199 xfs_inode_t *ip,
200 uint lock_flags)
201{
202 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
203
204 /*
205 * You can't set both SHARED and EXCL for the same lock,
206 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
207 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
208 */
209 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
210 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100211 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
212 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100213 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
214 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
215 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
216
217 if (lock_flags & XFS_IOLOCK_EXCL) {
218 if (!mrtryupdate(&ip->i_iolock))
219 goto out;
220 } else if (lock_flags & XFS_IOLOCK_SHARED) {
221 if (!mrtryaccess(&ip->i_iolock))
222 goto out;
223 }
Dave Chinner653c60b2015-02-23 21:43:37 +1100224
225 if (lock_flags & XFS_MMAPLOCK_EXCL) {
226 if (!mrtryupdate(&ip->i_mmaplock))
227 goto out_undo_iolock;
228 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
229 if (!mrtryaccess(&ip->i_mmaplock))
230 goto out_undo_iolock;
231 }
232
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100233 if (lock_flags & XFS_ILOCK_EXCL) {
234 if (!mrtryupdate(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100235 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100236 } else if (lock_flags & XFS_ILOCK_SHARED) {
237 if (!mrtryaccess(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100238 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100239 }
240 return 1;
241
Dave Chinner653c60b2015-02-23 21:43:37 +1100242out_undo_mmaplock:
243 if (lock_flags & XFS_MMAPLOCK_EXCL)
244 mrunlock_excl(&ip->i_mmaplock);
245 else if (lock_flags & XFS_MMAPLOCK_SHARED)
246 mrunlock_shared(&ip->i_mmaplock);
247out_undo_iolock:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100248 if (lock_flags & XFS_IOLOCK_EXCL)
249 mrunlock_excl(&ip->i_iolock);
250 else if (lock_flags & XFS_IOLOCK_SHARED)
251 mrunlock_shared(&ip->i_iolock);
Dave Chinner653c60b2015-02-23 21:43:37 +1100252out:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100253 return 0;
254}
255
256/*
257 * xfs_iunlock() is used to drop the inode locks acquired with
258 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
259 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
260 * that we know which locks to drop.
261 *
262 * ip -- the inode being unlocked
263 * lock_flags -- this parameter indicates the inode's locks to be
264 * to be unlocked. See the comment for xfs_ilock() for a list
265 * of valid values for this parameter.
266 *
267 */
268void
269xfs_iunlock(
270 xfs_inode_t *ip,
271 uint lock_flags)
272{
273 /*
274 * You can't set both SHARED and EXCL for the same lock,
275 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
276 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
277 */
278 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
279 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100280 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
281 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100282 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
283 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
284 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
285 ASSERT(lock_flags != 0);
286
287 if (lock_flags & XFS_IOLOCK_EXCL)
288 mrunlock_excl(&ip->i_iolock);
289 else if (lock_flags & XFS_IOLOCK_SHARED)
290 mrunlock_shared(&ip->i_iolock);
291
Dave Chinner653c60b2015-02-23 21:43:37 +1100292 if (lock_flags & XFS_MMAPLOCK_EXCL)
293 mrunlock_excl(&ip->i_mmaplock);
294 else if (lock_flags & XFS_MMAPLOCK_SHARED)
295 mrunlock_shared(&ip->i_mmaplock);
296
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100297 if (lock_flags & XFS_ILOCK_EXCL)
298 mrunlock_excl(&ip->i_lock);
299 else if (lock_flags & XFS_ILOCK_SHARED)
300 mrunlock_shared(&ip->i_lock);
301
302 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
303}
304
305/*
306 * give up write locks. the i/o lock cannot be held nested
307 * if it is being demoted.
308 */
309void
310xfs_ilock_demote(
311 xfs_inode_t *ip,
312 uint lock_flags)
313{
Dave Chinner653c60b2015-02-23 21:43:37 +1100314 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
315 ASSERT((lock_flags &
316 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100317
318 if (lock_flags & XFS_ILOCK_EXCL)
319 mrdemote(&ip->i_lock);
Dave Chinner653c60b2015-02-23 21:43:37 +1100320 if (lock_flags & XFS_MMAPLOCK_EXCL)
321 mrdemote(&ip->i_mmaplock);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100322 if (lock_flags & XFS_IOLOCK_EXCL)
323 mrdemote(&ip->i_iolock);
324
325 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
326}
327
Dave Chinner742ae1e2013-04-30 21:39:34 +1000328#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100329int
330xfs_isilocked(
331 xfs_inode_t *ip,
332 uint lock_flags)
333{
334 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
335 if (!(lock_flags & XFS_ILOCK_SHARED))
336 return !!ip->i_lock.mr_writer;
337 return rwsem_is_locked(&ip->i_lock.mr_lock);
338 }
339
Dave Chinner653c60b2015-02-23 21:43:37 +1100340 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
341 if (!(lock_flags & XFS_MMAPLOCK_SHARED))
342 return !!ip->i_mmaplock.mr_writer;
343 return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
344 }
345
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100346 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
347 if (!(lock_flags & XFS_IOLOCK_SHARED))
348 return !!ip->i_iolock.mr_writer;
349 return rwsem_is_locked(&ip->i_iolock.mr_lock);
350 }
351
352 ASSERT(0);
353 return 0;
354}
355#endif
356
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000357#ifdef DEBUG
358int xfs_locked_n;
359int xfs_small_retries;
360int xfs_middle_retries;
361int xfs_lots_retries;
362int xfs_lock_delays;
363#endif
364
365/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100366 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
367 * value. This shouldn't be called for page fault locking, but we also need to
368 * ensure we don't overrun the number of lockdep subclasses for the iolock or
369 * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000370 */
371static inline int
372xfs_lock_inumorder(int lock_mode, int subclass)
373{
Dave Chinner653c60b2015-02-23 21:43:37 +1100374 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
375 ASSERT(subclass + XFS_LOCK_INUMORDER <
376 (1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000377 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Dave Chinner653c60b2015-02-23 21:43:37 +1100378 }
379
380 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
381 ASSERT(subclass + XFS_LOCK_INUMORDER <
382 (1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
383 lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
384 XFS_MMAPLOCK_SHIFT;
385 }
386
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000387 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
388 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
389
390 return lock_mode;
391}
392
393/*
394 * The following routine will lock n inodes in exclusive mode.
395 * We assume the caller calls us with the inodes in i_ino order.
396 *
397 * We need to detect deadlock where an inode that we lock
398 * is in the AIL and we start waiting for another inode that is locked
399 * by a thread in a long running transaction (such as truncate). This can
400 * result in deadlock since the long running trans might need to wait
401 * for the inode we just locked in order to push the tail and free space
402 * in the log.
403 */
404void
405xfs_lock_inodes(
406 xfs_inode_t **ips,
407 int inodes,
408 uint lock_mode)
409{
410 int attempts = 0, i, j, try_lock;
411 xfs_log_item_t *lp;
412
413 ASSERT(ips && (inodes >= 2)); /* we need at least two */
414
415 try_lock = 0;
416 i = 0;
417
418again:
419 for (; i < inodes; i++) {
420 ASSERT(ips[i]);
421
422 if (i && (ips[i] == ips[i-1])) /* Already locked */
423 continue;
424
425 /*
426 * If try_lock is not set yet, make sure all locked inodes
427 * are not in the AIL.
428 * If any are, set try_lock to be used later.
429 */
430
431 if (!try_lock) {
432 for (j = (i - 1); j >= 0 && !try_lock; j--) {
433 lp = (xfs_log_item_t *)ips[j]->i_itemp;
434 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
435 try_lock++;
436 }
437 }
438 }
439
440 /*
441 * If any of the previous locks we have locked is in the AIL,
442 * we must TRY to get the second and subsequent locks. If
443 * we can't get any, we must release all we have
444 * and try again.
445 */
446
447 if (try_lock) {
448 /* try_lock must be 0 if i is 0. */
449 /*
450 * try_lock means we have an inode locked
451 * that is in the AIL.
452 */
453 ASSERT(i != 0);
454 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
455 attempts++;
456
457 /*
458 * Unlock all previous guys and try again.
459 * xfs_iunlock will try to push the tail
460 * if the inode is in the AIL.
461 */
462
463 for(j = i - 1; j >= 0; j--) {
464
465 /*
466 * Check to see if we've already
467 * unlocked this one.
468 * Not the first one going back,
469 * and the inode ptr is the same.
470 */
471 if ((j != (i - 1)) && ips[j] ==
472 ips[j+1])
473 continue;
474
475 xfs_iunlock(ips[j], lock_mode);
476 }
477
478 if ((attempts % 5) == 0) {
479 delay(1); /* Don't just spin the CPU */
480#ifdef DEBUG
481 xfs_lock_delays++;
482#endif
483 }
484 i = 0;
485 try_lock = 0;
486 goto again;
487 }
488 } else {
489 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
490 }
491 }
492
493#ifdef DEBUG
494 if (attempts) {
495 if (attempts < 5) xfs_small_retries++;
496 else if (attempts < 100) xfs_middle_retries++;
497 else xfs_lots_retries++;
498 } else {
499 xfs_locked_n++;
500 }
501#endif
502}
503
504/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100505 * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
506 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
507 * lock more than one at a time, lockdep will report false positives saying we
508 * have violated locking orders.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000509 */
510void
511xfs_lock_two_inodes(
512 xfs_inode_t *ip0,
513 xfs_inode_t *ip1,
514 uint lock_mode)
515{
516 xfs_inode_t *temp;
517 int attempts = 0;
518 xfs_log_item_t *lp;
519
Dave Chinner653c60b2015-02-23 21:43:37 +1100520 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
521 ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
522 ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
523 } else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
524 ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
525
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000526 ASSERT(ip0->i_ino != ip1->i_ino);
527
528 if (ip0->i_ino > ip1->i_ino) {
529 temp = ip0;
530 ip0 = ip1;
531 ip1 = temp;
532 }
533
534 again:
535 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
536
537 /*
538 * If the first lock we have locked is in the AIL, we must TRY to get
539 * the second lock. If we can't get it, we must release the first one
540 * and try again.
541 */
542 lp = (xfs_log_item_t *)ip0->i_itemp;
543 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
544 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
545 xfs_iunlock(ip0, lock_mode);
546 if ((++attempts % 5) == 0)
547 delay(1); /* Don't just spin the CPU */
548 goto again;
549 }
550 } else {
551 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
552 }
553}
554
555
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100556void
557__xfs_iflock(
558 struct xfs_inode *ip)
559{
560 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
561 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
562
563 do {
564 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
565 if (xfs_isiflocked(ip))
566 io_schedule();
567 } while (!xfs_iflock_nowait(ip));
568
569 finish_wait(wq, &wait.wait);
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572STATIC uint
573_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 __uint16_t di_flags)
575{
576 uint flags = 0;
577
578 if (di_flags & XFS_DIFLAG_ANY) {
579 if (di_flags & XFS_DIFLAG_REALTIME)
580 flags |= XFS_XFLAG_REALTIME;
581 if (di_flags & XFS_DIFLAG_PREALLOC)
582 flags |= XFS_XFLAG_PREALLOC;
583 if (di_flags & XFS_DIFLAG_IMMUTABLE)
584 flags |= XFS_XFLAG_IMMUTABLE;
585 if (di_flags & XFS_DIFLAG_APPEND)
586 flags |= XFS_XFLAG_APPEND;
587 if (di_flags & XFS_DIFLAG_SYNC)
588 flags |= XFS_XFLAG_SYNC;
589 if (di_flags & XFS_DIFLAG_NOATIME)
590 flags |= XFS_XFLAG_NOATIME;
591 if (di_flags & XFS_DIFLAG_NODUMP)
592 flags |= XFS_XFLAG_NODUMP;
593 if (di_flags & XFS_DIFLAG_RTINHERIT)
594 flags |= XFS_XFLAG_RTINHERIT;
595 if (di_flags & XFS_DIFLAG_PROJINHERIT)
596 flags |= XFS_XFLAG_PROJINHERIT;
597 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
598 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100599 if (di_flags & XFS_DIFLAG_EXTSIZE)
600 flags |= XFS_XFLAG_EXTSIZE;
601 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
602 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000603 if (di_flags & XFS_DIFLAG_NODEFRAG)
604 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000605 if (di_flags & XFS_DIFLAG_FILESTREAM)
606 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 return flags;
610}
611
612uint
613xfs_ip2xflags(
614 xfs_inode_t *ip)
615{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000616 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Nathan Scotta916e2b2006-06-09 17:12:17 +1000618 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100619 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622uint
623xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100624 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100626 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100627 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000631 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
632 * is allowed, otherwise it has to be an exact match. If a CI match is found,
633 * ci_name->name will point to a the actual name (caller must free) or
634 * will be set to NULL if an exact match is found.
635 */
636int
637xfs_lookup(
638 xfs_inode_t *dp,
639 struct xfs_name *name,
640 xfs_inode_t **ipp,
641 struct xfs_name *ci_name)
642{
643 xfs_ino_t inum;
644 int error;
645 uint lock_mode;
646
647 trace_xfs_lookup(dp, name);
648
649 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000650 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000651
Christoph Hellwig309ecac82013-12-06 12:30:09 -0800652 lock_mode = xfs_ilock_data_map_shared(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000653 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800654 xfs_iunlock(dp, lock_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000655
656 if (error)
657 goto out;
658
659 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
660 if (error)
661 goto out_free_name;
662
663 return 0;
664
665out_free_name:
666 if (ci_name)
667 kmem_free(ci_name->name);
668out:
669 *ipp = NULL;
670 return error;
671}
672
673/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * Allocate an inode on disk and return a copy of its in-core version.
675 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
676 * appropriately within the inode. The uid and gid for the inode are
677 * set according to the contents of the given cred structure.
678 *
679 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300680 * has a free inode available, call xfs_iget() to obtain the in-core
681 * version of the allocated inode. Finally, fill in the inode and
682 * log its initial contents. In this case, ialloc_context would be
683 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300685 * If xfs_dialloc() does not have an available inode, it will replenish
686 * its supply by doing an allocation. Since we can only do one
687 * allocation within a transaction without deadlocks, we must commit
688 * the current transaction before returning the inode itself.
689 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * The caller should then commit the current transaction, start a new
691 * transaction, and call xfs_ialloc() again to actually get the inode.
692 *
693 * To ensure that some other process does not grab the inode that
694 * was allocated during the first call to xfs_ialloc(), this routine
695 * also returns the [locked] bp pointing to the head of the freelist
696 * as ialloc_context. The caller should hold this buffer across
697 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000698 *
699 * If we are allocating quota inodes, we do not have a parent inode
700 * to attach to or associate with (i.e. pip == NULL) because they
701 * are not linked into the directory structure - they are attached
702 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704int
705xfs_ialloc(
706 xfs_trans_t *tp,
707 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400708 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700709 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000711 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int okalloc,
713 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 xfs_inode_t **ipp)
715{
Christoph Hellwig93848a92013-04-03 16:11:17 +1100716 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 xfs_ino_t ino;
718 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 uint flags;
720 int error;
Dave Chinnere076b0f2014-10-02 09:18:13 +1000721 struct timespec tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * Call the space management code to pick
725 * the on-disk inode to be allocated.
726 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000727 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -0400728 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100729 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -0400731 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *ipp = NULL;
733 return 0;
734 }
735 ASSERT(*ialloc_context == NULL);
736
737 /*
738 * Get the in-core inode with the lock held exclusively.
739 * This is because we're setting fields here we need
740 * to prevent others from looking at until we're done.
741 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100742 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000743 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100744 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ASSERT(ip != NULL);
747
Dave Chinner263997a2014-05-20 07:46:40 +1000748 /*
749 * We always convert v1 inodes to v2 now - we only support filesystems
750 * with >= v2 inode capability, so there is no reason for ever leaving
751 * an inode in v1 format.
752 */
753 if (ip->i_d.di_version == 1)
754 ip->i_d.di_version = 2;
755
Al Viro576b1d62011-07-26 02:50:15 -0400756 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ip->i_d.di_onlink = 0;
758 ip->i_d.di_nlink = nlink;
759 ASSERT(ip->i_d.di_nlink == nlink);
Dwight Engen7aab1b22013-08-15 14:08:01 -0400760 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
761 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000762 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
764
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000765 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -0400767 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ip->i_d.di_mode |= S_ISGID;
769 }
770 }
771
772 /*
773 * If the group ID of the new file does not match the effective group
774 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
775 * (and only if the irix_sgid_inherit compatibility variable is set).
776 */
777 if ((irix_sgid_inherit) &&
778 (ip->i_d.di_mode & S_ISGID) &&
Dwight Engen7aab1b22013-08-15 14:08:01 -0400779 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ip->i_d.di_mode &= ~S_ISGID;
781 }
782
783 ip->i_d.di_size = 0;
784 ip->i_d.di_nextents = 0;
785 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000786
Dave Chinnere076b0f2014-10-02 09:18:13 +1000787 tv = current_fs_time(mp->m_super);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000788 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
789 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
790 ip->i_d.di_atime = ip->i_d.di_mtime;
791 ip->i_d.di_ctime = ip->i_d.di_mtime;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * di_gen will have been taken care of in xfs_iread.
795 */
796 ip->i_d.di_extsize = 0;
797 ip->i_d.di_dmevmask = 0;
798 ip->i_d.di_dmstate = 0;
799 ip->i_d.di_flags = 0;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100800
801 if (ip->i_d.di_version == 3) {
802 ASSERT(ip->i_d.di_ino == ino);
803 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
804 ip->i_d.di_crc = 0;
805 ip->i_d.di_changecount = 1;
806 ip->i_d.di_lsn = 0;
807 ip->i_d.di_flags2 = 0;
808 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
809 ip->i_d.di_crtime = ip->i_d.di_mtime;
810 }
811
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 flags = XFS_ILOG_CORE;
814 switch (mode & S_IFMT) {
815 case S_IFIFO:
816 case S_IFCHR:
817 case S_IFBLK:
818 case S_IFSOCK:
819 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
820 ip->i_df.if_u2.if_rdev = rdev;
821 ip->i_df.if_flags = 0;
822 flags |= XFS_ILOG_DEV;
823 break;
824 case S_IFREG:
825 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +1000826 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000827 uint di_flags = 0;
828
Al Viroabbede12011-07-26 02:31:30 -0400829 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000830 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
831 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100832 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
833 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
834 ip->i_d.di_extsize = pip->i_d.di_extsize;
835 }
Dave Chinner9336e3a2014-10-02 09:18:40 +1000836 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
837 di_flags |= XFS_DIFLAG_PROJINHERIT;
Al Viroabbede12011-07-26 02:31:30 -0400838 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000839 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +1000840 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100841 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
842 di_flags |= XFS_DIFLAG_EXTSIZE;
843 ip->i_d.di_extsize = pip->i_d.di_extsize;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
847 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +1000848 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
850 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +1000851 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
853 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +1000854 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
856 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +1000857 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000858 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
859 xfs_inherit_nodefrag)
860 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000861 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
862 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +1000863 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 /* FALLTHROUGH */
866 case S_IFLNK:
867 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
868 ip->i_df.if_flags = XFS_IFEXTENTS;
869 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
870 ip->i_df.if_u1.if_extents = NULL;
871 break;
872 default:
873 ASSERT(0);
874 }
875 /*
876 * Attribute fork settings for new inode.
877 */
878 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
879 ip->i_d.di_anextents = 0;
880
881 /*
882 * Log the new values stuffed into the inode.
883 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000884 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 xfs_trans_log_inode(tp, ip, flags);
886
Nathan Scottb83bd132006-06-09 16:48:30 +1000887 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000888 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 *ipp = ip;
891 return 0;
892}
893
Dave Chinnere546cb72013-08-12 20:49:47 +1000894/*
895 * Allocates a new inode from disk and return a pointer to the
896 * incore copy. This routine will internally commit the current
897 * transaction and allocate a new one if the Space Manager needed
898 * to do an allocation to replenish the inode free-list.
899 *
900 * This routine is designed to be called from xfs_create and
901 * xfs_create_dir.
902 *
903 */
904int
905xfs_dir_ialloc(
906 xfs_trans_t **tpp, /* input: current transaction;
907 output: may be a new transaction. */
908 xfs_inode_t *dp, /* directory within whose allocate
909 the inode. */
910 umode_t mode,
911 xfs_nlink_t nlink,
912 xfs_dev_t rdev,
913 prid_t prid, /* project id */
914 int okalloc, /* ok to allocate new space */
915 xfs_inode_t **ipp, /* pointer to inode; it will be
916 locked. */
917 int *committed)
918
919{
920 xfs_trans_t *tp;
921 xfs_trans_t *ntp;
922 xfs_inode_t *ip;
923 xfs_buf_t *ialloc_context = NULL;
924 int code;
Dave Chinnere546cb72013-08-12 20:49:47 +1000925 void *dqinfo;
926 uint tflags;
927
928 tp = *tpp;
929 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
930
931 /*
932 * xfs_ialloc will return a pointer to an incore inode if
933 * the Space Manager has an available inode on the free
934 * list. Otherwise, it will do an allocation and replenish
935 * the freelist. Since we can only do one allocation per
936 * transaction without deadlocks, we will need to commit the
937 * current transaction and start a new one. We will then
938 * need to call xfs_ialloc again to get the inode.
939 *
940 * If xfs_ialloc did an allocation to replenish the freelist,
941 * it returns the bp containing the head of the freelist as
942 * ialloc_context. We will hold a lock on it across the
943 * transaction commit so that no other process can steal
944 * the inode(s) that we've just allocated.
945 */
946 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
947 &ialloc_context, &ip);
948
949 /*
950 * Return an error if we were unable to allocate a new inode.
951 * This should only happen if we run out of space on disk or
952 * encounter a disk error.
953 */
954 if (code) {
955 *ipp = NULL;
956 return code;
957 }
958 if (!ialloc_context && !ip) {
959 *ipp = NULL;
Dave Chinner24513372014-06-25 14:58:08 +1000960 return -ENOSPC;
Dave Chinnere546cb72013-08-12 20:49:47 +1000961 }
962
963 /*
964 * If the AGI buffer is non-NULL, then we were unable to get an
965 * inode in one operation. We need to commit the current
966 * transaction and call xfs_ialloc() again. It is guaranteed
967 * to succeed the second time.
968 */
969 if (ialloc_context) {
Jie Liu3d3c8b52013-08-12 20:49:59 +1000970 struct xfs_trans_res tres;
971
Dave Chinnere546cb72013-08-12 20:49:47 +1000972 /*
973 * Normally, xfs_trans_commit releases all the locks.
974 * We call bhold to hang on to the ialloc_context across
975 * the commit. Holding this buffer prevents any other
976 * processes from doing any allocations in this
977 * allocation group.
978 */
979 xfs_trans_bhold(tp, ialloc_context);
980 /*
981 * Save the log reservation so we can use
982 * them in the next transaction.
983 */
Jie Liu3d3c8b52013-08-12 20:49:59 +1000984 tres.tr_logres = xfs_trans_get_log_res(tp);
985 tres.tr_logcount = xfs_trans_get_log_count(tp);
Dave Chinnere546cb72013-08-12 20:49:47 +1000986
987 /*
988 * We want the quota changes to be associated with the next
989 * transaction, NOT this one. So, detach the dqinfo from this
990 * and attach it to the next transaction.
991 */
992 dqinfo = NULL;
993 tflags = 0;
994 if (tp->t_dqinfo) {
995 dqinfo = (void *)tp->t_dqinfo;
996 tp->t_dqinfo = NULL;
997 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
998 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
999 }
1000
1001 ntp = xfs_trans_dup(tp);
1002 code = xfs_trans_commit(tp, 0);
1003 tp = ntp;
1004 if (committed != NULL) {
1005 *committed = 1;
1006 }
1007 /*
1008 * If we get an error during the commit processing,
1009 * release the buffer that is still held and return
1010 * to the caller.
1011 */
1012 if (code) {
1013 xfs_buf_relse(ialloc_context);
1014 if (dqinfo) {
1015 tp->t_dqinfo = dqinfo;
1016 xfs_trans_free_dqinfo(tp);
1017 }
1018 *tpp = ntp;
1019 *ipp = NULL;
1020 return code;
1021 }
1022
1023 /*
1024 * transaction commit worked ok so we can drop the extra ticket
1025 * reference that we gained in xfs_trans_dup()
1026 */
1027 xfs_log_ticket_put(tp->t_ticket);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001028 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1029 code = xfs_trans_reserve(tp, &tres, 0, 0);
1030
Dave Chinnere546cb72013-08-12 20:49:47 +10001031 /*
1032 * Re-attach the quota info that we detached from prev trx.
1033 */
1034 if (dqinfo) {
1035 tp->t_dqinfo = dqinfo;
1036 tp->t_flags |= tflags;
1037 }
1038
1039 if (code) {
1040 xfs_buf_relse(ialloc_context);
1041 *tpp = ntp;
1042 *ipp = NULL;
1043 return code;
1044 }
1045 xfs_trans_bjoin(tp, ialloc_context);
1046
1047 /*
1048 * Call ialloc again. Since we've locked out all
1049 * other allocations in this allocation group,
1050 * this call should always succeed.
1051 */
1052 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1053 okalloc, &ialloc_context, &ip);
1054
1055 /*
1056 * If we get an error at this point, return to the caller
1057 * so that the current transaction can be aborted.
1058 */
1059 if (code) {
1060 *tpp = tp;
1061 *ipp = NULL;
1062 return code;
1063 }
1064 ASSERT(!ialloc_context && ip);
1065
1066 } else {
1067 if (committed != NULL)
1068 *committed = 0;
1069 }
1070
1071 *ipp = ip;
1072 *tpp = tp;
1073
1074 return 0;
1075}
1076
1077/*
1078 * Decrement the link count on an inode & log the change.
1079 * If this causes the link count to go to zero, initiate the
1080 * logging activity required to truncate a file.
1081 */
1082int /* error */
1083xfs_droplink(
1084 xfs_trans_t *tp,
1085 xfs_inode_t *ip)
1086{
1087 int error;
1088
1089 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1090
1091 ASSERT (ip->i_d.di_nlink > 0);
1092 ip->i_d.di_nlink--;
1093 drop_nlink(VFS_I(ip));
1094 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1095
1096 error = 0;
1097 if (ip->i_d.di_nlink == 0) {
1098 /*
1099 * We're dropping the last link to this file.
1100 * Move the on-disk inode to the AGI unlinked list.
1101 * From xfs_inactive() we will pull the inode from
1102 * the list and free it.
1103 */
1104 error = xfs_iunlink(tp, ip);
1105 }
1106 return error;
1107}
1108
1109/*
Dave Chinnere546cb72013-08-12 20:49:47 +10001110 * Increment the link count on an inode & log the change.
1111 */
1112int
1113xfs_bumplink(
1114 xfs_trans_t *tp,
1115 xfs_inode_t *ip)
1116{
1117 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1118
Dave Chinner263997a2014-05-20 07:46:40 +10001119 ASSERT(ip->i_d.di_version > 1);
Zhi Yong Wuab297432013-12-18 08:22:41 +08001120 ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
Dave Chinnere546cb72013-08-12 20:49:47 +10001121 ip->i_d.di_nlink++;
1122 inc_nlink(VFS_I(ip));
Dave Chinnere546cb72013-08-12 20:49:47 +10001123 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1124 return 0;
1125}
1126
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001127int
1128xfs_create(
1129 xfs_inode_t *dp,
1130 struct xfs_name *name,
1131 umode_t mode,
1132 xfs_dev_t rdev,
1133 xfs_inode_t **ipp)
1134{
1135 int is_dir = S_ISDIR(mode);
1136 struct xfs_mount *mp = dp->i_mount;
1137 struct xfs_inode *ip = NULL;
1138 struct xfs_trans *tp = NULL;
1139 int error;
1140 xfs_bmap_free_t free_list;
1141 xfs_fsblock_t first_block;
1142 bool unlock_dp_on_error = false;
1143 uint cancel_flags;
1144 int committed;
1145 prid_t prid;
1146 struct xfs_dquot *udqp = NULL;
1147 struct xfs_dquot *gdqp = NULL;
1148 struct xfs_dquot *pdqp = NULL;
Brian Foster062647a2014-11-28 14:00:16 +11001149 struct xfs_trans_res *tres;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001150 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001151
1152 trace_xfs_create(dp, name);
1153
1154 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001155 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001156
Zhi Yong Wu163467d2013-12-18 08:22:39 +08001157 prid = xfs_get_initial_prid(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001158
1159 /*
1160 * Make sure that we have allocated dquot(s) on disk.
1161 */
Dwight Engen7aab1b22013-08-15 14:08:01 -04001162 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1163 xfs_kgid_to_gid(current_fsgid()), prid,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001164 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1165 &udqp, &gdqp, &pdqp);
1166 if (error)
1167 return error;
1168
1169 if (is_dir) {
1170 rdev = 0;
1171 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +11001172 tres = &M_RES(mp)->tr_mkdir;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001173 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1174 } else {
1175 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +11001176 tres = &M_RES(mp)->tr_create;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001177 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1178 }
1179
1180 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1181
1182 /*
1183 * Initially assume that the file does not exist and
1184 * reserve the resources for that case. If that is not
1185 * the case we'll drop the one we have and get a more
1186 * appropriate transaction later.
1187 */
Brian Foster062647a2014-11-28 14:00:16 +11001188 error = xfs_trans_reserve(tp, tres, resblks, 0);
Dave Chinner24513372014-06-25 14:58:08 +10001189 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001190 /* flush outstanding delalloc blocks and retry */
1191 xfs_flush_inodes(mp);
Brian Foster062647a2014-11-28 14:00:16 +11001192 error = xfs_trans_reserve(tp, tres, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001193 }
Dave Chinner24513372014-06-25 14:58:08 +10001194 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001195 /* No space at all so try a "no-allocation" reservation */
1196 resblks = 0;
Brian Foster062647a2014-11-28 14:00:16 +11001197 error = xfs_trans_reserve(tp, tres, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001198 }
1199 if (error) {
1200 cancel_flags = 0;
1201 goto out_trans_cancel;
1202 }
1203
1204 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1205 unlock_dp_on_error = true;
1206
1207 xfs_bmap_init(&free_list, &first_block);
1208
1209 /*
1210 * Reserve disk quota and the inode.
1211 */
1212 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1213 pdqp, resblks, 1, 0);
1214 if (error)
1215 goto out_trans_cancel;
1216
Eric Sandeen94f3cad2014-09-09 11:57:52 +10001217 if (!resblks) {
1218 error = xfs_dir_canenter(tp, dp, name);
1219 if (error)
1220 goto out_trans_cancel;
1221 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001222
1223 /*
1224 * A newly created regular or special file just has one directory
1225 * entry pointing to them, but a directory also the "." entry
1226 * pointing to itself.
1227 */
1228 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1229 prid, resblks > 0, &ip, &committed);
1230 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001231 if (error == -ENOSPC)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001232 goto out_trans_cancel;
1233 goto out_trans_abort;
1234 }
1235
1236 /*
1237 * Now we join the directory inode to the transaction. We do not do it
1238 * earlier because xfs_dir_ialloc might commit the previous transaction
1239 * (and release all the locks). An error from here on will result in
1240 * the transaction cancel unlocking dp so don't do it explicitly in the
1241 * error path.
1242 */
1243 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1244 unlock_dp_on_error = false;
1245
1246 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1247 &first_block, &free_list, resblks ?
1248 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1249 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001250 ASSERT(error != -ENOSPC);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001251 goto out_trans_abort;
1252 }
1253 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1254 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1255
1256 if (is_dir) {
1257 error = xfs_dir_init(tp, ip, dp);
1258 if (error)
1259 goto out_bmap_cancel;
1260
1261 error = xfs_bumplink(tp, dp);
1262 if (error)
1263 goto out_bmap_cancel;
1264 }
1265
1266 /*
1267 * If this is a synchronous mount, make sure that the
1268 * create transaction goes to disk before returning to
1269 * the user.
1270 */
1271 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1272 xfs_trans_set_sync(tp);
1273
1274 /*
1275 * Attach the dquot(s) to the inodes and modify them incore.
1276 * These ids of the inode couldn't have changed since the new
1277 * inode has been locked ever since it was created.
1278 */
1279 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1280
1281 error = xfs_bmap_finish(&tp, &free_list, &committed);
1282 if (error)
1283 goto out_bmap_cancel;
1284
1285 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1286 if (error)
1287 goto out_release_inode;
1288
1289 xfs_qm_dqrele(udqp);
1290 xfs_qm_dqrele(gdqp);
1291 xfs_qm_dqrele(pdqp);
1292
1293 *ipp = ip;
1294 return 0;
1295
1296 out_bmap_cancel:
1297 xfs_bmap_cancel(&free_list);
1298 out_trans_abort:
1299 cancel_flags |= XFS_TRANS_ABORT;
1300 out_trans_cancel:
1301 xfs_trans_cancel(tp, cancel_flags);
1302 out_release_inode:
1303 /*
1304 * Wait until after the current transaction is aborted to
1305 * release the inode. This prevents recursive transactions
1306 * and deadlocks from xfs_inactive.
1307 */
1308 if (ip)
1309 IRELE(ip);
1310
1311 xfs_qm_dqrele(udqp);
1312 xfs_qm_dqrele(gdqp);
1313 xfs_qm_dqrele(pdqp);
1314
1315 if (unlock_dp_on_error)
1316 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1317 return error;
1318}
1319
1320int
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001321xfs_create_tmpfile(
1322 struct xfs_inode *dp,
1323 struct dentry *dentry,
Brian Foster330033d2014-04-17 08:15:30 +10001324 umode_t mode,
1325 struct xfs_inode **ipp)
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001326{
1327 struct xfs_mount *mp = dp->i_mount;
1328 struct xfs_inode *ip = NULL;
1329 struct xfs_trans *tp = NULL;
1330 int error;
1331 uint cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1332 prid_t prid;
1333 struct xfs_dquot *udqp = NULL;
1334 struct xfs_dquot *gdqp = NULL;
1335 struct xfs_dquot *pdqp = NULL;
1336 struct xfs_trans_res *tres;
1337 uint resblks;
1338
1339 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001340 return -EIO;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001341
1342 prid = xfs_get_initial_prid(dp);
1343
1344 /*
1345 * Make sure that we have allocated dquot(s) on disk.
1346 */
1347 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1348 xfs_kgid_to_gid(current_fsgid()), prid,
1349 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1350 &udqp, &gdqp, &pdqp);
1351 if (error)
1352 return error;
1353
1354 resblks = XFS_IALLOC_SPACE_RES(mp);
1355 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
1356
1357 tres = &M_RES(mp)->tr_create_tmpfile;
1358 error = xfs_trans_reserve(tp, tres, resblks, 0);
Dave Chinner24513372014-06-25 14:58:08 +10001359 if (error == -ENOSPC) {
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001360 /* No space at all so try a "no-allocation" reservation */
1361 resblks = 0;
1362 error = xfs_trans_reserve(tp, tres, 0, 0);
1363 }
1364 if (error) {
1365 cancel_flags = 0;
1366 goto out_trans_cancel;
1367 }
1368
1369 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1370 pdqp, resblks, 1, 0);
1371 if (error)
1372 goto out_trans_cancel;
1373
1374 error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
1375 prid, resblks > 0, &ip, NULL);
1376 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001377 if (error == -ENOSPC)
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001378 goto out_trans_cancel;
1379 goto out_trans_abort;
1380 }
1381
1382 if (mp->m_flags & XFS_MOUNT_WSYNC)
1383 xfs_trans_set_sync(tp);
1384
1385 /*
1386 * Attach the dquot(s) to the inodes and modify them incore.
1387 * These ids of the inode couldn't have changed since the new
1388 * inode has been locked ever since it was created.
1389 */
1390 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1391
1392 ip->i_d.di_nlink--;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001393 error = xfs_iunlink(tp, ip);
1394 if (error)
1395 goto out_trans_abort;
1396
1397 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1398 if (error)
1399 goto out_release_inode;
1400
1401 xfs_qm_dqrele(udqp);
1402 xfs_qm_dqrele(gdqp);
1403 xfs_qm_dqrele(pdqp);
1404
Brian Foster330033d2014-04-17 08:15:30 +10001405 *ipp = ip;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001406 return 0;
1407
1408 out_trans_abort:
1409 cancel_flags |= XFS_TRANS_ABORT;
1410 out_trans_cancel:
1411 xfs_trans_cancel(tp, cancel_flags);
1412 out_release_inode:
1413 /*
1414 * Wait until after the current transaction is aborted to
1415 * release the inode. This prevents recursive transactions
1416 * and deadlocks from xfs_inactive.
1417 */
1418 if (ip)
1419 IRELE(ip);
1420
1421 xfs_qm_dqrele(udqp);
1422 xfs_qm_dqrele(gdqp);
1423 xfs_qm_dqrele(pdqp);
1424
1425 return error;
1426}
1427
1428int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001429xfs_link(
1430 xfs_inode_t *tdp,
1431 xfs_inode_t *sip,
1432 struct xfs_name *target_name)
1433{
1434 xfs_mount_t *mp = tdp->i_mount;
1435 xfs_trans_t *tp;
1436 int error;
1437 xfs_bmap_free_t free_list;
1438 xfs_fsblock_t first_block;
1439 int cancel_flags;
1440 int committed;
1441 int resblks;
1442
1443 trace_xfs_link(tdp, target_name);
1444
1445 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1446
1447 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001448 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001449
1450 error = xfs_qm_dqattach(sip, 0);
1451 if (error)
1452 goto std_return;
1453
1454 error = xfs_qm_dqattach(tdp, 0);
1455 if (error)
1456 goto std_return;
1457
1458 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1459 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1460 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001461 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
Dave Chinner24513372014-06-25 14:58:08 +10001462 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001463 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001464 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001465 }
1466 if (error) {
1467 cancel_flags = 0;
1468 goto error_return;
1469 }
1470
1471 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1472
1473 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1474 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1475
1476 /*
1477 * If we are using project inheritance, we only allow hard link
1478 * creation in our tree when the project IDs are the same; else
1479 * the tree quota mechanism could be circumvented.
1480 */
1481 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1482 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
Dave Chinner24513372014-06-25 14:58:08 +10001483 error = -EXDEV;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001484 goto error_return;
1485 }
1486
Eric Sandeen94f3cad2014-09-09 11:57:52 +10001487 if (!resblks) {
1488 error = xfs_dir_canenter(tp, tdp, target_name);
1489 if (error)
1490 goto error_return;
1491 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001492
1493 xfs_bmap_init(&free_list, &first_block);
1494
Zhi Yong Wuab297432013-12-18 08:22:41 +08001495 if (sip->i_d.di_nlink == 0) {
1496 error = xfs_iunlink_remove(tp, sip);
1497 if (error)
1498 goto abort_return;
1499 }
1500
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001501 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1502 &first_block, &free_list, resblks);
1503 if (error)
1504 goto abort_return;
1505 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1506 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1507
1508 error = xfs_bumplink(tp, sip);
1509 if (error)
1510 goto abort_return;
1511
1512 /*
1513 * If this is a synchronous mount, make sure that the
1514 * link transaction goes to disk before returning to
1515 * the user.
1516 */
1517 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1518 xfs_trans_set_sync(tp);
1519 }
1520
1521 error = xfs_bmap_finish (&tp, &free_list, &committed);
1522 if (error) {
1523 xfs_bmap_cancel(&free_list);
1524 goto abort_return;
1525 }
1526
1527 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1528
1529 abort_return:
1530 cancel_flags |= XFS_TRANS_ABORT;
1531 error_return:
1532 xfs_trans_cancel(tp, cancel_flags);
1533 std_return:
1534 return error;
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001538 * Free up the underlying blocks past new_size. The new size must be smaller
1539 * than the current size. This routine can be used both for the attribute and
1540 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
David Chinnerf6485052008-04-17 16:50:04 +10001542 * The transaction passed to this routine must have made a permanent log
1543 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1544 * given transaction and start new ones, so make sure everything involved in
1545 * the transaction is tidy before calling here. Some transaction will be
1546 * returned to the caller to be committed. The incoming transaction must
1547 * already include the inode, and both inode locks must be held exclusively.
1548 * The inode must also be "held" within the transaction. On return the inode
1549 * will be "held" within the returned transaction. This routine does NOT
1550 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 *
David Chinnerf6485052008-04-17 16:50:04 +10001552 * If we get an error, we must return with the inode locked and linked into the
1553 * current transaction. This keeps things simple for the higher level code,
1554 * because it always knows that the inode is locked and held in the transaction
1555 * that returns to it whether errors occur or not. We don't mark the inode
1556 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
1558int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001559xfs_itruncate_extents(
1560 struct xfs_trans **tpp,
1561 struct xfs_inode *ip,
1562 int whichfork,
1563 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001565 struct xfs_mount *mp = ip->i_mount;
1566 struct xfs_trans *tp = *tpp;
1567 struct xfs_trans *ntp;
1568 xfs_bmap_free_t free_list;
1569 xfs_fsblock_t first_block;
1570 xfs_fileoff_t first_unmap_block;
1571 xfs_fileoff_t last_block;
1572 xfs_filblks_t unmap_len;
1573 int committed;
1574 int error = 0;
1575 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Christoph Hellwig0b561852012-07-04 11:13:31 -04001577 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1578 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1579 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001580 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001581 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001583 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001584 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001586 trace_xfs_itruncate_extents_start(ip, new_size);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Since it is possible for space to become allocated beyond
1590 * the end of the file (in a crash where the space is allocated
1591 * but the inode size is not yet updated), simply remove any
1592 * blocks which show up between the new EOF and the maximum
1593 * possible file size. If the first block to be removed is
1594 * beyond the maximum file size (ie it is the same as last_block),
1595 * then there is nothing to do.
1596 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001597 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001598 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001599 if (first_unmap_block == last_block)
1600 return 0;
1601
1602 ASSERT(first_unmap_block < last_block);
1603 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001605 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001606 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001607 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001608 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001610 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001611 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001612 if (error)
1613 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /*
1616 * Duplicate the transaction that has the permanent
1617 * reservation and commit the old transaction.
1618 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001619 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001620 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001621 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001622 if (error)
1623 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 if (committed) {
1626 /*
David Chinnerf6485052008-04-17 16:50:04 +10001627 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001628 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001630 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
David Chinnerf6485052008-04-17 16:50:04 +10001632
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001633 ntp = xfs_trans_dup(tp);
1634 error = xfs_trans_commit(tp, 0);
1635 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001636
Christoph Hellwigddc34152011-09-19 15:00:54 +00001637 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001638
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001639 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001640 goto out;
1641
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001642 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001643 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001644 * reference that we gained in xfs_trans_dup()
1645 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001646 xfs_log_ticket_put(tp->t_ticket);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001647 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001648 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001651
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001652 /*
1653 * Always re-log the inode so that our permanent transaction can keep
1654 * on rolling it forward in the log.
1655 */
1656 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1657
1658 trace_xfs_itruncate_extents_end(ip, new_size);
1659
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001660out:
1661 *tpp = tp;
1662 return error;
1663out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001665 * If the bunmapi call encounters an error, return to the caller where
1666 * the transaction can be properly aborted. We just need to make sure
1667 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001669 xfs_bmap_cancel(&free_list);
1670 goto out;
1671}
1672
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001673int
1674xfs_release(
1675 xfs_inode_t *ip)
1676{
1677 xfs_mount_t *mp = ip->i_mount;
1678 int error;
1679
1680 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1681 return 0;
1682
1683 /* If this is a read-only mount, don't do this (would generate I/O) */
1684 if (mp->m_flags & XFS_MOUNT_RDONLY)
1685 return 0;
1686
1687 if (!XFS_FORCED_SHUTDOWN(mp)) {
1688 int truncated;
1689
1690 /*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001691 * If we previously truncated this file and removed old data
1692 * in the process, we want to initiate "early" writeout on
1693 * the last close. This is an attempt to combat the notorious
1694 * NULL files problem which is particularly noticeable from a
1695 * truncate down, buffered (re-)write (delalloc), followed by
1696 * a crash. What we are effectively doing here is
1697 * significantly reducing the time window where we'd otherwise
1698 * be exposed to that problem.
1699 */
1700 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1701 if (truncated) {
1702 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
Dave Chinnereac152b2014-08-04 13:22:49 +10001703 if (ip->i_delayed_blks > 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001704 error = filemap_flush(VFS_I(ip)->i_mapping);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001705 if (error)
1706 return error;
1707 }
1708 }
1709 }
1710
1711 if (ip->i_d.di_nlink == 0)
1712 return 0;
1713
1714 if (xfs_can_free_eofblocks(ip, false)) {
1715
1716 /*
1717 * If we can't get the iolock just skip truncating the blocks
1718 * past EOF because we could deadlock with the mmap_sem
1719 * otherwise. We'll get another chance to drop them once the
1720 * last reference to the inode is dropped, so we'll never leak
1721 * blocks permanently.
1722 *
1723 * Further, check if the inode is being opened, written and
1724 * closed frequently and we have delayed allocation blocks
1725 * outstanding (e.g. streaming writes from the NFS server),
1726 * truncating the blocks past EOF will cause fragmentation to
1727 * occur.
1728 *
1729 * In this case don't do the truncation, either, but we have to
1730 * be careful how we detect this case. Blocks beyond EOF show
1731 * up as i_delayed_blks even when the inode is clean, so we
1732 * need to truncate them away first before checking for a dirty
1733 * release. Hence on the first dirty close we will still remove
1734 * the speculative allocation, but after that we will leave it
1735 * in place.
1736 */
1737 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1738 return 0;
1739
1740 error = xfs_free_eofblocks(mp, ip, true);
Dave Chinner24513372014-06-25 14:58:08 +10001741 if (error && error != -EAGAIN)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001742 return error;
1743
1744 /* delalloc blocks after truncation means it really is dirty */
1745 if (ip->i_delayed_blks)
1746 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1747 }
1748 return 0;
1749}
1750
1751/*
Brian Fosterf7be2d72013-09-20 11:06:10 -04001752 * xfs_inactive_truncate
1753 *
1754 * Called to perform a truncate when an inode becomes unlinked.
1755 */
1756STATIC int
1757xfs_inactive_truncate(
1758 struct xfs_inode *ip)
1759{
1760 struct xfs_mount *mp = ip->i_mount;
1761 struct xfs_trans *tp;
1762 int error;
1763
1764 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1765 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1766 if (error) {
1767 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1768 xfs_trans_cancel(tp, 0);
1769 return error;
1770 }
1771
1772 xfs_ilock(ip, XFS_ILOCK_EXCL);
1773 xfs_trans_ijoin(tp, ip, 0);
1774
1775 /*
1776 * Log the inode size first to prevent stale data exposure in the event
1777 * of a system crash before the truncate completes. See the related
1778 * comment in xfs_setattr_size() for details.
1779 */
1780 ip->i_d.di_size = 0;
1781 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1782
1783 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1784 if (error)
1785 goto error_trans_cancel;
1786
1787 ASSERT(ip->i_d.di_nextents == 0);
1788
1789 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1790 if (error)
1791 goto error_unlock;
1792
1793 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1794 return 0;
1795
1796error_trans_cancel:
1797 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1798error_unlock:
1799 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1800 return error;
1801}
1802
1803/*
Brian Foster88877d22013-09-20 11:06:11 -04001804 * xfs_inactive_ifree()
1805 *
1806 * Perform the inode free when an inode is unlinked.
1807 */
1808STATIC int
1809xfs_inactive_ifree(
1810 struct xfs_inode *ip)
1811{
1812 xfs_bmap_free_t free_list;
1813 xfs_fsblock_t first_block;
1814 int committed;
1815 struct xfs_mount *mp = ip->i_mount;
1816 struct xfs_trans *tp;
1817 int error;
1818
1819 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
Brian Foster9d43b182014-04-24 16:00:52 +10001820
1821 /*
1822 * The ifree transaction might need to allocate blocks for record
1823 * insertion to the finobt. We don't want to fail here at ENOSPC, so
1824 * allow ifree to dip into the reserved block pool if necessary.
1825 *
1826 * Freeing large sets of inodes generally means freeing inode chunks,
1827 * directory and file data blocks, so this should be relatively safe.
1828 * Only under severe circumstances should it be possible to free enough
1829 * inodes to exhaust the reserve block pool via finobt expansion while
1830 * at the same time not creating free space in the filesystem.
1831 *
1832 * Send a warning if the reservation does happen to fail, as the inode
1833 * now remains allocated and sits on the unlinked list until the fs is
1834 * repaired.
1835 */
1836 tp->t_flags |= XFS_TRANS_RESERVE;
1837 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
1838 XFS_IFREE_SPACE_RES(mp), 0);
Brian Foster88877d22013-09-20 11:06:11 -04001839 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001840 if (error == -ENOSPC) {
Brian Foster9d43b182014-04-24 16:00:52 +10001841 xfs_warn_ratelimited(mp,
1842 "Failed to remove inode(s) from unlinked list. "
1843 "Please free space, unmount and run xfs_repair.");
1844 } else {
1845 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1846 }
Brian Foster88877d22013-09-20 11:06:11 -04001847 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1848 return error;
1849 }
1850
1851 xfs_ilock(ip, XFS_ILOCK_EXCL);
1852 xfs_trans_ijoin(tp, ip, 0);
1853
1854 xfs_bmap_init(&free_list, &first_block);
1855 error = xfs_ifree(tp, ip, &free_list);
1856 if (error) {
1857 /*
1858 * If we fail to free the inode, shut down. The cancel
1859 * might do that, we need to make sure. Otherwise the
1860 * inode might be lost for a long time or forever.
1861 */
1862 if (!XFS_FORCED_SHUTDOWN(mp)) {
1863 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1864 __func__, error);
1865 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1866 }
1867 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1868 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1869 return error;
1870 }
1871
1872 /*
1873 * Credit the quota account(s). The inode is gone.
1874 */
1875 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1876
1877 /*
1878 * Just ignore errors at this point. There is nothing we can
1879 * do except to try to keep going. Make sure it's not a silent
1880 * error.
1881 */
1882 error = xfs_bmap_finish(&tp, &free_list, &committed);
1883 if (error)
1884 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1885 __func__, error);
1886 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1887 if (error)
1888 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1889 __func__, error);
1890
1891 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1892 return 0;
1893}
1894
1895/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001896 * xfs_inactive
1897 *
1898 * This is called when the vnode reference count for the vnode
1899 * goes to zero. If the file has been unlinked, then it must
1900 * now be truncated. Also, we clear all of the read-ahead state
1901 * kept for the inode here since the file is now closed.
1902 */
Brian Foster74564fb2013-09-20 11:06:12 -04001903void
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001904xfs_inactive(
1905 xfs_inode_t *ip)
1906{
Jie Liu3d3c8b52013-08-12 20:49:59 +10001907 struct xfs_mount *mp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001908 int error;
1909 int truncate = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001910
1911 /*
1912 * If the inode is already free, then there can be nothing
1913 * to clean up here.
1914 */
Ben Myersd9487092013-09-10 18:11:22 -05001915 if (ip->i_d.di_mode == 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001916 ASSERT(ip->i_df.if_real_bytes == 0);
1917 ASSERT(ip->i_df.if_broot_bytes == 0);
Brian Foster74564fb2013-09-20 11:06:12 -04001918 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001919 }
1920
1921 mp = ip->i_mount;
1922
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001923 /* If this is a read-only mount, don't do this (would generate I/O) */
1924 if (mp->m_flags & XFS_MOUNT_RDONLY)
Brian Foster74564fb2013-09-20 11:06:12 -04001925 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001926
1927 if (ip->i_d.di_nlink != 0) {
1928 /*
1929 * force is true because we are evicting an inode from the
1930 * cache. Post-eof blocks must be freed, lest we end up with
1931 * broken free space accounting.
1932 */
Brian Foster74564fb2013-09-20 11:06:12 -04001933 if (xfs_can_free_eofblocks(ip, true))
1934 xfs_free_eofblocks(mp, ip, false);
1935
1936 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001937 }
1938
1939 if (S_ISREG(ip->i_d.di_mode) &&
1940 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1941 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1942 truncate = 1;
1943
1944 error = xfs_qm_dqattach(ip, 0);
1945 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001946 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001947
Brian Fosterf7be2d72013-09-20 11:06:10 -04001948 if (S_ISLNK(ip->i_d.di_mode))
Brian Foster36b21dd2013-09-20 11:06:09 -04001949 error = xfs_inactive_symlink(ip);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001950 else if (truncate)
1951 error = xfs_inactive_truncate(ip);
1952 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001953 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001954
1955 /*
1956 * If there are attributes associated with the file then blow them away
1957 * now. The code calls a routine that recursively deconstructs the
1958 * attribute fork. We need to just commit the current transaction
1959 * because we can't use it for xfs_attr_inactive().
1960 */
1961 if (ip->i_d.di_anextents > 0) {
1962 ASSERT(ip->i_d.di_forkoff != 0);
1963
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001964 error = xfs_attr_inactive(ip);
1965 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001966 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001967 }
1968
1969 if (ip->i_afp)
1970 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1971
1972 ASSERT(ip->i_d.di_anextents == 0);
1973
1974 /*
1975 * Free the inode.
1976 */
Brian Foster88877d22013-09-20 11:06:11 -04001977 error = xfs_inactive_ifree(ip);
1978 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001979 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001980
1981 /*
1982 * Release the dquots held by inode, if any.
1983 */
1984 xfs_qm_dqdetach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001985}
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987/*
1988 * This is called when the inode's link count goes to 0.
1989 * We place the on-disk inode on a list in the AGI. It
1990 * will be pulled from this list when the inode is freed.
1991 */
1992int
1993xfs_iunlink(
1994 xfs_trans_t *tp,
1995 xfs_inode_t *ip)
1996{
1997 xfs_mount_t *mp;
1998 xfs_agi_t *agi;
1999 xfs_dinode_t *dip;
2000 xfs_buf_t *agibp;
2001 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 xfs_agino_t agino;
2003 short bucket_index;
2004 int offset;
2005 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 ASSERT(ip->i_d.di_nlink == 0);
2008 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 mp = tp->t_mountp;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /*
2013 * Get the agi buffer first. It ensures lock ordering
2014 * on the list.
2015 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002016 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10002017 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /*
2022 * Get the index into the agi hash table for the
2023 * list this inode will go on.
2024 */
2025 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2026 ASSERT(agino != 0);
2027 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2028 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002029 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002031 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * There is already another inode in the bucket we need
2034 * to add ourselves to. Add us at the front of the list.
2035 * Here we put the head pointer into our next pointer,
2036 * and then we fall through to point the head at us.
2037 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002038 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2039 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11002040 if (error)
2041 return error;
2042
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002043 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002045 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002047
2048 /* need to recalc the inode CRC if appropriate */
2049 xfs_dinode_calc_crc(mp, dip);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_trans_inode_buf(tp, ibp);
2052 xfs_trans_log_buf(tp, ibp, offset,
2053 (offset + sizeof(xfs_agino_t) - 1));
2054 xfs_inobp_check(mp, ibp);
2055 }
2056
2057 /*
2058 * Point the bucket head pointer at the inode being inserted.
2059 */
2060 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002061 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 offset = offsetof(xfs_agi_t, agi_unlinked) +
2063 (sizeof(xfs_agino_t) * bucket_index);
Dave Chinnerf19b8722015-01-22 09:29:40 +11002064 xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 xfs_trans_log_buf(tp, agibp, offset,
2066 (offset + sizeof(xfs_agino_t) - 1));
2067 return 0;
2068}
2069
2070/*
2071 * Pull the on-disk inode from the AGI unlinked list.
2072 */
2073STATIC int
2074xfs_iunlink_remove(
2075 xfs_trans_t *tp,
2076 xfs_inode_t *ip)
2077{
2078 xfs_ino_t next_ino;
2079 xfs_mount_t *mp;
2080 xfs_agi_t *agi;
2081 xfs_dinode_t *dip;
2082 xfs_buf_t *agibp;
2083 xfs_buf_t *ibp;
2084 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 xfs_agino_t agino;
2086 xfs_agino_t next_agino;
2087 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002088 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002090 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /*
2097 * Get the agi buffer first. It ensures lock ordering
2098 * on the list.
2099 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002100 error = xfs_read_agi(mp, tp, agno, &agibp);
2101 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Get the index into the agi hash table for the
2108 * list this inode will go on.
2109 */
2110 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2111 ASSERT(agino != 0);
2112 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002113 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 ASSERT(agi->agi_unlinked[bucket_index]);
2115
Christoph Hellwig16259e72005-11-02 15:11:25 +11002116 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04002118 * We're at the head of the list. Get the inode's on-disk
2119 * buffer to see if there is anyone after us on the list.
2120 * Only modify our next pointer if it is not already NULLAGINO.
2121 * This saves us the overhead of dealing with the buffer when
2122 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002124 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2125 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002127 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002128 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return error;
2130 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002131 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ASSERT(next_agino != 0);
2133 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002134 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002135 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002137
2138 /* need to recalc the inode CRC if appropriate */
2139 xfs_dinode_calc_crc(mp, dip);
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 xfs_trans_inode_buf(tp, ibp);
2142 xfs_trans_log_buf(tp, ibp, offset,
2143 (offset + sizeof(xfs_agino_t) - 1));
2144 xfs_inobp_check(mp, ibp);
2145 } else {
2146 xfs_trans_brelse(tp, ibp);
2147 }
2148 /*
2149 * Point the bucket head pointer at the next inode.
2150 */
2151 ASSERT(next_agino != 0);
2152 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002153 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 offset = offsetof(xfs_agi_t, agi_unlinked) +
2155 (sizeof(xfs_agino_t) * bucket_index);
Dave Chinnerf19b8722015-01-22 09:29:40 +11002156 xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 xfs_trans_log_buf(tp, agibp, offset,
2158 (offset + sizeof(xfs_agino_t) - 1));
2159 } else {
2160 /*
2161 * We need to search the list for the inode being freed.
2162 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002163 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 last_ibp = NULL;
2165 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002166 struct xfs_imap imap;
2167
2168 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002170
2171 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002173
2174 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002176 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002177 "%s: xfs_imap returned error %d.",
2178 __func__, error);
2179 return error;
2180 }
2181
2182 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2183 &last_ibp, 0, 0);
2184 if (error) {
2185 xfs_warn(mp,
2186 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002187 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return error;
2189 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002190
2191 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002192 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 ASSERT(next_agino != NULLAGINO);
2194 ASSERT(next_agino != 0);
2195 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04002198 * Now last_ibp points to the buffer previous to us on the
2199 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002201 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2202 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002204 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002205 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return error;
2207 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002208 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ASSERT(next_agino != 0);
2210 ASSERT(next_agino != agino);
2211 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002212 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002213 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002215
2216 /* need to recalc the inode CRC if appropriate */
2217 xfs_dinode_calc_crc(mp, dip);
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 xfs_trans_inode_buf(tp, ibp);
2220 xfs_trans_log_buf(tp, ibp, offset,
2221 (offset + sizeof(xfs_agino_t) - 1));
2222 xfs_inobp_check(mp, ibp);
2223 } else {
2224 xfs_trans_brelse(tp, ibp);
2225 }
2226 /*
2227 * Point the previous inode on the list to the next inode.
2228 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002229 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 ASSERT(next_agino != 0);
2231 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002232
2233 /* need to recalc the inode CRC if appropriate */
2234 xfs_dinode_calc_crc(mp, last_dip);
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 xfs_trans_inode_buf(tp, last_ibp);
2237 xfs_trans_log_buf(tp, last_ibp, offset,
2238 (offset + sizeof(xfs_agino_t) - 1));
2239 xfs_inobp_check(mp, last_ibp);
2240 }
2241 return 0;
2242}
2243
Dave Chinner5b3eed72010-08-24 11:42:41 +10002244/*
Zhi Yong Wu0b8182d2013-08-12 03:14:59 +00002245 * A big issue when freeing the inode cluster is that we _cannot_ skip any
Dave Chinner5b3eed72010-08-24 11:42:41 +10002246 * inodes that are in memory - they all must be marked stale and attached to
2247 * the cluster buffer.
2248 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002249STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250xfs_ifree_cluster(
2251 xfs_inode_t *free_ip,
2252 xfs_trans_t *tp,
2253 xfs_ino_t inum)
2254{
2255 xfs_mount_t *mp = free_ip->i_mount;
2256 int blks_per_cluster;
Jie Liu982e9392013-12-13 15:51:49 +11002257 int inodes_per_cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int nbufs;
Dave Chinner5b257b42010-06-03 16:22:29 +10002259 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 xfs_daddr_t blkno;
2261 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10002262 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 xfs_inode_log_item_t *iip;
2264 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00002265 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Dave Chinner5017e972010-01-11 11:47:40 +00002267 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Jie Liu982e9392013-12-13 15:51:49 +11002268 blks_per_cluster = xfs_icluster_size_fsb(mp);
2269 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2270 nbufs = mp->m_ialloc_blks / blks_per_cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Jie Liu982e9392013-12-13 15:51:49 +11002272 for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2274 XFS_INO_TO_AGBNO(mp, inum));
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10002277 * We obtain and lock the backing buffer first in the process
2278 * here, as we have to ensure that any dirty inode that we
2279 * can't get the flush lock on is attached to the buffer.
2280 * If we scan the in-memory inodes first, then buffer IO can
2281 * complete before we get a lock on it, and hence we may fail
2282 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002284 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11002285 mp->m_bsize * blks_per_cluster,
2286 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002288 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +10002289 return -ENOMEM;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002290
2291 /*
2292 * This buffer may not have been correctly initialised as we
2293 * didn't read it from disk. That's not important because we are
2294 * only using to mark the buffer as stale in the log, and to
2295 * attach stale cached inodes on it. That means it will never be
2296 * dispatched for IO. If it is, we want to know about it, and we
2297 * want it to fail. We can acheive this by adding a write
2298 * verifier to the buffer.
2299 */
Dave Chinner1813dd62012-11-14 17:54:40 +11002300 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002301
Dave Chinner5b257b42010-06-03 16:22:29 +10002302 /*
2303 * Walk the inodes already attached to the buffer and mark them
2304 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10002305 * in-memory inode walk can't lock them. By marking them all
2306 * stale first, we will not attempt to lock them in the loop
2307 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10002308 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002309 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 while (lip) {
2311 if (lip->li_type == XFS_LI_INODE) {
2312 iip = (xfs_inode_log_item_t *)lip;
2313 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002314 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11002315 xfs_trans_ail_copy_lsn(mp->m_ail,
2316 &iip->ili_flush_lsn,
2317 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11002318 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 lip = lip->li_bio_list;
2321 }
2322
Dave Chinner5b3eed72010-08-24 11:42:41 +10002323
Dave Chinner5b257b42010-06-03 16:22:29 +10002324 /*
2325 * For each inode in memory attempt to add it to the inode
2326 * buffer and set it up for being staled on buffer IO
2327 * completion. This is safe as we've locked out tail pushing
2328 * and flushing by locking the buffer.
2329 *
2330 * We have already marked every inode that was part of a
2331 * transaction stale above, which means there is no point in
2332 * even trying to lock them.
2333 */
Jie Liu982e9392013-12-13 15:51:49 +11002334 for (i = 0; i < inodes_per_cluster; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10002335retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002336 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002337 ip = radix_tree_lookup(&pag->pag_ici_root,
2338 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002340 /* Inode not in memory, nothing to do */
2341 if (!ip) {
2342 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002343 continue;
2344 }
2345
Dave Chinner5b3eed72010-08-24 11:42:41 +10002346 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002347 * because this is an RCU protected lookup, we could
2348 * find a recently freed or even reallocated inode
2349 * during the lookup. We need to check under the
2350 * i_flags_lock for a valid inode here. Skip it if it
2351 * is not valid, the wrong inode or stale.
2352 */
2353 spin_lock(&ip->i_flags_lock);
2354 if (ip->i_ino != inum + i ||
2355 __xfs_iflags_test(ip, XFS_ISTALE)) {
2356 spin_unlock(&ip->i_flags_lock);
2357 rcu_read_unlock();
2358 continue;
2359 }
2360 spin_unlock(&ip->i_flags_lock);
2361
2362 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10002363 * Don't try to lock/unlock the current inode, but we
2364 * _cannot_ skip the other inodes that we did not find
2365 * in the list attached to the buffer and are not
2366 * already marked stale. If we can't lock it, back off
2367 * and retry.
2368 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002369 if (ip != free_ip &&
2370 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002371 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10002372 delay(1);
2373 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10002374 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002375 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002376
Dave Chinner5b3eed72010-08-24 11:42:41 +10002377 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10002378 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10002379
Dave Chinner5b3eed72010-08-24 11:42:41 +10002380 /*
2381 * we don't need to attach clean inodes or those only
2382 * with unlogged changes (which we throw away, anyway).
2383 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002384 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10002385 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10002386 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 xfs_ifunlock(ip);
2388 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2389 continue;
2390 }
2391
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002392 iip->ili_last_fields = iip->ili_fields;
2393 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11002395 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2396 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002398 xfs_buf_attach_iodone(bp, xfs_istale_done,
2399 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10002400
2401 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
Dave Chinner5b3eed72010-08-24 11:42:41 +10002405 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 xfs_trans_binval(tp, bp);
2407 }
2408
Dave Chinner5017e972010-01-11 11:47:40 +00002409 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
2413/*
2414 * This is called to return an inode to the inode free list.
2415 * The inode should already be truncated to 0 length and have
2416 * no pages associated with it. This routine also assumes that
2417 * the inode is already a part of the transaction.
2418 *
2419 * The on-disk copy of the inode will have been added to the list
2420 * of unlinked inodes in the AGI. We need to remove the inode from
2421 * that list atomically with respect to freeing it here.
2422 */
2423int
2424xfs_ifree(
2425 xfs_trans_t *tp,
2426 xfs_inode_t *ip,
2427 xfs_bmap_free_t *flist)
2428{
2429 int error;
2430 int delete;
2431 xfs_ino_t first_ino;
2432
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002433 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ASSERT(ip->i_d.di_nlink == 0);
2435 ASSERT(ip->i_d.di_nextents == 0);
2436 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002437 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 ASSERT(ip->i_d.di_nblocks == 0);
2439
2440 /*
2441 * Pull the on-disk inode from the AGI unlinked list.
2442 */
2443 error = xfs_iunlink_remove(tp, ip);
Dave Chinner1baaed82013-06-27 16:04:50 +10002444 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
Dave Chinner1baaed82013-06-27 16:04:50 +10002448 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return error;
Dave Chinner1baaed82013-06-27 16:04:50 +10002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 ip->i_d.di_mode = 0; /* mark incore inode as free */
2452 ip->i_d.di_flags = 0;
2453 ip->i_d.di_dmevmask = 0;
2454 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2456 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2457 /*
2458 * Bump the generation count so no one will be confused
2459 * by reincarnations of this inode.
2460 */
2461 ip->i_d.di_gen++;
2462 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2463
Dave Chinner1baaed82013-06-27 16:04:50 +10002464 if (delete)
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002465 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
2470/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002471 * This is called to unpin an inode. The caller must have the inode locked
2472 * in at least shared mode so that the buffer cannot be subsequently pinned
2473 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002475static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002476xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002477 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002478{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002479 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002480
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002481 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2482
David Chinnera3f74ff2008-03-06 13:43:42 +11002483 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002484 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002485
David Chinnera3f74ff2008-03-06 13:43:42 +11002486}
2487
Christoph Hellwigf392e632011-12-18 20:00:10 +00002488static void
2489__xfs_iunpin_wait(
2490 struct xfs_inode *ip)
2491{
2492 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2493 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2494
2495 xfs_iunpin(ip);
2496
2497 do {
2498 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2499 if (xfs_ipincount(ip))
2500 io_schedule();
2501 } while (xfs_ipincount(ip));
2502 finish_wait(wq, &wait.wait);
2503}
2504
Dave Chinner777df5a2010-02-06 12:37:26 +11002505void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002507 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002509 if (xfs_ipincount(ip))
2510 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Dave Chinner27320362013-10-29 22:11:44 +11002513/*
2514 * Removing an inode from the namespace involves removing the directory entry
2515 * and dropping the link count on the inode. Removing the directory entry can
2516 * result in locking an AGF (directory blocks were freed) and removing a link
2517 * count can result in placing the inode on an unlinked list which results in
2518 * locking an AGI.
2519 *
2520 * The big problem here is that we have an ordering constraint on AGF and AGI
2521 * locking - inode allocation locks the AGI, then can allocate a new extent for
2522 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2523 * removes the inode from the unlinked list, requiring that we lock the AGI
2524 * first, and then freeing the inode can result in an inode chunk being freed
2525 * and hence freeing disk space requiring that we lock an AGF.
2526 *
2527 * Hence the ordering that is imposed by other parts of the code is AGI before
2528 * AGF. This means we cannot remove the directory entry before we drop the inode
2529 * reference count and put it on the unlinked list as this results in a lock
2530 * order of AGF then AGI, and this can deadlock against inode allocation and
2531 * freeing. Therefore we must drop the link counts before we remove the
2532 * directory entry.
2533 *
2534 * This is still safe from a transactional point of view - it is not until we
2535 * get to xfs_bmap_finish() that we have the possibility of multiple
2536 * transactions in this operation. Hence as long as we remove the directory
2537 * entry and drop the link count in the first transaction of the remove
2538 * operation, there are no transactional constraints on the ordering here.
2539 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002540int
2541xfs_remove(
2542 xfs_inode_t *dp,
2543 struct xfs_name *name,
2544 xfs_inode_t *ip)
2545{
2546 xfs_mount_t *mp = dp->i_mount;
2547 xfs_trans_t *tp = NULL;
2548 int is_dir = S_ISDIR(ip->i_d.di_mode);
2549 int error = 0;
2550 xfs_bmap_free_t free_list;
2551 xfs_fsblock_t first_block;
2552 int cancel_flags;
2553 int committed;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002554 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002555
2556 trace_xfs_remove(dp, name);
2557
2558 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10002559 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002560
2561 error = xfs_qm_dqattach(dp, 0);
2562 if (error)
2563 goto std_return;
2564
2565 error = xfs_qm_dqattach(ip, 0);
2566 if (error)
2567 goto std_return;
2568
Dave Chinner32296f82014-12-04 09:43:17 +11002569 if (is_dir)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002570 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
Dave Chinner32296f82014-12-04 09:43:17 +11002571 else
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002572 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002573 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2574
2575 /*
2576 * We try to get the real space reservation first,
2577 * allowing for directory btree deletion(s) implying
2578 * possible bmap insert(s). If we can't get the space
2579 * reservation then we use 0 instead, and avoid the bmap
2580 * btree insert(s) in the directory code by, if the bmap
2581 * insert tries to happen, instead trimming the LAST
2582 * block from the directory.
2583 */
2584 resblks = XFS_REMOVE_SPACE_RES(mp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002585 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
Dave Chinner24513372014-06-25 14:58:08 +10002586 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002587 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002588 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002589 }
2590 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002591 ASSERT(error != -ENOSPC);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002592 cancel_flags = 0;
2593 goto out_trans_cancel;
2594 }
2595
2596 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2597
2598 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2599 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2600
2601 /*
2602 * If we're removing a directory perform some additional validation.
2603 */
Dave Chinner27320362013-10-29 22:11:44 +11002604 cancel_flags |= XFS_TRANS_ABORT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002605 if (is_dir) {
2606 ASSERT(ip->i_d.di_nlink >= 2);
2607 if (ip->i_d.di_nlink != 2) {
Dave Chinner24513372014-06-25 14:58:08 +10002608 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002609 goto out_trans_cancel;
2610 }
2611 if (!xfs_dir_isempty(ip)) {
Dave Chinner24513372014-06-25 14:58:08 +10002612 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002613 goto out_trans_cancel;
2614 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002615
Dave Chinner27320362013-10-29 22:11:44 +11002616 /* Drop the link from ip's "..". */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002617 error = xfs_droplink(tp, dp);
2618 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002619 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002620
Dave Chinner27320362013-10-29 22:11:44 +11002621 /* Drop the "." link from ip to self. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002622 error = xfs_droplink(tp, ip);
2623 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002624 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002625 } else {
2626 /*
2627 * When removing a non-directory we need to log the parent
2628 * inode here. For a directory this is done implicitly
2629 * by the xfs_droplink call for the ".." entry.
2630 */
2631 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2632 }
Dave Chinner27320362013-10-29 22:11:44 +11002633 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002634
Dave Chinner27320362013-10-29 22:11:44 +11002635 /* Drop the link from dp to ip. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002636 error = xfs_droplink(tp, ip);
2637 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002638 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002639
Dave Chinner27320362013-10-29 22:11:44 +11002640 xfs_bmap_init(&free_list, &first_block);
2641 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2642 &first_block, &free_list, resblks);
2643 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002644 ASSERT(error != -ENOENT);
Dave Chinner27320362013-10-29 22:11:44 +11002645 goto out_bmap_cancel;
2646 }
2647
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002648 /*
2649 * If this is a synchronous mount, make sure that the
2650 * remove transaction goes to disk before returning to
2651 * the user.
2652 */
2653 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2654 xfs_trans_set_sync(tp);
2655
2656 error = xfs_bmap_finish(&tp, &free_list, &committed);
2657 if (error)
2658 goto out_bmap_cancel;
2659
2660 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2661 if (error)
2662 goto std_return;
2663
Christoph Hellwig2cd2ef62014-04-23 07:11:51 +10002664 if (is_dir && xfs_inode_is_filestream(ip))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002665 xfs_filestream_deassociate(ip);
2666
2667 return 0;
2668
2669 out_bmap_cancel:
2670 xfs_bmap_cancel(&free_list);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002671 out_trans_cancel:
2672 xfs_trans_cancel(tp, cancel_flags);
2673 std_return:
2674 return error;
2675}
2676
Dave Chinnerf6bba202013-08-12 20:49:46 +10002677/*
2678 * Enter all inodes for a rename transaction into a sorted array.
2679 */
2680STATIC void
2681xfs_sort_for_rename(
2682 xfs_inode_t *dp1, /* in: old (source) directory inode */
2683 xfs_inode_t *dp2, /* in: new (target) directory inode */
2684 xfs_inode_t *ip1, /* in: inode of old entry */
2685 xfs_inode_t *ip2, /* in: inode of new entry, if it
2686 already exists, NULL otherwise. */
2687 xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
2688 int *num_inodes) /* out: number of inodes in array */
2689{
2690 xfs_inode_t *temp;
2691 int i, j;
2692
2693 /*
2694 * i_tab contains a list of pointers to inodes. We initialize
2695 * the table here & we'll sort it. We will then use it to
2696 * order the acquisition of the inode locks.
2697 *
2698 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2699 */
2700 i_tab[0] = dp1;
2701 i_tab[1] = dp2;
2702 i_tab[2] = ip1;
2703 if (ip2) {
2704 *num_inodes = 4;
2705 i_tab[3] = ip2;
2706 } else {
2707 *num_inodes = 3;
2708 i_tab[3] = NULL;
2709 }
2710
2711 /*
2712 * Sort the elements via bubble sort. (Remember, there are at
2713 * most 4 elements to sort, so this is adequate.)
2714 */
2715 for (i = 0; i < *num_inodes; i++) {
2716 for (j = 1; j < *num_inodes; j++) {
2717 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2718 temp = i_tab[j];
2719 i_tab[j] = i_tab[j-1];
2720 i_tab[j-1] = temp;
2721 }
2722 }
2723 }
2724}
2725
2726/*
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002727 * xfs_cross_rename()
2728 *
2729 * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
2730 */
2731STATIC int
2732xfs_cross_rename(
2733 struct xfs_trans *tp,
2734 struct xfs_inode *dp1,
2735 struct xfs_name *name1,
2736 struct xfs_inode *ip1,
2737 struct xfs_inode *dp2,
2738 struct xfs_name *name2,
2739 struct xfs_inode *ip2,
2740 struct xfs_bmap_free *free_list,
2741 xfs_fsblock_t *first_block,
2742 int spaceres)
2743{
2744 int error = 0;
2745 int ip1_flags = 0;
2746 int ip2_flags = 0;
2747 int dp2_flags = 0;
2748
2749 /* Swap inode number for dirent in first parent */
2750 error = xfs_dir_replace(tp, dp1, name1,
2751 ip2->i_ino,
2752 first_block, free_list, spaceres);
2753 if (error)
2754 goto out;
2755
2756 /* Swap inode number for dirent in second parent */
2757 error = xfs_dir_replace(tp, dp2, name2,
2758 ip1->i_ino,
2759 first_block, free_list, spaceres);
2760 if (error)
2761 goto out;
2762
2763 /*
2764 * If we're renaming one or more directories across different parents,
2765 * update the respective ".." entries (and link counts) to match the new
2766 * parents.
2767 */
2768 if (dp1 != dp2) {
2769 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2770
2771 if (S_ISDIR(ip2->i_d.di_mode)) {
2772 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2773 dp1->i_ino, first_block,
2774 free_list, spaceres);
2775 if (error)
2776 goto out;
2777
2778 /* transfer ip2 ".." reference to dp1 */
2779 if (!S_ISDIR(ip1->i_d.di_mode)) {
2780 error = xfs_droplink(tp, dp2);
2781 if (error)
2782 goto out;
2783 error = xfs_bumplink(tp, dp1);
2784 if (error)
2785 goto out;
2786 }
2787
2788 /*
2789 * Although ip1 isn't changed here, userspace needs
2790 * to be warned about the change, so that applications
2791 * relying on it (like backup ones), will properly
2792 * notify the change
2793 */
2794 ip1_flags |= XFS_ICHGTIME_CHG;
2795 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2796 }
2797
2798 if (S_ISDIR(ip1->i_d.di_mode)) {
2799 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2800 dp2->i_ino, first_block,
2801 free_list, spaceres);
2802 if (error)
2803 goto out;
2804
2805 /* transfer ip1 ".." reference to dp2 */
2806 if (!S_ISDIR(ip2->i_d.di_mode)) {
2807 error = xfs_droplink(tp, dp1);
2808 if (error)
2809 goto out;
2810 error = xfs_bumplink(tp, dp2);
2811 if (error)
2812 goto out;
2813 }
2814
2815 /*
2816 * Although ip2 isn't changed here, userspace needs
2817 * to be warned about the change, so that applications
2818 * relying on it (like backup ones), will properly
2819 * notify the change
2820 */
2821 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2822 ip2_flags |= XFS_ICHGTIME_CHG;
2823 }
2824 }
2825
2826 if (ip1_flags) {
2827 xfs_trans_ichgtime(tp, ip1, ip1_flags);
2828 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2829 }
2830 if (ip2_flags) {
2831 xfs_trans_ichgtime(tp, ip2, ip2_flags);
2832 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2833 }
2834 if (dp2_flags) {
2835 xfs_trans_ichgtime(tp, dp2, dp2_flags);
2836 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2837 }
2838 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2839 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2840out:
2841 return error;
2842}
2843
2844/*
Dave Chinnerf6bba202013-08-12 20:49:46 +10002845 * xfs_rename
2846 */
2847int
2848xfs_rename(
2849 xfs_inode_t *src_dp,
2850 struct xfs_name *src_name,
2851 xfs_inode_t *src_ip,
2852 xfs_inode_t *target_dp,
2853 struct xfs_name *target_name,
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002854 xfs_inode_t *target_ip,
2855 unsigned int flags)
Dave Chinnerf6bba202013-08-12 20:49:46 +10002856{
2857 xfs_trans_t *tp = NULL;
2858 xfs_mount_t *mp = src_dp->i_mount;
2859 int new_parent; /* moving to a new dir */
2860 int src_is_directory; /* src_name is a directory */
2861 int error;
2862 xfs_bmap_free_t free_list;
2863 xfs_fsblock_t first_block;
2864 int cancel_flags;
2865 int committed;
2866 xfs_inode_t *inodes[4];
2867 int spaceres;
2868 int num_inodes;
2869
2870 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2871
2872 new_parent = (src_dp != target_dp);
2873 src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2874
2875 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
2876 inodes, &num_inodes);
2877
2878 xfs_bmap_init(&free_list, &first_block);
2879 tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2880 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2881 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002882 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
Dave Chinner24513372014-06-25 14:58:08 +10002883 if (error == -ENOSPC) {
Dave Chinnerf6bba202013-08-12 20:49:46 +10002884 spaceres = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002885 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
Dave Chinnerf6bba202013-08-12 20:49:46 +10002886 }
2887 if (error) {
2888 xfs_trans_cancel(tp, 0);
2889 goto std_return;
2890 }
2891
2892 /*
2893 * Attach the dquots to the inodes
2894 */
2895 error = xfs_qm_vop_rename_dqattach(inodes);
2896 if (error) {
2897 xfs_trans_cancel(tp, cancel_flags);
2898 goto std_return;
2899 }
2900
2901 /*
2902 * Lock all the participating inodes. Depending upon whether
2903 * the target_name exists in the target directory, and
2904 * whether the target directory is the same as the source
2905 * directory, we can lock from 2 to 4 inodes.
2906 */
2907 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2908
2909 /*
2910 * Join all the inodes to the transaction. From this point on,
2911 * we can rely on either trans_commit or trans_cancel to unlock
2912 * them.
2913 */
2914 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2915 if (new_parent)
2916 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2917 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2918 if (target_ip)
2919 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2920
2921 /*
2922 * If we are using project inheritance, we only allow renames
2923 * into our tree when the project IDs are the same; else the
2924 * tree quota mechanism would be circumvented.
2925 */
2926 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2927 (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
Dave Chinner24513372014-06-25 14:58:08 +10002928 error = -EXDEV;
Dave Chinnerf6bba202013-08-12 20:49:46 +10002929 goto error_return;
2930 }
2931
2932 /*
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002933 * Handle RENAME_EXCHANGE flags
2934 */
2935 if (flags & RENAME_EXCHANGE) {
2936 error = xfs_cross_rename(tp, src_dp, src_name, src_ip,
2937 target_dp, target_name, target_ip,
2938 &free_list, &first_block, spaceres);
2939 if (error)
2940 goto abort_return;
2941 goto finish_rename;
2942 }
2943
2944 /*
Dave Chinnerf6bba202013-08-12 20:49:46 +10002945 * Set up the target.
2946 */
2947 if (target_ip == NULL) {
2948 /*
2949 * If there's no space reservation, check the entry will
2950 * fit before actually inserting it.
2951 */
Eric Sandeen94f3cad2014-09-09 11:57:52 +10002952 if (!spaceres) {
2953 error = xfs_dir_canenter(tp, target_dp, target_name);
2954 if (error)
2955 goto error_return;
2956 }
Dave Chinnerf6bba202013-08-12 20:49:46 +10002957 /*
2958 * If target does not exist and the rename crosses
2959 * directories, adjust the target directory link count
2960 * to account for the ".." reference from the new entry.
2961 */
2962 error = xfs_dir_createname(tp, target_dp, target_name,
2963 src_ip->i_ino, &first_block,
2964 &free_list, spaceres);
Dave Chinner24513372014-06-25 14:58:08 +10002965 if (error == -ENOSPC)
Dave Chinnerf6bba202013-08-12 20:49:46 +10002966 goto error_return;
2967 if (error)
2968 goto abort_return;
2969
2970 xfs_trans_ichgtime(tp, target_dp,
2971 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2972
2973 if (new_parent && src_is_directory) {
2974 error = xfs_bumplink(tp, target_dp);
2975 if (error)
2976 goto abort_return;
2977 }
2978 } else { /* target_ip != NULL */
2979 /*
2980 * If target exists and it's a directory, check that both
2981 * target and source are directories and that target can be
2982 * destroyed, or that neither is a directory.
2983 */
2984 if (S_ISDIR(target_ip->i_d.di_mode)) {
2985 /*
2986 * Make sure target dir is empty.
2987 */
2988 if (!(xfs_dir_isempty(target_ip)) ||
2989 (target_ip->i_d.di_nlink > 2)) {
Dave Chinner24513372014-06-25 14:58:08 +10002990 error = -EEXIST;
Dave Chinnerf6bba202013-08-12 20:49:46 +10002991 goto error_return;
2992 }
2993 }
2994
2995 /*
2996 * Link the source inode under the target name.
2997 * If the source inode is a directory and we are moving
2998 * it across directories, its ".." entry will be
2999 * inconsistent until we replace that down below.
3000 *
3001 * In case there is already an entry with the same
3002 * name at the destination directory, remove it first.
3003 */
3004 error = xfs_dir_replace(tp, target_dp, target_name,
3005 src_ip->i_ino,
3006 &first_block, &free_list, spaceres);
3007 if (error)
3008 goto abort_return;
3009
3010 xfs_trans_ichgtime(tp, target_dp,
3011 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3012
3013 /*
3014 * Decrement the link count on the target since the target
3015 * dir no longer points to it.
3016 */
3017 error = xfs_droplink(tp, target_ip);
3018 if (error)
3019 goto abort_return;
3020
3021 if (src_is_directory) {
3022 /*
3023 * Drop the link from the old "." entry.
3024 */
3025 error = xfs_droplink(tp, target_ip);
3026 if (error)
3027 goto abort_return;
3028 }
3029 } /* target_ip != NULL */
3030
3031 /*
3032 * Remove the source.
3033 */
3034 if (new_parent && src_is_directory) {
3035 /*
3036 * Rewrite the ".." entry to point to the new
3037 * directory.
3038 */
3039 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3040 target_dp->i_ino,
3041 &first_block, &free_list, spaceres);
Dave Chinner24513372014-06-25 14:58:08 +10003042 ASSERT(error != -EEXIST);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003043 if (error)
3044 goto abort_return;
3045 }
3046
3047 /*
3048 * We always want to hit the ctime on the source inode.
3049 *
3050 * This isn't strictly required by the standards since the source
3051 * inode isn't really being changed, but old unix file systems did
3052 * it and some incremental backup programs won't work without it.
3053 */
3054 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3055 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3056
3057 /*
3058 * Adjust the link count on src_dp. This is necessary when
3059 * renaming a directory, either within one parent when
3060 * the target existed, or across two parent directories.
3061 */
3062 if (src_is_directory && (new_parent || target_ip != NULL)) {
3063
3064 /*
3065 * Decrement link count on src_directory since the
3066 * entry that's moved no longer points to it.
3067 */
3068 error = xfs_droplink(tp, src_dp);
3069 if (error)
3070 goto abort_return;
3071 }
3072
3073 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3074 &first_block, &free_list, spaceres);
3075 if (error)
3076 goto abort_return;
3077
3078 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3079 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3080 if (new_parent)
3081 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3082
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003083finish_rename:
Dave Chinnerf6bba202013-08-12 20:49:46 +10003084 /*
3085 * If this is a synchronous mount, make sure that the
3086 * rename transaction goes to disk before returning to
3087 * the user.
3088 */
3089 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3090 xfs_trans_set_sync(tp);
3091 }
3092
3093 error = xfs_bmap_finish(&tp, &free_list, &committed);
3094 if (error) {
3095 xfs_bmap_cancel(&free_list);
3096 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3097 XFS_TRANS_ABORT));
3098 goto std_return;
3099 }
3100
3101 /*
3102 * trans_commit will unlock src_ip, target_ip & decrement
3103 * the vnode references.
3104 */
3105 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3106
3107 abort_return:
3108 cancel_flags |= XFS_TRANS_ABORT;
3109 error_return:
3110 xfs_bmap_cancel(&free_list);
3111 xfs_trans_cancel(tp, cancel_flags);
3112 std_return:
3113 return error;
3114}
3115
David Chinnerbad55842008-03-06 13:43:49 +11003116STATIC int
3117xfs_iflush_cluster(
3118 xfs_inode_t *ip,
3119 xfs_buf_t *bp)
3120{
3121 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00003122 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11003123 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10003124 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11003125 int ilist_size;
3126 xfs_inode_t **ilist;
3127 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11003128 int nr_found;
3129 int clcount = 0;
3130 int bufwasdelwri;
3131 int i;
3132
Dave Chinner5017e972010-01-11 11:47:40 +00003133 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11003134
Jie Liu0f49efd2013-12-13 15:51:48 +11003135 inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
David Chinnerc8f5f122008-05-20 11:30:15 +10003136 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10003137 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11003138 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00003139 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11003140
Jie Liu0f49efd2013-12-13 15:51:48 +11003141 mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
David Chinnerbad55842008-03-06 13:43:49 +11003142 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11003143 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11003144 /* really need a gang lookup range call here */
3145 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10003146 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11003147 if (nr_found == 0)
3148 goto out_free;
3149
3150 for (i = 0; i < nr_found; i++) {
3151 iq = ilist[i];
3152 if (iq == ip)
3153 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11003154
3155 /*
3156 * because this is an RCU protected lookup, we could find a
3157 * recently freed or even reallocated inode during the lookup.
3158 * We need to check under the i_flags_lock for a valid inode
3159 * here. Skip it if it is not valid or the wrong inode.
3160 */
3161 spin_lock(&ip->i_flags_lock);
3162 if (!ip->i_ino ||
3163 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
3164 spin_unlock(&ip->i_flags_lock);
3165 continue;
3166 }
3167 spin_unlock(&ip->i_flags_lock);
3168
David Chinnerbad55842008-03-06 13:43:49 +11003169 /*
3170 * Do an un-protected check to see if the inode is dirty and
3171 * is a candidate for flushing. These checks will be repeated
3172 * later after the appropriate locks are acquired.
3173 */
David Chinner33540402008-03-06 13:43:59 +11003174 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11003175 continue;
David Chinnerbad55842008-03-06 13:43:49 +11003176
3177 /*
3178 * Try to get locks. If any are unavailable or it is pinned,
3179 * then this inode cannot be flushed and is skipped.
3180 */
3181
3182 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
3183 continue;
3184 if (!xfs_iflock_nowait(iq)) {
3185 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3186 continue;
3187 }
3188 if (xfs_ipincount(iq)) {
3189 xfs_ifunlock(iq);
3190 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3191 continue;
3192 }
3193
3194 /*
3195 * arriving here means that this inode can be flushed. First
3196 * re-check that it's dirty before flushing.
3197 */
David Chinner33540402008-03-06 13:43:59 +11003198 if (!xfs_inode_clean(iq)) {
3199 int error;
David Chinnerbad55842008-03-06 13:43:49 +11003200 error = xfs_iflush_int(iq, bp);
3201 if (error) {
3202 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3203 goto cluster_corrupt_out;
3204 }
3205 clcount++;
3206 } else {
3207 xfs_ifunlock(iq);
3208 }
3209 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3210 }
3211
3212 if (clcount) {
3213 XFS_STATS_INC(xs_icluster_flushcnt);
3214 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3215 }
3216
3217out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11003218 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003219 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00003220out_put:
3221 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11003222 return 0;
3223
3224
3225cluster_corrupt_out:
3226 /*
3227 * Corruption detected in the clustering loop. Invalidate the
3228 * inode buffer and shut down the filesystem.
3229 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11003230 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11003231 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003232 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11003233 * brelse can handle it with no problems. If not, shut down the
3234 * filesystem before releasing the buffer.
3235 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003236 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11003237 if (bufwasdelwri)
3238 xfs_buf_relse(bp);
3239
3240 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3241
3242 if (!bufwasdelwri) {
3243 /*
3244 * Just like incore_relse: if we have b_iodone functions,
3245 * mark the buffer as an error and call them. Otherwise
3246 * mark it as stale and brelse.
3247 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02003248 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11003249 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003250 xfs_buf_stale(bp);
Dave Chinner24513372014-06-25 14:58:08 +10003251 xfs_buf_ioerror(bp, -EIO);
Dave Chinnere8aaba92014-10-02 09:04:22 +10003252 xfs_buf_ioend(bp);
David Chinnerbad55842008-03-06 13:43:49 +11003253 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003254 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11003255 xfs_buf_relse(bp);
3256 }
3257 }
3258
3259 /*
3260 * Unlocks the flush lock
3261 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003262 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003263 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00003264 xfs_perag_put(pag);
Dave Chinner24513372014-06-25 14:58:08 +10003265 return -EFSCORRUPTED;
David Chinnerbad55842008-03-06 13:43:49 +11003266}
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10003269 * Flush dirty inode metadata into the backing buffer.
3270 *
3271 * The caller must have the inode lock and the inode flush lock held. The
3272 * inode lock will still be held upon return to the caller, and the inode
3273 * flush lock will be released after the inode has reached the disk.
3274 *
3275 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 */
3277int
3278xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10003279 struct xfs_inode *ip,
3280 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Christoph Hellwig4c468192012-04-23 15:58:36 +10003282 struct xfs_mount *mp = ip->i_mount;
3283 struct xfs_buf *bp;
3284 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287 XFS_STATS_INC(xs_iflush_count);
3288
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003289 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003290 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003292 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Christoph Hellwig4c468192012-04-23 15:58:36 +10003294 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 xfs_iunpin_wait(ip);
3297
3298 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00003299 * For stale inodes we cannot rely on the backing buffer remaining
3300 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04003301 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00003302 * inodes below. We have to check this after ensuring the inode is
3303 * unpinned so that it is safe to reclaim the stale inode after the
3304 * flush call.
3305 */
3306 if (xfs_iflags_test(ip, XFS_ISTALE)) {
3307 xfs_ifunlock(ip);
3308 return 0;
3309 }
3310
3311 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 * This may have been unpinned because the filesystem is shutting
3313 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003314 * to disk, because the log record didn't make it to disk.
3315 *
3316 * We also have to remove the log item from the AIL in this case,
3317 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
3319 if (XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner24513372014-06-25 14:58:08 +10003320 error = -EIO;
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003321 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323
3324 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003325 * Get the buffer containing the on-disk inode.
3326 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04003327 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3328 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003329 if (error || !bp) {
3330 xfs_ifunlock(ip);
3331 return error;
3332 }
3333
3334 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 * First flush out the inode that xfs_iflush was called with.
3336 */
3337 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11003338 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003342 * If the buffer is pinned then push on the log now so we won't
3343 * get stuck waiting in the write for too long.
3344 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00003345 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003346 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003347
3348 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 * inode clustering:
3350 * see if other inodes can be gathered into this write
3351 */
David Chinnerbad55842008-03-06 13:43:49 +11003352 error = xfs_iflush_cluster(ip, bp);
3353 if (error)
3354 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Christoph Hellwig4c468192012-04-23 15:58:36 +10003356 *bpp = bp;
3357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359corrupt_out:
3360 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10003361 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362cluster_corrupt_out:
Dave Chinner24513372014-06-25 14:58:08 +10003363 error = -EFSCORRUPTED;
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003364abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /*
3366 * Unlocks the flush lock
3367 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003368 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003369 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372STATIC int
3373xfs_iflush_int(
Christoph Hellwig93848a92013-04-03 16:11:17 +11003374 struct xfs_inode *ip,
3375 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Christoph Hellwig93848a92013-04-03 16:11:17 +11003377 struct xfs_inode_log_item *iip = ip->i_itemp;
3378 struct xfs_dinode *dip;
3379 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003381 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003382 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003384 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Christoph Hellwig93848a92013-04-03 16:11:17 +11003385 ASSERT(iip != NULL && iip->ili_fields != 0);
Dave Chinner263997a2014-05-20 07:46:40 +10003386 ASSERT(ip->i_d.di_version > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11003389 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003391 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003393 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3394 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3395 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto corrupt_out;
3397 }
3398 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3399 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003400 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3401 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3402 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 goto corrupt_out;
3404 }
Al Viroabbede12011-07-26 02:31:30 -04003405 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (XFS_TEST_ERROR(
3407 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3408 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3409 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003410 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3411 "%s: Bad regular inode %Lu, ptr 0x%p",
3412 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 goto corrupt_out;
3414 }
Al Viroabbede12011-07-26 02:31:30 -04003415 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (XFS_TEST_ERROR(
3417 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3418 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3419 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3420 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003421 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3422 "%s: Bad directory inode %Lu, ptr 0x%p",
3423 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 goto corrupt_out;
3425 }
3426 }
3427 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3428 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3429 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003430 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3431 "%s: detected corrupt incore inode %Lu, "
3432 "total extents = %d, nblocks = %Ld, ptr 0x%p",
3433 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11003435 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 goto corrupt_out;
3437 }
3438 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3439 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003440 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3441 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3442 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 goto corrupt_out;
3444 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /*
Dave Chinner263997a2014-05-20 07:46:40 +10003447 * Inode item log recovery for v2 inodes are dependent on the
Dave Chinnere60896d2013-07-24 15:47:30 +10003448 * di_flushiter count for correct sequencing. We bump the flush
3449 * iteration count so we can detect flushes which postdate a log record
3450 * during recovery. This is redundant as we now log every change and
3451 * hence this can't happen but we need to still do it to ensure
3452 * backwards compatibility with old kernels that predate logging all
3453 * inode changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 */
Dave Chinnere60896d2013-07-24 15:47:30 +10003455 if (ip->i_d.di_version < 3)
3456 ip->i_d.di_flushiter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 /*
3459 * Copy the dirty parts of the inode into the on-disk
3460 * inode. We always copy out the core of the inode,
3461 * because if the inode is dirty at all the core must
3462 * be.
3463 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003464 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3467 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3468 ip->i_d.di_flushiter = 0;
3469
Eric Sandeenfd9fdba2014-04-14 19:04:46 +10003470 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10003471 if (XFS_IFORK_Q(ip))
Eric Sandeenfd9fdba2014-04-14 19:04:46 +10003472 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 xfs_inobp_check(mp, bp);
3474
3475 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003476 * We've recorded everything logged in the inode, so we'd like to clear
3477 * the ili_fields bits so we don't log and flush things unnecessarily.
3478 * However, we can't stop logging all this information until the data
3479 * we've copied into the disk buffer is written to disk. If we did we
3480 * might overwrite the copy of the inode in the log with all the data
3481 * after re-logging only part of it, and in the face of a crash we
3482 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003484 * What we do is move the bits to the ili_last_fields field. When
3485 * logging the inode, these bits are moved back to the ili_fields field.
3486 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3487 * know that the information those bits represent is permanently on
3488 * disk. As long as the flush completes before the inode is logged
3489 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003491 * We can play with the ili_fields bits here, because the inode lock
3492 * must be held exclusively in order to set bits there and the flush
3493 * lock protects the ili_last_fields bits. Set ili_logged so the flush
3494 * done routine can tell whether or not to look in the AIL. Also, store
3495 * the current LSN of the inode so that we can tell whether the item has
3496 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
3497 * need the AIL lock, because it is a 64 bit value that cannot be read
3498 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11003500 iip->ili_last_fields = iip->ili_fields;
3501 iip->ili_fields = 0;
3502 iip->ili_logged = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Christoph Hellwig93848a92013-04-03 16:11:17 +11003504 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3505 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Christoph Hellwig93848a92013-04-03 16:11:17 +11003507 /*
3508 * Attach the function xfs_iflush_done to the inode's
3509 * buffer. This will remove the inode from the AIL
3510 * and unlock the inode's flush lock when the inode is
3511 * completely written to disk.
3512 */
3513 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Christoph Hellwig93848a92013-04-03 16:11:17 +11003515 /* update the lsn in the on disk inode if required */
3516 if (ip->i_d.di_version == 3)
3517 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Christoph Hellwig93848a92013-04-03 16:11:17 +11003519 /* generate the checksum. */
3520 xfs_dinode_calc_crc(mp, dip);
3521
3522 ASSERT(bp->b_fspriv != NULL);
3523 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return 0;
3525
3526corrupt_out:
Dave Chinner24513372014-06-25 14:58:08 +10003527 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}