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