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