Merge tag 'v3.10.103' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / xfs / xfs_inode.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
40ebd81d
RD
18#include <linux/log2.h>
19
1da177e4 20#include "xfs.h"
a844f451 21#include "xfs_fs.h"
1da177e4 22#include "xfs_types.h"
1da177e4 23#include "xfs_log.h"
a844f451 24#include "xfs_inum.h"
1da177e4
LT
25#include "xfs_trans.h"
26#include "xfs_trans_priv.h"
27#include "xfs_sb.h"
28#include "xfs_ag.h"
1da177e4 29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
a844f451 33#include "xfs_attr_sf.h"
1da177e4 34#include "xfs_dinode.h"
1da177e4 35#include "xfs_inode.h"
1da177e4 36#include "xfs_buf_item.h"
a844f451
NS
37#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"
1da177e4 42#include "xfs_error.h"
1da177e4 43#include "xfs_utils.h"
1da177e4 44#include "xfs_quota.h"
2a82b8be 45#include "xfs_filestream.h"
739bfb2a 46#include "xfs_vnodeops.h"
93848a99 47#include "xfs_cksum.h"
0b1b213f 48#include "xfs_trace.h"
33479e05 49#include "xfs_icache.h"
1da177e4 50
1da177e4
LT
51kmem_zone_t *xfs_ifork_zone;
52kmem_zone_t *xfs_inode_zone;
1da177e4
LT
53
54/*
8f04c47a 55 * Used in xfs_itruncate_extents(). This is the maximum number of extents
1da177e4
LT
56 * freed from a file in a single transaction.
57 */
58#define XFS_ITRUNC_MAX_EXTENTS 2
59
60STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
61STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
62STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
63STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
64
2a0ec1d9
DC
65/*
66 * helper function to extract extent size hint from inode
67 */
68xfs_extlen_t
69xfs_get_extsz_hint(
70 struct xfs_inode *ip)
71{
72 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
73 return ip->i_d.di_extsize;
74 if (XFS_IS_REALTIME_INODE(ip))
75 return ip->i_mount->m_sb.sb_rextsize;
76 return 0;
77}
78
fa96acad
DC
79/*
80 * This is a wrapper routine around the xfs_ilock() routine used to centralize
81 * some grungy code. It is used in places that wish to lock the inode solely
82 * for reading the extents. The reason these places can't just call
83 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
84 * extents from disk for a file in b-tree format. If the inode is in b-tree
85 * format, then we need to lock the inode exclusively until the extents are read
86 * in. Locking it exclusively all the time would limit our parallelism
87 * unnecessarily, though. What we do instead is check to see if the extents
88 * have been read in yet, and only lock the inode exclusively if they have not.
89 *
90 * The function returns a value which should be given to the corresponding
91 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
92 * actually taken.
93 */
94uint
95xfs_ilock_map_shared(
96 xfs_inode_t *ip)
97{
98 uint lock_mode;
99
100 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
101 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
102 lock_mode = XFS_ILOCK_EXCL;
103 } else {
104 lock_mode = XFS_ILOCK_SHARED;
105 }
106
107 xfs_ilock(ip, lock_mode);
108
109 return lock_mode;
110}
111
112/*
113 * This is simply the unlock routine to go with xfs_ilock_map_shared().
114 * All it does is call xfs_iunlock() with the given lock_mode.
115 */
116void
117xfs_iunlock_map_shared(
118 xfs_inode_t *ip,
119 unsigned int lock_mode)
120{
121 xfs_iunlock(ip, lock_mode);
122}
123
124/*
125 * The xfs inode contains 2 locks: a multi-reader lock called the
126 * i_iolock and a multi-reader lock called the i_lock. This routine
127 * allows either or both of the locks to be obtained.
128 *
129 * The 2 locks should always be ordered so that the IO lock is
130 * obtained first in order to prevent deadlock.
131 *
132 * ip -- the inode being locked
133 * lock_flags -- this parameter indicates the inode's locks
134 * to be locked. It can be:
135 * XFS_IOLOCK_SHARED,
136 * XFS_IOLOCK_EXCL,
137 * XFS_ILOCK_SHARED,
138 * XFS_ILOCK_EXCL,
139 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
140 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
141 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
142 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
143 */
144void
145xfs_ilock(
146 xfs_inode_t *ip,
147 uint lock_flags)
148{
149 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
150
151 /*
152 * You can't set both SHARED and EXCL for the same lock,
153 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
154 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
155 */
156 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
157 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
158 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
159 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
160 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
161
162 if (lock_flags & XFS_IOLOCK_EXCL)
163 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
164 else if (lock_flags & XFS_IOLOCK_SHARED)
165 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
166
167 if (lock_flags & XFS_ILOCK_EXCL)
168 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
169 else if (lock_flags & XFS_ILOCK_SHARED)
170 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
171}
172
173/*
174 * This is just like xfs_ilock(), except that the caller
175 * is guaranteed not to sleep. It returns 1 if it gets
176 * the requested locks and 0 otherwise. If the IO lock is
177 * obtained but the inode lock cannot be, then the IO lock
178 * is dropped before returning.
179 *
180 * ip -- the inode being locked
181 * lock_flags -- this parameter indicates the inode's locks to be
182 * to be locked. See the comment for xfs_ilock() for a list
183 * of valid values.
184 */
185int
186xfs_ilock_nowait(
187 xfs_inode_t *ip,
188 uint lock_flags)
189{
190 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
191
192 /*
193 * You can't set both SHARED and EXCL for the same lock,
194 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
195 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
196 */
197 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
198 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
199 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
200 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
201 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
202
203 if (lock_flags & XFS_IOLOCK_EXCL) {
204 if (!mrtryupdate(&ip->i_iolock))
205 goto out;
206 } else if (lock_flags & XFS_IOLOCK_SHARED) {
207 if (!mrtryaccess(&ip->i_iolock))
208 goto out;
209 }
210 if (lock_flags & XFS_ILOCK_EXCL) {
211 if (!mrtryupdate(&ip->i_lock))
212 goto out_undo_iolock;
213 } else if (lock_flags & XFS_ILOCK_SHARED) {
214 if (!mrtryaccess(&ip->i_lock))
215 goto out_undo_iolock;
216 }
217 return 1;
218
219 out_undo_iolock:
220 if (lock_flags & XFS_IOLOCK_EXCL)
221 mrunlock_excl(&ip->i_iolock);
222 else if (lock_flags & XFS_IOLOCK_SHARED)
223 mrunlock_shared(&ip->i_iolock);
224 out:
225 return 0;
226}
227
228/*
229 * xfs_iunlock() is used to drop the inode locks acquired with
230 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
231 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
232 * that we know which locks to drop.
233 *
234 * ip -- the inode being unlocked
235 * lock_flags -- this parameter indicates the inode's locks to be
236 * to be unlocked. See the comment for xfs_ilock() for a list
237 * of valid values for this parameter.
238 *
239 */
240void
241xfs_iunlock(
242 xfs_inode_t *ip,
243 uint lock_flags)
244{
245 /*
246 * You can't set both SHARED and EXCL for the same lock,
247 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
248 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
249 */
250 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
251 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
252 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
253 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
254 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
255 ASSERT(lock_flags != 0);
256
257 if (lock_flags & XFS_IOLOCK_EXCL)
258 mrunlock_excl(&ip->i_iolock);
259 else if (lock_flags & XFS_IOLOCK_SHARED)
260 mrunlock_shared(&ip->i_iolock);
261
262 if (lock_flags & XFS_ILOCK_EXCL)
263 mrunlock_excl(&ip->i_lock);
264 else if (lock_flags & XFS_ILOCK_SHARED)
265 mrunlock_shared(&ip->i_lock);
266
267 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
268}
269
270/*
271 * give up write locks. the i/o lock cannot be held nested
272 * if it is being demoted.
273 */
274void
275xfs_ilock_demote(
276 xfs_inode_t *ip,
277 uint lock_flags)
278{
279 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
280 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
281
282 if (lock_flags & XFS_ILOCK_EXCL)
283 mrdemote(&ip->i_lock);
284 if (lock_flags & XFS_IOLOCK_EXCL)
285 mrdemote(&ip->i_iolock);
286
287 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
288}
289
742ae1e3 290#if defined(DEBUG) || defined(XFS_WARN)
fa96acad
DC
291int
292xfs_isilocked(
293 xfs_inode_t *ip,
294 uint lock_flags)
295{
296 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
297 if (!(lock_flags & XFS_ILOCK_SHARED))
298 return !!ip->i_lock.mr_writer;
299 return rwsem_is_locked(&ip->i_lock.mr_lock);
300 }
301
302 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
303 if (!(lock_flags & XFS_IOLOCK_SHARED))
304 return !!ip->i_iolock.mr_writer;
305 return rwsem_is_locked(&ip->i_iolock.mr_lock);
306 }
307
308 ASSERT(0);
309 return 0;
310}
311#endif
312
313void
314__xfs_iflock(
315 struct xfs_inode *ip)
316{
317 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
318 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
319
320 do {
321 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
322 if (xfs_isiflocked(ip))
323 io_schedule();
324 } while (!xfs_iflock_nowait(ip));
325
326 finish_wait(wq, &wait.wait);
327}
328
1da177e4
LT
329#ifdef DEBUG
330/*
331 * Make sure that the extents in the given memory buffer
332 * are valid.
333 */
334STATIC void
335xfs_validate_extents(
4eea22f0 336 xfs_ifork_t *ifp,
1da177e4 337 int nrecs,
1da177e4
LT
338 xfs_exntfmt_t fmt)
339{
340 xfs_bmbt_irec_t irec;
a6f64d4a 341 xfs_bmbt_rec_host_t rec;
1da177e4
LT
342 int i;
343
344 for (i = 0; i < nrecs; i++) {
a6f64d4a
CH
345 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
346 rec.l0 = get_unaligned(&ep->l0);
347 rec.l1 = get_unaligned(&ep->l1);
348 xfs_bmbt_get_all(&rec, &irec);
1da177e4
LT
349 if (fmt == XFS_EXTFMT_NOSTATE)
350 ASSERT(irec.br_state == XFS_EXT_NORM);
1da177e4
LT
351 }
352}
353#else /* DEBUG */
a6f64d4a 354#define xfs_validate_extents(ifp, nrecs, fmt)
1da177e4
LT
355#endif /* DEBUG */
356
357/*
358 * Check that none of the inode's in the buffer have a next
359 * unlinked field of 0.
360 */
361#if defined(DEBUG)
362void
363xfs_inobp_check(
364 xfs_mount_t *mp,
365 xfs_buf_t *bp)
366{
367 int i;
368 int j;
369 xfs_dinode_t *dip;
370
371 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
372
373 for (i = 0; i < j; i++) {
374 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
375 i * mp->m_sb.sb_inodesize);
376 if (!dip->di_next_unlinked) {
53487786
DC
377 xfs_alert(mp,
378 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
1da177e4
LT
379 bp);
380 ASSERT(dip->di_next_unlinked);
381 }
382 }
383}
384#endif
385
612cfbfe 386static void
af133e86
DC
387xfs_inode_buf_verify(
388 struct xfs_buf *bp)
389{
390 struct xfs_mount *mp = bp->b_target->bt_mount;
391 int i;
392 int ni;
393
394 /*
395 * Validate the magic number and version of every inode in the buffer
396 */
397 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
398 for (i = 0; i < ni; i++) {
399 int di_ok;
400 xfs_dinode_t *dip;
401
402 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
403 (i << mp->m_sb.sb_inodelog));
404 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
405 XFS_DINODE_GOOD_VERSION(dip->di_version);
406 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
407 XFS_ERRTAG_ITOBP_INOTOBP,
408 XFS_RANDOM_ITOBP_INOTOBP))) {
409 xfs_buf_ioerror(bp, EFSCORRUPTED);
410 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
411 mp, dip);
412#ifdef DEBUG
413 xfs_emerg(mp,
414 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
415 (unsigned long long)bp->b_bn, i,
416 be16_to_cpu(dip->di_magic));
417 ASSERT(0);
418#endif
419 }
420 }
421 xfs_inobp_check(mp, bp);
612cfbfe
DC
422}
423
1813dd64
DC
424
425static void
426xfs_inode_buf_read_verify(
612cfbfe
DC
427 struct xfs_buf *bp)
428{
429 xfs_inode_buf_verify(bp);
430}
431
1813dd64
DC
432static void
433xfs_inode_buf_write_verify(
612cfbfe
DC
434 struct xfs_buf *bp)
435{
436 xfs_inode_buf_verify(bp);
af133e86
DC
437}
438
1813dd64
DC
439const struct xfs_buf_ops xfs_inode_buf_ops = {
440 .verify_read = xfs_inode_buf_read_verify,
441 .verify_write = xfs_inode_buf_write_verify,
442};
443
444
4ae29b43 445/*
475ee413
CH
446 * This routine is called to map an inode to the buffer containing the on-disk
447 * version of the inode. It returns a pointer to the buffer containing the
448 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
449 * pointer to the on-disk inode within that buffer.
450 *
451 * If a non-zero error is returned, then the contents of bpp and dipp are
452 * undefined.
4ae29b43 453 */
475ee413 454int
4ae29b43 455xfs_imap_to_bp(
475ee413
CH
456 struct xfs_mount *mp,
457 struct xfs_trans *tp,
458 struct xfs_imap *imap,
af133e86 459 struct xfs_dinode **dipp,
475ee413
CH
460 struct xfs_buf **bpp,
461 uint buf_flags,
462 uint iget_flags)
4ae29b43 463{
475ee413
CH
464 struct xfs_buf *bp;
465 int error;
4ae29b43 466
611c9946 467 buf_flags |= XBF_UNMAPPED;
4ae29b43 468 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
af133e86 469 (int)imap->im_len, buf_flags, &bp,
1813dd64 470 &xfs_inode_buf_ops);
4ae29b43 471 if (error) {
af133e86 472 if (error == EAGAIN) {
0cadda1c 473 ASSERT(buf_flags & XBF_TRYLOCK);
af133e86 474 return error;
a3f74ffb 475 }
4ae29b43 476
af133e86
DC
477 if (error == EFSCORRUPTED &&
478 (iget_flags & XFS_IGET_UNTRUSTED))
479 return XFS_ERROR(EINVAL);
4ae29b43 480
af133e86
DC
481 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
482 __func__, error);
483 return error;
4ae29b43
DC
484 }
485
4ae29b43 486 *bpp = bp;
475ee413 487 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
4ae29b43
DC
488 return 0;
489}
490
1da177e4
LT
491/*
492 * Move inode type and inode format specific information from the
493 * on-disk inode to the in-core inode. For fifos, devs, and sockets
494 * this means set if_rdev to the proper value. For files, directories,
495 * and symlinks this means to bring in the in-line data or extent
496 * pointers. For a file in B-tree format, only the root is immediately
497 * brought in-core. The rest will be in-lined in if_extents when it
498 * is first referenced (see xfs_iread_extents()).
499 */
500STATIC int
501xfs_iformat(
502 xfs_inode_t *ip,
503 xfs_dinode_t *dip)
504{
505 xfs_attr_shortform_t *atp;
506 int size;
8096b1eb 507 int error = 0;
1da177e4 508 xfs_fsize_t di_size;
1da177e4 509
81591fe2
CH
510 if (unlikely(be32_to_cpu(dip->di_nextents) +
511 be16_to_cpu(dip->di_anextents) >
512 be64_to_cpu(dip->di_nblocks))) {
65333b4c 513 xfs_warn(ip->i_mount,
3762ec6b 514 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
1da177e4 515 (unsigned long long)ip->i_ino,
81591fe2
CH
516 (int)(be32_to_cpu(dip->di_nextents) +
517 be16_to_cpu(dip->di_anextents)),
1da177e4 518 (unsigned long long)
81591fe2 519 be64_to_cpu(dip->di_nblocks));
1da177e4
LT
520 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
521 ip->i_mount, dip);
522 return XFS_ERROR(EFSCORRUPTED);
523 }
524
81591fe2 525 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
65333b4c 526 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
1da177e4 527 (unsigned long long)ip->i_ino,
81591fe2 528 dip->di_forkoff);
1da177e4
LT
529 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
530 ip->i_mount, dip);
531 return XFS_ERROR(EFSCORRUPTED);
532 }
533
b89d4208
CH
534 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
535 !ip->i_mount->m_rtdev_targp)) {
65333b4c 536 xfs_warn(ip->i_mount,
b89d4208
CH
537 "corrupt dinode %Lu, has realtime flag set.",
538 ip->i_ino);
539 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
540 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
541 return XFS_ERROR(EFSCORRUPTED);
542 }
543
1da177e4
LT
544 switch (ip->i_d.di_mode & S_IFMT) {
545 case S_IFIFO:
546 case S_IFCHR:
547 case S_IFBLK:
548 case S_IFSOCK:
81591fe2 549 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
1da177e4
LT
550 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
551 ip->i_mount, dip);
552 return XFS_ERROR(EFSCORRUPTED);
553 }
554 ip->i_d.di_size = 0;
81591fe2 555 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
1da177e4
LT
556 break;
557
558 case S_IFREG:
559 case S_IFLNK:
560 case S_IFDIR:
81591fe2 561 switch (dip->di_format) {
1da177e4
LT
562 case XFS_DINODE_FMT_LOCAL:
563 /*
564 * no local regular files yet
565 */
abbede1b 566 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
65333b4c
DC
567 xfs_warn(ip->i_mount,
568 "corrupt inode %Lu (local format for regular file).",
1da177e4
LT
569 (unsigned long long) ip->i_ino);
570 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
571 XFS_ERRLEVEL_LOW,
572 ip->i_mount, dip);
573 return XFS_ERROR(EFSCORRUPTED);
574 }
575
81591fe2 576 di_size = be64_to_cpu(dip->di_size);
1da177e4 577 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
65333b4c
DC
578 xfs_warn(ip->i_mount,
579 "corrupt inode %Lu (bad size %Ld for local inode).",
1da177e4
LT
580 (unsigned long long) ip->i_ino,
581 (long long) di_size);
582 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
583 XFS_ERRLEVEL_LOW,
584 ip->i_mount, dip);
585 return XFS_ERROR(EFSCORRUPTED);
586 }
587
588 size = (int)di_size;
589 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
590 break;
591 case XFS_DINODE_FMT_EXTENTS:
592 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
593 break;
594 case XFS_DINODE_FMT_BTREE:
595 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
596 break;
597 default:
598 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
599 ip->i_mount);
600 return XFS_ERROR(EFSCORRUPTED);
601 }
602 break;
603
604 default:
605 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
606 return XFS_ERROR(EFSCORRUPTED);
607 }
608 if (error) {
609 return error;
610 }
611 if (!XFS_DFORK_Q(dip))
612 return 0;
8096b1eb 613
1da177e4 614 ASSERT(ip->i_afp == NULL);
4a7edddc 615 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
8096b1eb 616
81591fe2 617 switch (dip->di_aformat) {
1da177e4
LT
618 case XFS_DINODE_FMT_LOCAL:
619 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
3b244aa8 620 size = be16_to_cpu(atp->hdr.totsize);
2809f76a
CH
621
622 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
65333b4c
DC
623 xfs_warn(ip->i_mount,
624 "corrupt inode %Lu (bad attr fork size %Ld).",
2809f76a
CH
625 (unsigned long long) ip->i_ino,
626 (long long) size);
627 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
628 XFS_ERRLEVEL_LOW,
629 ip->i_mount, dip);
630 return XFS_ERROR(EFSCORRUPTED);
631 }
632
1da177e4
LT
633 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
634 break;
635 case XFS_DINODE_FMT_EXTENTS:
636 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
637 break;
638 case XFS_DINODE_FMT_BTREE:
639 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
640 break;
641 default:
642 error = XFS_ERROR(EFSCORRUPTED);
643 break;
644 }
645 if (error) {
646 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
647 ip->i_afp = NULL;
648 xfs_idestroy_fork(ip, XFS_DATA_FORK);
649 }
650 return error;
651}
652
653/*
654 * The file is in-lined in the on-disk inode.
655 * If it fits into if_inline_data, then copy
656 * it there, otherwise allocate a buffer for it
657 * and copy the data there. Either way, set
658 * if_data to point at the data.
659 * If we allocate a buffer for the data, make
660 * sure that its size is a multiple of 4 and
661 * record the real size in i_real_bytes.
662 */
663STATIC int
664xfs_iformat_local(
665 xfs_inode_t *ip,
666 xfs_dinode_t *dip,
667 int whichfork,
668 int size)
669{
670 xfs_ifork_t *ifp;
671 int real_size;
672
673 /*
674 * If the size is unreasonable, then something
675 * is wrong and we just bail out rather than crash in
676 * kmem_alloc() or memcpy() below.
677 */
678 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
65333b4c
DC
679 xfs_warn(ip->i_mount,
680 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
1da177e4
LT
681 (unsigned long long) ip->i_ino, size,
682 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
683 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
684 ip->i_mount, dip);
685 return XFS_ERROR(EFSCORRUPTED);
686 }
687 ifp = XFS_IFORK_PTR(ip, whichfork);
688 real_size = 0;
689 if (size == 0)
690 ifp->if_u1.if_data = NULL;
691 else if (size <= sizeof(ifp->if_u2.if_inline_data))
692 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
693 else {
694 real_size = roundup(size, 4);
4a7edddc 695 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
1da177e4
LT
696 }
697 ifp->if_bytes = size;
698 ifp->if_real_bytes = real_size;
699 if (size)
700 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
701 ifp->if_flags &= ~XFS_IFEXTENTS;
702 ifp->if_flags |= XFS_IFINLINE;
703 return 0;
704}
705
706/*
707 * The file consists of a set of extents all
708 * of which fit into the on-disk inode.
709 * If there are few enough extents to fit into
710 * the if_inline_ext, then copy them there.
711 * Otherwise allocate a buffer for them and copy
712 * them into it. Either way, set if_extents
713 * to point at the extents.
714 */
715STATIC int
716xfs_iformat_extents(
717 xfs_inode_t *ip,
718 xfs_dinode_t *dip,
719 int whichfork)
720{
a6f64d4a 721 xfs_bmbt_rec_t *dp;
1da177e4
LT
722 xfs_ifork_t *ifp;
723 int nex;
1da177e4
LT
724 int size;
725 int i;
726
727 ifp = XFS_IFORK_PTR(ip, whichfork);
728 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
729 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
730
731 /*
732 * If the number of extents is unreasonable, then something
733 * is wrong and we just bail out rather than crash in
734 * kmem_alloc() or memcpy() below.
735 */
736 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
65333b4c 737 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
1da177e4
LT
738 (unsigned long long) ip->i_ino, nex);
739 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
740 ip->i_mount, dip);
741 return XFS_ERROR(EFSCORRUPTED);
742 }
743
4eea22f0 744 ifp->if_real_bytes = 0;
1da177e4
LT
745 if (nex == 0)
746 ifp->if_u1.if_extents = NULL;
747 else if (nex <= XFS_INLINE_EXTS)
748 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
4eea22f0
MK
749 else
750 xfs_iext_add(ifp, 0, nex);
751
1da177e4 752 ifp->if_bytes = size;
1da177e4
LT
753 if (size) {
754 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
a6f64d4a 755 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
4eea22f0 756 for (i = 0; i < nex; i++, dp++) {
a6f64d4a 757 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
597bca63
HH
758 ep->l0 = get_unaligned_be64(&dp->l0);
759 ep->l1 = get_unaligned_be64(&dp->l1);
1da177e4 760 }
3a59c94c 761 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
1da177e4
LT
762 if (whichfork != XFS_DATA_FORK ||
763 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
764 if (unlikely(xfs_check_nostate_extents(
4eea22f0 765 ifp, 0, nex))) {
1da177e4
LT
766 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
767 XFS_ERRLEVEL_LOW,
768 ip->i_mount);
769 return XFS_ERROR(EFSCORRUPTED);
770 }
771 }
772 ifp->if_flags |= XFS_IFEXTENTS;
773 return 0;
774}
775
776/*
777 * The file has too many extents to fit into
778 * the inode, so they are in B-tree format.
779 * Allocate a buffer for the root of the B-tree
780 * and copy the root into it. The i_extents
781 * field will remain NULL until all of the
782 * extents are read in (when they are needed).
783 */
784STATIC int
785xfs_iformat_btree(
786 xfs_inode_t *ip,
787 xfs_dinode_t *dip,
788 int whichfork)
789{
ee1a47ab 790 struct xfs_mount *mp = ip->i_mount;
1da177e4
LT
791 xfs_bmdr_block_t *dfp;
792 xfs_ifork_t *ifp;
793 /* REFERENCED */
794 int nrecs;
795 int size;
796
797 ifp = XFS_IFORK_PTR(ip, whichfork);
798 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
ee1a47ab 799 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
60197e8d 800 nrecs = be16_to_cpu(dfp->bb_numrecs);
1da177e4
LT
801
802 /*
803 * blow out if -- fork has less extents than can fit in
804 * fork (fork shouldn't be a btree format), root btree
805 * block has more records than can fit into the fork,
806 * or the number of extents is greater than the number of
807 * blocks.
808 */
8096b1eb 809 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
ee1a47ab 810 XFS_IFORK_MAXEXT(ip, whichfork) ||
8096b1eb 811 XFS_BMDR_SPACE_CALC(nrecs) >
ee1a47ab 812 XFS_DFORK_SIZE(dip, mp, whichfork) ||
8096b1eb 813 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
ee1a47ab
CH
814 xfs_warn(mp, "corrupt inode %Lu (btree).",
815 (unsigned long long) ip->i_ino);
65333b4c 816 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
ee1a47ab 817 mp, dip);
1da177e4
LT
818 return XFS_ERROR(EFSCORRUPTED);
819 }
820
821 ifp->if_broot_bytes = size;
4a7edddc 822 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
1da177e4
LT
823 ASSERT(ifp->if_broot != NULL);
824 /*
825 * Copy and convert from the on-disk structure
826 * to the in-memory structure.
827 */
ee1a47ab 828 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
60197e8d 829 ifp->if_broot, size);
1da177e4
LT
830 ifp->if_flags &= ~XFS_IFEXTENTS;
831 ifp->if_flags |= XFS_IFBROOT;
832
833 return 0;
834}
835
d96f8f89 836STATIC void
347d1c01
CH
837xfs_dinode_from_disk(
838 xfs_icdinode_t *to,
81591fe2 839 xfs_dinode_t *from)
1da177e4 840{
347d1c01
CH
841 to->di_magic = be16_to_cpu(from->di_magic);
842 to->di_mode = be16_to_cpu(from->di_mode);
843 to->di_version = from ->di_version;
844 to->di_format = from->di_format;
845 to->di_onlink = be16_to_cpu(from->di_onlink);
846 to->di_uid = be32_to_cpu(from->di_uid);
847 to->di_gid = be32_to_cpu(from->di_gid);
848 to->di_nlink = be32_to_cpu(from->di_nlink);
6743099c
AM
849 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
850 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
347d1c01
CH
851 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
852 to->di_flushiter = be16_to_cpu(from->di_flushiter);
853 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
854 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
855 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
856 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
857 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
858 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
859 to->di_size = be64_to_cpu(from->di_size);
860 to->di_nblocks = be64_to_cpu(from->di_nblocks);
861 to->di_extsize = be32_to_cpu(from->di_extsize);
862 to->di_nextents = be32_to_cpu(from->di_nextents);
863 to->di_anextents = be16_to_cpu(from->di_anextents);
864 to->di_forkoff = from->di_forkoff;
865 to->di_aformat = from->di_aformat;
866 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
867 to->di_dmstate = be16_to_cpu(from->di_dmstate);
868 to->di_flags = be16_to_cpu(from->di_flags);
869 to->di_gen = be32_to_cpu(from->di_gen);
93848a99
CH
870
871 if (to->di_version == 3) {
872 to->di_changecount = be64_to_cpu(from->di_changecount);
873 to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
874 to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
875 to->di_flags2 = be64_to_cpu(from->di_flags2);
876 to->di_ino = be64_to_cpu(from->di_ino);
877 to->di_lsn = be64_to_cpu(from->di_lsn);
878 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
879 uuid_copy(&to->di_uuid, &from->di_uuid);
880 }
347d1c01
CH
881}
882
883void
884xfs_dinode_to_disk(
81591fe2 885 xfs_dinode_t *to,
347d1c01
CH
886 xfs_icdinode_t *from)
887{
888 to->di_magic = cpu_to_be16(from->di_magic);
889 to->di_mode = cpu_to_be16(from->di_mode);
890 to->di_version = from ->di_version;
891 to->di_format = from->di_format;
892 to->di_onlink = cpu_to_be16(from->di_onlink);
893 to->di_uid = cpu_to_be32(from->di_uid);
894 to->di_gid = cpu_to_be32(from->di_gid);
895 to->di_nlink = cpu_to_be32(from->di_nlink);
6743099c
AM
896 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
897 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
347d1c01
CH
898 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
899 to->di_flushiter = cpu_to_be16(from->di_flushiter);
900 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
901 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
902 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
903 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
904 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
905 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
906 to->di_size = cpu_to_be64(from->di_size);
907 to->di_nblocks = cpu_to_be64(from->di_nblocks);
908 to->di_extsize = cpu_to_be32(from->di_extsize);
909 to->di_nextents = cpu_to_be32(from->di_nextents);
910 to->di_anextents = cpu_to_be16(from->di_anextents);
911 to->di_forkoff = from->di_forkoff;
912 to->di_aformat = from->di_aformat;
913 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
914 to->di_dmstate = cpu_to_be16(from->di_dmstate);
915 to->di_flags = cpu_to_be16(from->di_flags);
916 to->di_gen = cpu_to_be32(from->di_gen);
93848a99
CH
917
918 if (from->di_version == 3) {
919 to->di_changecount = cpu_to_be64(from->di_changecount);
920 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
921 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
922 to->di_flags2 = cpu_to_be64(from->di_flags2);
923 to->di_ino = cpu_to_be64(from->di_ino);
924 to->di_lsn = cpu_to_be64(from->di_lsn);
925 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
926 uuid_copy(&to->di_uuid, &from->di_uuid);
927 }
1da177e4
LT
928}
929
930STATIC uint
931_xfs_dic2xflags(
1da177e4
LT
932 __uint16_t di_flags)
933{
934 uint flags = 0;
935
936 if (di_flags & XFS_DIFLAG_ANY) {
937 if (di_flags & XFS_DIFLAG_REALTIME)
938 flags |= XFS_XFLAG_REALTIME;
939 if (di_flags & XFS_DIFLAG_PREALLOC)
940 flags |= XFS_XFLAG_PREALLOC;
941 if (di_flags & XFS_DIFLAG_IMMUTABLE)
942 flags |= XFS_XFLAG_IMMUTABLE;
943 if (di_flags & XFS_DIFLAG_APPEND)
944 flags |= XFS_XFLAG_APPEND;
945 if (di_flags & XFS_DIFLAG_SYNC)
946 flags |= XFS_XFLAG_SYNC;
947 if (di_flags & XFS_DIFLAG_NOATIME)
948 flags |= XFS_XFLAG_NOATIME;
949 if (di_flags & XFS_DIFLAG_NODUMP)
950 flags |= XFS_XFLAG_NODUMP;
951 if (di_flags & XFS_DIFLAG_RTINHERIT)
952 flags |= XFS_XFLAG_RTINHERIT;
953 if (di_flags & XFS_DIFLAG_PROJINHERIT)
954 flags |= XFS_XFLAG_PROJINHERIT;
955 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
956 flags |= XFS_XFLAG_NOSYMLINKS;
dd9f438e
NS
957 if (di_flags & XFS_DIFLAG_EXTSIZE)
958 flags |= XFS_XFLAG_EXTSIZE;
959 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
960 flags |= XFS_XFLAG_EXTSZINHERIT;
d3446eac
BN
961 if (di_flags & XFS_DIFLAG_NODEFRAG)
962 flags |= XFS_XFLAG_NODEFRAG;
2a82b8be
DC
963 if (di_flags & XFS_DIFLAG_FILESTREAM)
964 flags |= XFS_XFLAG_FILESTREAM;
1da177e4
LT
965 }
966
967 return flags;
968}
969
970uint
971xfs_ip2xflags(
972 xfs_inode_t *ip)
973{
347d1c01 974 xfs_icdinode_t *dic = &ip->i_d;
1da177e4 975
a916e2bd 976 return _xfs_dic2xflags(dic->di_flags) |
45ba598e 977 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
978}
979
980uint
981xfs_dic2xflags(
45ba598e 982 xfs_dinode_t *dip)
1da177e4 983{
81591fe2 984 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
45ba598e 985 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
986}
987
93848a99
CH
988static bool
989xfs_dinode_verify(
990 struct xfs_mount *mp,
991 struct xfs_inode *ip,
992 struct xfs_dinode *dip)
993{
994 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
995 return false;
996
997 /* only version 3 or greater inodes are extensively verified here */
998 if (dip->di_version < 3)
999 return true;
1000
1001 if (!xfs_sb_version_hascrc(&mp->m_sb))
1002 return false;
1003 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
1004 offsetof(struct xfs_dinode, di_crc)))
1005 return false;
1006 if (be64_to_cpu(dip->di_ino) != ip->i_ino)
1007 return false;
1008 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
1009 return false;
1010 return true;
1011}
1012
1013void
1014xfs_dinode_calc_crc(
1015 struct xfs_mount *mp,
1016 struct xfs_dinode *dip)
1017{
1018 __uint32_t crc;
1019
1020 if (dip->di_version < 3)
1021 return;
1022
1023 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
1024 crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
1025 offsetof(struct xfs_dinode, di_crc));
1026 dip->di_crc = xfs_end_cksum(crc);
1027}
1028
07c8f675 1029/*
24f211ba 1030 * Read the disk inode attributes into the in-core inode structure.
1da177e4
LT
1031 */
1032int
1033xfs_iread(
1034 xfs_mount_t *mp,
1035 xfs_trans_t *tp,
24f211ba 1036 xfs_inode_t *ip,
24f211ba 1037 uint iget_flags)
1da177e4
LT
1038{
1039 xfs_buf_t *bp;
1040 xfs_dinode_t *dip;
1da177e4
LT
1041 int error;
1042
1da177e4 1043 /*
92bfc6e7 1044 * Fill in the location information in the in-core inode.
1da177e4 1045 */
24f211ba 1046 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
76d8b277 1047 if (error)
24f211ba 1048 return error;
76d8b277
CH
1049
1050 /*
92bfc6e7 1051 * Get pointers to the on-disk inode and the buffer containing it.
76d8b277 1052 */
475ee413 1053 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
9ed0451e 1054 if (error)
24f211ba 1055 return error;
1da177e4 1056
93848a99
CH
1057 /* even unallocated inodes are verified */
1058 if (!xfs_dinode_verify(mp, ip, dip)) {
1059 xfs_alert(mp, "%s: validation failed for inode %lld failed",
1060 __func__, ip->i_ino);
1061
1062 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
1063 error = XFS_ERROR(EFSCORRUPTED);
9ed0451e 1064 goto out_brelse;
1da177e4
LT
1065 }
1066
1067 /*
1068 * If the on-disk inode is already linked to a directory
1069 * entry, copy all of the inode into the in-core inode.
1070 * xfs_iformat() handles copying in the inode format
1071 * specific information.
1072 * Otherwise, just get the truly permanent information.
1073 */
81591fe2
CH
1074 if (dip->di_mode) {
1075 xfs_dinode_from_disk(&ip->i_d, dip);
1da177e4
LT
1076 error = xfs_iformat(ip, dip);
1077 if (error) {
1da177e4 1078#ifdef DEBUG
53487786
DC
1079 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
1080 __func__, error);
1da177e4 1081#endif /* DEBUG */
9ed0451e 1082 goto out_brelse;
1da177e4
LT
1083 }
1084 } else {
93848a99
CH
1085 /*
1086 * Partial initialisation of the in-core inode. Just the bits
1087 * that xfs_ialloc won't overwrite or relies on being correct.
1088 */
81591fe2
CH
1089 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1090 ip->i_d.di_version = dip->di_version;
1091 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1092 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
93848a99
CH
1093
1094 if (dip->di_version == 3) {
1095 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
1096 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
1097 }
1098
1da177e4
LT
1099 /*
1100 * Make sure to pull in the mode here as well in
1101 * case the inode is released without being used.
1102 * This ensures that xfs_inactive() will see that
1103 * the inode is already free and not try to mess
1104 * with the uninitialized part of it.
1105 */
1106 ip->i_d.di_mode = 0;
1da177e4
LT
1107 }
1108
1da177e4
LT
1109 /*
1110 * The inode format changed when we moved the link count and
1111 * made it 32 bits long. If this is an old format inode,
1112 * convert it in memory to look like a new one. If it gets
1113 * flushed to disk we will convert back before flushing or
1114 * logging it. We zero out the new projid field and the old link
1115 * count field. We'll handle clearing the pad field (the remains
1116 * of the old uuid field) when we actually convert the inode to
1117 * the new format. We don't change the version number so that we
1118 * can distinguish this from a real new format inode.
1119 */
51ce16d5 1120 if (ip->i_d.di_version == 1) {
1da177e4
LT
1121 ip->i_d.di_nlink = ip->i_d.di_onlink;
1122 ip->i_d.di_onlink = 0;
6743099c 1123 xfs_set_projid(ip, 0);
1da177e4
LT
1124 }
1125
1126 ip->i_delayed_blks = 0;
1127
1128 /*
1129 * Mark the buffer containing the inode as something to keep
1130 * around for a while. This helps to keep recently accessed
1131 * meta-data in-core longer.
1132 */
821eb21d 1133 xfs_buf_set_ref(bp, XFS_INO_REF);
1da177e4
LT
1134
1135 /*
1136 * Use xfs_trans_brelse() to release the buffer containing the
1137 * on-disk inode, because it was acquired with xfs_trans_read_buf()
475ee413 1138 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
1da177e4
LT
1139 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1140 * will only release the buffer if it is not dirty within the
1141 * transaction. It will be OK to release the buffer in this case,
1142 * because inodes on disk are never destroyed and we will be
1143 * locking the new in-core inode before putting it in the hash
1144 * table where other processes can find it. Thus we don't have
1145 * to worry about the inode being changed just because we released
1146 * the buffer.
1147 */
9ed0451e
CH
1148 out_brelse:
1149 xfs_trans_brelse(tp, bp);
9ed0451e 1150 return error;
1da177e4
LT
1151}
1152
1153/*
1154 * Read in extents from a btree-format inode.
1155 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1156 */
1157int
1158xfs_iread_extents(
1159 xfs_trans_t *tp,
1160 xfs_inode_t *ip,
1161 int whichfork)
1162{
1163 int error;
1164 xfs_ifork_t *ifp;
4eea22f0 1165 xfs_extnum_t nextents;
1da177e4
LT
1166
1167 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1168 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1169 ip->i_mount);
1170 return XFS_ERROR(EFSCORRUPTED);
1171 }
4eea22f0 1172 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1da177e4 1173 ifp = XFS_IFORK_PTR(ip, whichfork);
4eea22f0 1174
1da177e4
LT
1175 /*
1176 * We know that the size is valid (it's checked in iformat_btree)
1177 */
4eea22f0 1178 ifp->if_bytes = ifp->if_real_bytes = 0;
1da177e4 1179 ifp->if_flags |= XFS_IFEXTENTS;
4eea22f0 1180 xfs_iext_add(ifp, 0, nextents);
1da177e4
LT
1181 error = xfs_bmap_read_extents(tp, ip, whichfork);
1182 if (error) {
4eea22f0 1183 xfs_iext_destroy(ifp);
1da177e4
LT
1184 ifp->if_flags &= ~XFS_IFEXTENTS;
1185 return error;
1186 }
a6f64d4a 1187 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
1da177e4
LT
1188 return 0;
1189}
1190
1191/*
1192 * Allocate an inode on disk and return a copy of its in-core version.
1193 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1194 * appropriately within the inode. The uid and gid for the inode are
1195 * set according to the contents of the given cred structure.
1196 *
1197 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
cd856db6
CM
1198 * has a free inode available, call xfs_iget() to obtain the in-core
1199 * version of the allocated inode. Finally, fill in the inode and
1200 * log its initial contents. In this case, ialloc_context would be
1201 * set to NULL.
1da177e4 1202 *
cd856db6
CM
1203 * If xfs_dialloc() does not have an available inode, it will replenish
1204 * its supply by doing an allocation. Since we can only do one
1205 * allocation within a transaction without deadlocks, we must commit
1206 * the current transaction before returning the inode itself.
1207 * In this case, therefore, we will set ialloc_context and return.
1da177e4
LT
1208 * The caller should then commit the current transaction, start a new
1209 * transaction, and call xfs_ialloc() again to actually get the inode.
1210 *
1211 * To ensure that some other process does not grab the inode that
1212 * was allocated during the first call to xfs_ialloc(), this routine
1213 * also returns the [locked] bp pointing to the head of the freelist
1214 * as ialloc_context. The caller should hold this buffer across
1215 * the commit and pass it back into this routine on the second call.
b11f94d5
DC
1216 *
1217 * If we are allocating quota inodes, we do not have a parent inode
1218 * to attach to or associate with (i.e. pip == NULL) because they
1219 * are not linked into the directory structure - they are attached
1220 * directly to the superblock - and so have no parent.
1da177e4
LT
1221 */
1222int
1223xfs_ialloc(
1224 xfs_trans_t *tp,
1225 xfs_inode_t *pip,
576b1d67 1226 umode_t mode,
31b084ae 1227 xfs_nlink_t nlink,
1da177e4 1228 xfs_dev_t rdev,
6743099c 1229 prid_t prid,
1da177e4
LT
1230 int okalloc,
1231 xfs_buf_t **ialloc_context,
1da177e4
LT
1232 xfs_inode_t **ipp)
1233{
93848a99 1234 struct xfs_mount *mp = tp->t_mountp;
1da177e4
LT
1235 xfs_ino_t ino;
1236 xfs_inode_t *ip;
1da177e4
LT
1237 uint flags;
1238 int error;
dff35fd4 1239 timespec_t tv;
bf904248 1240 int filestreams = 0;
1da177e4
LT
1241
1242 /*
1243 * Call the space management code to pick
1244 * the on-disk inode to be allocated.
1245 */
b11f94d5 1246 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
08358906 1247 ialloc_context, &ino);
bf904248 1248 if (error)
1da177e4 1249 return error;
08358906 1250 if (*ialloc_context || ino == NULLFSINO) {
1da177e4
LT
1251 *ipp = NULL;
1252 return 0;
1253 }
1254 ASSERT(*ialloc_context == NULL);
1255
1256 /*
1257 * Get the in-core inode with the lock held exclusively.
1258 * This is because we're setting fields here we need
1259 * to prevent others from looking at until we're done.
1260 */
93848a99 1261 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
ec3ba85f 1262 XFS_ILOCK_EXCL, &ip);
bf904248 1263 if (error)
1da177e4 1264 return error;
1da177e4
LT
1265 ASSERT(ip != NULL);
1266
576b1d67 1267 ip->i_d.di_mode = mode;
1da177e4
LT
1268 ip->i_d.di_onlink = 0;
1269 ip->i_d.di_nlink = nlink;
1270 ASSERT(ip->i_d.di_nlink == nlink);
9e2b2dc4
DH
1271 ip->i_d.di_uid = current_fsuid();
1272 ip->i_d.di_gid = current_fsgid();
6743099c 1273 xfs_set_projid(ip, prid);
1da177e4
LT
1274 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1275
1276 /*
1277 * If the superblock version is up to where we support new format
1278 * inodes and this is currently an old format inode, then change
1279 * the inode version number now. This way we only do the conversion
1280 * here rather than here and in the flush/logging code.
1281 */
93848a99 1282 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
51ce16d5
CH
1283 ip->i_d.di_version == 1) {
1284 ip->i_d.di_version = 2;
1da177e4
LT
1285 /*
1286 * We've already zeroed the old link count, the projid field,
1287 * and the pad field.
1288 */
1289 }
1290
1291 /*
1292 * Project ids won't be stored on disk if we are using a version 1 inode.
1293 */
51ce16d5 1294 if ((prid != 0) && (ip->i_d.di_version == 1))
1da177e4
LT
1295 xfs_bump_ino_vers2(tp, ip);
1296
bd186aa9 1297 if (pip && XFS_INHERIT_GID(pip)) {
1da177e4 1298 ip->i_d.di_gid = pip->i_d.di_gid;
abbede1b 1299 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1da177e4
LT
1300 ip->i_d.di_mode |= S_ISGID;
1301 }
1302 }
1303
1304 /*
1305 * If the group ID of the new file does not match the effective group
1306 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1307 * (and only if the irix_sgid_inherit compatibility variable is set).
1308 */
1309 if ((irix_sgid_inherit) &&
1310 (ip->i_d.di_mode & S_ISGID) &&
1311 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1312 ip->i_d.di_mode &= ~S_ISGID;
1313 }
1314
1315 ip->i_d.di_size = 0;
1316 ip->i_d.di_nextents = 0;
1317 ASSERT(ip->i_d.di_nblocks == 0);
dff35fd4
CH
1318
1319 nanotime(&tv);
1320 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1321 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1322 ip->i_d.di_atime = ip->i_d.di_mtime;
1323 ip->i_d.di_ctime = ip->i_d.di_mtime;
1324
1da177e4
LT
1325 /*
1326 * di_gen will have been taken care of in xfs_iread.
1327 */
1328 ip->i_d.di_extsize = 0;
1329 ip->i_d.di_dmevmask = 0;
1330 ip->i_d.di_dmstate = 0;
1331 ip->i_d.di_flags = 0;
93848a99
CH
1332
1333 if (ip->i_d.di_version == 3) {
1334 ASSERT(ip->i_d.di_ino == ino);
1335 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
1336 ip->i_d.di_crc = 0;
1337 ip->i_d.di_changecount = 1;
1338 ip->i_d.di_lsn = 0;
1339 ip->i_d.di_flags2 = 0;
1340 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
1341 ip->i_d.di_crtime = ip->i_d.di_mtime;
1342 }
1343
1344
1da177e4
LT
1345 flags = XFS_ILOG_CORE;
1346 switch (mode & S_IFMT) {
1347 case S_IFIFO:
1348 case S_IFCHR:
1349 case S_IFBLK:
1350 case S_IFSOCK:
1351 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1352 ip->i_df.if_u2.if_rdev = rdev;
1353 ip->i_df.if_flags = 0;
1354 flags |= XFS_ILOG_DEV;
1355 break;
1356 case S_IFREG:
bf904248
DC
1357 /*
1358 * we can't set up filestreams until after the VFS inode
1359 * is set up properly.
1360 */
1361 if (pip && xfs_inode_is_filestream(pip))
1362 filestreams = 1;
2a82b8be 1363 /* fall through */
1da177e4 1364 case S_IFDIR:
b11f94d5 1365 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
365ca83d
NS
1366 uint di_flags = 0;
1367
abbede1b 1368 if (S_ISDIR(mode)) {
365ca83d
NS
1369 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1370 di_flags |= XFS_DIFLAG_RTINHERIT;
dd9f438e
NS
1371 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1372 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1373 ip->i_d.di_extsize = pip->i_d.di_extsize;
1374 }
abbede1b 1375 } else if (S_ISREG(mode)) {
613d7043 1376 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
365ca83d 1377 di_flags |= XFS_DIFLAG_REALTIME;
dd9f438e
NS
1378 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1379 di_flags |= XFS_DIFLAG_EXTSIZE;
1380 ip->i_d.di_extsize = pip->i_d.di_extsize;
1381 }
1da177e4
LT
1382 }
1383 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1384 xfs_inherit_noatime)
365ca83d 1385 di_flags |= XFS_DIFLAG_NOATIME;
1da177e4
LT
1386 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1387 xfs_inherit_nodump)
365ca83d 1388 di_flags |= XFS_DIFLAG_NODUMP;
1da177e4
LT
1389 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1390 xfs_inherit_sync)
365ca83d 1391 di_flags |= XFS_DIFLAG_SYNC;
1da177e4
LT
1392 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1393 xfs_inherit_nosymlinks)
365ca83d
NS
1394 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1395 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1396 di_flags |= XFS_DIFLAG_PROJINHERIT;
d3446eac
BN
1397 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1398 xfs_inherit_nodefrag)
1399 di_flags |= XFS_DIFLAG_NODEFRAG;
2a82b8be
DC
1400 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1401 di_flags |= XFS_DIFLAG_FILESTREAM;
365ca83d 1402 ip->i_d.di_flags |= di_flags;
1da177e4
LT
1403 }
1404 /* FALLTHROUGH */
1405 case S_IFLNK:
1406 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1407 ip->i_df.if_flags = XFS_IFEXTENTS;
1408 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1409 ip->i_df.if_u1.if_extents = NULL;
1410 break;
1411 default:
1412 ASSERT(0);
1413 }
1414 /*
1415 * Attribute fork settings for new inode.
1416 */
1417 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1418 ip->i_d.di_anextents = 0;
1419
1420 /*
1421 * Log the new values stuffed into the inode.
1422 */
ddc3415a 1423 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4
LT
1424 xfs_trans_log_inode(tp, ip, flags);
1425
b83bd138 1426 /* now that we have an i_mode we can setup inode ops and unlock */
41be8bed 1427 xfs_setup_inode(ip);
1da177e4 1428
bf904248
DC
1429 /* now we have set up the vfs inode we can associate the filestream */
1430 if (filestreams) {
1431 error = xfs_filestream_associate(pip, ip);
1432 if (error < 0)
1433 return -error;
1434 if (!error)
1435 xfs_iflags_set(ip, XFS_IFILESTREAM);
1436 }
1437
1da177e4
LT
1438 *ipp = ip;
1439 return 0;
1440}
1441
1da177e4 1442/*
8f04c47a
CH
1443 * Free up the underlying blocks past new_size. The new size must be smaller
1444 * than the current size. This routine can be used both for the attribute and
1445 * data fork, and does not modify the inode size, which is left to the caller.
1da177e4 1446 *
f6485057
DC
1447 * The transaction passed to this routine must have made a permanent log
1448 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1449 * given transaction and start new ones, so make sure everything involved in
1450 * the transaction is tidy before calling here. Some transaction will be
1451 * returned to the caller to be committed. The incoming transaction must
1452 * already include the inode, and both inode locks must be held exclusively.
1453 * The inode must also be "held" within the transaction. On return the inode
1454 * will be "held" within the returned transaction. This routine does NOT
1455 * require any disk space to be reserved for it within the transaction.
1da177e4 1456 *
f6485057
DC
1457 * If we get an error, we must return with the inode locked and linked into the
1458 * current transaction. This keeps things simple for the higher level code,
1459 * because it always knows that the inode is locked and held in the transaction
1460 * that returns to it whether errors occur or not. We don't mark the inode
1461 * dirty on error so that transactions can be easily aborted if possible.
1da177e4
LT
1462 */
1463int
8f04c47a
CH
1464xfs_itruncate_extents(
1465 struct xfs_trans **tpp,
1466 struct xfs_inode *ip,
1467 int whichfork,
1468 xfs_fsize_t new_size)
1da177e4 1469{
8f04c47a
CH
1470 struct xfs_mount *mp = ip->i_mount;
1471 struct xfs_trans *tp = *tpp;
1472 struct xfs_trans *ntp;
1473 xfs_bmap_free_t free_list;
1474 xfs_fsblock_t first_block;
1475 xfs_fileoff_t first_unmap_block;
1476 xfs_fileoff_t last_block;
1477 xfs_filblks_t unmap_len;
1478 int committed;
1479 int error = 0;
1480 int done = 0;
1da177e4 1481
0b56185b
CH
1482 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1483 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1484 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
ce7ae151 1485 ASSERT(new_size <= XFS_ISIZE(ip));
8f04c47a 1486 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 1487 ASSERT(ip->i_itemp != NULL);
898621d5 1488 ASSERT(ip->i_itemp->ili_lock_flags == 0);
8f04c47a 1489 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1da177e4 1490
673e8e59
CH
1491 trace_xfs_itruncate_extents_start(ip, new_size);
1492
1da177e4
LT
1493 /*
1494 * Since it is possible for space to become allocated beyond
1495 * the end of the file (in a crash where the space is allocated
1496 * but the inode size is not yet updated), simply remove any
1497 * blocks which show up between the new EOF and the maximum
1498 * possible file size. If the first block to be removed is
1499 * beyond the maximum file size (ie it is the same as last_block),
1500 * then there is nothing to do.
1501 */
8f04c47a 1502 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
32972383 1503 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
8f04c47a
CH
1504 if (first_unmap_block == last_block)
1505 return 0;
1506
1507 ASSERT(first_unmap_block < last_block);
1508 unmap_len = last_block - first_unmap_block + 1;
1da177e4 1509 while (!done) {
9d87c319 1510 xfs_bmap_init(&free_list, &first_block);
8f04c47a 1511 error = xfs_bunmapi(tp, ip,
3e57ecf6 1512 first_unmap_block, unmap_len,
8f04c47a 1513 xfs_bmapi_aflag(whichfork),
1da177e4 1514 XFS_ITRUNC_MAX_EXTENTS,
3e57ecf6 1515 &first_block, &free_list,
b4e9181e 1516 &done);
8f04c47a
CH
1517 if (error)
1518 goto out_bmap_cancel;
1da177e4
LT
1519
1520 /*
1521 * Duplicate the transaction that has the permanent
1522 * reservation and commit the old transaction.
1523 */
8f04c47a 1524 error = xfs_bmap_finish(&tp, &free_list, &committed);
898621d5 1525 if (committed)
ddc3415a 1526 xfs_trans_ijoin(tp, ip, 0);
8f04c47a
CH
1527 if (error)
1528 goto out_bmap_cancel;
1da177e4
LT
1529
1530 if (committed) {
1531 /*
f6485057 1532 * Mark the inode dirty so it will be logged and
e5720eec 1533 * moved forward in the log as part of every commit.
1da177e4 1534 */
8f04c47a 1535 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1da177e4 1536 }
f6485057 1537
8f04c47a
CH
1538 ntp = xfs_trans_dup(tp);
1539 error = xfs_trans_commit(tp, 0);
1540 tp = ntp;
e5720eec 1541
ddc3415a 1542 xfs_trans_ijoin(tp, ip, 0);
f6485057 1543
cc09c0dc 1544 if (error)
8f04c47a
CH
1545 goto out;
1546
cc09c0dc 1547 /*
8f04c47a 1548 * Transaction commit worked ok so we can drop the extra ticket
cc09c0dc
DC
1549 * reference that we gained in xfs_trans_dup()
1550 */
8f04c47a
CH
1551 xfs_log_ticket_put(tp->t_ticket);
1552 error = xfs_trans_reserve(tp, 0,
f6485057
DC
1553 XFS_ITRUNCATE_LOG_RES(mp), 0,
1554 XFS_TRANS_PERM_LOG_RES,
1555 XFS_ITRUNCATE_LOG_COUNT);
1556 if (error)
8f04c47a 1557 goto out;
1da177e4 1558 }
8f04c47a 1559
673e8e59
CH
1560 /*
1561 * Always re-log the inode so that our permanent transaction can keep
1562 * on rolling it forward in the log.
1563 */
1564 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1565
1566 trace_xfs_itruncate_extents_end(ip, new_size);
1567
8f04c47a
CH
1568out:
1569 *tpp = tp;
1570 return error;
1571out_bmap_cancel:
1da177e4 1572 /*
8f04c47a
CH
1573 * If the bunmapi call encounters an error, return to the caller where
1574 * the transaction can be properly aborted. We just need to make sure
1575 * we're not holding any resources that we were not when we came in.
1da177e4 1576 */
8f04c47a
CH
1577 xfs_bmap_cancel(&free_list);
1578 goto out;
1579}
1580
1da177e4
LT
1581/*
1582 * This is called when the inode's link count goes to 0.
1583 * We place the on-disk inode on a list in the AGI. It
1584 * will be pulled from this list when the inode is freed.
1585 */
1586int
1587xfs_iunlink(
1588 xfs_trans_t *tp,
1589 xfs_inode_t *ip)
1590{
1591 xfs_mount_t *mp;
1592 xfs_agi_t *agi;
1593 xfs_dinode_t *dip;
1594 xfs_buf_t *agibp;
1595 xfs_buf_t *ibp;
1da177e4
LT
1596 xfs_agino_t agino;
1597 short bucket_index;
1598 int offset;
1599 int error;
1da177e4
LT
1600
1601 ASSERT(ip->i_d.di_nlink == 0);
1602 ASSERT(ip->i_d.di_mode != 0);
1da177e4
LT
1603
1604 mp = tp->t_mountp;
1605
1da177e4
LT
1606 /*
1607 * Get the agi buffer first. It ensures lock ordering
1608 * on the list.
1609 */
5e1be0fb 1610 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
859d7182 1611 if (error)
1da177e4 1612 return error;
1da177e4 1613 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 1614
1da177e4
LT
1615 /*
1616 * Get the index into the agi hash table for the
1617 * list this inode will go on.
1618 */
1619 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1620 ASSERT(agino != 0);
1621 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1622 ASSERT(agi->agi_unlinked[bucket_index]);
16259e7d 1623 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1da177e4 1624
69ef921b 1625 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1da177e4
LT
1626 /*
1627 * There is already another inode in the bucket we need
1628 * to add ourselves to. Add us at the front of the list.
1629 * Here we put the head pointer into our next pointer,
1630 * and then we fall through to point the head at us.
1631 */
475ee413
CH
1632 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1633 0, 0);
c319b58b
VA
1634 if (error)
1635 return error;
1636
69ef921b 1637 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1da177e4 1638 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
92bfc6e7 1639 offset = ip->i_imap.im_boffset +
1da177e4 1640 offsetof(xfs_dinode_t, di_next_unlinked);
ad868afd
DC
1641
1642 /* need to recalc the inode CRC if appropriate */
1643 xfs_dinode_calc_crc(mp, dip);
1644
1da177e4
LT
1645 xfs_trans_inode_buf(tp, ibp);
1646 xfs_trans_log_buf(tp, ibp, offset,
1647 (offset + sizeof(xfs_agino_t) - 1));
1648 xfs_inobp_check(mp, ibp);
1649 }
1650
1651 /*
1652 * Point the bucket head pointer at the inode being inserted.
1653 */
1654 ASSERT(agino != 0);
16259e7d 1655 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1da177e4
LT
1656 offset = offsetof(xfs_agi_t, agi_unlinked) +
1657 (sizeof(xfs_agino_t) * bucket_index);
70c0c8b3 1658 xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
1da177e4
LT
1659 xfs_trans_log_buf(tp, agibp, offset,
1660 (offset + sizeof(xfs_agino_t) - 1));
1661 return 0;
1662}
1663
1664/*
1665 * Pull the on-disk inode from the AGI unlinked list.
1666 */
1667STATIC int
1668xfs_iunlink_remove(
1669 xfs_trans_t *tp,
1670 xfs_inode_t *ip)
1671{
1672 xfs_ino_t next_ino;
1673 xfs_mount_t *mp;
1674 xfs_agi_t *agi;
1675 xfs_dinode_t *dip;
1676 xfs_buf_t *agibp;
1677 xfs_buf_t *ibp;
1678 xfs_agnumber_t agno;
1da177e4
LT
1679 xfs_agino_t agino;
1680 xfs_agino_t next_agino;
1681 xfs_buf_t *last_ibp;
6fdf8ccc 1682 xfs_dinode_t *last_dip = NULL;
1da177e4 1683 short bucket_index;
6fdf8ccc 1684 int offset, last_offset = 0;
1da177e4 1685 int error;
1da177e4 1686
1da177e4 1687 mp = tp->t_mountp;
1da177e4 1688 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1da177e4
LT
1689
1690 /*
1691 * Get the agi buffer first. It ensures lock ordering
1692 * on the list.
1693 */
5e1be0fb
CH
1694 error = xfs_read_agi(mp, tp, agno, &agibp);
1695 if (error)
1da177e4 1696 return error;
5e1be0fb 1697
1da177e4 1698 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 1699
1da177e4
LT
1700 /*
1701 * Get the index into the agi hash table for the
1702 * list this inode will go on.
1703 */
1704 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1705 ASSERT(agino != 0);
1706 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
69ef921b 1707 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1da177e4
LT
1708 ASSERT(agi->agi_unlinked[bucket_index]);
1709
16259e7d 1710 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1da177e4 1711 /*
475ee413
CH
1712 * We're at the head of the list. Get the inode's on-disk
1713 * buffer to see if there is anyone after us on the list.
1714 * Only modify our next pointer if it is not already NULLAGINO.
1715 * This saves us the overhead of dealing with the buffer when
1716 * there is no need to change it.
1da177e4 1717 */
475ee413
CH
1718 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1719 0, 0);
1da177e4 1720 if (error) {
475ee413 1721 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 1722 __func__, error);
1da177e4
LT
1723 return error;
1724 }
347d1c01 1725 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
1726 ASSERT(next_agino != 0);
1727 if (next_agino != NULLAGINO) {
347d1c01 1728 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 1729 offset = ip->i_imap.im_boffset +
1da177e4 1730 offsetof(xfs_dinode_t, di_next_unlinked);
ad868afd
DC
1731
1732 /* need to recalc the inode CRC if appropriate */
1733 xfs_dinode_calc_crc(mp, dip);
1734
1da177e4
LT
1735 xfs_trans_inode_buf(tp, ibp);
1736 xfs_trans_log_buf(tp, ibp, offset,
1737 (offset + sizeof(xfs_agino_t) - 1));
1738 xfs_inobp_check(mp, ibp);
1739 } else {
1740 xfs_trans_brelse(tp, ibp);
1741 }
1742 /*
1743 * Point the bucket head pointer at the next inode.
1744 */
1745 ASSERT(next_agino != 0);
1746 ASSERT(next_agino != agino);
16259e7d 1747 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1da177e4
LT
1748 offset = offsetof(xfs_agi_t, agi_unlinked) +
1749 (sizeof(xfs_agino_t) * bucket_index);
70c0c8b3 1750 xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
1da177e4
LT
1751 xfs_trans_log_buf(tp, agibp, offset,
1752 (offset + sizeof(xfs_agino_t) - 1));
1753 } else {
1754 /*
1755 * We need to search the list for the inode being freed.
1756 */
16259e7d 1757 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1da177e4
LT
1758 last_ibp = NULL;
1759 while (next_agino != agino) {
129dbc9a
CH
1760 struct xfs_imap imap;
1761
1762 if (last_ibp)
1da177e4 1763 xfs_trans_brelse(tp, last_ibp);
129dbc9a
CH
1764
1765 imap.im_blkno = 0;
1da177e4 1766 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
129dbc9a
CH
1767
1768 error = xfs_imap(mp, tp, next_ino, &imap, 0);
1769 if (error) {
1770 xfs_warn(mp,
1771 "%s: xfs_imap returned error %d.",
1772 __func__, error);
1773 return error;
1774 }
1775
1776 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1777 &last_ibp, 0, 0);
1da177e4 1778 if (error) {
0b932ccc 1779 xfs_warn(mp,
129dbc9a 1780 "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 1781 __func__, error);
1da177e4
LT
1782 return error;
1783 }
129dbc9a
CH
1784
1785 last_offset = imap.im_boffset;
347d1c01 1786 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1da177e4
LT
1787 ASSERT(next_agino != NULLAGINO);
1788 ASSERT(next_agino != 0);
1789 }
475ee413 1790
1da177e4 1791 /*
475ee413
CH
1792 * Now last_ibp points to the buffer previous to us on the
1793 * unlinked list. Pull us from the list.
1da177e4 1794 */
475ee413
CH
1795 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1796 0, 0);
1da177e4 1797 if (error) {
475ee413 1798 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
0b932ccc 1799 __func__, error);
1da177e4
LT
1800 return error;
1801 }
347d1c01 1802 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
1803 ASSERT(next_agino != 0);
1804 ASSERT(next_agino != agino);
1805 if (next_agino != NULLAGINO) {
347d1c01 1806 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 1807 offset = ip->i_imap.im_boffset +
1da177e4 1808 offsetof(xfs_dinode_t, di_next_unlinked);
ad868afd
DC
1809
1810 /* need to recalc the inode CRC if appropriate */
1811 xfs_dinode_calc_crc(mp, dip);
1812
1da177e4
LT
1813 xfs_trans_inode_buf(tp, ibp);
1814 xfs_trans_log_buf(tp, ibp, offset,
1815 (offset + sizeof(xfs_agino_t) - 1));
1816 xfs_inobp_check(mp, ibp);
1817 } else {
1818 xfs_trans_brelse(tp, ibp);
1819 }
1820 /*
1821 * Point the previous inode on the list to the next inode.
1822 */
347d1c01 1823 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1da177e4
LT
1824 ASSERT(next_agino != 0);
1825 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
ad868afd
DC
1826
1827 /* need to recalc the inode CRC if appropriate */
1828 xfs_dinode_calc_crc(mp, last_dip);
1829
1da177e4
LT
1830 xfs_trans_inode_buf(tp, last_ibp);
1831 xfs_trans_log_buf(tp, last_ibp, offset,
1832 (offset + sizeof(xfs_agino_t) - 1));
1833 xfs_inobp_check(mp, last_ibp);
1834 }
1835 return 0;
1836}
1837
5b3eed75
DC
1838/*
1839 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1840 * inodes that are in memory - they all must be marked stale and attached to
1841 * the cluster buffer.
1842 */
2a30f36d 1843STATIC int
1da177e4
LT
1844xfs_ifree_cluster(
1845 xfs_inode_t *free_ip,
1846 xfs_trans_t *tp,
1847 xfs_ino_t inum)
1848{
1849 xfs_mount_t *mp = free_ip->i_mount;
1850 int blks_per_cluster;
1851 int nbufs;
1852 int ninodes;
5b257b4a 1853 int i, j;
1da177e4
LT
1854 xfs_daddr_t blkno;
1855 xfs_buf_t *bp;
5b257b4a 1856 xfs_inode_t *ip;
1da177e4
LT
1857 xfs_inode_log_item_t *iip;
1858 xfs_log_item_t *lip;
5017e97d 1859 struct xfs_perag *pag;
1da177e4 1860
5017e97d 1861 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1da177e4
LT
1862 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1863 blks_per_cluster = 1;
1864 ninodes = mp->m_sb.sb_inopblock;
1865 nbufs = XFS_IALLOC_BLOCKS(mp);
1866 } else {
1867 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1868 mp->m_sb.sb_blocksize;
1869 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1870 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1871 }
1872
1da177e4
LT
1873 for (j = 0; j < nbufs; j++, inum += ninodes) {
1874 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1875 XFS_INO_TO_AGBNO(mp, inum));
1876
5b257b4a
DC
1877 /*
1878 * We obtain and lock the backing buffer first in the process
1879 * here, as we have to ensure that any dirty inode that we
1880 * can't get the flush lock on is attached to the buffer.
1881 * If we scan the in-memory inodes first, then buffer IO can
1882 * complete before we get a lock on it, and hence we may fail
1883 * to mark all the active inodes on the buffer stale.
1884 */
1885 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
b6aff29f
DC
1886 mp->m_bsize * blks_per_cluster,
1887 XBF_UNMAPPED);
5b257b4a 1888
2a30f36d
CS
1889 if (!bp)
1890 return ENOMEM;
b0f539de
DC
1891
1892 /*
1893 * This buffer may not have been correctly initialised as we
1894 * didn't read it from disk. That's not important because we are
1895 * only using to mark the buffer as stale in the log, and to
1896 * attach stale cached inodes on it. That means it will never be
1897 * dispatched for IO. If it is, we want to know about it, and we
1898 * want it to fail. We can acheive this by adding a write
1899 * verifier to the buffer.
1900 */
1813dd64 1901 bp->b_ops = &xfs_inode_buf_ops;
b0f539de 1902
5b257b4a
DC
1903 /*
1904 * Walk the inodes already attached to the buffer and mark them
1905 * stale. These will all have the flush locks held, so an
5b3eed75
DC
1906 * in-memory inode walk can't lock them. By marking them all
1907 * stale first, we will not attempt to lock them in the loop
1908 * below as the XFS_ISTALE flag will be set.
5b257b4a 1909 */
adadbeef 1910 lip = bp->b_fspriv;
5b257b4a
DC
1911 while (lip) {
1912 if (lip->li_type == XFS_LI_INODE) {
1913 iip = (xfs_inode_log_item_t *)lip;
1914 ASSERT(iip->ili_logged == 1);
ca30b2a7 1915 lip->li_cb = xfs_istale_done;
5b257b4a
DC
1916 xfs_trans_ail_copy_lsn(mp->m_ail,
1917 &iip->ili_flush_lsn,
1918 &iip->ili_item.li_lsn);
1919 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
5b257b4a
DC
1920 }
1921 lip = lip->li_bio_list;
1922 }
1da177e4 1923
5b3eed75 1924
1da177e4 1925 /*
5b257b4a
DC
1926 * For each inode in memory attempt to add it to the inode
1927 * buffer and set it up for being staled on buffer IO
1928 * completion. This is safe as we've locked out tail pushing
1929 * and flushing by locking the buffer.
1da177e4 1930 *
5b257b4a
DC
1931 * We have already marked every inode that was part of a
1932 * transaction stale above, which means there is no point in
1933 * even trying to lock them.
1da177e4 1934 */
1da177e4 1935 for (i = 0; i < ninodes; i++) {
5b3eed75 1936retry:
1a3e8f3d 1937 rcu_read_lock();
da353b0d
DC
1938 ip = radix_tree_lookup(&pag->pag_ici_root,
1939 XFS_INO_TO_AGINO(mp, (inum + i)));
1da177e4 1940
1a3e8f3d
DC
1941 /* Inode not in memory, nothing to do */
1942 if (!ip) {
1943 rcu_read_unlock();
1da177e4
LT
1944 continue;
1945 }
1946
1a3e8f3d
DC
1947 /*
1948 * because this is an RCU protected lookup, we could
1949 * find a recently freed or even reallocated inode
1950 * during the lookup. We need to check under the
1951 * i_flags_lock for a valid inode here. Skip it if it
1952 * is not valid, the wrong inode or stale.
1953 */
1954 spin_lock(&ip->i_flags_lock);
1955 if (ip->i_ino != inum + i ||
1956 __xfs_iflags_test(ip, XFS_ISTALE)) {
1957 spin_unlock(&ip->i_flags_lock);
1958 rcu_read_unlock();
1959 continue;
1960 }
1961 spin_unlock(&ip->i_flags_lock);
1962
5b3eed75
DC
1963 /*
1964 * Don't try to lock/unlock the current inode, but we
1965 * _cannot_ skip the other inodes that we did not find
1966 * in the list attached to the buffer and are not
1967 * already marked stale. If we can't lock it, back off
1968 * and retry.
1969 */
5b257b4a
DC
1970 if (ip != free_ip &&
1971 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1a3e8f3d 1972 rcu_read_unlock();
5b3eed75
DC
1973 delay(1);
1974 goto retry;
1da177e4 1975 }
1a3e8f3d 1976 rcu_read_unlock();
1da177e4 1977
5b3eed75 1978 xfs_iflock(ip);
5b257b4a 1979 xfs_iflags_set(ip, XFS_ISTALE);
1da177e4 1980
5b3eed75
DC
1981 /*
1982 * we don't need to attach clean inodes or those only
1983 * with unlogged changes (which we throw away, anyway).
1984 */
1da177e4 1985 iip = ip->i_itemp;
5b3eed75 1986 if (!iip || xfs_inode_clean(ip)) {
5b257b4a 1987 ASSERT(ip != free_ip);
1da177e4
LT
1988 xfs_ifunlock(ip);
1989 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1990 continue;
1991 }
1992
f5d8d5c4
CH
1993 iip->ili_last_fields = iip->ili_fields;
1994 iip->ili_fields = 0;
1da177e4 1995 iip->ili_logged = 1;
7b2e2a31
DC
1996 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1997 &iip->ili_item.li_lsn);
1da177e4 1998
ca30b2a7
CH
1999 xfs_buf_attach_iodone(bp, xfs_istale_done,
2000 &iip->ili_item);
5b257b4a
DC
2001
2002 if (ip != free_ip)
1da177e4 2003 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
2004 }
2005
5b3eed75 2006 xfs_trans_stale_inode_buf(tp, bp);
1da177e4
LT
2007 xfs_trans_binval(tp, bp);
2008 }
2009
5017e97d 2010 xfs_perag_put(pag);
2a30f36d 2011 return 0;
1da177e4
LT
2012}
2013
2014/*
2015 * This is called to return an inode to the inode free list.
2016 * The inode should already be truncated to 0 length and have
2017 * no pages associated with it. This routine also assumes that
2018 * the inode is already a part of the transaction.
2019 *
2020 * The on-disk copy of the inode will have been added to the list
2021 * of unlinked inodes in the AGI. We need to remove the inode from
2022 * that list atomically with respect to freeing it here.
2023 */
2024int
2025xfs_ifree(
2026 xfs_trans_t *tp,
2027 xfs_inode_t *ip,
2028 xfs_bmap_free_t *flist)
2029{
2030 int error;
2031 int delete;
2032 xfs_ino_t first_ino;
c319b58b
VA
2033 xfs_dinode_t *dip;
2034 xfs_buf_t *ibp;
1da177e4 2035
579aa9ca 2036 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
2037 ASSERT(ip->i_d.di_nlink == 0);
2038 ASSERT(ip->i_d.di_nextents == 0);
2039 ASSERT(ip->i_d.di_anextents == 0);
ce7ae151 2040 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
1da177e4
LT
2041 ASSERT(ip->i_d.di_nblocks == 0);
2042
2043 /*
2044 * Pull the on-disk inode from the AGI unlinked list.
2045 */
2046 error = xfs_iunlink_remove(tp, ip);
2047 if (error != 0) {
2048 return error;
2049 }
2050
2051 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2052 if (error != 0) {
2053 return error;
2054 }
2055 ip->i_d.di_mode = 0; /* mark incore inode as free */
2056 ip->i_d.di_flags = 0;
2057 ip->i_d.di_dmevmask = 0;
2058 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
1da177e4
LT
2059 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2060 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2061 /*
2062 * Bump the generation count so no one will be confused
2063 * by reincarnations of this inode.
2064 */
2065 ip->i_d.di_gen++;
c319b58b 2066
1da177e4
LT
2067 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2068
475ee413
CH
2069 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
2070 0, 0);
c319b58b
VA
2071 if (error)
2072 return error;
2073
2074 /*
2075 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
2076 * from picking up this inode when it is reclaimed (its incore state
2077 * initialzed but not flushed to disk yet). The in-core di_mode is
2078 * already cleared and a corresponding transaction logged.
2079 * The hack here just synchronizes the in-core to on-disk
2080 * di_mode value in advance before the actual inode sync to disk.
2081 * This is OK because the inode is already unlinked and would never
2082 * change its di_mode again for this inode generation.
2083 * This is a temporary hack that would require a proper fix
2084 * in the future.
2085 */
81591fe2 2086 dip->di_mode = 0;
c319b58b 2087
1da177e4 2088 if (delete) {
2a30f36d 2089 error = xfs_ifree_cluster(ip, tp, first_ino);
1da177e4
LT
2090 }
2091
2a30f36d 2092 return error;
1da177e4
LT
2093}
2094
2095/*
2096 * Reallocate the space for if_broot based on the number of records
2097 * being added or deleted as indicated in rec_diff. Move the records
2098 * and pointers in if_broot to fit the new size. When shrinking this
2099 * will eliminate holes between the records and pointers created by
2100 * the caller. When growing this will create holes to be filled in
2101 * by the caller.
2102 *
2103 * The caller must not request to add more records than would fit in
2104 * the on-disk inode root. If the if_broot is currently NULL, then
2105 * if we adding records one will be allocated. The caller must also
2106 * not request that the number of records go below zero, although
2107 * it can go to zero.
2108 *
2109 * ip -- the inode whose if_broot area is changing
2110 * ext_diff -- the change in the number of records, positive or negative,
2111 * requested for the if_broot array.
2112 */
2113void
2114xfs_iroot_realloc(
2115 xfs_inode_t *ip,
2116 int rec_diff,
2117 int whichfork)
2118{
60197e8d 2119 struct xfs_mount *mp = ip->i_mount;
1da177e4
LT
2120 int cur_max;
2121 xfs_ifork_t *ifp;
7cc95a82 2122 struct xfs_btree_block *new_broot;
1da177e4
LT
2123 int new_max;
2124 size_t new_size;
2125 char *np;
2126 char *op;
2127
2128 /*
2129 * Handle the degenerate case quietly.
2130 */
2131 if (rec_diff == 0) {
2132 return;
2133 }
2134
2135 ifp = XFS_IFORK_PTR(ip, whichfork);
2136 if (rec_diff > 0) {
2137 /*
2138 * If there wasn't any memory allocated before, just
2139 * allocate it now and get out.
2140 */
2141 if (ifp->if_broot_bytes == 0) {
ee1a47ab 2142 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
4a7edddc 2143 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1da177e4
LT
2144 ifp->if_broot_bytes = (int)new_size;
2145 return;
2146 }
2147
2148 /*
2149 * If there is already an existing if_broot, then we need
2150 * to realloc() it and shift the pointers to their new
2151 * location. The records don't change location because
2152 * they are kept butted up against the btree block header.
2153 */
60197e8d 2154 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1da177e4 2155 new_max = cur_max + rec_diff;
ee1a47ab 2156 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
7cc95a82 2157 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
ee1a47ab 2158 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
4a7edddc 2159 KM_SLEEP | KM_NOFS);
60197e8d
CH
2160 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2161 ifp->if_broot_bytes);
2162 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2163 (int)new_size);
1da177e4
LT
2164 ifp->if_broot_bytes = (int)new_size;
2165 ASSERT(ifp->if_broot_bytes <=
ee1a47ab 2166 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
1da177e4
LT
2167 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2168 return;
2169 }
2170
2171 /*
2172 * rec_diff is less than 0. In this case, we are shrinking the
2173 * if_broot buffer. It must already exist. If we go to zero
2174 * records, just get rid of the root and clear the status bit.
2175 */
2176 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
60197e8d 2177 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1da177e4
LT
2178 new_max = cur_max + rec_diff;
2179 ASSERT(new_max >= 0);
2180 if (new_max > 0)
ee1a47ab 2181 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
1da177e4
LT
2182 else
2183 new_size = 0;
2184 if (new_size > 0) {
4a7edddc 2185 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1da177e4
LT
2186 /*
2187 * First copy over the btree block header.
2188 */
ee1a47ab
CH
2189 memcpy(new_broot, ifp->if_broot,
2190 XFS_BMBT_BLOCK_LEN(ip->i_mount));
1da177e4
LT
2191 } else {
2192 new_broot = NULL;
2193 ifp->if_flags &= ~XFS_IFBROOT;
2194 }
2195
2196 /*
2197 * Only copy the records and pointers if there are any.
2198 */
2199 if (new_max > 0) {
2200 /*
2201 * First copy the records.
2202 */
136341b4
CH
2203 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2204 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
1da177e4
LT
2205 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2206
2207 /*
2208 * Then copy the pointers.
2209 */
60197e8d 2210 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1da177e4 2211 ifp->if_broot_bytes);
60197e8d 2212 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
1da177e4
LT
2213 (int)new_size);
2214 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2215 }
f0e2d93c 2216 kmem_free(ifp->if_broot);
1da177e4
LT
2217 ifp->if_broot = new_broot;
2218 ifp->if_broot_bytes = (int)new_size;
2219 ASSERT(ifp->if_broot_bytes <=
ee1a47ab 2220 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
1da177e4
LT
2221 return;
2222}
2223
2224
1da177e4
LT
2225/*
2226 * This is called when the amount of space needed for if_data
2227 * is increased or decreased. The change in size is indicated by
2228 * the number of bytes that need to be added or deleted in the
2229 * byte_diff parameter.
2230 *
2231 * If the amount of space needed has decreased below the size of the
2232 * inline buffer, then switch to using the inline buffer. Otherwise,
2233 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2234 * to what is needed.
2235 *
2236 * ip -- the inode whose if_data area is changing
2237 * byte_diff -- the change in the number of bytes, positive or negative,
2238 * requested for the if_data array.
2239 */
2240void
2241xfs_idata_realloc(
2242 xfs_inode_t *ip,
2243 int byte_diff,
2244 int whichfork)
2245{
2246 xfs_ifork_t *ifp;
2247 int new_size;
2248 int real_size;
2249
2250 if (byte_diff == 0) {
2251 return;
2252 }
2253
2254 ifp = XFS_IFORK_PTR(ip, whichfork);
2255 new_size = (int)ifp->if_bytes + byte_diff;
2256 ASSERT(new_size >= 0);
2257
2258 if (new_size == 0) {
2259 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
f0e2d93c 2260 kmem_free(ifp->if_u1.if_data);
1da177e4
LT
2261 }
2262 ifp->if_u1.if_data = NULL;
2263 real_size = 0;
2264 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2265 /*
2266 * If the valid extents/data can fit in if_inline_ext/data,
2267 * copy them from the malloc'd vector and free it.
2268 */
2269 if (ifp->if_u1.if_data == NULL) {
2270 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2271 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2272 ASSERT(ifp->if_real_bytes != 0);
2273 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2274 new_size);
f0e2d93c 2275 kmem_free(ifp->if_u1.if_data);
1da177e4
LT
2276 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2277 }
2278 real_size = 0;
2279 } else {
2280 /*
2281 * Stuck with malloc/realloc.
2282 * For inline data, the underlying buffer must be
2283 * a multiple of 4 bytes in size so that it can be
2284 * logged and stay on word boundaries. We enforce
2285 * that here.
2286 */
2287 real_size = roundup(new_size, 4);
2288 if (ifp->if_u1.if_data == NULL) {
2289 ASSERT(ifp->if_real_bytes == 0);
4a7edddc
DC
2290 ifp->if_u1.if_data = kmem_alloc(real_size,
2291 KM_SLEEP | KM_NOFS);
1da177e4
LT
2292 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2293 /*
2294 * Only do the realloc if the underlying size
2295 * is really changing.
2296 */
2297 if (ifp->if_real_bytes != real_size) {
2298 ifp->if_u1.if_data =
2299 kmem_realloc(ifp->if_u1.if_data,
2300 real_size,
2301 ifp->if_real_bytes,
4a7edddc 2302 KM_SLEEP | KM_NOFS);
1da177e4
LT
2303 }
2304 } else {
2305 ASSERT(ifp->if_real_bytes == 0);
4a7edddc
DC
2306 ifp->if_u1.if_data = kmem_alloc(real_size,
2307 KM_SLEEP | KM_NOFS);
1da177e4
LT
2308 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2309 ifp->if_bytes);
2310 }
2311 }
2312 ifp->if_real_bytes = real_size;
2313 ifp->if_bytes = new_size;
2314 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2315}
2316
1da177e4
LT
2317void
2318xfs_idestroy_fork(
2319 xfs_inode_t *ip,
2320 int whichfork)
2321{
2322 xfs_ifork_t *ifp;
2323
2324 ifp = XFS_IFORK_PTR(ip, whichfork);
2325 if (ifp->if_broot != NULL) {
f0e2d93c 2326 kmem_free(ifp->if_broot);
1da177e4
LT
2327 ifp->if_broot = NULL;
2328 }
2329
2330 /*
2331 * If the format is local, then we can't have an extents
2332 * array so just look for an inline data array. If we're
2333 * not local then we may or may not have an extents list,
2334 * so check and free it up if we do.
2335 */
2336 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2337 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2338 (ifp->if_u1.if_data != NULL)) {
2339 ASSERT(ifp->if_real_bytes != 0);
f0e2d93c 2340 kmem_free(ifp->if_u1.if_data);
1da177e4
LT
2341 ifp->if_u1.if_data = NULL;
2342 ifp->if_real_bytes = 0;
2343 }
2344 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
0293ce3a
MK
2345 ((ifp->if_flags & XFS_IFEXTIREC) ||
2346 ((ifp->if_u1.if_extents != NULL) &&
2347 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
1da177e4 2348 ASSERT(ifp->if_real_bytes != 0);
4eea22f0 2349 xfs_iext_destroy(ifp);
1da177e4
LT
2350 }
2351 ASSERT(ifp->if_u1.if_extents == NULL ||
2352 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2353 ASSERT(ifp->if_real_bytes == 0);
2354 if (whichfork == XFS_ATTR_FORK) {
2355 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2356 ip->i_afp = NULL;
2357 }
2358}
2359
1da177e4 2360/*
60ec6783
CH
2361 * This is called to unpin an inode. The caller must have the inode locked
2362 * in at least shared mode so that the buffer cannot be subsequently pinned
2363 * once someone is waiting for it to be unpinned.
1da177e4 2364 */
60ec6783 2365static void
f392e631 2366xfs_iunpin(
60ec6783 2367 struct xfs_inode *ip)
1da177e4 2368{
579aa9ca 2369 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1da177e4 2370
4aaf15d1
DC
2371 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2372
a3f74ffb 2373 /* Give the log a push to start the unpinning I/O */
60ec6783 2374 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
a14a348b 2375
a3f74ffb 2376}
1da177e4 2377
f392e631
CH
2378static void
2379__xfs_iunpin_wait(
2380 struct xfs_inode *ip)
2381{
2382 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2383 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2384
2385 xfs_iunpin(ip);
2386
2387 do {
2388 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2389 if (xfs_ipincount(ip))
2390 io_schedule();
2391 } while (xfs_ipincount(ip));
2392 finish_wait(wq, &wait.wait);
2393}
2394
777df5af 2395void
a3f74ffb 2396xfs_iunpin_wait(
60ec6783 2397 struct xfs_inode *ip)
a3f74ffb 2398{
f392e631
CH
2399 if (xfs_ipincount(ip))
2400 __xfs_iunpin_wait(ip);
1da177e4
LT
2401}
2402
1da177e4
LT
2403/*
2404 * xfs_iextents_copy()
2405 *
2406 * This is called to copy the REAL extents (as opposed to the delayed
2407 * allocation extents) from the inode into the given buffer. It
2408 * returns the number of bytes copied into the buffer.
2409 *
2410 * If there are no delayed allocation extents, then we can just
2411 * memcpy() the extents into the buffer. Otherwise, we need to
2412 * examine each extent in turn and skip those which are delayed.
2413 */
2414int
2415xfs_iextents_copy(
2416 xfs_inode_t *ip,
a6f64d4a 2417 xfs_bmbt_rec_t *dp,
1da177e4
LT
2418 int whichfork)
2419{
2420 int copied;
1da177e4
LT
2421 int i;
2422 xfs_ifork_t *ifp;
2423 int nrecs;
2424 xfs_fsblock_t start_block;
2425
2426 ifp = XFS_IFORK_PTR(ip, whichfork);
579aa9ca 2427 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1da177e4
LT
2428 ASSERT(ifp->if_bytes > 0);
2429
2430 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3a59c94c 2431 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
1da177e4
LT
2432 ASSERT(nrecs > 0);
2433
2434 /*
2435 * There are some delayed allocation extents in the
2436 * inode, so copy the extents one at a time and skip
2437 * the delayed ones. There must be at least one
2438 * non-delayed extent.
2439 */
1da177e4
LT
2440 copied = 0;
2441 for (i = 0; i < nrecs; i++) {
a6f64d4a 2442 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
1da177e4 2443 start_block = xfs_bmbt_get_startblock(ep);
9d87c319 2444 if (isnullstartblock(start_block)) {
1da177e4
LT
2445 /*
2446 * It's a delayed allocation extent, so skip it.
2447 */
1da177e4
LT
2448 continue;
2449 }
2450
2451 /* Translate to on disk format */
cd8b0a97
CH
2452 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2453 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
a6f64d4a 2454 dp++;
1da177e4
LT
2455 copied++;
2456 }
2457 ASSERT(copied != 0);
a6f64d4a 2458 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
1da177e4
LT
2459
2460 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2461}
2462
2463/*
2464 * Each of the following cases stores data into the same region
2465 * of the on-disk inode, so only one of them can be valid at
2466 * any given time. While it is possible to have conflicting formats
2467 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2468 * in EXTENTS format, this can only happen when the fork has
2469 * changed formats after being modified but before being flushed.
2470 * In these cases, the format always takes precedence, because the
2471 * format indicates the current state of the fork.
2472 */
2473/*ARGSUSED*/
e4ac967b 2474STATIC void
1da177e4
LT
2475xfs_iflush_fork(
2476 xfs_inode_t *ip,
2477 xfs_dinode_t *dip,
2478 xfs_inode_log_item_t *iip,
2479 int whichfork,
2480 xfs_buf_t *bp)
2481{
2482 char *cp;
2483 xfs_ifork_t *ifp;
2484 xfs_mount_t *mp;
1da177e4
LT
2485 static const short brootflag[2] =
2486 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2487 static const short dataflag[2] =
2488 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2489 static const short extflag[2] =
2490 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2491
e4ac967b
DC
2492 if (!iip)
2493 return;
1da177e4
LT
2494 ifp = XFS_IFORK_PTR(ip, whichfork);
2495 /*
2496 * This can happen if we gave up in iformat in an error path,
2497 * for the attribute fork.
2498 */
e4ac967b 2499 if (!ifp) {
1da177e4 2500 ASSERT(whichfork == XFS_ATTR_FORK);
e4ac967b 2501 return;
1da177e4
LT
2502 }
2503 cp = XFS_DFORK_PTR(dip, whichfork);
2504 mp = ip->i_mount;
2505 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2506 case XFS_DINODE_FMT_LOCAL:
f5d8d5c4 2507 if ((iip->ili_fields & dataflag[whichfork]) &&
1da177e4
LT
2508 (ifp->if_bytes > 0)) {
2509 ASSERT(ifp->if_u1.if_data != NULL);
2510 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2511 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2512 }
1da177e4
LT
2513 break;
2514
2515 case XFS_DINODE_FMT_EXTENTS:
2516 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
f5d8d5c4
CH
2517 !(iip->ili_fields & extflag[whichfork]));
2518 if ((iip->ili_fields & extflag[whichfork]) &&
1da177e4 2519 (ifp->if_bytes > 0)) {
ab1908a5 2520 ASSERT(xfs_iext_get_ext(ifp, 0));
1da177e4
LT
2521 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2522 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2523 whichfork);
2524 }
2525 break;
2526
2527 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 2528 if ((iip->ili_fields & brootflag[whichfork]) &&
1da177e4
LT
2529 (ifp->if_broot_bytes > 0)) {
2530 ASSERT(ifp->if_broot != NULL);
2531 ASSERT(ifp->if_broot_bytes <=
2532 (XFS_IFORK_SIZE(ip, whichfork) +
ee1a47ab 2533 XFS_BROOT_SIZE_ADJ(ip)));
60197e8d 2534 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
1da177e4
LT
2535 (xfs_bmdr_block_t *)cp,
2536 XFS_DFORK_SIZE(dip, mp, whichfork));
2537 }
2538 break;
2539
2540 case XFS_DINODE_FMT_DEV:
f5d8d5c4 2541 if (iip->ili_fields & XFS_ILOG_DEV) {
1da177e4 2542 ASSERT(whichfork == XFS_DATA_FORK);
81591fe2 2543 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
1da177e4
LT
2544 }
2545 break;
2546
2547 case XFS_DINODE_FMT_UUID:
f5d8d5c4 2548 if (iip->ili_fields & XFS_ILOG_UUID) {
1da177e4 2549 ASSERT(whichfork == XFS_DATA_FORK);
81591fe2
CH
2550 memcpy(XFS_DFORK_DPTR(dip),
2551 &ip->i_df.if_u2.if_uuid,
2552 sizeof(uuid_t));
1da177e4
LT
2553 }
2554 break;
2555
2556 default:
2557 ASSERT(0);
2558 break;
2559 }
1da177e4
LT
2560}
2561
bad55843
DC
2562STATIC int
2563xfs_iflush_cluster(
2564 xfs_inode_t *ip,
2565 xfs_buf_t *bp)
2566{
2567 xfs_mount_t *mp = ip->i_mount;
5017e97d 2568 struct xfs_perag *pag;
bad55843 2569 unsigned long first_index, mask;
c8f5f12e 2570 unsigned long inodes_per_cluster;
bad55843
DC
2571 int ilist_size;
2572 xfs_inode_t **ilist;
2573 xfs_inode_t *iq;
bad55843
DC
2574 int nr_found;
2575 int clcount = 0;
2576 int bufwasdelwri;
2577 int i;
2578
5017e97d 2579 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
bad55843 2580
c8f5f12e
DC
2581 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2582 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
49383b0e 2583 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
bad55843 2584 if (!ilist)
44b56e0a 2585 goto out_put;
bad55843
DC
2586
2587 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2588 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
1a3e8f3d 2589 rcu_read_lock();
bad55843
DC
2590 /* really need a gang lookup range call here */
2591 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
c8f5f12e 2592 first_index, inodes_per_cluster);
bad55843
DC
2593 if (nr_found == 0)
2594 goto out_free;
2595
2596 for (i = 0; i < nr_found; i++) {
2597 iq = ilist[i];
2598 if (iq == ip)
2599 continue;
1a3e8f3d
DC
2600
2601 /*
2602 * because this is an RCU protected lookup, we could find a
2603 * recently freed or even reallocated inode during the lookup.
2604 * We need to check under the i_flags_lock for a valid inode
2605 * here. Skip it if it is not valid or the wrong inode.
2606 */
360914d6
DC
2607 spin_lock(&iq->i_flags_lock);
2608 if (!iq->i_ino ||
9eccedc4 2609 __xfs_iflags_test(iq, XFS_ISTALE) ||
1a3e8f3d 2610 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
360914d6 2611 spin_unlock(&iq->i_flags_lock);
1a3e8f3d
DC
2612 continue;
2613 }
360914d6 2614 spin_unlock(&iq->i_flags_lock);
1a3e8f3d 2615
bad55843
DC
2616 /*
2617 * Do an un-protected check to see if the inode is dirty and
2618 * is a candidate for flushing. These checks will be repeated
2619 * later after the appropriate locks are acquired.
2620 */
33540408 2621 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
bad55843 2622 continue;
bad55843
DC
2623
2624 /*
2625 * Try to get locks. If any are unavailable or it is pinned,
2626 * then this inode cannot be flushed and is skipped.
2627 */
2628
2629 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2630 continue;
2631 if (!xfs_iflock_nowait(iq)) {
2632 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2633 continue;
2634 }
2635 if (xfs_ipincount(iq)) {
2636 xfs_ifunlock(iq);
2637 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2638 continue;
2639 }
2640
2641 /*
2642 * arriving here means that this inode can be flushed. First
2643 * re-check that it's dirty before flushing.
2644 */
33540408
DC
2645 if (!xfs_inode_clean(iq)) {
2646 int error;
bad55843
DC
2647 error = xfs_iflush_int(iq, bp);
2648 if (error) {
2649 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2650 goto cluster_corrupt_out;
2651 }
2652 clcount++;
2653 } else {
2654 xfs_ifunlock(iq);
2655 }
2656 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2657 }
2658
2659 if (clcount) {
2660 XFS_STATS_INC(xs_icluster_flushcnt);
2661 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2662 }
2663
2664out_free:
1a3e8f3d 2665 rcu_read_unlock();
f0e2d93c 2666 kmem_free(ilist);
44b56e0a
DC
2667out_put:
2668 xfs_perag_put(pag);
bad55843
DC
2669 return 0;
2670
2671
2672cluster_corrupt_out:
2673 /*
2674 * Corruption detected in the clustering loop. Invalidate the
2675 * inode buffer and shut down the filesystem.
2676 */
1a3e8f3d 2677 rcu_read_unlock();
bad55843 2678 /*
43ff2122 2679 * Clean up the buffer. If it was delwri, just release it --
bad55843
DC
2680 * brelse can handle it with no problems. If not, shut down the
2681 * filesystem before releasing the buffer.
2682 */
43ff2122 2683 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
bad55843
DC
2684 if (bufwasdelwri)
2685 xfs_buf_relse(bp);
2686
2687 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2688
2689 if (!bufwasdelwri) {
2690 /*
2691 * Just like incore_relse: if we have b_iodone functions,
2692 * mark the buffer as an error and call them. Otherwise
2693 * mark it as stale and brelse.
2694 */
cb669ca5 2695 if (bp->b_iodone) {
bad55843 2696 XFS_BUF_UNDONE(bp);
c867cb61 2697 xfs_buf_stale(bp);
5a52c2a5 2698 xfs_buf_ioerror(bp, EIO);
1a1a3e97 2699 xfs_buf_ioend(bp, 0);
bad55843 2700 } else {
c867cb61 2701 xfs_buf_stale(bp);
bad55843
DC
2702 xfs_buf_relse(bp);
2703 }
2704 }
2705
2706 /*
2707 * Unlocks the flush lock
2708 */
04913fdd 2709 xfs_iflush_abort(iq, false);
f0e2d93c 2710 kmem_free(ilist);
44b56e0a 2711 xfs_perag_put(pag);
bad55843
DC
2712 return XFS_ERROR(EFSCORRUPTED);
2713}
2714
1da177e4 2715/*
4c46819a
CH
2716 * Flush dirty inode metadata into the backing buffer.
2717 *
2718 * The caller must have the inode lock and the inode flush lock held. The
2719 * inode lock will still be held upon return to the caller, and the inode
2720 * flush lock will be released after the inode has reached the disk.
2721 *
2722 * The caller must write out the buffer returned in *bpp and release it.
1da177e4
LT
2723 */
2724int
2725xfs_iflush(
4c46819a
CH
2726 struct xfs_inode *ip,
2727 struct xfs_buf **bpp)
1da177e4 2728{
4c46819a 2729 struct xfs_mount *mp = ip->i_mount;
01ee4801 2730 struct xfs_buf *bp = NULL;
4c46819a 2731 struct xfs_dinode *dip;
1da177e4 2732 int error;
1da177e4
LT
2733
2734 XFS_STATS_INC(xs_iflush_count);
2735
579aa9ca 2736 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 2737 ASSERT(xfs_isiflocked(ip));
1da177e4 2738 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 2739 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
1da177e4 2740
4c46819a 2741 *bpp = NULL;
1da177e4 2742
1da177e4
LT
2743 xfs_iunpin_wait(ip);
2744
4b6a4688
DC
2745 /*
2746 * For stale inodes we cannot rely on the backing buffer remaining
2747 * stale in cache for the remaining life of the stale inode and so
475ee413 2748 * xfs_imap_to_bp() below may give us a buffer that no longer contains
4b6a4688
DC
2749 * inodes below. We have to check this after ensuring the inode is
2750 * unpinned so that it is safe to reclaim the stale inode after the
2751 * flush call.
2752 */
2753 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2754 xfs_ifunlock(ip);
2755 return 0;
2756 }
2757
1da177e4
LT
2758 /*
2759 * This may have been unpinned because the filesystem is shutting
2760 * down forcibly. If that's the case we must not write this inode
32ce90a4
CH
2761 * to disk, because the log record didn't make it to disk.
2762 *
2763 * We also have to remove the log item from the AIL in this case,
2764 * as we wait for an empty AIL as part of the unmount process.
1da177e4
LT
2765 */
2766 if (XFS_FORCED_SHUTDOWN(mp)) {
32ce90a4
CH
2767 error = XFS_ERROR(EIO);
2768 goto abort_out;
1da177e4
LT
2769 }
2770
a3f74ffb 2771 /*
01ee4801
DC
2772 * Get the buffer containing the on-disk inode. We are doing a try-lock
2773 * operation here, so we may get an EAGAIN error. In that case, we
2774 * simply want to return with the inode still dirty.
2775 *
2776 * If we get any other error, we effectively have a corruption situation
2777 * and we cannot flush the inode, so we treat it the same as failing
2778 * xfs_iflush_int().
a3f74ffb 2779 */
475ee413
CH
2780 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2781 0);
01ee4801 2782 if (error == EAGAIN) {
a3f74ffb
DC
2783 xfs_ifunlock(ip);
2784 return error;
2785 }
01ee4801
DC
2786 if (error)
2787 goto corrupt_out;
a3f74ffb 2788
1da177e4
LT
2789 /*
2790 * First flush out the inode that xfs_iflush was called with.
2791 */
2792 error = xfs_iflush_int(ip, bp);
bad55843 2793 if (error)
1da177e4 2794 goto corrupt_out;
1da177e4 2795
a3f74ffb
DC
2796 /*
2797 * If the buffer is pinned then push on the log now so we won't
2798 * get stuck waiting in the write for too long.
2799 */
811e64c7 2800 if (xfs_buf_ispinned(bp))
a14a348b 2801 xfs_log_force(mp, 0);
a3f74ffb 2802
1da177e4
LT
2803 /*
2804 * inode clustering:
2805 * see if other inodes can be gathered into this write
2806 */
bad55843
DC
2807 error = xfs_iflush_cluster(ip, bp);
2808 if (error)
2809 goto cluster_corrupt_out;
1da177e4 2810
4c46819a
CH
2811 *bpp = bp;
2812 return 0;
1da177e4
LT
2813
2814corrupt_out:
01ee4801
DC
2815 if (bp)
2816 xfs_buf_relse(bp);
7d04a335 2817 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1da177e4 2818cluster_corrupt_out:
32ce90a4
CH
2819 error = XFS_ERROR(EFSCORRUPTED);
2820abort_out:
1da177e4
LT
2821 /*
2822 * Unlocks the flush lock
2823 */
04913fdd 2824 xfs_iflush_abort(ip, false);
32ce90a4 2825 return error;
1da177e4
LT
2826}
2827
2828
2829STATIC int
2830xfs_iflush_int(
93848a99
CH
2831 struct xfs_inode *ip,
2832 struct xfs_buf *bp)
1da177e4 2833{
93848a99
CH
2834 struct xfs_inode_log_item *iip = ip->i_itemp;
2835 struct xfs_dinode *dip;
2836 struct xfs_mount *mp = ip->i_mount;
1da177e4 2837
579aa9ca 2838 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 2839 ASSERT(xfs_isiflocked(ip));
1da177e4 2840 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 2841 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
93848a99 2842 ASSERT(iip != NULL && iip->ili_fields != 0);
1da177e4 2843
1da177e4 2844 /* set *dip = inode's place in the buffer */
92bfc6e7 2845 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
1da177e4 2846
69ef921b 2847 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
1da177e4 2848 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
6a19d939
DC
2849 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2850 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2851 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
1da177e4
LT
2852 goto corrupt_out;
2853 }
2854 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2855 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
6a19d939
DC
2856 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2857 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2858 __func__, ip->i_ino, ip, ip->i_d.di_magic);
1da177e4
LT
2859 goto corrupt_out;
2860 }
abbede1b 2861 if (S_ISREG(ip->i_d.di_mode)) {
1da177e4
LT
2862 if (XFS_TEST_ERROR(
2863 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2864 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2865 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
6a19d939
DC
2866 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2867 "%s: Bad regular inode %Lu, ptr 0x%p",
2868 __func__, ip->i_ino, ip);
1da177e4
LT
2869 goto corrupt_out;
2870 }
abbede1b 2871 } else if (S_ISDIR(ip->i_d.di_mode)) {
1da177e4
LT
2872 if (XFS_TEST_ERROR(
2873 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2874 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2875 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2876 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
6a19d939
DC
2877 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2878 "%s: Bad directory inode %Lu, ptr 0x%p",
2879 __func__, ip->i_ino, ip);
1da177e4
LT
2880 goto corrupt_out;
2881 }
2882 }
2883 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2884 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2885 XFS_RANDOM_IFLUSH_5)) {
6a19d939
DC
2886 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2887 "%s: detected corrupt incore inode %Lu, "
2888 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2889 __func__, ip->i_ino,
1da177e4 2890 ip->i_d.di_nextents + ip->i_d.di_anextents,
6a19d939 2891 ip->i_d.di_nblocks, ip);
1da177e4
LT
2892 goto corrupt_out;
2893 }
2894 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2895 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
6a19d939
DC
2896 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2897 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2898 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
1da177e4
LT
2899 goto corrupt_out;
2900 }
2901 /*
2902 * bump the flush iteration count, used to detect flushes which
93848a99
CH
2903 * postdate a log record during recovery. This is redundant as we now
2904 * log every change and hence this can't happen. Still, it doesn't hurt.
1da177e4 2905 */
1da177e4
LT
2906 ip->i_d.di_flushiter++;
2907
2908 /*
2909 * Copy the dirty parts of the inode into the on-disk
2910 * inode. We always copy out the core of the inode,
2911 * because if the inode is dirty at all the core must
2912 * be.
2913 */
81591fe2 2914 xfs_dinode_to_disk(dip, &ip->i_d);
1da177e4
LT
2915
2916 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2917 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2918 ip->i_d.di_flushiter = 0;
2919
2920 /*
2921 * If this is really an old format inode and the superblock version
2922 * has not been updated to support only new format inodes, then
2923 * convert back to the old inode format. If the superblock version
2924 * has been updated, then make the conversion permanent.
2925 */
51ce16d5
CH
2926 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2927 if (ip->i_d.di_version == 1) {
62118709 2928 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1da177e4
LT
2929 /*
2930 * Convert it back.
2931 */
2932 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
81591fe2 2933 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
1da177e4
LT
2934 } else {
2935 /*
2936 * The superblock version has already been bumped,
2937 * so just make the conversion to the new inode
2938 * format permanent.
2939 */
51ce16d5
CH
2940 ip->i_d.di_version = 2;
2941 dip->di_version = 2;
1da177e4 2942 ip->i_d.di_onlink = 0;
81591fe2 2943 dip->di_onlink = 0;
1da177e4 2944 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
81591fe2
CH
2945 memset(&(dip->di_pad[0]), 0,
2946 sizeof(dip->di_pad));
6743099c 2947 ASSERT(xfs_get_projid(ip) == 0);
1da177e4
LT
2948 }
2949 }
2950
e4ac967b
DC
2951 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2952 if (XFS_IFORK_Q(ip))
2953 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
1da177e4
LT
2954 xfs_inobp_check(mp, bp);
2955
2956 /*
f5d8d5c4
CH
2957 * We've recorded everything logged in the inode, so we'd like to clear
2958 * the ili_fields bits so we don't log and flush things unnecessarily.
2959 * However, we can't stop logging all this information until the data
2960 * we've copied into the disk buffer is written to disk. If we did we
2961 * might overwrite the copy of the inode in the log with all the data
2962 * after re-logging only part of it, and in the face of a crash we
2963 * wouldn't have all the data we need to recover.
1da177e4 2964 *
f5d8d5c4
CH
2965 * What we do is move the bits to the ili_last_fields field. When
2966 * logging the inode, these bits are moved back to the ili_fields field.
2967 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2968 * know that the information those bits represent is permanently on
2969 * disk. As long as the flush completes before the inode is logged
2970 * again, then both ili_fields and ili_last_fields will be cleared.
1da177e4 2971 *
f5d8d5c4
CH
2972 * We can play with the ili_fields bits here, because the inode lock
2973 * must be held exclusively in order to set bits there and the flush
2974 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2975 * done routine can tell whether or not to look in the AIL. Also, store
2976 * the current LSN of the inode so that we can tell whether the item has
2977 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2978 * need the AIL lock, because it is a 64 bit value that cannot be read
2979 * atomically.
1da177e4 2980 */
93848a99
CH
2981 iip->ili_last_fields = iip->ili_fields;
2982 iip->ili_fields = 0;
2983 iip->ili_logged = 1;
1da177e4 2984
93848a99
CH
2985 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2986 &iip->ili_item.li_lsn);
1da177e4 2987
93848a99
CH
2988 /*
2989 * Attach the function xfs_iflush_done to the inode's
2990 * buffer. This will remove the inode from the AIL
2991 * and unlock the inode's flush lock when the inode is
2992 * completely written to disk.
2993 */
2994 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
1da177e4 2995
93848a99
CH
2996 /* update the lsn in the on disk inode if required */
2997 if (ip->i_d.di_version == 3)
2998 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
2999
3000 /* generate the checksum. */
3001 xfs_dinode_calc_crc(mp, dip);
1da177e4 3002
93848a99
CH
3003 ASSERT(bp->b_fspriv != NULL);
3004 ASSERT(bp->b_iodone != NULL);
1da177e4
LT
3005 return 0;
3006
3007corrupt_out:
3008 return XFS_ERROR(EFSCORRUPTED);
3009}
3010
4eea22f0
MK
3011/*
3012 * Return a pointer to the extent record at file index idx.
3013 */
a6f64d4a 3014xfs_bmbt_rec_host_t *
4eea22f0
MK
3015xfs_iext_get_ext(
3016 xfs_ifork_t *ifp, /* inode fork pointer */
3017 xfs_extnum_t idx) /* index of target extent */
3018{
3019 ASSERT(idx >= 0);
87bef181
CH
3020 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3021
0293ce3a
MK
3022 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3023 return ifp->if_u1.if_ext_irec->er_extbuf;
3024 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3025 xfs_ext_irec_t *erp; /* irec pointer */
3026 int erp_idx = 0; /* irec index */
3027 xfs_extnum_t page_idx = idx; /* ext index in target list */
3028
3029 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3030 return &erp->er_extbuf[page_idx];
3031 } else if (ifp->if_bytes) {
4eea22f0
MK
3032 return &ifp->if_u1.if_extents[idx];
3033 } else {
3034 return NULL;
3035 }
3036}
3037
3038/*
3039 * Insert new item(s) into the extent records for incore inode
3040 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3041 */
3042void
3043xfs_iext_insert(
6ef35544 3044 xfs_inode_t *ip, /* incore inode pointer */
4eea22f0
MK
3045 xfs_extnum_t idx, /* starting index of new items */
3046 xfs_extnum_t count, /* number of inserted items */
6ef35544
CH
3047 xfs_bmbt_irec_t *new, /* items to insert */
3048 int state) /* type of extent conversion */
4eea22f0 3049{
6ef35544 3050 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
4eea22f0
MK
3051 xfs_extnum_t i; /* extent record index */
3052
0b1b213f
CH
3053 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
3054
4eea22f0
MK
3055 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3056 xfs_iext_add(ifp, idx, count);
a6f64d4a
CH
3057 for (i = idx; i < idx + count; i++, new++)
3058 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
4eea22f0
MK
3059}
3060
3061/*
3062 * This is called when the amount of space required for incore file
3063 * extents needs to be increased. The ext_diff parameter stores the
3064 * number of new extents being added and the idx parameter contains
3065 * the extent index where the new extents will be added. If the new
3066 * extents are being appended, then we just need to (re)allocate and
3067 * initialize the space. Otherwise, if the new extents are being
3068 * inserted into the middle of the existing entries, a bit more work
3069 * is required to make room for the new extents to be inserted. The
3070 * caller is responsible for filling in the new extent entries upon
3071 * return.
3072 */
3073void
3074xfs_iext_add(
3075 xfs_ifork_t *ifp, /* inode fork pointer */
3076 xfs_extnum_t idx, /* index to begin adding exts */
c41564b5 3077 int ext_diff) /* number of extents to add */
4eea22f0
MK
3078{
3079 int byte_diff; /* new bytes being added */
3080 int new_size; /* size of extents after adding */
3081 xfs_extnum_t nextents; /* number of extents in file */
3082
3083 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3084 ASSERT((idx >= 0) && (idx <= nextents));
3085 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3086 new_size = ifp->if_bytes + byte_diff;
3087 /*
3088 * If the new number of extents (nextents + ext_diff)
3089 * fits inside the inode, then continue to use the inline
3090 * extent buffer.
3091 */
3092 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3093 if (idx < nextents) {
3094 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3095 &ifp->if_u2.if_inline_ext[idx],
3096 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3097 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3098 }
3099 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3100 ifp->if_real_bytes = 0;
3101 }
3102 /*
3103 * Otherwise use a linear (direct) extent list.
3104 * If the extents are currently inside the inode,
3105 * xfs_iext_realloc_direct will switch us from
3106 * inline to direct extent allocation mode.
3107 */
0293ce3a 3108 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
4eea22f0
MK
3109 xfs_iext_realloc_direct(ifp, new_size);
3110 if (idx < nextents) {
3111 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3112 &ifp->if_u1.if_extents[idx],
3113 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3114 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3115 }
3116 }
0293ce3a
MK
3117 /* Indirection array */
3118 else {
3119 xfs_ext_irec_t *erp;
3120 int erp_idx = 0;
3121 int page_idx = idx;
3122
3123 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3124 if (ifp->if_flags & XFS_IFEXTIREC) {
3125 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3126 } else {
3127 xfs_iext_irec_init(ifp);
3128 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3129 erp = ifp->if_u1.if_ext_irec;
3130 }
3131 /* Extents fit in target extent page */
3132 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3133 if (page_idx < erp->er_extcount) {
3134 memmove(&erp->er_extbuf[page_idx + ext_diff],
3135 &erp->er_extbuf[page_idx],
3136 (erp->er_extcount - page_idx) *
3137 sizeof(xfs_bmbt_rec_t));
3138 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3139 }
3140 erp->er_extcount += ext_diff;
3141 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3142 }
3143 /* Insert a new extent page */
3144 else if (erp) {
3145 xfs_iext_add_indirect_multi(ifp,
3146 erp_idx, page_idx, ext_diff);
3147 }
3148 /*
3149 * If extent(s) are being appended to the last page in
3150 * the indirection array and the new extent(s) don't fit
3151 * in the page, then erp is NULL and erp_idx is set to
3152 * the next index needed in the indirection array.
3153 */
3154 else {
3155 int count = ext_diff;
3156
3157 while (count) {
3158 erp = xfs_iext_irec_new(ifp, erp_idx);
3159 erp->er_extcount = count;
3160 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3161 if (count) {
3162 erp_idx++;
3163 }
3164 }
3165 }
3166 }
4eea22f0
MK
3167 ifp->if_bytes = new_size;
3168}
3169
0293ce3a
MK
3170/*
3171 * This is called when incore extents are being added to the indirection
3172 * array and the new extents do not fit in the target extent list. The
3173 * erp_idx parameter contains the irec index for the target extent list
3174 * in the indirection array, and the idx parameter contains the extent
3175 * index within the list. The number of extents being added is stored
3176 * in the count parameter.
3177 *
3178 * |-------| |-------|
3179 * | | | | idx - number of extents before idx
3180 * | idx | | count |
3181 * | | | | count - number of extents being inserted at idx
3182 * |-------| |-------|
3183 * | count | | nex2 | nex2 - number of extents after idx + count
3184 * |-------| |-------|
3185 */
3186void
3187xfs_iext_add_indirect_multi(
3188 xfs_ifork_t *ifp, /* inode fork pointer */
3189 int erp_idx, /* target extent irec index */
3190 xfs_extnum_t idx, /* index within target list */
3191 int count) /* new extents being added */
3192{
3193 int byte_diff; /* new bytes being added */
3194 xfs_ext_irec_t *erp; /* pointer to irec entry */
3195 xfs_extnum_t ext_diff; /* number of extents to add */
3196 xfs_extnum_t ext_cnt; /* new extents still needed */
3197 xfs_extnum_t nex2; /* extents after idx + count */
3198 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3199 int nlists; /* number of irec's (lists) */
3200
3201 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3202 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3203 nex2 = erp->er_extcount - idx;
3204 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3205
3206 /*
3207 * Save second part of target extent list
3208 * (all extents past */
3209 if (nex2) {
3210 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
6785073b 3211 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
0293ce3a
MK
3212 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3213 erp->er_extcount -= nex2;
3214 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3215 memset(&erp->er_extbuf[idx], 0, byte_diff);
3216 }
3217
3218 /*
3219 * Add the new extents to the end of the target
3220 * list, then allocate new irec record(s) and
3221 * extent buffer(s) as needed to store the rest
3222 * of the new extents.
3223 */
3224 ext_cnt = count;
3225 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3226 if (ext_diff) {
3227 erp->er_extcount += ext_diff;
3228 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3229 ext_cnt -= ext_diff;
3230 }
3231 while (ext_cnt) {
3232 erp_idx++;
3233 erp = xfs_iext_irec_new(ifp, erp_idx);
3234 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3235 erp->er_extcount = ext_diff;
3236 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3237 ext_cnt -= ext_diff;
3238 }
3239
3240 /* Add nex2 extents back to indirection array */
3241 if (nex2) {
3242 xfs_extnum_t ext_avail;
3243 int i;
3244
3245 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3246 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3247 i = 0;
3248 /*
3249 * If nex2 extents fit in the current page, append
3250 * nex2_ep after the new extents.
3251 */
3252 if (nex2 <= ext_avail) {
3253 i = erp->er_extcount;
3254 }
3255 /*
3256 * Otherwise, check if space is available in the
3257 * next page.
3258 */
3259 else if ((erp_idx < nlists - 1) &&
3260 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3261 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3262 erp_idx++;
3263 erp++;
3264 /* Create a hole for nex2 extents */
3265 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3266 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3267 }
3268 /*
3269 * Final choice, create a new extent page for
3270 * nex2 extents.
3271 */
3272 else {
3273 erp_idx++;
3274 erp = xfs_iext_irec_new(ifp, erp_idx);
3275 }
3276 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
f0e2d93c 3277 kmem_free(nex2_ep);
0293ce3a
MK
3278 erp->er_extcount += nex2;
3279 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3280 }
3281}
3282
4eea22f0
MK
3283/*
3284 * This is called when the amount of space required for incore file
3285 * extents needs to be decreased. The ext_diff parameter stores the
3286 * number of extents to be removed and the idx parameter contains
3287 * the extent index where the extents will be removed from.
0293ce3a
MK
3288 *
3289 * If the amount of space needed has decreased below the linear
3290 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3291 * extent array. Otherwise, use kmem_realloc() to adjust the
3292 * size to what is needed.
4eea22f0
MK
3293 */
3294void
3295xfs_iext_remove(
6ef35544 3296 xfs_inode_t *ip, /* incore inode pointer */
4eea22f0 3297 xfs_extnum_t idx, /* index to begin removing exts */
6ef35544
CH
3298 int ext_diff, /* number of extents to remove */
3299 int state) /* type of extent conversion */
4eea22f0 3300{
6ef35544 3301 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
4eea22f0
MK
3302 xfs_extnum_t nextents; /* number of extents in file */
3303 int new_size; /* size of extents after removal */
3304
0b1b213f
CH
3305 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3306
4eea22f0
MK
3307 ASSERT(ext_diff > 0);
3308 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3309 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3310
3311 if (new_size == 0) {
3312 xfs_iext_destroy(ifp);
0293ce3a
MK
3313 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3314 xfs_iext_remove_indirect(ifp, idx, ext_diff);
4eea22f0
MK
3315 } else if (ifp->if_real_bytes) {
3316 xfs_iext_remove_direct(ifp, idx, ext_diff);
3317 } else {
3318 xfs_iext_remove_inline(ifp, idx, ext_diff);
3319 }
3320 ifp->if_bytes = new_size;
3321}
3322
3323/*
3324 * This removes ext_diff extents from the inline buffer, beginning
3325 * at extent index idx.
3326 */
3327void
3328xfs_iext_remove_inline(
3329 xfs_ifork_t *ifp, /* inode fork pointer */
3330 xfs_extnum_t idx, /* index to begin removing exts */
3331 int ext_diff) /* number of extents to remove */
3332{
3333 int nextents; /* number of extents in file */
3334
0293ce3a 3335 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
4eea22f0
MK
3336 ASSERT(idx < XFS_INLINE_EXTS);
3337 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3338 ASSERT(((nextents - ext_diff) > 0) &&
3339 (nextents - ext_diff) < XFS_INLINE_EXTS);
3340
3341 if (idx + ext_diff < nextents) {
3342 memmove(&ifp->if_u2.if_inline_ext[idx],
3343 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3344 (nextents - (idx + ext_diff)) *
3345 sizeof(xfs_bmbt_rec_t));
3346 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3347 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3348 } else {
3349 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3350 ext_diff * sizeof(xfs_bmbt_rec_t));
3351 }
3352}
3353
3354/*
3355 * This removes ext_diff extents from a linear (direct) extent list,
3356 * beginning at extent index idx. If the extents are being removed
3357 * from the end of the list (ie. truncate) then we just need to re-
3358 * allocate the list to remove the extra space. Otherwise, if the
3359 * extents are being removed from the middle of the existing extent
3360 * entries, then we first need to move the extent records beginning
3361 * at idx + ext_diff up in the list to overwrite the records being
3362 * removed, then remove the extra space via kmem_realloc.
3363 */
3364void
3365xfs_iext_remove_direct(
3366 xfs_ifork_t *ifp, /* inode fork pointer */
3367 xfs_extnum_t idx, /* index to begin removing exts */
3368 int ext_diff) /* number of extents to remove */
3369{
3370 xfs_extnum_t nextents; /* number of extents in file */
3371 int new_size; /* size of extents after removal */
3372
0293ce3a 3373 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
4eea22f0
MK
3374 new_size = ifp->if_bytes -
3375 (ext_diff * sizeof(xfs_bmbt_rec_t));
3376 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3377
3378 if (new_size == 0) {
3379 xfs_iext_destroy(ifp);
3380 return;
3381 }
3382 /* Move extents up in the list (if needed) */
3383 if (idx + ext_diff < nextents) {
3384 memmove(&ifp->if_u1.if_extents[idx],
3385 &ifp->if_u1.if_extents[idx + ext_diff],
3386 (nextents - (idx + ext_diff)) *
3387 sizeof(xfs_bmbt_rec_t));
3388 }
3389 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3390 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3391 /*
3392 * Reallocate the direct extent list. If the extents
3393 * will fit inside the inode then xfs_iext_realloc_direct
3394 * will switch from direct to inline extent allocation
3395 * mode for us.
3396 */
3397 xfs_iext_realloc_direct(ifp, new_size);
3398 ifp->if_bytes = new_size;
3399}
3400
0293ce3a
MK
3401/*
3402 * This is called when incore extents are being removed from the
3403 * indirection array and the extents being removed span multiple extent
3404 * buffers. The idx parameter contains the file extent index where we
3405 * want to begin removing extents, and the count parameter contains
3406 * how many extents need to be removed.
3407 *
3408 * |-------| |-------|
3409 * | nex1 | | | nex1 - number of extents before idx
3410 * |-------| | count |
3411 * | | | | count - number of extents being removed at idx
3412 * | count | |-------|
3413 * | | | nex2 | nex2 - number of extents after idx + count
3414 * |-------| |-------|
3415 */
3416void
3417xfs_iext_remove_indirect(
3418 xfs_ifork_t *ifp, /* inode fork pointer */
3419 xfs_extnum_t idx, /* index to begin removing extents */
3420 int count) /* number of extents to remove */
3421{
3422 xfs_ext_irec_t *erp; /* indirection array pointer */
3423 int erp_idx = 0; /* indirection array index */
3424 xfs_extnum_t ext_cnt; /* extents left to remove */
3425 xfs_extnum_t ext_diff; /* extents to remove in current list */
3426 xfs_extnum_t nex1; /* number of extents before idx */
3427 xfs_extnum_t nex2; /* extents after idx + count */
0293ce3a
MK
3428 int page_idx = idx; /* index in target extent list */
3429
3430 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3431 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3432 ASSERT(erp != NULL);
0293ce3a
MK
3433 nex1 = page_idx;
3434 ext_cnt = count;
3435 while (ext_cnt) {
3436 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3437 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3438 /*
3439 * Check for deletion of entire list;
3440 * xfs_iext_irec_remove() updates extent offsets.
3441 */
3442 if (ext_diff == erp->er_extcount) {
3443 xfs_iext_irec_remove(ifp, erp_idx);
3444 ext_cnt -= ext_diff;
3445 nex1 = 0;
3446 if (ext_cnt) {
3447 ASSERT(erp_idx < ifp->if_real_bytes /
3448 XFS_IEXT_BUFSZ);
3449 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3450 nex1 = 0;
3451 continue;
3452 } else {
3453 break;
3454 }
3455 }
3456 /* Move extents up (if needed) */
3457 if (nex2) {
3458 memmove(&erp->er_extbuf[nex1],
3459 &erp->er_extbuf[nex1 + ext_diff],
3460 nex2 * sizeof(xfs_bmbt_rec_t));
3461 }
3462 /* Zero out rest of page */
3463 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3464 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3465 /* Update remaining counters */
3466 erp->er_extcount -= ext_diff;
3467 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3468 ext_cnt -= ext_diff;
3469 nex1 = 0;
3470 erp_idx++;
3471 erp++;
3472 }
3473 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3474 xfs_iext_irec_compact(ifp);
3475}
3476
4eea22f0
MK
3477/*
3478 * Create, destroy, or resize a linear (direct) block of extents.
3479 */
3480void
3481xfs_iext_realloc_direct(
3482 xfs_ifork_t *ifp, /* inode fork pointer */
3483 int new_size) /* new size of extents */
3484{
3485 int rnew_size; /* real new size of extents */
3486
3487 rnew_size = new_size;
3488
0293ce3a
MK
3489 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3490 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3491 (new_size != ifp->if_real_bytes)));
3492
4eea22f0
MK
3493 /* Free extent records */
3494 if (new_size == 0) {
3495 xfs_iext_destroy(ifp);
3496 }
3497 /* Resize direct extent list and zero any new bytes */
3498 else if (ifp->if_real_bytes) {
3499 /* Check if extents will fit inside the inode */
3500 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3501 xfs_iext_direct_to_inline(ifp, new_size /
3502 (uint)sizeof(xfs_bmbt_rec_t));
3503 ifp->if_bytes = new_size;
3504 return;
3505 }
16a087d8 3506 if (!is_power_of_2(new_size)){
40ebd81d 3507 rnew_size = roundup_pow_of_two(new_size);
4eea22f0
MK
3508 }
3509 if (rnew_size != ifp->if_real_bytes) {
a6f64d4a 3510 ifp->if_u1.if_extents =
4eea22f0
MK
3511 kmem_realloc(ifp->if_u1.if_extents,
3512 rnew_size,
6785073b 3513 ifp->if_real_bytes, KM_NOFS);
4eea22f0
MK
3514 }
3515 if (rnew_size > ifp->if_real_bytes) {
3516 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3517 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3518 rnew_size - ifp->if_real_bytes);
3519 }
3520 }
3521 /*
3522 * Switch from the inline extent buffer to a direct
3523 * extent list. Be sure to include the inline extent
3524 * bytes in new_size.
3525 */
3526 else {
3527 new_size += ifp->if_bytes;
16a087d8 3528 if (!is_power_of_2(new_size)) {
40ebd81d 3529 rnew_size = roundup_pow_of_two(new_size);
4eea22f0
MK
3530 }
3531 xfs_iext_inline_to_direct(ifp, rnew_size);
3532 }
3533 ifp->if_real_bytes = rnew_size;
3534 ifp->if_bytes = new_size;
3535}
3536
3537/*
3538 * Switch from linear (direct) extent records to inline buffer.
3539 */
3540void
3541xfs_iext_direct_to_inline(
3542 xfs_ifork_t *ifp, /* inode fork pointer */
3543 xfs_extnum_t nextents) /* number of extents in file */
3544{
3545 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3546 ASSERT(nextents <= XFS_INLINE_EXTS);
3547 /*
3548 * The inline buffer was zeroed when we switched
3549 * from inline to direct extent allocation mode,
3550 * so we don't need to clear it here.
3551 */
3552 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3553 nextents * sizeof(xfs_bmbt_rec_t));
f0e2d93c 3554 kmem_free(ifp->if_u1.if_extents);
4eea22f0
MK
3555 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3556 ifp->if_real_bytes = 0;
3557}
3558
3559/*
3560 * Switch from inline buffer to linear (direct) extent records.
3561 * new_size should already be rounded up to the next power of 2
3562 * by the caller (when appropriate), so use new_size as it is.
3563 * However, since new_size may be rounded up, we can't update
3564 * if_bytes here. It is the caller's responsibility to update
3565 * if_bytes upon return.
3566 */
3567void
3568xfs_iext_inline_to_direct(
3569 xfs_ifork_t *ifp, /* inode fork pointer */
3570 int new_size) /* number of extents in file */
3571{
6785073b 3572 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
4eea22f0
MK
3573 memset(ifp->if_u1.if_extents, 0, new_size);
3574 if (ifp->if_bytes) {
3575 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3576 ifp->if_bytes);
3577 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3578 sizeof(xfs_bmbt_rec_t));
3579 }
3580 ifp->if_real_bytes = new_size;
3581}
3582
0293ce3a
MK
3583/*
3584 * Resize an extent indirection array to new_size bytes.
3585 */
d96f8f89 3586STATIC void
0293ce3a
MK
3587xfs_iext_realloc_indirect(
3588 xfs_ifork_t *ifp, /* inode fork pointer */
3589 int new_size) /* new indirection array size */
3590{
3591 int nlists; /* number of irec's (ex lists) */
3592 int size; /* current indirection array size */
3593
3594 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3595 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3596 size = nlists * sizeof(xfs_ext_irec_t);
3597 ASSERT(ifp->if_real_bytes);
3598 ASSERT((new_size >= 0) && (new_size != size));
3599 if (new_size == 0) {
3600 xfs_iext_destroy(ifp);
3601 } else {
3602 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3603 kmem_realloc(ifp->if_u1.if_ext_irec,
6785073b 3604 new_size, size, KM_NOFS);
0293ce3a
MK
3605 }
3606}
3607
3608/*
3609 * Switch from indirection array to linear (direct) extent allocations.
3610 */
d96f8f89 3611STATIC void
0293ce3a
MK
3612xfs_iext_indirect_to_direct(
3613 xfs_ifork_t *ifp) /* inode fork pointer */
3614{
a6f64d4a 3615 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
0293ce3a
MK
3616 xfs_extnum_t nextents; /* number of extents in file */
3617 int size; /* size of file extents */
3618
3619 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3620 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3621 ASSERT(nextents <= XFS_LINEAR_EXTS);
3622 size = nextents * sizeof(xfs_bmbt_rec_t);
3623
71a8c87f 3624 xfs_iext_irec_compact_pages(ifp);
0293ce3a
MK
3625 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3626
3627 ep = ifp->if_u1.if_ext_irec->er_extbuf;
f0e2d93c 3628 kmem_free(ifp->if_u1.if_ext_irec);
0293ce3a
MK
3629 ifp->if_flags &= ~XFS_IFEXTIREC;
3630 ifp->if_u1.if_extents = ep;
3631 ifp->if_bytes = size;
3632 if (nextents < XFS_LINEAR_EXTS) {
3633 xfs_iext_realloc_direct(ifp, size);
3634 }
3635}
3636
4eea22f0
MK
3637/*
3638 * Free incore file extents.
3639 */
3640void
3641xfs_iext_destroy(
3642 xfs_ifork_t *ifp) /* inode fork pointer */
3643{
0293ce3a
MK
3644 if (ifp->if_flags & XFS_IFEXTIREC) {
3645 int erp_idx;
3646 int nlists;
3647
3648 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3649 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3650 xfs_iext_irec_remove(ifp, erp_idx);
3651 }
3652 ifp->if_flags &= ~XFS_IFEXTIREC;
3653 } else if (ifp->if_real_bytes) {
f0e2d93c 3654 kmem_free(ifp->if_u1.if_extents);
4eea22f0
MK
3655 } else if (ifp->if_bytes) {
3656 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3657 sizeof(xfs_bmbt_rec_t));
3658 }
3659 ifp->if_u1.if_extents = NULL;
3660 ifp->if_real_bytes = 0;
3661 ifp->if_bytes = 0;
3662}
0293ce3a 3663
8867bc9b
MK
3664/*
3665 * Return a pointer to the extent record for file system block bno.
3666 */
a6f64d4a 3667xfs_bmbt_rec_host_t * /* pointer to found extent record */
8867bc9b
MK
3668xfs_iext_bno_to_ext(
3669 xfs_ifork_t *ifp, /* inode fork pointer */
3670 xfs_fileoff_t bno, /* block number to search for */
3671 xfs_extnum_t *idxp) /* index of target extent */
3672{
a6f64d4a 3673 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
8867bc9b 3674 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
a6f64d4a 3675 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
8867bc9b 3676 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
c41564b5 3677 int high; /* upper boundary in search */
8867bc9b 3678 xfs_extnum_t idx = 0; /* index of target extent */
c41564b5 3679 int low; /* lower boundary in search */
8867bc9b
MK
3680 xfs_extnum_t nextents; /* number of file extents */
3681 xfs_fileoff_t startoff = 0; /* start offset of extent */
3682
3683 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3684 if (nextents == 0) {
3685 *idxp = 0;
3686 return NULL;
3687 }
3688 low = 0;
3689 if (ifp->if_flags & XFS_IFEXTIREC) {
3690 /* Find target extent list */
3691 int erp_idx = 0;
3692 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3693 base = erp->er_extbuf;
3694 high = erp->er_extcount - 1;
3695 } else {
3696 base = ifp->if_u1.if_extents;
3697 high = nextents - 1;
3698 }
3699 /* Binary search extent records */
3700 while (low <= high) {
3701 idx = (low + high) >> 1;
3702 ep = base + idx;
3703 startoff = xfs_bmbt_get_startoff(ep);
3704 blockcount = xfs_bmbt_get_blockcount(ep);
3705 if (bno < startoff) {
3706 high = idx - 1;
3707 } else if (bno >= startoff + blockcount) {
3708 low = idx + 1;
3709 } else {
3710 /* Convert back to file-based extent index */
3711 if (ifp->if_flags & XFS_IFEXTIREC) {
3712 idx += erp->er_extoff;
3713 }
3714 *idxp = idx;
3715 return ep;
3716 }
3717 }
3718 /* Convert back to file-based extent index */
3719 if (ifp->if_flags & XFS_IFEXTIREC) {
3720 idx += erp->er_extoff;
3721 }
3722 if (bno >= startoff + blockcount) {
3723 if (++idx == nextents) {
3724 ep = NULL;
3725 } else {
3726 ep = xfs_iext_get_ext(ifp, idx);
3727 }
3728 }
3729 *idxp = idx;
3730 return ep;
3731}
3732
0293ce3a
MK
3733/*
3734 * Return a pointer to the indirection array entry containing the
3735 * extent record for filesystem block bno. Store the index of the
3736 * target irec in *erp_idxp.
3737 */
8867bc9b 3738xfs_ext_irec_t * /* pointer to found extent record */
0293ce3a
MK
3739xfs_iext_bno_to_irec(
3740 xfs_ifork_t *ifp, /* inode fork pointer */
3741 xfs_fileoff_t bno, /* block number to search for */
3742 int *erp_idxp) /* irec index of target ext list */
3743{
3744 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3745 xfs_ext_irec_t *erp_next; /* next indirection array entry */
8867bc9b 3746 int erp_idx; /* indirection array index */
0293ce3a
MK
3747 int nlists; /* number of extent irec's (lists) */
3748 int high; /* binary search upper limit */
3749 int low; /* binary search lower limit */
3750
3751 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3752 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3753 erp_idx = 0;
3754 low = 0;
3755 high = nlists - 1;
3756 while (low <= high) {
3757 erp_idx = (low + high) >> 1;
3758 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3759 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3760 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3761 high = erp_idx - 1;
3762 } else if (erp_next && bno >=
3763 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3764 low = erp_idx + 1;
3765 } else {
3766 break;
3767 }
3768 }
3769 *erp_idxp = erp_idx;
3770 return erp;
3771}
3772
3773/*
3774 * Return a pointer to the indirection array entry containing the
3775 * extent record at file extent index *idxp. Store the index of the
3776 * target irec in *erp_idxp and store the page index of the target
3777 * extent record in *idxp.
3778 */
3779xfs_ext_irec_t *
3780xfs_iext_idx_to_irec(
3781 xfs_ifork_t *ifp, /* inode fork pointer */
3782 xfs_extnum_t *idxp, /* extent index (file -> page) */
3783 int *erp_idxp, /* pointer to target irec */
3784 int realloc) /* new bytes were just added */
3785{
3786 xfs_ext_irec_t *prev; /* pointer to previous irec */
3787 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3788 int erp_idx; /* indirection array index */
3789 int nlists; /* number of irec's (ex lists) */
3790 int high; /* binary search upper limit */
3791 int low; /* binary search lower limit */
3792 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3793
3794 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
87bef181
CH
3795 ASSERT(page_idx >= 0);
3796 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3797 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3798
0293ce3a
MK
3799 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3800 erp_idx = 0;
3801 low = 0;
3802 high = nlists - 1;
3803
3804 /* Binary search extent irec's */
3805 while (low <= high) {
3806 erp_idx = (low + high) >> 1;
3807 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3808 prev = erp_idx > 0 ? erp - 1 : NULL;
3809 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3810 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3811 high = erp_idx - 1;
3812 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3813 (page_idx == erp->er_extoff + erp->er_extcount &&
3814 !realloc)) {
3815 low = erp_idx + 1;
3816 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3817 erp->er_extcount == XFS_LINEAR_EXTS) {
3818 ASSERT(realloc);
3819 page_idx = 0;
3820 erp_idx++;
3821 erp = erp_idx < nlists ? erp + 1 : NULL;
3822 break;
3823 } else {
3824 page_idx -= erp->er_extoff;
3825 break;
3826 }
3827 }
3828 *idxp = page_idx;
3829 *erp_idxp = erp_idx;
3830 return(erp);
3831}
3832
3833/*
3834 * Allocate and initialize an indirection array once the space needed
3835 * for incore extents increases above XFS_IEXT_BUFSZ.
3836 */
3837void
3838xfs_iext_irec_init(
3839 xfs_ifork_t *ifp) /* inode fork pointer */
3840{
3841 xfs_ext_irec_t *erp; /* indirection array pointer */
3842 xfs_extnum_t nextents; /* number of extents in file */
3843
3844 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3845 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3846 ASSERT(nextents <= XFS_LINEAR_EXTS);
3847
6785073b 3848 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
0293ce3a
MK
3849
3850 if (nextents == 0) {
6785073b 3851 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
0293ce3a
MK
3852 } else if (!ifp->if_real_bytes) {
3853 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3854 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3855 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3856 }
3857 erp->er_extbuf = ifp->if_u1.if_extents;
3858 erp->er_extcount = nextents;
3859 erp->er_extoff = 0;
3860
3861 ifp->if_flags |= XFS_IFEXTIREC;
3862 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3863 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3864 ifp->if_u1.if_ext_irec = erp;
3865
3866 return;
3867}
3868
3869/*
3870 * Allocate and initialize a new entry in the indirection array.
3871 */
3872xfs_ext_irec_t *
3873xfs_iext_irec_new(
3874 xfs_ifork_t *ifp, /* inode fork pointer */
3875 int erp_idx) /* index for new irec */
3876{
3877 xfs_ext_irec_t *erp; /* indirection array pointer */
3878 int i; /* loop counter */
3879 int nlists; /* number of irec's (ex lists) */
3880
3881 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3882 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3883
3884 /* Resize indirection array */
3885 xfs_iext_realloc_indirect(ifp, ++nlists *
3886 sizeof(xfs_ext_irec_t));
3887 /*
3888 * Move records down in the array so the
3889 * new page can use erp_idx.
3890 */
3891 erp = ifp->if_u1.if_ext_irec;
3892 for (i = nlists - 1; i > erp_idx; i--) {
3893 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3894 }
3895 ASSERT(i == erp_idx);
3896
3897 /* Initialize new extent record */
3898 erp = ifp->if_u1.if_ext_irec;
6785073b 3899 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
0293ce3a
MK
3900 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3901 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3902 erp[erp_idx].er_extcount = 0;
3903 erp[erp_idx].er_extoff = erp_idx > 0 ?
3904 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3905 return (&erp[erp_idx]);
3906}
3907
3908/*
3909 * Remove a record from the indirection array.
3910 */
3911void
3912xfs_iext_irec_remove(
3913 xfs_ifork_t *ifp, /* inode fork pointer */
3914 int erp_idx) /* irec index to remove */
3915{
3916 xfs_ext_irec_t *erp; /* indirection array pointer */
3917 int i; /* loop counter */
3918 int nlists; /* number of irec's (ex lists) */
3919
3920 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3921 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3922 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3923 if (erp->er_extbuf) {
3924 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3925 -erp->er_extcount);
f0e2d93c 3926 kmem_free(erp->er_extbuf);
0293ce3a
MK
3927 }
3928 /* Compact extent records */
3929 erp = ifp->if_u1.if_ext_irec;
3930 for (i = erp_idx; i < nlists - 1; i++) {
3931 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3932 }
3933 /*
3934 * Manually free the last extent record from the indirection
3935 * array. A call to xfs_iext_realloc_indirect() with a size
3936 * of zero would result in a call to xfs_iext_destroy() which
3937 * would in turn call this function again, creating a nasty
3938 * infinite loop.
3939 */
3940 if (--nlists) {
3941 xfs_iext_realloc_indirect(ifp,
3942 nlists * sizeof(xfs_ext_irec_t));
3943 } else {
f0e2d93c 3944 kmem_free(ifp->if_u1.if_ext_irec);
0293ce3a
MK
3945 }
3946 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3947}
3948
3949/*
3950 * This is called to clean up large amounts of unused memory allocated
3951 * by the indirection array. Before compacting anything though, verify
3952 * that the indirection array is still needed and switch back to the
3953 * linear extent list (or even the inline buffer) if possible. The
3954 * compaction policy is as follows:
3955 *
3956 * Full Compaction: Extents fit into a single page (or inline buffer)
71a8c87f 3957 * Partial Compaction: Extents occupy less than 50% of allocated space
0293ce3a
MK
3958 * No Compaction: Extents occupy at least 50% of allocated space
3959 */
3960void
3961xfs_iext_irec_compact(
3962 xfs_ifork_t *ifp) /* inode fork pointer */
3963{
3964 xfs_extnum_t nextents; /* number of extents in file */
3965 int nlists; /* number of irec's (ex lists) */
3966
3967 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3968 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3969 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3970
3971 if (nextents == 0) {
3972 xfs_iext_destroy(ifp);
3973 } else if (nextents <= XFS_INLINE_EXTS) {
3974 xfs_iext_indirect_to_direct(ifp);
3975 xfs_iext_direct_to_inline(ifp, nextents);
3976 } else if (nextents <= XFS_LINEAR_EXTS) {
3977 xfs_iext_indirect_to_direct(ifp);
0293ce3a
MK
3978 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3979 xfs_iext_irec_compact_pages(ifp);
3980 }
3981}
3982
3983/*
3984 * Combine extents from neighboring extent pages.
3985 */
3986void
3987xfs_iext_irec_compact_pages(
3988 xfs_ifork_t *ifp) /* inode fork pointer */
3989{
3990 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3991 int erp_idx = 0; /* indirection array index */
3992 int nlists; /* number of irec's (ex lists) */
3993
3994 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3995 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3996 while (erp_idx < nlists - 1) {
3997 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3998 erp_next = erp + 1;
3999 if (erp_next->er_extcount <=
4000 (XFS_LINEAR_EXTS - erp->er_extcount)) {
71a8c87f 4001 memcpy(&erp->er_extbuf[erp->er_extcount],
0293ce3a
MK
4002 erp_next->er_extbuf, erp_next->er_extcount *
4003 sizeof(xfs_bmbt_rec_t));
4004 erp->er_extcount += erp_next->er_extcount;
4005 /*
4006 * Free page before removing extent record
4007 * so er_extoffs don't get modified in
4008 * xfs_iext_irec_remove.
4009 */
f0e2d93c 4010 kmem_free(erp_next->er_extbuf);
0293ce3a
MK
4011 erp_next->er_extbuf = NULL;
4012 xfs_iext_irec_remove(ifp, erp_idx + 1);
4013 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4014 } else {
4015 erp_idx++;
4016 }
4017 }
4018}
4019
0293ce3a
MK
4020/*
4021 * This is called to update the er_extoff field in the indirection
4022 * array when extents have been added or removed from one of the
4023 * extent lists. erp_idx contains the irec index to begin updating
4024 * at and ext_diff contains the number of extents that were added
4025 * or removed.
4026 */
4027void
4028xfs_iext_irec_update_extoffs(
4029 xfs_ifork_t *ifp, /* inode fork pointer */
4030 int erp_idx, /* irec index to update */
4031 int ext_diff) /* number of new extents */
4032{
4033 int i; /* loop counter */
4034 int nlists; /* number of irec's (ex lists */
4035
4036 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4037 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4038 for (i = erp_idx; i < nlists; i++) {
4039 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4040 }
4041}
72b53efa
BF
4042
4043/*
4044 * Test whether it is appropriate to check an inode for and free post EOF
4045 * blocks. The 'force' parameter determines whether we should also consider
4046 * regular files that are marked preallocated or append-only.
4047 */
4048bool
4049xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
4050{
4051 /* prealloc/delalloc exists only on regular files */
4052 if (!S_ISREG(ip->i_d.di_mode))
4053 return false;
4054
4055 /*
4056 * Zero sized files with no cached pages and delalloc blocks will not
4057 * have speculative prealloc/delalloc blocks to remove.
4058 */
4059 if (VFS_I(ip)->i_size == 0 &&
4060 VN_CACHED(VFS_I(ip)) == 0 &&
4061 ip->i_delayed_blks == 0)
4062 return false;
4063
4064 /* If we haven't read in the extent list, then don't do it now. */
4065 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
4066 return false;
4067
4068 /*
4069 * Do not free real preallocated or append-only files unless the file
4070 * has delalloc blocks and we are forced to remove them.
4071 */
4072 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
4073 if (!force || ip->i_delayed_blks == 0)
4074 return false;
4075
4076 return true;
4077}
4078