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