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