blob: 965598eb308c74904f0f3dbd6da808dc99142d5c [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
385/*
Christoph Hellwig475ee412012-07-03 12:21:22 -0400386 * This routine is called to map an inode to the buffer containing the on-disk
387 * version of the inode. It returns a pointer to the buffer containing the
388 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
389 * pointer to the on-disk inode within that buffer.
390 *
391 * If a non-zero error is returned, then the contents of bpp and dipp are
392 * undefined.
David Chinner4ae29b42008-03-06 13:43:34 +1100393 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400394int
David Chinner4ae29b42008-03-06 13:43:34 +1100395xfs_imap_to_bp(
Christoph Hellwig475ee412012-07-03 12:21:22 -0400396 struct xfs_mount *mp,
397 struct xfs_trans *tp,
398 struct xfs_imap *imap,
399 struct xfs_dinode **dipp,
400 struct xfs_buf **bpp,
401 uint buf_flags,
402 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100403{
Christoph Hellwig475ee412012-07-03 12:21:22 -0400404 struct xfs_buf *bp;
405 int error;
406 int i;
407 int ni;
David Chinner4ae29b42008-03-06 13:43:34 +1100408
Dave Chinner611c9942012-04-23 15:59:07 +1000409 buf_flags |= XBF_UNMAPPED;
David Chinner4ae29b42008-03-06 13:43:34 +1100410 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100411 (int)imap->im_len, buf_flags, &bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100412 if (error) {
David Chinnera3f74ff2008-03-06 13:43:42 +1100413 if (error != EAGAIN) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100414 xfs_warn(mp,
415 "%s: xfs_trans_read_buf() returned error %d.",
416 __func__, error);
David Chinnera3f74ff2008-03-06 13:43:42 +1100417 } else {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000418 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100419 }
David Chinner4ae29b42008-03-06 13:43:34 +1100420 return error;
421 }
422
423 /*
424 * Validate the magic number and version of every inode in the buffer
425 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
426 */
427#ifdef DEBUG
428 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
429#else /* usual case */
430 ni = 1;
431#endif
432
433 for (i = 0; i < ni; i++) {
434 int di_ok;
435 xfs_dinode_t *dip;
436
437 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
438 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200439 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100440 XFS_DINODE_GOOD_VERSION(dip->di_version);
David Chinner4ae29b42008-03-06 13:43:34 +1100441 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
442 XFS_ERRTAG_ITOBP_INOTOBP,
443 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinner19207792010-06-24 11:15:47 +1000444 if (iget_flags & XFS_IGET_UNTRUSTED) {
David Chinner4ae29b42008-03-06 13:43:34 +1100445 xfs_trans_brelse(tp, bp);
446 return XFS_ERROR(EINVAL);
447 }
Christoph Hellwig475ee412012-07-03 12:21:22 -0400448 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
449 mp, dip);
David Chinner4ae29b42008-03-06 13:43:34 +1100450#ifdef DEBUG
Dave Chinner0b932cc2011-03-07 10:08:35 +1100451 xfs_emerg(mp,
452 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
David Chinner4ae29b42008-03-06 13:43:34 +1100453 (unsigned long long)imap->im_blkno, i,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100454 be16_to_cpu(dip->di_magic));
Dave Chinner0b932cc2011-03-07 10:08:35 +1100455 ASSERT(0);
David Chinner4ae29b42008-03-06 13:43:34 +1100456#endif
457 xfs_trans_brelse(tp, bp);
458 return XFS_ERROR(EFSCORRUPTED);
459 }
460 }
461
462 xfs_inobp_check(mp, bp);
Christoph Hellwig475ee412012-07-03 12:21:22 -0400463
David Chinner4ae29b42008-03-06 13:43:34 +1100464 *bpp = bp;
Christoph Hellwig475ee412012-07-03 12:21:22 -0400465 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
David Chinner4ae29b42008-03-06 13:43:34 +1100466 return 0;
467}
468
469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * Move inode type and inode format specific information from the
471 * on-disk inode to the in-core inode. For fifos, devs, and sockets
472 * this means set if_rdev to the proper value. For files, directories,
473 * and symlinks this means to bring in the in-line data or extent
474 * pointers. For a file in B-tree format, only the root is immediately
475 * brought in-core. The rest will be in-lined in if_extents when it
476 * is first referenced (see xfs_iread_extents()).
477 */
478STATIC int
479xfs_iformat(
480 xfs_inode_t *ip,
481 xfs_dinode_t *dip)
482{
483 xfs_attr_shortform_t *atp;
484 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000485 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100488 if (unlikely(be32_to_cpu(dip->di_nextents) +
489 be16_to_cpu(dip->di_anextents) >
490 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100491 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100492 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100494 (int)(be32_to_cpu(dip->di_nextents) +
495 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100497 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
499 ip->i_mount, dip);
500 return XFS_ERROR(EFSCORRUPTED);
501 }
502
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100503 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100504 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100506 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
508 ip->i_mount, dip);
509 return XFS_ERROR(EFSCORRUPTED);
510 }
511
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300512 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
513 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100514 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300515 "corrupt dinode %Lu, has realtime flag set.",
516 ip->i_ino);
517 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
518 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
519 return XFS_ERROR(EFSCORRUPTED);
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 switch (ip->i_d.di_mode & S_IFMT) {
523 case S_IFIFO:
524 case S_IFCHR:
525 case S_IFBLK:
526 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100527 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
529 ip->i_mount, dip);
530 return XFS_ERROR(EFSCORRUPTED);
531 }
532 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100533 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 break;
535
536 case S_IFREG:
537 case S_IFLNK:
538 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100539 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 case XFS_DINODE_FMT_LOCAL:
541 /*
542 * no local regular files yet
543 */
Al Viroabbede12011-07-26 02:31:30 -0400544 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100545 xfs_warn(ip->i_mount,
546 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (unsigned long long) ip->i_ino);
548 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
549 XFS_ERRLEVEL_LOW,
550 ip->i_mount, dip);
551 return XFS_ERROR(EFSCORRUPTED);
552 }
553
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100554 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100556 xfs_warn(ip->i_mount,
557 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 (unsigned long long) ip->i_ino,
559 (long long) di_size);
560 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
561 XFS_ERRLEVEL_LOW,
562 ip->i_mount, dip);
563 return XFS_ERROR(EFSCORRUPTED);
564 }
565
566 size = (int)di_size;
567 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
568 break;
569 case XFS_DINODE_FMT_EXTENTS:
570 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
571 break;
572 case XFS_DINODE_FMT_BTREE:
573 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
574 break;
575 default:
576 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
577 ip->i_mount);
578 return XFS_ERROR(EFSCORRUPTED);
579 }
580 break;
581
582 default:
583 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
584 return XFS_ERROR(EFSCORRUPTED);
585 }
586 if (error) {
587 return error;
588 }
589 if (!XFS_DFORK_Q(dip))
590 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000593 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000594
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100595 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case XFS_DINODE_FMT_LOCAL:
597 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100598 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100599
600 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100601 xfs_warn(ip->i_mount,
602 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100603 (unsigned long long) ip->i_ino,
604 (long long) size);
605 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
606 XFS_ERRLEVEL_LOW,
607 ip->i_mount, dip);
608 return XFS_ERROR(EFSCORRUPTED);
609 }
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
612 break;
613 case XFS_DINODE_FMT_EXTENTS:
614 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
615 break;
616 case XFS_DINODE_FMT_BTREE:
617 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
618 break;
619 default:
620 error = XFS_ERROR(EFSCORRUPTED);
621 break;
622 }
623 if (error) {
624 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
625 ip->i_afp = NULL;
626 xfs_idestroy_fork(ip, XFS_DATA_FORK);
627 }
628 return error;
629}
630
631/*
632 * The file is in-lined in the on-disk inode.
633 * If it fits into if_inline_data, then copy
634 * it there, otherwise allocate a buffer for it
635 * and copy the data there. Either way, set
636 * if_data to point at the data.
637 * If we allocate a buffer for the data, make
638 * sure that its size is a multiple of 4 and
639 * record the real size in i_real_bytes.
640 */
641STATIC int
642xfs_iformat_local(
643 xfs_inode_t *ip,
644 xfs_dinode_t *dip,
645 int whichfork,
646 int size)
647{
648 xfs_ifork_t *ifp;
649 int real_size;
650
651 /*
652 * If the size is unreasonable, then something
653 * is wrong and we just bail out rather than crash in
654 * kmem_alloc() or memcpy() below.
655 */
656 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100657 xfs_warn(ip->i_mount,
658 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 (unsigned long long) ip->i_ino, size,
660 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
661 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
662 ip->i_mount, dip);
663 return XFS_ERROR(EFSCORRUPTED);
664 }
665 ifp = XFS_IFORK_PTR(ip, whichfork);
666 real_size = 0;
667 if (size == 0)
668 ifp->if_u1.if_data = NULL;
669 else if (size <= sizeof(ifp->if_u2.if_inline_data))
670 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
671 else {
672 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000673 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 ifp->if_bytes = size;
676 ifp->if_real_bytes = real_size;
677 if (size)
678 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
679 ifp->if_flags &= ~XFS_IFEXTENTS;
680 ifp->if_flags |= XFS_IFINLINE;
681 return 0;
682}
683
684/*
685 * The file consists of a set of extents all
686 * of which fit into the on-disk inode.
687 * If there are few enough extents to fit into
688 * the if_inline_ext, then copy them there.
689 * Otherwise allocate a buffer for them and copy
690 * them into it. Either way, set if_extents
691 * to point at the extents.
692 */
693STATIC int
694xfs_iformat_extents(
695 xfs_inode_t *ip,
696 xfs_dinode_t *dip,
697 int whichfork)
698{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000699 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 xfs_ifork_t *ifp;
701 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int size;
703 int i;
704
705 ifp = XFS_IFORK_PTR(ip, whichfork);
706 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
707 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
708
709 /*
710 * If the number of extents is unreasonable, then something
711 * is wrong and we just bail out rather than crash in
712 * kmem_alloc() or memcpy() below.
713 */
714 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100715 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 (unsigned long long) ip->i_ino, nex);
717 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
718 ip->i_mount, dip);
719 return XFS_ERROR(EFSCORRUPTED);
720 }
721
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100722 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (nex == 0)
724 ifp->if_u1.if_extents = NULL;
725 else if (nex <= XFS_INLINE_EXTS)
726 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100727 else
728 xfs_iext_add(ifp, 0, nex);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (size) {
732 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000733 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100734 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000735 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000736 ep->l0 = get_unaligned_be64(&dp->l0);
737 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000739 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (whichfork != XFS_DATA_FORK ||
741 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
742 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100743 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
745 XFS_ERRLEVEL_LOW,
746 ip->i_mount);
747 return XFS_ERROR(EFSCORRUPTED);
748 }
749 }
750 ifp->if_flags |= XFS_IFEXTENTS;
751 return 0;
752}
753
754/*
755 * The file has too many extents to fit into
756 * the inode, so they are in B-tree format.
757 * Allocate a buffer for the root of the B-tree
758 * and copy the root into it. The i_extents
759 * field will remain NULL until all of the
760 * extents are read in (when they are needed).
761 */
762STATIC int
763xfs_iformat_btree(
764 xfs_inode_t *ip,
765 xfs_dinode_t *dip,
766 int whichfork)
767{
768 xfs_bmdr_block_t *dfp;
769 xfs_ifork_t *ifp;
770 /* REFERENCED */
771 int nrecs;
772 int size;
773
774 ifp = XFS_IFORK_PTR(ip, whichfork);
775 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
776 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100777 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /*
780 * blow out if -- fork has less extents than can fit in
781 * fork (fork shouldn't be a btree format), root btree
782 * block has more records than can fit into the fork,
783 * or the number of extents is greater than the number of
784 * blocks.
785 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000786 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
787 XFS_IFORK_MAXEXT(ip, whichfork) ||
788 XFS_BMDR_SPACE_CALC(nrecs) >
789 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
790 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100791 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100793 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
794 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return XFS_ERROR(EFSCORRUPTED);
796 }
797
798 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000799 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ASSERT(ifp->if_broot != NULL);
801 /*
802 * Copy and convert from the on-disk structure
803 * to the in-memory structure.
804 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100805 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
806 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
807 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ifp->if_flags &= ~XFS_IFEXTENTS;
809 ifp->if_flags |= XFS_IFBROOT;
810
811 return 0;
812}
813
Eric Sandeend96f8f82009-07-02 00:09:33 -0500814STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000815xfs_dinode_from_disk(
816 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100817 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000819 to->di_magic = be16_to_cpu(from->di_magic);
820 to->di_mode = be16_to_cpu(from->di_mode);
821 to->di_version = from ->di_version;
822 to->di_format = from->di_format;
823 to->di_onlink = be16_to_cpu(from->di_onlink);
824 to->di_uid = be32_to_cpu(from->di_uid);
825 to->di_gid = be32_to_cpu(from->di_gid);
826 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000827 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
828 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000829 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
830 to->di_flushiter = be16_to_cpu(from->di_flushiter);
831 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
832 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
833 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
834 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
835 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
836 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
837 to->di_size = be64_to_cpu(from->di_size);
838 to->di_nblocks = be64_to_cpu(from->di_nblocks);
839 to->di_extsize = be32_to_cpu(from->di_extsize);
840 to->di_nextents = be32_to_cpu(from->di_nextents);
841 to->di_anextents = be16_to_cpu(from->di_anextents);
842 to->di_forkoff = from->di_forkoff;
843 to->di_aformat = from->di_aformat;
844 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
845 to->di_dmstate = be16_to_cpu(from->di_dmstate);
846 to->di_flags = be16_to_cpu(from->di_flags);
847 to->di_gen = be32_to_cpu(from->di_gen);
848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000850void
851xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100852 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000853 xfs_icdinode_t *from)
854{
855 to->di_magic = cpu_to_be16(from->di_magic);
856 to->di_mode = cpu_to_be16(from->di_mode);
857 to->di_version = from ->di_version;
858 to->di_format = from->di_format;
859 to->di_onlink = cpu_to_be16(from->di_onlink);
860 to->di_uid = cpu_to_be32(from->di_uid);
861 to->di_gid = cpu_to_be32(from->di_gid);
862 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000863 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
864 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000865 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
866 to->di_flushiter = cpu_to_be16(from->di_flushiter);
867 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
868 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
869 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
870 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
871 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
872 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
873 to->di_size = cpu_to_be64(from->di_size);
874 to->di_nblocks = cpu_to_be64(from->di_nblocks);
875 to->di_extsize = cpu_to_be32(from->di_extsize);
876 to->di_nextents = cpu_to_be32(from->di_nextents);
877 to->di_anextents = cpu_to_be16(from->di_anextents);
878 to->di_forkoff = from->di_forkoff;
879 to->di_aformat = from->di_aformat;
880 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
881 to->di_dmstate = cpu_to_be16(from->di_dmstate);
882 to->di_flags = cpu_to_be16(from->di_flags);
883 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886STATIC uint
887_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 __uint16_t di_flags)
889{
890 uint flags = 0;
891
892 if (di_flags & XFS_DIFLAG_ANY) {
893 if (di_flags & XFS_DIFLAG_REALTIME)
894 flags |= XFS_XFLAG_REALTIME;
895 if (di_flags & XFS_DIFLAG_PREALLOC)
896 flags |= XFS_XFLAG_PREALLOC;
897 if (di_flags & XFS_DIFLAG_IMMUTABLE)
898 flags |= XFS_XFLAG_IMMUTABLE;
899 if (di_flags & XFS_DIFLAG_APPEND)
900 flags |= XFS_XFLAG_APPEND;
901 if (di_flags & XFS_DIFLAG_SYNC)
902 flags |= XFS_XFLAG_SYNC;
903 if (di_flags & XFS_DIFLAG_NOATIME)
904 flags |= XFS_XFLAG_NOATIME;
905 if (di_flags & XFS_DIFLAG_NODUMP)
906 flags |= XFS_XFLAG_NODUMP;
907 if (di_flags & XFS_DIFLAG_RTINHERIT)
908 flags |= XFS_XFLAG_RTINHERIT;
909 if (di_flags & XFS_DIFLAG_PROJINHERIT)
910 flags |= XFS_XFLAG_PROJINHERIT;
911 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
912 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100913 if (di_flags & XFS_DIFLAG_EXTSIZE)
914 flags |= XFS_XFLAG_EXTSIZE;
915 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
916 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000917 if (di_flags & XFS_DIFLAG_NODEFRAG)
918 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000919 if (di_flags & XFS_DIFLAG_FILESTREAM)
920 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 return flags;
924}
925
926uint
927xfs_ip2xflags(
928 xfs_inode_t *ip)
929{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000930 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Nathan Scotta916e2b2006-06-09 17:12:17 +1000932 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100933 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936uint
937xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100938 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100940 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100941 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100945 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947int
948xfs_iread(
949 xfs_mount_t *mp,
950 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100951 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100952 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 xfs_buf_t *bp;
955 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int error;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100959 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100961 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100962 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100963 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100966 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100967 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400968 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100969 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100970 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100971
972 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * If we got something that isn't an inode it means someone
974 * (nfs or dmi) has a stale handle.
975 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200976 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100978 xfs_alert(mp,
979 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
980 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100982 error = XFS_ERROR(EINVAL);
983 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 /*
987 * If the on-disk inode is already linked to a directory
988 * entry, copy all of the inode into the in-core inode.
989 * xfs_iformat() handles copying in the inode format
990 * specific information.
991 * Otherwise, just get the truly permanent information.
992 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100993 if (dip->di_mode) {
994 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 error = xfs_iformat(ip, dip);
996 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100998 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
999 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001001 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001004 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1005 ip->i_d.di_version = dip->di_version;
1006 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1007 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * Make sure to pull in the mode here as well in
1010 * case the inode is released without being used.
1011 * This ensures that xfs_inactive() will see that
1012 * the inode is already free and not try to mess
1013 * with the uninitialized part of it.
1014 */
1015 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /*
1019 * The inode format changed when we moved the link count and
1020 * made it 32 bits long. If this is an old format inode,
1021 * convert it in memory to look like a new one. If it gets
1022 * flushed to disk we will convert back before flushing or
1023 * logging it. We zero out the new projid field and the old link
1024 * count field. We'll handle clearing the pad field (the remains
1025 * of the old uuid field) when we actually convert the inode to
1026 * the new format. We don't change the version number so that we
1027 * can distinguish this from a real new format inode.
1028 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001029 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ip->i_d.di_nlink = ip->i_d.di_onlink;
1031 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001032 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 ip->i_delayed_blks = 0;
1036
1037 /*
1038 * Mark the buffer containing the inode as something to keep
1039 * around for a while. This helps to keep recently accessed
1040 * meta-data in-core longer.
1041 */
Dave Chinner821eb212010-12-02 16:31:13 +11001042 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
1045 * Use xfs_trans_brelse() to release the buffer containing the
1046 * on-disk inode, because it was acquired with xfs_trans_read_buf()
Christoph Hellwig475ee412012-07-03 12:21:22 -04001047 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1049 * will only release the buffer if it is not dirty within the
1050 * transaction. It will be OK to release the buffer in this case,
1051 * because inodes on disk are never destroyed and we will be
1052 * locking the new in-core inode before putting it in the hash
1053 * table where other processes can find it. Thus we don't have
1054 * to worry about the inode being changed just because we released
1055 * the buffer.
1056 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001057 out_brelse:
1058 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001059 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/*
1063 * Read in extents from a btree-format inode.
1064 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1065 */
1066int
1067xfs_iread_extents(
1068 xfs_trans_t *tp,
1069 xfs_inode_t *ip,
1070 int whichfork)
1071{
1072 int error;
1073 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001074 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1077 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1078 ip->i_mount);
1079 return XFS_ERROR(EFSCORRUPTED);
1080 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001081 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * We know that the size is valid (it's checked in iformat_btree)
1086 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001087 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001089 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 error = xfs_bmap_read_extents(tp, ip, whichfork);
1091 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001092 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ifp->if_flags &= ~XFS_IFEXTENTS;
1094 return error;
1095 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001096 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
1100/*
1101 * Allocate an inode on disk and return a copy of its in-core version.
1102 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1103 * appropriately within the inode. The uid and gid for the inode are
1104 * set according to the contents of the given cred structure.
1105 *
1106 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001107 * has a free inode available, call xfs_iget() to obtain the in-core
1108 * version of the allocated inode. Finally, fill in the inode and
1109 * log its initial contents. In this case, ialloc_context would be
1110 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001112 * If xfs_dialloc() does not have an available inode, it will replenish
1113 * its supply by doing an allocation. Since we can only do one
1114 * allocation within a transaction without deadlocks, we must commit
1115 * the current transaction before returning the inode itself.
1116 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * The caller should then commit the current transaction, start a new
1118 * transaction, and call xfs_ialloc() again to actually get the inode.
1119 *
1120 * To ensure that some other process does not grab the inode that
1121 * was allocated during the first call to xfs_ialloc(), this routine
1122 * also returns the [locked] bp pointing to the head of the freelist
1123 * as ialloc_context. The caller should hold this buffer across
1124 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +10001125 *
1126 * If we are allocating quota inodes, we do not have a parent inode
1127 * to attach to or associate with (i.e. pip == NULL) because they
1128 * are not linked into the directory structure - they are attached
1129 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
1131int
1132xfs_ialloc(
1133 xfs_trans_t *tp,
1134 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -04001135 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -07001136 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001138 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int okalloc,
1140 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 xfs_inode_t **ipp)
1142{
1143 xfs_ino_t ino;
1144 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 uint flags;
1146 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001147 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +11001148 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /*
1151 * Call the space management code to pick
1152 * the on-disk inode to be allocated.
1153 */
David Chinnerb11f94d2007-07-11 11:09:33 +10001154 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -04001155 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +11001156 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -04001158 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *ipp = NULL;
1160 return 0;
1161 }
1162 ASSERT(*ialloc_context == NULL);
1163
1164 /*
1165 * Get the in-core inode with the lock held exclusively.
1166 * This is because we're setting fields here we need
1167 * to prevent others from looking at until we're done.
1168 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001169 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
1170 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +11001171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ASSERT(ip != NULL);
1174
Al Viro576b1d62011-07-26 02:50:15 -04001175 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ip->i_d.di_onlink = 0;
1177 ip->i_d.di_nlink = nlink;
1178 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +01001179 ip->i_d.di_uid = current_fsuid();
1180 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001181 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1183
1184 /*
1185 * If the superblock version is up to where we support new format
1186 * inodes and this is currently an old format inode, then change
1187 * the inode version number now. This way we only do the conversion
1188 * here rather than here and in the flush/logging code.
1189 */
Eric Sandeen62118702008-03-06 13:44:28 +11001190 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001191 ip->i_d.di_version == 1) {
1192 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * We've already zeroed the old link count, the projid field,
1195 * and the pad field.
1196 */
1197 }
1198
1199 /*
1200 * Project ids won't be stored on disk if we are using a version 1 inode.
1201 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001202 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 xfs_bump_ino_vers2(tp, ip);
1204
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001205 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001207 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 ip->i_d.di_mode |= S_ISGID;
1209 }
1210 }
1211
1212 /*
1213 * If the group ID of the new file does not match the effective group
1214 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1215 * (and only if the irix_sgid_inherit compatibility variable is set).
1216 */
1217 if ((irix_sgid_inherit) &&
1218 (ip->i_d.di_mode & S_ISGID) &&
1219 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1220 ip->i_d.di_mode &= ~S_ISGID;
1221 }
1222
1223 ip->i_d.di_size = 0;
1224 ip->i_d.di_nextents = 0;
1225 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001226
1227 nanotime(&tv);
1228 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1229 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1230 ip->i_d.di_atime = ip->i_d.di_mtime;
1231 ip->i_d.di_ctime = ip->i_d.di_mtime;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * di_gen will have been taken care of in xfs_iread.
1235 */
1236 ip->i_d.di_extsize = 0;
1237 ip->i_d.di_dmevmask = 0;
1238 ip->i_d.di_dmstate = 0;
1239 ip->i_d.di_flags = 0;
1240 flags = XFS_ILOG_CORE;
1241 switch (mode & S_IFMT) {
1242 case S_IFIFO:
1243 case S_IFCHR:
1244 case S_IFBLK:
1245 case S_IFSOCK:
1246 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1247 ip->i_df.if_u2.if_rdev = rdev;
1248 ip->i_df.if_flags = 0;
1249 flags |= XFS_ILOG_DEV;
1250 break;
1251 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001252 /*
1253 * we can't set up filestreams until after the VFS inode
1254 * is set up properly.
1255 */
1256 if (pip && xfs_inode_is_filestream(pip))
1257 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001258 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001260 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001261 uint di_flags = 0;
1262
Al Viroabbede12011-07-26 02:31:30 -04001263 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001264 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1265 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001266 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1267 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1268 ip->i_d.di_extsize = pip->i_d.di_extsize;
1269 }
Al Viroabbede12011-07-26 02:31:30 -04001270 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001271 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001272 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001273 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1274 di_flags |= XFS_DIFLAG_EXTSIZE;
1275 ip->i_d.di_extsize = pip->i_d.di_extsize;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1279 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001280 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1282 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001283 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1285 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001286 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1288 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001289 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1290 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1291 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001292 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1293 xfs_inherit_nodefrag)
1294 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001295 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1296 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001297 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 /* FALLTHROUGH */
1300 case S_IFLNK:
1301 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1302 ip->i_df.if_flags = XFS_IFEXTENTS;
1303 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1304 ip->i_df.if_u1.if_extents = NULL;
1305 break;
1306 default:
1307 ASSERT(0);
1308 }
1309 /*
1310 * Attribute fork settings for new inode.
1311 */
1312 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1313 ip->i_d.di_anextents = 0;
1314
1315 /*
1316 * Log the new values stuffed into the inode.
1317 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001318 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 xfs_trans_log_inode(tp, ip, flags);
1320
Nathan Scottb83bd132006-06-09 16:48:30 +10001321 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001322 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
David Chinnerbf904242008-10-30 17:36:14 +11001324 /* now we have set up the vfs inode we can associate the filestream */
1325 if (filestreams) {
1326 error = xfs_filestream_associate(pip, ip);
1327 if (error < 0)
1328 return -error;
1329 if (!error)
1330 xfs_iflags_set(ip, XFS_IFILESTREAM);
1331 }
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *ipp = ip;
1334 return 0;
1335}
1336
1337/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001338 * Free up the underlying blocks past new_size. The new size must be smaller
1339 * than the current size. This routine can be used both for the attribute and
1340 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 *
David Chinnerf6485052008-04-17 16:50:04 +10001342 * The transaction passed to this routine must have made a permanent log
1343 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1344 * given transaction and start new ones, so make sure everything involved in
1345 * the transaction is tidy before calling here. Some transaction will be
1346 * returned to the caller to be committed. The incoming transaction must
1347 * already include the inode, and both inode locks must be held exclusively.
1348 * The inode must also be "held" within the transaction. On return the inode
1349 * will be "held" within the returned transaction. This routine does NOT
1350 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
David Chinnerf6485052008-04-17 16:50:04 +10001352 * If we get an error, we must return with the inode locked and linked into the
1353 * current transaction. This keeps things simple for the higher level code,
1354 * because it always knows that the inode is locked and held in the transaction
1355 * that returns to it whether errors occur or not. We don't mark the inode
1356 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001359xfs_itruncate_extents(
1360 struct xfs_trans **tpp,
1361 struct xfs_inode *ip,
1362 int whichfork,
1363 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001365 struct xfs_mount *mp = ip->i_mount;
1366 struct xfs_trans *tp = *tpp;
1367 struct xfs_trans *ntp;
1368 xfs_bmap_free_t free_list;
1369 xfs_fsblock_t first_block;
1370 xfs_fileoff_t first_unmap_block;
1371 xfs_fileoff_t last_block;
1372 xfs_filblks_t unmap_len;
1373 int committed;
1374 int error = 0;
1375 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Christoph Hellwig0b561852012-07-04 11:13:31 -04001377 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1378 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1379 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001380 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001381 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001383 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001384 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001386 trace_xfs_itruncate_extents_start(ip, new_size);
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /*
1389 * Since it is possible for space to become allocated beyond
1390 * the end of the file (in a crash where the space is allocated
1391 * but the inode size is not yet updated), simply remove any
1392 * blocks which show up between the new EOF and the maximum
1393 * possible file size. If the first block to be removed is
1394 * beyond the maximum file size (ie it is the same as last_block),
1395 * then there is nothing to do.
1396 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001397 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001398 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001399 if (first_unmap_block == last_block)
1400 return 0;
1401
1402 ASSERT(first_unmap_block < last_block);
1403 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001405 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001406 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001407 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001408 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001410 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001411 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001412 if (error)
1413 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
1416 * Duplicate the transaction that has the permanent
1417 * reservation and commit the old transaction.
1418 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001419 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001420 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001421 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001422 if (error)
1423 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (committed) {
1426 /*
David Chinnerf6485052008-04-17 16:50:04 +10001427 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001428 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001430 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
David Chinnerf6485052008-04-17 16:50:04 +10001432
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001433 ntp = xfs_trans_dup(tp);
1434 error = xfs_trans_commit(tp, 0);
1435 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001436
Christoph Hellwigddc34152011-09-19 15:00:54 +00001437 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001438
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001439 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001440 goto out;
1441
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001442 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001443 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001444 * reference that we gained in xfs_trans_dup()
1445 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001446 xfs_log_ticket_put(tp->t_ticket);
1447 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001448 XFS_ITRUNCATE_LOG_RES(mp), 0,
1449 XFS_TRANS_PERM_LOG_RES,
1450 XFS_ITRUNCATE_LOG_COUNT);
1451 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001454
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001455 /*
1456 * Always re-log the inode so that our permanent transaction can keep
1457 * on rolling it forward in the log.
1458 */
1459 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1460
1461 trace_xfs_itruncate_extents_end(ip, new_size);
1462
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001463out:
1464 *tpp = tp;
1465 return error;
1466out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001468 * If the bunmapi call encounters an error, return to the caller where
1469 * the transaction can be properly aborted. We just need to make sure
1470 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001472 xfs_bmap_cancel(&free_list);
1473 goto out;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * This is called when the inode's link count goes to 0.
1478 * We place the on-disk inode on a list in the AGI. It
1479 * will be pulled from this list when the inode is freed.
1480 */
1481int
1482xfs_iunlink(
1483 xfs_trans_t *tp,
1484 xfs_inode_t *ip)
1485{
1486 xfs_mount_t *mp;
1487 xfs_agi_t *agi;
1488 xfs_dinode_t *dip;
1489 xfs_buf_t *agibp;
1490 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_agino_t agino;
1492 short bucket_index;
1493 int offset;
1494 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 ASSERT(ip->i_d.di_nlink == 0);
1497 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 mp = tp->t_mountp;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /*
1502 * Get the agi buffer first. It ensures lock ordering
1503 * on the list.
1504 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001505 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001506 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * Get the index into the agi hash table for the
1512 * list this inode will go on.
1513 */
1514 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1515 ASSERT(agino != 0);
1516 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1517 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001518 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001520 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /*
1522 * There is already another inode in the bucket we need
1523 * to add ourselves to. Add us at the front of the list.
1524 * Here we put the head pointer into our next pointer,
1525 * and then we fall through to point the head at us.
1526 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001527 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1528 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001529 if (error)
1530 return error;
1531
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001532 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001534 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 offsetof(xfs_dinode_t, di_next_unlinked);
1536 xfs_trans_inode_buf(tp, ibp);
1537 xfs_trans_log_buf(tp, ibp, offset,
1538 (offset + sizeof(xfs_agino_t) - 1));
1539 xfs_inobp_check(mp, ibp);
1540 }
1541
1542 /*
1543 * Point the bucket head pointer at the inode being inserted.
1544 */
1545 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001546 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 offset = offsetof(xfs_agi_t, agi_unlinked) +
1548 (sizeof(xfs_agino_t) * bucket_index);
1549 xfs_trans_log_buf(tp, agibp, offset,
1550 (offset + sizeof(xfs_agino_t) - 1));
1551 return 0;
1552}
1553
1554/*
1555 * Pull the on-disk inode from the AGI unlinked list.
1556 */
1557STATIC int
1558xfs_iunlink_remove(
1559 xfs_trans_t *tp,
1560 xfs_inode_t *ip)
1561{
1562 xfs_ino_t next_ino;
1563 xfs_mount_t *mp;
1564 xfs_agi_t *agi;
1565 xfs_dinode_t *dip;
1566 xfs_buf_t *agibp;
1567 xfs_buf_t *ibp;
1568 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 xfs_agino_t agino;
1570 xfs_agino_t next_agino;
1571 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001572 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001574 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /*
1581 * Get the agi buffer first. It ensures lock ordering
1582 * on the list.
1583 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001584 error = xfs_read_agi(mp, tp, agno, &agibp);
1585 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Get the index into the agi hash table for the
1592 * list this inode will go on.
1593 */
1594 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1595 ASSERT(agino != 0);
1596 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001597 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 ASSERT(agi->agi_unlinked[bucket_index]);
1599
Christoph Hellwig16259e72005-11-02 15:11:25 +11001600 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001602 * We're at the head of the list. Get the inode's on-disk
1603 * buffer to see if there is anyone after us on the list.
1604 * Only modify our next pointer if it is not already NULLAGINO.
1605 * This saves us the overhead of dealing with the buffer when
1606 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001608 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1609 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001611 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001612 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return error;
1614 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001615 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ASSERT(next_agino != 0);
1617 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001618 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001619 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 offsetof(xfs_dinode_t, di_next_unlinked);
1621 xfs_trans_inode_buf(tp, ibp);
1622 xfs_trans_log_buf(tp, ibp, offset,
1623 (offset + sizeof(xfs_agino_t) - 1));
1624 xfs_inobp_check(mp, ibp);
1625 } else {
1626 xfs_trans_brelse(tp, ibp);
1627 }
1628 /*
1629 * Point the bucket head pointer at the next inode.
1630 */
1631 ASSERT(next_agino != 0);
1632 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001633 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 offset = offsetof(xfs_agi_t, agi_unlinked) +
1635 (sizeof(xfs_agino_t) * bucket_index);
1636 xfs_trans_log_buf(tp, agibp, offset,
1637 (offset + sizeof(xfs_agino_t) - 1));
1638 } else {
1639 /*
1640 * We need to search the list for the inode being freed.
1641 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001642 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 last_ibp = NULL;
1644 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001645 struct xfs_imap imap;
1646
1647 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001649
1650 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001652
1653 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001655 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001656 "%s: xfs_imap returned error %d.",
1657 __func__, error);
1658 return error;
1659 }
1660
1661 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1662 &last_ibp, 0, 0);
1663 if (error) {
1664 xfs_warn(mp,
1665 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001666 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return error;
1668 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001669
1670 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001671 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ASSERT(next_agino != NULLAGINO);
1673 ASSERT(next_agino != 0);
1674 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001677 * Now last_ibp points to the buffer previous to us on the
1678 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001680 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1681 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001683 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001684 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return error;
1686 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001687 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 ASSERT(next_agino != 0);
1689 ASSERT(next_agino != agino);
1690 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001691 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001692 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 offsetof(xfs_dinode_t, di_next_unlinked);
1694 xfs_trans_inode_buf(tp, ibp);
1695 xfs_trans_log_buf(tp, ibp, offset,
1696 (offset + sizeof(xfs_agino_t) - 1));
1697 xfs_inobp_check(mp, ibp);
1698 } else {
1699 xfs_trans_brelse(tp, ibp);
1700 }
1701 /*
1702 * Point the previous inode on the list to the next inode.
1703 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001704 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ASSERT(next_agino != 0);
1706 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1707 xfs_trans_inode_buf(tp, last_ibp);
1708 xfs_trans_log_buf(tp, last_ibp, offset,
1709 (offset + sizeof(xfs_agino_t) - 1));
1710 xfs_inobp_check(mp, last_ibp);
1711 }
1712 return 0;
1713}
1714
Dave Chinner5b3eed72010-08-24 11:42:41 +10001715/*
1716 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1717 * inodes that are in memory - they all must be marked stale and attached to
1718 * the cluster buffer.
1719 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001720STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721xfs_ifree_cluster(
1722 xfs_inode_t *free_ip,
1723 xfs_trans_t *tp,
1724 xfs_ino_t inum)
1725{
1726 xfs_mount_t *mp = free_ip->i_mount;
1727 int blks_per_cluster;
1728 int nbufs;
1729 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001730 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 xfs_daddr_t blkno;
1732 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001733 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 xfs_inode_log_item_t *iip;
1735 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001736 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Dave Chinner5017e972010-01-11 11:47:40 +00001738 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1740 blks_per_cluster = 1;
1741 ninodes = mp->m_sb.sb_inopblock;
1742 nbufs = XFS_IALLOC_BLOCKS(mp);
1743 } else {
1744 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1745 mp->m_sb.sb_blocksize;
1746 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1747 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1748 }
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 for (j = 0; j < nbufs; j++, inum += ninodes) {
1751 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1752 XFS_INO_TO_AGBNO(mp, inum));
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001755 * We obtain and lock the backing buffer first in the process
1756 * here, as we have to ensure that any dirty inode that we
1757 * can't get the flush lock on is attached to the buffer.
1758 * If we scan the in-memory inodes first, then buffer IO can
1759 * complete before we get a lock on it, and hence we may fail
1760 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001762 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11001763 mp->m_bsize * blks_per_cluster,
1764 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001766 if (!bp)
1767 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001768 /*
1769 * Walk the inodes already attached to the buffer and mark them
1770 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001771 * in-memory inode walk can't lock them. By marking them all
1772 * stale first, we will not attempt to lock them in the loop
1773 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001774 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001775 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 while (lip) {
1777 if (lip->li_type == XFS_LI_INODE) {
1778 iip = (xfs_inode_log_item_t *)lip;
1779 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001780 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001781 xfs_trans_ail_copy_lsn(mp->m_ail,
1782 &iip->ili_flush_lsn,
1783 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001784 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 lip = lip->li_bio_list;
1787 }
1788
Dave Chinner5b3eed72010-08-24 11:42:41 +10001789
Dave Chinner5b257b42010-06-03 16:22:29 +10001790 /*
1791 * For each inode in memory attempt to add it to the inode
1792 * buffer and set it up for being staled on buffer IO
1793 * completion. This is safe as we've locked out tail pushing
1794 * and flushing by locking the buffer.
1795 *
1796 * We have already marked every inode that was part of a
1797 * transaction stale above, which means there is no point in
1798 * even trying to lock them.
1799 */
1800 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001801retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001802 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001803 ip = radix_tree_lookup(&pag->pag_ici_root,
1804 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001806 /* Inode not in memory, nothing to do */
1807 if (!ip) {
1808 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001809 continue;
1810 }
1811
Dave Chinner5b3eed72010-08-24 11:42:41 +10001812 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001813 * because this is an RCU protected lookup, we could
1814 * find a recently freed or even reallocated inode
1815 * during the lookup. We need to check under the
1816 * i_flags_lock for a valid inode here. Skip it if it
1817 * is not valid, the wrong inode or stale.
1818 */
1819 spin_lock(&ip->i_flags_lock);
1820 if (ip->i_ino != inum + i ||
1821 __xfs_iflags_test(ip, XFS_ISTALE)) {
1822 spin_unlock(&ip->i_flags_lock);
1823 rcu_read_unlock();
1824 continue;
1825 }
1826 spin_unlock(&ip->i_flags_lock);
1827
1828 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001829 * Don't try to lock/unlock the current inode, but we
1830 * _cannot_ skip the other inodes that we did not find
1831 * in the list attached to the buffer and are not
1832 * already marked stale. If we can't lock it, back off
1833 * and retry.
1834 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001835 if (ip != free_ip &&
1836 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001837 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001838 delay(1);
1839 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001840 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001841 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001842
Dave Chinner5b3eed72010-08-24 11:42:41 +10001843 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001844 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001845
Dave Chinner5b3eed72010-08-24 11:42:41 +10001846 /*
1847 * we don't need to attach clean inodes or those only
1848 * with unlogged changes (which we throw away, anyway).
1849 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001850 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001851 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001852 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 xfs_ifunlock(ip);
1854 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1855 continue;
1856 }
1857
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001858 iip->ili_last_fields = iip->ili_fields;
1859 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001861 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1862 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001864 xfs_buf_attach_iodone(bp, xfs_istale_done,
1865 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001866
1867 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
Dave Chinner5b3eed72010-08-24 11:42:41 +10001871 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 xfs_trans_binval(tp, bp);
1873 }
1874
Dave Chinner5017e972010-01-11 11:47:40 +00001875 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/*
1880 * This is called to return an inode to the inode free list.
1881 * The inode should already be truncated to 0 length and have
1882 * no pages associated with it. This routine also assumes that
1883 * the inode is already a part of the transaction.
1884 *
1885 * The on-disk copy of the inode will have been added to the list
1886 * of unlinked inodes in the AGI. We need to remove the inode from
1887 * that list atomically with respect to freeing it here.
1888 */
1889int
1890xfs_ifree(
1891 xfs_trans_t *tp,
1892 xfs_inode_t *ip,
1893 xfs_bmap_free_t *flist)
1894{
1895 int error;
1896 int delete;
1897 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001898 xfs_dinode_t *dip;
1899 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001901 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 ASSERT(ip->i_d.di_nlink == 0);
1903 ASSERT(ip->i_d.di_nextents == 0);
1904 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001905 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 ASSERT(ip->i_d.di_nblocks == 0);
1907
1908 /*
1909 * Pull the on-disk inode from the AGI unlinked list.
1910 */
1911 error = xfs_iunlink_remove(tp, ip);
1912 if (error != 0) {
1913 return error;
1914 }
1915
1916 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1917 if (error != 0) {
1918 return error;
1919 }
1920 ip->i_d.di_mode = 0; /* mark incore inode as free */
1921 ip->i_d.di_flags = 0;
1922 ip->i_d.di_dmevmask = 0;
1923 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1925 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1926 /*
1927 * Bump the generation count so no one will be confused
1928 * by reincarnations of this inode.
1929 */
1930 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1933
Christoph Hellwig475ee412012-07-03 12:21:22 -04001934 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
1935 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001936 if (error)
1937 return error;
1938
1939 /*
1940 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1941 * from picking up this inode when it is reclaimed (its incore state
1942 * initialzed but not flushed to disk yet). The in-core di_mode is
1943 * already cleared and a corresponding transaction logged.
1944 * The hack here just synchronizes the in-core to on-disk
1945 * di_mode value in advance before the actual inode sync to disk.
1946 * This is OK because the inode is already unlinked and would never
1947 * change its di_mode again for this inode generation.
1948 * This is a temporary hack that would require a proper fix
1949 * in the future.
1950 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001951 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001954 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001957 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/*
1961 * Reallocate the space for if_broot based on the number of records
1962 * being added or deleted as indicated in rec_diff. Move the records
1963 * and pointers in if_broot to fit the new size. When shrinking this
1964 * will eliminate holes between the records and pointers created by
1965 * the caller. When growing this will create holes to be filled in
1966 * by the caller.
1967 *
1968 * The caller must not request to add more records than would fit in
1969 * the on-disk inode root. If the if_broot is currently NULL, then
1970 * if we adding records one will be allocated. The caller must also
1971 * not request that the number of records go below zero, although
1972 * it can go to zero.
1973 *
1974 * ip -- the inode whose if_broot area is changing
1975 * ext_diff -- the change in the number of records, positive or negative,
1976 * requested for the if_broot array.
1977 */
1978void
1979xfs_iroot_realloc(
1980 xfs_inode_t *ip,
1981 int rec_diff,
1982 int whichfork)
1983{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001984 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 int cur_max;
1986 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001987 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 int new_max;
1989 size_t new_size;
1990 char *np;
1991 char *op;
1992
1993 /*
1994 * Handle the degenerate case quietly.
1995 */
1996 if (rec_diff == 0) {
1997 return;
1998 }
1999
2000 ifp = XFS_IFORK_PTR(ip, whichfork);
2001 if (rec_diff > 0) {
2002 /*
2003 * If there wasn't any memory allocated before, just
2004 * allocate it now and get out.
2005 */
2006 if (ifp->if_broot_bytes == 0) {
2007 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002008 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 ifp->if_broot_bytes = (int)new_size;
2010 return;
2011 }
2012
2013 /*
2014 * If there is already an existing if_broot, then we need
2015 * to realloc() it and shift the pointers to their new
2016 * location. The records don't change location because
2017 * they are kept butted up against the btree block header.
2018 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002019 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 new_max = cur_max + rec_diff;
2021 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002022 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002024 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002025 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2026 ifp->if_broot_bytes);
2027 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2028 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 ifp->if_broot_bytes = (int)new_size;
2030 ASSERT(ifp->if_broot_bytes <=
2031 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2032 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2033 return;
2034 }
2035
2036 /*
2037 * rec_diff is less than 0. In this case, we are shrinking the
2038 * if_broot buffer. It must already exist. If we go to zero
2039 * records, just get rid of the root and clear the status bit.
2040 */
2041 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002042 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 new_max = cur_max + rec_diff;
2044 ASSERT(new_max >= 0);
2045 if (new_max > 0)
2046 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2047 else
2048 new_size = 0;
2049 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002050 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
2052 * First copy over the btree block header.
2053 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002054 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 } else {
2056 new_broot = NULL;
2057 ifp->if_flags &= ~XFS_IFBROOT;
2058 }
2059
2060 /*
2061 * Only copy the records and pointers if there are any.
2062 */
2063 if (new_max > 0) {
2064 /*
2065 * First copy the records.
2066 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11002067 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2068 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2070
2071 /*
2072 * Then copy the pointers.
2073 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002074 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002076 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 (int)new_size);
2078 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2079 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002080 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 ifp->if_broot = new_broot;
2082 ifp->if_broot_bytes = (int)new_size;
2083 ASSERT(ifp->if_broot_bytes <=
2084 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2085 return;
2086}
2087
2088
2089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 * This is called when the amount of space needed for if_data
2091 * is increased or decreased. The change in size is indicated by
2092 * the number of bytes that need to be added or deleted in the
2093 * byte_diff parameter.
2094 *
2095 * If the amount of space needed has decreased below the size of the
2096 * inline buffer, then switch to using the inline buffer. Otherwise,
2097 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2098 * to what is needed.
2099 *
2100 * ip -- the inode whose if_data area is changing
2101 * byte_diff -- the change in the number of bytes, positive or negative,
2102 * requested for the if_data array.
2103 */
2104void
2105xfs_idata_realloc(
2106 xfs_inode_t *ip,
2107 int byte_diff,
2108 int whichfork)
2109{
2110 xfs_ifork_t *ifp;
2111 int new_size;
2112 int real_size;
2113
2114 if (byte_diff == 0) {
2115 return;
2116 }
2117
2118 ifp = XFS_IFORK_PTR(ip, whichfork);
2119 new_size = (int)ifp->if_bytes + byte_diff;
2120 ASSERT(new_size >= 0);
2121
2122 if (new_size == 0) {
2123 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002124 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 ifp->if_u1.if_data = NULL;
2127 real_size = 0;
2128 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2129 /*
2130 * If the valid extents/data can fit in if_inline_ext/data,
2131 * copy them from the malloc'd vector and free it.
2132 */
2133 if (ifp->if_u1.if_data == NULL) {
2134 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2135 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2136 ASSERT(ifp->if_real_bytes != 0);
2137 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2138 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002139 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2141 }
2142 real_size = 0;
2143 } else {
2144 /*
2145 * Stuck with malloc/realloc.
2146 * For inline data, the underlying buffer must be
2147 * a multiple of 4 bytes in size so that it can be
2148 * logged and stay on word boundaries. We enforce
2149 * that here.
2150 */
2151 real_size = roundup(new_size, 4);
2152 if (ifp->if_u1.if_data == NULL) {
2153 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002154 ifp->if_u1.if_data = kmem_alloc(real_size,
2155 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2157 /*
2158 * Only do the realloc if the underlying size
2159 * is really changing.
2160 */
2161 if (ifp->if_real_bytes != real_size) {
2162 ifp->if_u1.if_data =
2163 kmem_realloc(ifp->if_u1.if_data,
2164 real_size,
2165 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002166 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 } else {
2169 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002170 ifp->if_u1.if_data = kmem_alloc(real_size,
2171 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2173 ifp->if_bytes);
2174 }
2175 }
2176 ifp->if_real_bytes = real_size;
2177 ifp->if_bytes = new_size;
2178 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181void
2182xfs_idestroy_fork(
2183 xfs_inode_t *ip,
2184 int whichfork)
2185{
2186 xfs_ifork_t *ifp;
2187
2188 ifp = XFS_IFORK_PTR(ip, whichfork);
2189 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002190 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 ifp->if_broot = NULL;
2192 }
2193
2194 /*
2195 * If the format is local, then we can't have an extents
2196 * array so just look for an inline data array. If we're
2197 * not local then we may or may not have an extents list,
2198 * so check and free it up if we do.
2199 */
2200 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2201 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2202 (ifp->if_u1.if_data != NULL)) {
2203 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002204 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 ifp->if_u1.if_data = NULL;
2206 ifp->if_real_bytes = 0;
2207 }
2208 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002209 ((ifp->if_flags & XFS_IFEXTIREC) ||
2210 ((ifp->if_u1.if_extents != NULL) &&
2211 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002213 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 ASSERT(ifp->if_u1.if_extents == NULL ||
2216 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2217 ASSERT(ifp->if_real_bytes == 0);
2218 if (whichfork == XFS_ATTR_FORK) {
2219 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2220 ip->i_afp = NULL;
2221 }
2222}
2223
2224/*
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002225 * This is called to unpin an inode. The caller must have the inode locked
2226 * in at least shared mode so that the buffer cannot be subsequently pinned
2227 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002229static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002230xfs_iunpin(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002231 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002232{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002233 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002234
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002235 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2236
David Chinnera3f74ff2008-03-06 13:43:42 +11002237 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002238 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002239
David Chinnera3f74ff2008-03-06 13:43:42 +11002240}
2241
Christoph Hellwigf392e632011-12-18 20:00:10 +00002242static void
2243__xfs_iunpin_wait(
2244 struct xfs_inode *ip)
2245{
2246 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2247 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2248
2249 xfs_iunpin(ip);
2250
2251 do {
2252 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2253 if (xfs_ipincount(ip))
2254 io_schedule();
2255 } while (xfs_ipincount(ip));
2256 finish_wait(wq, &wait.wait);
2257}
2258
Dave Chinner777df5a2010-02-06 12:37:26 +11002259void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260xfs_iunpin_wait(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002261 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002263 if (xfs_ipincount(ip))
2264 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267/*
2268 * xfs_iextents_copy()
2269 *
2270 * This is called to copy the REAL extents (as opposed to the delayed
2271 * allocation extents) from the inode into the given buffer. It
2272 * returns the number of bytes copied into the buffer.
2273 *
2274 * If there are no delayed allocation extents, then we can just
2275 * memcpy() the extents into the buffer. Otherwise, we need to
2276 * examine each extent in turn and skip those which are delayed.
2277 */
2278int
2279xfs_iextents_copy(
2280 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002281 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 int whichfork)
2283{
2284 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 int i;
2286 xfs_ifork_t *ifp;
2287 int nrecs;
2288 xfs_fsblock_t start_block;
2289
2290 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002291 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 ASSERT(ifp->if_bytes > 0);
2293
2294 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002295 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 ASSERT(nrecs > 0);
2297
2298 /*
2299 * There are some delayed allocation extents in the
2300 * inode, so copy the extents one at a time and skip
2301 * the delayed ones. There must be at least one
2302 * non-delayed extent.
2303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 copied = 0;
2305 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002306 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002308 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * It's a delayed allocation extent, so skip it.
2311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 continue;
2313 }
2314
2315 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002316 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2317 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002318 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 copied++;
2320 }
2321 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002322 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2325}
2326
2327/*
2328 * Each of the following cases stores data into the same region
2329 * of the on-disk inode, so only one of them can be valid at
2330 * any given time. While it is possible to have conflicting formats
2331 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2332 * in EXTENTS format, this can only happen when the fork has
2333 * changed formats after being modified but before being flushed.
2334 * In these cases, the format always takes precedence, because the
2335 * format indicates the current state of the fork.
2336 */
2337/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002338STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339xfs_iflush_fork(
2340 xfs_inode_t *ip,
2341 xfs_dinode_t *dip,
2342 xfs_inode_log_item_t *iip,
2343 int whichfork,
2344 xfs_buf_t *bp)
2345{
2346 char *cp;
2347 xfs_ifork_t *ifp;
2348 xfs_mount_t *mp;
2349#ifdef XFS_TRANS_DEBUG
2350 int first;
2351#endif
2352 static const short brootflag[2] =
2353 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2354 static const short dataflag[2] =
2355 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2356 static const short extflag[2] =
2357 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2358
David Chinnere4ac9672008-04-10 12:23:58 +10002359 if (!iip)
2360 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 ifp = XFS_IFORK_PTR(ip, whichfork);
2362 /*
2363 * This can happen if we gave up in iformat in an error path,
2364 * for the attribute fork.
2365 */
David Chinnere4ac9672008-04-10 12:23:58 +10002366 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 cp = XFS_DFORK_PTR(dip, whichfork);
2371 mp = ip->i_mount;
2372 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2373 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002374 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 (ifp->if_bytes > 0)) {
2376 ASSERT(ifp->if_u1.if_data != NULL);
2377 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2378 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 break;
2381
2382 case XFS_DINODE_FMT_EXTENTS:
2383 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002384 !(iip->ili_fields & extflag[whichfork]));
2385 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002387 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2389 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2390 whichfork);
2391 }
2392 break;
2393
2394 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002395 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 (ifp->if_broot_bytes > 0)) {
2397 ASSERT(ifp->if_broot != NULL);
2398 ASSERT(ifp->if_broot_bytes <=
2399 (XFS_IFORK_SIZE(ip, whichfork) +
2400 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002401 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 (xfs_bmdr_block_t *)cp,
2403 XFS_DFORK_SIZE(dip, mp, whichfork));
2404 }
2405 break;
2406
2407 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002408 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002410 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 break;
2413
2414 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002415 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002417 memcpy(XFS_DFORK_DPTR(dip),
2418 &ip->i_df.if_u2.if_uuid,
2419 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421 break;
2422
2423 default:
2424 ASSERT(0);
2425 break;
2426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
David Chinnerbad55842008-03-06 13:43:49 +11002429STATIC int
2430xfs_iflush_cluster(
2431 xfs_inode_t *ip,
2432 xfs_buf_t *bp)
2433{
2434 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002435 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002436 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002437 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002438 int ilist_size;
2439 xfs_inode_t **ilist;
2440 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002441 int nr_found;
2442 int clcount = 0;
2443 int bufwasdelwri;
2444 int i;
2445
Dave Chinner5017e972010-01-11 11:47:40 +00002446 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002447
David Chinnerc8f5f122008-05-20 11:30:15 +10002448 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2449 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002450 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002451 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002452 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002453
2454 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2455 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002456 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002457 /* really need a gang lookup range call here */
2458 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002459 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002460 if (nr_found == 0)
2461 goto out_free;
2462
2463 for (i = 0; i < nr_found; i++) {
2464 iq = ilist[i];
2465 if (iq == ip)
2466 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002467
2468 /*
2469 * because this is an RCU protected lookup, we could find a
2470 * recently freed or even reallocated inode during the lookup.
2471 * We need to check under the i_flags_lock for a valid inode
2472 * here. Skip it if it is not valid or the wrong inode.
2473 */
2474 spin_lock(&ip->i_flags_lock);
2475 if (!ip->i_ino ||
2476 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2477 spin_unlock(&ip->i_flags_lock);
2478 continue;
2479 }
2480 spin_unlock(&ip->i_flags_lock);
2481
David Chinnerbad55842008-03-06 13:43:49 +11002482 /*
2483 * Do an un-protected check to see if the inode is dirty and
2484 * is a candidate for flushing. These checks will be repeated
2485 * later after the appropriate locks are acquired.
2486 */
David Chinner33540402008-03-06 13:43:59 +11002487 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002488 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002489
2490 /*
2491 * Try to get locks. If any are unavailable or it is pinned,
2492 * then this inode cannot be flushed and is skipped.
2493 */
2494
2495 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2496 continue;
2497 if (!xfs_iflock_nowait(iq)) {
2498 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2499 continue;
2500 }
2501 if (xfs_ipincount(iq)) {
2502 xfs_ifunlock(iq);
2503 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2504 continue;
2505 }
2506
2507 /*
2508 * arriving here means that this inode can be flushed. First
2509 * re-check that it's dirty before flushing.
2510 */
David Chinner33540402008-03-06 13:43:59 +11002511 if (!xfs_inode_clean(iq)) {
2512 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002513 error = xfs_iflush_int(iq, bp);
2514 if (error) {
2515 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2516 goto cluster_corrupt_out;
2517 }
2518 clcount++;
2519 } else {
2520 xfs_ifunlock(iq);
2521 }
2522 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2523 }
2524
2525 if (clcount) {
2526 XFS_STATS_INC(xs_icluster_flushcnt);
2527 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2528 }
2529
2530out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002531 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002532 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002533out_put:
2534 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002535 return 0;
2536
2537
2538cluster_corrupt_out:
2539 /*
2540 * Corruption detected in the clustering loop. Invalidate the
2541 * inode buffer and shut down the filesystem.
2542 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002543 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002544 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002545 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002546 * brelse can handle it with no problems. If not, shut down the
2547 * filesystem before releasing the buffer.
2548 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002549 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002550 if (bufwasdelwri)
2551 xfs_buf_relse(bp);
2552
2553 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2554
2555 if (!bufwasdelwri) {
2556 /*
2557 * Just like incore_relse: if we have b_iodone functions,
2558 * mark the buffer as an error and call them. Otherwise
2559 * mark it as stale and brelse.
2560 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002561 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002562 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002563 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002564 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002565 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002566 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002567 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002568 xfs_buf_relse(bp);
2569 }
2570 }
2571
2572 /*
2573 * Unlocks the flush lock
2574 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002575 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002576 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002577 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002578 return XFS_ERROR(EFSCORRUPTED);
2579}
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002582 * Flush dirty inode metadata into the backing buffer.
2583 *
2584 * The caller must have the inode lock and the inode flush lock held. The
2585 * inode lock will still be held upon return to the caller, and the inode
2586 * flush lock will be released after the inode has reached the disk.
2587 *
2588 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
2590int
2591xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002592 struct xfs_inode *ip,
2593 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002595 struct xfs_mount *mp = ip->i_mount;
2596 struct xfs_buf *bp;
2597 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 XFS_STATS_INC(xs_iflush_count);
2601
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002602 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002603 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002605 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Christoph Hellwig4c468192012-04-23 15:58:36 +10002607 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 xfs_iunpin_wait(ip);
2610
2611 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002612 * For stale inodes we cannot rely on the backing buffer remaining
2613 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002614 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002615 * inodes below. We have to check this after ensuring the inode is
2616 * unpinned so that it is safe to reclaim the stale inode after the
2617 * flush call.
2618 */
2619 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2620 xfs_ifunlock(ip);
2621 return 0;
2622 }
2623
2624 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * This may have been unpinned because the filesystem is shutting
2626 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002627 * to disk, because the log record didn't make it to disk.
2628 *
2629 * We also have to remove the log item from the AIL in this case,
2630 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 */
2632 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002633 error = XFS_ERROR(EIO);
2634 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636
2637 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002638 * Get the buffer containing the on-disk inode.
2639 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002640 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2641 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002642 if (error || !bp) {
2643 xfs_ifunlock(ip);
2644 return error;
2645 }
2646
2647 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 * First flush out the inode that xfs_iflush was called with.
2649 */
2650 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002651 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002655 * If the buffer is pinned then push on the log now so we won't
2656 * get stuck waiting in the write for too long.
2657 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002658 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002659 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002660
2661 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * inode clustering:
2663 * see if other inodes can be gathered into this write
2664 */
David Chinnerbad55842008-03-06 13:43:49 +11002665 error = xfs_iflush_cluster(ip, bp);
2666 if (error)
2667 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Christoph Hellwig4c468192012-04-23 15:58:36 +10002669 *bpp = bp;
2670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672corrupt_out:
2673 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002674 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002676 error = XFS_ERROR(EFSCORRUPTED);
2677abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 /*
2679 * Unlocks the flush lock
2680 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002681 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
2685
2686STATIC int
2687xfs_iflush_int(
2688 xfs_inode_t *ip,
2689 xfs_buf_t *bp)
2690{
2691 xfs_inode_log_item_t *iip;
2692 xfs_dinode_t *dip;
2693 xfs_mount_t *mp;
2694#ifdef XFS_TRANS_DEBUG
2695 int first;
2696#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002698 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002699 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002701 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 iip = ip->i_itemp;
2704 mp = ip->i_mount;
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002707 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002709 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002711 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2712 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2713 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto corrupt_out;
2715 }
2716 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2717 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002718 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2719 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2720 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 goto corrupt_out;
2722 }
Al Viroabbede12011-07-26 02:31:30 -04002723 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (XFS_TEST_ERROR(
2725 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2726 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2727 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002728 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2729 "%s: Bad regular inode %Lu, ptr 0x%p",
2730 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 goto corrupt_out;
2732 }
Al Viroabbede12011-07-26 02:31:30 -04002733 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (XFS_TEST_ERROR(
2735 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2736 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2737 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2738 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002739 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2740 "%s: Bad directory inode %Lu, ptr 0x%p",
2741 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 goto corrupt_out;
2743 }
2744 }
2745 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2746 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2747 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002748 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2749 "%s: detected corrupt incore inode %Lu, "
2750 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2751 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002753 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 goto corrupt_out;
2755 }
2756 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2757 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002758 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2759 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2760 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 goto corrupt_out;
2762 }
2763 /*
2764 * bump the flush iteration count, used to detect flushes which
2765 * postdate a log record during recovery.
2766 */
2767
2768 ip->i_d.di_flushiter++;
2769
2770 /*
2771 * Copy the dirty parts of the inode into the on-disk
2772 * inode. We always copy out the core of the inode,
2773 * because if the inode is dirty at all the core must
2774 * be.
2775 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002776 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2779 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2780 ip->i_d.di_flushiter = 0;
2781
2782 /*
2783 * If this is really an old format inode and the superblock version
2784 * has not been updated to support only new format inodes, then
2785 * convert back to the old inode format. If the superblock version
2786 * has been updated, then make the conversion permanent.
2787 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002788 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2789 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002790 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /*
2792 * Convert it back.
2793 */
2794 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002795 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 } else {
2797 /*
2798 * The superblock version has already been bumped,
2799 * so just make the conversion to the new inode
2800 * format permanent.
2801 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002802 ip->i_d.di_version = 2;
2803 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002805 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002807 memset(&(dip->di_pad[0]), 0,
2808 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002809 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811 }
2812
David Chinnere4ac9672008-04-10 12:23:58 +10002813 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2814 if (XFS_IFORK_Q(ip))
2815 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 xfs_inobp_check(mp, bp);
2817
2818 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002819 * We've recorded everything logged in the inode, so we'd like to clear
2820 * the ili_fields bits so we don't log and flush things unnecessarily.
2821 * However, we can't stop logging all this information until the data
2822 * we've copied into the disk buffer is written to disk. If we did we
2823 * might overwrite the copy of the inode in the log with all the data
2824 * after re-logging only part of it, and in the face of a crash we
2825 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002827 * What we do is move the bits to the ili_last_fields field. When
2828 * logging the inode, these bits are moved back to the ili_fields field.
2829 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2830 * know that the information those bits represent is permanently on
2831 * disk. As long as the flush completes before the inode is logged
2832 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002834 * We can play with the ili_fields bits here, because the inode lock
2835 * must be held exclusively in order to set bits there and the flush
2836 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2837 * done routine can tell whether or not to look in the AIL. Also, store
2838 * the current LSN of the inode so that we can tell whether the item has
2839 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2840 * need the AIL lock, because it is a 64 bit value that cannot be read
2841 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002843 if (iip != NULL && iip->ili_fields != 0) {
2844 iip->ili_last_fields = iip->ili_fields;
2845 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 iip->ili_logged = 1;
2847
David Chinner7b2e2a32008-10-30 17:39:12 +11002848 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2849 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /*
2852 * Attach the function xfs_iflush_done to the inode's
2853 * buffer. This will remove the inode from the AIL
2854 * and unlock the inode's flush lock when the inode is
2855 * completely written to disk.
2856 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002857 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002859 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002860 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 } else {
2862 /*
2863 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002864 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 * the inode flush lock because we can avoid the whole
2866 * AIL state thing. It's OK to drop the flush lock now,
2867 * because we've already locked the buffer and to do anything
2868 * you really need both.
2869 */
2870 if (iip != NULL) {
2871 ASSERT(iip->ili_logged == 0);
2872 ASSERT(iip->ili_last_fields == 0);
2873 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2874 }
2875 xfs_ifunlock(ip);
2876 }
2877
2878 return 0;
2879
2880corrupt_out:
2881 return XFS_ERROR(EFSCORRUPTED);
2882}
2883
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002884/*
2885 * Return a pointer to the extent record at file index idx.
2886 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002887xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002888xfs_iext_get_ext(
2889 xfs_ifork_t *ifp, /* inode fork pointer */
2890 xfs_extnum_t idx) /* index of target extent */
2891{
2892 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002893 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2894
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002895 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2896 return ifp->if_u1.if_ext_irec->er_extbuf;
2897 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2898 xfs_ext_irec_t *erp; /* irec pointer */
2899 int erp_idx = 0; /* irec index */
2900 xfs_extnum_t page_idx = idx; /* ext index in target list */
2901
2902 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2903 return &erp->er_extbuf[page_idx];
2904 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002905 return &ifp->if_u1.if_extents[idx];
2906 } else {
2907 return NULL;
2908 }
2909}
2910
2911/*
2912 * Insert new item(s) into the extent records for incore inode
2913 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2914 */
2915void
2916xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002917 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002918 xfs_extnum_t idx, /* starting index of new items */
2919 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002920 xfs_bmbt_irec_t *new, /* items to insert */
2921 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002922{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002923 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002924 xfs_extnum_t i; /* extent record index */
2925
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002926 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2927
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002928 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2929 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002930 for (i = idx; i < idx + count; i++, new++)
2931 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002932}
2933
2934/*
2935 * This is called when the amount of space required for incore file
2936 * extents needs to be increased. The ext_diff parameter stores the
2937 * number of new extents being added and the idx parameter contains
2938 * the extent index where the new extents will be added. If the new
2939 * extents are being appended, then we just need to (re)allocate and
2940 * initialize the space. Otherwise, if the new extents are being
2941 * inserted into the middle of the existing entries, a bit more work
2942 * is required to make room for the new extents to be inserted. The
2943 * caller is responsible for filling in the new extent entries upon
2944 * return.
2945 */
2946void
2947xfs_iext_add(
2948 xfs_ifork_t *ifp, /* inode fork pointer */
2949 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002950 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002951{
2952 int byte_diff; /* new bytes being added */
2953 int new_size; /* size of extents after adding */
2954 xfs_extnum_t nextents; /* number of extents in file */
2955
2956 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2957 ASSERT((idx >= 0) && (idx <= nextents));
2958 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2959 new_size = ifp->if_bytes + byte_diff;
2960 /*
2961 * If the new number of extents (nextents + ext_diff)
2962 * fits inside the inode, then continue to use the inline
2963 * extent buffer.
2964 */
2965 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2966 if (idx < nextents) {
2967 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2968 &ifp->if_u2.if_inline_ext[idx],
2969 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2970 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2971 }
2972 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2973 ifp->if_real_bytes = 0;
2974 }
2975 /*
2976 * Otherwise use a linear (direct) extent list.
2977 * If the extents are currently inside the inode,
2978 * xfs_iext_realloc_direct will switch us from
2979 * inline to direct extent allocation mode.
2980 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002981 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002982 xfs_iext_realloc_direct(ifp, new_size);
2983 if (idx < nextents) {
2984 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2985 &ifp->if_u1.if_extents[idx],
2986 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2987 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2988 }
2989 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002990 /* Indirection array */
2991 else {
2992 xfs_ext_irec_t *erp;
2993 int erp_idx = 0;
2994 int page_idx = idx;
2995
2996 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2997 if (ifp->if_flags & XFS_IFEXTIREC) {
2998 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2999 } else {
3000 xfs_iext_irec_init(ifp);
3001 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3002 erp = ifp->if_u1.if_ext_irec;
3003 }
3004 /* Extents fit in target extent page */
3005 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3006 if (page_idx < erp->er_extcount) {
3007 memmove(&erp->er_extbuf[page_idx + ext_diff],
3008 &erp->er_extbuf[page_idx],
3009 (erp->er_extcount - page_idx) *
3010 sizeof(xfs_bmbt_rec_t));
3011 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3012 }
3013 erp->er_extcount += ext_diff;
3014 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3015 }
3016 /* Insert a new extent page */
3017 else if (erp) {
3018 xfs_iext_add_indirect_multi(ifp,
3019 erp_idx, page_idx, ext_diff);
3020 }
3021 /*
3022 * If extent(s) are being appended to the last page in
3023 * the indirection array and the new extent(s) don't fit
3024 * in the page, then erp is NULL and erp_idx is set to
3025 * the next index needed in the indirection array.
3026 */
3027 else {
3028 int count = ext_diff;
3029
3030 while (count) {
3031 erp = xfs_iext_irec_new(ifp, erp_idx);
3032 erp->er_extcount = count;
3033 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3034 if (count) {
3035 erp_idx++;
3036 }
3037 }
3038 }
3039 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003040 ifp->if_bytes = new_size;
3041}
3042
3043/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003044 * This is called when incore extents are being added to the indirection
3045 * array and the new extents do not fit in the target extent list. The
3046 * erp_idx parameter contains the irec index for the target extent list
3047 * in the indirection array, and the idx parameter contains the extent
3048 * index within the list. The number of extents being added is stored
3049 * in the count parameter.
3050 *
3051 * |-------| |-------|
3052 * | | | | idx - number of extents before idx
3053 * | idx | | count |
3054 * | | | | count - number of extents being inserted at idx
3055 * |-------| |-------|
3056 * | count | | nex2 | nex2 - number of extents after idx + count
3057 * |-------| |-------|
3058 */
3059void
3060xfs_iext_add_indirect_multi(
3061 xfs_ifork_t *ifp, /* inode fork pointer */
3062 int erp_idx, /* target extent irec index */
3063 xfs_extnum_t idx, /* index within target list */
3064 int count) /* new extents being added */
3065{
3066 int byte_diff; /* new bytes being added */
3067 xfs_ext_irec_t *erp; /* pointer to irec entry */
3068 xfs_extnum_t ext_diff; /* number of extents to add */
3069 xfs_extnum_t ext_cnt; /* new extents still needed */
3070 xfs_extnum_t nex2; /* extents after idx + count */
3071 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3072 int nlists; /* number of irec's (lists) */
3073
3074 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3075 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3076 nex2 = erp->er_extcount - idx;
3077 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3078
3079 /*
3080 * Save second part of target extent list
3081 * (all extents past */
3082 if (nex2) {
3083 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10003084 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003085 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3086 erp->er_extcount -= nex2;
3087 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3088 memset(&erp->er_extbuf[idx], 0, byte_diff);
3089 }
3090
3091 /*
3092 * Add the new extents to the end of the target
3093 * list, then allocate new irec record(s) and
3094 * extent buffer(s) as needed to store the rest
3095 * of the new extents.
3096 */
3097 ext_cnt = count;
3098 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3099 if (ext_diff) {
3100 erp->er_extcount += ext_diff;
3101 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3102 ext_cnt -= ext_diff;
3103 }
3104 while (ext_cnt) {
3105 erp_idx++;
3106 erp = xfs_iext_irec_new(ifp, erp_idx);
3107 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3108 erp->er_extcount = ext_diff;
3109 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3110 ext_cnt -= ext_diff;
3111 }
3112
3113 /* Add nex2 extents back to indirection array */
3114 if (nex2) {
3115 xfs_extnum_t ext_avail;
3116 int i;
3117
3118 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3119 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3120 i = 0;
3121 /*
3122 * If nex2 extents fit in the current page, append
3123 * nex2_ep after the new extents.
3124 */
3125 if (nex2 <= ext_avail) {
3126 i = erp->er_extcount;
3127 }
3128 /*
3129 * Otherwise, check if space is available in the
3130 * next page.
3131 */
3132 else if ((erp_idx < nlists - 1) &&
3133 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3134 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3135 erp_idx++;
3136 erp++;
3137 /* Create a hole for nex2 extents */
3138 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3139 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3140 }
3141 /*
3142 * Final choice, create a new extent page for
3143 * nex2 extents.
3144 */
3145 else {
3146 erp_idx++;
3147 erp = xfs_iext_irec_new(ifp, erp_idx);
3148 }
3149 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003150 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003151 erp->er_extcount += nex2;
3152 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3153 }
3154}
3155
3156/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003157 * This is called when the amount of space required for incore file
3158 * extents needs to be decreased. The ext_diff parameter stores the
3159 * number of extents to be removed and the idx parameter contains
3160 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003161 *
3162 * If the amount of space needed has decreased below the linear
3163 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3164 * extent array. Otherwise, use kmem_realloc() to adjust the
3165 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003166 */
3167void
3168xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003169 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003170 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003171 int ext_diff, /* number of extents to remove */
3172 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003173{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003174 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003175 xfs_extnum_t nextents; /* number of extents in file */
3176 int new_size; /* size of extents after removal */
3177
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003178 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3179
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003180 ASSERT(ext_diff > 0);
3181 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3182 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3183
3184 if (new_size == 0) {
3185 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003186 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3187 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003188 } else if (ifp->if_real_bytes) {
3189 xfs_iext_remove_direct(ifp, idx, ext_diff);
3190 } else {
3191 xfs_iext_remove_inline(ifp, idx, ext_diff);
3192 }
3193 ifp->if_bytes = new_size;
3194}
3195
3196/*
3197 * This removes ext_diff extents from the inline buffer, beginning
3198 * at extent index idx.
3199 */
3200void
3201xfs_iext_remove_inline(
3202 xfs_ifork_t *ifp, /* inode fork pointer */
3203 xfs_extnum_t idx, /* index to begin removing exts */
3204 int ext_diff) /* number of extents to remove */
3205{
3206 int nextents; /* number of extents in file */
3207
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003208 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003209 ASSERT(idx < XFS_INLINE_EXTS);
3210 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3211 ASSERT(((nextents - ext_diff) > 0) &&
3212 (nextents - ext_diff) < XFS_INLINE_EXTS);
3213
3214 if (idx + ext_diff < nextents) {
3215 memmove(&ifp->if_u2.if_inline_ext[idx],
3216 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3217 (nextents - (idx + ext_diff)) *
3218 sizeof(xfs_bmbt_rec_t));
3219 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3220 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3221 } else {
3222 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3223 ext_diff * sizeof(xfs_bmbt_rec_t));
3224 }
3225}
3226
3227/*
3228 * This removes ext_diff extents from a linear (direct) extent list,
3229 * beginning at extent index idx. If the extents are being removed
3230 * from the end of the list (ie. truncate) then we just need to re-
3231 * allocate the list to remove the extra space. Otherwise, if the
3232 * extents are being removed from the middle of the existing extent
3233 * entries, then we first need to move the extent records beginning
3234 * at idx + ext_diff up in the list to overwrite the records being
3235 * removed, then remove the extra space via kmem_realloc.
3236 */
3237void
3238xfs_iext_remove_direct(
3239 xfs_ifork_t *ifp, /* inode fork pointer */
3240 xfs_extnum_t idx, /* index to begin removing exts */
3241 int ext_diff) /* number of extents to remove */
3242{
3243 xfs_extnum_t nextents; /* number of extents in file */
3244 int new_size; /* size of extents after removal */
3245
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003246 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003247 new_size = ifp->if_bytes -
3248 (ext_diff * sizeof(xfs_bmbt_rec_t));
3249 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3250
3251 if (new_size == 0) {
3252 xfs_iext_destroy(ifp);
3253 return;
3254 }
3255 /* Move extents up in the list (if needed) */
3256 if (idx + ext_diff < nextents) {
3257 memmove(&ifp->if_u1.if_extents[idx],
3258 &ifp->if_u1.if_extents[idx + ext_diff],
3259 (nextents - (idx + ext_diff)) *
3260 sizeof(xfs_bmbt_rec_t));
3261 }
3262 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3263 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3264 /*
3265 * Reallocate the direct extent list. If the extents
3266 * will fit inside the inode then xfs_iext_realloc_direct
3267 * will switch from direct to inline extent allocation
3268 * mode for us.
3269 */
3270 xfs_iext_realloc_direct(ifp, new_size);
3271 ifp->if_bytes = new_size;
3272}
3273
3274/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003275 * This is called when incore extents are being removed from the
3276 * indirection array and the extents being removed span multiple extent
3277 * buffers. The idx parameter contains the file extent index where we
3278 * want to begin removing extents, and the count parameter contains
3279 * how many extents need to be removed.
3280 *
3281 * |-------| |-------|
3282 * | nex1 | | | nex1 - number of extents before idx
3283 * |-------| | count |
3284 * | | | | count - number of extents being removed at idx
3285 * | count | |-------|
3286 * | | | nex2 | nex2 - number of extents after idx + count
3287 * |-------| |-------|
3288 */
3289void
3290xfs_iext_remove_indirect(
3291 xfs_ifork_t *ifp, /* inode fork pointer */
3292 xfs_extnum_t idx, /* index to begin removing extents */
3293 int count) /* number of extents to remove */
3294{
3295 xfs_ext_irec_t *erp; /* indirection array pointer */
3296 int erp_idx = 0; /* indirection array index */
3297 xfs_extnum_t ext_cnt; /* extents left to remove */
3298 xfs_extnum_t ext_diff; /* extents to remove in current list */
3299 xfs_extnum_t nex1; /* number of extents before idx */
3300 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003301 int page_idx = idx; /* index in target extent list */
3302
3303 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3304 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3305 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003306 nex1 = page_idx;
3307 ext_cnt = count;
3308 while (ext_cnt) {
3309 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3310 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3311 /*
3312 * Check for deletion of entire list;
3313 * xfs_iext_irec_remove() updates extent offsets.
3314 */
3315 if (ext_diff == erp->er_extcount) {
3316 xfs_iext_irec_remove(ifp, erp_idx);
3317 ext_cnt -= ext_diff;
3318 nex1 = 0;
3319 if (ext_cnt) {
3320 ASSERT(erp_idx < ifp->if_real_bytes /
3321 XFS_IEXT_BUFSZ);
3322 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3323 nex1 = 0;
3324 continue;
3325 } else {
3326 break;
3327 }
3328 }
3329 /* Move extents up (if needed) */
3330 if (nex2) {
3331 memmove(&erp->er_extbuf[nex1],
3332 &erp->er_extbuf[nex1 + ext_diff],
3333 nex2 * sizeof(xfs_bmbt_rec_t));
3334 }
3335 /* Zero out rest of page */
3336 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3337 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3338 /* Update remaining counters */
3339 erp->er_extcount -= ext_diff;
3340 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3341 ext_cnt -= ext_diff;
3342 nex1 = 0;
3343 erp_idx++;
3344 erp++;
3345 }
3346 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3347 xfs_iext_irec_compact(ifp);
3348}
3349
3350/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003351 * Create, destroy, or resize a linear (direct) block of extents.
3352 */
3353void
3354xfs_iext_realloc_direct(
3355 xfs_ifork_t *ifp, /* inode fork pointer */
3356 int new_size) /* new size of extents */
3357{
3358 int rnew_size; /* real new size of extents */
3359
3360 rnew_size = new_size;
3361
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003362 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3363 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3364 (new_size != ifp->if_real_bytes)));
3365
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003366 /* Free extent records */
3367 if (new_size == 0) {
3368 xfs_iext_destroy(ifp);
3369 }
3370 /* Resize direct extent list and zero any new bytes */
3371 else if (ifp->if_real_bytes) {
3372 /* Check if extents will fit inside the inode */
3373 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3374 xfs_iext_direct_to_inline(ifp, new_size /
3375 (uint)sizeof(xfs_bmbt_rec_t));
3376 ifp->if_bytes = new_size;
3377 return;
3378 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003379 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003380 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003381 }
3382 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003383 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003384 kmem_realloc(ifp->if_u1.if_extents,
3385 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003386 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003387 }
3388 if (rnew_size > ifp->if_real_bytes) {
3389 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3390 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3391 rnew_size - ifp->if_real_bytes);
3392 }
3393 }
3394 /*
3395 * Switch from the inline extent buffer to a direct
3396 * extent list. Be sure to include the inline extent
3397 * bytes in new_size.
3398 */
3399 else {
3400 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003401 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003402 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003403 }
3404 xfs_iext_inline_to_direct(ifp, rnew_size);
3405 }
3406 ifp->if_real_bytes = rnew_size;
3407 ifp->if_bytes = new_size;
3408}
3409
3410/*
3411 * Switch from linear (direct) extent records to inline buffer.
3412 */
3413void
3414xfs_iext_direct_to_inline(
3415 xfs_ifork_t *ifp, /* inode fork pointer */
3416 xfs_extnum_t nextents) /* number of extents in file */
3417{
3418 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3419 ASSERT(nextents <= XFS_INLINE_EXTS);
3420 /*
3421 * The inline buffer was zeroed when we switched
3422 * from inline to direct extent allocation mode,
3423 * so we don't need to clear it here.
3424 */
3425 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3426 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003427 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003428 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3429 ifp->if_real_bytes = 0;
3430}
3431
3432/*
3433 * Switch from inline buffer to linear (direct) extent records.
3434 * new_size should already be rounded up to the next power of 2
3435 * by the caller (when appropriate), so use new_size as it is.
3436 * However, since new_size may be rounded up, we can't update
3437 * if_bytes here. It is the caller's responsibility to update
3438 * if_bytes upon return.
3439 */
3440void
3441xfs_iext_inline_to_direct(
3442 xfs_ifork_t *ifp, /* inode fork pointer */
3443 int new_size) /* number of extents in file */
3444{
David Chinner67850732008-08-13 16:02:51 +10003445 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003446 memset(ifp->if_u1.if_extents, 0, new_size);
3447 if (ifp->if_bytes) {
3448 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3449 ifp->if_bytes);
3450 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3451 sizeof(xfs_bmbt_rec_t));
3452 }
3453 ifp->if_real_bytes = new_size;
3454}
3455
3456/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003457 * Resize an extent indirection array to new_size bytes.
3458 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003459STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003460xfs_iext_realloc_indirect(
3461 xfs_ifork_t *ifp, /* inode fork pointer */
3462 int new_size) /* new indirection array size */
3463{
3464 int nlists; /* number of irec's (ex lists) */
3465 int size; /* current indirection array size */
3466
3467 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3468 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3469 size = nlists * sizeof(xfs_ext_irec_t);
3470 ASSERT(ifp->if_real_bytes);
3471 ASSERT((new_size >= 0) && (new_size != size));
3472 if (new_size == 0) {
3473 xfs_iext_destroy(ifp);
3474 } else {
3475 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3476 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003477 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003478 }
3479}
3480
3481/*
3482 * Switch from indirection array to linear (direct) extent allocations.
3483 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003484STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003485xfs_iext_indirect_to_direct(
3486 xfs_ifork_t *ifp) /* inode fork pointer */
3487{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003488 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003489 xfs_extnum_t nextents; /* number of extents in file */
3490 int size; /* size of file extents */
3491
3492 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3493 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3494 ASSERT(nextents <= XFS_LINEAR_EXTS);
3495 size = nextents * sizeof(xfs_bmbt_rec_t);
3496
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003497 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003498 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3499
3500 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003501 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003502 ifp->if_flags &= ~XFS_IFEXTIREC;
3503 ifp->if_u1.if_extents = ep;
3504 ifp->if_bytes = size;
3505 if (nextents < XFS_LINEAR_EXTS) {
3506 xfs_iext_realloc_direct(ifp, size);
3507 }
3508}
3509
3510/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003511 * Free incore file extents.
3512 */
3513void
3514xfs_iext_destroy(
3515 xfs_ifork_t *ifp) /* inode fork pointer */
3516{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003517 if (ifp->if_flags & XFS_IFEXTIREC) {
3518 int erp_idx;
3519 int nlists;
3520
3521 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3522 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3523 xfs_iext_irec_remove(ifp, erp_idx);
3524 }
3525 ifp->if_flags &= ~XFS_IFEXTIREC;
3526 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003527 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003528 } else if (ifp->if_bytes) {
3529 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3530 sizeof(xfs_bmbt_rec_t));
3531 }
3532 ifp->if_u1.if_extents = NULL;
3533 ifp->if_real_bytes = 0;
3534 ifp->if_bytes = 0;
3535}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003536
3537/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003538 * Return a pointer to the extent record for file system block bno.
3539 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003540xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003541xfs_iext_bno_to_ext(
3542 xfs_ifork_t *ifp, /* inode fork pointer */
3543 xfs_fileoff_t bno, /* block number to search for */
3544 xfs_extnum_t *idxp) /* index of target extent */
3545{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003546 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003547 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003548 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003549 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003550 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003551 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003552 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003553 xfs_extnum_t nextents; /* number of file extents */
3554 xfs_fileoff_t startoff = 0; /* start offset of extent */
3555
3556 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3557 if (nextents == 0) {
3558 *idxp = 0;
3559 return NULL;
3560 }
3561 low = 0;
3562 if (ifp->if_flags & XFS_IFEXTIREC) {
3563 /* Find target extent list */
3564 int erp_idx = 0;
3565 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3566 base = erp->er_extbuf;
3567 high = erp->er_extcount - 1;
3568 } else {
3569 base = ifp->if_u1.if_extents;
3570 high = nextents - 1;
3571 }
3572 /* Binary search extent records */
3573 while (low <= high) {
3574 idx = (low + high) >> 1;
3575 ep = base + idx;
3576 startoff = xfs_bmbt_get_startoff(ep);
3577 blockcount = xfs_bmbt_get_blockcount(ep);
3578 if (bno < startoff) {
3579 high = idx - 1;
3580 } else if (bno >= startoff + blockcount) {
3581 low = idx + 1;
3582 } else {
3583 /* Convert back to file-based extent index */
3584 if (ifp->if_flags & XFS_IFEXTIREC) {
3585 idx += erp->er_extoff;
3586 }
3587 *idxp = idx;
3588 return ep;
3589 }
3590 }
3591 /* Convert back to file-based extent index */
3592 if (ifp->if_flags & XFS_IFEXTIREC) {
3593 idx += erp->er_extoff;
3594 }
3595 if (bno >= startoff + blockcount) {
3596 if (++idx == nextents) {
3597 ep = NULL;
3598 } else {
3599 ep = xfs_iext_get_ext(ifp, idx);
3600 }
3601 }
3602 *idxp = idx;
3603 return ep;
3604}
3605
3606/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003607 * Return a pointer to the indirection array entry containing the
3608 * extent record for filesystem block bno. Store the index of the
3609 * target irec in *erp_idxp.
3610 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003611xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003612xfs_iext_bno_to_irec(
3613 xfs_ifork_t *ifp, /* inode fork pointer */
3614 xfs_fileoff_t bno, /* block number to search for */
3615 int *erp_idxp) /* irec index of target ext list */
3616{
3617 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3618 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003619 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003620 int nlists; /* number of extent irec's (lists) */
3621 int high; /* binary search upper limit */
3622 int low; /* binary search lower limit */
3623
3624 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3625 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3626 erp_idx = 0;
3627 low = 0;
3628 high = nlists - 1;
3629 while (low <= high) {
3630 erp_idx = (low + high) >> 1;
3631 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3632 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3633 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3634 high = erp_idx - 1;
3635 } else if (erp_next && bno >=
3636 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3637 low = erp_idx + 1;
3638 } else {
3639 break;
3640 }
3641 }
3642 *erp_idxp = erp_idx;
3643 return erp;
3644}
3645
3646/*
3647 * Return a pointer to the indirection array entry containing the
3648 * extent record at file extent index *idxp. Store the index of the
3649 * target irec in *erp_idxp and store the page index of the target
3650 * extent record in *idxp.
3651 */
3652xfs_ext_irec_t *
3653xfs_iext_idx_to_irec(
3654 xfs_ifork_t *ifp, /* inode fork pointer */
3655 xfs_extnum_t *idxp, /* extent index (file -> page) */
3656 int *erp_idxp, /* pointer to target irec */
3657 int realloc) /* new bytes were just added */
3658{
3659 xfs_ext_irec_t *prev; /* pointer to previous irec */
3660 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3661 int erp_idx; /* indirection array index */
3662 int nlists; /* number of irec's (ex lists) */
3663 int high; /* binary search upper limit */
3664 int low; /* binary search lower limit */
3665 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3666
3667 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003668 ASSERT(page_idx >= 0);
3669 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3670 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3671
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003672 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3673 erp_idx = 0;
3674 low = 0;
3675 high = nlists - 1;
3676
3677 /* Binary search extent irec's */
3678 while (low <= high) {
3679 erp_idx = (low + high) >> 1;
3680 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3681 prev = erp_idx > 0 ? erp - 1 : NULL;
3682 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3683 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3684 high = erp_idx - 1;
3685 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3686 (page_idx == erp->er_extoff + erp->er_extcount &&
3687 !realloc)) {
3688 low = erp_idx + 1;
3689 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3690 erp->er_extcount == XFS_LINEAR_EXTS) {
3691 ASSERT(realloc);
3692 page_idx = 0;
3693 erp_idx++;
3694 erp = erp_idx < nlists ? erp + 1 : NULL;
3695 break;
3696 } else {
3697 page_idx -= erp->er_extoff;
3698 break;
3699 }
3700 }
3701 *idxp = page_idx;
3702 *erp_idxp = erp_idx;
3703 return(erp);
3704}
3705
3706/*
3707 * Allocate and initialize an indirection array once the space needed
3708 * for incore extents increases above XFS_IEXT_BUFSZ.
3709 */
3710void
3711xfs_iext_irec_init(
3712 xfs_ifork_t *ifp) /* inode fork pointer */
3713{
3714 xfs_ext_irec_t *erp; /* indirection array pointer */
3715 xfs_extnum_t nextents; /* number of extents in file */
3716
3717 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3718 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3719 ASSERT(nextents <= XFS_LINEAR_EXTS);
3720
David Chinner67850732008-08-13 16:02:51 +10003721 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003722
3723 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003724 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003725 } else if (!ifp->if_real_bytes) {
3726 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3727 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3728 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3729 }
3730 erp->er_extbuf = ifp->if_u1.if_extents;
3731 erp->er_extcount = nextents;
3732 erp->er_extoff = 0;
3733
3734 ifp->if_flags |= XFS_IFEXTIREC;
3735 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3736 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3737 ifp->if_u1.if_ext_irec = erp;
3738
3739 return;
3740}
3741
3742/*
3743 * Allocate and initialize a new entry in the indirection array.
3744 */
3745xfs_ext_irec_t *
3746xfs_iext_irec_new(
3747 xfs_ifork_t *ifp, /* inode fork pointer */
3748 int erp_idx) /* index for new irec */
3749{
3750 xfs_ext_irec_t *erp; /* indirection array pointer */
3751 int i; /* loop counter */
3752 int nlists; /* number of irec's (ex lists) */
3753
3754 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3755 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3756
3757 /* Resize indirection array */
3758 xfs_iext_realloc_indirect(ifp, ++nlists *
3759 sizeof(xfs_ext_irec_t));
3760 /*
3761 * Move records down in the array so the
3762 * new page can use erp_idx.
3763 */
3764 erp = ifp->if_u1.if_ext_irec;
3765 for (i = nlists - 1; i > erp_idx; i--) {
3766 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3767 }
3768 ASSERT(i == erp_idx);
3769
3770 /* Initialize new extent record */
3771 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003772 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003773 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3774 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3775 erp[erp_idx].er_extcount = 0;
3776 erp[erp_idx].er_extoff = erp_idx > 0 ?
3777 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3778 return (&erp[erp_idx]);
3779}
3780
3781/*
3782 * Remove a record from the indirection array.
3783 */
3784void
3785xfs_iext_irec_remove(
3786 xfs_ifork_t *ifp, /* inode fork pointer */
3787 int erp_idx) /* irec index to remove */
3788{
3789 xfs_ext_irec_t *erp; /* indirection array pointer */
3790 int i; /* loop counter */
3791 int nlists; /* number of irec's (ex lists) */
3792
3793 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3794 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3795 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3796 if (erp->er_extbuf) {
3797 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3798 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003799 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003800 }
3801 /* Compact extent records */
3802 erp = ifp->if_u1.if_ext_irec;
3803 for (i = erp_idx; i < nlists - 1; i++) {
3804 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3805 }
3806 /*
3807 * Manually free the last extent record from the indirection
3808 * array. A call to xfs_iext_realloc_indirect() with a size
3809 * of zero would result in a call to xfs_iext_destroy() which
3810 * would in turn call this function again, creating a nasty
3811 * infinite loop.
3812 */
3813 if (--nlists) {
3814 xfs_iext_realloc_indirect(ifp,
3815 nlists * sizeof(xfs_ext_irec_t));
3816 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003817 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003818 }
3819 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3820}
3821
3822/*
3823 * This is called to clean up large amounts of unused memory allocated
3824 * by the indirection array. Before compacting anything though, verify
3825 * that the indirection array is still needed and switch back to the
3826 * linear extent list (or even the inline buffer) if possible. The
3827 * compaction policy is as follows:
3828 *
3829 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003830 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003831 * No Compaction: Extents occupy at least 50% of allocated space
3832 */
3833void
3834xfs_iext_irec_compact(
3835 xfs_ifork_t *ifp) /* inode fork pointer */
3836{
3837 xfs_extnum_t nextents; /* number of extents in file */
3838 int nlists; /* number of irec's (ex lists) */
3839
3840 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3841 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3842 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3843
3844 if (nextents == 0) {
3845 xfs_iext_destroy(ifp);
3846 } else if (nextents <= XFS_INLINE_EXTS) {
3847 xfs_iext_indirect_to_direct(ifp);
3848 xfs_iext_direct_to_inline(ifp, nextents);
3849 } else if (nextents <= XFS_LINEAR_EXTS) {
3850 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003851 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3852 xfs_iext_irec_compact_pages(ifp);
3853 }
3854}
3855
3856/*
3857 * Combine extents from neighboring extent pages.
3858 */
3859void
3860xfs_iext_irec_compact_pages(
3861 xfs_ifork_t *ifp) /* inode fork pointer */
3862{
3863 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3864 int erp_idx = 0; /* indirection array index */
3865 int nlists; /* number of irec's (ex lists) */
3866
3867 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3868 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3869 while (erp_idx < nlists - 1) {
3870 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3871 erp_next = erp + 1;
3872 if (erp_next->er_extcount <=
3873 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003874 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003875 erp_next->er_extbuf, erp_next->er_extcount *
3876 sizeof(xfs_bmbt_rec_t));
3877 erp->er_extcount += erp_next->er_extcount;
3878 /*
3879 * Free page before removing extent record
3880 * so er_extoffs don't get modified in
3881 * xfs_iext_irec_remove.
3882 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003883 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003884 erp_next->er_extbuf = NULL;
3885 xfs_iext_irec_remove(ifp, erp_idx + 1);
3886 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3887 } else {
3888 erp_idx++;
3889 }
3890 }
3891}
3892
3893/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003894 * This is called to update the er_extoff field in the indirection
3895 * array when extents have been added or removed from one of the
3896 * extent lists. erp_idx contains the irec index to begin updating
3897 * at and ext_diff contains the number of extents that were added
3898 * or removed.
3899 */
3900void
3901xfs_iext_irec_update_extoffs(
3902 xfs_ifork_t *ifp, /* inode fork pointer */
3903 int erp_idx, /* irec index to update */
3904 int ext_diff) /* number of new extents */
3905{
3906 int i; /* loop counter */
3907 int nlists; /* number of irec's (ex lists */
3908
3909 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3910 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3911 for (i = erp_idx; i < nlists; i++) {
3912 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3913 }
3914}