blob: 910b2da01042eec1f5811a6e5d47325e3df0e9d4 [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 Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110048#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050kmem_zone_t *xfs_ifork_zone;
51kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020054 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * freed from a file in a single transaction.
56 */
57#define XFS_ITRUNC_MAX_EXTENTS 2
58
59STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
60STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
61STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
62STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
63
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100064/*
65 * helper function to extract extent size hint from inode
66 */
67xfs_extlen_t
68xfs_get_extsz_hint(
69 struct xfs_inode *ip)
70{
71 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
72 return ip->i_d.di_extsize;
73 if (XFS_IS_REALTIME_INODE(ip))
74 return ip->i_mount->m_sb.sb_rextsize;
75 return 0;
76}
77
Dave Chinnerfa96aca2012-10-08 21:56:10 +110078/*
79 * This is a wrapper routine around the xfs_ilock() routine used to centralize
80 * some grungy code. It is used in places that wish to lock the inode solely
81 * for reading the extents. The reason these places can't just call
82 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
83 * extents from disk for a file in b-tree format. If the inode is in b-tree
84 * format, then we need to lock the inode exclusively until the extents are read
85 * in. Locking it exclusively all the time would limit our parallelism
86 * unnecessarily, though. What we do instead is check to see if the extents
87 * have been read in yet, and only lock the inode exclusively if they have not.
88 *
89 * The function returns a value which should be given to the corresponding
90 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
91 * actually taken.
92 */
93uint
94xfs_ilock_map_shared(
95 xfs_inode_t *ip)
96{
97 uint lock_mode;
98
99 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
100 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
101 lock_mode = XFS_ILOCK_EXCL;
102 } else {
103 lock_mode = XFS_ILOCK_SHARED;
104 }
105
106 xfs_ilock(ip, lock_mode);
107
108 return lock_mode;
109}
110
111/*
112 * This is simply the unlock routine to go with xfs_ilock_map_shared().
113 * All it does is call xfs_iunlock() with the given lock_mode.
114 */
115void
116xfs_iunlock_map_shared(
117 xfs_inode_t *ip,
118 unsigned int lock_mode)
119{
120 xfs_iunlock(ip, lock_mode);
121}
122
123/*
124 * The xfs inode contains 2 locks: a multi-reader lock called the
125 * i_iolock and a multi-reader lock called the i_lock. This routine
126 * allows either or both of the locks to be obtained.
127 *
128 * The 2 locks should always be ordered so that the IO lock is
129 * obtained first in order to prevent deadlock.
130 *
131 * ip -- the inode being locked
132 * lock_flags -- this parameter indicates the inode's locks
133 * to be locked. It can be:
134 * XFS_IOLOCK_SHARED,
135 * XFS_IOLOCK_EXCL,
136 * XFS_ILOCK_SHARED,
137 * XFS_ILOCK_EXCL,
138 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
139 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
140 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
141 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
142 */
143void
144xfs_ilock(
145 xfs_inode_t *ip,
146 uint lock_flags)
147{
148 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
149
150 /*
151 * You can't set both SHARED and EXCL for the same lock,
152 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
153 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
154 */
155 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
156 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
157 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
158 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
159 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
160
161 if (lock_flags & XFS_IOLOCK_EXCL)
162 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
163 else if (lock_flags & XFS_IOLOCK_SHARED)
164 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
165
166 if (lock_flags & XFS_ILOCK_EXCL)
167 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
168 else if (lock_flags & XFS_ILOCK_SHARED)
169 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
170}
171
172/*
173 * This is just like xfs_ilock(), except that the caller
174 * is guaranteed not to sleep. It returns 1 if it gets
175 * the requested locks and 0 otherwise. If the IO lock is
176 * obtained but the inode lock cannot be, then the IO lock
177 * is dropped before returning.
178 *
179 * ip -- the inode being locked
180 * lock_flags -- this parameter indicates the inode's locks to be
181 * to be locked. See the comment for xfs_ilock() for a list
182 * of valid values.
183 */
184int
185xfs_ilock_nowait(
186 xfs_inode_t *ip,
187 uint lock_flags)
188{
189 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
190
191 /*
192 * You can't set both SHARED and EXCL for the same lock,
193 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
194 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
195 */
196 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
197 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
198 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
199 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
200 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
201
202 if (lock_flags & XFS_IOLOCK_EXCL) {
203 if (!mrtryupdate(&ip->i_iolock))
204 goto out;
205 } else if (lock_flags & XFS_IOLOCK_SHARED) {
206 if (!mrtryaccess(&ip->i_iolock))
207 goto out;
208 }
209 if (lock_flags & XFS_ILOCK_EXCL) {
210 if (!mrtryupdate(&ip->i_lock))
211 goto out_undo_iolock;
212 } else if (lock_flags & XFS_ILOCK_SHARED) {
213 if (!mrtryaccess(&ip->i_lock))
214 goto out_undo_iolock;
215 }
216 return 1;
217
218 out_undo_iolock:
219 if (lock_flags & XFS_IOLOCK_EXCL)
220 mrunlock_excl(&ip->i_iolock);
221 else if (lock_flags & XFS_IOLOCK_SHARED)
222 mrunlock_shared(&ip->i_iolock);
223 out:
224 return 0;
225}
226
227/*
228 * xfs_iunlock() is used to drop the inode locks acquired with
229 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
230 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
231 * that we know which locks to drop.
232 *
233 * ip -- the inode being unlocked
234 * lock_flags -- this parameter indicates the inode's locks to be
235 * to be unlocked. See the comment for xfs_ilock() for a list
236 * of valid values for this parameter.
237 *
238 */
239void
240xfs_iunlock(
241 xfs_inode_t *ip,
242 uint lock_flags)
243{
244 /*
245 * You can't set both SHARED and EXCL for the same lock,
246 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
247 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
248 */
249 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
250 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
251 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
252 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
253 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
254 ASSERT(lock_flags != 0);
255
256 if (lock_flags & XFS_IOLOCK_EXCL)
257 mrunlock_excl(&ip->i_iolock);
258 else if (lock_flags & XFS_IOLOCK_SHARED)
259 mrunlock_shared(&ip->i_iolock);
260
261 if (lock_flags & XFS_ILOCK_EXCL)
262 mrunlock_excl(&ip->i_lock);
263 else if (lock_flags & XFS_ILOCK_SHARED)
264 mrunlock_shared(&ip->i_lock);
265
266 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
267}
268
269/*
270 * give up write locks. the i/o lock cannot be held nested
271 * if it is being demoted.
272 */
273void
274xfs_ilock_demote(
275 xfs_inode_t *ip,
276 uint lock_flags)
277{
278 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
279 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
280
281 if (lock_flags & XFS_ILOCK_EXCL)
282 mrdemote(&ip->i_lock);
283 if (lock_flags & XFS_IOLOCK_EXCL)
284 mrdemote(&ip->i_iolock);
285
286 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
287}
288
289#ifdef DEBUG
290int
291xfs_isilocked(
292 xfs_inode_t *ip,
293 uint lock_flags)
294{
295 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
296 if (!(lock_flags & XFS_ILOCK_SHARED))
297 return !!ip->i_lock.mr_writer;
298 return rwsem_is_locked(&ip->i_lock.mr_lock);
299 }
300
301 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
302 if (!(lock_flags & XFS_IOLOCK_SHARED))
303 return !!ip->i_iolock.mr_writer;
304 return rwsem_is_locked(&ip->i_iolock.mr_lock);
305 }
306
307 ASSERT(0);
308 return 0;
309}
310#endif
311
312void
313__xfs_iflock(
314 struct xfs_inode *ip)
315{
316 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
317 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
318
319 do {
320 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
321 if (xfs_isiflocked(ip))
322 io_schedule();
323 } while (!xfs_iflock_nowait(ip));
324
325 finish_wait(wq, &wait.wait);
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#ifdef DEBUG
329/*
330 * Make sure that the extents in the given memory buffer
331 * are valid.
332 */
333STATIC void
334xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100335 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_exntfmt_t fmt)
338{
339 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000340 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int i;
342
343 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000344 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
345 rec.l0 = get_unaligned(&ep->l0);
346 rec.l1 = get_unaligned(&ep->l1);
347 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (fmt == XFS_EXTFMT_NOSTATE)
349 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351}
352#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000353#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif /* DEBUG */
355
356/*
357 * Check that none of the inode's in the buffer have a next
358 * unlinked field of 0.
359 */
360#if defined(DEBUG)
361void
362xfs_inobp_check(
363 xfs_mount_t *mp,
364 xfs_buf_t *bp)
365{
366 int i;
367 int j;
368 xfs_dinode_t *dip;
369
370 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
371
372 for (i = 0; i < j; i++) {
373 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
374 i * mp->m_sb.sb_inodesize);
375 if (!dip->di_next_unlinked) {
Dave Chinner53487782011-03-07 10:05:35 +1100376 xfs_alert(mp,
377 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 bp);
379 ASSERT(dip->di_next_unlinked);
380 }
381 }
382}
383#endif
384
Dave Chinner612cfbf2012-11-14 17:52:32 +1100385static void
Dave Chinneraf133e82012-11-12 22:54:07 +1100386xfs_inode_buf_verify(
387 struct xfs_buf *bp)
388{
389 struct xfs_mount *mp = bp->b_target->bt_mount;
390 int i;
391 int ni;
392
393 /*
394 * Validate the magic number and version of every inode in the buffer
395 */
396 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
397 for (i = 0; i < ni; i++) {
398 int di_ok;
399 xfs_dinode_t *dip;
400
401 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
402 (i << mp->m_sb.sb_inodelog));
403 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
404 XFS_DINODE_GOOD_VERSION(dip->di_version);
405 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
406 XFS_ERRTAG_ITOBP_INOTOBP,
407 XFS_RANDOM_ITOBP_INOTOBP))) {
408 xfs_buf_ioerror(bp, EFSCORRUPTED);
409 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
410 mp, dip);
411#ifdef DEBUG
412 xfs_emerg(mp,
413 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
414 (unsigned long long)bp->b_bn, i,
415 be16_to_cpu(dip->di_magic));
416 ASSERT(0);
417#endif
418 }
419 }
420 xfs_inobp_check(mp, bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100421}
422
423static void
424xfs_inode_buf_write_verify(
425 struct xfs_buf *bp)
426{
427 xfs_inode_buf_verify(bp);
428}
429
430void
431xfs_inode_buf_read_verify(
432 struct xfs_buf *bp)
433{
434 xfs_inode_buf_verify(bp);
435 bp->b_pre_io = xfs_inode_buf_write_verify;
Dave Chinneraf133e82012-11-12 22:54:07 +1100436 bp->b_iodone = NULL;
437 xfs_buf_ioend(bp, 0);
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
Christoph Hellwig475ee412012-07-03 12:21:22 -0400441 * This routine is called to map an inode to the buffer containing the on-disk
442 * version of the inode. It returns a pointer to the buffer containing the
443 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
444 * pointer to the on-disk inode within that buffer.
445 *
446 * If a non-zero error is returned, then the contents of bpp and dipp are
447 * undefined.
David Chinner4ae29b42008-03-06 13:43:34 +1100448 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400449int
David Chinner4ae29b42008-03-06 13:43:34 +1100450xfs_imap_to_bp(
Christoph Hellwig475ee412012-07-03 12:21:22 -0400451 struct xfs_mount *mp,
452 struct xfs_trans *tp,
453 struct xfs_imap *imap,
Dave Chinneraf133e82012-11-12 22:54:07 +1100454 struct xfs_dinode **dipp,
Christoph Hellwig475ee412012-07-03 12:21:22 -0400455 struct xfs_buf **bpp,
456 uint buf_flags,
457 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100458{
Christoph Hellwig475ee412012-07-03 12:21:22 -0400459 struct xfs_buf *bp;
460 int error;
David Chinner4ae29b42008-03-06 13:43:34 +1100461
Dave Chinner611c9942012-04-23 15:59:07 +1000462 buf_flags |= XBF_UNMAPPED;
David Chinner4ae29b42008-03-06 13:43:34 +1100463 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
Dave Chinneraf133e82012-11-12 22:54:07 +1100464 (int)imap->im_len, buf_flags, &bp,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100465 xfs_inode_buf_read_verify);
David Chinner4ae29b42008-03-06 13:43:34 +1100466 if (error) {
Dave Chinneraf133e82012-11-12 22:54:07 +1100467 if (error == EAGAIN) {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000468 ASSERT(buf_flags & XBF_TRYLOCK);
Dave Chinneraf133e82012-11-12 22:54:07 +1100469 return error;
David Chinnera3f74ff2008-03-06 13:43:42 +1100470 }
Dave Chinneraf133e82012-11-12 22:54:07 +1100471
472 if (error == EFSCORRUPTED &&
473 (iget_flags & XFS_IGET_UNTRUSTED))
474 return XFS_ERROR(EINVAL);
475
476 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
477 __func__, error);
David Chinner4ae29b42008-03-06 13:43:34 +1100478 return error;
479 }
480
David Chinner4ae29b42008-03-06 13:43:34 +1100481 *bpp = bp;
Christoph Hellwig475ee412012-07-03 12:21:22 -0400482 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
David Chinner4ae29b42008-03-06 13:43:34 +1100483 return 0;
484}
485
486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Move inode type and inode format specific information from the
488 * on-disk inode to the in-core inode. For fifos, devs, and sockets
489 * this means set if_rdev to the proper value. For files, directories,
490 * and symlinks this means to bring in the in-line data or extent
491 * pointers. For a file in B-tree format, only the root is immediately
492 * brought in-core. The rest will be in-lined in if_extents when it
493 * is first referenced (see xfs_iread_extents()).
494 */
495STATIC int
496xfs_iformat(
497 xfs_inode_t *ip,
498 xfs_dinode_t *dip)
499{
500 xfs_attr_shortform_t *atp;
501 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000502 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100505 if (unlikely(be32_to_cpu(dip->di_nextents) +
506 be16_to_cpu(dip->di_anextents) >
507 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100508 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100509 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100511 (int)(be32_to_cpu(dip->di_nextents) +
512 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100514 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
516 ip->i_mount, dip);
517 return XFS_ERROR(EFSCORRUPTED);
518 }
519
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100520 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100521 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100523 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
525 ip->i_mount, dip);
526 return XFS_ERROR(EFSCORRUPTED);
527 }
528
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300529 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
530 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100531 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300532 "corrupt dinode %Lu, has realtime flag set.",
533 ip->i_ino);
534 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
535 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
536 return XFS_ERROR(EFSCORRUPTED);
537 }
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 switch (ip->i_d.di_mode & S_IFMT) {
540 case S_IFIFO:
541 case S_IFCHR:
542 case S_IFBLK:
543 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100544 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
546 ip->i_mount, dip);
547 return XFS_ERROR(EFSCORRUPTED);
548 }
549 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100550 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552
553 case S_IFREG:
554 case S_IFLNK:
555 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100556 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 case XFS_DINODE_FMT_LOCAL:
558 /*
559 * no local regular files yet
560 */
Al Viroabbede12011-07-26 02:31:30 -0400561 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100562 xfs_warn(ip->i_mount,
563 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 (unsigned long long) ip->i_ino);
565 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
566 XFS_ERRLEVEL_LOW,
567 ip->i_mount, dip);
568 return XFS_ERROR(EFSCORRUPTED);
569 }
570
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100571 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100573 xfs_warn(ip->i_mount,
574 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 (unsigned long long) ip->i_ino,
576 (long long) di_size);
577 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
578 XFS_ERRLEVEL_LOW,
579 ip->i_mount, dip);
580 return XFS_ERROR(EFSCORRUPTED);
581 }
582
583 size = (int)di_size;
584 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
585 break;
586 case XFS_DINODE_FMT_EXTENTS:
587 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
588 break;
589 case XFS_DINODE_FMT_BTREE:
590 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
591 break;
592 default:
593 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
594 ip->i_mount);
595 return XFS_ERROR(EFSCORRUPTED);
596 }
597 break;
598
599 default:
600 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
601 return XFS_ERROR(EFSCORRUPTED);
602 }
603 if (error) {
604 return error;
605 }
606 if (!XFS_DFORK_Q(dip))
607 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000610 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000611
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100612 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 case XFS_DINODE_FMT_LOCAL:
614 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100615 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100616
617 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100618 xfs_warn(ip->i_mount,
619 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100620 (unsigned long long) ip->i_ino,
621 (long long) size);
622 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
623 XFS_ERRLEVEL_LOW,
624 ip->i_mount, dip);
625 return XFS_ERROR(EFSCORRUPTED);
626 }
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
629 break;
630 case XFS_DINODE_FMT_EXTENTS:
631 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
632 break;
633 case XFS_DINODE_FMT_BTREE:
634 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
635 break;
636 default:
637 error = XFS_ERROR(EFSCORRUPTED);
638 break;
639 }
640 if (error) {
641 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
642 ip->i_afp = NULL;
643 xfs_idestroy_fork(ip, XFS_DATA_FORK);
644 }
645 return error;
646}
647
648/*
649 * The file is in-lined in the on-disk inode.
650 * If it fits into if_inline_data, then copy
651 * it there, otherwise allocate a buffer for it
652 * and copy the data there. Either way, set
653 * if_data to point at the data.
654 * If we allocate a buffer for the data, make
655 * sure that its size is a multiple of 4 and
656 * record the real size in i_real_bytes.
657 */
658STATIC int
659xfs_iformat_local(
660 xfs_inode_t *ip,
661 xfs_dinode_t *dip,
662 int whichfork,
663 int size)
664{
665 xfs_ifork_t *ifp;
666 int real_size;
667
668 /*
669 * If the size is unreasonable, then something
670 * is wrong and we just bail out rather than crash in
671 * kmem_alloc() or memcpy() below.
672 */
673 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100674 xfs_warn(ip->i_mount,
675 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 (unsigned long long) ip->i_ino, size,
677 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
678 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
679 ip->i_mount, dip);
680 return XFS_ERROR(EFSCORRUPTED);
681 }
682 ifp = XFS_IFORK_PTR(ip, whichfork);
683 real_size = 0;
684 if (size == 0)
685 ifp->if_u1.if_data = NULL;
686 else if (size <= sizeof(ifp->if_u2.if_inline_data))
687 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
688 else {
689 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000690 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 ifp->if_bytes = size;
693 ifp->if_real_bytes = real_size;
694 if (size)
695 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
696 ifp->if_flags &= ~XFS_IFEXTENTS;
697 ifp->if_flags |= XFS_IFINLINE;
698 return 0;
699}
700
701/*
702 * The file consists of a set of extents all
703 * of which fit into the on-disk inode.
704 * If there are few enough extents to fit into
705 * the if_inline_ext, then copy them there.
706 * Otherwise allocate a buffer for them and copy
707 * them into it. Either way, set if_extents
708 * to point at the extents.
709 */
710STATIC int
711xfs_iformat_extents(
712 xfs_inode_t *ip,
713 xfs_dinode_t *dip,
714 int whichfork)
715{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000716 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 xfs_ifork_t *ifp;
718 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int size;
720 int i;
721
722 ifp = XFS_IFORK_PTR(ip, whichfork);
723 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
724 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
725
726 /*
727 * If the number of extents is unreasonable, then something
728 * is wrong and we just bail out rather than crash in
729 * kmem_alloc() or memcpy() below.
730 */
731 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100732 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 (unsigned long long) ip->i_ino, nex);
734 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
735 ip->i_mount, dip);
736 return XFS_ERROR(EFSCORRUPTED);
737 }
738
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100739 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (nex == 0)
741 ifp->if_u1.if_extents = NULL;
742 else if (nex <= XFS_INLINE_EXTS)
743 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100744 else
745 xfs_iext_add(ifp, 0, nex);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (size) {
749 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000750 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100751 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000752 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000753 ep->l0 = get_unaligned_be64(&dp->l0);
754 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000756 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (whichfork != XFS_DATA_FORK ||
758 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
759 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100760 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
762 XFS_ERRLEVEL_LOW,
763 ip->i_mount);
764 return XFS_ERROR(EFSCORRUPTED);
765 }
766 }
767 ifp->if_flags |= XFS_IFEXTENTS;
768 return 0;
769}
770
771/*
772 * The file has too many extents to fit into
773 * the inode, so they are in B-tree format.
774 * Allocate a buffer for the root of the B-tree
775 * and copy the root into it. The i_extents
776 * field will remain NULL until all of the
777 * extents are read in (when they are needed).
778 */
779STATIC int
780xfs_iformat_btree(
781 xfs_inode_t *ip,
782 xfs_dinode_t *dip,
783 int whichfork)
784{
785 xfs_bmdr_block_t *dfp;
786 xfs_ifork_t *ifp;
787 /* REFERENCED */
788 int nrecs;
789 int size;
790
791 ifp = XFS_IFORK_PTR(ip, whichfork);
792 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
793 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100794 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /*
797 * blow out if -- fork has less extents than can fit in
798 * fork (fork shouldn't be a btree format), root btree
799 * block has more records than can fit into the fork,
800 * or the number of extents is greater than the number of
801 * blocks.
802 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000803 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
804 XFS_IFORK_MAXEXT(ip, whichfork) ||
805 XFS_BMDR_SPACE_CALC(nrecs) >
806 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
807 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100808 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100810 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
811 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return XFS_ERROR(EFSCORRUPTED);
813 }
814
815 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000816 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ASSERT(ifp->if_broot != NULL);
818 /*
819 * Copy and convert from the on-disk structure
820 * to the in-memory structure.
821 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100822 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
823 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
824 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ifp->if_flags &= ~XFS_IFEXTENTS;
826 ifp->if_flags |= XFS_IFBROOT;
827
828 return 0;
829}
830
Eric Sandeend96f8f82009-07-02 00:09:33 -0500831STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000832xfs_dinode_from_disk(
833 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100834 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000836 to->di_magic = be16_to_cpu(from->di_magic);
837 to->di_mode = be16_to_cpu(from->di_mode);
838 to->di_version = from ->di_version;
839 to->di_format = from->di_format;
840 to->di_onlink = be16_to_cpu(from->di_onlink);
841 to->di_uid = be32_to_cpu(from->di_uid);
842 to->di_gid = be32_to_cpu(from->di_gid);
843 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000844 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
845 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000846 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
847 to->di_flushiter = be16_to_cpu(from->di_flushiter);
848 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
849 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
850 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
851 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
852 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
853 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
854 to->di_size = be64_to_cpu(from->di_size);
855 to->di_nblocks = be64_to_cpu(from->di_nblocks);
856 to->di_extsize = be32_to_cpu(from->di_extsize);
857 to->di_nextents = be32_to_cpu(from->di_nextents);
858 to->di_anextents = be16_to_cpu(from->di_anextents);
859 to->di_forkoff = from->di_forkoff;
860 to->di_aformat = from->di_aformat;
861 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
862 to->di_dmstate = be16_to_cpu(from->di_dmstate);
863 to->di_flags = be16_to_cpu(from->di_flags);
864 to->di_gen = be32_to_cpu(from->di_gen);
865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000867void
868xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100869 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000870 xfs_icdinode_t *from)
871{
872 to->di_magic = cpu_to_be16(from->di_magic);
873 to->di_mode = cpu_to_be16(from->di_mode);
874 to->di_version = from ->di_version;
875 to->di_format = from->di_format;
876 to->di_onlink = cpu_to_be16(from->di_onlink);
877 to->di_uid = cpu_to_be32(from->di_uid);
878 to->di_gid = cpu_to_be32(from->di_gid);
879 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000880 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
881 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000882 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
883 to->di_flushiter = cpu_to_be16(from->di_flushiter);
884 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
885 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
886 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
887 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
888 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
889 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
890 to->di_size = cpu_to_be64(from->di_size);
891 to->di_nblocks = cpu_to_be64(from->di_nblocks);
892 to->di_extsize = cpu_to_be32(from->di_extsize);
893 to->di_nextents = cpu_to_be32(from->di_nextents);
894 to->di_anextents = cpu_to_be16(from->di_anextents);
895 to->di_forkoff = from->di_forkoff;
896 to->di_aformat = from->di_aformat;
897 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
898 to->di_dmstate = cpu_to_be16(from->di_dmstate);
899 to->di_flags = cpu_to_be16(from->di_flags);
900 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903STATIC uint
904_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 __uint16_t di_flags)
906{
907 uint flags = 0;
908
909 if (di_flags & XFS_DIFLAG_ANY) {
910 if (di_flags & XFS_DIFLAG_REALTIME)
911 flags |= XFS_XFLAG_REALTIME;
912 if (di_flags & XFS_DIFLAG_PREALLOC)
913 flags |= XFS_XFLAG_PREALLOC;
914 if (di_flags & XFS_DIFLAG_IMMUTABLE)
915 flags |= XFS_XFLAG_IMMUTABLE;
916 if (di_flags & XFS_DIFLAG_APPEND)
917 flags |= XFS_XFLAG_APPEND;
918 if (di_flags & XFS_DIFLAG_SYNC)
919 flags |= XFS_XFLAG_SYNC;
920 if (di_flags & XFS_DIFLAG_NOATIME)
921 flags |= XFS_XFLAG_NOATIME;
922 if (di_flags & XFS_DIFLAG_NODUMP)
923 flags |= XFS_XFLAG_NODUMP;
924 if (di_flags & XFS_DIFLAG_RTINHERIT)
925 flags |= XFS_XFLAG_RTINHERIT;
926 if (di_flags & XFS_DIFLAG_PROJINHERIT)
927 flags |= XFS_XFLAG_PROJINHERIT;
928 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
929 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100930 if (di_flags & XFS_DIFLAG_EXTSIZE)
931 flags |= XFS_XFLAG_EXTSIZE;
932 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
933 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000934 if (di_flags & XFS_DIFLAG_NODEFRAG)
935 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000936 if (di_flags & XFS_DIFLAG_FILESTREAM)
937 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 return flags;
941}
942
943uint
944xfs_ip2xflags(
945 xfs_inode_t *ip)
946{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000947 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Nathan Scotta916e2b2006-06-09 17:12:17 +1000949 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100950 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953uint
954xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100955 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100957 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100958 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100962 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
964int
965xfs_iread(
966 xfs_mount_t *mp,
967 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100968 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100969 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 xfs_buf_t *bp;
972 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int error;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100976 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100978 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100979 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100983 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100984 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400985 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100986 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100987 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100988
989 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * If we got something that isn't an inode it means someone
991 * (nfs or dmi) has a stale handle.
992 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200993 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100995 xfs_alert(mp,
996 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
997 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100999 error = XFS_ERROR(EINVAL);
1000 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
1003 /*
1004 * If the on-disk inode is already linked to a directory
1005 * entry, copy all of the inode into the in-core inode.
1006 * xfs_iformat() handles copying in the inode format
1007 * specific information.
1008 * Otherwise, just get the truly permanent information.
1009 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001010 if (dip->di_mode) {
1011 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 error = xfs_iformat(ip, dip);
1013 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +11001015 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
1016 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001018 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001021 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1022 ip->i_d.di_version = dip->di_version;
1023 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1024 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * Make sure to pull in the mode here as well in
1027 * case the inode is released without being used.
1028 * This ensures that xfs_inactive() will see that
1029 * the inode is already free and not try to mess
1030 * with the uninitialized part of it.
1031 */
1032 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
1036 * The inode format changed when we moved the link count and
1037 * made it 32 bits long. If this is an old format inode,
1038 * convert it in memory to look like a new one. If it gets
1039 * flushed to disk we will convert back before flushing or
1040 * logging it. We zero out the new projid field and the old link
1041 * count field. We'll handle clearing the pad field (the remains
1042 * of the old uuid field) when we actually convert the inode to
1043 * the new format. We don't change the version number so that we
1044 * can distinguish this from a real new format inode.
1045 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001046 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 ip->i_d.di_nlink = ip->i_d.di_onlink;
1048 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001049 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
1052 ip->i_delayed_blks = 0;
1053
1054 /*
1055 * Mark the buffer containing the inode as something to keep
1056 * around for a while. This helps to keep recently accessed
1057 * meta-data in-core longer.
1058 */
Dave Chinner821eb212010-12-02 16:31:13 +11001059 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /*
1062 * Use xfs_trans_brelse() to release the buffer containing the
1063 * on-disk inode, because it was acquired with xfs_trans_read_buf()
Christoph Hellwig475ee412012-07-03 12:21:22 -04001064 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1066 * will only release the buffer if it is not dirty within the
1067 * transaction. It will be OK to release the buffer in this case,
1068 * because inodes on disk are never destroyed and we will be
1069 * locking the new in-core inode before putting it in the hash
1070 * table where other processes can find it. Thus we don't have
1071 * to worry about the inode being changed just because we released
1072 * the buffer.
1073 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001074 out_brelse:
1075 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001076 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
1080 * Read in extents from a btree-format inode.
1081 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1082 */
1083int
1084xfs_iread_extents(
1085 xfs_trans_t *tp,
1086 xfs_inode_t *ip,
1087 int whichfork)
1088{
1089 int error;
1090 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001091 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1094 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1095 ip->i_mount);
1096 return XFS_ERROR(EFSCORRUPTED);
1097 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001098 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * We know that the size is valid (it's checked in iformat_btree)
1103 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001104 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001106 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 error = xfs_bmap_read_extents(tp, ip, whichfork);
1108 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001109 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 ifp->if_flags &= ~XFS_IFEXTENTS;
1111 return error;
1112 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001113 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115}
1116
1117/*
1118 * Allocate an inode on disk and return a copy of its in-core version.
1119 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1120 * appropriately within the inode. The uid and gid for the inode are
1121 * set according to the contents of the given cred structure.
1122 *
1123 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001124 * has a free inode available, call xfs_iget() to obtain the in-core
1125 * version of the allocated inode. Finally, fill in the inode and
1126 * log its initial contents. In this case, ialloc_context would be
1127 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001129 * If xfs_dialloc() does not have an available inode, it will replenish
1130 * its supply by doing an allocation. Since we can only do one
1131 * allocation within a transaction without deadlocks, we must commit
1132 * the current transaction before returning the inode itself.
1133 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * The caller should then commit the current transaction, start a new
1135 * transaction, and call xfs_ialloc() again to actually get the inode.
1136 *
1137 * To ensure that some other process does not grab the inode that
1138 * was allocated during the first call to xfs_ialloc(), this routine
1139 * also returns the [locked] bp pointing to the head of the freelist
1140 * as ialloc_context. The caller should hold this buffer across
1141 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +10001142 *
1143 * If we are allocating quota inodes, we do not have a parent inode
1144 * to attach to or associate with (i.e. pip == NULL) because they
1145 * are not linked into the directory structure - they are attached
1146 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 */
1148int
1149xfs_ialloc(
1150 xfs_trans_t *tp,
1151 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -04001152 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -07001153 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001155 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int okalloc,
1157 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 xfs_inode_t **ipp)
1159{
1160 xfs_ino_t ino;
1161 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 uint flags;
1163 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001164 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +11001165 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * Call the space management code to pick
1169 * the on-disk inode to be allocated.
1170 */
David Chinnerb11f94d2007-07-11 11:09:33 +10001171 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -04001172 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +11001173 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -04001175 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 *ipp = NULL;
1177 return 0;
1178 }
1179 ASSERT(*ialloc_context == NULL);
1180
1181 /*
1182 * Get the in-core inode with the lock held exclusively.
1183 * This is because we're setting fields here we need
1184 * to prevent others from looking at until we're done.
1185 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001186 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
1187 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +11001188 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ASSERT(ip != NULL);
1191
Al Viro576b1d62011-07-26 02:50:15 -04001192 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ip->i_d.di_onlink = 0;
1194 ip->i_d.di_nlink = nlink;
1195 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +01001196 ip->i_d.di_uid = current_fsuid();
1197 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001198 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1200
1201 /*
1202 * If the superblock version is up to where we support new format
1203 * inodes and this is currently an old format inode, then change
1204 * the inode version number now. This way we only do the conversion
1205 * here rather than here and in the flush/logging code.
1206 */
Eric Sandeen62118702008-03-06 13:44:28 +11001207 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001208 ip->i_d.di_version == 1) {
1209 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /*
1211 * We've already zeroed the old link count, the projid field,
1212 * and the pad field.
1213 */
1214 }
1215
1216 /*
1217 * Project ids won't be stored on disk if we are using a version 1 inode.
1218 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001219 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_bump_ino_vers2(tp, ip);
1221
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001222 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001224 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ip->i_d.di_mode |= S_ISGID;
1226 }
1227 }
1228
1229 /*
1230 * If the group ID of the new file does not match the effective group
1231 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1232 * (and only if the irix_sgid_inherit compatibility variable is set).
1233 */
1234 if ((irix_sgid_inherit) &&
1235 (ip->i_d.di_mode & S_ISGID) &&
1236 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1237 ip->i_d.di_mode &= ~S_ISGID;
1238 }
1239
1240 ip->i_d.di_size = 0;
1241 ip->i_d.di_nextents = 0;
1242 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001243
1244 nanotime(&tv);
1245 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1246 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1247 ip->i_d.di_atime = ip->i_d.di_mtime;
1248 ip->i_d.di_ctime = ip->i_d.di_mtime;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * di_gen will have been taken care of in xfs_iread.
1252 */
1253 ip->i_d.di_extsize = 0;
1254 ip->i_d.di_dmevmask = 0;
1255 ip->i_d.di_dmstate = 0;
1256 ip->i_d.di_flags = 0;
1257 flags = XFS_ILOG_CORE;
1258 switch (mode & S_IFMT) {
1259 case S_IFIFO:
1260 case S_IFCHR:
1261 case S_IFBLK:
1262 case S_IFSOCK:
1263 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1264 ip->i_df.if_u2.if_rdev = rdev;
1265 ip->i_df.if_flags = 0;
1266 flags |= XFS_ILOG_DEV;
1267 break;
1268 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001269 /*
1270 * we can't set up filestreams until after the VFS inode
1271 * is set up properly.
1272 */
1273 if (pip && xfs_inode_is_filestream(pip))
1274 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001275 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001277 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001278 uint di_flags = 0;
1279
Al Viroabbede12011-07-26 02:31:30 -04001280 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001281 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1282 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001283 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1284 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1285 ip->i_d.di_extsize = pip->i_d.di_extsize;
1286 }
Al Viroabbede12011-07-26 02:31:30 -04001287 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001288 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001289 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001290 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1291 di_flags |= XFS_DIFLAG_EXTSIZE;
1292 ip->i_d.di_extsize = pip->i_d.di_extsize;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1296 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001297 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1299 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001300 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1302 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001303 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1305 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001306 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1307 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1308 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001309 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1310 xfs_inherit_nodefrag)
1311 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001312 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1313 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001314 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 /* FALLTHROUGH */
1317 case S_IFLNK:
1318 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1319 ip->i_df.if_flags = XFS_IFEXTENTS;
1320 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1321 ip->i_df.if_u1.if_extents = NULL;
1322 break;
1323 default:
1324 ASSERT(0);
1325 }
1326 /*
1327 * Attribute fork settings for new inode.
1328 */
1329 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1330 ip->i_d.di_anextents = 0;
1331
1332 /*
1333 * Log the new values stuffed into the inode.
1334 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001335 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 xfs_trans_log_inode(tp, ip, flags);
1337
Nathan Scottb83bd132006-06-09 16:48:30 +10001338 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001339 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
David Chinnerbf904242008-10-30 17:36:14 +11001341 /* now we have set up the vfs inode we can associate the filestream */
1342 if (filestreams) {
1343 error = xfs_filestream_associate(pip, ip);
1344 if (error < 0)
1345 return -error;
1346 if (!error)
1347 xfs_iflags_set(ip, XFS_IFILESTREAM);
1348 }
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *ipp = ip;
1351 return 0;
1352}
1353
1354/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001355 * Free up the underlying blocks past new_size. The new size must be smaller
1356 * than the current size. This routine can be used both for the attribute and
1357 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
David Chinnerf6485052008-04-17 16:50:04 +10001359 * The transaction passed to this routine must have made a permanent log
1360 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1361 * given transaction and start new ones, so make sure everything involved in
1362 * the transaction is tidy before calling here. Some transaction will be
1363 * returned to the caller to be committed. The incoming transaction must
1364 * already include the inode, and both inode locks must be held exclusively.
1365 * The inode must also be "held" within the transaction. On return the inode
1366 * will be "held" within the returned transaction. This routine does NOT
1367 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 *
David Chinnerf6485052008-04-17 16:50:04 +10001369 * If we get an error, we must return with the inode locked and linked into the
1370 * current transaction. This keeps things simple for the higher level code,
1371 * because it always knows that the inode is locked and held in the transaction
1372 * that returns to it whether errors occur or not. We don't mark the inode
1373 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
1375int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001376xfs_itruncate_extents(
1377 struct xfs_trans **tpp,
1378 struct xfs_inode *ip,
1379 int whichfork,
1380 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001382 struct xfs_mount *mp = ip->i_mount;
1383 struct xfs_trans *tp = *tpp;
1384 struct xfs_trans *ntp;
1385 xfs_bmap_free_t free_list;
1386 xfs_fsblock_t first_block;
1387 xfs_fileoff_t first_unmap_block;
1388 xfs_fileoff_t last_block;
1389 xfs_filblks_t unmap_len;
1390 int committed;
1391 int error = 0;
1392 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Christoph Hellwig0b561852012-07-04 11:13:31 -04001394 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1395 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1396 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001397 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001398 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001400 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001401 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001403 trace_xfs_itruncate_extents_start(ip, new_size);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * Since it is possible for space to become allocated beyond
1407 * the end of the file (in a crash where the space is allocated
1408 * but the inode size is not yet updated), simply remove any
1409 * blocks which show up between the new EOF and the maximum
1410 * possible file size. If the first block to be removed is
1411 * beyond the maximum file size (ie it is the same as last_block),
1412 * then there is nothing to do.
1413 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001414 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001415 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001416 if (first_unmap_block == last_block)
1417 return 0;
1418
1419 ASSERT(first_unmap_block < last_block);
1420 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001422 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001423 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001424 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001425 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001427 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001428 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001429 if (error)
1430 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /*
1433 * Duplicate the transaction that has the permanent
1434 * reservation and commit the old transaction.
1435 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001436 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001437 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001438 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001439 if (error)
1440 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (committed) {
1443 /*
David Chinnerf6485052008-04-17 16:50:04 +10001444 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001445 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001447 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
David Chinnerf6485052008-04-17 16:50:04 +10001449
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001450 ntp = xfs_trans_dup(tp);
1451 error = xfs_trans_commit(tp, 0);
1452 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001453
Christoph Hellwigddc34152011-09-19 15:00:54 +00001454 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001455
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001456 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001457 goto out;
1458
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001459 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001460 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001461 * reference that we gained in xfs_trans_dup()
1462 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001463 xfs_log_ticket_put(tp->t_ticket);
1464 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001465 XFS_ITRUNCATE_LOG_RES(mp), 0,
1466 XFS_TRANS_PERM_LOG_RES,
1467 XFS_ITRUNCATE_LOG_COUNT);
1468 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001471
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001472 /*
1473 * Always re-log the inode so that our permanent transaction can keep
1474 * on rolling it forward in the log.
1475 */
1476 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1477
1478 trace_xfs_itruncate_extents_end(ip, new_size);
1479
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001480out:
1481 *tpp = tp;
1482 return error;
1483out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001485 * If the bunmapi call encounters an error, return to the caller where
1486 * the transaction can be properly aborted. We just need to make sure
1487 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001489 xfs_bmap_cancel(&free_list);
1490 goto out;
1491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/*
1494 * This is called when the inode's link count goes to 0.
1495 * We place the on-disk inode on a list in the AGI. It
1496 * will be pulled from this list when the inode is freed.
1497 */
1498int
1499xfs_iunlink(
1500 xfs_trans_t *tp,
1501 xfs_inode_t *ip)
1502{
1503 xfs_mount_t *mp;
1504 xfs_agi_t *agi;
1505 xfs_dinode_t *dip;
1506 xfs_buf_t *agibp;
1507 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 xfs_agino_t agino;
1509 short bucket_index;
1510 int offset;
1511 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 ASSERT(ip->i_d.di_nlink == 0);
1514 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 mp = tp->t_mountp;
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 /*
1519 * Get the agi buffer first. It ensures lock ordering
1520 * on the list.
1521 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001522 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001523 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Get the index into the agi hash table for the
1529 * list this inode will go on.
1530 */
1531 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1532 ASSERT(agino != 0);
1533 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1534 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001535 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001537 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /*
1539 * There is already another inode in the bucket we need
1540 * to add ourselves to. Add us at the front of the list.
1541 * Here we put the head pointer into our next pointer,
1542 * and then we fall through to point the head at us.
1543 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001544 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1545 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001546 if (error)
1547 return error;
1548
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001549 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001551 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 offsetof(xfs_dinode_t, di_next_unlinked);
1553 xfs_trans_inode_buf(tp, ibp);
1554 xfs_trans_log_buf(tp, ibp, offset,
1555 (offset + sizeof(xfs_agino_t) - 1));
1556 xfs_inobp_check(mp, ibp);
1557 }
1558
1559 /*
1560 * Point the bucket head pointer at the inode being inserted.
1561 */
1562 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001563 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 offset = offsetof(xfs_agi_t, agi_unlinked) +
1565 (sizeof(xfs_agino_t) * bucket_index);
1566 xfs_trans_log_buf(tp, agibp, offset,
1567 (offset + sizeof(xfs_agino_t) - 1));
1568 return 0;
1569}
1570
1571/*
1572 * Pull the on-disk inode from the AGI unlinked list.
1573 */
1574STATIC int
1575xfs_iunlink_remove(
1576 xfs_trans_t *tp,
1577 xfs_inode_t *ip)
1578{
1579 xfs_ino_t next_ino;
1580 xfs_mount_t *mp;
1581 xfs_agi_t *agi;
1582 xfs_dinode_t *dip;
1583 xfs_buf_t *agibp;
1584 xfs_buf_t *ibp;
1585 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfs_agino_t agino;
1587 xfs_agino_t next_agino;
1588 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001589 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001591 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
1598 * Get the agi buffer first. It ensures lock ordering
1599 * on the list.
1600 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001601 error = xfs_read_agi(mp, tp, agno, &agibp);
1602 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Get the index into the agi hash table for the
1609 * list this inode will go on.
1610 */
1611 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1612 ASSERT(agino != 0);
1613 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001614 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ASSERT(agi->agi_unlinked[bucket_index]);
1616
Christoph Hellwig16259e72005-11-02 15:11:25 +11001617 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001619 * We're at the head of the list. Get the inode's on-disk
1620 * buffer to see if there is anyone after us on the list.
1621 * Only modify our next pointer if it is not already NULLAGINO.
1622 * This saves us the overhead of dealing with the buffer when
1623 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001625 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1626 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001628 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001629 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return error;
1631 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001632 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ASSERT(next_agino != 0);
1634 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001635 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001636 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 offsetof(xfs_dinode_t, di_next_unlinked);
1638 xfs_trans_inode_buf(tp, ibp);
1639 xfs_trans_log_buf(tp, ibp, offset,
1640 (offset + sizeof(xfs_agino_t) - 1));
1641 xfs_inobp_check(mp, ibp);
1642 } else {
1643 xfs_trans_brelse(tp, ibp);
1644 }
1645 /*
1646 * Point the bucket head pointer at the next inode.
1647 */
1648 ASSERT(next_agino != 0);
1649 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001650 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 offset = offsetof(xfs_agi_t, agi_unlinked) +
1652 (sizeof(xfs_agino_t) * bucket_index);
1653 xfs_trans_log_buf(tp, agibp, offset,
1654 (offset + sizeof(xfs_agino_t) - 1));
1655 } else {
1656 /*
1657 * We need to search the list for the inode being freed.
1658 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001659 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 last_ibp = NULL;
1661 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001662 struct xfs_imap imap;
1663
1664 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001666
1667 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001669
1670 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001672 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001673 "%s: xfs_imap returned error %d.",
1674 __func__, error);
1675 return error;
1676 }
1677
1678 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1679 &last_ibp, 0, 0);
1680 if (error) {
1681 xfs_warn(mp,
1682 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001683 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return error;
1685 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001686
1687 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001688 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ASSERT(next_agino != NULLAGINO);
1690 ASSERT(next_agino != 0);
1691 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001694 * Now last_ibp points to the buffer previous to us on the
1695 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001697 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1698 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001700 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001701 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return error;
1703 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001704 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ASSERT(next_agino != 0);
1706 ASSERT(next_agino != agino);
1707 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001708 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001709 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 offsetof(xfs_dinode_t, di_next_unlinked);
1711 xfs_trans_inode_buf(tp, ibp);
1712 xfs_trans_log_buf(tp, ibp, offset,
1713 (offset + sizeof(xfs_agino_t) - 1));
1714 xfs_inobp_check(mp, ibp);
1715 } else {
1716 xfs_trans_brelse(tp, ibp);
1717 }
1718 /*
1719 * Point the previous inode on the list to the next inode.
1720 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001721 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ASSERT(next_agino != 0);
1723 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1724 xfs_trans_inode_buf(tp, last_ibp);
1725 xfs_trans_log_buf(tp, last_ibp, offset,
1726 (offset + sizeof(xfs_agino_t) - 1));
1727 xfs_inobp_check(mp, last_ibp);
1728 }
1729 return 0;
1730}
1731
Dave Chinner5b3eed72010-08-24 11:42:41 +10001732/*
1733 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1734 * inodes that are in memory - they all must be marked stale and attached to
1735 * the cluster buffer.
1736 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001737STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738xfs_ifree_cluster(
1739 xfs_inode_t *free_ip,
1740 xfs_trans_t *tp,
1741 xfs_ino_t inum)
1742{
1743 xfs_mount_t *mp = free_ip->i_mount;
1744 int blks_per_cluster;
1745 int nbufs;
1746 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001747 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 xfs_daddr_t blkno;
1749 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001750 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 xfs_inode_log_item_t *iip;
1752 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001753 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Dave Chinner5017e972010-01-11 11:47:40 +00001755 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1757 blks_per_cluster = 1;
1758 ninodes = mp->m_sb.sb_inopblock;
1759 nbufs = XFS_IALLOC_BLOCKS(mp);
1760 } else {
1761 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1762 mp->m_sb.sb_blocksize;
1763 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1764 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1765 }
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 for (j = 0; j < nbufs; j++, inum += ninodes) {
1768 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1769 XFS_INO_TO_AGBNO(mp, inum));
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001772 * We obtain and lock the backing buffer first in the process
1773 * here, as we have to ensure that any dirty inode that we
1774 * can't get the flush lock on is attached to the buffer.
1775 * If we scan the in-memory inodes first, then buffer IO can
1776 * complete before we get a lock on it, and hence we may fail
1777 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001779 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11001780 mp->m_bsize * blks_per_cluster,
1781 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001783 if (!bp)
1784 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001785 /*
1786 * Walk the inodes already attached to the buffer and mark them
1787 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001788 * in-memory inode walk can't lock them. By marking them all
1789 * stale first, we will not attempt to lock them in the loop
1790 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001791 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001792 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 while (lip) {
1794 if (lip->li_type == XFS_LI_INODE) {
1795 iip = (xfs_inode_log_item_t *)lip;
1796 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001797 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001798 xfs_trans_ail_copy_lsn(mp->m_ail,
1799 &iip->ili_flush_lsn,
1800 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001801 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 lip = lip->li_bio_list;
1804 }
1805
Dave Chinner5b3eed72010-08-24 11:42:41 +10001806
Dave Chinner5b257b42010-06-03 16:22:29 +10001807 /*
1808 * For each inode in memory attempt to add it to the inode
1809 * buffer and set it up for being staled on buffer IO
1810 * completion. This is safe as we've locked out tail pushing
1811 * and flushing by locking the buffer.
1812 *
1813 * We have already marked every inode that was part of a
1814 * transaction stale above, which means there is no point in
1815 * even trying to lock them.
1816 */
1817 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001818retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001819 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001820 ip = radix_tree_lookup(&pag->pag_ici_root,
1821 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001823 /* Inode not in memory, nothing to do */
1824 if (!ip) {
1825 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001826 continue;
1827 }
1828
Dave Chinner5b3eed72010-08-24 11:42:41 +10001829 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001830 * because this is an RCU protected lookup, we could
1831 * find a recently freed or even reallocated inode
1832 * during the lookup. We need to check under the
1833 * i_flags_lock for a valid inode here. Skip it if it
1834 * is not valid, the wrong inode or stale.
1835 */
1836 spin_lock(&ip->i_flags_lock);
1837 if (ip->i_ino != inum + i ||
1838 __xfs_iflags_test(ip, XFS_ISTALE)) {
1839 spin_unlock(&ip->i_flags_lock);
1840 rcu_read_unlock();
1841 continue;
1842 }
1843 spin_unlock(&ip->i_flags_lock);
1844
1845 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001846 * Don't try to lock/unlock the current inode, but we
1847 * _cannot_ skip the other inodes that we did not find
1848 * in the list attached to the buffer and are not
1849 * already marked stale. If we can't lock it, back off
1850 * and retry.
1851 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001852 if (ip != free_ip &&
1853 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001854 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001855 delay(1);
1856 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001857 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001858 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001859
Dave Chinner5b3eed72010-08-24 11:42:41 +10001860 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001861 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001862
Dave Chinner5b3eed72010-08-24 11:42:41 +10001863 /*
1864 * we don't need to attach clean inodes or those only
1865 * with unlogged changes (which we throw away, anyway).
1866 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001867 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001868 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001869 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 xfs_ifunlock(ip);
1871 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1872 continue;
1873 }
1874
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001875 iip->ili_last_fields = iip->ili_fields;
1876 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001878 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1879 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001881 xfs_buf_attach_iodone(bp, xfs_istale_done,
1882 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001883
1884 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Dave Chinner5b3eed72010-08-24 11:42:41 +10001888 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 xfs_trans_binval(tp, bp);
1890 }
1891
Dave Chinner5017e972010-01-11 11:47:40 +00001892 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896/*
1897 * This is called to return an inode to the inode free list.
1898 * The inode should already be truncated to 0 length and have
1899 * no pages associated with it. This routine also assumes that
1900 * the inode is already a part of the transaction.
1901 *
1902 * The on-disk copy of the inode will have been added to the list
1903 * of unlinked inodes in the AGI. We need to remove the inode from
1904 * that list atomically with respect to freeing it here.
1905 */
1906int
1907xfs_ifree(
1908 xfs_trans_t *tp,
1909 xfs_inode_t *ip,
1910 xfs_bmap_free_t *flist)
1911{
1912 int error;
1913 int delete;
1914 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001915 xfs_dinode_t *dip;
1916 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001918 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ASSERT(ip->i_d.di_nlink == 0);
1920 ASSERT(ip->i_d.di_nextents == 0);
1921 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001922 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ASSERT(ip->i_d.di_nblocks == 0);
1924
1925 /*
1926 * Pull the on-disk inode from the AGI unlinked list.
1927 */
1928 error = xfs_iunlink_remove(tp, ip);
1929 if (error != 0) {
1930 return error;
1931 }
1932
1933 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1934 if (error != 0) {
1935 return error;
1936 }
1937 ip->i_d.di_mode = 0; /* mark incore inode as free */
1938 ip->i_d.di_flags = 0;
1939 ip->i_d.di_dmevmask = 0;
1940 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1942 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1943 /*
1944 * Bump the generation count so no one will be confused
1945 * by reincarnations of this inode.
1946 */
1947 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1950
Christoph Hellwig475ee412012-07-03 12:21:22 -04001951 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
1952 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001953 if (error)
1954 return error;
1955
1956 /*
1957 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1958 * from picking up this inode when it is reclaimed (its incore state
1959 * initialzed but not flushed to disk yet). The in-core di_mode is
1960 * already cleared and a corresponding transaction logged.
1961 * The hack here just synchronizes the in-core to on-disk
1962 * di_mode value in advance before the actual inode sync to disk.
1963 * This is OK because the inode is already unlinked and would never
1964 * change its di_mode again for this inode generation.
1965 * This is a temporary hack that would require a proper fix
1966 * in the future.
1967 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001968 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001971 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001974 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977/*
1978 * Reallocate the space for if_broot based on the number of records
1979 * being added or deleted as indicated in rec_diff. Move the records
1980 * and pointers in if_broot to fit the new size. When shrinking this
1981 * will eliminate holes between the records and pointers created by
1982 * the caller. When growing this will create holes to be filled in
1983 * by the caller.
1984 *
1985 * The caller must not request to add more records than would fit in
1986 * the on-disk inode root. If the if_broot is currently NULL, then
1987 * if we adding records one will be allocated. The caller must also
1988 * not request that the number of records go below zero, although
1989 * it can go to zero.
1990 *
1991 * ip -- the inode whose if_broot area is changing
1992 * ext_diff -- the change in the number of records, positive or negative,
1993 * requested for the if_broot array.
1994 */
1995void
1996xfs_iroot_realloc(
1997 xfs_inode_t *ip,
1998 int rec_diff,
1999 int whichfork)
2000{
Christoph Hellwig60197e82008-10-30 17:11:19 +11002001 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int cur_max;
2003 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002004 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int new_max;
2006 size_t new_size;
2007 char *np;
2008 char *op;
2009
2010 /*
2011 * Handle the degenerate case quietly.
2012 */
2013 if (rec_diff == 0) {
2014 return;
2015 }
2016
2017 ifp = XFS_IFORK_PTR(ip, whichfork);
2018 if (rec_diff > 0) {
2019 /*
2020 * If there wasn't any memory allocated before, just
2021 * allocate it now and get out.
2022 */
2023 if (ifp->if_broot_bytes == 0) {
2024 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002025 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 ifp->if_broot_bytes = (int)new_size;
2027 return;
2028 }
2029
2030 /*
2031 * If there is already an existing if_broot, then we need
2032 * to realloc() it and shift the pointers to their new
2033 * location. The records don't change location because
2034 * they are kept butted up against the btree block header.
2035 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002036 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 new_max = cur_max + rec_diff;
2038 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002039 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002041 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002042 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2043 ifp->if_broot_bytes);
2044 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2045 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 ifp->if_broot_bytes = (int)new_size;
2047 ASSERT(ifp->if_broot_bytes <=
2048 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2049 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2050 return;
2051 }
2052
2053 /*
2054 * rec_diff is less than 0. In this case, we are shrinking the
2055 * if_broot buffer. It must already exist. If we go to zero
2056 * records, just get rid of the root and clear the status bit.
2057 */
2058 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002059 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 new_max = cur_max + rec_diff;
2061 ASSERT(new_max >= 0);
2062 if (new_max > 0)
2063 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2064 else
2065 new_size = 0;
2066 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002067 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * First copy over the btree block header.
2070 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002071 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 } else {
2073 new_broot = NULL;
2074 ifp->if_flags &= ~XFS_IFBROOT;
2075 }
2076
2077 /*
2078 * Only copy the records and pointers if there are any.
2079 */
2080 if (new_max > 0) {
2081 /*
2082 * First copy the records.
2083 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11002084 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2085 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2087
2088 /*
2089 * Then copy the pointers.
2090 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002091 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002093 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 (int)new_size);
2095 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2096 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002097 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ifp->if_broot = new_broot;
2099 ifp->if_broot_bytes = (int)new_size;
2100 ASSERT(ifp->if_broot_bytes <=
2101 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2102 return;
2103}
2104
2105
2106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 * This is called when the amount of space needed for if_data
2108 * is increased or decreased. The change in size is indicated by
2109 * the number of bytes that need to be added or deleted in the
2110 * byte_diff parameter.
2111 *
2112 * If the amount of space needed has decreased below the size of the
2113 * inline buffer, then switch to using the inline buffer. Otherwise,
2114 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2115 * to what is needed.
2116 *
2117 * ip -- the inode whose if_data area is changing
2118 * byte_diff -- the change in the number of bytes, positive or negative,
2119 * requested for the if_data array.
2120 */
2121void
2122xfs_idata_realloc(
2123 xfs_inode_t *ip,
2124 int byte_diff,
2125 int whichfork)
2126{
2127 xfs_ifork_t *ifp;
2128 int new_size;
2129 int real_size;
2130
2131 if (byte_diff == 0) {
2132 return;
2133 }
2134
2135 ifp = XFS_IFORK_PTR(ip, whichfork);
2136 new_size = (int)ifp->if_bytes + byte_diff;
2137 ASSERT(new_size >= 0);
2138
2139 if (new_size == 0) {
2140 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002141 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 ifp->if_u1.if_data = NULL;
2144 real_size = 0;
2145 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2146 /*
2147 * If the valid extents/data can fit in if_inline_ext/data,
2148 * copy them from the malloc'd vector and free it.
2149 */
2150 if (ifp->if_u1.if_data == NULL) {
2151 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2152 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2153 ASSERT(ifp->if_real_bytes != 0);
2154 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2155 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002156 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2158 }
2159 real_size = 0;
2160 } else {
2161 /*
2162 * Stuck with malloc/realloc.
2163 * For inline data, the underlying buffer must be
2164 * a multiple of 4 bytes in size so that it can be
2165 * logged and stay on word boundaries. We enforce
2166 * that here.
2167 */
2168 real_size = roundup(new_size, 4);
2169 if (ifp->if_u1.if_data == NULL) {
2170 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002171 ifp->if_u1.if_data = kmem_alloc(real_size,
2172 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2174 /*
2175 * Only do the realloc if the underlying size
2176 * is really changing.
2177 */
2178 if (ifp->if_real_bytes != real_size) {
2179 ifp->if_u1.if_data =
2180 kmem_realloc(ifp->if_u1.if_data,
2181 real_size,
2182 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002183 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 } else {
2186 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002187 ifp->if_u1.if_data = kmem_alloc(real_size,
2188 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2190 ifp->if_bytes);
2191 }
2192 }
2193 ifp->if_real_bytes = real_size;
2194 ifp->if_bytes = new_size;
2195 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198void
2199xfs_idestroy_fork(
2200 xfs_inode_t *ip,
2201 int whichfork)
2202{
2203 xfs_ifork_t *ifp;
2204
2205 ifp = XFS_IFORK_PTR(ip, whichfork);
2206 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002207 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 ifp->if_broot = NULL;
2209 }
2210
2211 /*
2212 * If the format is local, then we can't have an extents
2213 * array so just look for an inline data array. If we're
2214 * not local then we may or may not have an extents list,
2215 * so check and free it up if we do.
2216 */
2217 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2218 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2219 (ifp->if_u1.if_data != NULL)) {
2220 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002221 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ifp->if_u1.if_data = NULL;
2223 ifp->if_real_bytes = 0;
2224 }
2225 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002226 ((ifp->if_flags & XFS_IFEXTIREC) ||
2227 ((ifp->if_u1.if_extents != NULL) &&
2228 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002230 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 ASSERT(ifp->if_u1.if_extents == NULL ||
2233 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2234 ASSERT(ifp->if_real_bytes == 0);
2235 if (whichfork == XFS_ATTR_FORK) {
2236 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2237 ip->i_afp = NULL;
2238 }
2239}
2240
2241/*
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002242 * This is called to unpin an inode. The caller must have the inode locked
2243 * in at least shared mode so that the buffer cannot be subsequently pinned
2244 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002246static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002247xfs_iunpin(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002248 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002249{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002250 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002251
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002252 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2253
David Chinnera3f74ff2008-03-06 13:43:42 +11002254 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002255 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002256
David Chinnera3f74ff2008-03-06 13:43:42 +11002257}
2258
Christoph Hellwigf392e632011-12-18 20:00:10 +00002259static void
2260__xfs_iunpin_wait(
2261 struct xfs_inode *ip)
2262{
2263 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2264 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2265
2266 xfs_iunpin(ip);
2267
2268 do {
2269 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2270 if (xfs_ipincount(ip))
2271 io_schedule();
2272 } while (xfs_ipincount(ip));
2273 finish_wait(wq, &wait.wait);
2274}
2275
Dave Chinner777df5a2010-02-06 12:37:26 +11002276void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277xfs_iunpin_wait(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002278 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002280 if (xfs_ipincount(ip))
2281 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284/*
2285 * xfs_iextents_copy()
2286 *
2287 * This is called to copy the REAL extents (as opposed to the delayed
2288 * allocation extents) from the inode into the given buffer. It
2289 * returns the number of bytes copied into the buffer.
2290 *
2291 * If there are no delayed allocation extents, then we can just
2292 * memcpy() the extents into the buffer. Otherwise, we need to
2293 * examine each extent in turn and skip those which are delayed.
2294 */
2295int
2296xfs_iextents_copy(
2297 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002298 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 int whichfork)
2300{
2301 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int i;
2303 xfs_ifork_t *ifp;
2304 int nrecs;
2305 xfs_fsblock_t start_block;
2306
2307 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002308 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 ASSERT(ifp->if_bytes > 0);
2310
2311 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002312 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 ASSERT(nrecs > 0);
2314
2315 /*
2316 * There are some delayed allocation extents in the
2317 * inode, so copy the extents one at a time and skip
2318 * the delayed ones. There must be at least one
2319 * non-delayed extent.
2320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 copied = 0;
2322 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002323 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002325 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /*
2327 * It's a delayed allocation extent, so skip it.
2328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 continue;
2330 }
2331
2332 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002333 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2334 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002335 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 copied++;
2337 }
2338 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002339 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2342}
2343
2344/*
2345 * Each of the following cases stores data into the same region
2346 * of the on-disk inode, so only one of them can be valid at
2347 * any given time. While it is possible to have conflicting formats
2348 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2349 * in EXTENTS format, this can only happen when the fork has
2350 * changed formats after being modified but before being flushed.
2351 * In these cases, the format always takes precedence, because the
2352 * format indicates the current state of the fork.
2353 */
2354/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002355STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356xfs_iflush_fork(
2357 xfs_inode_t *ip,
2358 xfs_dinode_t *dip,
2359 xfs_inode_log_item_t *iip,
2360 int whichfork,
2361 xfs_buf_t *bp)
2362{
2363 char *cp;
2364 xfs_ifork_t *ifp;
2365 xfs_mount_t *mp;
2366#ifdef XFS_TRANS_DEBUG
2367 int first;
2368#endif
2369 static const short brootflag[2] =
2370 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2371 static const short dataflag[2] =
2372 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2373 static const short extflag[2] =
2374 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2375
David Chinnere4ac9672008-04-10 12:23:58 +10002376 if (!iip)
2377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 ifp = XFS_IFORK_PTR(ip, whichfork);
2379 /*
2380 * This can happen if we gave up in iformat in an error path,
2381 * for the attribute fork.
2382 */
David Chinnere4ac9672008-04-10 12:23:58 +10002383 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 cp = XFS_DFORK_PTR(dip, whichfork);
2388 mp = ip->i_mount;
2389 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2390 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002391 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 (ifp->if_bytes > 0)) {
2393 ASSERT(ifp->if_u1.if_data != NULL);
2394 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2395 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 break;
2398
2399 case XFS_DINODE_FMT_EXTENTS:
2400 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002401 !(iip->ili_fields & extflag[whichfork]));
2402 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002404 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2406 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2407 whichfork);
2408 }
2409 break;
2410
2411 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002412 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 (ifp->if_broot_bytes > 0)) {
2414 ASSERT(ifp->if_broot != NULL);
2415 ASSERT(ifp->if_broot_bytes <=
2416 (XFS_IFORK_SIZE(ip, whichfork) +
2417 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002418 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 (xfs_bmdr_block_t *)cp,
2420 XFS_DFORK_SIZE(dip, mp, whichfork));
2421 }
2422 break;
2423
2424 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002425 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002427 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429 break;
2430
2431 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002432 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002434 memcpy(XFS_DFORK_DPTR(dip),
2435 &ip->i_df.if_u2.if_uuid,
2436 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438 break;
2439
2440 default:
2441 ASSERT(0);
2442 break;
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
David Chinnerbad55842008-03-06 13:43:49 +11002446STATIC int
2447xfs_iflush_cluster(
2448 xfs_inode_t *ip,
2449 xfs_buf_t *bp)
2450{
2451 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002452 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002453 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002454 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002455 int ilist_size;
2456 xfs_inode_t **ilist;
2457 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002458 int nr_found;
2459 int clcount = 0;
2460 int bufwasdelwri;
2461 int i;
2462
Dave Chinner5017e972010-01-11 11:47:40 +00002463 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002464
David Chinnerc8f5f122008-05-20 11:30:15 +10002465 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2466 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002467 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002468 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002469 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002470
2471 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2472 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002473 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002474 /* really need a gang lookup range call here */
2475 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002476 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002477 if (nr_found == 0)
2478 goto out_free;
2479
2480 for (i = 0; i < nr_found; i++) {
2481 iq = ilist[i];
2482 if (iq == ip)
2483 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002484
2485 /*
2486 * because this is an RCU protected lookup, we could find a
2487 * recently freed or even reallocated inode during the lookup.
2488 * We need to check under the i_flags_lock for a valid inode
2489 * here. Skip it if it is not valid or the wrong inode.
2490 */
2491 spin_lock(&ip->i_flags_lock);
2492 if (!ip->i_ino ||
2493 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2494 spin_unlock(&ip->i_flags_lock);
2495 continue;
2496 }
2497 spin_unlock(&ip->i_flags_lock);
2498
David Chinnerbad55842008-03-06 13:43:49 +11002499 /*
2500 * Do an un-protected check to see if the inode is dirty and
2501 * is a candidate for flushing. These checks will be repeated
2502 * later after the appropriate locks are acquired.
2503 */
David Chinner33540402008-03-06 13:43:59 +11002504 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002505 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002506
2507 /*
2508 * Try to get locks. If any are unavailable or it is pinned,
2509 * then this inode cannot be flushed and is skipped.
2510 */
2511
2512 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2513 continue;
2514 if (!xfs_iflock_nowait(iq)) {
2515 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2516 continue;
2517 }
2518 if (xfs_ipincount(iq)) {
2519 xfs_ifunlock(iq);
2520 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2521 continue;
2522 }
2523
2524 /*
2525 * arriving here means that this inode can be flushed. First
2526 * re-check that it's dirty before flushing.
2527 */
David Chinner33540402008-03-06 13:43:59 +11002528 if (!xfs_inode_clean(iq)) {
2529 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002530 error = xfs_iflush_int(iq, bp);
2531 if (error) {
2532 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2533 goto cluster_corrupt_out;
2534 }
2535 clcount++;
2536 } else {
2537 xfs_ifunlock(iq);
2538 }
2539 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2540 }
2541
2542 if (clcount) {
2543 XFS_STATS_INC(xs_icluster_flushcnt);
2544 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2545 }
2546
2547out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002548 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002549 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002550out_put:
2551 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002552 return 0;
2553
2554
2555cluster_corrupt_out:
2556 /*
2557 * Corruption detected in the clustering loop. Invalidate the
2558 * inode buffer and shut down the filesystem.
2559 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002560 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002561 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002562 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002563 * brelse can handle it with no problems. If not, shut down the
2564 * filesystem before releasing the buffer.
2565 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002566 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002567 if (bufwasdelwri)
2568 xfs_buf_relse(bp);
2569
2570 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2571
2572 if (!bufwasdelwri) {
2573 /*
2574 * Just like incore_relse: if we have b_iodone functions,
2575 * mark the buffer as an error and call them. Otherwise
2576 * mark it as stale and brelse.
2577 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002578 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002579 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002580 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002581 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002582 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002583 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002584 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002585 xfs_buf_relse(bp);
2586 }
2587 }
2588
2589 /*
2590 * Unlocks the flush lock
2591 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002592 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002593 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002594 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002595 return XFS_ERROR(EFSCORRUPTED);
2596}
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002599 * Flush dirty inode metadata into the backing buffer.
2600 *
2601 * The caller must have the inode lock and the inode flush lock held. The
2602 * inode lock will still be held upon return to the caller, and the inode
2603 * flush lock will be released after the inode has reached the disk.
2604 *
2605 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
2607int
2608xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002609 struct xfs_inode *ip,
2610 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002612 struct xfs_mount *mp = ip->i_mount;
2613 struct xfs_buf *bp;
2614 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 XFS_STATS_INC(xs_iflush_count);
2618
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002619 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002620 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002622 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Christoph Hellwig4c468192012-04-23 15:58:36 +10002624 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 xfs_iunpin_wait(ip);
2627
2628 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002629 * For stale inodes we cannot rely on the backing buffer remaining
2630 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002631 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002632 * inodes below. We have to check this after ensuring the inode is
2633 * unpinned so that it is safe to reclaim the stale inode after the
2634 * flush call.
2635 */
2636 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2637 xfs_ifunlock(ip);
2638 return 0;
2639 }
2640
2641 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 * This may have been unpinned because the filesystem is shutting
2643 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002644 * to disk, because the log record didn't make it to disk.
2645 *
2646 * We also have to remove the log item from the AIL in this case,
2647 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 */
2649 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002650 error = XFS_ERROR(EIO);
2651 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653
2654 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002655 * Get the buffer containing the on-disk inode.
2656 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002657 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2658 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002659 if (error || !bp) {
2660 xfs_ifunlock(ip);
2661 return error;
2662 }
2663
2664 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * First flush out the inode that xfs_iflush was called with.
2666 */
2667 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002672 * If the buffer is pinned then push on the log now so we won't
2673 * get stuck waiting in the write for too long.
2674 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002675 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002676 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002677
2678 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * inode clustering:
2680 * see if other inodes can be gathered into this write
2681 */
David Chinnerbad55842008-03-06 13:43:49 +11002682 error = xfs_iflush_cluster(ip, bp);
2683 if (error)
2684 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Christoph Hellwig4c468192012-04-23 15:58:36 +10002686 *bpp = bp;
2687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689corrupt_out:
2690 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002691 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002693 error = XFS_ERROR(EFSCORRUPTED);
2694abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /*
2696 * Unlocks the flush lock
2697 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002698 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
2702
2703STATIC int
2704xfs_iflush_int(
2705 xfs_inode_t *ip,
2706 xfs_buf_t *bp)
2707{
2708 xfs_inode_log_item_t *iip;
2709 xfs_dinode_t *dip;
2710 xfs_mount_t *mp;
2711#ifdef XFS_TRANS_DEBUG
2712 int first;
2713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002715 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002716 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002718 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 iip = ip->i_itemp;
2721 mp = ip->i_mount;
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002724 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002726 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002728 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2729 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2730 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 goto corrupt_out;
2732 }
2733 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2734 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002735 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2736 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2737 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 goto corrupt_out;
2739 }
Al Viroabbede12011-07-26 02:31:30 -04002740 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (XFS_TEST_ERROR(
2742 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2743 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2744 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002745 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2746 "%s: Bad regular inode %Lu, ptr 0x%p",
2747 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 goto corrupt_out;
2749 }
Al Viroabbede12011-07-26 02:31:30 -04002750 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (XFS_TEST_ERROR(
2752 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2753 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2754 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2755 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002756 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2757 "%s: Bad directory inode %Lu, ptr 0x%p",
2758 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 goto corrupt_out;
2760 }
2761 }
2762 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2763 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2764 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002765 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2766 "%s: detected corrupt incore inode %Lu, "
2767 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2768 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002770 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 goto corrupt_out;
2772 }
2773 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2774 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002775 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2776 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2777 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 goto corrupt_out;
2779 }
2780 /*
2781 * bump the flush iteration count, used to detect flushes which
2782 * postdate a log record during recovery.
2783 */
2784
2785 ip->i_d.di_flushiter++;
2786
2787 /*
2788 * Copy the dirty parts of the inode into the on-disk
2789 * inode. We always copy out the core of the inode,
2790 * because if the inode is dirty at all the core must
2791 * be.
2792 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002793 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2796 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2797 ip->i_d.di_flushiter = 0;
2798
2799 /*
2800 * If this is really an old format inode and the superblock version
2801 * has not been updated to support only new format inodes, then
2802 * convert back to the old inode format. If the superblock version
2803 * has been updated, then make the conversion permanent.
2804 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002805 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2806 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002807 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /*
2809 * Convert it back.
2810 */
2811 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002812 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 } else {
2814 /*
2815 * The superblock version has already been bumped,
2816 * so just make the conversion to the new inode
2817 * format permanent.
2818 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002819 ip->i_d.di_version = 2;
2820 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002822 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002824 memset(&(dip->di_pad[0]), 0,
2825 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002826 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 }
2829
David Chinnere4ac9672008-04-10 12:23:58 +10002830 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2831 if (XFS_IFORK_Q(ip))
2832 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 xfs_inobp_check(mp, bp);
2834
2835 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002836 * We've recorded everything logged in the inode, so we'd like to clear
2837 * the ili_fields bits so we don't log and flush things unnecessarily.
2838 * However, we can't stop logging all this information until the data
2839 * we've copied into the disk buffer is written to disk. If we did we
2840 * might overwrite the copy of the inode in the log with all the data
2841 * after re-logging only part of it, and in the face of a crash we
2842 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002844 * What we do is move the bits to the ili_last_fields field. When
2845 * logging the inode, these bits are moved back to the ili_fields field.
2846 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2847 * know that the information those bits represent is permanently on
2848 * disk. As long as the flush completes before the inode is logged
2849 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002851 * We can play with the ili_fields bits here, because the inode lock
2852 * must be held exclusively in order to set bits there and the flush
2853 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2854 * done routine can tell whether or not to look in the AIL. Also, store
2855 * the current LSN of the inode so that we can tell whether the item has
2856 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2857 * need the AIL lock, because it is a 64 bit value that cannot be read
2858 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002860 if (iip != NULL && iip->ili_fields != 0) {
2861 iip->ili_last_fields = iip->ili_fields;
2862 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 iip->ili_logged = 1;
2864
David Chinner7b2e2a32008-10-30 17:39:12 +11002865 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2866 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 /*
2869 * Attach the function xfs_iflush_done to the inode's
2870 * buffer. This will remove the inode from the AIL
2871 * and unlock the inode's flush lock when the inode is
2872 * completely written to disk.
2873 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002874 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002876 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002877 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 } else {
2879 /*
2880 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002881 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 * the inode flush lock because we can avoid the whole
2883 * AIL state thing. It's OK to drop the flush lock now,
2884 * because we've already locked the buffer and to do anything
2885 * you really need both.
2886 */
2887 if (iip != NULL) {
2888 ASSERT(iip->ili_logged == 0);
2889 ASSERT(iip->ili_last_fields == 0);
2890 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2891 }
2892 xfs_ifunlock(ip);
2893 }
2894
2895 return 0;
2896
2897corrupt_out:
2898 return XFS_ERROR(EFSCORRUPTED);
2899}
2900
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002901/*
2902 * Return a pointer to the extent record at file index idx.
2903 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002904xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002905xfs_iext_get_ext(
2906 xfs_ifork_t *ifp, /* inode fork pointer */
2907 xfs_extnum_t idx) /* index of target extent */
2908{
2909 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002910 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2911
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002912 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2913 return ifp->if_u1.if_ext_irec->er_extbuf;
2914 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2915 xfs_ext_irec_t *erp; /* irec pointer */
2916 int erp_idx = 0; /* irec index */
2917 xfs_extnum_t page_idx = idx; /* ext index in target list */
2918
2919 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2920 return &erp->er_extbuf[page_idx];
2921 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002922 return &ifp->if_u1.if_extents[idx];
2923 } else {
2924 return NULL;
2925 }
2926}
2927
2928/*
2929 * Insert new item(s) into the extent records for incore inode
2930 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2931 */
2932void
2933xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002934 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002935 xfs_extnum_t idx, /* starting index of new items */
2936 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002937 xfs_bmbt_irec_t *new, /* items to insert */
2938 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002939{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002940 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002941 xfs_extnum_t i; /* extent record index */
2942
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002943 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2944
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002945 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2946 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002947 for (i = idx; i < idx + count; i++, new++)
2948 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002949}
2950
2951/*
2952 * This is called when the amount of space required for incore file
2953 * extents needs to be increased. The ext_diff parameter stores the
2954 * number of new extents being added and the idx parameter contains
2955 * the extent index where the new extents will be added. If the new
2956 * extents are being appended, then we just need to (re)allocate and
2957 * initialize the space. Otherwise, if the new extents are being
2958 * inserted into the middle of the existing entries, a bit more work
2959 * is required to make room for the new extents to be inserted. The
2960 * caller is responsible for filling in the new extent entries upon
2961 * return.
2962 */
2963void
2964xfs_iext_add(
2965 xfs_ifork_t *ifp, /* inode fork pointer */
2966 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002967 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002968{
2969 int byte_diff; /* new bytes being added */
2970 int new_size; /* size of extents after adding */
2971 xfs_extnum_t nextents; /* number of extents in file */
2972
2973 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2974 ASSERT((idx >= 0) && (idx <= nextents));
2975 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2976 new_size = ifp->if_bytes + byte_diff;
2977 /*
2978 * If the new number of extents (nextents + ext_diff)
2979 * fits inside the inode, then continue to use the inline
2980 * extent buffer.
2981 */
2982 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2983 if (idx < nextents) {
2984 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2985 &ifp->if_u2.if_inline_ext[idx],
2986 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2987 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2988 }
2989 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2990 ifp->if_real_bytes = 0;
2991 }
2992 /*
2993 * Otherwise use a linear (direct) extent list.
2994 * If the extents are currently inside the inode,
2995 * xfs_iext_realloc_direct will switch us from
2996 * inline to direct extent allocation mode.
2997 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002998 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002999 xfs_iext_realloc_direct(ifp, new_size);
3000 if (idx < nextents) {
3001 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3002 &ifp->if_u1.if_extents[idx],
3003 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3004 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3005 }
3006 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003007 /* Indirection array */
3008 else {
3009 xfs_ext_irec_t *erp;
3010 int erp_idx = 0;
3011 int page_idx = idx;
3012
3013 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3014 if (ifp->if_flags & XFS_IFEXTIREC) {
3015 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3016 } else {
3017 xfs_iext_irec_init(ifp);
3018 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3019 erp = ifp->if_u1.if_ext_irec;
3020 }
3021 /* Extents fit in target extent page */
3022 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3023 if (page_idx < erp->er_extcount) {
3024 memmove(&erp->er_extbuf[page_idx + ext_diff],
3025 &erp->er_extbuf[page_idx],
3026 (erp->er_extcount - page_idx) *
3027 sizeof(xfs_bmbt_rec_t));
3028 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3029 }
3030 erp->er_extcount += ext_diff;
3031 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3032 }
3033 /* Insert a new extent page */
3034 else if (erp) {
3035 xfs_iext_add_indirect_multi(ifp,
3036 erp_idx, page_idx, ext_diff);
3037 }
3038 /*
3039 * If extent(s) are being appended to the last page in
3040 * the indirection array and the new extent(s) don't fit
3041 * in the page, then erp is NULL and erp_idx is set to
3042 * the next index needed in the indirection array.
3043 */
3044 else {
3045 int count = ext_diff;
3046
3047 while (count) {
3048 erp = xfs_iext_irec_new(ifp, erp_idx);
3049 erp->er_extcount = count;
3050 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3051 if (count) {
3052 erp_idx++;
3053 }
3054 }
3055 }
3056 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003057 ifp->if_bytes = new_size;
3058}
3059
3060/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003061 * This is called when incore extents are being added to the indirection
3062 * array and the new extents do not fit in the target extent list. The
3063 * erp_idx parameter contains the irec index for the target extent list
3064 * in the indirection array, and the idx parameter contains the extent
3065 * index within the list. The number of extents being added is stored
3066 * in the count parameter.
3067 *
3068 * |-------| |-------|
3069 * | | | | idx - number of extents before idx
3070 * | idx | | count |
3071 * | | | | count - number of extents being inserted at idx
3072 * |-------| |-------|
3073 * | count | | nex2 | nex2 - number of extents after idx + count
3074 * |-------| |-------|
3075 */
3076void
3077xfs_iext_add_indirect_multi(
3078 xfs_ifork_t *ifp, /* inode fork pointer */
3079 int erp_idx, /* target extent irec index */
3080 xfs_extnum_t idx, /* index within target list */
3081 int count) /* new extents being added */
3082{
3083 int byte_diff; /* new bytes being added */
3084 xfs_ext_irec_t *erp; /* pointer to irec entry */
3085 xfs_extnum_t ext_diff; /* number of extents to add */
3086 xfs_extnum_t ext_cnt; /* new extents still needed */
3087 xfs_extnum_t nex2; /* extents after idx + count */
3088 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3089 int nlists; /* number of irec's (lists) */
3090
3091 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3092 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3093 nex2 = erp->er_extcount - idx;
3094 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3095
3096 /*
3097 * Save second part of target extent list
3098 * (all extents past */
3099 if (nex2) {
3100 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10003101 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003102 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3103 erp->er_extcount -= nex2;
3104 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3105 memset(&erp->er_extbuf[idx], 0, byte_diff);
3106 }
3107
3108 /*
3109 * Add the new extents to the end of the target
3110 * list, then allocate new irec record(s) and
3111 * extent buffer(s) as needed to store the rest
3112 * of the new extents.
3113 */
3114 ext_cnt = count;
3115 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3116 if (ext_diff) {
3117 erp->er_extcount += ext_diff;
3118 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3119 ext_cnt -= ext_diff;
3120 }
3121 while (ext_cnt) {
3122 erp_idx++;
3123 erp = xfs_iext_irec_new(ifp, erp_idx);
3124 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3125 erp->er_extcount = ext_diff;
3126 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3127 ext_cnt -= ext_diff;
3128 }
3129
3130 /* Add nex2 extents back to indirection array */
3131 if (nex2) {
3132 xfs_extnum_t ext_avail;
3133 int i;
3134
3135 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3136 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3137 i = 0;
3138 /*
3139 * If nex2 extents fit in the current page, append
3140 * nex2_ep after the new extents.
3141 */
3142 if (nex2 <= ext_avail) {
3143 i = erp->er_extcount;
3144 }
3145 /*
3146 * Otherwise, check if space is available in the
3147 * next page.
3148 */
3149 else if ((erp_idx < nlists - 1) &&
3150 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3151 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3152 erp_idx++;
3153 erp++;
3154 /* Create a hole for nex2 extents */
3155 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3156 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3157 }
3158 /*
3159 * Final choice, create a new extent page for
3160 * nex2 extents.
3161 */
3162 else {
3163 erp_idx++;
3164 erp = xfs_iext_irec_new(ifp, erp_idx);
3165 }
3166 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003167 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003168 erp->er_extcount += nex2;
3169 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3170 }
3171}
3172
3173/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003174 * This is called when the amount of space required for incore file
3175 * extents needs to be decreased. The ext_diff parameter stores the
3176 * number of extents to be removed and the idx parameter contains
3177 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003178 *
3179 * If the amount of space needed has decreased below the linear
3180 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3181 * extent array. Otherwise, use kmem_realloc() to adjust the
3182 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003183 */
3184void
3185xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003186 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003187 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003188 int ext_diff, /* number of extents to remove */
3189 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003190{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003191 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003192 xfs_extnum_t nextents; /* number of extents in file */
3193 int new_size; /* size of extents after removal */
3194
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003195 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3196
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003197 ASSERT(ext_diff > 0);
3198 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3199 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3200
3201 if (new_size == 0) {
3202 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003203 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3204 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003205 } else if (ifp->if_real_bytes) {
3206 xfs_iext_remove_direct(ifp, idx, ext_diff);
3207 } else {
3208 xfs_iext_remove_inline(ifp, idx, ext_diff);
3209 }
3210 ifp->if_bytes = new_size;
3211}
3212
3213/*
3214 * This removes ext_diff extents from the inline buffer, beginning
3215 * at extent index idx.
3216 */
3217void
3218xfs_iext_remove_inline(
3219 xfs_ifork_t *ifp, /* inode fork pointer */
3220 xfs_extnum_t idx, /* index to begin removing exts */
3221 int ext_diff) /* number of extents to remove */
3222{
3223 int nextents; /* number of extents in file */
3224
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003225 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003226 ASSERT(idx < XFS_INLINE_EXTS);
3227 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3228 ASSERT(((nextents - ext_diff) > 0) &&
3229 (nextents - ext_diff) < XFS_INLINE_EXTS);
3230
3231 if (idx + ext_diff < nextents) {
3232 memmove(&ifp->if_u2.if_inline_ext[idx],
3233 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3234 (nextents - (idx + ext_diff)) *
3235 sizeof(xfs_bmbt_rec_t));
3236 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3237 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3238 } else {
3239 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3240 ext_diff * sizeof(xfs_bmbt_rec_t));
3241 }
3242}
3243
3244/*
3245 * This removes ext_diff extents from a linear (direct) extent list,
3246 * beginning at extent index idx. If the extents are being removed
3247 * from the end of the list (ie. truncate) then we just need to re-
3248 * allocate the list to remove the extra space. Otherwise, if the
3249 * extents are being removed from the middle of the existing extent
3250 * entries, then we first need to move the extent records beginning
3251 * at idx + ext_diff up in the list to overwrite the records being
3252 * removed, then remove the extra space via kmem_realloc.
3253 */
3254void
3255xfs_iext_remove_direct(
3256 xfs_ifork_t *ifp, /* inode fork pointer */
3257 xfs_extnum_t idx, /* index to begin removing exts */
3258 int ext_diff) /* number of extents to remove */
3259{
3260 xfs_extnum_t nextents; /* number of extents in file */
3261 int new_size; /* size of extents after removal */
3262
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003263 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003264 new_size = ifp->if_bytes -
3265 (ext_diff * sizeof(xfs_bmbt_rec_t));
3266 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3267
3268 if (new_size == 0) {
3269 xfs_iext_destroy(ifp);
3270 return;
3271 }
3272 /* Move extents up in the list (if needed) */
3273 if (idx + ext_diff < nextents) {
3274 memmove(&ifp->if_u1.if_extents[idx],
3275 &ifp->if_u1.if_extents[idx + ext_diff],
3276 (nextents - (idx + ext_diff)) *
3277 sizeof(xfs_bmbt_rec_t));
3278 }
3279 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3280 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3281 /*
3282 * Reallocate the direct extent list. If the extents
3283 * will fit inside the inode then xfs_iext_realloc_direct
3284 * will switch from direct to inline extent allocation
3285 * mode for us.
3286 */
3287 xfs_iext_realloc_direct(ifp, new_size);
3288 ifp->if_bytes = new_size;
3289}
3290
3291/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003292 * This is called when incore extents are being removed from the
3293 * indirection array and the extents being removed span multiple extent
3294 * buffers. The idx parameter contains the file extent index where we
3295 * want to begin removing extents, and the count parameter contains
3296 * how many extents need to be removed.
3297 *
3298 * |-------| |-------|
3299 * | nex1 | | | nex1 - number of extents before idx
3300 * |-------| | count |
3301 * | | | | count - number of extents being removed at idx
3302 * | count | |-------|
3303 * | | | nex2 | nex2 - number of extents after idx + count
3304 * |-------| |-------|
3305 */
3306void
3307xfs_iext_remove_indirect(
3308 xfs_ifork_t *ifp, /* inode fork pointer */
3309 xfs_extnum_t idx, /* index to begin removing extents */
3310 int count) /* number of extents to remove */
3311{
3312 xfs_ext_irec_t *erp; /* indirection array pointer */
3313 int erp_idx = 0; /* indirection array index */
3314 xfs_extnum_t ext_cnt; /* extents left to remove */
3315 xfs_extnum_t ext_diff; /* extents to remove in current list */
3316 xfs_extnum_t nex1; /* number of extents before idx */
3317 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003318 int page_idx = idx; /* index in target extent list */
3319
3320 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3321 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3322 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003323 nex1 = page_idx;
3324 ext_cnt = count;
3325 while (ext_cnt) {
3326 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3327 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3328 /*
3329 * Check for deletion of entire list;
3330 * xfs_iext_irec_remove() updates extent offsets.
3331 */
3332 if (ext_diff == erp->er_extcount) {
3333 xfs_iext_irec_remove(ifp, erp_idx);
3334 ext_cnt -= ext_diff;
3335 nex1 = 0;
3336 if (ext_cnt) {
3337 ASSERT(erp_idx < ifp->if_real_bytes /
3338 XFS_IEXT_BUFSZ);
3339 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3340 nex1 = 0;
3341 continue;
3342 } else {
3343 break;
3344 }
3345 }
3346 /* Move extents up (if needed) */
3347 if (nex2) {
3348 memmove(&erp->er_extbuf[nex1],
3349 &erp->er_extbuf[nex1 + ext_diff],
3350 nex2 * sizeof(xfs_bmbt_rec_t));
3351 }
3352 /* Zero out rest of page */
3353 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3354 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3355 /* Update remaining counters */
3356 erp->er_extcount -= ext_diff;
3357 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3358 ext_cnt -= ext_diff;
3359 nex1 = 0;
3360 erp_idx++;
3361 erp++;
3362 }
3363 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3364 xfs_iext_irec_compact(ifp);
3365}
3366
3367/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003368 * Create, destroy, or resize a linear (direct) block of extents.
3369 */
3370void
3371xfs_iext_realloc_direct(
3372 xfs_ifork_t *ifp, /* inode fork pointer */
3373 int new_size) /* new size of extents */
3374{
3375 int rnew_size; /* real new size of extents */
3376
3377 rnew_size = new_size;
3378
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003379 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3380 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3381 (new_size != ifp->if_real_bytes)));
3382
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003383 /* Free extent records */
3384 if (new_size == 0) {
3385 xfs_iext_destroy(ifp);
3386 }
3387 /* Resize direct extent list and zero any new bytes */
3388 else if (ifp->if_real_bytes) {
3389 /* Check if extents will fit inside the inode */
3390 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3391 xfs_iext_direct_to_inline(ifp, new_size /
3392 (uint)sizeof(xfs_bmbt_rec_t));
3393 ifp->if_bytes = new_size;
3394 return;
3395 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003396 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003397 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003398 }
3399 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003400 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003401 kmem_realloc(ifp->if_u1.if_extents,
3402 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003403 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003404 }
3405 if (rnew_size > ifp->if_real_bytes) {
3406 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3407 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3408 rnew_size - ifp->if_real_bytes);
3409 }
3410 }
3411 /*
3412 * Switch from the inline extent buffer to a direct
3413 * extent list. Be sure to include the inline extent
3414 * bytes in new_size.
3415 */
3416 else {
3417 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003418 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003419 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003420 }
3421 xfs_iext_inline_to_direct(ifp, rnew_size);
3422 }
3423 ifp->if_real_bytes = rnew_size;
3424 ifp->if_bytes = new_size;
3425}
3426
3427/*
3428 * Switch from linear (direct) extent records to inline buffer.
3429 */
3430void
3431xfs_iext_direct_to_inline(
3432 xfs_ifork_t *ifp, /* inode fork pointer */
3433 xfs_extnum_t nextents) /* number of extents in file */
3434{
3435 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3436 ASSERT(nextents <= XFS_INLINE_EXTS);
3437 /*
3438 * The inline buffer was zeroed when we switched
3439 * from inline to direct extent allocation mode,
3440 * so we don't need to clear it here.
3441 */
3442 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3443 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003444 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003445 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3446 ifp->if_real_bytes = 0;
3447}
3448
3449/*
3450 * Switch from inline buffer to linear (direct) extent records.
3451 * new_size should already be rounded up to the next power of 2
3452 * by the caller (when appropriate), so use new_size as it is.
3453 * However, since new_size may be rounded up, we can't update
3454 * if_bytes here. It is the caller's responsibility to update
3455 * if_bytes upon return.
3456 */
3457void
3458xfs_iext_inline_to_direct(
3459 xfs_ifork_t *ifp, /* inode fork pointer */
3460 int new_size) /* number of extents in file */
3461{
David Chinner67850732008-08-13 16:02:51 +10003462 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003463 memset(ifp->if_u1.if_extents, 0, new_size);
3464 if (ifp->if_bytes) {
3465 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3466 ifp->if_bytes);
3467 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3468 sizeof(xfs_bmbt_rec_t));
3469 }
3470 ifp->if_real_bytes = new_size;
3471}
3472
3473/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003474 * Resize an extent indirection array to new_size bytes.
3475 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003476STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003477xfs_iext_realloc_indirect(
3478 xfs_ifork_t *ifp, /* inode fork pointer */
3479 int new_size) /* new indirection array size */
3480{
3481 int nlists; /* number of irec's (ex lists) */
3482 int size; /* current indirection array size */
3483
3484 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3485 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3486 size = nlists * sizeof(xfs_ext_irec_t);
3487 ASSERT(ifp->if_real_bytes);
3488 ASSERT((new_size >= 0) && (new_size != size));
3489 if (new_size == 0) {
3490 xfs_iext_destroy(ifp);
3491 } else {
3492 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3493 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003494 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003495 }
3496}
3497
3498/*
3499 * Switch from indirection array to linear (direct) extent allocations.
3500 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003501STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003502xfs_iext_indirect_to_direct(
3503 xfs_ifork_t *ifp) /* inode fork pointer */
3504{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003505 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003506 xfs_extnum_t nextents; /* number of extents in file */
3507 int size; /* size of file extents */
3508
3509 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3510 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3511 ASSERT(nextents <= XFS_LINEAR_EXTS);
3512 size = nextents * sizeof(xfs_bmbt_rec_t);
3513
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003514 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003515 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3516
3517 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003518 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003519 ifp->if_flags &= ~XFS_IFEXTIREC;
3520 ifp->if_u1.if_extents = ep;
3521 ifp->if_bytes = size;
3522 if (nextents < XFS_LINEAR_EXTS) {
3523 xfs_iext_realloc_direct(ifp, size);
3524 }
3525}
3526
3527/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003528 * Free incore file extents.
3529 */
3530void
3531xfs_iext_destroy(
3532 xfs_ifork_t *ifp) /* inode fork pointer */
3533{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003534 if (ifp->if_flags & XFS_IFEXTIREC) {
3535 int erp_idx;
3536 int nlists;
3537
3538 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3539 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3540 xfs_iext_irec_remove(ifp, erp_idx);
3541 }
3542 ifp->if_flags &= ~XFS_IFEXTIREC;
3543 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003544 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003545 } else if (ifp->if_bytes) {
3546 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3547 sizeof(xfs_bmbt_rec_t));
3548 }
3549 ifp->if_u1.if_extents = NULL;
3550 ifp->if_real_bytes = 0;
3551 ifp->if_bytes = 0;
3552}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003553
3554/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003555 * Return a pointer to the extent record for file system block bno.
3556 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003557xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003558xfs_iext_bno_to_ext(
3559 xfs_ifork_t *ifp, /* inode fork pointer */
3560 xfs_fileoff_t bno, /* block number to search for */
3561 xfs_extnum_t *idxp) /* index of target extent */
3562{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003563 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003564 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003565 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003566 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003567 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003568 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003569 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003570 xfs_extnum_t nextents; /* number of file extents */
3571 xfs_fileoff_t startoff = 0; /* start offset of extent */
3572
3573 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3574 if (nextents == 0) {
3575 *idxp = 0;
3576 return NULL;
3577 }
3578 low = 0;
3579 if (ifp->if_flags & XFS_IFEXTIREC) {
3580 /* Find target extent list */
3581 int erp_idx = 0;
3582 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3583 base = erp->er_extbuf;
3584 high = erp->er_extcount - 1;
3585 } else {
3586 base = ifp->if_u1.if_extents;
3587 high = nextents - 1;
3588 }
3589 /* Binary search extent records */
3590 while (low <= high) {
3591 idx = (low + high) >> 1;
3592 ep = base + idx;
3593 startoff = xfs_bmbt_get_startoff(ep);
3594 blockcount = xfs_bmbt_get_blockcount(ep);
3595 if (bno < startoff) {
3596 high = idx - 1;
3597 } else if (bno >= startoff + blockcount) {
3598 low = idx + 1;
3599 } else {
3600 /* Convert back to file-based extent index */
3601 if (ifp->if_flags & XFS_IFEXTIREC) {
3602 idx += erp->er_extoff;
3603 }
3604 *idxp = idx;
3605 return ep;
3606 }
3607 }
3608 /* Convert back to file-based extent index */
3609 if (ifp->if_flags & XFS_IFEXTIREC) {
3610 idx += erp->er_extoff;
3611 }
3612 if (bno >= startoff + blockcount) {
3613 if (++idx == nextents) {
3614 ep = NULL;
3615 } else {
3616 ep = xfs_iext_get_ext(ifp, idx);
3617 }
3618 }
3619 *idxp = idx;
3620 return ep;
3621}
3622
3623/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003624 * Return a pointer to the indirection array entry containing the
3625 * extent record for filesystem block bno. Store the index of the
3626 * target irec in *erp_idxp.
3627 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003628xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003629xfs_iext_bno_to_irec(
3630 xfs_ifork_t *ifp, /* inode fork pointer */
3631 xfs_fileoff_t bno, /* block number to search for */
3632 int *erp_idxp) /* irec index of target ext list */
3633{
3634 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3635 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003636 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003637 int nlists; /* number of extent irec's (lists) */
3638 int high; /* binary search upper limit */
3639 int low; /* binary search lower limit */
3640
3641 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3642 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3643 erp_idx = 0;
3644 low = 0;
3645 high = nlists - 1;
3646 while (low <= high) {
3647 erp_idx = (low + high) >> 1;
3648 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3649 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3650 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3651 high = erp_idx - 1;
3652 } else if (erp_next && bno >=
3653 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3654 low = erp_idx + 1;
3655 } else {
3656 break;
3657 }
3658 }
3659 *erp_idxp = erp_idx;
3660 return erp;
3661}
3662
3663/*
3664 * Return a pointer to the indirection array entry containing the
3665 * extent record at file extent index *idxp. Store the index of the
3666 * target irec in *erp_idxp and store the page index of the target
3667 * extent record in *idxp.
3668 */
3669xfs_ext_irec_t *
3670xfs_iext_idx_to_irec(
3671 xfs_ifork_t *ifp, /* inode fork pointer */
3672 xfs_extnum_t *idxp, /* extent index (file -> page) */
3673 int *erp_idxp, /* pointer to target irec */
3674 int realloc) /* new bytes were just added */
3675{
3676 xfs_ext_irec_t *prev; /* pointer to previous irec */
3677 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3678 int erp_idx; /* indirection array index */
3679 int nlists; /* number of irec's (ex lists) */
3680 int high; /* binary search upper limit */
3681 int low; /* binary search lower limit */
3682 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3683
3684 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003685 ASSERT(page_idx >= 0);
3686 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3687 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3688
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003689 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3690 erp_idx = 0;
3691 low = 0;
3692 high = nlists - 1;
3693
3694 /* Binary search extent irec's */
3695 while (low <= high) {
3696 erp_idx = (low + high) >> 1;
3697 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3698 prev = erp_idx > 0 ? erp - 1 : NULL;
3699 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3700 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3701 high = erp_idx - 1;
3702 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3703 (page_idx == erp->er_extoff + erp->er_extcount &&
3704 !realloc)) {
3705 low = erp_idx + 1;
3706 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3707 erp->er_extcount == XFS_LINEAR_EXTS) {
3708 ASSERT(realloc);
3709 page_idx = 0;
3710 erp_idx++;
3711 erp = erp_idx < nlists ? erp + 1 : NULL;
3712 break;
3713 } else {
3714 page_idx -= erp->er_extoff;
3715 break;
3716 }
3717 }
3718 *idxp = page_idx;
3719 *erp_idxp = erp_idx;
3720 return(erp);
3721}
3722
3723/*
3724 * Allocate and initialize an indirection array once the space needed
3725 * for incore extents increases above XFS_IEXT_BUFSZ.
3726 */
3727void
3728xfs_iext_irec_init(
3729 xfs_ifork_t *ifp) /* inode fork pointer */
3730{
3731 xfs_ext_irec_t *erp; /* indirection array pointer */
3732 xfs_extnum_t nextents; /* number of extents in file */
3733
3734 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3735 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3736 ASSERT(nextents <= XFS_LINEAR_EXTS);
3737
David Chinner67850732008-08-13 16:02:51 +10003738 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003739
3740 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003741 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003742 } else if (!ifp->if_real_bytes) {
3743 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3744 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3745 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3746 }
3747 erp->er_extbuf = ifp->if_u1.if_extents;
3748 erp->er_extcount = nextents;
3749 erp->er_extoff = 0;
3750
3751 ifp->if_flags |= XFS_IFEXTIREC;
3752 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3753 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3754 ifp->if_u1.if_ext_irec = erp;
3755
3756 return;
3757}
3758
3759/*
3760 * Allocate and initialize a new entry in the indirection array.
3761 */
3762xfs_ext_irec_t *
3763xfs_iext_irec_new(
3764 xfs_ifork_t *ifp, /* inode fork pointer */
3765 int erp_idx) /* index for new irec */
3766{
3767 xfs_ext_irec_t *erp; /* indirection array pointer */
3768 int i; /* loop counter */
3769 int nlists; /* number of irec's (ex lists) */
3770
3771 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3772 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3773
3774 /* Resize indirection array */
3775 xfs_iext_realloc_indirect(ifp, ++nlists *
3776 sizeof(xfs_ext_irec_t));
3777 /*
3778 * Move records down in the array so the
3779 * new page can use erp_idx.
3780 */
3781 erp = ifp->if_u1.if_ext_irec;
3782 for (i = nlists - 1; i > erp_idx; i--) {
3783 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3784 }
3785 ASSERT(i == erp_idx);
3786
3787 /* Initialize new extent record */
3788 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003789 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003790 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3791 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3792 erp[erp_idx].er_extcount = 0;
3793 erp[erp_idx].er_extoff = erp_idx > 0 ?
3794 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3795 return (&erp[erp_idx]);
3796}
3797
3798/*
3799 * Remove a record from the indirection array.
3800 */
3801void
3802xfs_iext_irec_remove(
3803 xfs_ifork_t *ifp, /* inode fork pointer */
3804 int erp_idx) /* irec index to remove */
3805{
3806 xfs_ext_irec_t *erp; /* indirection array pointer */
3807 int i; /* loop counter */
3808 int nlists; /* number of irec's (ex lists) */
3809
3810 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3811 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3812 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3813 if (erp->er_extbuf) {
3814 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3815 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003816 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003817 }
3818 /* Compact extent records */
3819 erp = ifp->if_u1.if_ext_irec;
3820 for (i = erp_idx; i < nlists - 1; i++) {
3821 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3822 }
3823 /*
3824 * Manually free the last extent record from the indirection
3825 * array. A call to xfs_iext_realloc_indirect() with a size
3826 * of zero would result in a call to xfs_iext_destroy() which
3827 * would in turn call this function again, creating a nasty
3828 * infinite loop.
3829 */
3830 if (--nlists) {
3831 xfs_iext_realloc_indirect(ifp,
3832 nlists * sizeof(xfs_ext_irec_t));
3833 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003834 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003835 }
3836 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3837}
3838
3839/*
3840 * This is called to clean up large amounts of unused memory allocated
3841 * by the indirection array. Before compacting anything though, verify
3842 * that the indirection array is still needed and switch back to the
3843 * linear extent list (or even the inline buffer) if possible. The
3844 * compaction policy is as follows:
3845 *
3846 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003847 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003848 * No Compaction: Extents occupy at least 50% of allocated space
3849 */
3850void
3851xfs_iext_irec_compact(
3852 xfs_ifork_t *ifp) /* inode fork pointer */
3853{
3854 xfs_extnum_t nextents; /* number of extents in file */
3855 int nlists; /* number of irec's (ex lists) */
3856
3857 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3858 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3859 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3860
3861 if (nextents == 0) {
3862 xfs_iext_destroy(ifp);
3863 } else if (nextents <= XFS_INLINE_EXTS) {
3864 xfs_iext_indirect_to_direct(ifp);
3865 xfs_iext_direct_to_inline(ifp, nextents);
3866 } else if (nextents <= XFS_LINEAR_EXTS) {
3867 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003868 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3869 xfs_iext_irec_compact_pages(ifp);
3870 }
3871}
3872
3873/*
3874 * Combine extents from neighboring extent pages.
3875 */
3876void
3877xfs_iext_irec_compact_pages(
3878 xfs_ifork_t *ifp) /* inode fork pointer */
3879{
3880 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3881 int erp_idx = 0; /* indirection array index */
3882 int nlists; /* number of irec's (ex lists) */
3883
3884 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3885 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3886 while (erp_idx < nlists - 1) {
3887 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3888 erp_next = erp + 1;
3889 if (erp_next->er_extcount <=
3890 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003891 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003892 erp_next->er_extbuf, erp_next->er_extcount *
3893 sizeof(xfs_bmbt_rec_t));
3894 erp->er_extcount += erp_next->er_extcount;
3895 /*
3896 * Free page before removing extent record
3897 * so er_extoffs don't get modified in
3898 * xfs_iext_irec_remove.
3899 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003900 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003901 erp_next->er_extbuf = NULL;
3902 xfs_iext_irec_remove(ifp, erp_idx + 1);
3903 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3904 } else {
3905 erp_idx++;
3906 }
3907 }
3908}
3909
3910/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003911 * This is called to update the er_extoff field in the indirection
3912 * array when extents have been added or removed from one of the
3913 * extent lists. erp_idx contains the irec index to begin updating
3914 * at and ext_diff contains the number of extents that were added
3915 * or removed.
3916 */
3917void
3918xfs_iext_irec_update_extoffs(
3919 xfs_ifork_t *ifp, /* inode fork pointer */
3920 int erp_idx, /* irec index to update */
3921 int ext_diff) /* number of new extents */
3922{
3923 int i; /* loop counter */
3924 int nlists; /* number of irec's (ex lists */
3925
3926 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3927 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3928 for (i = erp_idx; i < nlists; i++) {
3929 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3930 }
3931}
Brian Foster72b53ef2012-11-06 09:50:40 -05003932
3933/*
3934 * Test whether it is appropriate to check an inode for and free post EOF
3935 * blocks. The 'force' parameter determines whether we should also consider
3936 * regular files that are marked preallocated or append-only.
3937 */
3938bool
3939xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
3940{
3941 /* prealloc/delalloc exists only on regular files */
3942 if (!S_ISREG(ip->i_d.di_mode))
3943 return false;
3944
3945 /*
3946 * Zero sized files with no cached pages and delalloc blocks will not
3947 * have speculative prealloc/delalloc blocks to remove.
3948 */
3949 if (VFS_I(ip)->i_size == 0 &&
3950 VN_CACHED(VFS_I(ip)) == 0 &&
3951 ip->i_delayed_blks == 0)
3952 return false;
3953
3954 /* If we haven't read in the extent list, then don't do it now. */
3955 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
3956 return false;
3957
3958 /*
3959 * Do not free real preallocated or append-only files unless the file
3960 * has delalloc blocks and we are forced to remove them.
3961 */
3962 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
3963 if (!force || ip->i_delayed_blks == 0)
3964 return false;
3965
3966 return true;
3967}
3968