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