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