blob: ba404e4b9f0cb334054ec4e219c0ae77e807242a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049kmem_zone_t *xfs_ifork_zone;
50kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020053 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * freed from a file in a single transaction.
55 */
56#define XFS_ITRUNC_MAX_EXTENTS 2
57
58STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
59STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
60STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
61STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
62
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100063/*
64 * helper function to extract extent size hint from inode
65 */
66xfs_extlen_t
67xfs_get_extsz_hint(
68 struct xfs_inode *ip)
69{
70 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
71 return ip->i_d.di_extsize;
72 if (XFS_IS_REALTIME_INODE(ip))
73 return ip->i_mount->m_sb.sb_rextsize;
74 return 0;
75}
76
Dave Chinnerfa96aca2012-10-08 21:56:10 +110077/*
78 * This is a wrapper routine around the xfs_ilock() routine used to centralize
79 * some grungy code. It is used in places that wish to lock the inode solely
80 * for reading the extents. The reason these places can't just call
81 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
82 * extents from disk for a file in b-tree format. If the inode is in b-tree
83 * format, then we need to lock the inode exclusively until the extents are read
84 * in. Locking it exclusively all the time would limit our parallelism
85 * unnecessarily, though. What we do instead is check to see if the extents
86 * have been read in yet, and only lock the inode exclusively if they have not.
87 *
88 * The function returns a value which should be given to the corresponding
89 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
90 * actually taken.
91 */
92uint
93xfs_ilock_map_shared(
94 xfs_inode_t *ip)
95{
96 uint lock_mode;
97
98 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
99 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
100 lock_mode = XFS_ILOCK_EXCL;
101 } else {
102 lock_mode = XFS_ILOCK_SHARED;
103 }
104
105 xfs_ilock(ip, lock_mode);
106
107 return lock_mode;
108}
109
110/*
111 * This is simply the unlock routine to go with xfs_ilock_map_shared().
112 * All it does is call xfs_iunlock() with the given lock_mode.
113 */
114void
115xfs_iunlock_map_shared(
116 xfs_inode_t *ip,
117 unsigned int lock_mode)
118{
119 xfs_iunlock(ip, lock_mode);
120}
121
122/*
123 * The xfs inode contains 2 locks: a multi-reader lock called the
124 * i_iolock and a multi-reader lock called the i_lock. This routine
125 * allows either or both of the locks to be obtained.
126 *
127 * The 2 locks should always be ordered so that the IO lock is
128 * obtained first in order to prevent deadlock.
129 *
130 * ip -- the inode being locked
131 * lock_flags -- this parameter indicates the inode's locks
132 * to be locked. It can be:
133 * XFS_IOLOCK_SHARED,
134 * XFS_IOLOCK_EXCL,
135 * XFS_ILOCK_SHARED,
136 * XFS_ILOCK_EXCL,
137 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
138 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
139 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
140 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
141 */
142void
143xfs_ilock(
144 xfs_inode_t *ip,
145 uint lock_flags)
146{
147 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
148
149 /*
150 * You can't set both SHARED and EXCL for the same lock,
151 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
152 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
153 */
154 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
155 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
156 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
157 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
158 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
159
160 if (lock_flags & XFS_IOLOCK_EXCL)
161 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
162 else if (lock_flags & XFS_IOLOCK_SHARED)
163 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
164
165 if (lock_flags & XFS_ILOCK_EXCL)
166 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
167 else if (lock_flags & XFS_ILOCK_SHARED)
168 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
169}
170
171/*
172 * This is just like xfs_ilock(), except that the caller
173 * is guaranteed not to sleep. It returns 1 if it gets
174 * the requested locks and 0 otherwise. If the IO lock is
175 * obtained but the inode lock cannot be, then the IO lock
176 * is dropped before returning.
177 *
178 * ip -- the inode being locked
179 * lock_flags -- this parameter indicates the inode's locks to be
180 * to be locked. See the comment for xfs_ilock() for a list
181 * of valid values.
182 */
183int
184xfs_ilock_nowait(
185 xfs_inode_t *ip,
186 uint lock_flags)
187{
188 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
189
190 /*
191 * You can't set both SHARED and EXCL for the same lock,
192 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
193 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
194 */
195 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
196 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
197 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
198 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
199 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
200
201 if (lock_flags & XFS_IOLOCK_EXCL) {
202 if (!mrtryupdate(&ip->i_iolock))
203 goto out;
204 } else if (lock_flags & XFS_IOLOCK_SHARED) {
205 if (!mrtryaccess(&ip->i_iolock))
206 goto out;
207 }
208 if (lock_flags & XFS_ILOCK_EXCL) {
209 if (!mrtryupdate(&ip->i_lock))
210 goto out_undo_iolock;
211 } else if (lock_flags & XFS_ILOCK_SHARED) {
212 if (!mrtryaccess(&ip->i_lock))
213 goto out_undo_iolock;
214 }
215 return 1;
216
217 out_undo_iolock:
218 if (lock_flags & XFS_IOLOCK_EXCL)
219 mrunlock_excl(&ip->i_iolock);
220 else if (lock_flags & XFS_IOLOCK_SHARED)
221 mrunlock_shared(&ip->i_iolock);
222 out:
223 return 0;
224}
225
226/*
227 * xfs_iunlock() is used to drop the inode locks acquired with
228 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
229 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
230 * that we know which locks to drop.
231 *
232 * ip -- the inode being unlocked
233 * lock_flags -- this parameter indicates the inode's locks to be
234 * to be unlocked. See the comment for xfs_ilock() for a list
235 * of valid values for this parameter.
236 *
237 */
238void
239xfs_iunlock(
240 xfs_inode_t *ip,
241 uint lock_flags)
242{
243 /*
244 * You can't set both SHARED and EXCL for the same lock,
245 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
246 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
247 */
248 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
249 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
250 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
251 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
252 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
253 ASSERT(lock_flags != 0);
254
255 if (lock_flags & XFS_IOLOCK_EXCL)
256 mrunlock_excl(&ip->i_iolock);
257 else if (lock_flags & XFS_IOLOCK_SHARED)
258 mrunlock_shared(&ip->i_iolock);
259
260 if (lock_flags & XFS_ILOCK_EXCL)
261 mrunlock_excl(&ip->i_lock);
262 else if (lock_flags & XFS_ILOCK_SHARED)
263 mrunlock_shared(&ip->i_lock);
264
265 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
266}
267
268/*
269 * give up write locks. the i/o lock cannot be held nested
270 * if it is being demoted.
271 */
272void
273xfs_ilock_demote(
274 xfs_inode_t *ip,
275 uint lock_flags)
276{
277 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
278 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
279
280 if (lock_flags & XFS_ILOCK_EXCL)
281 mrdemote(&ip->i_lock);
282 if (lock_flags & XFS_IOLOCK_EXCL)
283 mrdemote(&ip->i_iolock);
284
285 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
286}
287
288#ifdef DEBUG
289int
290xfs_isilocked(
291 xfs_inode_t *ip,
292 uint lock_flags)
293{
294 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
295 if (!(lock_flags & XFS_ILOCK_SHARED))
296 return !!ip->i_lock.mr_writer;
297 return rwsem_is_locked(&ip->i_lock.mr_lock);
298 }
299
300 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
301 if (!(lock_flags & XFS_IOLOCK_SHARED))
302 return !!ip->i_iolock.mr_writer;
303 return rwsem_is_locked(&ip->i_iolock.mr_lock);
304 }
305
306 ASSERT(0);
307 return 0;
308}
309#endif
310
311void
312__xfs_iflock(
313 struct xfs_inode *ip)
314{
315 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
316 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
317
318 do {
319 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
320 if (xfs_isiflocked(ip))
321 io_schedule();
322 } while (!xfs_iflock_nowait(ip));
323
324 finish_wait(wq, &wait.wait);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifdef DEBUG
328/*
329 * Make sure that the extents in the given memory buffer
330 * are valid.
331 */
332STATIC void
333xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100334 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xfs_exntfmt_t fmt)
337{
338 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000339 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int i;
341
342 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000343 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
344 rec.l0 = get_unaligned(&ep->l0);
345 rec.l1 = get_unaligned(&ep->l1);
346 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (fmt == XFS_EXTFMT_NOSTATE)
348 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350}
351#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000352#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#endif /* DEBUG */
354
355/*
356 * Check that none of the inode's in the buffer have a next
357 * unlinked field of 0.
358 */
359#if defined(DEBUG)
360void
361xfs_inobp_check(
362 xfs_mount_t *mp,
363 xfs_buf_t *bp)
364{
365 int i;
366 int j;
367 xfs_dinode_t *dip;
368
369 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
370
371 for (i = 0; i < j; i++) {
372 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
373 i * mp->m_sb.sb_inodesize);
374 if (!dip->di_next_unlinked) {
Dave Chinner53487782011-03-07 10:05:35 +1100375 xfs_alert(mp,
376 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 bp);
378 ASSERT(dip->di_next_unlinked);
379 }
380 }
381}
382#endif
383
384/*
Christoph Hellwig475ee412012-07-03 12:21:22 -0400385 * This routine is called to map an inode to the buffer containing the on-disk
386 * version of the inode. It returns a pointer to the buffer containing the
387 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
388 * pointer to the on-disk inode within that buffer.
389 *
390 * If a non-zero error is returned, then the contents of bpp and dipp are
391 * undefined.
David Chinner4ae29b42008-03-06 13:43:34 +1100392 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400393int
David Chinner4ae29b42008-03-06 13:43:34 +1100394xfs_imap_to_bp(
Christoph Hellwig475ee412012-07-03 12:21:22 -0400395 struct xfs_mount *mp,
396 struct xfs_trans *tp,
397 struct xfs_imap *imap,
398 struct xfs_dinode **dipp,
399 struct xfs_buf **bpp,
400 uint buf_flags,
401 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100402{
Christoph Hellwig475ee412012-07-03 12:21:22 -0400403 struct xfs_buf *bp;
404 int error;
405 int i;
406 int ni;
David Chinner4ae29b42008-03-06 13:43:34 +1100407
Dave Chinner611c9942012-04-23 15:59:07 +1000408 buf_flags |= XBF_UNMAPPED;
David Chinner4ae29b42008-03-06 13:43:34 +1100409 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100410 (int)imap->im_len, buf_flags, &bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100411 if (error) {
David Chinnera3f74ff2008-03-06 13:43:42 +1100412 if (error != EAGAIN) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100413 xfs_warn(mp,
414 "%s: xfs_trans_read_buf() returned error %d.",
415 __func__, error);
David Chinnera3f74ff2008-03-06 13:43:42 +1100416 } else {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000417 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100418 }
David Chinner4ae29b42008-03-06 13:43:34 +1100419 return error;
420 }
421
422 /*
423 * Validate the magic number and version of every inode in the buffer
424 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
425 */
426#ifdef DEBUG
427 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
428#else /* usual case */
429 ni = 1;
430#endif
431
432 for (i = 0; i < ni; i++) {
433 int di_ok;
434 xfs_dinode_t *dip;
435
436 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
437 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200438 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100439 XFS_DINODE_GOOD_VERSION(dip->di_version);
David Chinner4ae29b42008-03-06 13:43:34 +1100440 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
441 XFS_ERRTAG_ITOBP_INOTOBP,
442 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinner19207792010-06-24 11:15:47 +1000443 if (iget_flags & XFS_IGET_UNTRUSTED) {
David Chinner4ae29b42008-03-06 13:43:34 +1100444 xfs_trans_brelse(tp, bp);
445 return XFS_ERROR(EINVAL);
446 }
Christoph Hellwig475ee412012-07-03 12:21:22 -0400447 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
448 mp, dip);
David Chinner4ae29b42008-03-06 13:43:34 +1100449#ifdef DEBUG
Dave Chinner0b932cc2011-03-07 10:08:35 +1100450 xfs_emerg(mp,
451 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
David Chinner4ae29b42008-03-06 13:43:34 +1100452 (unsigned long long)imap->im_blkno, i,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100453 be16_to_cpu(dip->di_magic));
Dave Chinner0b932cc2011-03-07 10:08:35 +1100454 ASSERT(0);
David Chinner4ae29b42008-03-06 13:43:34 +1100455#endif
456 xfs_trans_brelse(tp, bp);
457 return XFS_ERROR(EFSCORRUPTED);
458 }
459 }
460
461 xfs_inobp_check(mp, bp);
Christoph Hellwig475ee412012-07-03 12:21:22 -0400462
David Chinner4ae29b42008-03-06 13:43:34 +1100463 *bpp = bp;
Christoph Hellwig475ee412012-07-03 12:21:22 -0400464 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
David Chinner4ae29b42008-03-06 13:43:34 +1100465 return 0;
466}
467
468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * Move inode type and inode format specific information from the
470 * on-disk inode to the in-core inode. For fifos, devs, and sockets
471 * this means set if_rdev to the proper value. For files, directories,
472 * and symlinks this means to bring in the in-line data or extent
473 * pointers. For a file in B-tree format, only the root is immediately
474 * brought in-core. The rest will be in-lined in if_extents when it
475 * is first referenced (see xfs_iread_extents()).
476 */
477STATIC int
478xfs_iformat(
479 xfs_inode_t *ip,
480 xfs_dinode_t *dip)
481{
482 xfs_attr_shortform_t *atp;
483 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000484 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100487 if (unlikely(be32_to_cpu(dip->di_nextents) +
488 be16_to_cpu(dip->di_anextents) >
489 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100490 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100491 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100493 (int)(be32_to_cpu(dip->di_nextents) +
494 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100496 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
498 ip->i_mount, dip);
499 return XFS_ERROR(EFSCORRUPTED);
500 }
501
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100502 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100503 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100505 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
507 ip->i_mount, dip);
508 return XFS_ERROR(EFSCORRUPTED);
509 }
510
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300511 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
512 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100513 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300514 "corrupt dinode %Lu, has realtime flag set.",
515 ip->i_ino);
516 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
517 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
518 return XFS_ERROR(EFSCORRUPTED);
519 }
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 switch (ip->i_d.di_mode & S_IFMT) {
522 case S_IFIFO:
523 case S_IFCHR:
524 case S_IFBLK:
525 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100526 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
528 ip->i_mount, dip);
529 return XFS_ERROR(EFSCORRUPTED);
530 }
531 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100532 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case S_IFREG:
536 case S_IFLNK:
537 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100538 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 case XFS_DINODE_FMT_LOCAL:
540 /*
541 * no local regular files yet
542 */
Al Viroabbede12011-07-26 02:31:30 -0400543 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100544 xfs_warn(ip->i_mount,
545 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (unsigned long long) ip->i_ino);
547 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
548 XFS_ERRLEVEL_LOW,
549 ip->i_mount, dip);
550 return XFS_ERROR(EFSCORRUPTED);
551 }
552
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100553 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100555 xfs_warn(ip->i_mount,
556 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 (unsigned long long) ip->i_ino,
558 (long long) di_size);
559 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
560 XFS_ERRLEVEL_LOW,
561 ip->i_mount, dip);
562 return XFS_ERROR(EFSCORRUPTED);
563 }
564
565 size = (int)di_size;
566 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
567 break;
568 case XFS_DINODE_FMT_EXTENTS:
569 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
570 break;
571 case XFS_DINODE_FMT_BTREE:
572 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
573 break;
574 default:
575 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
576 ip->i_mount);
577 return XFS_ERROR(EFSCORRUPTED);
578 }
579 break;
580
581 default:
582 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
583 return XFS_ERROR(EFSCORRUPTED);
584 }
585 if (error) {
586 return error;
587 }
588 if (!XFS_DFORK_Q(dip))
589 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000592 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000593
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100594 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 case XFS_DINODE_FMT_LOCAL:
596 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100597 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100598
599 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100600 xfs_warn(ip->i_mount,
601 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100602 (unsigned long long) ip->i_ino,
603 (long long) size);
604 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
605 XFS_ERRLEVEL_LOW,
606 ip->i_mount, dip);
607 return XFS_ERROR(EFSCORRUPTED);
608 }
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
611 break;
612 case XFS_DINODE_FMT_EXTENTS:
613 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
614 break;
615 case XFS_DINODE_FMT_BTREE:
616 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
617 break;
618 default:
619 error = XFS_ERROR(EFSCORRUPTED);
620 break;
621 }
622 if (error) {
623 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
624 ip->i_afp = NULL;
625 xfs_idestroy_fork(ip, XFS_DATA_FORK);
626 }
627 return error;
628}
629
630/*
631 * The file is in-lined in the on-disk inode.
632 * If it fits into if_inline_data, then copy
633 * it there, otherwise allocate a buffer for it
634 * and copy the data there. Either way, set
635 * if_data to point at the data.
636 * If we allocate a buffer for the data, make
637 * sure that its size is a multiple of 4 and
638 * record the real size in i_real_bytes.
639 */
640STATIC int
641xfs_iformat_local(
642 xfs_inode_t *ip,
643 xfs_dinode_t *dip,
644 int whichfork,
645 int size)
646{
647 xfs_ifork_t *ifp;
648 int real_size;
649
650 /*
651 * If the size is unreasonable, then something
652 * is wrong and we just bail out rather than crash in
653 * kmem_alloc() or memcpy() below.
654 */
655 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100656 xfs_warn(ip->i_mount,
657 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 (unsigned long long) ip->i_ino, size,
659 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
660 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
661 ip->i_mount, dip);
662 return XFS_ERROR(EFSCORRUPTED);
663 }
664 ifp = XFS_IFORK_PTR(ip, whichfork);
665 real_size = 0;
666 if (size == 0)
667 ifp->if_u1.if_data = NULL;
668 else if (size <= sizeof(ifp->if_u2.if_inline_data))
669 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
670 else {
671 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000672 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 ifp->if_bytes = size;
675 ifp->if_real_bytes = real_size;
676 if (size)
677 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
678 ifp->if_flags &= ~XFS_IFEXTENTS;
679 ifp->if_flags |= XFS_IFINLINE;
680 return 0;
681}
682
683/*
684 * The file consists of a set of extents all
685 * of which fit into the on-disk inode.
686 * If there are few enough extents to fit into
687 * the if_inline_ext, then copy them there.
688 * Otherwise allocate a buffer for them and copy
689 * them into it. Either way, set if_extents
690 * to point at the extents.
691 */
692STATIC int
693xfs_iformat_extents(
694 xfs_inode_t *ip,
695 xfs_dinode_t *dip,
696 int whichfork)
697{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000698 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 xfs_ifork_t *ifp;
700 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int size;
702 int i;
703
704 ifp = XFS_IFORK_PTR(ip, whichfork);
705 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
706 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
707
708 /*
709 * If the number of extents is unreasonable, then something
710 * is wrong and we just bail out rather than crash in
711 * kmem_alloc() or memcpy() below.
712 */
713 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100714 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 (unsigned long long) ip->i_ino, nex);
716 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
717 ip->i_mount, dip);
718 return XFS_ERROR(EFSCORRUPTED);
719 }
720
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100721 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (nex == 0)
723 ifp->if_u1.if_extents = NULL;
724 else if (nex <= XFS_INLINE_EXTS)
725 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100726 else
727 xfs_iext_add(ifp, 0, nex);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (size) {
731 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000732 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100733 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000734 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000735 ep->l0 = get_unaligned_be64(&dp->l0);
736 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000738 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (whichfork != XFS_DATA_FORK ||
740 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
741 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100742 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
744 XFS_ERRLEVEL_LOW,
745 ip->i_mount);
746 return XFS_ERROR(EFSCORRUPTED);
747 }
748 }
749 ifp->if_flags |= XFS_IFEXTENTS;
750 return 0;
751}
752
753/*
754 * The file has too many extents to fit into
755 * the inode, so they are in B-tree format.
756 * Allocate a buffer for the root of the B-tree
757 * and copy the root into it. The i_extents
758 * field will remain NULL until all of the
759 * extents are read in (when they are needed).
760 */
761STATIC int
762xfs_iformat_btree(
763 xfs_inode_t *ip,
764 xfs_dinode_t *dip,
765 int whichfork)
766{
767 xfs_bmdr_block_t *dfp;
768 xfs_ifork_t *ifp;
769 /* REFERENCED */
770 int nrecs;
771 int size;
772
773 ifp = XFS_IFORK_PTR(ip, whichfork);
774 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
775 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100776 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /*
779 * blow out if -- fork has less extents than can fit in
780 * fork (fork shouldn't be a btree format), root btree
781 * block has more records than can fit into the fork,
782 * or the number of extents is greater than the number of
783 * blocks.
784 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000785 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
786 XFS_IFORK_MAXEXT(ip, whichfork) ||
787 XFS_BMDR_SPACE_CALC(nrecs) >
788 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
789 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100790 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100792 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
793 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return XFS_ERROR(EFSCORRUPTED);
795 }
796
797 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000798 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ASSERT(ifp->if_broot != NULL);
800 /*
801 * Copy and convert from the on-disk structure
802 * to the in-memory structure.
803 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100804 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
805 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
806 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ifp->if_flags &= ~XFS_IFEXTENTS;
808 ifp->if_flags |= XFS_IFBROOT;
809
810 return 0;
811}
812
Eric Sandeend96f8f82009-07-02 00:09:33 -0500813STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000814xfs_dinode_from_disk(
815 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100816 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000818 to->di_magic = be16_to_cpu(from->di_magic);
819 to->di_mode = be16_to_cpu(from->di_mode);
820 to->di_version = from ->di_version;
821 to->di_format = from->di_format;
822 to->di_onlink = be16_to_cpu(from->di_onlink);
823 to->di_uid = be32_to_cpu(from->di_uid);
824 to->di_gid = be32_to_cpu(from->di_gid);
825 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000826 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
827 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000828 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
829 to->di_flushiter = be16_to_cpu(from->di_flushiter);
830 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
831 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
832 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
833 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
834 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
835 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
836 to->di_size = be64_to_cpu(from->di_size);
837 to->di_nblocks = be64_to_cpu(from->di_nblocks);
838 to->di_extsize = be32_to_cpu(from->di_extsize);
839 to->di_nextents = be32_to_cpu(from->di_nextents);
840 to->di_anextents = be16_to_cpu(from->di_anextents);
841 to->di_forkoff = from->di_forkoff;
842 to->di_aformat = from->di_aformat;
843 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
844 to->di_dmstate = be16_to_cpu(from->di_dmstate);
845 to->di_flags = be16_to_cpu(from->di_flags);
846 to->di_gen = be32_to_cpu(from->di_gen);
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000849void
850xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100851 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000852 xfs_icdinode_t *from)
853{
854 to->di_magic = cpu_to_be16(from->di_magic);
855 to->di_mode = cpu_to_be16(from->di_mode);
856 to->di_version = from ->di_version;
857 to->di_format = from->di_format;
858 to->di_onlink = cpu_to_be16(from->di_onlink);
859 to->di_uid = cpu_to_be32(from->di_uid);
860 to->di_gid = cpu_to_be32(from->di_gid);
861 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000862 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
863 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000864 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
865 to->di_flushiter = cpu_to_be16(from->di_flushiter);
866 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
867 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
868 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
869 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
870 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
871 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
872 to->di_size = cpu_to_be64(from->di_size);
873 to->di_nblocks = cpu_to_be64(from->di_nblocks);
874 to->di_extsize = cpu_to_be32(from->di_extsize);
875 to->di_nextents = cpu_to_be32(from->di_nextents);
876 to->di_anextents = cpu_to_be16(from->di_anextents);
877 to->di_forkoff = from->di_forkoff;
878 to->di_aformat = from->di_aformat;
879 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
880 to->di_dmstate = cpu_to_be16(from->di_dmstate);
881 to->di_flags = cpu_to_be16(from->di_flags);
882 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885STATIC uint
886_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 __uint16_t di_flags)
888{
889 uint flags = 0;
890
891 if (di_flags & XFS_DIFLAG_ANY) {
892 if (di_flags & XFS_DIFLAG_REALTIME)
893 flags |= XFS_XFLAG_REALTIME;
894 if (di_flags & XFS_DIFLAG_PREALLOC)
895 flags |= XFS_XFLAG_PREALLOC;
896 if (di_flags & XFS_DIFLAG_IMMUTABLE)
897 flags |= XFS_XFLAG_IMMUTABLE;
898 if (di_flags & XFS_DIFLAG_APPEND)
899 flags |= XFS_XFLAG_APPEND;
900 if (di_flags & XFS_DIFLAG_SYNC)
901 flags |= XFS_XFLAG_SYNC;
902 if (di_flags & XFS_DIFLAG_NOATIME)
903 flags |= XFS_XFLAG_NOATIME;
904 if (di_flags & XFS_DIFLAG_NODUMP)
905 flags |= XFS_XFLAG_NODUMP;
906 if (di_flags & XFS_DIFLAG_RTINHERIT)
907 flags |= XFS_XFLAG_RTINHERIT;
908 if (di_flags & XFS_DIFLAG_PROJINHERIT)
909 flags |= XFS_XFLAG_PROJINHERIT;
910 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
911 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100912 if (di_flags & XFS_DIFLAG_EXTSIZE)
913 flags |= XFS_XFLAG_EXTSIZE;
914 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
915 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000916 if (di_flags & XFS_DIFLAG_NODEFRAG)
917 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000918 if (di_flags & XFS_DIFLAG_FILESTREAM)
919 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 return flags;
923}
924
925uint
926xfs_ip2xflags(
927 xfs_inode_t *ip)
928{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000929 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Nathan Scotta916e2b2006-06-09 17:12:17 +1000931 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100932 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935uint
936xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100937 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100939 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100940 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100944 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946int
947xfs_iread(
948 xfs_mount_t *mp,
949 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100950 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100951 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 xfs_buf_t *bp;
954 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int error;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100958 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100960 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100961 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100962 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100965 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100966 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400967 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100968 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100969 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100970
971 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * If we got something that isn't an inode it means someone
973 * (nfs or dmi) has a stale handle.
974 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200975 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100977 xfs_alert(mp,
978 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
979 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100981 error = XFS_ERROR(EINVAL);
982 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 /*
986 * If the on-disk inode is already linked to a directory
987 * entry, copy all of the inode into the in-core inode.
988 * xfs_iformat() handles copying in the inode format
989 * specific information.
990 * Otherwise, just get the truly permanent information.
991 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100992 if (dip->di_mode) {
993 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 error = xfs_iformat(ip, dip);
995 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100997 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
998 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001000 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001003 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1004 ip->i_d.di_version = dip->di_version;
1005 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1006 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Make sure to pull in the mode here as well in
1009 * case the inode is released without being used.
1010 * This ensures that xfs_inactive() will see that
1011 * the inode is already free and not try to mess
1012 * with the uninitialized part of it.
1013 */
1014 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * The inode format changed when we moved the link count and
1019 * made it 32 bits long. If this is an old format inode,
1020 * convert it in memory to look like a new one. If it gets
1021 * flushed to disk we will convert back before flushing or
1022 * logging it. We zero out the new projid field and the old link
1023 * count field. We'll handle clearing the pad field (the remains
1024 * of the old uuid field) when we actually convert the inode to
1025 * the new format. We don't change the version number so that we
1026 * can distinguish this from a real new format inode.
1027 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001028 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ip->i_d.di_nlink = ip->i_d.di_onlink;
1030 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001031 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 ip->i_delayed_blks = 0;
1035
1036 /*
1037 * Mark the buffer containing the inode as something to keep
1038 * around for a while. This helps to keep recently accessed
1039 * meta-data in-core longer.
1040 */
Dave Chinner821eb212010-12-02 16:31:13 +11001041 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Use xfs_trans_brelse() to release the buffer containing the
1045 * on-disk inode, because it was acquired with xfs_trans_read_buf()
Christoph Hellwig475ee412012-07-03 12:21:22 -04001046 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1048 * will only release the buffer if it is not dirty within the
1049 * transaction. It will be OK to release the buffer in this case,
1050 * because inodes on disk are never destroyed and we will be
1051 * locking the new in-core inode before putting it in the hash
1052 * table where other processes can find it. Thus we don't have
1053 * to worry about the inode being changed just because we released
1054 * the buffer.
1055 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001056 out_brelse:
1057 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +11001058 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
1062 * Read in extents from a btree-format inode.
1063 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1064 */
1065int
1066xfs_iread_extents(
1067 xfs_trans_t *tp,
1068 xfs_inode_t *ip,
1069 int whichfork)
1070{
1071 int error;
1072 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001073 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1076 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1077 ip->i_mount);
1078 return XFS_ERROR(EFSCORRUPTED);
1079 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001080 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * We know that the size is valid (it's checked in iformat_btree)
1085 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001086 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001088 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 error = xfs_bmap_read_extents(tp, ip, whichfork);
1090 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001091 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ifp->if_flags &= ~XFS_IFEXTENTS;
1093 return error;
1094 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001095 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
1099/*
1100 * Allocate an inode on disk and return a copy of its in-core version.
1101 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1102 * appropriately within the inode. The uid and gid for the inode are
1103 * set according to the contents of the given cred structure.
1104 *
1105 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
1106 * has a free inode available, call xfs_iget()
1107 * to obtain the in-core version of the allocated inode. Finally,
1108 * fill in the inode and log its initial contents. In this case,
1109 * ialloc_context would be set to NULL and call_again set to false.
1110 *
1111 * If xfs_dialloc() does not have an available inode,
1112 * it will replenish its supply by doing an allocation. Since we can
1113 * only do one allocation within a transaction without deadlocks, we
1114 * must commit the current transaction before returning the inode itself.
1115 * In this case, therefore, we will set call_again to true and return.
1116 * The caller should then commit the current transaction, start a new
1117 * transaction, and call xfs_ialloc() again to actually get the inode.
1118 *
1119 * To ensure that some other process does not grab the inode that
1120 * was allocated during the first call to xfs_ialloc(), this routine
1121 * also returns the [locked] bp pointing to the head of the freelist
1122 * as ialloc_context. The caller should hold this buffer across
1123 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +10001124 *
1125 * If we are allocating quota inodes, we do not have a parent inode
1126 * to attach to or associate with (i.e. pip == NULL) because they
1127 * are not linked into the directory structure - they are attached
1128 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
1130int
1131xfs_ialloc(
1132 xfs_trans_t *tp,
1133 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -04001134 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -07001135 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001137 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int okalloc,
1139 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_inode_t **ipp)
1141{
1142 xfs_ino_t ino;
1143 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 uint flags;
1145 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001146 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +11001147 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /*
1150 * Call the space management code to pick
1151 * the on-disk inode to be allocated.
1152 */
David Chinnerb11f94d2007-07-11 11:09:33 +10001153 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -04001154 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +11001155 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -04001157 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 *ipp = NULL;
1159 return 0;
1160 }
1161 ASSERT(*ialloc_context == NULL);
1162
1163 /*
1164 * Get the in-core inode with the lock held exclusively.
1165 * This is because we're setting fields here we need
1166 * to prevent others from looking at until we're done.
1167 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001168 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
1169 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +11001170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 ASSERT(ip != NULL);
1173
Al Viro576b1d62011-07-26 02:50:15 -04001174 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ip->i_d.di_onlink = 0;
1176 ip->i_d.di_nlink = nlink;
1177 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +01001178 ip->i_d.di_uid = current_fsuid();
1179 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001180 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1182
1183 /*
1184 * If the superblock version is up to where we support new format
1185 * inodes and this is currently an old format inode, then change
1186 * the inode version number now. This way we only do the conversion
1187 * here rather than here and in the flush/logging code.
1188 */
Eric Sandeen62118702008-03-06 13:44:28 +11001189 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001190 ip->i_d.di_version == 1) {
1191 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /*
1193 * We've already zeroed the old link count, the projid field,
1194 * and the pad field.
1195 */
1196 }
1197
1198 /*
1199 * Project ids won't be stored on disk if we are using a version 1 inode.
1200 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001201 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfs_bump_ino_vers2(tp, ip);
1203
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001204 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001206 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ip->i_d.di_mode |= S_ISGID;
1208 }
1209 }
1210
1211 /*
1212 * If the group ID of the new file does not match the effective group
1213 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1214 * (and only if the irix_sgid_inherit compatibility variable is set).
1215 */
1216 if ((irix_sgid_inherit) &&
1217 (ip->i_d.di_mode & S_ISGID) &&
1218 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1219 ip->i_d.di_mode &= ~S_ISGID;
1220 }
1221
1222 ip->i_d.di_size = 0;
1223 ip->i_d.di_nextents = 0;
1224 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001225
1226 nanotime(&tv);
1227 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1228 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1229 ip->i_d.di_atime = ip->i_d.di_mtime;
1230 ip->i_d.di_ctime = ip->i_d.di_mtime;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * di_gen will have been taken care of in xfs_iread.
1234 */
1235 ip->i_d.di_extsize = 0;
1236 ip->i_d.di_dmevmask = 0;
1237 ip->i_d.di_dmstate = 0;
1238 ip->i_d.di_flags = 0;
1239 flags = XFS_ILOG_CORE;
1240 switch (mode & S_IFMT) {
1241 case S_IFIFO:
1242 case S_IFCHR:
1243 case S_IFBLK:
1244 case S_IFSOCK:
1245 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1246 ip->i_df.if_u2.if_rdev = rdev;
1247 ip->i_df.if_flags = 0;
1248 flags |= XFS_ILOG_DEV;
1249 break;
1250 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001251 /*
1252 * we can't set up filestreams until after the VFS inode
1253 * is set up properly.
1254 */
1255 if (pip && xfs_inode_is_filestream(pip))
1256 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001257 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001259 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001260 uint di_flags = 0;
1261
Al Viroabbede12011-07-26 02:31:30 -04001262 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001263 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1264 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001265 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1266 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1267 ip->i_d.di_extsize = pip->i_d.di_extsize;
1268 }
Al Viroabbede12011-07-26 02:31:30 -04001269 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001270 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001271 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001272 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1273 di_flags |= XFS_DIFLAG_EXTSIZE;
1274 ip->i_d.di_extsize = pip->i_d.di_extsize;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1278 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001279 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1281 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001282 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1284 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001285 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1287 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001288 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1289 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1290 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001291 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1292 xfs_inherit_nodefrag)
1293 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001294 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1295 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001296 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 /* FALLTHROUGH */
1299 case S_IFLNK:
1300 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1301 ip->i_df.if_flags = XFS_IFEXTENTS;
1302 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1303 ip->i_df.if_u1.if_extents = NULL;
1304 break;
1305 default:
1306 ASSERT(0);
1307 }
1308 /*
1309 * Attribute fork settings for new inode.
1310 */
1311 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1312 ip->i_d.di_anextents = 0;
1313
1314 /*
1315 * Log the new values stuffed into the inode.
1316 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001317 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 xfs_trans_log_inode(tp, ip, flags);
1319
Nathan Scottb83bd132006-06-09 16:48:30 +10001320 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001321 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
David Chinnerbf904242008-10-30 17:36:14 +11001323 /* now we have set up the vfs inode we can associate the filestream */
1324 if (filestreams) {
1325 error = xfs_filestream_associate(pip, ip);
1326 if (error < 0)
1327 return -error;
1328 if (!error)
1329 xfs_iflags_set(ip, XFS_IFILESTREAM);
1330 }
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *ipp = ip;
1333 return 0;
1334}
1335
1336/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001337 * Free up the underlying blocks past new_size. The new size must be smaller
1338 * than the current size. This routine can be used both for the attribute and
1339 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
David Chinnerf6485052008-04-17 16:50:04 +10001341 * The transaction passed to this routine must have made a permanent log
1342 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1343 * given transaction and start new ones, so make sure everything involved in
1344 * the transaction is tidy before calling here. Some transaction will be
1345 * returned to the caller to be committed. The incoming transaction must
1346 * already include the inode, and both inode locks must be held exclusively.
1347 * The inode must also be "held" within the transaction. On return the inode
1348 * will be "held" within the returned transaction. This routine does NOT
1349 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
David Chinnerf6485052008-04-17 16:50:04 +10001351 * If we get an error, we must return with the inode locked and linked into the
1352 * current transaction. This keeps things simple for the higher level code,
1353 * because it always knows that the inode is locked and held in the transaction
1354 * that returns to it whether errors occur or not. We don't mark the inode
1355 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
1357int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001358xfs_itruncate_extents(
1359 struct xfs_trans **tpp,
1360 struct xfs_inode *ip,
1361 int whichfork,
1362 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001364 struct xfs_mount *mp = ip->i_mount;
1365 struct xfs_trans *tp = *tpp;
1366 struct xfs_trans *ntp;
1367 xfs_bmap_free_t free_list;
1368 xfs_fsblock_t first_block;
1369 xfs_fileoff_t first_unmap_block;
1370 xfs_fileoff_t last_block;
1371 xfs_filblks_t unmap_len;
1372 int committed;
1373 int error = 0;
1374 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Christoph Hellwig0b561852012-07-04 11:13:31 -04001376 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1377 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1378 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001379 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001380 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001382 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001383 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001385 trace_xfs_itruncate_extents_start(ip, new_size);
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /*
1388 * Since it is possible for space to become allocated beyond
1389 * the end of the file (in a crash where the space is allocated
1390 * but the inode size is not yet updated), simply remove any
1391 * blocks which show up between the new EOF and the maximum
1392 * possible file size. If the first block to be removed is
1393 * beyond the maximum file size (ie it is the same as last_block),
1394 * then there is nothing to do.
1395 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001396 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001397 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001398 if (first_unmap_block == last_block)
1399 return 0;
1400
1401 ASSERT(first_unmap_block < last_block);
1402 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001404 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001405 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001406 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001407 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001409 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001410 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001411 if (error)
1412 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * Duplicate the transaction that has the permanent
1416 * reservation and commit the old transaction.
1417 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001418 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001419 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001420 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001421 if (error)
1422 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (committed) {
1425 /*
David Chinnerf6485052008-04-17 16:50:04 +10001426 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001427 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001429 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
David Chinnerf6485052008-04-17 16:50:04 +10001431
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001432 ntp = xfs_trans_dup(tp);
1433 error = xfs_trans_commit(tp, 0);
1434 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001435
Christoph Hellwigddc34152011-09-19 15:00:54 +00001436 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001437
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001438 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001439 goto out;
1440
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001441 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001442 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001443 * reference that we gained in xfs_trans_dup()
1444 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001445 xfs_log_ticket_put(tp->t_ticket);
1446 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001447 XFS_ITRUNCATE_LOG_RES(mp), 0,
1448 XFS_TRANS_PERM_LOG_RES,
1449 XFS_ITRUNCATE_LOG_COUNT);
1450 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001453
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001454 /*
1455 * Always re-log the inode so that our permanent transaction can keep
1456 * on rolling it forward in the log.
1457 */
1458 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1459
1460 trace_xfs_itruncate_extents_end(ip, new_size);
1461
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001462out:
1463 *tpp = tp;
1464 return error;
1465out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001467 * If the bunmapi call encounters an error, return to the caller where
1468 * the transaction can be properly aborted. We just need to make sure
1469 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001471 xfs_bmap_cancel(&free_list);
1472 goto out;
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/*
1476 * This is called when the inode's link count goes to 0.
1477 * We place the on-disk inode on a list in the AGI. It
1478 * will be pulled from this list when the inode is freed.
1479 */
1480int
1481xfs_iunlink(
1482 xfs_trans_t *tp,
1483 xfs_inode_t *ip)
1484{
1485 xfs_mount_t *mp;
1486 xfs_agi_t *agi;
1487 xfs_dinode_t *dip;
1488 xfs_buf_t *agibp;
1489 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 xfs_agino_t agino;
1491 short bucket_index;
1492 int offset;
1493 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 ASSERT(ip->i_d.di_nlink == 0);
1496 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 mp = tp->t_mountp;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /*
1501 * Get the agi buffer first. It ensures lock ordering
1502 * on the list.
1503 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001504 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001505 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /*
1510 * Get the index into the agi hash table for the
1511 * list this inode will go on.
1512 */
1513 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1514 ASSERT(agino != 0);
1515 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1516 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001517 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001519 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * There is already another inode in the bucket we need
1522 * to add ourselves to. Add us at the front of the list.
1523 * Here we put the head pointer into our next pointer,
1524 * and then we fall through to point the head at us.
1525 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001526 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1527 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001528 if (error)
1529 return error;
1530
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001531 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001533 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 offsetof(xfs_dinode_t, di_next_unlinked);
1535 xfs_trans_inode_buf(tp, ibp);
1536 xfs_trans_log_buf(tp, ibp, offset,
1537 (offset + sizeof(xfs_agino_t) - 1));
1538 xfs_inobp_check(mp, ibp);
1539 }
1540
1541 /*
1542 * Point the bucket head pointer at the inode being inserted.
1543 */
1544 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001545 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 offset = offsetof(xfs_agi_t, agi_unlinked) +
1547 (sizeof(xfs_agino_t) * bucket_index);
1548 xfs_trans_log_buf(tp, agibp, offset,
1549 (offset + sizeof(xfs_agino_t) - 1));
1550 return 0;
1551}
1552
1553/*
1554 * Pull the on-disk inode from the AGI unlinked list.
1555 */
1556STATIC int
1557xfs_iunlink_remove(
1558 xfs_trans_t *tp,
1559 xfs_inode_t *ip)
1560{
1561 xfs_ino_t next_ino;
1562 xfs_mount_t *mp;
1563 xfs_agi_t *agi;
1564 xfs_dinode_t *dip;
1565 xfs_buf_t *agibp;
1566 xfs_buf_t *ibp;
1567 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 xfs_agino_t agino;
1569 xfs_agino_t next_agino;
1570 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001571 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001573 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 /*
1580 * Get the agi buffer first. It ensures lock ordering
1581 * on the list.
1582 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001583 error = xfs_read_agi(mp, tp, agno, &agibp);
1584 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * Get the index into the agi hash table for the
1591 * list this inode will go on.
1592 */
1593 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1594 ASSERT(agino != 0);
1595 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001596 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ASSERT(agi->agi_unlinked[bucket_index]);
1598
Christoph Hellwig16259e72005-11-02 15:11:25 +11001599 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001601 * We're at the head of the list. Get the inode's on-disk
1602 * buffer to see if there is anyone after us on the list.
1603 * Only modify our next pointer if it is not already NULLAGINO.
1604 * This saves us the overhead of dealing with the buffer when
1605 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001607 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1608 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001610 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001611 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return error;
1613 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001614 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ASSERT(next_agino != 0);
1616 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001617 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001618 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 offsetof(xfs_dinode_t, di_next_unlinked);
1620 xfs_trans_inode_buf(tp, ibp);
1621 xfs_trans_log_buf(tp, ibp, offset,
1622 (offset + sizeof(xfs_agino_t) - 1));
1623 xfs_inobp_check(mp, ibp);
1624 } else {
1625 xfs_trans_brelse(tp, ibp);
1626 }
1627 /*
1628 * Point the bucket head pointer at the next inode.
1629 */
1630 ASSERT(next_agino != 0);
1631 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001632 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 offset = offsetof(xfs_agi_t, agi_unlinked) +
1634 (sizeof(xfs_agino_t) * bucket_index);
1635 xfs_trans_log_buf(tp, agibp, offset,
1636 (offset + sizeof(xfs_agino_t) - 1));
1637 } else {
1638 /*
1639 * We need to search the list for the inode being freed.
1640 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001641 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 last_ibp = NULL;
1643 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001644 struct xfs_imap imap;
1645
1646 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001648
1649 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001651
1652 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001654 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001655 "%s: xfs_imap returned error %d.",
1656 __func__, error);
1657 return error;
1658 }
1659
1660 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1661 &last_ibp, 0, 0);
1662 if (error) {
1663 xfs_warn(mp,
1664 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001665 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return error;
1667 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001668
1669 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001670 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 ASSERT(next_agino != NULLAGINO);
1672 ASSERT(next_agino != 0);
1673 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001676 * Now last_ibp points to the buffer previous to us on the
1677 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001679 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1680 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001682 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001683 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return error;
1685 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001686 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 ASSERT(next_agino != 0);
1688 ASSERT(next_agino != agino);
1689 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001690 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001691 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 offsetof(xfs_dinode_t, di_next_unlinked);
1693 xfs_trans_inode_buf(tp, ibp);
1694 xfs_trans_log_buf(tp, ibp, offset,
1695 (offset + sizeof(xfs_agino_t) - 1));
1696 xfs_inobp_check(mp, ibp);
1697 } else {
1698 xfs_trans_brelse(tp, ibp);
1699 }
1700 /*
1701 * Point the previous inode on the list to the next inode.
1702 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001703 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ASSERT(next_agino != 0);
1705 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1706 xfs_trans_inode_buf(tp, last_ibp);
1707 xfs_trans_log_buf(tp, last_ibp, offset,
1708 (offset + sizeof(xfs_agino_t) - 1));
1709 xfs_inobp_check(mp, last_ibp);
1710 }
1711 return 0;
1712}
1713
Dave Chinner5b3eed72010-08-24 11:42:41 +10001714/*
1715 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1716 * inodes that are in memory - they all must be marked stale and attached to
1717 * the cluster buffer.
1718 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001719STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720xfs_ifree_cluster(
1721 xfs_inode_t *free_ip,
1722 xfs_trans_t *tp,
1723 xfs_ino_t inum)
1724{
1725 xfs_mount_t *mp = free_ip->i_mount;
1726 int blks_per_cluster;
1727 int nbufs;
1728 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001729 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 xfs_daddr_t blkno;
1731 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001732 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 xfs_inode_log_item_t *iip;
1734 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001735 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Dave Chinner5017e972010-01-11 11:47:40 +00001737 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1739 blks_per_cluster = 1;
1740 ninodes = mp->m_sb.sb_inopblock;
1741 nbufs = XFS_IALLOC_BLOCKS(mp);
1742 } else {
1743 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1744 mp->m_sb.sb_blocksize;
1745 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1746 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1747 }
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 for (j = 0; j < nbufs; j++, inum += ninodes) {
1750 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1751 XFS_INO_TO_AGBNO(mp, inum));
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001754 * We obtain and lock the backing buffer first in the process
1755 * here, as we have to ensure that any dirty inode that we
1756 * can't get the flush lock on is attached to the buffer.
1757 * If we scan the in-memory inodes first, then buffer IO can
1758 * complete before we get a lock on it, and hence we may fail
1759 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001761 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnera8acad72012-04-23 15:58:54 +10001762 mp->m_bsize * blks_per_cluster, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001764 if (!bp)
1765 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001766 /*
1767 * Walk the inodes already attached to the buffer and mark them
1768 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001769 * in-memory inode walk can't lock them. By marking them all
1770 * stale first, we will not attempt to lock them in the loop
1771 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001772 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001773 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 while (lip) {
1775 if (lip->li_type == XFS_LI_INODE) {
1776 iip = (xfs_inode_log_item_t *)lip;
1777 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001778 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001779 xfs_trans_ail_copy_lsn(mp->m_ail,
1780 &iip->ili_flush_lsn,
1781 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001782 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 lip = lip->li_bio_list;
1785 }
1786
Dave Chinner5b3eed72010-08-24 11:42:41 +10001787
Dave Chinner5b257b42010-06-03 16:22:29 +10001788 /*
1789 * For each inode in memory attempt to add it to the inode
1790 * buffer and set it up for being staled on buffer IO
1791 * completion. This is safe as we've locked out tail pushing
1792 * and flushing by locking the buffer.
1793 *
1794 * We have already marked every inode that was part of a
1795 * transaction stale above, which means there is no point in
1796 * even trying to lock them.
1797 */
1798 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001799retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001800 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001801 ip = radix_tree_lookup(&pag->pag_ici_root,
1802 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001804 /* Inode not in memory, nothing to do */
1805 if (!ip) {
1806 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001807 continue;
1808 }
1809
Dave Chinner5b3eed72010-08-24 11:42:41 +10001810 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001811 * because this is an RCU protected lookup, we could
1812 * find a recently freed or even reallocated inode
1813 * during the lookup. We need to check under the
1814 * i_flags_lock for a valid inode here. Skip it if it
1815 * is not valid, the wrong inode or stale.
1816 */
1817 spin_lock(&ip->i_flags_lock);
1818 if (ip->i_ino != inum + i ||
1819 __xfs_iflags_test(ip, XFS_ISTALE)) {
1820 spin_unlock(&ip->i_flags_lock);
1821 rcu_read_unlock();
1822 continue;
1823 }
1824 spin_unlock(&ip->i_flags_lock);
1825
1826 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001827 * Don't try to lock/unlock the current inode, but we
1828 * _cannot_ skip the other inodes that we did not find
1829 * in the list attached to the buffer and are not
1830 * already marked stale. If we can't lock it, back off
1831 * and retry.
1832 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001833 if (ip != free_ip &&
1834 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001835 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001836 delay(1);
1837 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001838 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001839 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001840
Dave Chinner5b3eed72010-08-24 11:42:41 +10001841 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001842 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001843
Dave Chinner5b3eed72010-08-24 11:42:41 +10001844 /*
1845 * we don't need to attach clean inodes or those only
1846 * with unlogged changes (which we throw away, anyway).
1847 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001848 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001849 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001850 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 xfs_ifunlock(ip);
1852 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1853 continue;
1854 }
1855
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001856 iip->ili_last_fields = iip->ili_fields;
1857 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001859 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1860 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001862 xfs_buf_attach_iodone(bp, xfs_istale_done,
1863 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001864
1865 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
Dave Chinner5b3eed72010-08-24 11:42:41 +10001869 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 xfs_trans_binval(tp, bp);
1871 }
1872
Dave Chinner5017e972010-01-11 11:47:40 +00001873 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/*
1878 * This is called to return an inode to the inode free list.
1879 * The inode should already be truncated to 0 length and have
1880 * no pages associated with it. This routine also assumes that
1881 * the inode is already a part of the transaction.
1882 *
1883 * The on-disk copy of the inode will have been added to the list
1884 * of unlinked inodes in the AGI. We need to remove the inode from
1885 * that list atomically with respect to freeing it here.
1886 */
1887int
1888xfs_ifree(
1889 xfs_trans_t *tp,
1890 xfs_inode_t *ip,
1891 xfs_bmap_free_t *flist)
1892{
1893 int error;
1894 int delete;
1895 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001896 xfs_dinode_t *dip;
1897 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001899 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 ASSERT(ip->i_d.di_nlink == 0);
1901 ASSERT(ip->i_d.di_nextents == 0);
1902 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001903 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 ASSERT(ip->i_d.di_nblocks == 0);
1905
1906 /*
1907 * Pull the on-disk inode from the AGI unlinked list.
1908 */
1909 error = xfs_iunlink_remove(tp, ip);
1910 if (error != 0) {
1911 return error;
1912 }
1913
1914 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1915 if (error != 0) {
1916 return error;
1917 }
1918 ip->i_d.di_mode = 0; /* mark incore inode as free */
1919 ip->i_d.di_flags = 0;
1920 ip->i_d.di_dmevmask = 0;
1921 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1923 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1924 /*
1925 * Bump the generation count so no one will be confused
1926 * by reincarnations of this inode.
1927 */
1928 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1931
Christoph Hellwig475ee412012-07-03 12:21:22 -04001932 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
1933 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001934 if (error)
1935 return error;
1936
1937 /*
1938 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1939 * from picking up this inode when it is reclaimed (its incore state
1940 * initialzed but not flushed to disk yet). The in-core di_mode is
1941 * already cleared and a corresponding transaction logged.
1942 * The hack here just synchronizes the in-core to on-disk
1943 * di_mode value in advance before the actual inode sync to disk.
1944 * This is OK because the inode is already unlinked and would never
1945 * change its di_mode again for this inode generation.
1946 * This is a temporary hack that would require a proper fix
1947 * in the future.
1948 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001949 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001952 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001955 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958/*
1959 * Reallocate the space for if_broot based on the number of records
1960 * being added or deleted as indicated in rec_diff. Move the records
1961 * and pointers in if_broot to fit the new size. When shrinking this
1962 * will eliminate holes between the records and pointers created by
1963 * the caller. When growing this will create holes to be filled in
1964 * by the caller.
1965 *
1966 * The caller must not request to add more records than would fit in
1967 * the on-disk inode root. If the if_broot is currently NULL, then
1968 * if we adding records one will be allocated. The caller must also
1969 * not request that the number of records go below zero, although
1970 * it can go to zero.
1971 *
1972 * ip -- the inode whose if_broot area is changing
1973 * ext_diff -- the change in the number of records, positive or negative,
1974 * requested for the if_broot array.
1975 */
1976void
1977xfs_iroot_realloc(
1978 xfs_inode_t *ip,
1979 int rec_diff,
1980 int whichfork)
1981{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001982 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int cur_max;
1984 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001985 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int new_max;
1987 size_t new_size;
1988 char *np;
1989 char *op;
1990
1991 /*
1992 * Handle the degenerate case quietly.
1993 */
1994 if (rec_diff == 0) {
1995 return;
1996 }
1997
1998 ifp = XFS_IFORK_PTR(ip, whichfork);
1999 if (rec_diff > 0) {
2000 /*
2001 * If there wasn't any memory allocated before, just
2002 * allocate it now and get out.
2003 */
2004 if (ifp->if_broot_bytes == 0) {
2005 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002006 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ifp->if_broot_bytes = (int)new_size;
2008 return;
2009 }
2010
2011 /*
2012 * If there is already an existing if_broot, then we need
2013 * to realloc() it and shift the pointers to their new
2014 * location. The records don't change location because
2015 * they are kept butted up against the btree block header.
2016 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002017 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 new_max = cur_max + rec_diff;
2019 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002020 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002022 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002023 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2024 ifp->if_broot_bytes);
2025 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2026 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 ifp->if_broot_bytes = (int)new_size;
2028 ASSERT(ifp->if_broot_bytes <=
2029 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2030 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2031 return;
2032 }
2033
2034 /*
2035 * rec_diff is less than 0. In this case, we are shrinking the
2036 * if_broot buffer. It must already exist. If we go to zero
2037 * records, just get rid of the root and clear the status bit.
2038 */
2039 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002040 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 new_max = cur_max + rec_diff;
2042 ASSERT(new_max >= 0);
2043 if (new_max > 0)
2044 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2045 else
2046 new_size = 0;
2047 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002048 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /*
2050 * First copy over the btree block header.
2051 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002052 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else {
2054 new_broot = NULL;
2055 ifp->if_flags &= ~XFS_IFBROOT;
2056 }
2057
2058 /*
2059 * Only copy the records and pointers if there are any.
2060 */
2061 if (new_max > 0) {
2062 /*
2063 * First copy the records.
2064 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11002065 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2066 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2068
2069 /*
2070 * Then copy the pointers.
2071 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11002072 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002074 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 (int)new_size);
2076 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2077 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002078 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 ifp->if_broot = new_broot;
2080 ifp->if_broot_bytes = (int)new_size;
2081 ASSERT(ifp->if_broot_bytes <=
2082 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2083 return;
2084}
2085
2086
2087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * This is called when the amount of space needed for if_data
2089 * is increased or decreased. The change in size is indicated by
2090 * the number of bytes that need to be added or deleted in the
2091 * byte_diff parameter.
2092 *
2093 * If the amount of space needed has decreased below the size of the
2094 * inline buffer, then switch to using the inline buffer. Otherwise,
2095 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2096 * to what is needed.
2097 *
2098 * ip -- the inode whose if_data area is changing
2099 * byte_diff -- the change in the number of bytes, positive or negative,
2100 * requested for the if_data array.
2101 */
2102void
2103xfs_idata_realloc(
2104 xfs_inode_t *ip,
2105 int byte_diff,
2106 int whichfork)
2107{
2108 xfs_ifork_t *ifp;
2109 int new_size;
2110 int real_size;
2111
2112 if (byte_diff == 0) {
2113 return;
2114 }
2115
2116 ifp = XFS_IFORK_PTR(ip, whichfork);
2117 new_size = (int)ifp->if_bytes + byte_diff;
2118 ASSERT(new_size >= 0);
2119
2120 if (new_size == 0) {
2121 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002122 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 ifp->if_u1.if_data = NULL;
2125 real_size = 0;
2126 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2127 /*
2128 * If the valid extents/data can fit in if_inline_ext/data,
2129 * copy them from the malloc'd vector and free it.
2130 */
2131 if (ifp->if_u1.if_data == NULL) {
2132 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2133 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2134 ASSERT(ifp->if_real_bytes != 0);
2135 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2136 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002137 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2139 }
2140 real_size = 0;
2141 } else {
2142 /*
2143 * Stuck with malloc/realloc.
2144 * For inline data, the underlying buffer must be
2145 * a multiple of 4 bytes in size so that it can be
2146 * logged and stay on word boundaries. We enforce
2147 * that here.
2148 */
2149 real_size = roundup(new_size, 4);
2150 if (ifp->if_u1.if_data == NULL) {
2151 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002152 ifp->if_u1.if_data = kmem_alloc(real_size,
2153 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2155 /*
2156 * Only do the realloc if the underlying size
2157 * is really changing.
2158 */
2159 if (ifp->if_real_bytes != real_size) {
2160 ifp->if_u1.if_data =
2161 kmem_realloc(ifp->if_u1.if_data,
2162 real_size,
2163 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002164 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 } else {
2167 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10002168 ifp->if_u1.if_data = kmem_alloc(real_size,
2169 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2171 ifp->if_bytes);
2172 }
2173 }
2174 ifp->if_real_bytes = real_size;
2175 ifp->if_bytes = new_size;
2176 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179void
2180xfs_idestroy_fork(
2181 xfs_inode_t *ip,
2182 int whichfork)
2183{
2184 xfs_ifork_t *ifp;
2185
2186 ifp = XFS_IFORK_PTR(ip, whichfork);
2187 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002188 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 ifp->if_broot = NULL;
2190 }
2191
2192 /*
2193 * If the format is local, then we can't have an extents
2194 * array so just look for an inline data array. If we're
2195 * not local then we may or may not have an extents list,
2196 * so check and free it up if we do.
2197 */
2198 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2199 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2200 (ifp->if_u1.if_data != NULL)) {
2201 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002202 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 ifp->if_u1.if_data = NULL;
2204 ifp->if_real_bytes = 0;
2205 }
2206 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002207 ((ifp->if_flags & XFS_IFEXTIREC) ||
2208 ((ifp->if_u1.if_extents != NULL) &&
2209 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002211 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213 ASSERT(ifp->if_u1.if_extents == NULL ||
2214 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2215 ASSERT(ifp->if_real_bytes == 0);
2216 if (whichfork == XFS_ATTR_FORK) {
2217 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2218 ip->i_afp = NULL;
2219 }
2220}
2221
2222/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002223 * This is called to unpin an inode. The caller must have the inode locked
2224 * in at least shared mode so that the buffer cannot be subsequently pinned
2225 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002227static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002228xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002229 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002230{
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10002231 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002232
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002233 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2234
David Chinnera3f74ff2008-03-06 13:43:42 +11002235 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002236 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002237
David Chinnera3f74ff2008-03-06 13:43:42 +11002238}
2239
Christoph Hellwigf392e632011-12-18 20:00:10 +00002240static void
2241__xfs_iunpin_wait(
2242 struct xfs_inode *ip)
2243{
2244 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2245 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2246
2247 xfs_iunpin(ip);
2248
2249 do {
2250 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2251 if (xfs_ipincount(ip))
2252 io_schedule();
2253 } while (xfs_ipincount(ip));
2254 finish_wait(wq, &wait.wait);
2255}
2256
Dave Chinner777df5a2010-02-06 12:37:26 +11002257void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002259 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002261 if (xfs_ipincount(ip))
2262 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265/*
2266 * xfs_iextents_copy()
2267 *
2268 * This is called to copy the REAL extents (as opposed to the delayed
2269 * allocation extents) from the inode into the given buffer. It
2270 * returns the number of bytes copied into the buffer.
2271 *
2272 * If there are no delayed allocation extents, then we can just
2273 * memcpy() the extents into the buffer. Otherwise, we need to
2274 * examine each extent in turn and skip those which are delayed.
2275 */
2276int
2277xfs_iextents_copy(
2278 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002279 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 int whichfork)
2281{
2282 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 int i;
2284 xfs_ifork_t *ifp;
2285 int nrecs;
2286 xfs_fsblock_t start_block;
2287
2288 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10002289 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 ASSERT(ifp->if_bytes > 0);
2291
2292 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002293 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 ASSERT(nrecs > 0);
2295
2296 /*
2297 * There are some delayed allocation extents in the
2298 * inode, so copy the extents one at a time and skip
2299 * the delayed ones. There must be at least one
2300 * non-delayed extent.
2301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 copied = 0;
2303 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002304 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002306 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * It's a delayed allocation extent, so skip it.
2309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 continue;
2311 }
2312
2313 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002314 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2315 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002316 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 copied++;
2318 }
2319 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002320 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2323}
2324
2325/*
2326 * Each of the following cases stores data into the same region
2327 * of the on-disk inode, so only one of them can be valid at
2328 * any given time. While it is possible to have conflicting formats
2329 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2330 * in EXTENTS format, this can only happen when the fork has
2331 * changed formats after being modified but before being flushed.
2332 * In these cases, the format always takes precedence, because the
2333 * format indicates the current state of the fork.
2334 */
2335/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002336STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337xfs_iflush_fork(
2338 xfs_inode_t *ip,
2339 xfs_dinode_t *dip,
2340 xfs_inode_log_item_t *iip,
2341 int whichfork,
2342 xfs_buf_t *bp)
2343{
2344 char *cp;
2345 xfs_ifork_t *ifp;
2346 xfs_mount_t *mp;
2347#ifdef XFS_TRANS_DEBUG
2348 int first;
2349#endif
2350 static const short brootflag[2] =
2351 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2352 static const short dataflag[2] =
2353 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2354 static const short extflag[2] =
2355 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2356
David Chinnere4ac9672008-04-10 12:23:58 +10002357 if (!iip)
2358 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 ifp = XFS_IFORK_PTR(ip, whichfork);
2360 /*
2361 * This can happen if we gave up in iformat in an error path,
2362 * for the attribute fork.
2363 */
David Chinnere4ac9672008-04-10 12:23:58 +10002364 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002366 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 cp = XFS_DFORK_PTR(dip, whichfork);
2369 mp = ip->i_mount;
2370 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2371 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002372 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 (ifp->if_bytes > 0)) {
2374 ASSERT(ifp->if_u1.if_data != NULL);
2375 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2376 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 break;
2379
2380 case XFS_DINODE_FMT_EXTENTS:
2381 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002382 !(iip->ili_fields & extflag[whichfork]));
2383 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002385 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2387 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2388 whichfork);
2389 }
2390 break;
2391
2392 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002393 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 (ifp->if_broot_bytes > 0)) {
2395 ASSERT(ifp->if_broot != NULL);
2396 ASSERT(ifp->if_broot_bytes <=
2397 (XFS_IFORK_SIZE(ip, whichfork) +
2398 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002399 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 (xfs_bmdr_block_t *)cp,
2401 XFS_DFORK_SIZE(dip, mp, whichfork));
2402 }
2403 break;
2404
2405 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002406 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002408 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 break;
2411
2412 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002413 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002415 memcpy(XFS_DFORK_DPTR(dip),
2416 &ip->i_df.if_u2.if_uuid,
2417 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419 break;
2420
2421 default:
2422 ASSERT(0);
2423 break;
2424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
David Chinnerbad55842008-03-06 13:43:49 +11002427STATIC int
2428xfs_iflush_cluster(
2429 xfs_inode_t *ip,
2430 xfs_buf_t *bp)
2431{
2432 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002433 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002434 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002435 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002436 int ilist_size;
2437 xfs_inode_t **ilist;
2438 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002439 int nr_found;
2440 int clcount = 0;
2441 int bufwasdelwri;
2442 int i;
2443
Dave Chinner5017e972010-01-11 11:47:40 +00002444 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002445
David Chinnerc8f5f122008-05-20 11:30:15 +10002446 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2447 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002448 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002449 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002450 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002451
2452 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2453 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002454 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002455 /* really need a gang lookup range call here */
2456 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002457 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002458 if (nr_found == 0)
2459 goto out_free;
2460
2461 for (i = 0; i < nr_found; i++) {
2462 iq = ilist[i];
2463 if (iq == ip)
2464 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002465
2466 /*
2467 * because this is an RCU protected lookup, we could find a
2468 * recently freed or even reallocated inode during the lookup.
2469 * We need to check under the i_flags_lock for a valid inode
2470 * here. Skip it if it is not valid or the wrong inode.
2471 */
2472 spin_lock(&ip->i_flags_lock);
2473 if (!ip->i_ino ||
2474 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2475 spin_unlock(&ip->i_flags_lock);
2476 continue;
2477 }
2478 spin_unlock(&ip->i_flags_lock);
2479
David Chinnerbad55842008-03-06 13:43:49 +11002480 /*
2481 * Do an un-protected check to see if the inode is dirty and
2482 * is a candidate for flushing. These checks will be repeated
2483 * later after the appropriate locks are acquired.
2484 */
David Chinner33540402008-03-06 13:43:59 +11002485 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002486 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002487
2488 /*
2489 * Try to get locks. If any are unavailable or it is pinned,
2490 * then this inode cannot be flushed and is skipped.
2491 */
2492
2493 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2494 continue;
2495 if (!xfs_iflock_nowait(iq)) {
2496 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2497 continue;
2498 }
2499 if (xfs_ipincount(iq)) {
2500 xfs_ifunlock(iq);
2501 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2502 continue;
2503 }
2504
2505 /*
2506 * arriving here means that this inode can be flushed. First
2507 * re-check that it's dirty before flushing.
2508 */
David Chinner33540402008-03-06 13:43:59 +11002509 if (!xfs_inode_clean(iq)) {
2510 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002511 error = xfs_iflush_int(iq, bp);
2512 if (error) {
2513 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2514 goto cluster_corrupt_out;
2515 }
2516 clcount++;
2517 } else {
2518 xfs_ifunlock(iq);
2519 }
2520 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2521 }
2522
2523 if (clcount) {
2524 XFS_STATS_INC(xs_icluster_flushcnt);
2525 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2526 }
2527
2528out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002529 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002530 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002531out_put:
2532 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002533 return 0;
2534
2535
2536cluster_corrupt_out:
2537 /*
2538 * Corruption detected in the clustering loop. Invalidate the
2539 * inode buffer and shut down the filesystem.
2540 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002541 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002542 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002543 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002544 * brelse can handle it with no problems. If not, shut down the
2545 * filesystem before releasing the buffer.
2546 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002547 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002548 if (bufwasdelwri)
2549 xfs_buf_relse(bp);
2550
2551 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2552
2553 if (!bufwasdelwri) {
2554 /*
2555 * Just like incore_relse: if we have b_iodone functions,
2556 * mark the buffer as an error and call them. Otherwise
2557 * mark it as stale and brelse.
2558 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002559 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002560 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002561 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002562 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002563 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002564 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002565 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002566 xfs_buf_relse(bp);
2567 }
2568 }
2569
2570 /*
2571 * Unlocks the flush lock
2572 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002573 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002574 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002575 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002576 return XFS_ERROR(EFSCORRUPTED);
2577}
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002580 * Flush dirty inode metadata into the backing buffer.
2581 *
2582 * The caller must have the inode lock and the inode flush lock held. The
2583 * inode lock will still be held upon return to the caller, and the inode
2584 * flush lock will be released after the inode has reached the disk.
2585 *
2586 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
2588int
2589xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002590 struct xfs_inode *ip,
2591 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002593 struct xfs_mount *mp = ip->i_mount;
2594 struct xfs_buf *bp;
2595 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 XFS_STATS_INC(xs_iflush_count);
2599
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10002600 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002601 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002603 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Christoph Hellwig4c468192012-04-23 15:58:36 +10002605 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 xfs_iunpin_wait(ip);
2608
2609 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002610 * For stale inodes we cannot rely on the backing buffer remaining
2611 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002612 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002613 * inodes below. We have to check this after ensuring the inode is
2614 * unpinned so that it is safe to reclaim the stale inode after the
2615 * flush call.
2616 */
2617 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2618 xfs_ifunlock(ip);
2619 return 0;
2620 }
2621
2622 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 * This may have been unpinned because the filesystem is shutting
2624 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002625 * to disk, because the log record didn't make it to disk.
2626 *
2627 * We also have to remove the log item from the AIL in this case,
2628 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 */
2630 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002631 error = XFS_ERROR(EIO);
2632 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634
2635 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002636 * Get the buffer containing the on-disk inode.
2637 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002638 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2639 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002640 if (error || !bp) {
2641 xfs_ifunlock(ip);
2642 return error;
2643 }
2644
2645 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * First flush out the inode that xfs_iflush was called with.
2647 */
2648 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002649 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002653 * If the buffer is pinned then push on the log now so we won't
2654 * get stuck waiting in the write for too long.
2655 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002656 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002657 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002658
2659 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * inode clustering:
2661 * see if other inodes can be gathered into this write
2662 */
David Chinnerbad55842008-03-06 13:43:49 +11002663 error = xfs_iflush_cluster(ip, bp);
2664 if (error)
2665 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Christoph Hellwig4c468192012-04-23 15:58:36 +10002667 *bpp = bp;
2668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670corrupt_out:
2671 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002672 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002674 error = XFS_ERROR(EFSCORRUPTED);
2675abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /*
2677 * Unlocks the flush lock
2678 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002679 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
2683
2684STATIC int
2685xfs_iflush_int(
2686 xfs_inode_t *ip,
2687 xfs_buf_t *bp)
2688{
2689 xfs_inode_log_item_t *iip;
2690 xfs_dinode_t *dip;
2691 xfs_mount_t *mp;
2692#ifdef XFS_TRANS_DEBUG
2693 int first;
2694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10002696 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002697 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002699 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 iip = ip->i_itemp;
2702 mp = ip->i_mount;
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002705 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002707 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002709 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2710 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2711 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 goto corrupt_out;
2713 }
2714 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2715 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002716 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2717 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2718 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 goto corrupt_out;
2720 }
Al Viroabbede12011-07-26 02:31:30 -04002721 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (XFS_TEST_ERROR(
2723 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2724 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2725 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002726 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2727 "%s: Bad regular inode %Lu, ptr 0x%p",
2728 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 goto corrupt_out;
2730 }
Al Viroabbede12011-07-26 02:31:30 -04002731 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (XFS_TEST_ERROR(
2733 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2734 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2735 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2736 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002737 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2738 "%s: Bad directory inode %Lu, ptr 0x%p",
2739 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 goto corrupt_out;
2741 }
2742 }
2743 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2744 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2745 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002746 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2747 "%s: detected corrupt incore inode %Lu, "
2748 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2749 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002751 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 goto corrupt_out;
2753 }
2754 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2755 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002756 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2757 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2758 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 goto corrupt_out;
2760 }
2761 /*
2762 * bump the flush iteration count, used to detect flushes which
2763 * postdate a log record during recovery.
2764 */
2765
2766 ip->i_d.di_flushiter++;
2767
2768 /*
2769 * Copy the dirty parts of the inode into the on-disk
2770 * inode. We always copy out the core of the inode,
2771 * because if the inode is dirty at all the core must
2772 * be.
2773 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002774 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2777 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2778 ip->i_d.di_flushiter = 0;
2779
2780 /*
2781 * If this is really an old format inode and the superblock version
2782 * has not been updated to support only new format inodes, then
2783 * convert back to the old inode format. If the superblock version
2784 * has been updated, then make the conversion permanent.
2785 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002786 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2787 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002788 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * Convert it back.
2791 */
2792 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002793 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 } else {
2795 /*
2796 * The superblock version has already been bumped,
2797 * so just make the conversion to the new inode
2798 * format permanent.
2799 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002800 ip->i_d.di_version = 2;
2801 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002803 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002805 memset(&(dip->di_pad[0]), 0,
2806 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002807 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
2809 }
2810
David Chinnere4ac9672008-04-10 12:23:58 +10002811 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2812 if (XFS_IFORK_Q(ip))
2813 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 xfs_inobp_check(mp, bp);
2815
2816 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002817 * We've recorded everything logged in the inode, so we'd like to clear
2818 * the ili_fields bits so we don't log and flush things unnecessarily.
2819 * However, we can't stop logging all this information until the data
2820 * we've copied into the disk buffer is written to disk. If we did we
2821 * might overwrite the copy of the inode in the log with all the data
2822 * after re-logging only part of it, and in the face of a crash we
2823 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002825 * What we do is move the bits to the ili_last_fields field. When
2826 * logging the inode, these bits are moved back to the ili_fields field.
2827 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2828 * know that the information those bits represent is permanently on
2829 * disk. As long as the flush completes before the inode is logged
2830 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002832 * We can play with the ili_fields bits here, because the inode lock
2833 * must be held exclusively in order to set bits there and the flush
2834 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2835 * done routine can tell whether or not to look in the AIL. Also, store
2836 * the current LSN of the inode so that we can tell whether the item has
2837 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2838 * need the AIL lock, because it is a 64 bit value that cannot be read
2839 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002841 if (iip != NULL && iip->ili_fields != 0) {
2842 iip->ili_last_fields = iip->ili_fields;
2843 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 iip->ili_logged = 1;
2845
David Chinner7b2e2a32008-10-30 17:39:12 +11002846 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2847 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /*
2850 * Attach the function xfs_iflush_done to the inode's
2851 * buffer. This will remove the inode from the AIL
2852 * and unlock the inode's flush lock when the inode is
2853 * completely written to disk.
2854 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002855 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002857 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002858 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 } else {
2860 /*
2861 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002862 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 * the inode flush lock because we can avoid the whole
2864 * AIL state thing. It's OK to drop the flush lock now,
2865 * because we've already locked the buffer and to do anything
2866 * you really need both.
2867 */
2868 if (iip != NULL) {
2869 ASSERT(iip->ili_logged == 0);
2870 ASSERT(iip->ili_last_fields == 0);
2871 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2872 }
2873 xfs_ifunlock(ip);
2874 }
2875
2876 return 0;
2877
2878corrupt_out:
2879 return XFS_ERROR(EFSCORRUPTED);
2880}
2881
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002882/*
2883 * Return a pointer to the extent record at file index idx.
2884 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002885xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002886xfs_iext_get_ext(
2887 xfs_ifork_t *ifp, /* inode fork pointer */
2888 xfs_extnum_t idx) /* index of target extent */
2889{
2890 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002891 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2892
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002893 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2894 return ifp->if_u1.if_ext_irec->er_extbuf;
2895 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2896 xfs_ext_irec_t *erp; /* irec pointer */
2897 int erp_idx = 0; /* irec index */
2898 xfs_extnum_t page_idx = idx; /* ext index in target list */
2899
2900 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2901 return &erp->er_extbuf[page_idx];
2902 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002903 return &ifp->if_u1.if_extents[idx];
2904 } else {
2905 return NULL;
2906 }
2907}
2908
2909/*
2910 * Insert new item(s) into the extent records for incore inode
2911 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2912 */
2913void
2914xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002915 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002916 xfs_extnum_t idx, /* starting index of new items */
2917 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002918 xfs_bmbt_irec_t *new, /* items to insert */
2919 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002920{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002921 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002922 xfs_extnum_t i; /* extent record index */
2923
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002924 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2925
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002926 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2927 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002928 for (i = idx; i < idx + count; i++, new++)
2929 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002930}
2931
2932/*
2933 * This is called when the amount of space required for incore file
2934 * extents needs to be increased. The ext_diff parameter stores the
2935 * number of new extents being added and the idx parameter contains
2936 * the extent index where the new extents will be added. If the new
2937 * extents are being appended, then we just need to (re)allocate and
2938 * initialize the space. Otherwise, if the new extents are being
2939 * inserted into the middle of the existing entries, a bit more work
2940 * is required to make room for the new extents to be inserted. The
2941 * caller is responsible for filling in the new extent entries upon
2942 * return.
2943 */
2944void
2945xfs_iext_add(
2946 xfs_ifork_t *ifp, /* inode fork pointer */
2947 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002948 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002949{
2950 int byte_diff; /* new bytes being added */
2951 int new_size; /* size of extents after adding */
2952 xfs_extnum_t nextents; /* number of extents in file */
2953
2954 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2955 ASSERT((idx >= 0) && (idx <= nextents));
2956 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2957 new_size = ifp->if_bytes + byte_diff;
2958 /*
2959 * If the new number of extents (nextents + ext_diff)
2960 * fits inside the inode, then continue to use the inline
2961 * extent buffer.
2962 */
2963 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2964 if (idx < nextents) {
2965 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2966 &ifp->if_u2.if_inline_ext[idx],
2967 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2968 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2969 }
2970 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2971 ifp->if_real_bytes = 0;
2972 }
2973 /*
2974 * Otherwise use a linear (direct) extent list.
2975 * If the extents are currently inside the inode,
2976 * xfs_iext_realloc_direct will switch us from
2977 * inline to direct extent allocation mode.
2978 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002979 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002980 xfs_iext_realloc_direct(ifp, new_size);
2981 if (idx < nextents) {
2982 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2983 &ifp->if_u1.if_extents[idx],
2984 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2985 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2986 }
2987 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002988 /* Indirection array */
2989 else {
2990 xfs_ext_irec_t *erp;
2991 int erp_idx = 0;
2992 int page_idx = idx;
2993
2994 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2995 if (ifp->if_flags & XFS_IFEXTIREC) {
2996 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2997 } else {
2998 xfs_iext_irec_init(ifp);
2999 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3000 erp = ifp->if_u1.if_ext_irec;
3001 }
3002 /* Extents fit in target extent page */
3003 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3004 if (page_idx < erp->er_extcount) {
3005 memmove(&erp->er_extbuf[page_idx + ext_diff],
3006 &erp->er_extbuf[page_idx],
3007 (erp->er_extcount - page_idx) *
3008 sizeof(xfs_bmbt_rec_t));
3009 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3010 }
3011 erp->er_extcount += ext_diff;
3012 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3013 }
3014 /* Insert a new extent page */
3015 else if (erp) {
3016 xfs_iext_add_indirect_multi(ifp,
3017 erp_idx, page_idx, ext_diff);
3018 }
3019 /*
3020 * If extent(s) are being appended to the last page in
3021 * the indirection array and the new extent(s) don't fit
3022 * in the page, then erp is NULL and erp_idx is set to
3023 * the next index needed in the indirection array.
3024 */
3025 else {
3026 int count = ext_diff;
3027
3028 while (count) {
3029 erp = xfs_iext_irec_new(ifp, erp_idx);
3030 erp->er_extcount = count;
3031 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3032 if (count) {
3033 erp_idx++;
3034 }
3035 }
3036 }
3037 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003038 ifp->if_bytes = new_size;
3039}
3040
3041/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003042 * This is called when incore extents are being added to the indirection
3043 * array and the new extents do not fit in the target extent list. The
3044 * erp_idx parameter contains the irec index for the target extent list
3045 * in the indirection array, and the idx parameter contains the extent
3046 * index within the list. The number of extents being added is stored
3047 * in the count parameter.
3048 *
3049 * |-------| |-------|
3050 * | | | | idx - number of extents before idx
3051 * | idx | | count |
3052 * | | | | count - number of extents being inserted at idx
3053 * |-------| |-------|
3054 * | count | | nex2 | nex2 - number of extents after idx + count
3055 * |-------| |-------|
3056 */
3057void
3058xfs_iext_add_indirect_multi(
3059 xfs_ifork_t *ifp, /* inode fork pointer */
3060 int erp_idx, /* target extent irec index */
3061 xfs_extnum_t idx, /* index within target list */
3062 int count) /* new extents being added */
3063{
3064 int byte_diff; /* new bytes being added */
3065 xfs_ext_irec_t *erp; /* pointer to irec entry */
3066 xfs_extnum_t ext_diff; /* number of extents to add */
3067 xfs_extnum_t ext_cnt; /* new extents still needed */
3068 xfs_extnum_t nex2; /* extents after idx + count */
3069 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3070 int nlists; /* number of irec's (lists) */
3071
3072 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3073 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3074 nex2 = erp->er_extcount - idx;
3075 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3076
3077 /*
3078 * Save second part of target extent list
3079 * (all extents past */
3080 if (nex2) {
3081 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10003082 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003083 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3084 erp->er_extcount -= nex2;
3085 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3086 memset(&erp->er_extbuf[idx], 0, byte_diff);
3087 }
3088
3089 /*
3090 * Add the new extents to the end of the target
3091 * list, then allocate new irec record(s) and
3092 * extent buffer(s) as needed to store the rest
3093 * of the new extents.
3094 */
3095 ext_cnt = count;
3096 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3097 if (ext_diff) {
3098 erp->er_extcount += ext_diff;
3099 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3100 ext_cnt -= ext_diff;
3101 }
3102 while (ext_cnt) {
3103 erp_idx++;
3104 erp = xfs_iext_irec_new(ifp, erp_idx);
3105 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3106 erp->er_extcount = ext_diff;
3107 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3108 ext_cnt -= ext_diff;
3109 }
3110
3111 /* Add nex2 extents back to indirection array */
3112 if (nex2) {
3113 xfs_extnum_t ext_avail;
3114 int i;
3115
3116 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3117 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3118 i = 0;
3119 /*
3120 * If nex2 extents fit in the current page, append
3121 * nex2_ep after the new extents.
3122 */
3123 if (nex2 <= ext_avail) {
3124 i = erp->er_extcount;
3125 }
3126 /*
3127 * Otherwise, check if space is available in the
3128 * next page.
3129 */
3130 else if ((erp_idx < nlists - 1) &&
3131 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3132 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3133 erp_idx++;
3134 erp++;
3135 /* Create a hole for nex2 extents */
3136 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3137 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3138 }
3139 /*
3140 * Final choice, create a new extent page for
3141 * nex2 extents.
3142 */
3143 else {
3144 erp_idx++;
3145 erp = xfs_iext_irec_new(ifp, erp_idx);
3146 }
3147 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003148 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003149 erp->er_extcount += nex2;
3150 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3151 }
3152}
3153
3154/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003155 * This is called when the amount of space required for incore file
3156 * extents needs to be decreased. The ext_diff parameter stores the
3157 * number of extents to be removed and the idx parameter contains
3158 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003159 *
3160 * If the amount of space needed has decreased below the linear
3161 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3162 * extent array. Otherwise, use kmem_realloc() to adjust the
3163 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003164 */
3165void
3166xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003167 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003168 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003169 int ext_diff, /* number of extents to remove */
3170 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003171{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003172 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003173 xfs_extnum_t nextents; /* number of extents in file */
3174 int new_size; /* size of extents after removal */
3175
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003176 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3177
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003178 ASSERT(ext_diff > 0);
3179 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3180 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3181
3182 if (new_size == 0) {
3183 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003184 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3185 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003186 } else if (ifp->if_real_bytes) {
3187 xfs_iext_remove_direct(ifp, idx, ext_diff);
3188 } else {
3189 xfs_iext_remove_inline(ifp, idx, ext_diff);
3190 }
3191 ifp->if_bytes = new_size;
3192}
3193
3194/*
3195 * This removes ext_diff extents from the inline buffer, beginning
3196 * at extent index idx.
3197 */
3198void
3199xfs_iext_remove_inline(
3200 xfs_ifork_t *ifp, /* inode fork pointer */
3201 xfs_extnum_t idx, /* index to begin removing exts */
3202 int ext_diff) /* number of extents to remove */
3203{
3204 int nextents; /* number of extents in file */
3205
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003206 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003207 ASSERT(idx < XFS_INLINE_EXTS);
3208 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3209 ASSERT(((nextents - ext_diff) > 0) &&
3210 (nextents - ext_diff) < XFS_INLINE_EXTS);
3211
3212 if (idx + ext_diff < nextents) {
3213 memmove(&ifp->if_u2.if_inline_ext[idx],
3214 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3215 (nextents - (idx + ext_diff)) *
3216 sizeof(xfs_bmbt_rec_t));
3217 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3218 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3219 } else {
3220 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3221 ext_diff * sizeof(xfs_bmbt_rec_t));
3222 }
3223}
3224
3225/*
3226 * This removes ext_diff extents from a linear (direct) extent list,
3227 * beginning at extent index idx. If the extents are being removed
3228 * from the end of the list (ie. truncate) then we just need to re-
3229 * allocate the list to remove the extra space. Otherwise, if the
3230 * extents are being removed from the middle of the existing extent
3231 * entries, then we first need to move the extent records beginning
3232 * at idx + ext_diff up in the list to overwrite the records being
3233 * removed, then remove the extra space via kmem_realloc.
3234 */
3235void
3236xfs_iext_remove_direct(
3237 xfs_ifork_t *ifp, /* inode fork pointer */
3238 xfs_extnum_t idx, /* index to begin removing exts */
3239 int ext_diff) /* number of extents to remove */
3240{
3241 xfs_extnum_t nextents; /* number of extents in file */
3242 int new_size; /* size of extents after removal */
3243
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003244 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003245 new_size = ifp->if_bytes -
3246 (ext_diff * sizeof(xfs_bmbt_rec_t));
3247 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3248
3249 if (new_size == 0) {
3250 xfs_iext_destroy(ifp);
3251 return;
3252 }
3253 /* Move extents up in the list (if needed) */
3254 if (idx + ext_diff < nextents) {
3255 memmove(&ifp->if_u1.if_extents[idx],
3256 &ifp->if_u1.if_extents[idx + ext_diff],
3257 (nextents - (idx + ext_diff)) *
3258 sizeof(xfs_bmbt_rec_t));
3259 }
3260 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3261 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3262 /*
3263 * Reallocate the direct extent list. If the extents
3264 * will fit inside the inode then xfs_iext_realloc_direct
3265 * will switch from direct to inline extent allocation
3266 * mode for us.
3267 */
3268 xfs_iext_realloc_direct(ifp, new_size);
3269 ifp->if_bytes = new_size;
3270}
3271
3272/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003273 * This is called when incore extents are being removed from the
3274 * indirection array and the extents being removed span multiple extent
3275 * buffers. The idx parameter contains the file extent index where we
3276 * want to begin removing extents, and the count parameter contains
3277 * how many extents need to be removed.
3278 *
3279 * |-------| |-------|
3280 * | nex1 | | | nex1 - number of extents before idx
3281 * |-------| | count |
3282 * | | | | count - number of extents being removed at idx
3283 * | count | |-------|
3284 * | | | nex2 | nex2 - number of extents after idx + count
3285 * |-------| |-------|
3286 */
3287void
3288xfs_iext_remove_indirect(
3289 xfs_ifork_t *ifp, /* inode fork pointer */
3290 xfs_extnum_t idx, /* index to begin removing extents */
3291 int count) /* number of extents to remove */
3292{
3293 xfs_ext_irec_t *erp; /* indirection array pointer */
3294 int erp_idx = 0; /* indirection array index */
3295 xfs_extnum_t ext_cnt; /* extents left to remove */
3296 xfs_extnum_t ext_diff; /* extents to remove in current list */
3297 xfs_extnum_t nex1; /* number of extents before idx */
3298 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003299 int page_idx = idx; /* index in target extent list */
3300
3301 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3302 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3303 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003304 nex1 = page_idx;
3305 ext_cnt = count;
3306 while (ext_cnt) {
3307 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3308 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3309 /*
3310 * Check for deletion of entire list;
3311 * xfs_iext_irec_remove() updates extent offsets.
3312 */
3313 if (ext_diff == erp->er_extcount) {
3314 xfs_iext_irec_remove(ifp, erp_idx);
3315 ext_cnt -= ext_diff;
3316 nex1 = 0;
3317 if (ext_cnt) {
3318 ASSERT(erp_idx < ifp->if_real_bytes /
3319 XFS_IEXT_BUFSZ);
3320 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3321 nex1 = 0;
3322 continue;
3323 } else {
3324 break;
3325 }
3326 }
3327 /* Move extents up (if needed) */
3328 if (nex2) {
3329 memmove(&erp->er_extbuf[nex1],
3330 &erp->er_extbuf[nex1 + ext_diff],
3331 nex2 * sizeof(xfs_bmbt_rec_t));
3332 }
3333 /* Zero out rest of page */
3334 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3335 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3336 /* Update remaining counters */
3337 erp->er_extcount -= ext_diff;
3338 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3339 ext_cnt -= ext_diff;
3340 nex1 = 0;
3341 erp_idx++;
3342 erp++;
3343 }
3344 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3345 xfs_iext_irec_compact(ifp);
3346}
3347
3348/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003349 * Create, destroy, or resize a linear (direct) block of extents.
3350 */
3351void
3352xfs_iext_realloc_direct(
3353 xfs_ifork_t *ifp, /* inode fork pointer */
3354 int new_size) /* new size of extents */
3355{
3356 int rnew_size; /* real new size of extents */
3357
3358 rnew_size = new_size;
3359
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003360 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3361 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3362 (new_size != ifp->if_real_bytes)));
3363
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003364 /* Free extent records */
3365 if (new_size == 0) {
3366 xfs_iext_destroy(ifp);
3367 }
3368 /* Resize direct extent list and zero any new bytes */
3369 else if (ifp->if_real_bytes) {
3370 /* Check if extents will fit inside the inode */
3371 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3372 xfs_iext_direct_to_inline(ifp, new_size /
3373 (uint)sizeof(xfs_bmbt_rec_t));
3374 ifp->if_bytes = new_size;
3375 return;
3376 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003377 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003378 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003379 }
3380 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003381 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003382 kmem_realloc(ifp->if_u1.if_extents,
3383 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003384 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003385 }
3386 if (rnew_size > ifp->if_real_bytes) {
3387 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3388 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3389 rnew_size - ifp->if_real_bytes);
3390 }
3391 }
3392 /*
3393 * Switch from the inline extent buffer to a direct
3394 * extent list. Be sure to include the inline extent
3395 * bytes in new_size.
3396 */
3397 else {
3398 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003399 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003400 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003401 }
3402 xfs_iext_inline_to_direct(ifp, rnew_size);
3403 }
3404 ifp->if_real_bytes = rnew_size;
3405 ifp->if_bytes = new_size;
3406}
3407
3408/*
3409 * Switch from linear (direct) extent records to inline buffer.
3410 */
3411void
3412xfs_iext_direct_to_inline(
3413 xfs_ifork_t *ifp, /* inode fork pointer */
3414 xfs_extnum_t nextents) /* number of extents in file */
3415{
3416 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3417 ASSERT(nextents <= XFS_INLINE_EXTS);
3418 /*
3419 * The inline buffer was zeroed when we switched
3420 * from inline to direct extent allocation mode,
3421 * so we don't need to clear it here.
3422 */
3423 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3424 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003425 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003426 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3427 ifp->if_real_bytes = 0;
3428}
3429
3430/*
3431 * Switch from inline buffer to linear (direct) extent records.
3432 * new_size should already be rounded up to the next power of 2
3433 * by the caller (when appropriate), so use new_size as it is.
3434 * However, since new_size may be rounded up, we can't update
3435 * if_bytes here. It is the caller's responsibility to update
3436 * if_bytes upon return.
3437 */
3438void
3439xfs_iext_inline_to_direct(
3440 xfs_ifork_t *ifp, /* inode fork pointer */
3441 int new_size) /* number of extents in file */
3442{
David Chinner67850732008-08-13 16:02:51 +10003443 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003444 memset(ifp->if_u1.if_extents, 0, new_size);
3445 if (ifp->if_bytes) {
3446 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3447 ifp->if_bytes);
3448 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3449 sizeof(xfs_bmbt_rec_t));
3450 }
3451 ifp->if_real_bytes = new_size;
3452}
3453
3454/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003455 * Resize an extent indirection array to new_size bytes.
3456 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003457STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003458xfs_iext_realloc_indirect(
3459 xfs_ifork_t *ifp, /* inode fork pointer */
3460 int new_size) /* new indirection array size */
3461{
3462 int nlists; /* number of irec's (ex lists) */
3463 int size; /* current indirection array size */
3464
3465 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3466 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3467 size = nlists * sizeof(xfs_ext_irec_t);
3468 ASSERT(ifp->if_real_bytes);
3469 ASSERT((new_size >= 0) && (new_size != size));
3470 if (new_size == 0) {
3471 xfs_iext_destroy(ifp);
3472 } else {
3473 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3474 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003475 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003476 }
3477}
3478
3479/*
3480 * Switch from indirection array to linear (direct) extent allocations.
3481 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003482STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003483xfs_iext_indirect_to_direct(
3484 xfs_ifork_t *ifp) /* inode fork pointer */
3485{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003486 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003487 xfs_extnum_t nextents; /* number of extents in file */
3488 int size; /* size of file extents */
3489
3490 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3491 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3492 ASSERT(nextents <= XFS_LINEAR_EXTS);
3493 size = nextents * sizeof(xfs_bmbt_rec_t);
3494
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003495 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003496 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3497
3498 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003499 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003500 ifp->if_flags &= ~XFS_IFEXTIREC;
3501 ifp->if_u1.if_extents = ep;
3502 ifp->if_bytes = size;
3503 if (nextents < XFS_LINEAR_EXTS) {
3504 xfs_iext_realloc_direct(ifp, size);
3505 }
3506}
3507
3508/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003509 * Free incore file extents.
3510 */
3511void
3512xfs_iext_destroy(
3513 xfs_ifork_t *ifp) /* inode fork pointer */
3514{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003515 if (ifp->if_flags & XFS_IFEXTIREC) {
3516 int erp_idx;
3517 int nlists;
3518
3519 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3520 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3521 xfs_iext_irec_remove(ifp, erp_idx);
3522 }
3523 ifp->if_flags &= ~XFS_IFEXTIREC;
3524 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003525 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003526 } else if (ifp->if_bytes) {
3527 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3528 sizeof(xfs_bmbt_rec_t));
3529 }
3530 ifp->if_u1.if_extents = NULL;
3531 ifp->if_real_bytes = 0;
3532 ifp->if_bytes = 0;
3533}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003534
3535/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003536 * Return a pointer to the extent record for file system block bno.
3537 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003538xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003539xfs_iext_bno_to_ext(
3540 xfs_ifork_t *ifp, /* inode fork pointer */
3541 xfs_fileoff_t bno, /* block number to search for */
3542 xfs_extnum_t *idxp) /* index of target extent */
3543{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003544 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003545 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003546 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003547 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003548 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003549 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003550 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003551 xfs_extnum_t nextents; /* number of file extents */
3552 xfs_fileoff_t startoff = 0; /* start offset of extent */
3553
3554 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3555 if (nextents == 0) {
3556 *idxp = 0;
3557 return NULL;
3558 }
3559 low = 0;
3560 if (ifp->if_flags & XFS_IFEXTIREC) {
3561 /* Find target extent list */
3562 int erp_idx = 0;
3563 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3564 base = erp->er_extbuf;
3565 high = erp->er_extcount - 1;
3566 } else {
3567 base = ifp->if_u1.if_extents;
3568 high = nextents - 1;
3569 }
3570 /* Binary search extent records */
3571 while (low <= high) {
3572 idx = (low + high) >> 1;
3573 ep = base + idx;
3574 startoff = xfs_bmbt_get_startoff(ep);
3575 blockcount = xfs_bmbt_get_blockcount(ep);
3576 if (bno < startoff) {
3577 high = idx - 1;
3578 } else if (bno >= startoff + blockcount) {
3579 low = idx + 1;
3580 } else {
3581 /* Convert back to file-based extent index */
3582 if (ifp->if_flags & XFS_IFEXTIREC) {
3583 idx += erp->er_extoff;
3584 }
3585 *idxp = idx;
3586 return ep;
3587 }
3588 }
3589 /* Convert back to file-based extent index */
3590 if (ifp->if_flags & XFS_IFEXTIREC) {
3591 idx += erp->er_extoff;
3592 }
3593 if (bno >= startoff + blockcount) {
3594 if (++idx == nextents) {
3595 ep = NULL;
3596 } else {
3597 ep = xfs_iext_get_ext(ifp, idx);
3598 }
3599 }
3600 *idxp = idx;
3601 return ep;
3602}
3603
3604/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003605 * Return a pointer to the indirection array entry containing the
3606 * extent record for filesystem block bno. Store the index of the
3607 * target irec in *erp_idxp.
3608 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003609xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003610xfs_iext_bno_to_irec(
3611 xfs_ifork_t *ifp, /* inode fork pointer */
3612 xfs_fileoff_t bno, /* block number to search for */
3613 int *erp_idxp) /* irec index of target ext list */
3614{
3615 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3616 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003617 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003618 int nlists; /* number of extent irec's (lists) */
3619 int high; /* binary search upper limit */
3620 int low; /* binary search lower limit */
3621
3622 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3623 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3624 erp_idx = 0;
3625 low = 0;
3626 high = nlists - 1;
3627 while (low <= high) {
3628 erp_idx = (low + high) >> 1;
3629 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3630 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3631 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3632 high = erp_idx - 1;
3633 } else if (erp_next && bno >=
3634 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3635 low = erp_idx + 1;
3636 } else {
3637 break;
3638 }
3639 }
3640 *erp_idxp = erp_idx;
3641 return erp;
3642}
3643
3644/*
3645 * Return a pointer to the indirection array entry containing the
3646 * extent record at file extent index *idxp. Store the index of the
3647 * target irec in *erp_idxp and store the page index of the target
3648 * extent record in *idxp.
3649 */
3650xfs_ext_irec_t *
3651xfs_iext_idx_to_irec(
3652 xfs_ifork_t *ifp, /* inode fork pointer */
3653 xfs_extnum_t *idxp, /* extent index (file -> page) */
3654 int *erp_idxp, /* pointer to target irec */
3655 int realloc) /* new bytes were just added */
3656{
3657 xfs_ext_irec_t *prev; /* pointer to previous irec */
3658 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3659 int erp_idx; /* indirection array index */
3660 int nlists; /* number of irec's (ex lists) */
3661 int high; /* binary search upper limit */
3662 int low; /* binary search lower limit */
3663 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3664
3665 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003666 ASSERT(page_idx >= 0);
3667 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3668 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3669
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003670 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3671 erp_idx = 0;
3672 low = 0;
3673 high = nlists - 1;
3674
3675 /* Binary search extent irec's */
3676 while (low <= high) {
3677 erp_idx = (low + high) >> 1;
3678 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3679 prev = erp_idx > 0 ? erp - 1 : NULL;
3680 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3681 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3682 high = erp_idx - 1;
3683 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3684 (page_idx == erp->er_extoff + erp->er_extcount &&
3685 !realloc)) {
3686 low = erp_idx + 1;
3687 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3688 erp->er_extcount == XFS_LINEAR_EXTS) {
3689 ASSERT(realloc);
3690 page_idx = 0;
3691 erp_idx++;
3692 erp = erp_idx < nlists ? erp + 1 : NULL;
3693 break;
3694 } else {
3695 page_idx -= erp->er_extoff;
3696 break;
3697 }
3698 }
3699 *idxp = page_idx;
3700 *erp_idxp = erp_idx;
3701 return(erp);
3702}
3703
3704/*
3705 * Allocate and initialize an indirection array once the space needed
3706 * for incore extents increases above XFS_IEXT_BUFSZ.
3707 */
3708void
3709xfs_iext_irec_init(
3710 xfs_ifork_t *ifp) /* inode fork pointer */
3711{
3712 xfs_ext_irec_t *erp; /* indirection array pointer */
3713 xfs_extnum_t nextents; /* number of extents in file */
3714
3715 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3716 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3717 ASSERT(nextents <= XFS_LINEAR_EXTS);
3718
David Chinner67850732008-08-13 16:02:51 +10003719 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003720
3721 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003722 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003723 } else if (!ifp->if_real_bytes) {
3724 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3725 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3726 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3727 }
3728 erp->er_extbuf = ifp->if_u1.if_extents;
3729 erp->er_extcount = nextents;
3730 erp->er_extoff = 0;
3731
3732 ifp->if_flags |= XFS_IFEXTIREC;
3733 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3734 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3735 ifp->if_u1.if_ext_irec = erp;
3736
3737 return;
3738}
3739
3740/*
3741 * Allocate and initialize a new entry in the indirection array.
3742 */
3743xfs_ext_irec_t *
3744xfs_iext_irec_new(
3745 xfs_ifork_t *ifp, /* inode fork pointer */
3746 int erp_idx) /* index for new irec */
3747{
3748 xfs_ext_irec_t *erp; /* indirection array pointer */
3749 int i; /* loop counter */
3750 int nlists; /* number of irec's (ex lists) */
3751
3752 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3753 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3754
3755 /* Resize indirection array */
3756 xfs_iext_realloc_indirect(ifp, ++nlists *
3757 sizeof(xfs_ext_irec_t));
3758 /*
3759 * Move records down in the array so the
3760 * new page can use erp_idx.
3761 */
3762 erp = ifp->if_u1.if_ext_irec;
3763 for (i = nlists - 1; i > erp_idx; i--) {
3764 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3765 }
3766 ASSERT(i == erp_idx);
3767
3768 /* Initialize new extent record */
3769 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003770 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003771 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3772 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3773 erp[erp_idx].er_extcount = 0;
3774 erp[erp_idx].er_extoff = erp_idx > 0 ?
3775 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3776 return (&erp[erp_idx]);
3777}
3778
3779/*
3780 * Remove a record from the indirection array.
3781 */
3782void
3783xfs_iext_irec_remove(
3784 xfs_ifork_t *ifp, /* inode fork pointer */
3785 int erp_idx) /* irec index to remove */
3786{
3787 xfs_ext_irec_t *erp; /* indirection array pointer */
3788 int i; /* loop counter */
3789 int nlists; /* number of irec's (ex lists) */
3790
3791 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3792 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3793 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3794 if (erp->er_extbuf) {
3795 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3796 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003797 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003798 }
3799 /* Compact extent records */
3800 erp = ifp->if_u1.if_ext_irec;
3801 for (i = erp_idx; i < nlists - 1; i++) {
3802 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3803 }
3804 /*
3805 * Manually free the last extent record from the indirection
3806 * array. A call to xfs_iext_realloc_indirect() with a size
3807 * of zero would result in a call to xfs_iext_destroy() which
3808 * would in turn call this function again, creating a nasty
3809 * infinite loop.
3810 */
3811 if (--nlists) {
3812 xfs_iext_realloc_indirect(ifp,
3813 nlists * sizeof(xfs_ext_irec_t));
3814 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003815 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003816 }
3817 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3818}
3819
3820/*
3821 * This is called to clean up large amounts of unused memory allocated
3822 * by the indirection array. Before compacting anything though, verify
3823 * that the indirection array is still needed and switch back to the
3824 * linear extent list (or even the inline buffer) if possible. The
3825 * compaction policy is as follows:
3826 *
3827 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003828 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003829 * No Compaction: Extents occupy at least 50% of allocated space
3830 */
3831void
3832xfs_iext_irec_compact(
3833 xfs_ifork_t *ifp) /* inode fork pointer */
3834{
3835 xfs_extnum_t nextents; /* number of extents in file */
3836 int nlists; /* number of irec's (ex lists) */
3837
3838 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3839 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3840 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3841
3842 if (nextents == 0) {
3843 xfs_iext_destroy(ifp);
3844 } else if (nextents <= XFS_INLINE_EXTS) {
3845 xfs_iext_indirect_to_direct(ifp);
3846 xfs_iext_direct_to_inline(ifp, nextents);
3847 } else if (nextents <= XFS_LINEAR_EXTS) {
3848 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003849 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3850 xfs_iext_irec_compact_pages(ifp);
3851 }
3852}
3853
3854/*
3855 * Combine extents from neighboring extent pages.
3856 */
3857void
3858xfs_iext_irec_compact_pages(
3859 xfs_ifork_t *ifp) /* inode fork pointer */
3860{
3861 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3862 int erp_idx = 0; /* indirection array index */
3863 int nlists; /* number of irec's (ex lists) */
3864
3865 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3866 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3867 while (erp_idx < nlists - 1) {
3868 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3869 erp_next = erp + 1;
3870 if (erp_next->er_extcount <=
3871 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003872 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003873 erp_next->er_extbuf, erp_next->er_extcount *
3874 sizeof(xfs_bmbt_rec_t));
3875 erp->er_extcount += erp_next->er_extcount;
3876 /*
3877 * Free page before removing extent record
3878 * so er_extoffs don't get modified in
3879 * xfs_iext_irec_remove.
3880 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003881 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003882 erp_next->er_extbuf = NULL;
3883 xfs_iext_irec_remove(ifp, erp_idx + 1);
3884 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3885 } else {
3886 erp_idx++;
3887 }
3888 }
3889}
3890
3891/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003892 * This is called to update the er_extoff field in the indirection
3893 * array when extents have been added or removed from one of the
3894 * extent lists. erp_idx contains the irec index to begin updating
3895 * at and ext_diff contains the number of extents that were added
3896 * or removed.
3897 */
3898void
3899xfs_iext_irec_update_extoffs(
3900 xfs_ifork_t *ifp, /* inode fork pointer */
3901 int erp_idx, /* irec index to update */
3902 int ext_diff) /* number of new extents */
3903{
3904 int i; /* loop counter */
3905 int nlists; /* number of irec's (ex lists */
3906
3907 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3908 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3909 for (i = erp_idx; i < nlists; i++) {
3910 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3911 }
3912}