blob: d1f76da6f8bfaee95e19321df2d9d8933a1dfa70 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_trans_priv.h"
27#include "xfs_sb.h"
28#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.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"
38#include "xfs_btree.h"
39#include "xfs_alloc.h"
40#include "xfs_ialloc.h"
41#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100045#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100046#include "xfs_vnodeops.h"
Christoph Hellwig93848a92013-04-03 16:11:17 +110047#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000048#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110049#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051kmem_zone_t *xfs_ifork_zone;
52kmem_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 *);
61STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
62STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
63STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
64
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100065/*
66 * helper function to extract extent size hint from inode
67 */
68xfs_extlen_t
69xfs_get_extsz_hint(
70 struct xfs_inode *ip)
71{
72 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
73 return ip->i_d.di_extsize;
74 if (XFS_IS_REALTIME_INODE(ip))
75 return ip->i_mount->m_sb.sb_rextsize;
76 return 0;
77}
78
Dave Chinnerfa96aca2012-10-08 21:56:10 +110079/*
80 * This is a wrapper routine around the xfs_ilock() routine used to centralize
81 * some grungy code. It is used in places that wish to lock the inode solely
82 * for reading the extents. The reason these places can't just call
83 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
84 * extents from disk for a file in b-tree format. If the inode is in b-tree
85 * format, then we need to lock the inode exclusively until the extents are read
86 * in. Locking it exclusively all the time would limit our parallelism
87 * unnecessarily, though. What we do instead is check to see if the extents
88 * have been read in yet, and only lock the inode exclusively if they have not.
89 *
90 * The function returns a value which should be given to the corresponding
91 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
92 * actually taken.
93 */
94uint
95xfs_ilock_map_shared(
96 xfs_inode_t *ip)
97{
98 uint lock_mode;
99
100 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
101 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
102 lock_mode = XFS_ILOCK_EXCL;
103 } else {
104 lock_mode = XFS_ILOCK_SHARED;
105 }
106
107 xfs_ilock(ip, lock_mode);
108
109 return lock_mode;
110}
111
112/*
113 * This is simply the unlock routine to go with xfs_ilock_map_shared().
114 * All it does is call xfs_iunlock() with the given lock_mode.
115 */
116void
117xfs_iunlock_map_shared(
118 xfs_inode_t *ip,
119 unsigned int lock_mode)
120{
121 xfs_iunlock(ip, lock_mode);
122}
123
124/*
125 * The xfs inode contains 2 locks: a multi-reader lock called the
126 * i_iolock and a multi-reader lock called the i_lock. This routine
127 * allows either or both of the locks to be obtained.
128 *
129 * The 2 locks should always be ordered so that the IO lock is
130 * obtained first in order to prevent deadlock.
131 *
132 * ip -- the inode being locked
133 * lock_flags -- this parameter indicates the inode's locks
134 * to be locked. It can be:
135 * XFS_IOLOCK_SHARED,
136 * XFS_IOLOCK_EXCL,
137 * XFS_ILOCK_SHARED,
138 * XFS_ILOCK_EXCL,
139 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
140 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
141 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
142 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
143 */
144void
145xfs_ilock(
146 xfs_inode_t *ip,
147 uint lock_flags)
148{
149 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
150
151 /*
152 * You can't set both SHARED and EXCL for the same lock,
153 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
154 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
155 */
156 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
157 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
158 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
159 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
160 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
161
162 if (lock_flags & XFS_IOLOCK_EXCL)
163 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
164 else if (lock_flags & XFS_IOLOCK_SHARED)
165 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
166
167 if (lock_flags & XFS_ILOCK_EXCL)
168 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
169 else if (lock_flags & XFS_ILOCK_SHARED)
170 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
171}
172
173/*
174 * This is just like xfs_ilock(), except that the caller
175 * is guaranteed not to sleep. It returns 1 if it gets
176 * the requested locks and 0 otherwise. If the IO lock is
177 * obtained but the inode lock cannot be, then the IO lock
178 * is dropped before returning.
179 *
180 * ip -- the inode being locked
181 * lock_flags -- this parameter indicates the inode's locks to be
182 * to be locked. See the comment for xfs_ilock() for a list
183 * of valid values.
184 */
185int
186xfs_ilock_nowait(
187 xfs_inode_t *ip,
188 uint lock_flags)
189{
190 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
191
192 /*
193 * You can't set both SHARED and EXCL for the same lock,
194 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
195 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
196 */
197 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
198 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
199 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
200 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
201 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
202
203 if (lock_flags & XFS_IOLOCK_EXCL) {
204 if (!mrtryupdate(&ip->i_iolock))
205 goto out;
206 } else if (lock_flags & XFS_IOLOCK_SHARED) {
207 if (!mrtryaccess(&ip->i_iolock))
208 goto out;
209 }
210 if (lock_flags & XFS_ILOCK_EXCL) {
211 if (!mrtryupdate(&ip->i_lock))
212 goto out_undo_iolock;
213 } else if (lock_flags & XFS_ILOCK_SHARED) {
214 if (!mrtryaccess(&ip->i_lock))
215 goto out_undo_iolock;
216 }
217 return 1;
218
219 out_undo_iolock:
220 if (lock_flags & XFS_IOLOCK_EXCL)
221 mrunlock_excl(&ip->i_iolock);
222 else if (lock_flags & XFS_IOLOCK_SHARED)
223 mrunlock_shared(&ip->i_iolock);
224 out:
225 return 0;
226}
227
228/*
229 * xfs_iunlock() is used to drop the inode locks acquired with
230 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
231 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
232 * that we know which locks to drop.
233 *
234 * ip -- the inode being unlocked
235 * lock_flags -- this parameter indicates the inode's locks to be
236 * to be unlocked. See the comment for xfs_ilock() for a list
237 * of valid values for this parameter.
238 *
239 */
240void
241xfs_iunlock(
242 xfs_inode_t *ip,
243 uint lock_flags)
244{
245 /*
246 * You can't set both SHARED and EXCL for the same lock,
247 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
248 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
249 */
250 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
251 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
252 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
253 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
254 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
255 ASSERT(lock_flags != 0);
256
257 if (lock_flags & XFS_IOLOCK_EXCL)
258 mrunlock_excl(&ip->i_iolock);
259 else if (lock_flags & XFS_IOLOCK_SHARED)
260 mrunlock_shared(&ip->i_iolock);
261
262 if (lock_flags & XFS_ILOCK_EXCL)
263 mrunlock_excl(&ip->i_lock);
264 else if (lock_flags & XFS_ILOCK_SHARED)
265 mrunlock_shared(&ip->i_lock);
266
267 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
268}
269
270/*
271 * give up write locks. the i/o lock cannot be held nested
272 * if it is being demoted.
273 */
274void
275xfs_ilock_demote(
276 xfs_inode_t *ip,
277 uint lock_flags)
278{
279 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
280 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
281
282 if (lock_flags & XFS_ILOCK_EXCL)
283 mrdemote(&ip->i_lock);
284 if (lock_flags & XFS_IOLOCK_EXCL)
285 mrdemote(&ip->i_iolock);
286
287 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
288}
289
Dave Chinner742ae1e2013-04-30 21:39:34 +1000290#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100291int
292xfs_isilocked(
293 xfs_inode_t *ip,
294 uint lock_flags)
295{
296 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
297 if (!(lock_flags & XFS_ILOCK_SHARED))
298 return !!ip->i_lock.mr_writer;
299 return rwsem_is_locked(&ip->i_lock.mr_lock);
300 }
301
302 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
303 if (!(lock_flags & XFS_IOLOCK_SHARED))
304 return !!ip->i_iolock.mr_writer;
305 return rwsem_is_locked(&ip->i_iolock.mr_lock);
306 }
307
308 ASSERT(0);
309 return 0;
310}
311#endif
312
313void
314__xfs_iflock(
315 struct xfs_inode *ip)
316{
317 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
318 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
319
320 do {
321 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
322 if (xfs_isiflocked(ip))
323 io_schedule();
324 } while (!xfs_iflock_nowait(ip));
325
326 finish_wait(wq, &wait.wait);
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#ifdef DEBUG
330/*
331 * Make sure that the extents in the given memory buffer
332 * are valid.
333 */
334STATIC void
335xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100336 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_exntfmt_t fmt)
339{
340 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000341 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int i;
343
344 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000345 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
346 rec.l0 = get_unaligned(&ep->l0);
347 rec.l1 = get_unaligned(&ep->l1);
348 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (fmt == XFS_EXTFMT_NOSTATE)
350 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352}
353#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000354#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#endif /* DEBUG */
356
357/*
358 * Check that none of the inode's in the buffer have a next
359 * unlinked field of 0.
360 */
361#if defined(DEBUG)
362void
363xfs_inobp_check(
364 xfs_mount_t *mp,
365 xfs_buf_t *bp)
366{
367 int i;
368 int j;
369 xfs_dinode_t *dip;
370
371 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
372
373 for (i = 0; i < j; i++) {
374 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
375 i * mp->m_sb.sb_inodesize);
376 if (!dip->di_next_unlinked) {
Dave Chinner53487782011-03-07 10:05:35 +1100377 xfs_alert(mp,
378 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 bp);
380 ASSERT(dip->di_next_unlinked);
381 }
382 }
383}
384#endif
385
Dave Chinner612cfbf2012-11-14 17:52:32 +1100386static void
Dave Chinneraf133e82012-11-12 22:54:07 +1100387xfs_inode_buf_verify(
388 struct xfs_buf *bp)
389{
390 struct xfs_mount *mp = bp->b_target->bt_mount;
391 int i;
392 int ni;
393
394 /*
395 * Validate the magic number and version of every inode in the buffer
396 */
397 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
398 for (i = 0; i < ni; i++) {
399 int di_ok;
400 xfs_dinode_t *dip;
401
402 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
403 (i << mp->m_sb.sb_inodelog));
404 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
405 XFS_DINODE_GOOD_VERSION(dip->di_version);
406 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
407 XFS_ERRTAG_ITOBP_INOTOBP,
408 XFS_RANDOM_ITOBP_INOTOBP))) {
409 xfs_buf_ioerror(bp, EFSCORRUPTED);
410 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
411 mp, dip);
412#ifdef DEBUG
413 xfs_emerg(mp,
414 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
415 (unsigned long long)bp->b_bn, i,
416 be16_to_cpu(dip->di_magic));
417 ASSERT(0);
418#endif
419 }
420 }
421 xfs_inobp_check(mp, bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100422}
423
Dave Chinner1813dd62012-11-14 17:54:40 +1100424
425static void
426xfs_inode_buf_read_verify(
427 struct xfs_buf *bp)
428{
429 xfs_inode_buf_verify(bp);
430}
431
432static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100433xfs_inode_buf_write_verify(
434 struct xfs_buf *bp)
435{
436 xfs_inode_buf_verify(bp);
437}
438
Dave Chinner1813dd62012-11-14 17:54:40 +1100439const struct xfs_buf_ops xfs_inode_buf_ops = {
440 .verify_read = xfs_inode_buf_read_verify,
441 .verify_write = xfs_inode_buf_write_verify,
442};
443
Dave Chinneraf133e82012-11-12 22:54:07 +1100444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/*
Christoph Hellwig475ee412012-07-03 12:21:22 -0400446 * This routine is called to map an inode to the buffer containing the on-disk
447 * version of the inode. It returns a pointer to the buffer containing the
448 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
449 * pointer to the on-disk inode within that buffer.
450 *
451 * If a non-zero error is returned, then the contents of bpp and dipp are
452 * undefined.
David Chinner4ae29b42008-03-06 13:43:34 +1100453 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400454int
David Chinner4ae29b42008-03-06 13:43:34 +1100455xfs_imap_to_bp(
Christoph Hellwig475ee412012-07-03 12:21:22 -0400456 struct xfs_mount *mp,
457 struct xfs_trans *tp,
458 struct xfs_imap *imap,
Dave Chinneraf133e82012-11-12 22:54:07 +1100459 struct xfs_dinode **dipp,
Christoph Hellwig475ee412012-07-03 12:21:22 -0400460 struct xfs_buf **bpp,
461 uint buf_flags,
462 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100463{
Christoph Hellwig475ee412012-07-03 12:21:22 -0400464 struct xfs_buf *bp;
465 int error;
David Chinner4ae29b42008-03-06 13:43:34 +1100466
Dave Chinner611c9942012-04-23 15:59:07 +1000467 buf_flags |= XBF_UNMAPPED;
David Chinner4ae29b42008-03-06 13:43:34 +1100468 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
Dave Chinneraf133e82012-11-12 22:54:07 +1100469 (int)imap->im_len, buf_flags, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100470 &xfs_inode_buf_ops);
David Chinner4ae29b42008-03-06 13:43:34 +1100471 if (error) {
Dave Chinneraf133e82012-11-12 22:54:07 +1100472 if (error == EAGAIN) {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000473 ASSERT(buf_flags & XBF_TRYLOCK);
Dave Chinneraf133e82012-11-12 22:54:07 +1100474 return error;
David Chinnera3f74ff2008-03-06 13:43:42 +1100475 }
Dave Chinneraf133e82012-11-12 22:54:07 +1100476
477 if (error == EFSCORRUPTED &&
478 (iget_flags & XFS_IGET_UNTRUSTED))
479 return XFS_ERROR(EINVAL);
480
481 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
482 __func__, error);
David Chinner4ae29b42008-03-06 13:43:34 +1100483 return error;
484 }
485
David Chinner4ae29b42008-03-06 13:43:34 +1100486 *bpp = bp;
Christoph Hellwig475ee412012-07-03 12:21:22 -0400487 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
David Chinner4ae29b42008-03-06 13:43:34 +1100488 return 0;
489}
490
491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Move inode type and inode format specific information from the
493 * on-disk inode to the in-core inode. For fifos, devs, and sockets
494 * this means set if_rdev to the proper value. For files, directories,
495 * and symlinks this means to bring in the in-line data or extent
496 * pointers. For a file in B-tree format, only the root is immediately
497 * brought in-core. The rest will be in-lined in if_extents when it
498 * is first referenced (see xfs_iread_extents()).
499 */
500STATIC int
501xfs_iformat(
502 xfs_inode_t *ip,
503 xfs_dinode_t *dip)
504{
505 xfs_attr_shortform_t *atp;
506 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000507 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100510 if (unlikely(be32_to_cpu(dip->di_nextents) +
511 be16_to_cpu(dip->di_anextents) >
512 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100513 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100514 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100516 (int)(be32_to_cpu(dip->di_nextents) +
517 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100519 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
521 ip->i_mount, dip);
522 return XFS_ERROR(EFSCORRUPTED);
523 }
524
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100525 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100526 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100528 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
530 ip->i_mount, dip);
531 return XFS_ERROR(EFSCORRUPTED);
532 }
533
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300534 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
535 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100536 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300537 "corrupt dinode %Lu, has realtime flag set.",
538 ip->i_ino);
539 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
540 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
541 return XFS_ERROR(EFSCORRUPTED);
542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (ip->i_d.di_mode & S_IFMT) {
545 case S_IFIFO:
546 case S_IFCHR:
547 case S_IFBLK:
548 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100549 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
551 ip->i_mount, dip);
552 return XFS_ERROR(EFSCORRUPTED);
553 }
554 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100555 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557
558 case S_IFREG:
559 case S_IFLNK:
560 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100561 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case XFS_DINODE_FMT_LOCAL:
563 /*
564 * no local regular files yet
565 */
Al Viroabbede12011-07-26 02:31:30 -0400566 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100567 xfs_warn(ip->i_mount,
568 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 (unsigned long long) ip->i_ino);
570 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
571 XFS_ERRLEVEL_LOW,
572 ip->i_mount, dip);
573 return XFS_ERROR(EFSCORRUPTED);
574 }
575
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100576 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100578 xfs_warn(ip->i_mount,
579 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 (unsigned long long) ip->i_ino,
581 (long long) di_size);
582 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
583 XFS_ERRLEVEL_LOW,
584 ip->i_mount, dip);
585 return XFS_ERROR(EFSCORRUPTED);
586 }
587
588 size = (int)di_size;
589 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
590 break;
591 case XFS_DINODE_FMT_EXTENTS:
592 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
593 break;
594 case XFS_DINODE_FMT_BTREE:
595 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
596 break;
597 default:
598 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
599 ip->i_mount);
600 return XFS_ERROR(EFSCORRUPTED);
601 }
602 break;
603
604 default:
605 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
606 return XFS_ERROR(EFSCORRUPTED);
607 }
608 if (error) {
609 return error;
610 }
611 if (!XFS_DFORK_Q(dip))
612 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000615 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000616
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100617 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 case XFS_DINODE_FMT_LOCAL:
619 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100620 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100621
622 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100623 xfs_warn(ip->i_mount,
624 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100625 (unsigned long long) ip->i_ino,
626 (long long) size);
627 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
628 XFS_ERRLEVEL_LOW,
629 ip->i_mount, dip);
630 return XFS_ERROR(EFSCORRUPTED);
631 }
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
634 break;
635 case XFS_DINODE_FMT_EXTENTS:
636 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
637 break;
638 case XFS_DINODE_FMT_BTREE:
639 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
640 break;
641 default:
642 error = XFS_ERROR(EFSCORRUPTED);
643 break;
644 }
645 if (error) {
646 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
647 ip->i_afp = NULL;
648 xfs_idestroy_fork(ip, XFS_DATA_FORK);
649 }
650 return error;
651}
652
653/*
654 * The file is in-lined in the on-disk inode.
655 * If it fits into if_inline_data, then copy
656 * it there, otherwise allocate a buffer for it
657 * and copy the data there. Either way, set
658 * if_data to point at the data.
659 * If we allocate a buffer for the data, make
660 * sure that its size is a multiple of 4 and
661 * record the real size in i_real_bytes.
662 */
663STATIC int
664xfs_iformat_local(
665 xfs_inode_t *ip,
666 xfs_dinode_t *dip,
667 int whichfork,
668 int size)
669{
670 xfs_ifork_t *ifp;
671 int real_size;
672
673 /*
674 * If the size is unreasonable, then something
675 * is wrong and we just bail out rather than crash in
676 * kmem_alloc() or memcpy() below.
677 */
678 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100679 xfs_warn(ip->i_mount,
680 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 (unsigned long long) ip->i_ino, size,
682 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
683 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
684 ip->i_mount, dip);
685 return XFS_ERROR(EFSCORRUPTED);
686 }
687 ifp = XFS_IFORK_PTR(ip, whichfork);
688 real_size = 0;
689 if (size == 0)
690 ifp->if_u1.if_data = NULL;
691 else if (size <= sizeof(ifp->if_u2.if_inline_data))
692 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
693 else {
694 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000695 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 ifp->if_bytes = size;
698 ifp->if_real_bytes = real_size;
699 if (size)
700 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
701 ifp->if_flags &= ~XFS_IFEXTENTS;
702 ifp->if_flags |= XFS_IFINLINE;
703 return 0;
704}
705
706/*
707 * The file consists of a set of extents all
708 * of which fit into the on-disk inode.
709 * If there are few enough extents to fit into
710 * the if_inline_ext, then copy them there.
711 * Otherwise allocate a buffer for them and copy
712 * them into it. Either way, set if_extents
713 * to point at the extents.
714 */
715STATIC int
716xfs_iformat_extents(
717 xfs_inode_t *ip,
718 xfs_dinode_t *dip,
719 int whichfork)
720{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000721 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_ifork_t *ifp;
723 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int size;
725 int i;
726
727 ifp = XFS_IFORK_PTR(ip, whichfork);
728 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
729 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
730
731 /*
732 * If the number of extents is unreasonable, then something
733 * is wrong and we just bail out rather than crash in
734 * kmem_alloc() or memcpy() below.
735 */
736 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100737 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 (unsigned long long) ip->i_ino, nex);
739 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
740 ip->i_mount, dip);
741 return XFS_ERROR(EFSCORRUPTED);
742 }
743
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100744 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (nex == 0)
746 ifp->if_u1.if_extents = NULL;
747 else if (nex <= XFS_INLINE_EXTS)
748 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100749 else
750 xfs_iext_add(ifp, 0, nex);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (size) {
754 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000755 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100756 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000757 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000758 ep->l0 = get_unaligned_be64(&dp->l0);
759 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000761 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (whichfork != XFS_DATA_FORK ||
763 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
764 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100765 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
767 XFS_ERRLEVEL_LOW,
768 ip->i_mount);
769 return XFS_ERROR(EFSCORRUPTED);
770 }
771 }
772 ifp->if_flags |= XFS_IFEXTENTS;
773 return 0;
774}
775
776/*
777 * The file has too many extents to fit into
778 * the inode, so they are in B-tree format.
779 * Allocate a buffer for the root of the B-tree
780 * and copy the root into it. The i_extents
781 * field will remain NULL until all of the
782 * extents are read in (when they are needed).
783 */
784STATIC int
785xfs_iformat_btree(
786 xfs_inode_t *ip,
787 xfs_dinode_t *dip,
788 int whichfork)
789{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500790 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 xfs_bmdr_block_t *dfp;
792 xfs_ifork_t *ifp;
793 /* REFERENCED */
794 int nrecs;
795 int size;
796
797 ifp = XFS_IFORK_PTR(ip, whichfork);
798 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500799 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100800 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /*
803 * blow out if -- fork has less extents than can fit in
804 * fork (fork shouldn't be a btree format), root btree
805 * block has more records than can fit into the fork,
806 * or the number of extents is greater than the number of
807 * blocks.
808 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000809 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500810 XFS_IFORK_MAXEXT(ip, whichfork) ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000811 XFS_BMDR_SPACE_CALC(nrecs) >
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500812 XFS_DFORK_SIZE(dip, mp, whichfork) ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000813 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500814 xfs_warn(mp, "corrupt inode %Lu (btree).",
815 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100816 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500817 mp, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return XFS_ERROR(EFSCORRUPTED);
819 }
820
821 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000822 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ASSERT(ifp->if_broot != NULL);
824 /*
825 * Copy and convert from the on-disk structure
826 * to the in-memory structure.
827 */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500828 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
Christoph Hellwig60197e82008-10-30 17:11:19 +1100829 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 ifp->if_flags &= ~XFS_IFEXTENTS;
831 ifp->if_flags |= XFS_IFBROOT;
832
833 return 0;
834}
835
Eric Sandeend96f8f82009-07-02 00:09:33 -0500836STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000837xfs_dinode_from_disk(
838 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100839 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000841 to->di_magic = be16_to_cpu(from->di_magic);
842 to->di_mode = be16_to_cpu(from->di_mode);
843 to->di_version = from ->di_version;
844 to->di_format = from->di_format;
845 to->di_onlink = be16_to_cpu(from->di_onlink);
846 to->di_uid = be32_to_cpu(from->di_uid);
847 to->di_gid = be32_to_cpu(from->di_gid);
848 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000849 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
850 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000851 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
852 to->di_flushiter = be16_to_cpu(from->di_flushiter);
853 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
854 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
855 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
856 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
857 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
858 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
859 to->di_size = be64_to_cpu(from->di_size);
860 to->di_nblocks = be64_to_cpu(from->di_nblocks);
861 to->di_extsize = be32_to_cpu(from->di_extsize);
862 to->di_nextents = be32_to_cpu(from->di_nextents);
863 to->di_anextents = be16_to_cpu(from->di_anextents);
864 to->di_forkoff = from->di_forkoff;
865 to->di_aformat = from->di_aformat;
866 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
867 to->di_dmstate = be16_to_cpu(from->di_dmstate);
868 to->di_flags = be16_to_cpu(from->di_flags);
869 to->di_gen = be32_to_cpu(from->di_gen);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100870
871 if (to->di_version == 3) {
872 to->di_changecount = be64_to_cpu(from->di_changecount);
873 to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
874 to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
875 to->di_flags2 = be64_to_cpu(from->di_flags2);
876 to->di_ino = be64_to_cpu(from->di_ino);
877 to->di_lsn = be64_to_cpu(from->di_lsn);
878 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
879 uuid_copy(&to->di_uuid, &from->di_uuid);
880 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000883void
884xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100885 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000886 xfs_icdinode_t *from)
887{
888 to->di_magic = cpu_to_be16(from->di_magic);
889 to->di_mode = cpu_to_be16(from->di_mode);
890 to->di_version = from ->di_version;
891 to->di_format = from->di_format;
892 to->di_onlink = cpu_to_be16(from->di_onlink);
893 to->di_uid = cpu_to_be32(from->di_uid);
894 to->di_gid = cpu_to_be32(from->di_gid);
895 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000896 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
897 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000898 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
899 to->di_flushiter = cpu_to_be16(from->di_flushiter);
900 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
901 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
902 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
903 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
904 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
905 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
906 to->di_size = cpu_to_be64(from->di_size);
907 to->di_nblocks = cpu_to_be64(from->di_nblocks);
908 to->di_extsize = cpu_to_be32(from->di_extsize);
909 to->di_nextents = cpu_to_be32(from->di_nextents);
910 to->di_anextents = cpu_to_be16(from->di_anextents);
911 to->di_forkoff = from->di_forkoff;
912 to->di_aformat = from->di_aformat;
913 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
914 to->di_dmstate = cpu_to_be16(from->di_dmstate);
915 to->di_flags = cpu_to_be16(from->di_flags);
916 to->di_gen = cpu_to_be32(from->di_gen);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100917
918 if (from->di_version == 3) {
919 to->di_changecount = cpu_to_be64(from->di_changecount);
920 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
921 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
922 to->di_flags2 = cpu_to_be64(from->di_flags2);
923 to->di_ino = cpu_to_be64(from->di_ino);
924 to->di_lsn = cpu_to_be64(from->di_lsn);
925 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
926 uuid_copy(&to->di_uuid, &from->di_uuid);
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930STATIC uint
931_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 __uint16_t di_flags)
933{
934 uint flags = 0;
935
936 if (di_flags & XFS_DIFLAG_ANY) {
937 if (di_flags & XFS_DIFLAG_REALTIME)
938 flags |= XFS_XFLAG_REALTIME;
939 if (di_flags & XFS_DIFLAG_PREALLOC)
940 flags |= XFS_XFLAG_PREALLOC;
941 if (di_flags & XFS_DIFLAG_IMMUTABLE)
942 flags |= XFS_XFLAG_IMMUTABLE;
943 if (di_flags & XFS_DIFLAG_APPEND)
944 flags |= XFS_XFLAG_APPEND;
945 if (di_flags & XFS_DIFLAG_SYNC)
946 flags |= XFS_XFLAG_SYNC;
947 if (di_flags & XFS_DIFLAG_NOATIME)
948 flags |= XFS_XFLAG_NOATIME;
949 if (di_flags & XFS_DIFLAG_NODUMP)
950 flags |= XFS_XFLAG_NODUMP;
951 if (di_flags & XFS_DIFLAG_RTINHERIT)
952 flags |= XFS_XFLAG_RTINHERIT;
953 if (di_flags & XFS_DIFLAG_PROJINHERIT)
954 flags |= XFS_XFLAG_PROJINHERIT;
955 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
956 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100957 if (di_flags & XFS_DIFLAG_EXTSIZE)
958 flags |= XFS_XFLAG_EXTSIZE;
959 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
960 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000961 if (di_flags & XFS_DIFLAG_NODEFRAG)
962 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000963 if (di_flags & XFS_DIFLAG_FILESTREAM)
964 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
967 return flags;
968}
969
970uint
971xfs_ip2xflags(
972 xfs_inode_t *ip)
973{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000974 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Nathan Scotta916e2b2006-06-09 17:12:17 +1000976 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100977 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980uint
981xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100982 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100984 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100985 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Christoph Hellwig93848a92013-04-03 16:11:17 +1100988static bool
989xfs_dinode_verify(
990 struct xfs_mount *mp,
991 struct xfs_inode *ip,
992 struct xfs_dinode *dip)
993{
994 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
995 return false;
996
997 /* only version 3 or greater inodes are extensively verified here */
998 if (dip->di_version < 3)
999 return true;
1000
1001 if (!xfs_sb_version_hascrc(&mp->m_sb))
1002 return false;
1003 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
1004 offsetof(struct xfs_dinode, di_crc)))
1005 return false;
1006 if (be64_to_cpu(dip->di_ino) != ip->i_ino)
1007 return false;
1008 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
1009 return false;
1010 return true;
1011}
1012
1013void
1014xfs_dinode_calc_crc(
1015 struct xfs_mount *mp,
1016 struct xfs_dinode *dip)
1017{
1018 __uint32_t crc;
1019
1020 if (dip->di_version < 3)
1021 return;
1022
1023 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
1024 crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
1025 offsetof(struct xfs_dinode, di_crc));
1026 dip->di_crc = xfs_end_cksum(crc);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001030 * Read the disk inode attributes into the in-core inode structure.
Dave Chinnercca9f932013-06-27 16:04:49 +10001031 *
1032 * If we are initialising a new inode and we are not utilising the
1033 * XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new inode core
1034 * with a random generation number. If we are keeping inodes around, we need to
1035 * read the inode cluster to get the existing generation number off disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037int
1038xfs_iread(
1039 xfs_mount_t *mp,
1040 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001041 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001042 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 xfs_buf_t *bp;
1045 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int error;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001049 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001051 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001052 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001053 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Dave Chinnercca9f932013-06-27 16:04:49 +10001055 /* shortcut IO on inode allocation if possible */
1056 if ((iget_flags & XFS_IGET_CREATE) &&
1057 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1058 /* initialise the on-disk inode core */
1059 memset(&ip->i_d, 0, sizeof(ip->i_d));
1060 ip->i_d.di_magic = XFS_DINODE_MAGIC;
1061 ip->i_d.di_gen = prandom_u32();
1062 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1063 ip->i_d.di_version = 3;
1064 ip->i_d.di_ino = ip->i_ino;
1065 uuid_copy(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid);
1066 } else
1067 ip->i_d.di_version = 2;
1068 return 0;
1069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001072 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +11001073 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001074 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +11001075 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +11001076 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +11001077
Christoph Hellwig93848a92013-04-03 16:11:17 +11001078 /* even unallocated inodes are verified */
1079 if (!xfs_dinode_verify(mp, ip, dip)) {
1080 xfs_alert(mp, "%s: validation failed for inode %lld failed",
1081 __func__, ip->i_ino);
1082
1083 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
1084 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001085 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087
1088 /*
1089 * If the on-disk inode is already linked to a directory
1090 * entry, copy all of the inode into the in-core inode.
1091 * xfs_iformat() handles copying in the inode format
1092 * specific information.
1093 * Otherwise, just get the truly permanent information.
1094 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001095 if (dip->di_mode) {
1096 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 error = xfs_iformat(ip, dip);
1098 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +11001100 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
1101 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001103 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 } else {
Christoph Hellwig93848a92013-04-03 16:11:17 +11001106 /*
1107 * Partial initialisation of the in-core inode. Just the bits
1108 * that xfs_ialloc won't overwrite or relies on being correct.
1109 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001110 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1111 ip->i_d.di_version = dip->di_version;
1112 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1113 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Christoph Hellwig93848a92013-04-03 16:11:17 +11001114
1115 if (dip->di_version == 3) {
1116 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
1117 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
1118 }
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Make sure to pull in the mode here as well in
1122 * case the inode is released without being used.
1123 * This ensures that xfs_inactive() will see that
1124 * the inode is already free and not try to mess
1125 * with the uninitialized part of it.
1126 */
1127 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
1131 * The inode format changed when we moved the link count and
1132 * made it 32 bits long. If this is an old format inode,
1133 * convert it in memory to look like a new one. If it gets
1134 * flushed to disk we will convert back before flushing or
1135 * logging it. We zero out the new projid field and the old link
1136 * count field. We'll handle clearing the pad field (the remains
1137 * of the old uuid field) when we actually convert the inode to
1138 * the new format. We don't change the version number so that we
1139 * can distinguish this from a real new format inode.
1140 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001141 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ip->i_d.di_nlink = ip->i_d.di_onlink;
1143 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001144 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 ip->i_delayed_blks = 0;
1148
1149 /*
1150 * Mark the buffer containing the inode as something to keep
1151 * around for a while. This helps to keep recently accessed
1152 * meta-data in-core longer.
1153 */
Dave Chinner821eb212010-12-02 16:31:13 +11001154 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /*
Dave Chinnercca9f932013-06-27 16:04:49 +10001157 * Use xfs_trans_brelse() to release the buffer containing the on-disk
1158 * inode, because it was acquired with xfs_trans_read_buf() in
1159 * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1161 * will only release the buffer if it is not dirty within the
1162 * transaction. It will be OK to release the buffer in this case,
Dave Chinnercca9f932013-06-27 16:04:49 +10001163 * because inodes on disk are never destroyed and we will be locking the
1164 * new in-core inode before putting it in the cache where other
1165 * processes can find it. Thus we don't have to worry about the inode
1166 * being changed just because we released the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001168 out_brelse:
1169 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001170 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/*
1174 * Read in extents from a btree-format inode.
1175 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1176 */
1177int
1178xfs_iread_extents(
1179 xfs_trans_t *tp,
1180 xfs_inode_t *ip,
1181 int whichfork)
1182{
1183 int error;
1184 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001185 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1188 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1189 ip->i_mount);
1190 return XFS_ERROR(EFSCORRUPTED);
1191 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001192 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
1196 * We know that the size is valid (it's checked in iformat_btree)
1197 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001198 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001200 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 error = xfs_bmap_read_extents(tp, ip, whichfork);
1202 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001203 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ifp->if_flags &= ~XFS_IFEXTENTS;
1205 return error;
1206 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001207 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
1209}
1210
1211/*
1212 * Allocate an inode on disk and return a copy of its in-core version.
1213 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1214 * appropriately within the inode. The uid and gid for the inode are
1215 * set according to the contents of the given cred structure.
1216 *
1217 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001218 * has a free inode available, call xfs_iget() to obtain the in-core
1219 * version of the allocated inode. Finally, fill in the inode and
1220 * log its initial contents. In this case, ialloc_context would be
1221 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001223 * If xfs_dialloc() does not have an available inode, it will replenish
1224 * its supply by doing an allocation. Since we can only do one
1225 * allocation within a transaction without deadlocks, we must commit
1226 * the current transaction before returning the inode itself.
1227 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 * The caller should then commit the current transaction, start a new
1229 * transaction, and call xfs_ialloc() again to actually get the inode.
1230 *
1231 * To ensure that some other process does not grab the inode that
1232 * was allocated during the first call to xfs_ialloc(), this routine
1233 * also returns the [locked] bp pointing to the head of the freelist
1234 * as ialloc_context. The caller should hold this buffer across
1235 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +10001236 *
1237 * If we are allocating quota inodes, we do not have a parent inode
1238 * to attach to or associate with (i.e. pip == NULL) because they
1239 * are not linked into the directory structure - they are attached
1240 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
1242int
1243xfs_ialloc(
1244 xfs_trans_t *tp,
1245 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -04001246 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -07001247 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001249 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int okalloc,
1251 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 xfs_inode_t **ipp)
1253{
Christoph Hellwig93848a92013-04-03 16:11:17 +11001254 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfs_ino_t ino;
1256 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 uint flags;
1258 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001259 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +11001260 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /*
1263 * Call the space management code to pick
1264 * the on-disk inode to be allocated.
1265 */
David Chinnerb11f94d2007-07-11 11:09:33 +10001266 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -04001267 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +11001268 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -04001270 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 *ipp = NULL;
1272 return 0;
1273 }
1274 ASSERT(*ialloc_context == NULL);
1275
1276 /*
1277 * Get the in-core inode with the lock held exclusively.
1278 * This is because we're setting fields here we need
1279 * to prevent others from looking at until we're done.
1280 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11001281 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001282 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +11001283 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 ASSERT(ip != NULL);
1286
Al Viro576b1d62011-07-26 02:50:15 -04001287 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ip->i_d.di_onlink = 0;
1289 ip->i_d.di_nlink = nlink;
1290 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +01001291 ip->i_d.di_uid = current_fsuid();
1292 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001293 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1295
1296 /*
1297 * If the superblock version is up to where we support new format
1298 * inodes and this is currently an old format inode, then change
1299 * the inode version number now. This way we only do the conversion
1300 * here rather than here and in the flush/logging code.
1301 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11001302 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001303 ip->i_d.di_version == 1) {
1304 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /*
1306 * We've already zeroed the old link count, the projid field,
1307 * and the pad field.
1308 */
1309 }
1310
1311 /*
1312 * Project ids won't be stored on disk if we are using a version 1 inode.
1313 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001314 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 xfs_bump_ino_vers2(tp, ip);
1316
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001317 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001319 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ip->i_d.di_mode |= S_ISGID;
1321 }
1322 }
1323
1324 /*
1325 * If the group ID of the new file does not match the effective group
1326 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1327 * (and only if the irix_sgid_inherit compatibility variable is set).
1328 */
1329 if ((irix_sgid_inherit) &&
1330 (ip->i_d.di_mode & S_ISGID) &&
1331 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1332 ip->i_d.di_mode &= ~S_ISGID;
1333 }
1334
1335 ip->i_d.di_size = 0;
1336 ip->i_d.di_nextents = 0;
1337 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001338
1339 nanotime(&tv);
1340 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1341 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1342 ip->i_d.di_atime = ip->i_d.di_mtime;
1343 ip->i_d.di_ctime = ip->i_d.di_mtime;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /*
1346 * di_gen will have been taken care of in xfs_iread.
1347 */
1348 ip->i_d.di_extsize = 0;
1349 ip->i_d.di_dmevmask = 0;
1350 ip->i_d.di_dmstate = 0;
1351 ip->i_d.di_flags = 0;
Christoph Hellwig93848a92013-04-03 16:11:17 +11001352
1353 if (ip->i_d.di_version == 3) {
1354 ASSERT(ip->i_d.di_ino == ino);
1355 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
1356 ip->i_d.di_crc = 0;
1357 ip->i_d.di_changecount = 1;
1358 ip->i_d.di_lsn = 0;
1359 ip->i_d.di_flags2 = 0;
1360 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
1361 ip->i_d.di_crtime = ip->i_d.di_mtime;
1362 }
1363
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 flags = XFS_ILOG_CORE;
1366 switch (mode & S_IFMT) {
1367 case S_IFIFO:
1368 case S_IFCHR:
1369 case S_IFBLK:
1370 case S_IFSOCK:
1371 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1372 ip->i_df.if_u2.if_rdev = rdev;
1373 ip->i_df.if_flags = 0;
1374 flags |= XFS_ILOG_DEV;
1375 break;
1376 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001377 /*
1378 * we can't set up filestreams until after the VFS inode
1379 * is set up properly.
1380 */
1381 if (pip && xfs_inode_is_filestream(pip))
1382 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001383 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001385 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001386 uint di_flags = 0;
1387
Al Viroabbede12011-07-26 02:31:30 -04001388 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001389 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1390 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001391 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1392 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1393 ip->i_d.di_extsize = pip->i_d.di_extsize;
1394 }
Al Viroabbede12011-07-26 02:31:30 -04001395 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001396 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001397 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001398 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1399 di_flags |= XFS_DIFLAG_EXTSIZE;
1400 ip->i_d.di_extsize = pip->i_d.di_extsize;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1404 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001405 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1407 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001408 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1410 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001411 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1413 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001414 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1415 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1416 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001417 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1418 xfs_inherit_nodefrag)
1419 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001420 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1421 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001422 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 /* FALLTHROUGH */
1425 case S_IFLNK:
1426 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1427 ip->i_df.if_flags = XFS_IFEXTENTS;
1428 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1429 ip->i_df.if_u1.if_extents = NULL;
1430 break;
1431 default:
1432 ASSERT(0);
1433 }
1434 /*
1435 * Attribute fork settings for new inode.
1436 */
1437 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1438 ip->i_d.di_anextents = 0;
1439
1440 /*
1441 * Log the new values stuffed into the inode.
1442 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001443 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 xfs_trans_log_inode(tp, ip, flags);
1445
Nathan Scottb83bd132006-06-09 16:48:30 +10001446 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001447 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
David Chinnerbf904242008-10-30 17:36:14 +11001449 /* now we have set up the vfs inode we can associate the filestream */
1450 if (filestreams) {
1451 error = xfs_filestream_associate(pip, ip);
1452 if (error < 0)
1453 return -error;
1454 if (!error)
1455 xfs_iflags_set(ip, XFS_IFILESTREAM);
1456 }
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *ipp = ip;
1459 return 0;
1460}
1461
1462/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001463 * Free up the underlying blocks past new_size. The new size must be smaller
1464 * than the current size. This routine can be used both for the attribute and
1465 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
David Chinnerf6485052008-04-17 16:50:04 +10001467 * The transaction passed to this routine must have made a permanent log
1468 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1469 * given transaction and start new ones, so make sure everything involved in
1470 * the transaction is tidy before calling here. Some transaction will be
1471 * returned to the caller to be committed. The incoming transaction must
1472 * already include the inode, and both inode locks must be held exclusively.
1473 * The inode must also be "held" within the transaction. On return the inode
1474 * will be "held" within the returned transaction. This routine does NOT
1475 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 *
David Chinnerf6485052008-04-17 16:50:04 +10001477 * If we get an error, we must return with the inode locked and linked into the
1478 * current transaction. This keeps things simple for the higher level code,
1479 * because it always knows that the inode is locked and held in the transaction
1480 * that returns to it whether errors occur or not. We don't mark the inode
1481 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001484xfs_itruncate_extents(
1485 struct xfs_trans **tpp,
1486 struct xfs_inode *ip,
1487 int whichfork,
1488 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001490 struct xfs_mount *mp = ip->i_mount;
1491 struct xfs_trans *tp = *tpp;
1492 struct xfs_trans *ntp;
1493 xfs_bmap_free_t free_list;
1494 xfs_fsblock_t first_block;
1495 xfs_fileoff_t first_unmap_block;
1496 xfs_fileoff_t last_block;
1497 xfs_filblks_t unmap_len;
1498 int committed;
1499 int error = 0;
1500 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Christoph Hellwig0b561852012-07-04 11:13:31 -04001502 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1503 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1504 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001505 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001506 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001508 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001509 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001511 trace_xfs_itruncate_extents_start(ip, new_size);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /*
1514 * Since it is possible for space to become allocated beyond
1515 * the end of the file (in a crash where the space is allocated
1516 * but the inode size is not yet updated), simply remove any
1517 * blocks which show up between the new EOF and the maximum
1518 * possible file size. If the first block to be removed is
1519 * beyond the maximum file size (ie it is the same as last_block),
1520 * then there is nothing to do.
1521 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001522 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001523 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001524 if (first_unmap_block == last_block)
1525 return 0;
1526
1527 ASSERT(first_unmap_block < last_block);
1528 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001530 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001531 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001532 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001533 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001535 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001536 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001537 if (error)
1538 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /*
1541 * Duplicate the transaction that has the permanent
1542 * reservation and commit the old transaction.
1543 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001544 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001545 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001546 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001547 if (error)
1548 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (committed) {
1551 /*
David Chinnerf6485052008-04-17 16:50:04 +10001552 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001553 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001555 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
David Chinnerf6485052008-04-17 16:50:04 +10001557
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001558 ntp = xfs_trans_dup(tp);
1559 error = xfs_trans_commit(tp, 0);
1560 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001561
Christoph Hellwigddc34152011-09-19 15:00:54 +00001562 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001563
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001564 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001565 goto out;
1566
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001567 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001568 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001569 * reference that we gained in xfs_trans_dup()
1570 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001571 xfs_log_ticket_put(tp->t_ticket);
1572 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001573 XFS_ITRUNCATE_LOG_RES(mp), 0,
1574 XFS_TRANS_PERM_LOG_RES,
1575 XFS_ITRUNCATE_LOG_COUNT);
1576 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001579
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001580 /*
1581 * Always re-log the inode so that our permanent transaction can keep
1582 * on rolling it forward in the log.
1583 */
1584 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1585
1586 trace_xfs_itruncate_extents_end(ip, new_size);
1587
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001588out:
1589 *tpp = tp;
1590 return error;
1591out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001593 * If the bunmapi call encounters an error, return to the caller where
1594 * the transaction can be properly aborted. We just need to make sure
1595 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001597 xfs_bmap_cancel(&free_list);
1598 goto out;
1599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601/*
1602 * This is called when the inode's link count goes to 0.
1603 * We place the on-disk inode on a list in the AGI. It
1604 * will be pulled from this list when the inode is freed.
1605 */
1606int
1607xfs_iunlink(
1608 xfs_trans_t *tp,
1609 xfs_inode_t *ip)
1610{
1611 xfs_mount_t *mp;
1612 xfs_agi_t *agi;
1613 xfs_dinode_t *dip;
1614 xfs_buf_t *agibp;
1615 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 xfs_agino_t agino;
1617 short bucket_index;
1618 int offset;
1619 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 ASSERT(ip->i_d.di_nlink == 0);
1622 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 mp = tp->t_mountp;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Get the agi buffer first. It ensures lock ordering
1628 * on the list.
1629 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001630 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001631 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Get the index into the agi hash table for the
1637 * list this inode will go on.
1638 */
1639 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1640 ASSERT(agino != 0);
1641 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1642 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001643 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001645 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /*
1647 * There is already another inode in the bucket we need
1648 * to add ourselves to. Add us at the front of the list.
1649 * Here we put the head pointer into our next pointer,
1650 * and then we fall through to point the head at us.
1651 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001652 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1653 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001654 if (error)
1655 return error;
1656
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001657 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001659 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001661
1662 /* need to recalc the inode CRC if appropriate */
1663 xfs_dinode_calc_crc(mp, dip);
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 xfs_trans_inode_buf(tp, ibp);
1666 xfs_trans_log_buf(tp, ibp, offset,
1667 (offset + sizeof(xfs_agino_t) - 1));
1668 xfs_inobp_check(mp, ibp);
1669 }
1670
1671 /*
1672 * Point the bucket head pointer at the inode being inserted.
1673 */
1674 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001675 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 offset = offsetof(xfs_agi_t, agi_unlinked) +
1677 (sizeof(xfs_agino_t) * bucket_index);
1678 xfs_trans_log_buf(tp, agibp, offset,
1679 (offset + sizeof(xfs_agino_t) - 1));
1680 return 0;
1681}
1682
1683/*
1684 * Pull the on-disk inode from the AGI unlinked list.
1685 */
1686STATIC int
1687xfs_iunlink_remove(
1688 xfs_trans_t *tp,
1689 xfs_inode_t *ip)
1690{
1691 xfs_ino_t next_ino;
1692 xfs_mount_t *mp;
1693 xfs_agi_t *agi;
1694 xfs_dinode_t *dip;
1695 xfs_buf_t *agibp;
1696 xfs_buf_t *ibp;
1697 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 xfs_agino_t agino;
1699 xfs_agino_t next_agino;
1700 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001701 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001703 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
1710 * Get the agi buffer first. It ensures lock ordering
1711 * on the list.
1712 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001713 error = xfs_read_agi(mp, tp, agno, &agibp);
1714 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /*
1720 * Get the index into the agi hash table for the
1721 * list this inode will go on.
1722 */
1723 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1724 ASSERT(agino != 0);
1725 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001726 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ASSERT(agi->agi_unlinked[bucket_index]);
1728
Christoph Hellwig16259e72005-11-02 15:11:25 +11001729 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001731 * We're at the head of the list. Get the inode's on-disk
1732 * buffer to see if there is anyone after us on the list.
1733 * Only modify our next pointer if it is not already NULLAGINO.
1734 * This saves us the overhead of dealing with the buffer when
1735 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001737 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1738 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001740 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001741 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return error;
1743 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001744 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ASSERT(next_agino != 0);
1746 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001747 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001748 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001750
1751 /* need to recalc the inode CRC if appropriate */
1752 xfs_dinode_calc_crc(mp, dip);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 xfs_trans_inode_buf(tp, ibp);
1755 xfs_trans_log_buf(tp, ibp, offset,
1756 (offset + sizeof(xfs_agino_t) - 1));
1757 xfs_inobp_check(mp, ibp);
1758 } else {
1759 xfs_trans_brelse(tp, ibp);
1760 }
1761 /*
1762 * Point the bucket head pointer at the next inode.
1763 */
1764 ASSERT(next_agino != 0);
1765 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001766 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 offset = offsetof(xfs_agi_t, agi_unlinked) +
1768 (sizeof(xfs_agino_t) * bucket_index);
1769 xfs_trans_log_buf(tp, agibp, offset,
1770 (offset + sizeof(xfs_agino_t) - 1));
1771 } else {
1772 /*
1773 * We need to search the list for the inode being freed.
1774 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001775 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 last_ibp = NULL;
1777 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001778 struct xfs_imap imap;
1779
1780 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001782
1783 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001785
1786 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001788 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001789 "%s: xfs_imap returned error %d.",
1790 __func__, error);
1791 return error;
1792 }
1793
1794 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1795 &last_ibp, 0, 0);
1796 if (error) {
1797 xfs_warn(mp,
1798 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001799 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return error;
1801 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001802
1803 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001804 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ASSERT(next_agino != NULLAGINO);
1806 ASSERT(next_agino != 0);
1807 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001810 * Now last_ibp points to the buffer previous to us on the
1811 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001813 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1814 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001816 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001817 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return error;
1819 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001820 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ASSERT(next_agino != 0);
1822 ASSERT(next_agino != agino);
1823 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001824 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001825 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001827
1828 /* need to recalc the inode CRC if appropriate */
1829 xfs_dinode_calc_crc(mp, dip);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 xfs_trans_inode_buf(tp, ibp);
1832 xfs_trans_log_buf(tp, ibp, offset,
1833 (offset + sizeof(xfs_agino_t) - 1));
1834 xfs_inobp_check(mp, ibp);
1835 } else {
1836 xfs_trans_brelse(tp, ibp);
1837 }
1838 /*
1839 * Point the previous inode on the list to the next inode.
1840 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001841 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 ASSERT(next_agino != 0);
1843 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001844
1845 /* need to recalc the inode CRC if appropriate */
1846 xfs_dinode_calc_crc(mp, last_dip);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 xfs_trans_inode_buf(tp, last_ibp);
1849 xfs_trans_log_buf(tp, last_ibp, offset,
1850 (offset + sizeof(xfs_agino_t) - 1));
1851 xfs_inobp_check(mp, last_ibp);
1852 }
1853 return 0;
1854}
1855
Dave Chinner5b3eed72010-08-24 11:42:41 +10001856/*
1857 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1858 * inodes that are in memory - they all must be marked stale and attached to
1859 * the cluster buffer.
1860 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001861STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862xfs_ifree_cluster(
1863 xfs_inode_t *free_ip,
1864 xfs_trans_t *tp,
1865 xfs_ino_t inum)
1866{
1867 xfs_mount_t *mp = free_ip->i_mount;
1868 int blks_per_cluster;
1869 int nbufs;
1870 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001871 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 xfs_daddr_t blkno;
1873 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001874 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_inode_log_item_t *iip;
1876 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001877 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Dave Chinner5017e972010-01-11 11:47:40 +00001879 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1881 blks_per_cluster = 1;
1882 ninodes = mp->m_sb.sb_inopblock;
1883 nbufs = XFS_IALLOC_BLOCKS(mp);
1884 } else {
1885 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1886 mp->m_sb.sb_blocksize;
1887 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1888 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1889 }
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 for (j = 0; j < nbufs; j++, inum += ninodes) {
1892 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1893 XFS_INO_TO_AGBNO(mp, inum));
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001896 * We obtain and lock the backing buffer first in the process
1897 * here, as we have to ensure that any dirty inode that we
1898 * can't get the flush lock on is attached to the buffer.
1899 * If we scan the in-memory inodes first, then buffer IO can
1900 * complete before we get a lock on it, and hence we may fail
1901 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001903 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11001904 mp->m_bsize * blks_per_cluster,
1905 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001907 if (!bp)
1908 return ENOMEM;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001909
1910 /*
1911 * This buffer may not have been correctly initialised as we
1912 * didn't read it from disk. That's not important because we are
1913 * only using to mark the buffer as stale in the log, and to
1914 * attach stale cached inodes on it. That means it will never be
1915 * dispatched for IO. If it is, we want to know about it, and we
1916 * want it to fail. We can acheive this by adding a write
1917 * verifier to the buffer.
1918 */
Dave Chinner1813dd62012-11-14 17:54:40 +11001919 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001920
Dave Chinner5b257b42010-06-03 16:22:29 +10001921 /*
1922 * Walk the inodes already attached to the buffer and mark them
1923 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001924 * in-memory inode walk can't lock them. By marking them all
1925 * stale first, we will not attempt to lock them in the loop
1926 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001927 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001928 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 while (lip) {
1930 if (lip->li_type == XFS_LI_INODE) {
1931 iip = (xfs_inode_log_item_t *)lip;
1932 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001933 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001934 xfs_trans_ail_copy_lsn(mp->m_ail,
1935 &iip->ili_flush_lsn,
1936 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001937 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 lip = lip->li_bio_list;
1940 }
1941
Dave Chinner5b3eed72010-08-24 11:42:41 +10001942
Dave Chinner5b257b42010-06-03 16:22:29 +10001943 /*
1944 * For each inode in memory attempt to add it to the inode
1945 * buffer and set it up for being staled on buffer IO
1946 * completion. This is safe as we've locked out tail pushing
1947 * and flushing by locking the buffer.
1948 *
1949 * We have already marked every inode that was part of a
1950 * transaction stale above, which means there is no point in
1951 * even trying to lock them.
1952 */
1953 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001954retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001955 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001956 ip = radix_tree_lookup(&pag->pag_ici_root,
1957 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001959 /* Inode not in memory, nothing to do */
1960 if (!ip) {
1961 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001962 continue;
1963 }
1964
Dave Chinner5b3eed72010-08-24 11:42:41 +10001965 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001966 * because this is an RCU protected lookup, we could
1967 * find a recently freed or even reallocated inode
1968 * during the lookup. We need to check under the
1969 * i_flags_lock for a valid inode here. Skip it if it
1970 * is not valid, the wrong inode or stale.
1971 */
1972 spin_lock(&ip->i_flags_lock);
1973 if (ip->i_ino != inum + i ||
1974 __xfs_iflags_test(ip, XFS_ISTALE)) {
1975 spin_unlock(&ip->i_flags_lock);
1976 rcu_read_unlock();
1977 continue;
1978 }
1979 spin_unlock(&ip->i_flags_lock);
1980
1981 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001982 * Don't try to lock/unlock the current inode, but we
1983 * _cannot_ skip the other inodes that we did not find
1984 * in the list attached to the buffer and are not
1985 * already marked stale. If we can't lock it, back off
1986 * and retry.
1987 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001988 if (ip != free_ip &&
1989 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001990 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001991 delay(1);
1992 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001993 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001994 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001995
Dave Chinner5b3eed72010-08-24 11:42:41 +10001996 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001997 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001998
Dave Chinner5b3eed72010-08-24 11:42:41 +10001999 /*
2000 * we don't need to attach clean inodes or those only
2001 * with unlogged changes (which we throw away, anyway).
2002 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002003 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10002004 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10002005 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 xfs_ifunlock(ip);
2007 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2008 continue;
2009 }
2010
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002011 iip->ili_last_fields = iip->ili_fields;
2012 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11002014 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2015 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002017 xfs_buf_attach_iodone(bp, xfs_istale_done,
2018 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10002019
2020 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
Dave Chinner5b3eed72010-08-24 11:42:41 +10002024 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 xfs_trans_binval(tp, bp);
2026 }
2027
Dave Chinner5017e972010-01-11 11:47:40 +00002028 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032/*
2033 * This is called to return an inode to the inode free list.
2034 * The inode should already be truncated to 0 length and have
2035 * no pages associated with it. This routine also assumes that
2036 * the inode is already a part of the transaction.
2037 *
2038 * The on-disk copy of the inode will have been added to the list
2039 * of unlinked inodes in the AGI. We need to remove the inode from
2040 * that list atomically with respect to freeing it here.
2041 */
2042int
2043xfs_ifree(
2044 xfs_trans_t *tp,
2045 xfs_inode_t *ip,
2046 xfs_bmap_free_t *flist)
2047{
2048 int error;
2049 int delete;
2050 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11002051 xfs_dinode_t *dip;
2052 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002054 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 ASSERT(ip->i_d.di_nlink == 0);
2056 ASSERT(ip->i_d.di_nextents == 0);
2057 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002058 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 ASSERT(ip->i_d.di_nblocks == 0);
2060
2061 /*
2062 * Pull the on-disk inode from the AGI unlinked list.
2063 */
2064 error = xfs_iunlink_remove(tp, ip);
2065 if (error != 0) {
2066 return error;
2067 }
2068
2069 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2070 if (error != 0) {
2071 return error;
2072 }
2073 ip->i_d.di_mode = 0; /* mark incore inode as free */
2074 ip->i_d.di_flags = 0;
2075 ip->i_d.di_dmevmask = 0;
2076 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2078 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2079 /*
2080 * Bump the generation count so no one will be confused
2081 * by reincarnations of this inode.
2082 */
2083 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2086
Christoph Hellwig475ee412012-07-03 12:21:22 -04002087 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
2088 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11002089 if (error)
2090 return error;
2091
2092 /*
2093 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
2094 * from picking up this inode when it is reclaimed (its incore state
2095 * initialzed but not flushed to disk yet). The in-core di_mode is
2096 * already cleared and a corresponding transaction logged.
2097 * The hack here just synchronizes the in-core to on-disk
2098 * di_mode value in advance before the actual inode sync to disk.
2099 * This is OK because the inode is already unlinked and would never
2100 * change its di_mode again for this inode generation.
2101 * This is a temporary hack that would require a proper fix
2102 * in the future.
2103 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002104 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002107 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002110 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113/*
2114 * Reallocate the space for if_broot based on the number of records
2115 * being added or deleted as indicated in rec_diff. Move the records
2116 * and pointers in if_broot to fit the new size. When shrinking this
2117 * will eliminate holes between the records and pointers created by
2118 * the caller. When growing this will create holes to be filled in
2119 * by the caller.
2120 *
2121 * The caller must not request to add more records than would fit in
2122 * the on-disk inode root. If the if_broot is currently NULL, then
2123 * if we adding records one will be allocated. The caller must also
2124 * not request that the number of records go below zero, although
2125 * it can go to zero.
2126 *
2127 * ip -- the inode whose if_broot area is changing
2128 * ext_diff -- the change in the number of records, positive or negative,
2129 * requested for the if_broot array.
2130 */
2131void
2132xfs_iroot_realloc(
2133 xfs_inode_t *ip,
2134 int rec_diff,
2135 int whichfork)
2136{
Christoph Hellwig60197e82008-10-30 17:11:19 +11002137 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int cur_max;
2139 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002140 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int new_max;
2142 size_t new_size;
2143 char *np;
2144 char *op;
2145
2146 /*
2147 * Handle the degenerate case quietly.
2148 */
2149 if (rec_diff == 0) {
2150 return;
2151 }
2152
2153 ifp = XFS_IFORK_PTR(ip, whichfork);
2154 if (rec_diff > 0) {
2155 /*
2156 * If there wasn't any memory allocated before, just
2157 * allocate it now and get out.
2158 */
2159 if (ifp->if_broot_bytes == 0) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002160 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002161 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ifp->if_broot_bytes = (int)new_size;
2163 return;
2164 }
2165
2166 /*
2167 * If there is already an existing if_broot, then we need
2168 * to realloc() it and shift the pointers to their new
2169 * location. The records don't change location because
2170 * they are kept butted up against the btree block header.
2171 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002172 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 new_max = cur_max + rec_diff;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002174 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002175 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002176 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002177 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002178 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2179 ifp->if_broot_bytes);
2180 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2181 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 ifp->if_broot_bytes = (int)new_size;
2183 ASSERT(ifp->if_broot_bytes <=
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002184 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2186 return;
2187 }
2188
2189 /*
2190 * rec_diff is less than 0. In this case, we are shrinking the
2191 * if_broot buffer. It must already exist. If we go to zero
2192 * records, just get rid of the root and clear the status bit.
2193 */
2194 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002195 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 new_max = cur_max + rec_diff;
2197 ASSERT(new_max >= 0);
2198 if (new_max > 0)
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002199 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 else
2201 new_size = 0;
2202 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002203 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /*
2205 * First copy over the btree block header.
2206 */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002207 memcpy(new_broot, ifp->if_broot,
2208 XFS_BMBT_BLOCK_LEN(ip->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 } else {
2210 new_broot = NULL;
2211 ifp->if_flags &= ~XFS_IFBROOT;
2212 }
2213
2214 /*
2215 * Only copy the records and pointers if there are any.
2216 */
2217 if (new_max > 0) {
2218 /*
2219 * First copy the records.
2220 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11002221 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2222 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2224
2225 /*
2226 * Then copy the pointers.
2227 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002228 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002230 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 (int)new_size);
2232 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2233 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002234 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 ifp->if_broot = new_broot;
2236 ifp->if_broot_bytes = (int)new_size;
2237 ASSERT(ifp->if_broot_bytes <=
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002238 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return;
2240}
2241
2242
2243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * This is called when the amount of space needed for if_data
2245 * is increased or decreased. The change in size is indicated by
2246 * the number of bytes that need to be added or deleted in the
2247 * byte_diff parameter.
2248 *
2249 * If the amount of space needed has decreased below the size of the
2250 * inline buffer, then switch to using the inline buffer. Otherwise,
2251 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2252 * to what is needed.
2253 *
2254 * ip -- the inode whose if_data area is changing
2255 * byte_diff -- the change in the number of bytes, positive or negative,
2256 * requested for the if_data array.
2257 */
2258void
2259xfs_idata_realloc(
2260 xfs_inode_t *ip,
2261 int byte_diff,
2262 int whichfork)
2263{
2264 xfs_ifork_t *ifp;
2265 int new_size;
2266 int real_size;
2267
2268 if (byte_diff == 0) {
2269 return;
2270 }
2271
2272 ifp = XFS_IFORK_PTR(ip, whichfork);
2273 new_size = (int)ifp->if_bytes + byte_diff;
2274 ASSERT(new_size >= 0);
2275
2276 if (new_size == 0) {
2277 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002278 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 ifp->if_u1.if_data = NULL;
2281 real_size = 0;
2282 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2283 /*
2284 * If the valid extents/data can fit in if_inline_ext/data,
2285 * copy them from the malloc'd vector and free it.
2286 */
2287 if (ifp->if_u1.if_data == NULL) {
2288 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2289 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2290 ASSERT(ifp->if_real_bytes != 0);
2291 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2292 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002293 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2295 }
2296 real_size = 0;
2297 } else {
2298 /*
2299 * Stuck with malloc/realloc.
2300 * For inline data, the underlying buffer must be
2301 * a multiple of 4 bytes in size so that it can be
2302 * logged and stay on word boundaries. We enforce
2303 * that here.
2304 */
2305 real_size = roundup(new_size, 4);
2306 if (ifp->if_u1.if_data == NULL) {
2307 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002308 ifp->if_u1.if_data = kmem_alloc(real_size,
2309 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2311 /*
2312 * Only do the realloc if the underlying size
2313 * is really changing.
2314 */
2315 if (ifp->if_real_bytes != real_size) {
2316 ifp->if_u1.if_data =
2317 kmem_realloc(ifp->if_u1.if_data,
2318 real_size,
2319 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002320 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 } else {
2323 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002324 ifp->if_u1.if_data = kmem_alloc(real_size,
2325 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2327 ifp->if_bytes);
2328 }
2329 }
2330 ifp->if_real_bytes = real_size;
2331 ifp->if_bytes = new_size;
2332 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335void
2336xfs_idestroy_fork(
2337 xfs_inode_t *ip,
2338 int whichfork)
2339{
2340 xfs_ifork_t *ifp;
2341
2342 ifp = XFS_IFORK_PTR(ip, whichfork);
2343 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002344 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 ifp->if_broot = NULL;
2346 }
2347
2348 /*
2349 * If the format is local, then we can't have an extents
2350 * array so just look for an inline data array. If we're
2351 * not local then we may or may not have an extents list,
2352 * so check and free it up if we do.
2353 */
2354 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2355 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2356 (ifp->if_u1.if_data != NULL)) {
2357 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002358 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 ifp->if_u1.if_data = NULL;
2360 ifp->if_real_bytes = 0;
2361 }
2362 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002363 ((ifp->if_flags & XFS_IFEXTIREC) ||
2364 ((ifp->if_u1.if_extents != NULL) &&
2365 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002367 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 ASSERT(ifp->if_u1.if_extents == NULL ||
2370 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2371 ASSERT(ifp->if_real_bytes == 0);
2372 if (whichfork == XFS_ATTR_FORK) {
2373 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2374 ip->i_afp = NULL;
2375 }
2376}
2377
2378/*
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002379 * This is called to unpin an inode. The caller must have the inode locked
2380 * in at least shared mode so that the buffer cannot be subsequently pinned
2381 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002383static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002384xfs_iunpin(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002385 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002386{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002387 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002388
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002389 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2390
David Chinnera3f74ff2008-03-06 13:43:42 +11002391 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002392 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002393
David Chinnera3f74ff2008-03-06 13:43:42 +11002394}
2395
Christoph Hellwigf392e632011-12-18 20:00:10 +00002396static void
2397__xfs_iunpin_wait(
2398 struct xfs_inode *ip)
2399{
2400 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2401 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2402
2403 xfs_iunpin(ip);
2404
2405 do {
2406 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2407 if (xfs_ipincount(ip))
2408 io_schedule();
2409 } while (xfs_ipincount(ip));
2410 finish_wait(wq, &wait.wait);
2411}
2412
Dave Chinner777df5a2010-02-06 12:37:26 +11002413void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414xfs_iunpin_wait(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002415 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002417 if (xfs_ipincount(ip))
2418 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421/*
2422 * xfs_iextents_copy()
2423 *
2424 * This is called to copy the REAL extents (as opposed to the delayed
2425 * allocation extents) from the inode into the given buffer. It
2426 * returns the number of bytes copied into the buffer.
2427 *
2428 * If there are no delayed allocation extents, then we can just
2429 * memcpy() the extents into the buffer. Otherwise, we need to
2430 * examine each extent in turn and skip those which are delayed.
2431 */
2432int
2433xfs_iextents_copy(
2434 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002435 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int whichfork)
2437{
2438 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 int i;
2440 xfs_ifork_t *ifp;
2441 int nrecs;
2442 xfs_fsblock_t start_block;
2443
2444 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002445 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 ASSERT(ifp->if_bytes > 0);
2447
2448 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002449 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 ASSERT(nrecs > 0);
2451
2452 /*
2453 * There are some delayed allocation extents in the
2454 * inode, so copy the extents one at a time and skip
2455 * the delayed ones. There must be at least one
2456 * non-delayed extent.
2457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 copied = 0;
2459 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002460 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002462 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /*
2464 * It's a delayed allocation extent, so skip it.
2465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 continue;
2467 }
2468
2469 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002470 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2471 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002472 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 copied++;
2474 }
2475 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002476 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2479}
2480
2481/*
2482 * Each of the following cases stores data into the same region
2483 * of the on-disk inode, so only one of them can be valid at
2484 * any given time. While it is possible to have conflicting formats
2485 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2486 * in EXTENTS format, this can only happen when the fork has
2487 * changed formats after being modified but before being flushed.
2488 * In these cases, the format always takes precedence, because the
2489 * format indicates the current state of the fork.
2490 */
2491/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002492STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493xfs_iflush_fork(
2494 xfs_inode_t *ip,
2495 xfs_dinode_t *dip,
2496 xfs_inode_log_item_t *iip,
2497 int whichfork,
2498 xfs_buf_t *bp)
2499{
2500 char *cp;
2501 xfs_ifork_t *ifp;
2502 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 static const short brootflag[2] =
2504 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2505 static const short dataflag[2] =
2506 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2507 static const short extflag[2] =
2508 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2509
David Chinnere4ac9672008-04-10 12:23:58 +10002510 if (!iip)
2511 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 ifp = XFS_IFORK_PTR(ip, whichfork);
2513 /*
2514 * This can happen if we gave up in iformat in an error path,
2515 * for the attribute fork.
2516 */
David Chinnere4ac9672008-04-10 12:23:58 +10002517 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521 cp = XFS_DFORK_PTR(dip, whichfork);
2522 mp = ip->i_mount;
2523 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2524 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002525 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 (ifp->if_bytes > 0)) {
2527 ASSERT(ifp->if_u1.if_data != NULL);
2528 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2529 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
2532
2533 case XFS_DINODE_FMT_EXTENTS:
2534 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002535 !(iip->ili_fields & extflag[whichfork]));
2536 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002538 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2540 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2541 whichfork);
2542 }
2543 break;
2544
2545 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002546 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 (ifp->if_broot_bytes > 0)) {
2548 ASSERT(ifp->if_broot != NULL);
2549 ASSERT(ifp->if_broot_bytes <=
2550 (XFS_IFORK_SIZE(ip, whichfork) +
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002551 XFS_BROOT_SIZE_ADJ(ip)));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002552 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 (xfs_bmdr_block_t *)cp,
2554 XFS_DFORK_SIZE(dip, mp, whichfork));
2555 }
2556 break;
2557
2558 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002559 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002561 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563 break;
2564
2565 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002566 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002568 memcpy(XFS_DFORK_DPTR(dip),
2569 &ip->i_df.if_u2.if_uuid,
2570 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572 break;
2573
2574 default:
2575 ASSERT(0);
2576 break;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
David Chinnerbad55842008-03-06 13:43:49 +11002580STATIC int
2581xfs_iflush_cluster(
2582 xfs_inode_t *ip,
2583 xfs_buf_t *bp)
2584{
2585 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002586 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002587 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002588 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002589 int ilist_size;
2590 xfs_inode_t **ilist;
2591 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002592 int nr_found;
2593 int clcount = 0;
2594 int bufwasdelwri;
2595 int i;
2596
Dave Chinner5017e972010-01-11 11:47:40 +00002597 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002598
David Chinnerc8f5f122008-05-20 11:30:15 +10002599 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2600 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002601 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002602 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002603 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002604
2605 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2606 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002607 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002608 /* really need a gang lookup range call here */
2609 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002610 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002611 if (nr_found == 0)
2612 goto out_free;
2613
2614 for (i = 0; i < nr_found; i++) {
2615 iq = ilist[i];
2616 if (iq == ip)
2617 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002618
2619 /*
2620 * because this is an RCU protected lookup, we could find a
2621 * recently freed or even reallocated inode during the lookup.
2622 * We need to check under the i_flags_lock for a valid inode
2623 * here. Skip it if it is not valid or the wrong inode.
2624 */
2625 spin_lock(&ip->i_flags_lock);
2626 if (!ip->i_ino ||
2627 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2628 spin_unlock(&ip->i_flags_lock);
2629 continue;
2630 }
2631 spin_unlock(&ip->i_flags_lock);
2632
David Chinnerbad55842008-03-06 13:43:49 +11002633 /*
2634 * Do an un-protected check to see if the inode is dirty and
2635 * is a candidate for flushing. These checks will be repeated
2636 * later after the appropriate locks are acquired.
2637 */
David Chinner33540402008-03-06 13:43:59 +11002638 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002639 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002640
2641 /*
2642 * Try to get locks. If any are unavailable or it is pinned,
2643 * then this inode cannot be flushed and is skipped.
2644 */
2645
2646 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2647 continue;
2648 if (!xfs_iflock_nowait(iq)) {
2649 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2650 continue;
2651 }
2652 if (xfs_ipincount(iq)) {
2653 xfs_ifunlock(iq);
2654 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2655 continue;
2656 }
2657
2658 /*
2659 * arriving here means that this inode can be flushed. First
2660 * re-check that it's dirty before flushing.
2661 */
David Chinner33540402008-03-06 13:43:59 +11002662 if (!xfs_inode_clean(iq)) {
2663 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002664 error = xfs_iflush_int(iq, bp);
2665 if (error) {
2666 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2667 goto cluster_corrupt_out;
2668 }
2669 clcount++;
2670 } else {
2671 xfs_ifunlock(iq);
2672 }
2673 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2674 }
2675
2676 if (clcount) {
2677 XFS_STATS_INC(xs_icluster_flushcnt);
2678 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2679 }
2680
2681out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002682 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002683 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002684out_put:
2685 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002686 return 0;
2687
2688
2689cluster_corrupt_out:
2690 /*
2691 * Corruption detected in the clustering loop. Invalidate the
2692 * inode buffer and shut down the filesystem.
2693 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002694 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002695 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002696 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002697 * brelse can handle it with no problems. If not, shut down the
2698 * filesystem before releasing the buffer.
2699 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002700 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002701 if (bufwasdelwri)
2702 xfs_buf_relse(bp);
2703
2704 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2705
2706 if (!bufwasdelwri) {
2707 /*
2708 * Just like incore_relse: if we have b_iodone functions,
2709 * mark the buffer as an error and call them. Otherwise
2710 * mark it as stale and brelse.
2711 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002712 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002713 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002714 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002715 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002716 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002717 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002718 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002719 xfs_buf_relse(bp);
2720 }
2721 }
2722
2723 /*
2724 * Unlocks the flush lock
2725 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002726 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002727 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002728 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002729 return XFS_ERROR(EFSCORRUPTED);
2730}
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002733 * Flush dirty inode metadata into the backing buffer.
2734 *
2735 * The caller must have the inode lock and the inode flush lock held. The
2736 * inode lock will still be held upon return to the caller, and the inode
2737 * flush lock will be released after the inode has reached the disk.
2738 *
2739 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 */
2741int
2742xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002743 struct xfs_inode *ip,
2744 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002746 struct xfs_mount *mp = ip->i_mount;
2747 struct xfs_buf *bp;
2748 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 XFS_STATS_INC(xs_iflush_count);
2752
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002753 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002754 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002756 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Christoph Hellwig4c468192012-04-23 15:58:36 +10002758 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 xfs_iunpin_wait(ip);
2761
2762 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002763 * For stale inodes we cannot rely on the backing buffer remaining
2764 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002765 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002766 * inodes below. We have to check this after ensuring the inode is
2767 * unpinned so that it is safe to reclaim the stale inode after the
2768 * flush call.
2769 */
2770 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2771 xfs_ifunlock(ip);
2772 return 0;
2773 }
2774
2775 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 * This may have been unpinned because the filesystem is shutting
2777 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002778 * to disk, because the log record didn't make it to disk.
2779 *
2780 * We also have to remove the log item from the AIL in this case,
2781 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
2783 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002784 error = XFS_ERROR(EIO);
2785 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
2787
2788 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002789 * Get the buffer containing the on-disk inode.
2790 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002791 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2792 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002793 if (error || !bp) {
2794 xfs_ifunlock(ip);
2795 return error;
2796 }
2797
2798 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 * First flush out the inode that xfs_iflush was called with.
2800 */
2801 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002806 * If the buffer is pinned then push on the log now so we won't
2807 * get stuck waiting in the write for too long.
2808 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002809 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002810 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002811
2812 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 * inode clustering:
2814 * see if other inodes can be gathered into this write
2815 */
David Chinnerbad55842008-03-06 13:43:49 +11002816 error = xfs_iflush_cluster(ip, bp);
2817 if (error)
2818 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Christoph Hellwig4c468192012-04-23 15:58:36 +10002820 *bpp = bp;
2821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823corrupt_out:
2824 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002825 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002827 error = XFS_ERROR(EFSCORRUPTED);
2828abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /*
2830 * Unlocks the flush lock
2831 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002832 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002833 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836
2837STATIC int
2838xfs_iflush_int(
Christoph Hellwig93848a92013-04-03 16:11:17 +11002839 struct xfs_inode *ip,
2840 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Christoph Hellwig93848a92013-04-03 16:11:17 +11002842 struct xfs_inode_log_item *iip = ip->i_itemp;
2843 struct xfs_dinode *dip;
2844 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002846 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002847 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002849 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Christoph Hellwig93848a92013-04-03 16:11:17 +11002850 ASSERT(iip != NULL && iip->ili_fields != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002853 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002855 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002857 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2858 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2859 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 goto corrupt_out;
2861 }
2862 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2863 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002864 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2865 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2866 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 goto corrupt_out;
2868 }
Al Viroabbede12011-07-26 02:31:30 -04002869 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (XFS_TEST_ERROR(
2871 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2872 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2873 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002874 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2875 "%s: Bad regular inode %Lu, ptr 0x%p",
2876 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 goto corrupt_out;
2878 }
Al Viroabbede12011-07-26 02:31:30 -04002879 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (XFS_TEST_ERROR(
2881 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2882 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2883 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2884 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002885 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2886 "%s: Bad directory inode %Lu, ptr 0x%p",
2887 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 goto corrupt_out;
2889 }
2890 }
2891 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2892 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2893 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002894 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2895 "%s: detected corrupt incore inode %Lu, "
2896 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2897 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002899 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 goto corrupt_out;
2901 }
2902 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2903 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002904 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2905 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2906 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto corrupt_out;
2908 }
2909 /*
2910 * bump the flush iteration count, used to detect flushes which
Christoph Hellwig93848a92013-04-03 16:11:17 +11002911 * postdate a log record during recovery. This is redundant as we now
2912 * log every change and hence this can't happen. Still, it doesn't hurt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 ip->i_d.di_flushiter++;
2915
2916 /*
2917 * Copy the dirty parts of the inode into the on-disk
2918 * inode. We always copy out the core of the inode,
2919 * because if the inode is dirty at all the core must
2920 * be.
2921 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002922 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2925 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2926 ip->i_d.di_flushiter = 0;
2927
2928 /*
2929 * If this is really an old format inode and the superblock version
2930 * has not been updated to support only new format inodes, then
2931 * convert back to the old inode format. If the superblock version
2932 * has been updated, then make the conversion permanent.
2933 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002934 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2935 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002936 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /*
2938 * Convert it back.
2939 */
2940 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002941 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
2943 /*
2944 * The superblock version has already been bumped,
2945 * so just make the conversion to the new inode
2946 * format permanent.
2947 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002948 ip->i_d.di_version = 2;
2949 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002951 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002953 memset(&(dip->di_pad[0]), 0,
2954 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002955 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957 }
2958
David Chinnere4ac9672008-04-10 12:23:58 +10002959 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2960 if (XFS_IFORK_Q(ip))
2961 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 xfs_inobp_check(mp, bp);
2963
2964 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002965 * We've recorded everything logged in the inode, so we'd like to clear
2966 * the ili_fields bits so we don't log and flush things unnecessarily.
2967 * However, we can't stop logging all this information until the data
2968 * we've copied into the disk buffer is written to disk. If we did we
2969 * might overwrite the copy of the inode in the log with all the data
2970 * after re-logging only part of it, and in the face of a crash we
2971 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002973 * What we do is move the bits to the ili_last_fields field. When
2974 * logging the inode, these bits are moved back to the ili_fields field.
2975 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2976 * know that the information those bits represent is permanently on
2977 * disk. As long as the flush completes before the inode is logged
2978 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002980 * We can play with the ili_fields bits here, because the inode lock
2981 * must be held exclusively in order to set bits there and the flush
2982 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2983 * done routine can tell whether or not to look in the AIL. Also, store
2984 * the current LSN of the inode so that we can tell whether the item has
2985 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2986 * need the AIL lock, because it is a 64 bit value that cannot be read
2987 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11002989 iip->ili_last_fields = iip->ili_fields;
2990 iip->ili_fields = 0;
2991 iip->ili_logged = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Christoph Hellwig93848a92013-04-03 16:11:17 +11002993 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2994 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Christoph Hellwig93848a92013-04-03 16:11:17 +11002996 /*
2997 * Attach the function xfs_iflush_done to the inode's
2998 * buffer. This will remove the inode from the AIL
2999 * and unlock the inode's flush lock when the inode is
3000 * completely written to disk.
3001 */
3002 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Christoph Hellwig93848a92013-04-03 16:11:17 +11003004 /* update the lsn in the on disk inode if required */
3005 if (ip->i_d.di_version == 3)
3006 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Christoph Hellwig93848a92013-04-03 16:11:17 +11003008 /* generate the checksum. */
3009 xfs_dinode_calc_crc(mp, dip);
3010
3011 ASSERT(bp->b_fspriv != NULL);
3012 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return 0;
3014
3015corrupt_out:
3016 return XFS_ERROR(EFSCORRUPTED);
3017}
3018
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003019/*
3020 * Return a pointer to the extent record at file index idx.
3021 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003022xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003023xfs_iext_get_ext(
3024 xfs_ifork_t *ifp, /* inode fork pointer */
3025 xfs_extnum_t idx) /* index of target extent */
3026{
3027 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003028 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3029
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003030 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3031 return ifp->if_u1.if_ext_irec->er_extbuf;
3032 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3033 xfs_ext_irec_t *erp; /* irec pointer */
3034 int erp_idx = 0; /* irec index */
3035 xfs_extnum_t page_idx = idx; /* ext index in target list */
3036
3037 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3038 return &erp->er_extbuf[page_idx];
3039 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003040 return &ifp->if_u1.if_extents[idx];
3041 } else {
3042 return NULL;
3043 }
3044}
3045
3046/*
3047 * Insert new item(s) into the extent records for incore inode
3048 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3049 */
3050void
3051xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003052 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003053 xfs_extnum_t idx, /* starting index of new items */
3054 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003055 xfs_bmbt_irec_t *new, /* items to insert */
3056 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003057{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003058 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003059 xfs_extnum_t i; /* extent record index */
3060
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003061 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
3062
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003063 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3064 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003065 for (i = idx; i < idx + count; i++, new++)
3066 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003067}
3068
3069/*
3070 * This is called when the amount of space required for incore file
3071 * extents needs to be increased. The ext_diff parameter stores the
3072 * number of new extents being added and the idx parameter contains
3073 * the extent index where the new extents will be added. If the new
3074 * extents are being appended, then we just need to (re)allocate and
3075 * initialize the space. Otherwise, if the new extents are being
3076 * inserted into the middle of the existing entries, a bit more work
3077 * is required to make room for the new extents to be inserted. The
3078 * caller is responsible for filling in the new extent entries upon
3079 * return.
3080 */
3081void
3082xfs_iext_add(
3083 xfs_ifork_t *ifp, /* inode fork pointer */
3084 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10003085 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003086{
3087 int byte_diff; /* new bytes being added */
3088 int new_size; /* size of extents after adding */
3089 xfs_extnum_t nextents; /* number of extents in file */
3090
3091 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3092 ASSERT((idx >= 0) && (idx <= nextents));
3093 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3094 new_size = ifp->if_bytes + byte_diff;
3095 /*
3096 * If the new number of extents (nextents + ext_diff)
3097 * fits inside the inode, then continue to use the inline
3098 * extent buffer.
3099 */
3100 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3101 if (idx < nextents) {
3102 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3103 &ifp->if_u2.if_inline_ext[idx],
3104 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3105 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3106 }
3107 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3108 ifp->if_real_bytes = 0;
3109 }
3110 /*
3111 * Otherwise use a linear (direct) extent list.
3112 * If the extents are currently inside the inode,
3113 * xfs_iext_realloc_direct will switch us from
3114 * inline to direct extent allocation mode.
3115 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003116 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003117 xfs_iext_realloc_direct(ifp, new_size);
3118 if (idx < nextents) {
3119 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3120 &ifp->if_u1.if_extents[idx],
3121 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3122 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3123 }
3124 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003125 /* Indirection array */
3126 else {
3127 xfs_ext_irec_t *erp;
3128 int erp_idx = 0;
3129 int page_idx = idx;
3130
3131 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3132 if (ifp->if_flags & XFS_IFEXTIREC) {
3133 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3134 } else {
3135 xfs_iext_irec_init(ifp);
3136 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3137 erp = ifp->if_u1.if_ext_irec;
3138 }
3139 /* Extents fit in target extent page */
3140 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3141 if (page_idx < erp->er_extcount) {
3142 memmove(&erp->er_extbuf[page_idx + ext_diff],
3143 &erp->er_extbuf[page_idx],
3144 (erp->er_extcount - page_idx) *
3145 sizeof(xfs_bmbt_rec_t));
3146 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3147 }
3148 erp->er_extcount += ext_diff;
3149 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3150 }
3151 /* Insert a new extent page */
3152 else if (erp) {
3153 xfs_iext_add_indirect_multi(ifp,
3154 erp_idx, page_idx, ext_diff);
3155 }
3156 /*
3157 * If extent(s) are being appended to the last page in
3158 * the indirection array and the new extent(s) don't fit
3159 * in the page, then erp is NULL and erp_idx is set to
3160 * the next index needed in the indirection array.
3161 */
3162 else {
3163 int count = ext_diff;
3164
3165 while (count) {
3166 erp = xfs_iext_irec_new(ifp, erp_idx);
3167 erp->er_extcount = count;
3168 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3169 if (count) {
3170 erp_idx++;
3171 }
3172 }
3173 }
3174 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003175 ifp->if_bytes = new_size;
3176}
3177
3178/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003179 * This is called when incore extents are being added to the indirection
3180 * array and the new extents do not fit in the target extent list. The
3181 * erp_idx parameter contains the irec index for the target extent list
3182 * in the indirection array, and the idx parameter contains the extent
3183 * index within the list. The number of extents being added is stored
3184 * in the count parameter.
3185 *
3186 * |-------| |-------|
3187 * | | | | idx - number of extents before idx
3188 * | idx | | count |
3189 * | | | | count - number of extents being inserted at idx
3190 * |-------| |-------|
3191 * | count | | nex2 | nex2 - number of extents after idx + count
3192 * |-------| |-------|
3193 */
3194void
3195xfs_iext_add_indirect_multi(
3196 xfs_ifork_t *ifp, /* inode fork pointer */
3197 int erp_idx, /* target extent irec index */
3198 xfs_extnum_t idx, /* index within target list */
3199 int count) /* new extents being added */
3200{
3201 int byte_diff; /* new bytes being added */
3202 xfs_ext_irec_t *erp; /* pointer to irec entry */
3203 xfs_extnum_t ext_diff; /* number of extents to add */
3204 xfs_extnum_t ext_cnt; /* new extents still needed */
3205 xfs_extnum_t nex2; /* extents after idx + count */
3206 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3207 int nlists; /* number of irec's (lists) */
3208
3209 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3210 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3211 nex2 = erp->er_extcount - idx;
3212 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3213
3214 /*
3215 * Save second part of target extent list
3216 * (all extents past */
3217 if (nex2) {
3218 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10003219 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003220 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3221 erp->er_extcount -= nex2;
3222 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3223 memset(&erp->er_extbuf[idx], 0, byte_diff);
3224 }
3225
3226 /*
3227 * Add the new extents to the end of the target
3228 * list, then allocate new irec record(s) and
3229 * extent buffer(s) as needed to store the rest
3230 * of the new extents.
3231 */
3232 ext_cnt = count;
3233 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3234 if (ext_diff) {
3235 erp->er_extcount += ext_diff;
3236 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3237 ext_cnt -= ext_diff;
3238 }
3239 while (ext_cnt) {
3240 erp_idx++;
3241 erp = xfs_iext_irec_new(ifp, erp_idx);
3242 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3243 erp->er_extcount = ext_diff;
3244 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3245 ext_cnt -= ext_diff;
3246 }
3247
3248 /* Add nex2 extents back to indirection array */
3249 if (nex2) {
3250 xfs_extnum_t ext_avail;
3251 int i;
3252
3253 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3254 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3255 i = 0;
3256 /*
3257 * If nex2 extents fit in the current page, append
3258 * nex2_ep after the new extents.
3259 */
3260 if (nex2 <= ext_avail) {
3261 i = erp->er_extcount;
3262 }
3263 /*
3264 * Otherwise, check if space is available in the
3265 * next page.
3266 */
3267 else if ((erp_idx < nlists - 1) &&
3268 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3269 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3270 erp_idx++;
3271 erp++;
3272 /* Create a hole for nex2 extents */
3273 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3274 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3275 }
3276 /*
3277 * Final choice, create a new extent page for
3278 * nex2 extents.
3279 */
3280 else {
3281 erp_idx++;
3282 erp = xfs_iext_irec_new(ifp, erp_idx);
3283 }
3284 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003285 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003286 erp->er_extcount += nex2;
3287 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3288 }
3289}
3290
3291/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003292 * This is called when the amount of space required for incore file
3293 * extents needs to be decreased. The ext_diff parameter stores the
3294 * number of extents to be removed and the idx parameter contains
3295 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003296 *
3297 * If the amount of space needed has decreased below the linear
3298 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3299 * extent array. Otherwise, use kmem_realloc() to adjust the
3300 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003301 */
3302void
3303xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003304 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003305 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003306 int ext_diff, /* number of extents to remove */
3307 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003308{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003309 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003310 xfs_extnum_t nextents; /* number of extents in file */
3311 int new_size; /* size of extents after removal */
3312
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003313 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3314
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003315 ASSERT(ext_diff > 0);
3316 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3317 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3318
3319 if (new_size == 0) {
3320 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003321 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3322 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003323 } else if (ifp->if_real_bytes) {
3324 xfs_iext_remove_direct(ifp, idx, ext_diff);
3325 } else {
3326 xfs_iext_remove_inline(ifp, idx, ext_diff);
3327 }
3328 ifp->if_bytes = new_size;
3329}
3330
3331/*
3332 * This removes ext_diff extents from the inline buffer, beginning
3333 * at extent index idx.
3334 */
3335void
3336xfs_iext_remove_inline(
3337 xfs_ifork_t *ifp, /* inode fork pointer */
3338 xfs_extnum_t idx, /* index to begin removing exts */
3339 int ext_diff) /* number of extents to remove */
3340{
3341 int nextents; /* number of extents in file */
3342
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003343 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003344 ASSERT(idx < XFS_INLINE_EXTS);
3345 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3346 ASSERT(((nextents - ext_diff) > 0) &&
3347 (nextents - ext_diff) < XFS_INLINE_EXTS);
3348
3349 if (idx + ext_diff < nextents) {
3350 memmove(&ifp->if_u2.if_inline_ext[idx],
3351 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3352 (nextents - (idx + ext_diff)) *
3353 sizeof(xfs_bmbt_rec_t));
3354 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3355 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3356 } else {
3357 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3358 ext_diff * sizeof(xfs_bmbt_rec_t));
3359 }
3360}
3361
3362/*
3363 * This removes ext_diff extents from a linear (direct) extent list,
3364 * beginning at extent index idx. If the extents are being removed
3365 * from the end of the list (ie. truncate) then we just need to re-
3366 * allocate the list to remove the extra space. Otherwise, if the
3367 * extents are being removed from the middle of the existing extent
3368 * entries, then we first need to move the extent records beginning
3369 * at idx + ext_diff up in the list to overwrite the records being
3370 * removed, then remove the extra space via kmem_realloc.
3371 */
3372void
3373xfs_iext_remove_direct(
3374 xfs_ifork_t *ifp, /* inode fork pointer */
3375 xfs_extnum_t idx, /* index to begin removing exts */
3376 int ext_diff) /* number of extents to remove */
3377{
3378 xfs_extnum_t nextents; /* number of extents in file */
3379 int new_size; /* size of extents after removal */
3380
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003381 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003382 new_size = ifp->if_bytes -
3383 (ext_diff * sizeof(xfs_bmbt_rec_t));
3384 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3385
3386 if (new_size == 0) {
3387 xfs_iext_destroy(ifp);
3388 return;
3389 }
3390 /* Move extents up in the list (if needed) */
3391 if (idx + ext_diff < nextents) {
3392 memmove(&ifp->if_u1.if_extents[idx],
3393 &ifp->if_u1.if_extents[idx + ext_diff],
3394 (nextents - (idx + ext_diff)) *
3395 sizeof(xfs_bmbt_rec_t));
3396 }
3397 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3398 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3399 /*
3400 * Reallocate the direct extent list. If the extents
3401 * will fit inside the inode then xfs_iext_realloc_direct
3402 * will switch from direct to inline extent allocation
3403 * mode for us.
3404 */
3405 xfs_iext_realloc_direct(ifp, new_size);
3406 ifp->if_bytes = new_size;
3407}
3408
3409/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003410 * This is called when incore extents are being removed from the
3411 * indirection array and the extents being removed span multiple extent
3412 * buffers. The idx parameter contains the file extent index where we
3413 * want to begin removing extents, and the count parameter contains
3414 * how many extents need to be removed.
3415 *
3416 * |-------| |-------|
3417 * | nex1 | | | nex1 - number of extents before idx
3418 * |-------| | count |
3419 * | | | | count - number of extents being removed at idx
3420 * | count | |-------|
3421 * | | | nex2 | nex2 - number of extents after idx + count
3422 * |-------| |-------|
3423 */
3424void
3425xfs_iext_remove_indirect(
3426 xfs_ifork_t *ifp, /* inode fork pointer */
3427 xfs_extnum_t idx, /* index to begin removing extents */
3428 int count) /* number of extents to remove */
3429{
3430 xfs_ext_irec_t *erp; /* indirection array pointer */
3431 int erp_idx = 0; /* indirection array index */
3432 xfs_extnum_t ext_cnt; /* extents left to remove */
3433 xfs_extnum_t ext_diff; /* extents to remove in current list */
3434 xfs_extnum_t nex1; /* number of extents before idx */
3435 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003436 int page_idx = idx; /* index in target extent list */
3437
3438 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3439 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3440 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003441 nex1 = page_idx;
3442 ext_cnt = count;
3443 while (ext_cnt) {
3444 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3445 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3446 /*
3447 * Check for deletion of entire list;
3448 * xfs_iext_irec_remove() updates extent offsets.
3449 */
3450 if (ext_diff == erp->er_extcount) {
3451 xfs_iext_irec_remove(ifp, erp_idx);
3452 ext_cnt -= ext_diff;
3453 nex1 = 0;
3454 if (ext_cnt) {
3455 ASSERT(erp_idx < ifp->if_real_bytes /
3456 XFS_IEXT_BUFSZ);
3457 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3458 nex1 = 0;
3459 continue;
3460 } else {
3461 break;
3462 }
3463 }
3464 /* Move extents up (if needed) */
3465 if (nex2) {
3466 memmove(&erp->er_extbuf[nex1],
3467 &erp->er_extbuf[nex1 + ext_diff],
3468 nex2 * sizeof(xfs_bmbt_rec_t));
3469 }
3470 /* Zero out rest of page */
3471 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3472 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3473 /* Update remaining counters */
3474 erp->er_extcount -= ext_diff;
3475 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3476 ext_cnt -= ext_diff;
3477 nex1 = 0;
3478 erp_idx++;
3479 erp++;
3480 }
3481 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3482 xfs_iext_irec_compact(ifp);
3483}
3484
3485/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003486 * Create, destroy, or resize a linear (direct) block of extents.
3487 */
3488void
3489xfs_iext_realloc_direct(
3490 xfs_ifork_t *ifp, /* inode fork pointer */
3491 int new_size) /* new size of extents */
3492{
3493 int rnew_size; /* real new size of extents */
3494
3495 rnew_size = new_size;
3496
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003497 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3498 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3499 (new_size != ifp->if_real_bytes)));
3500
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003501 /* Free extent records */
3502 if (new_size == 0) {
3503 xfs_iext_destroy(ifp);
3504 }
3505 /* Resize direct extent list and zero any new bytes */
3506 else if (ifp->if_real_bytes) {
3507 /* Check if extents will fit inside the inode */
3508 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3509 xfs_iext_direct_to_inline(ifp, new_size /
3510 (uint)sizeof(xfs_bmbt_rec_t));
3511 ifp->if_bytes = new_size;
3512 return;
3513 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003514 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003515 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003516 }
3517 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003518 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003519 kmem_realloc(ifp->if_u1.if_extents,
3520 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003521 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003522 }
3523 if (rnew_size > ifp->if_real_bytes) {
3524 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3525 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3526 rnew_size - ifp->if_real_bytes);
3527 }
3528 }
3529 /*
3530 * Switch from the inline extent buffer to a direct
3531 * extent list. Be sure to include the inline extent
3532 * bytes in new_size.
3533 */
3534 else {
3535 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003536 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003537 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003538 }
3539 xfs_iext_inline_to_direct(ifp, rnew_size);
3540 }
3541 ifp->if_real_bytes = rnew_size;
3542 ifp->if_bytes = new_size;
3543}
3544
3545/*
3546 * Switch from linear (direct) extent records to inline buffer.
3547 */
3548void
3549xfs_iext_direct_to_inline(
3550 xfs_ifork_t *ifp, /* inode fork pointer */
3551 xfs_extnum_t nextents) /* number of extents in file */
3552{
3553 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3554 ASSERT(nextents <= XFS_INLINE_EXTS);
3555 /*
3556 * The inline buffer was zeroed when we switched
3557 * from inline to direct extent allocation mode,
3558 * so we don't need to clear it here.
3559 */
3560 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3561 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003562 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003563 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3564 ifp->if_real_bytes = 0;
3565}
3566
3567/*
3568 * Switch from inline buffer to linear (direct) extent records.
3569 * new_size should already be rounded up to the next power of 2
3570 * by the caller (when appropriate), so use new_size as it is.
3571 * However, since new_size may be rounded up, we can't update
3572 * if_bytes here. It is the caller's responsibility to update
3573 * if_bytes upon return.
3574 */
3575void
3576xfs_iext_inline_to_direct(
3577 xfs_ifork_t *ifp, /* inode fork pointer */
3578 int new_size) /* number of extents in file */
3579{
David Chinner67850732008-08-13 16:02:51 +10003580 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003581 memset(ifp->if_u1.if_extents, 0, new_size);
3582 if (ifp->if_bytes) {
3583 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3584 ifp->if_bytes);
3585 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3586 sizeof(xfs_bmbt_rec_t));
3587 }
3588 ifp->if_real_bytes = new_size;
3589}
3590
3591/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003592 * Resize an extent indirection array to new_size bytes.
3593 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003594STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003595xfs_iext_realloc_indirect(
3596 xfs_ifork_t *ifp, /* inode fork pointer */
3597 int new_size) /* new indirection array size */
3598{
3599 int nlists; /* number of irec's (ex lists) */
3600 int size; /* current indirection array size */
3601
3602 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3603 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3604 size = nlists * sizeof(xfs_ext_irec_t);
3605 ASSERT(ifp->if_real_bytes);
3606 ASSERT((new_size >= 0) && (new_size != size));
3607 if (new_size == 0) {
3608 xfs_iext_destroy(ifp);
3609 } else {
3610 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3611 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003612 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003613 }
3614}
3615
3616/*
3617 * Switch from indirection array to linear (direct) extent allocations.
3618 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003619STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003620xfs_iext_indirect_to_direct(
3621 xfs_ifork_t *ifp) /* inode fork pointer */
3622{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003623 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003624 xfs_extnum_t nextents; /* number of extents in file */
3625 int size; /* size of file extents */
3626
3627 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3628 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3629 ASSERT(nextents <= XFS_LINEAR_EXTS);
3630 size = nextents * sizeof(xfs_bmbt_rec_t);
3631
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003632 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003633 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3634
3635 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003636 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003637 ifp->if_flags &= ~XFS_IFEXTIREC;
3638 ifp->if_u1.if_extents = ep;
3639 ifp->if_bytes = size;
3640 if (nextents < XFS_LINEAR_EXTS) {
3641 xfs_iext_realloc_direct(ifp, size);
3642 }
3643}
3644
3645/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003646 * Free incore file extents.
3647 */
3648void
3649xfs_iext_destroy(
3650 xfs_ifork_t *ifp) /* inode fork pointer */
3651{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003652 if (ifp->if_flags & XFS_IFEXTIREC) {
3653 int erp_idx;
3654 int nlists;
3655
3656 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3657 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3658 xfs_iext_irec_remove(ifp, erp_idx);
3659 }
3660 ifp->if_flags &= ~XFS_IFEXTIREC;
3661 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003662 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003663 } else if (ifp->if_bytes) {
3664 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3665 sizeof(xfs_bmbt_rec_t));
3666 }
3667 ifp->if_u1.if_extents = NULL;
3668 ifp->if_real_bytes = 0;
3669 ifp->if_bytes = 0;
3670}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003671
3672/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003673 * Return a pointer to the extent record for file system block bno.
3674 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003675xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003676xfs_iext_bno_to_ext(
3677 xfs_ifork_t *ifp, /* inode fork pointer */
3678 xfs_fileoff_t bno, /* block number to search for */
3679 xfs_extnum_t *idxp) /* index of target extent */
3680{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003681 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003682 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003683 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003684 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003685 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003686 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003687 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003688 xfs_extnum_t nextents; /* number of file extents */
3689 xfs_fileoff_t startoff = 0; /* start offset of extent */
3690
3691 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3692 if (nextents == 0) {
3693 *idxp = 0;
3694 return NULL;
3695 }
3696 low = 0;
3697 if (ifp->if_flags & XFS_IFEXTIREC) {
3698 /* Find target extent list */
3699 int erp_idx = 0;
3700 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3701 base = erp->er_extbuf;
3702 high = erp->er_extcount - 1;
3703 } else {
3704 base = ifp->if_u1.if_extents;
3705 high = nextents - 1;
3706 }
3707 /* Binary search extent records */
3708 while (low <= high) {
3709 idx = (low + high) >> 1;
3710 ep = base + idx;
3711 startoff = xfs_bmbt_get_startoff(ep);
3712 blockcount = xfs_bmbt_get_blockcount(ep);
3713 if (bno < startoff) {
3714 high = idx - 1;
3715 } else if (bno >= startoff + blockcount) {
3716 low = idx + 1;
3717 } else {
3718 /* Convert back to file-based extent index */
3719 if (ifp->if_flags & XFS_IFEXTIREC) {
3720 idx += erp->er_extoff;
3721 }
3722 *idxp = idx;
3723 return ep;
3724 }
3725 }
3726 /* Convert back to file-based extent index */
3727 if (ifp->if_flags & XFS_IFEXTIREC) {
3728 idx += erp->er_extoff;
3729 }
3730 if (bno >= startoff + blockcount) {
3731 if (++idx == nextents) {
3732 ep = NULL;
3733 } else {
3734 ep = xfs_iext_get_ext(ifp, idx);
3735 }
3736 }
3737 *idxp = idx;
3738 return ep;
3739}
3740
3741/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003742 * Return a pointer to the indirection array entry containing the
3743 * extent record for filesystem block bno. Store the index of the
3744 * target irec in *erp_idxp.
3745 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003746xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003747xfs_iext_bno_to_irec(
3748 xfs_ifork_t *ifp, /* inode fork pointer */
3749 xfs_fileoff_t bno, /* block number to search for */
3750 int *erp_idxp) /* irec index of target ext list */
3751{
3752 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3753 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003754 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003755 int nlists; /* number of extent irec's (lists) */
3756 int high; /* binary search upper limit */
3757 int low; /* binary search lower limit */
3758
3759 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3760 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3761 erp_idx = 0;
3762 low = 0;
3763 high = nlists - 1;
3764 while (low <= high) {
3765 erp_idx = (low + high) >> 1;
3766 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3767 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3768 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3769 high = erp_idx - 1;
3770 } else if (erp_next && bno >=
3771 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3772 low = erp_idx + 1;
3773 } else {
3774 break;
3775 }
3776 }
3777 *erp_idxp = erp_idx;
3778 return erp;
3779}
3780
3781/*
3782 * Return a pointer to the indirection array entry containing the
3783 * extent record at file extent index *idxp. Store the index of the
3784 * target irec in *erp_idxp and store the page index of the target
3785 * extent record in *idxp.
3786 */
3787xfs_ext_irec_t *
3788xfs_iext_idx_to_irec(
3789 xfs_ifork_t *ifp, /* inode fork pointer */
3790 xfs_extnum_t *idxp, /* extent index (file -> page) */
3791 int *erp_idxp, /* pointer to target irec */
3792 int realloc) /* new bytes were just added */
3793{
3794 xfs_ext_irec_t *prev; /* pointer to previous irec */
3795 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3796 int erp_idx; /* indirection array index */
3797 int nlists; /* number of irec's (ex lists) */
3798 int high; /* binary search upper limit */
3799 int low; /* binary search lower limit */
3800 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3801
3802 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003803 ASSERT(page_idx >= 0);
3804 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3805 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3806
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003807 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3808 erp_idx = 0;
3809 low = 0;
3810 high = nlists - 1;
3811
3812 /* Binary search extent irec's */
3813 while (low <= high) {
3814 erp_idx = (low + high) >> 1;
3815 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3816 prev = erp_idx > 0 ? erp - 1 : NULL;
3817 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3818 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3819 high = erp_idx - 1;
3820 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3821 (page_idx == erp->er_extoff + erp->er_extcount &&
3822 !realloc)) {
3823 low = erp_idx + 1;
3824 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3825 erp->er_extcount == XFS_LINEAR_EXTS) {
3826 ASSERT(realloc);
3827 page_idx = 0;
3828 erp_idx++;
3829 erp = erp_idx < nlists ? erp + 1 : NULL;
3830 break;
3831 } else {
3832 page_idx -= erp->er_extoff;
3833 break;
3834 }
3835 }
3836 *idxp = page_idx;
3837 *erp_idxp = erp_idx;
3838 return(erp);
3839}
3840
3841/*
3842 * Allocate and initialize an indirection array once the space needed
3843 * for incore extents increases above XFS_IEXT_BUFSZ.
3844 */
3845void
3846xfs_iext_irec_init(
3847 xfs_ifork_t *ifp) /* inode fork pointer */
3848{
3849 xfs_ext_irec_t *erp; /* indirection array pointer */
3850 xfs_extnum_t nextents; /* number of extents in file */
3851
3852 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3853 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3854 ASSERT(nextents <= XFS_LINEAR_EXTS);
3855
David Chinner67850732008-08-13 16:02:51 +10003856 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003857
3858 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003859 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003860 } else if (!ifp->if_real_bytes) {
3861 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3862 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3863 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3864 }
3865 erp->er_extbuf = ifp->if_u1.if_extents;
3866 erp->er_extcount = nextents;
3867 erp->er_extoff = 0;
3868
3869 ifp->if_flags |= XFS_IFEXTIREC;
3870 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3871 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3872 ifp->if_u1.if_ext_irec = erp;
3873
3874 return;
3875}
3876
3877/*
3878 * Allocate and initialize a new entry in the indirection array.
3879 */
3880xfs_ext_irec_t *
3881xfs_iext_irec_new(
3882 xfs_ifork_t *ifp, /* inode fork pointer */
3883 int erp_idx) /* index for new irec */
3884{
3885 xfs_ext_irec_t *erp; /* indirection array pointer */
3886 int i; /* loop counter */
3887 int nlists; /* number of irec's (ex lists) */
3888
3889 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3890 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3891
3892 /* Resize indirection array */
3893 xfs_iext_realloc_indirect(ifp, ++nlists *
3894 sizeof(xfs_ext_irec_t));
3895 /*
3896 * Move records down in the array so the
3897 * new page can use erp_idx.
3898 */
3899 erp = ifp->if_u1.if_ext_irec;
3900 for (i = nlists - 1; i > erp_idx; i--) {
3901 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3902 }
3903 ASSERT(i == erp_idx);
3904
3905 /* Initialize new extent record */
3906 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003907 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003908 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3909 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3910 erp[erp_idx].er_extcount = 0;
3911 erp[erp_idx].er_extoff = erp_idx > 0 ?
3912 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3913 return (&erp[erp_idx]);
3914}
3915
3916/*
3917 * Remove a record from the indirection array.
3918 */
3919void
3920xfs_iext_irec_remove(
3921 xfs_ifork_t *ifp, /* inode fork pointer */
3922 int erp_idx) /* irec index to remove */
3923{
3924 xfs_ext_irec_t *erp; /* indirection array pointer */
3925 int i; /* loop counter */
3926 int nlists; /* number of irec's (ex lists) */
3927
3928 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3929 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3930 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3931 if (erp->er_extbuf) {
3932 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3933 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003934 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003935 }
3936 /* Compact extent records */
3937 erp = ifp->if_u1.if_ext_irec;
3938 for (i = erp_idx; i < nlists - 1; i++) {
3939 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3940 }
3941 /*
3942 * Manually free the last extent record from the indirection
3943 * array. A call to xfs_iext_realloc_indirect() with a size
3944 * of zero would result in a call to xfs_iext_destroy() which
3945 * would in turn call this function again, creating a nasty
3946 * infinite loop.
3947 */
3948 if (--nlists) {
3949 xfs_iext_realloc_indirect(ifp,
3950 nlists * sizeof(xfs_ext_irec_t));
3951 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003952 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003953 }
3954 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3955}
3956
3957/*
3958 * This is called to clean up large amounts of unused memory allocated
3959 * by the indirection array. Before compacting anything though, verify
3960 * that the indirection array is still needed and switch back to the
3961 * linear extent list (or even the inline buffer) if possible. The
3962 * compaction policy is as follows:
3963 *
3964 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003965 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003966 * No Compaction: Extents occupy at least 50% of allocated space
3967 */
3968void
3969xfs_iext_irec_compact(
3970 xfs_ifork_t *ifp) /* inode fork pointer */
3971{
3972 xfs_extnum_t nextents; /* number of extents in file */
3973 int nlists; /* number of irec's (ex lists) */
3974
3975 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3976 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3977 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3978
3979 if (nextents == 0) {
3980 xfs_iext_destroy(ifp);
3981 } else if (nextents <= XFS_INLINE_EXTS) {
3982 xfs_iext_indirect_to_direct(ifp);
3983 xfs_iext_direct_to_inline(ifp, nextents);
3984 } else if (nextents <= XFS_LINEAR_EXTS) {
3985 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003986 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3987 xfs_iext_irec_compact_pages(ifp);
3988 }
3989}
3990
3991/*
3992 * Combine extents from neighboring extent pages.
3993 */
3994void
3995xfs_iext_irec_compact_pages(
3996 xfs_ifork_t *ifp) /* inode fork pointer */
3997{
3998 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3999 int erp_idx = 0; /* indirection array index */
4000 int nlists; /* number of irec's (ex lists) */
4001
4002 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4003 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4004 while (erp_idx < nlists - 1) {
4005 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4006 erp_next = erp + 1;
4007 if (erp_next->er_extcount <=
4008 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10004009 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004010 erp_next->er_extbuf, erp_next->er_extcount *
4011 sizeof(xfs_bmbt_rec_t));
4012 erp->er_extcount += erp_next->er_extcount;
4013 /*
4014 * Free page before removing extent record
4015 * so er_extoffs don't get modified in
4016 * xfs_iext_irec_remove.
4017 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10004018 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004019 erp_next->er_extbuf = NULL;
4020 xfs_iext_irec_remove(ifp, erp_idx + 1);
4021 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4022 } else {
4023 erp_idx++;
4024 }
4025 }
4026}
4027
4028/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004029 * This is called to update the er_extoff field in the indirection
4030 * array when extents have been added or removed from one of the
4031 * extent lists. erp_idx contains the irec index to begin updating
4032 * at and ext_diff contains the number of extents that were added
4033 * or removed.
4034 */
4035void
4036xfs_iext_irec_update_extoffs(
4037 xfs_ifork_t *ifp, /* inode fork pointer */
4038 int erp_idx, /* irec index to update */
4039 int ext_diff) /* number of new extents */
4040{
4041 int i; /* loop counter */
4042 int nlists; /* number of irec's (ex lists */
4043
4044 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4045 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4046 for (i = erp_idx; i < nlists; i++) {
4047 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4048 }
4049}
Brian Foster72b53ef2012-11-06 09:50:40 -05004050
4051/*
4052 * Test whether it is appropriate to check an inode for and free post EOF
4053 * blocks. The 'force' parameter determines whether we should also consider
4054 * regular files that are marked preallocated or append-only.
4055 */
4056bool
4057xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
4058{
4059 /* prealloc/delalloc exists only on regular files */
4060 if (!S_ISREG(ip->i_d.di_mode))
4061 return false;
4062
4063 /*
4064 * Zero sized files with no cached pages and delalloc blocks will not
4065 * have speculative prealloc/delalloc blocks to remove.
4066 */
4067 if (VFS_I(ip)->i_size == 0 &&
4068 VN_CACHED(VFS_I(ip)) == 0 &&
4069 ip->i_delayed_blks == 0)
4070 return false;
4071
4072 /* If we haven't read in the extent list, then don't do it now. */
4073 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
4074 return false;
4075
4076 /*
4077 * Do not free real preallocated or append-only files unless the file
4078 * has delalloc blocks and we are forced to remove them.
4079 */
4080 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
4081 if (!force || ip->i_delayed_blks == 0)
4082 return false;
4083
4084 return true;
4085}
4086