]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_inode_fork.c
xfs: use a b+tree for the in-core extent list
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_inode_fork.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 "libxfs_priv.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_inode.h"
25 #include "xfs_trans.h"
26 #include "xfs_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_bmap.h"
29 #include "xfs_trace.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_da_format.h"
32 #include "xfs_da_btree.h"
33 #include "xfs_dir2_priv.h"
34
35
36 kmem_zone_t *xfs_ifork_zone;
37
38 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
39 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
40 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
41
42 static inline dev_t xfs_to_linux_dev_t(xfs_dev_t dev)
43 {
44 return MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
45 }
46
47 /*
48 * Copy inode type and data and attr format specific information from the
49 * on-disk inode to the in-core inode and fork structures. For fifos, devices,
50 * and sockets this means set i_rdev to the proper value. For files,
51 * directories, and symlinks this means to bring in the in-line data or extent
52 * pointers as well as the attribute fork. For a fork in B-tree format, only
53 * the root is immediately brought in-core. The rest will be read in later when
54 * first referenced (see xfs_iread_extents()).
55 */
56 int
57 xfs_iformat_fork(
58 struct xfs_inode *ip,
59 struct xfs_dinode *dip)
60 {
61 struct inode *inode = VFS_I(ip);
62 struct xfs_attr_shortform *atp;
63 int size;
64 int error = 0;
65 xfs_fsize_t di_size;
66
67 if (unlikely(be32_to_cpu(dip->di_nextents) +
68 be16_to_cpu(dip->di_anextents) >
69 be64_to_cpu(dip->di_nblocks))) {
70 xfs_warn(ip->i_mount,
71 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
72 (unsigned long long)ip->i_ino,
73 (int)(be32_to_cpu(dip->di_nextents) +
74 be16_to_cpu(dip->di_anextents)),
75 (unsigned long long)
76 be64_to_cpu(dip->di_nblocks));
77 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
78 ip->i_mount, dip);
79 return -EFSCORRUPTED;
80 }
81
82 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
83 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
84 (unsigned long long)ip->i_ino,
85 dip->di_forkoff);
86 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
87 ip->i_mount, dip);
88 return -EFSCORRUPTED;
89 }
90
91 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
92 !ip->i_mount->m_rtdev_targp)) {
93 xfs_warn(ip->i_mount,
94 "corrupt dinode %Lu, has realtime flag set.",
95 ip->i_ino);
96 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
97 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
98 return -EFSCORRUPTED;
99 }
100
101 if (unlikely(xfs_is_reflink_inode(ip) && !S_ISREG(inode->i_mode))) {
102 xfs_warn(ip->i_mount,
103 "corrupt dinode %llu, wrong file type for reflink.",
104 ip->i_ino);
105 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
106 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
107 return -EFSCORRUPTED;
108 }
109
110 if (unlikely(xfs_is_reflink_inode(ip) &&
111 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
112 xfs_warn(ip->i_mount,
113 "corrupt dinode %llu, has reflink+realtime flag set.",
114 ip->i_ino);
115 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
116 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
117 return -EFSCORRUPTED;
118 }
119
120 switch (inode->i_mode & S_IFMT) {
121 case S_IFIFO:
122 case S_IFCHR:
123 case S_IFBLK:
124 case S_IFSOCK:
125 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
126 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
127 ip->i_mount, dip);
128 return -EFSCORRUPTED;
129 }
130 ip->i_d.di_size = 0;
131 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
132 break;
133
134 case S_IFREG:
135 case S_IFLNK:
136 case S_IFDIR:
137 switch (dip->di_format) {
138 case XFS_DINODE_FMT_LOCAL:
139 /*
140 * no local regular files yet
141 */
142 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
143 xfs_warn(ip->i_mount,
144 "corrupt inode %Lu (local format for regular file).",
145 (unsigned long long) ip->i_ino);
146 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
147 XFS_ERRLEVEL_LOW,
148 ip->i_mount, dip);
149 return -EFSCORRUPTED;
150 }
151
152 di_size = be64_to_cpu(dip->di_size);
153 if (unlikely(di_size < 0 ||
154 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
155 xfs_warn(ip->i_mount,
156 "corrupt inode %Lu (bad size %Ld for local inode).",
157 (unsigned long long) ip->i_ino,
158 (long long) di_size);
159 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
160 XFS_ERRLEVEL_LOW,
161 ip->i_mount, dip);
162 return -EFSCORRUPTED;
163 }
164
165 size = (int)di_size;
166 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
167 break;
168 case XFS_DINODE_FMT_EXTENTS:
169 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
170 break;
171 case XFS_DINODE_FMT_BTREE:
172 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
173 break;
174 default:
175 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
176 ip->i_mount);
177 return -EFSCORRUPTED;
178 }
179 break;
180
181 default:
182 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
183 return -EFSCORRUPTED;
184 }
185 if (error)
186 return error;
187
188 /* Check inline dir contents. */
189 if (S_ISDIR(inode->i_mode) && dip->di_format == XFS_DINODE_FMT_LOCAL) {
190 error = xfs_dir2_sf_verify(ip);
191 if (error) {
192 xfs_idestroy_fork(ip, XFS_DATA_FORK);
193 return error;
194 }
195 }
196
197 if (xfs_is_reflink_inode(ip)) {
198 ASSERT(ip->i_cowfp == NULL);
199 xfs_ifork_init_cow(ip);
200 }
201
202 if (!XFS_DFORK_Q(dip))
203 return 0;
204
205 ASSERT(ip->i_afp == NULL);
206 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
207
208 switch (dip->di_aformat) {
209 case XFS_DINODE_FMT_LOCAL:
210 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
211 size = be16_to_cpu(atp->hdr.totsize);
212
213 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
214 xfs_warn(ip->i_mount,
215 "corrupt inode %Lu (bad attr fork size %Ld).",
216 (unsigned long long) ip->i_ino,
217 (long long) size);
218 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
219 XFS_ERRLEVEL_LOW,
220 ip->i_mount, dip);
221 error = -EFSCORRUPTED;
222 break;
223 }
224
225 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
226 break;
227 case XFS_DINODE_FMT_EXTENTS:
228 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
229 break;
230 case XFS_DINODE_FMT_BTREE:
231 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
232 break;
233 default:
234 error = -EFSCORRUPTED;
235 break;
236 }
237 if (error) {
238 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
239 ip->i_afp = NULL;
240 if (ip->i_cowfp)
241 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
242 ip->i_cowfp = NULL;
243 xfs_idestroy_fork(ip, XFS_DATA_FORK);
244 }
245 return error;
246 }
247
248 void
249 xfs_init_local_fork(
250 struct xfs_inode *ip,
251 int whichfork,
252 const void *data,
253 int size)
254 {
255 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
256 int mem_size = size, real_size = 0;
257 bool zero_terminate;
258
259 /*
260 * If we are using the local fork to store a symlink body we need to
261 * zero-terminate it so that we can pass it back to the VFS directly.
262 * Overallocate the in-memory fork by one for that and add a zero
263 * to terminate it below.
264 */
265 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
266 if (zero_terminate)
267 mem_size++;
268
269 if (size) {
270 real_size = roundup(mem_size, 4);
271 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
272 memcpy(ifp->if_u1.if_data, data, size);
273 if (zero_terminate)
274 ifp->if_u1.if_data[size] = '\0';
275 } else {
276 ifp->if_u1.if_data = NULL;
277 }
278
279 ifp->if_bytes = size;
280 ifp->if_real_bytes = real_size;
281 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
282 ifp->if_flags |= XFS_IFINLINE;
283 }
284
285 /*
286 * The file is in-lined in the on-disk inode.
287 */
288 STATIC int
289 xfs_iformat_local(
290 xfs_inode_t *ip,
291 xfs_dinode_t *dip,
292 int whichfork,
293 int size)
294 {
295 /*
296 * If the size is unreasonable, then something
297 * is wrong and we just bail out rather than crash in
298 * kmem_alloc() or memcpy() below.
299 */
300 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
301 xfs_warn(ip->i_mount,
302 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
303 (unsigned long long) ip->i_ino, size,
304 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
305 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
306 ip->i_mount, dip);
307 return -EFSCORRUPTED;
308 }
309
310 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
311 return 0;
312 }
313
314 /*
315 * The file consists of a set of extents all of which fit into the on-disk
316 * inode.
317 */
318 STATIC int
319 xfs_iformat_extents(
320 struct xfs_inode *ip,
321 struct xfs_dinode *dip,
322 int whichfork)
323 {
324 struct xfs_mount *mp = ip->i_mount;
325 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
326 int state = xfs_bmap_fork_to_state(whichfork);
327 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
328 int size = nex * sizeof(xfs_bmbt_rec_t);
329 struct xfs_iext_cursor icur;
330 struct xfs_bmbt_rec *dp;
331 struct xfs_bmbt_irec new;
332 int i;
333
334 /*
335 * If the number of extents is unreasonable, then something is wrong and
336 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
337 */
338 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
339 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
340 (unsigned long long) ip->i_ino, nex);
341 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
342 mp, dip);
343 return -EFSCORRUPTED;
344 }
345
346 ifp->if_real_bytes = 0;
347 ifp->if_bytes = 0;
348 ifp->if_u1.if_root = NULL;
349 ifp->if_height = 0;
350 if (size) {
351 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
352
353 xfs_iext_first(ifp, &icur);
354 for (i = 0; i < nex; i++, dp++) {
355 if (!xfs_bmbt_validate_extent(mp, whichfork, dp)) {
356 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
357 XFS_ERRLEVEL_LOW, mp);
358 return -EFSCORRUPTED;
359 }
360
361 xfs_bmbt_disk_get_all(dp, &new);
362 xfs_iext_insert(ip, &icur, 1, &new, state);
363 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
364 xfs_iext_next(ifp, &icur);
365 }
366 }
367 ifp->if_flags |= XFS_IFEXTENTS;
368 return 0;
369 }
370
371 /*
372 * The file has too many extents to fit into
373 * the inode, so they are in B-tree format.
374 * Allocate a buffer for the root of the B-tree
375 * and copy the root into it. The i_extents
376 * field will remain NULL until all of the
377 * extents are read in (when they are needed).
378 */
379 STATIC int
380 xfs_iformat_btree(
381 xfs_inode_t *ip,
382 xfs_dinode_t *dip,
383 int whichfork)
384 {
385 struct xfs_mount *mp = ip->i_mount;
386 xfs_bmdr_block_t *dfp;
387 xfs_ifork_t *ifp;
388 /* REFERENCED */
389 int nrecs;
390 int size;
391 int level;
392
393 ifp = XFS_IFORK_PTR(ip, whichfork);
394 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
395 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
396 nrecs = be16_to_cpu(dfp->bb_numrecs);
397 level = be16_to_cpu(dfp->bb_level);
398
399 /*
400 * blow out if -- fork has less extents than can fit in
401 * fork (fork shouldn't be a btree format), root btree
402 * block has more records than can fit into the fork,
403 * or the number of extents is greater than the number of
404 * blocks.
405 */
406 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
407 XFS_IFORK_MAXEXT(ip, whichfork) ||
408 XFS_BMDR_SPACE_CALC(nrecs) >
409 XFS_DFORK_SIZE(dip, mp, whichfork) ||
410 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
411 level == 0 || level > XFS_BTREE_MAXLEVELS) {
412 xfs_warn(mp, "corrupt inode %Lu (btree).",
413 (unsigned long long) ip->i_ino);
414 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
415 mp, dip);
416 return -EFSCORRUPTED;
417 }
418
419 ifp->if_broot_bytes = size;
420 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
421 ASSERT(ifp->if_broot != NULL);
422 /*
423 * Copy and convert from the on-disk structure
424 * to the in-memory structure.
425 */
426 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
427 ifp->if_broot, size);
428 ifp->if_flags &= ~XFS_IFEXTENTS;
429 ifp->if_flags |= XFS_IFBROOT;
430
431 ifp->if_real_bytes = 0;
432 ifp->if_bytes = 0;
433 ifp->if_u1.if_root = NULL;
434 ifp->if_height = 0;
435 return 0;
436 }
437
438 /*
439 * Reallocate the space for if_broot based on the number of records
440 * being added or deleted as indicated in rec_diff. Move the records
441 * and pointers in if_broot to fit the new size. When shrinking this
442 * will eliminate holes between the records and pointers created by
443 * the caller. When growing this will create holes to be filled in
444 * by the caller.
445 *
446 * The caller must not request to add more records than would fit in
447 * the on-disk inode root. If the if_broot is currently NULL, then
448 * if we are adding records, one will be allocated. The caller must also
449 * not request that the number of records go below zero, although
450 * it can go to zero.
451 *
452 * ip -- the inode whose if_broot area is changing
453 * ext_diff -- the change in the number of records, positive or negative,
454 * requested for the if_broot array.
455 */
456 void
457 xfs_iroot_realloc(
458 xfs_inode_t *ip,
459 int rec_diff,
460 int whichfork)
461 {
462 struct xfs_mount *mp = ip->i_mount;
463 int cur_max;
464 xfs_ifork_t *ifp;
465 struct xfs_btree_block *new_broot;
466 int new_max;
467 size_t new_size;
468 char *np;
469 char *op;
470
471 /*
472 * Handle the degenerate case quietly.
473 */
474 if (rec_diff == 0) {
475 return;
476 }
477
478 ifp = XFS_IFORK_PTR(ip, whichfork);
479 if (rec_diff > 0) {
480 /*
481 * If there wasn't any memory allocated before, just
482 * allocate it now and get out.
483 */
484 if (ifp->if_broot_bytes == 0) {
485 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
486 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
487 ifp->if_broot_bytes = (int)new_size;
488 return;
489 }
490
491 /*
492 * If there is already an existing if_broot, then we need
493 * to realloc() it and shift the pointers to their new
494 * location. The records don't change location because
495 * they are kept butted up against the btree block header.
496 */
497 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
498 new_max = cur_max + rec_diff;
499 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
500 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
501 KM_SLEEP | KM_NOFS);
502 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
503 ifp->if_broot_bytes);
504 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
505 (int)new_size);
506 ifp->if_broot_bytes = (int)new_size;
507 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
508 XFS_IFORK_SIZE(ip, whichfork));
509 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
510 return;
511 }
512
513 /*
514 * rec_diff is less than 0. In this case, we are shrinking the
515 * if_broot buffer. It must already exist. If we go to zero
516 * records, just get rid of the root and clear the status bit.
517 */
518 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
519 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
520 new_max = cur_max + rec_diff;
521 ASSERT(new_max >= 0);
522 if (new_max > 0)
523 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
524 else
525 new_size = 0;
526 if (new_size > 0) {
527 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
528 /*
529 * First copy over the btree block header.
530 */
531 memcpy(new_broot, ifp->if_broot,
532 XFS_BMBT_BLOCK_LEN(ip->i_mount));
533 } else {
534 new_broot = NULL;
535 ifp->if_flags &= ~XFS_IFBROOT;
536 }
537
538 /*
539 * Only copy the records and pointers if there are any.
540 */
541 if (new_max > 0) {
542 /*
543 * First copy the records.
544 */
545 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
546 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
547 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
548
549 /*
550 * Then copy the pointers.
551 */
552 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
553 ifp->if_broot_bytes);
554 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
555 (int)new_size);
556 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
557 }
558 kmem_free(ifp->if_broot);
559 ifp->if_broot = new_broot;
560 ifp->if_broot_bytes = (int)new_size;
561 if (ifp->if_broot)
562 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
563 XFS_IFORK_SIZE(ip, whichfork));
564 return;
565 }
566
567
568 /*
569 * This is called when the amount of space needed for if_data
570 * is increased or decreased. The change in size is indicated by
571 * the number of bytes that need to be added or deleted in the
572 * byte_diff parameter.
573 *
574 * If the amount of space needed has decreased below the size of the
575 * inline buffer, then switch to using the inline buffer. Otherwise,
576 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
577 * to what is needed.
578 *
579 * ip -- the inode whose if_data area is changing
580 * byte_diff -- the change in the number of bytes, positive or negative,
581 * requested for the if_data array.
582 */
583 void
584 xfs_idata_realloc(
585 xfs_inode_t *ip,
586 int byte_diff,
587 int whichfork)
588 {
589 xfs_ifork_t *ifp;
590 int new_size;
591 int real_size;
592
593 if (byte_diff == 0) {
594 return;
595 }
596
597 ifp = XFS_IFORK_PTR(ip, whichfork);
598 new_size = (int)ifp->if_bytes + byte_diff;
599 ASSERT(new_size >= 0);
600
601 if (new_size == 0) {
602 kmem_free(ifp->if_u1.if_data);
603 ifp->if_u1.if_data = NULL;
604 real_size = 0;
605 } else {
606 /*
607 * Stuck with malloc/realloc.
608 * For inline data, the underlying buffer must be
609 * a multiple of 4 bytes in size so that it can be
610 * logged and stay on word boundaries. We enforce
611 * that here.
612 */
613 real_size = roundup(new_size, 4);
614 if (ifp->if_u1.if_data == NULL) {
615 ASSERT(ifp->if_real_bytes == 0);
616 ifp->if_u1.if_data = kmem_alloc(real_size,
617 KM_SLEEP | KM_NOFS);
618 } else {
619 /*
620 * Only do the realloc if the underlying size
621 * is really changing.
622 */
623 if (ifp->if_real_bytes != real_size) {
624 ifp->if_u1.if_data =
625 kmem_realloc(ifp->if_u1.if_data,
626 real_size,
627 KM_SLEEP | KM_NOFS);
628 }
629 }
630 }
631 ifp->if_real_bytes = real_size;
632 ifp->if_bytes = new_size;
633 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
634 }
635
636 void
637 xfs_idestroy_fork(
638 xfs_inode_t *ip,
639 int whichfork)
640 {
641 xfs_ifork_t *ifp;
642
643 ifp = XFS_IFORK_PTR(ip, whichfork);
644 if (ifp->if_broot != NULL) {
645 kmem_free(ifp->if_broot);
646 ifp->if_broot = NULL;
647 }
648
649 /*
650 * If the format is local, then we can't have an extents
651 * array so just look for an inline data array. If we're
652 * not local then we may or may not have an extents list,
653 * so check and free it up if we do.
654 */
655 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
656 if (ifp->if_u1.if_data != NULL) {
657 ASSERT(ifp->if_real_bytes != 0);
658 kmem_free(ifp->if_u1.if_data);
659 ifp->if_u1.if_data = NULL;
660 ifp->if_real_bytes = 0;
661 }
662 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
663 xfs_iext_destroy(ifp);
664 }
665
666 ASSERT(ifp->if_real_bytes == 0);
667
668 if (whichfork == XFS_ATTR_FORK) {
669 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
670 ip->i_afp = NULL;
671 } else if (whichfork == XFS_COW_FORK) {
672 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
673 ip->i_cowfp = NULL;
674 }
675 }
676
677 /*
678 * Convert in-core extents to on-disk form
679 *
680 * In the case of the data fork, the in-core and on-disk fork sizes can be
681 * different due to delayed allocation extents. We only copy on-disk extents
682 * here, so callers must always use the physical fork size to determine the
683 * size of the buffer passed to this routine. We will return the size actually
684 * used.
685 */
686 int
687 xfs_iextents_copy(
688 struct xfs_inode *ip,
689 struct xfs_bmbt_rec *dp,
690 int whichfork)
691 {
692 int state = xfs_bmap_fork_to_state(whichfork);
693 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
694 struct xfs_iext_cursor icur;
695 struct xfs_bmbt_irec rec;
696 int copied = 0;
697
698 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
699 ASSERT(ifp->if_bytes > 0);
700
701 for_each_xfs_iext(ifp, &icur, &rec) {
702 if (isnullstartblock(rec.br_startblock))
703 continue;
704 xfs_bmbt_disk_set_all(dp, &rec);
705 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
706 ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, dp));
707 copied += sizeof(struct xfs_bmbt_rec);
708 dp++;
709 }
710
711 ASSERT(copied > 0);
712 ASSERT(copied <= ifp->if_bytes);
713 return copied;
714 }
715
716 /*
717 * Each of the following cases stores data into the same region
718 * of the on-disk inode, so only one of them can be valid at
719 * any given time. While it is possible to have conflicting formats
720 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
721 * in EXTENTS format, this can only happen when the fork has
722 * changed formats after being modified but before being flushed.
723 * In these cases, the format always takes precedence, because the
724 * format indicates the current state of the fork.
725 */
726 void
727 xfs_iflush_fork(
728 xfs_inode_t *ip,
729 xfs_dinode_t *dip,
730 xfs_inode_log_item_t *iip,
731 int whichfork)
732 {
733 char *cp;
734 xfs_ifork_t *ifp;
735 xfs_mount_t *mp;
736 static const short brootflag[2] =
737 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
738 static const short dataflag[2] =
739 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
740 static const short extflag[2] =
741 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
742
743 if (!iip)
744 return;
745 ifp = XFS_IFORK_PTR(ip, whichfork);
746 /*
747 * This can happen if we gave up in iformat in an error path,
748 * for the attribute fork.
749 */
750 if (!ifp) {
751 ASSERT(whichfork == XFS_ATTR_FORK);
752 return;
753 }
754 cp = XFS_DFORK_PTR(dip, whichfork);
755 mp = ip->i_mount;
756 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
757 case XFS_DINODE_FMT_LOCAL:
758 if ((iip->ili_fields & dataflag[whichfork]) &&
759 (ifp->if_bytes > 0)) {
760 ASSERT(ifp->if_u1.if_data != NULL);
761 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
762 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
763 }
764 break;
765
766 case XFS_DINODE_FMT_EXTENTS:
767 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
768 !(iip->ili_fields & extflag[whichfork]));
769 if ((iip->ili_fields & extflag[whichfork]) &&
770 (ifp->if_bytes > 0)) {
771 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
772 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
773 whichfork);
774 }
775 break;
776
777 case XFS_DINODE_FMT_BTREE:
778 if ((iip->ili_fields & brootflag[whichfork]) &&
779 (ifp->if_broot_bytes > 0)) {
780 ASSERT(ifp->if_broot != NULL);
781 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
782 XFS_IFORK_SIZE(ip, whichfork));
783 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
784 (xfs_bmdr_block_t *)cp,
785 XFS_DFORK_SIZE(dip, mp, whichfork));
786 }
787 break;
788
789 case XFS_DINODE_FMT_DEV:
790 if (iip->ili_fields & XFS_ILOG_DEV) {
791 ASSERT(whichfork == XFS_DATA_FORK);
792 xfs_dinode_put_rdev(dip, sysv_encode_dev(VFS_I(ip)->i_rdev));
793 }
794 break;
795
796 default:
797 ASSERT(0);
798 break;
799 }
800 }
801
802 /* Convert bmap state flags to an inode fork. */
803 struct xfs_ifork *
804 xfs_iext_state_to_fork(
805 struct xfs_inode *ip,
806 int state)
807 {
808 if (state & BMAP_COWFORK)
809 return ip->i_cowfp;
810 else if (state & BMAP_ATTRFORK)
811 return ip->i_afp;
812 return &ip->i_df;
813 }
814
815 /*
816 * Initialize an inode's copy-on-write fork.
817 */
818 void
819 xfs_ifork_init_cow(
820 struct xfs_inode *ip)
821 {
822 if (ip->i_cowfp)
823 return;
824
825 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
826 KM_SLEEP | KM_NOFS);
827 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
828 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
829 ip->i_cnextents = 0;
830 }