4 * hed - Hexadecimal editor
5 * Copyright (C) 2004 Petr Baudis <pasky@ucw.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * There hammer on the anvil smote,
23 * There chisel clove, and graver wrote;
24 * There forged was blade, and bound was hilt;
25 * The delver mined, the mason built.
26 * There beryl, pearl, and opal pale,
27 * And metal wrought like fishes' mail,
28 * Buckler and corslet, axe and sword,
29 * And shining spears were laid in hoard.
32 /* Feature macros needed for:
47 #include <sys/ioctl.h>
49 #include <linux/fs.h> /* BLKGETSIZE and BLKGETSIZE64 */
58 /* memrchr() might not be available */
61 #endif /* HAVE_MEMRCHR */
64 * `Piles of jewels?' said Gandalf. `No. The Orcs have often plundered Moria;
65 * there is nothing left in the upper halls. And since the dwarves fled, no one
66 * dares to seek the shafts and treasuries down in the deep places: they are
67 * drowned in water--or in a shadow of fear.'
70 /* TODO: Currently the file blocks allocation is not very sophisticated and
71 * when the weather is bad it could probably have rather horrible results. */
75 #define BDEBUG(x...) fprintf(stderr, x)
80 /* Number of blocks in cache */
81 #define CACHE_LENGTH 64
83 /* Blocks for readahead */
84 #define FILE_READAHEAD (CACHE_LENGTH/2)
86 #define file_block hed_block
87 #define blockoff_t hed_cursor_t
89 #define first_block(tree) (tree_entry(first_in_tree(tree),struct file_block,t))
90 #define prev_block(b) (tree_entry(prev_in_tree(&(b)->t),struct file_block,t))
91 #define next_block(b) (tree_entry(next_in_tree(&(b)->t),struct file_block,t))
92 #define last_block(tree) (tree_entry(last_in_tree(tree),struct file_block,t))
94 #define block_offset(b) tree_block_offset(&(b)->t)
96 #define recalc_block_recursive(b) recalc_node_recursive(&(b)->t)
98 #define chain_block(tree,b) insert_into_tree((tree), &(b)->t, NULL)
99 #define recalc_chain_block(tree,b) do { \
100 chain_block((tree), (b)); \
101 recalc_block_recursive((b)); \
104 #define chain_block_after(tree,p,b) insert_into_tree((tree), &(b)->t, &(p)->t)
106 #define recalc_chain_block_after(tree,p,b) do { \
107 chain_block_after((tree), (p), (b)); \
108 recalc_block_recursive((b)); \
111 #define unchain_block(tree,b) del_from_tree((tree), &(b)->t)
112 #define recalc_unchain_block(tree,b) recalc_del_from_tree((tree), &(b)->t)
114 #define init_block_list(tree,b) init_tree(tree, &(b)->t)
115 #define init_block_link(b) init_node(&(b)->t)
117 #define find_block(tree,o) tree_entry(find_in_tree((tree),(o)),struct file_block,t)
119 #define file_size hed_file_size
120 #define file_blocks hed_file_blocks
122 #ifdef HED_CONFIG_SWAP
124 /* Return the swp file object */
125 static inline struct swp_file
*
126 file_swp(struct hed_file
*file
)
133 /* Provide a stub for the non-swap case */
135 file_swp(struct hed_file
*file
)
142 #ifdef HED_CONFIG_READAHEAD
144 #define file_ra_none(f) ((f)->readahead == HED_RA_NONE)
145 #define file_ra_forward(f) ((f)->readahead == HED_RA_FORWARD)
146 #define file_ra_backward(f) ((f)->readahead == HED_RA_BACKWARD)
150 #define file_ra_none(f) (1)
151 #define file_ra_forward(f) (0)
152 #define file_ra_backward(f) (0)
154 #endif /* HED_CONFIG_READAHEAD */
156 /* Get the physical offset of the byte immediately following @block. */
157 static inline hed_uoff_t
158 phys_end(const struct hed_block
*block
)
160 return hed_block_is_inserted(block
)
162 : block
->phys_pos
+ hed_block_size(block
);
165 static struct hed_block
*
166 next_nonzero_block(const struct hed_tree
*tree
, struct hed_block
*block
)
168 while (!hed_block_is_eof(block
)) {
169 block
= next_block(block
);
170 if (hed_block_size(block
))
176 static struct hed_block
*
177 prev_nonzero_block(const struct hed_tree
*tree
, struct hed_block
*block
)
180 block
= prev_block(block
);
181 if (hed_block_is_eof(block
))
183 } while (!hed_block_size(block
));
188 hed_block_is_after_erase(const struct hed_tree
*tree
, struct hed_block
*block
)
190 struct hed_block
*prev
= prev_nonzero_block(tree
, block
);
192 ? block
->phys_pos
> phys_end(prev
)
197 hed_block_is_after_insert(const struct hed_tree
*tree
, struct hed_block
*block
)
199 struct hed_block
*prev
= prev_nonzero_block(tree
, block
);
200 return prev
&& hed_block_is_inserted(prev
);
204 # define dump_blocks(file) {}
208 block_phys_size(struct hed_file
*file
, struct file_block
*block
)
210 struct file_block
*next
;
212 if (hed_block_is_eof(block
))
214 next
= next_block(block
);
215 return next
->phys_pos
- block
->phys_pos
;
219 dump_block(int level
, struct hed_file
*file
, struct hed_tree_node
*node
,
220 hed_uoff_t
*cur_offset
, hed_uoff_t
*cur_poffset
)
222 struct hed_block
*block
= tree_entry(node
, struct hed_block
, t
);
223 bool virtual = hed_block_is_virtual(block
);
229 dump_block(level
+ 1, file
, node
->left
, cur_offset
, cur_poffset
);
230 p
= hed_block_data(block
);
231 if (level
< 20) t
[level
] = '>'; else t
[19] = '.';
232 fprintf(stderr
, "%s [%06llx] [%06llx] %c%c%c %05llx %05llx"
233 " {%02x%02x%02x%02x} -- %p ^%p [%06llx]\n",
235 (unsigned long long) *cur_offset
,
236 (unsigned long long) *cur_poffset
,
238 hed_block_is_inserted(block
) ? 'i' : ' ',
239 hed_block_is_dirty(block
) ? '*' : ' ',
240 (unsigned long long) node
->size
,
241 (unsigned long long) block_phys_size(file
, block
),
242 p
&& block
->t
.size
> 0 ? p
[0] : 0,
243 p
&& block
->t
.size
> 1 ? p
[1] : 0,
244 p
&& block
->t
.size
> 2 ? p
[2] : 0,
245 p
&& block
->t
.size
> 3 ? p
[3] : 0,
247 (unsigned long long) node
->cover_size
249 list_for_each_entry (cur
, &block
->refs
, list
) {
250 fprintf(stderr
, " <%p>: %llx->%p:%llx\n",
251 cur
, (long long)cur
->pos
,
252 cur
->block
, (unsigned long long)cur
->off
);
254 assert(*cur_poffset
== block
->phys_pos
);
255 *cur_offset
+= node
->size
;
256 *cur_poffset
+= block_phys_size(file
, block
);
258 dump_block(level
+ 1, file
, node
->right
, cur_offset
, cur_poffset
);
259 assert(node
->cover_size
== (node
->left
? node
->left
->cover_size
: 0)
260 + (node
->right
? node
->right
->cover_size
: 0)
264 /* Walk the tree manually here, because foreach_block() does not provide
265 * the tree structure.
266 * TODO: Change this if you plan to debug any other block containers.
269 dump_blocks(struct hed_file
*file
)
271 struct file_block
*first
= first_block(file_blocks(file
));
272 hed_uoff_t cur_offset
, cur_poffset
;
274 fprintf(stderr
, "-- blocks dump --\n");
276 cur_poffset
= first
->phys_pos
;
277 dump_block(0, file
, file_blocks(file
)->root
,
278 &cur_offset
, &cur_poffset
);
279 fprintf(stderr
, "-- blocks dump end --\n");
284 get_cursor(struct hed_file
*file
, hed_uoff_t offset
, hed_cursor_t
*curs
)
286 struct file_block
*block
;
288 block
= find_block(file_blocks(file
), offset
);
289 assert(block
!= NULL
);
292 curs
->off
= offset
- block_offset(block
);
293 list_add(&curs
->list
, &block
->refs
);
295 BDEBUG("Mapped %llx to %llx+%llx/%llx\n",
296 offset
, offset
- curs
->off
, curs
->off
, block
->t
.size
);
300 hed_get_cursor(struct hed_file
*file
, hed_uoff_t offset
, hed_cursor_t
*curs
)
302 get_cursor(file
, offset
, curs
);
306 put_cursor(hed_cursor_t
*curs
)
308 list_del(&curs
->list
);
312 hed_put_cursor(hed_cursor_t
*curs
)
318 hed_update_cursor(struct hed_file
*file
, hed_uoff_t offset
, hed_cursor_t
*curs
)
321 get_cursor(file
, offset
, curs
);
325 hed_dup_cursor(const hed_cursor_t
*src
, hed_cursor_t
*dst
)
328 dst
->block
= src
->block
;
330 list_add_tail(&dst
->list
, &src
->block
->refs
);
334 hed_dup2_cursor(const hed_cursor_t
*src
, hed_cursor_t
*dst
)
336 if (hed_is_a_cursor(dst
))
338 hed_dup_cursor(src
, dst
);
341 /* Move blockoff's from @old to @new, adding @off to their block
342 * offsets to keep them at the same position. */
344 update_blockoffs(const struct file_block
*old
, struct file_block
*new,
347 blockoff_t
*blockoff
;
349 BDEBUG("Updating blockoffs from <%p> to <%p>%c%llx\n",
350 old
, new, off
>= 0 ? '+' : '-', off
>= 0 ? off
: -off
);
352 list_for_each_entry(blockoff
, &old
->refs
, list
) {
353 blockoff
->block
= new;
354 blockoff
->off
+= off
;
358 /* Move blockoff's in the range <@start;@end> from @old to @new,
359 * adding @off to their block offset, plus moving the reference list. */
361 move_blockoffs(const struct file_block
*old
, struct file_block
*new,
362 hed_uoff_t start
, hed_uoff_t end
, hed_off_t off
)
364 blockoff_t
*blockoff
, *nextoff
;
366 BDEBUG("Moving blockoffs from <%p>:%llx:%llx to <%p>%c%llx\n",
367 old
, start
, end
, new,
368 off
>= 0 ? '+' : '-', off
>= 0 ? off
: -off
);
370 list_for_each_entry_safe(blockoff
, nextoff
, &old
->refs
, list
)
371 if (blockoff
->off
>= start
&& blockoff
->off
<= end
) {
372 blockoff
->block
= new;
373 blockoff
->off
+= off
;
374 list_move(&blockoff
->list
, &new->refs
);
378 /* Move cursors in the range @block:<@start;@end> to @newpos */
380 move_cursors_abs(const struct file_block
*block
,
381 hed_uoff_t start
, hed_uoff_t end
,
382 const hed_cursor_t
*newpos
)
384 hed_cursor_t
*curs
, *nextcurs
;
386 BDEBUG("Moving blockoffs from <%p>:%llx:%llx to <%p>:%llx\n",
387 block
, start
, end
, newpos
->block
, newpos
->off
);
389 list_for_each_entry_safe(curs
, nextcurs
, &block
->refs
, list
)
390 if (curs
->off
>= start
&& curs
->off
<= end
) {
391 curs
->pos
= newpos
->pos
;
392 curs
->block
= newpos
->block
;
393 curs
->off
= newpos
->off
;
394 list_move(&curs
->list
, &newpos
->block
->refs
);
398 /* Update the positions of blockoffs at and after @start for all
399 * blocks starting at @block */
401 slide_blockoffs(struct hed_file
*file
, const struct file_block
*block
,
402 hed_uoff_t start
, hed_off_t off
)
404 blockoff_t
*blockoff
;
405 const struct hed_block
*nblock
;
407 BDEBUG("Sliding blockoffs >= %llx by %c%llx, starting at <%p>\n",
408 start
, off
>= 0 ? '+' : '-', off
>= 0 ? off
: -off
, block
);
412 list_for_each_entry(blockoff
, &block
->refs
, list
)
413 if (blockoff
->pos
>= start
)
414 blockoff
->pos
+= off
;
415 nblock
= next_block(block
);
416 } while (!hed_block_is_eof(block
));
419 static struct hed_block
*
420 new_block(struct hed_file
*file
, long flags
)
422 struct file_block
*new;
424 if (! (new = swp_zalloc(file_swp(file
), sizeof(struct file_block
))) )
428 init_block_link(new);
429 INIT_LIST_HEAD(&new->refs
);
430 if (flags
& HED_BLOCK_EXCACHE
)
431 INIT_LIST_HEAD(&new->lru
);
433 list_add_tail(&new->lru
, &file
->lru
);
438 static struct hed_block
*
439 new_virt_block(struct hed_file
*file
, hed_uoff_t pos
, hed_uoff_t size
,
442 struct hed_block
*new =
443 new_block(file
, (HED_BLOCK_EXCACHE
|
451 BDEBUG("Spawned new virtual block [%llx] at %llx\n", size
, pos
);
455 static struct hed_block
*
456 new_data_block(struct hed_file
*file
, hed_uoff_t pos
, hed_uoff_t size
,
457 struct hed_block_data
*dataobj
)
459 struct hed_block
*new =
465 new->dataobj
= dataobj
;
468 new->dataoff
= FILE_BLOCK_OFF(pos
);
469 BDEBUG("Spawned new data block [%llx] at %llx\n", size
, pos
);
474 file_free_block(struct hed_file
*file
, struct file_block
*block
)
477 cache_put(file
->cache
, block
->dataobj
);
478 list_del(&block
->lru
);
480 swp_free(file_swp(file
), block
);
484 kill_block_if_empty(struct hed_file
*file
, struct file_block
*block
)
486 if (!hed_block_is_eof(block
) && block
->t
.size
== 0 &&
487 list_empty(&block
->refs
)) {
488 /* No recalculation needed, zero size. */
489 unchain_block(file_blocks(file
), block
);
490 file_free_block(file
, block
);
496 /* This may kill the previous block as well, if it can be merged
497 * with the next one. It will never kill anything which _follows_. */
499 file_kill_block(struct hed_file
*file
, struct file_block
*block
)
501 hed_uoff_t phys_pos
= block
->phys_pos
;
502 struct file_block
*prev
= prev_block(block
);
503 struct file_block
*next
= next_block(block
);
504 struct file_block
*merger
;
505 bool killprev
= false;
507 /* We should never kill a dirty block! */
508 assert(!hed_block_is_dirty(block
));
509 /* We shouldn't get with an empty block here (that might
510 * need special considerations with virtualization). */
511 assert(block
->t
.size
> 0);
513 if (!hed_block_is_eof(block
) &&
514 hed_block_is_inner_virtual(next
) &&
515 phys_pos
+ block
->t
.size
== next
->phys_pos
) {
516 if (!hed_block_is_eof(prev
) &&
517 hed_block_is_inner_virtual(prev
) &&
518 prev
->phys_pos
+ prev
->t
.size
== phys_pos
)
521 merger
->phys_pos
-= block
->t
.size
;
522 update_blockoffs(merger
, merger
, block
->t
.size
);
523 update_blockoffs(block
, merger
, 0);
524 } else if (!hed_block_is_eof(prev
) &&
525 hed_block_is_inner_virtual(prev
) &&
526 prev
->phys_pos
+ prev
->t
.size
== phys_pos
) {
528 update_blockoffs(block
, merger
, merger
->t
.size
);
529 } else if (!hed_block_is_virtual(block
)) {
530 /* Convert physical to virtual */
531 assert(block
->dataobj
);
532 cache_put(file
->cache
, block
->dataobj
);
533 block
->dataobj
= NULL
;
535 list_del_init(&block
->lru
); /* unlink the block from LRU */
536 hed_block_set_excache(block
); /* say it's unlinked */
537 hed_block_set_virtual(block
);
540 /* Already virtual and cannot merge */
543 list_splice(&block
->refs
, &merger
->refs
);
545 /* Messing with block sizes and unchaining is a bit tricky
546 * since unchain_block() can splay(). So we really need
547 * to recalc_block_recursive() right after we update the size.
548 * If this place turns out to be a hot-spot, we can optimize
549 * the tree operations here. */
550 merger
->t
.size
+= block
->t
.size
;
551 recalc_block_recursive(merger
);
553 /* Destroy the block */
554 recalc_unchain_block(file_blocks(file
), block
);
555 file_free_block(file
, block
);
558 file_kill_block(file
, prev
);
561 static struct file_block
*
562 split_block(struct hed_file
*file
, struct hed_block
*block
,
563 hed_uoff_t splitpoint
)
565 struct file_block
*next
;
567 next
= new_block(file
, block
->flags
);
571 if ( (next
->dataobj
= block
->dataobj
) ) {
572 cache_get(next
->dataobj
);
573 next
->dataoff
= block
->dataoff
+ splitpoint
;
575 assert(hed_block_is_virtual(block
));
577 next
->t
.size
= block
->t
.size
- splitpoint
;
578 next
->phys_pos
= block
->phys_pos
;
579 if (!hed_block_is_inserted(block
))
580 next
->phys_pos
+= splitpoint
;
582 block
->t
.size
= splitpoint
;
583 recalc_block_recursive(block
);
584 recalc_chain_block_after(file_blocks(file
), block
, next
);
586 move_blockoffs(block
, next
, splitpoint
, UOFF_MAX
, -splitpoint
);
591 /* Replace a chunk in @block with @newblock */
593 replace_chunk(struct hed_file
*file
, struct hed_block
*block
,
594 hed_uoff_t offset
, struct hed_block
*newblock
)
596 size_t len
= newblock
->t
.size
;
597 hed_uoff_t leadlen
= offset
+ len
;
599 assert(offset
< block
->t
.size
);
600 assert(len
<= block
->t
.size
- offset
);
602 /* Re-create the tail block if necessary */
603 if (hed_block_is_eof(block
) || block
->t
.size
- offset
> len
) {
604 struct file_block
*tail
;
606 tail
= new_block(file
, block
->flags
);
609 tail
->t
.size
= block
->t
.size
- leadlen
;
610 tail
->dataobj
= block
->dataobj
;
611 tail
->dataoff
= block
->dataoff
+ leadlen
;
612 tail
->phys_pos
= block
->phys_pos
+ leadlen
;
614 hed_block_clear_eof(block
);
615 recalc_chain_block_after(file_blocks(file
), block
, tail
);
617 /* Move offsets to the tail */
618 move_blockoffs(block
, tail
, leadlen
, UOFF_MAX
, -leadlen
);
621 /* Move pointers to the new block */
623 move_blockoffs(block
, newblock
, offset
, leadlen
- 1, -offset
);
625 /* Shorten the leading block */
626 block
->t
.size
= offset
;
627 recalc_block_recursive(block
);
629 /* Insert the new block */
630 recalc_chain_block_after(file_blocks(file
), block
, newblock
);
632 /* Kill the leading block if possible */
633 kill_block_if_empty(file
, block
);
638 #ifdef HED_CONFIG_SWAP
641 swp_filename(const char *filename
)
643 size_t fnlen
= strlen(filename
);
647 if (!(swp
= malloc(fnlen
+ 9)) )
649 strcpy(swp
, filename
);
651 file
= strrchr(swp
, '/');
652 file
= file
? file
+ 1 : swp
;
654 strcpy(stpcpy(file
+ 1, filename
+ (file
-swp
)), ".hedswp");
659 newswp_filename(char *swpname
)
664 while (!access(ret
, F_OK
)) {
665 if (ret
== swpname
) {
666 if (! (ret
= strdup(swpname
)) )
668 p
= ret
+ strlen(ret
) - 1;
681 hed_file_remove_swap(struct hed_file
*file
)
683 if (remove(file
->swpname
))
685 if (rename(file
->newswpname
, file
->swpname
))
688 free(file
->newswpname
);
689 file
->newswpname
= file
->swpname
;
693 static inline struct hed_file
*
694 file_swp_init(const char *name
)
696 char *swpname
, *newswpname
;
697 struct swp_file
*swp
;
698 struct hed_file
*file
;
700 swpname
= swp_filename(name
);
703 newswpname
= newswp_filename(swpname
);
706 swp
= swp_init_write(newswpname
);
708 goto fail_free_newname
;
710 assert(sizeof(struct swp_header
) + sizeof(struct hed_file
)
712 file
= swp_private(swp
);
713 memset(file
, 0, sizeof *file
);
716 file
->swpname
= swpname
;
717 file
->newswpname
= newswpname
;
724 if (swpname
!= newswpname
)
731 file_swp_done(struct hed_file
*file
)
733 remove(file
->newswpname
);
734 if (file
->newswpname
!= file
->swpname
)
735 free(file
->newswpname
);
737 swp_done(file_swp(file
));
738 /* file was de-allocated automatically with file->swp */
741 #else /* HED_CONFIG_SWAP */
743 static inline struct hed_file
*
744 file_swp_init(const char *name
)
746 return calloc(1, sizeof(struct hed_file
));
750 file_swp_done(struct hed_file
*file
)
754 #endif /* HED_CONFIG_SWAP */
756 static inline struct stat
*
757 file_stat(struct hed_file
*file
)
763 hed_file_update_size(struct hed_file
*file
)
765 hed_uoff_t oldsize
= file
->phys_size
;
767 if(fstat(file
->fd
, file_stat(file
)) < 0)
770 if (S_ISBLK(file_stat(file
)->st_mode
)) {
771 if (ioctl(file
->fd
, BLKGETSIZE64
, &file
->phys_size
)) {
772 unsigned long size_in_blocks
;
773 if (ioctl(file
->fd
, BLKGETSIZE
, &size_in_blocks
))
775 file
->phys_size
= (hed_uoff_t
)size_in_blocks
<< 9;
777 } else if (S_ISREG(file_stat(file
)->st_mode
)) {
778 file
->phys_size
= file_stat(file
)->st_size
;
779 } else if (S_ISCHR(file_stat(file
)->st_mode
)) {
780 if (lseek(file
->fd
, 0, SEEK_SET
) < 0)
782 file
->phys_size
= (hed_uoff_t
)OFF_MAX
+ 1;
788 file
->size
+= file
->phys_size
- oldsize
;
793 do_file_open(struct hed_file
*file
)
795 file
->fd
= open(file
->name
, O_RDONLY
);
799 fprintf(stderr
, "Warning: File %s does not exist\n",
801 memset(file_stat(file
), 0, sizeof(struct stat
));
803 } else if (hed_file_update_size(file
)) {
812 file_setup_blocks(struct hed_file
*file
)
814 hed_uoff_t phys_size
= file
->phys_size
;
815 struct file_block
*block
;
817 block
= &file
->eof_block
;
818 block
->flags
= HED_BLOCK_EXCACHE
| HED_BLOCK_VIRTUAL
| HED_BLOCK_EOF
;
819 INIT_LIST_HEAD(&block
->lru
);
820 INIT_LIST_HEAD(&block
->refs
);
821 block
->t
.size
= OFF_MAX
- phys_size
+ 1;
822 block
->phys_pos
= phys_size
;
824 init_block_list(file_blocks(file
), block
);
827 block
= new_virt_block(file
, 0, phys_size
, 0);
830 recalc_chain_block(file_blocks(file
), block
);
839 return file_access_init();
843 hed_open(const char *name
)
845 struct hed_file
*file
;
847 if (! (file
= file_swp_init(name
)) )
852 file
->cache
= cache_init(CACHE_LENGTH
, file_swp(file
));
855 INIT_LIST_HEAD(&file
->lru
);
857 if (do_file_open(file
))
860 if (file_setup_blocks(file
))
863 fixup_register(file
);
874 hed_close(struct hed_file
*file
)
878 /* Do not check for errors:
879 * 1. This FD is read-only => no data loss possbile
880 * 2. We're about to exit anyway => no resource leak
885 fixup_deregister(file
);
887 /* No need to free file blocks here, because all data is
888 * allocated either from the cache or from the swap file
889 * and both is going to be destroyed now.
893 cache_done(file
->cache
);
898 /* Adjust blockoff after off gets outside its block */
900 fixup_blockoff_slow(blockoff_t
*blockoff
)
902 struct file_block
*block
= blockoff
->block
;
903 hed_uoff_t off
= blockoff
->off
;
906 if ((hed_off_t
)off
< 0) {
907 block
= prev_block(block
);
908 off
+= block
->t
.size
;
910 off
-= block
->t
.size
;
911 block
= next_block(block
);
913 } while (off
>= block
->t
.size
);
915 blockoff
->block
= block
;
917 list_move(&blockoff
->list
, &block
->refs
);
920 /* Adjust blockoff if off gets outside its block.
921 * This is separate from fixup_blockoff_slow, because it is supposed
922 * to be small enough to be inlined (which is a win, because most of
923 * the time no fixup has to be done, so the fast inlined path is used).
926 fixup_blockoff(blockoff_t
*blockoff
)
928 if (blockoff
->off
>= blockoff
->block
->t
.size
)
929 fixup_blockoff_slow(blockoff
);
933 hed_move_relative(blockoff_t
*blockoff
, hed_off_t num
)
935 hed_off_t newpos
= blockoff
->pos
+ num
;
937 newpos
= num
< 0 ? 0 : OFF_MAX
;
938 num
= newpos
- blockoff
->pos
;
940 blockoff
->pos
= newpos
;
941 blockoff
->off
+= num
;
942 fixup_blockoff(blockoff
);
946 /* Relative move with no checking (and only by a small amount) */
948 move_rel_fast(blockoff_t
*blockoff
, ssize_t num
)
950 blockoff
->off
+= num
;
951 blockoff
->pos
+= num
;
952 fixup_blockoff(blockoff
);
956 alloc_caches(struct hed_file
*file
, struct hed_block_data
**caches
, int n
)
958 struct remap_control rc
;
961 BDEBUG("Allocate %d caches (%d free slots available)\n",
962 n
, file
->cache
->nfree
);
964 assert(n
<= CACHE_LENGTH
);
965 while (file
->cache
->nfree
< n
) {
966 struct file_block
*block
;
968 assert(!list_empty(&file
->lru
));
969 block
= list_entry(file
->lru
.next
, struct file_block
, lru
);
970 BDEBUG("Killing block at physical %llx\n", block
->phys_pos
);
971 file_kill_block(file
, block
);
974 for (i
= 0; i
< n
; ++i
) {
975 caches
[i
] = cache_alloc(file
->cache
);
980 remap_compact(&rc
, file
->cache
, caches
, n
);
981 for (i
= 0; i
< n
; ++i
)
982 remap_add(&rc
, caches
[i
]->data
);
987 free_caches(struct hed_file
*file
, struct hed_block_data
**preload
, int n
)
991 for (i
= 0; i
< n
; ++i
)
993 cache_put(file
->cache
, preload
[i
]);
997 file_load_data(struct hed_file
*file
,
998 struct hed_block_data
**caches
, int n
,
1001 struct hed_block_data
*dataobj
= caches
[0];
1002 void *data
= dataobj
->data
;
1003 ssize_t rsize
, total
, segsize
;
1005 segsize
= n
<< FILE_BLOCK_SHIFT
;
1006 for (total
= 0; total
< segsize
; total
+= rsize
) {
1007 rsize
= pread(file
->fd
, data
+ total
,
1008 segsize
- total
, offset
+ total
);
1012 dataobj
= caches
[total
>> FILE_BLOCK_SHIFT
];
1013 caches
[total
>> FILE_BLOCK_SHIFT
] = NULL
;
1014 data
= dataobj
->data
;
1015 cache_put(file
->cache
, dataobj
);
1016 total
= FILE_BLOCK_ROUND(total
);
1017 rsize
= FILE_BLOCK_SIZE
;
1018 BDEBUG("Error reading block at phys %llx: %s\n",
1019 offset
+ total
, strerror(errno
));
1023 BDEBUG("Loaded data at phys %llx up to %llx\n",
1024 offset
, offset
+ segsize
);
1028 #ifdef HED_CONFIG_MMAP
1031 file_load_data_mmap(struct hed_file
*file
,
1032 struct hed_block_data
**caches
, int n
,
1039 segsize
= n
<< FILE_BLOCK_SHIFT
;
1040 data
= mmap(NULL
, segsize
,
1041 PROT_READ
| PROT_WRITE
,
1042 MAP_PRIVATE
| (file
->fd
< 0 ? MAP_ANONYMOUS
: 0),
1045 if (data
== MAP_FAILED
) {
1046 BDEBUG("mmap failed at %llx: fail over to traditional read\n",
1049 data
= mmap(NULL
, segsize
,
1050 PROT_READ
| PROT_WRITE
,
1051 MAP_PRIVATE
| MAP_ANONYMOUS
,
1053 if (data
== MAP_FAILED
)
1056 for (i
= 0; i
< n
; ++i
)
1057 caches
[i
]->data
= data
+ (i
<< FILE_BLOCK_SHIFT
);
1058 return file_load_data(file
, caches
, n
, offset
);
1061 for (i
= 0; i
< n
; ++i
)
1062 caches
[i
]->data
= data
+ (i
<< FILE_BLOCK_SHIFT
);
1064 BDEBUG("Loaded data at phys %llx up to %llx\n",
1065 offset
, offset
+ segsize
);
1068 # define file_load_data file_load_data_mmap
1072 /* Find the block with the lowest physical position that intersects
1073 * the loaded segment. The search starts at @block.
1075 static struct hed_block
*
1076 first_load_block(struct hed_tree
*tree
, struct hed_block
*block
,
1077 hed_uoff_t segstart
)
1079 struct hed_block
*prev
= block
;
1082 prev
= prev_block(prev
);
1083 } while (!hed_block_is_eof(prev
) && phys_end(prev
) > segstart
);
1088 load_blocks(struct hed_file
*file
, const blockoff_t
*from
)
1090 hed_uoff_t physpos
, segstart
;
1091 struct hed_block_data
*preload
[FILE_READAHEAD
];
1092 size_t ra_bkw
, ra_fwd
, ra_off
;
1096 segstart
= hed_cursor_phys_pos(from
);
1097 ra_bkw
= FILE_BLOCK_OFF(segstart
);
1098 ra_fwd
= FILE_BLOCK_SIZE
- ra_bkw
;
1100 if (file_ra_forward(file
))
1101 ra_fwd
+= (FILE_READAHEAD
- 1) << FILE_BLOCK_SHIFT
;
1102 else if (file_ra_backward(file
))
1103 ra_bkw
+= (FILE_READAHEAD
- 1) << FILE_BLOCK_SHIFT
;
1105 if (ra_bkw
> segstart
)
1107 if (ra_fwd
> file
->phys_size
- segstart
)
1108 ra_fwd
= file
->phys_size
- segstart
;
1112 pos
.block
= first_load_block(file_blocks(file
), from
->block
, segstart
);
1113 pos
.off
= segstart
>= pos
.block
->phys_pos
1114 ? segstart
- pos
.block
->phys_pos
1117 list_add(&pos
.list
, &pos
.block
->refs
);
1118 nblocks
= ((ra_fwd
- 1) >> FILE_BLOCK_SHIFT
) + 1;
1119 alloc_caches(file
, preload
, nblocks
);
1122 if (file_load_data(file
, preload
, nblocks
, segstart
)) {
1123 free_caches(file
, preload
, nblocks
);
1127 while (physpos
= hed_cursor_phys_pos(&pos
),
1128 ra_off
= physpos
- segstart
,
1130 struct hed_block_data
*dataobj
;
1131 struct hed_block
*newblock
;
1134 if (!hed_block_is_virtual(pos
.block
)) {
1135 pos
.block
= next_block(pos
.block
);
1140 datalen
= FILE_BLOCK_SIZE
- FILE_BLOCK_OFF(physpos
);
1141 if (datalen
> hed_block_size(pos
.block
) - pos
.off
)
1142 datalen
= hed_block_size(pos
.block
) - pos
.off
;
1144 dataobj
= preload
[ra_off
>> FILE_BLOCK_SHIFT
];
1146 ? new_data_block(file
, physpos
, datalen
, dataobj
)
1147 : new_virt_block(file
, physpos
, datalen
,
1150 /* Punch the new block */
1151 BDEBUG("Add %s block at %llx, length %llx\n",
1152 hed_block_is_virtual(newblock
) ? "error" : "physical",
1153 newblock
->phys_pos
, newblock
->t
.size
);
1154 if (replace_chunk(file
, pos
.block
, pos
.off
, newblock
)) {
1155 file_free_block(file
, newblock
);
1156 free_caches(file
, preload
, nblocks
);
1160 pos
.block
= next_block(newblock
);
1164 /* All cache objects now have an extra reference from the
1165 * allocation. Drop it. */
1166 free_caches(file
, preload
, nblocks
);
1172 /* Shorten a block at beginning and enlarge the preceding block.
1174 * Re-allocate at most @len bytes from the beginning of @block to the
1175 * end of the preceding block.
1176 * If @block is virtual, this will effectively devirtualize the range.
1177 * If @block is not virtual, this will change the backing store of
1178 * the bytes in the range.
1179 * Returns: the number of bytes actually moved.
1182 shrink_at_begin(struct hed_file
*file
, struct file_block
*block
,
1183 size_t len
, long state
)
1185 struct file_block
*prev
= prev_block(block
);
1188 /* Basic assumptions */
1189 assert(!(state
& HED_BLOCK_VIRTUAL
));
1191 /* The previous block must exist: */
1192 if (hed_block_is_eof(prev
))
1195 /* The block flags must match the requested @state: */
1196 if ((prev
->flags
& HED_BLOCK_STATEMASK
) != state
)
1199 /* No deletions at end, or similar: */
1200 if (prev
->phys_pos
+ prev
->t
.size
!= block
->phys_pos
)
1203 /* Append less bytes than requested if not all are available */
1204 assert(prev
->t
.size
<= prev
->dataobj
->size
);
1205 maxgrow
= prev
->dataobj
->size
- prev
->dataoff
- prev
->t
.size
;
1211 BDEBUG("Appending 0:%lx to the previous block\n", len
);
1213 /* Move blockoffs away from the to-be-chopped beginning */
1214 move_blockoffs(block
, prev
, 0, len
- 1, prev
->t
.size
);
1216 /* Enlarge the previous block */
1217 prev
->t
.size
+= len
;
1218 recalc_block_recursive(prev
);
1220 /* Shorten the original block */
1221 block
->t
.size
-= len
;
1222 block
->dataoff
+= len
;
1223 block
->phys_pos
+= len
;
1224 recalc_block_recursive(block
);
1228 /* Shorten a block at end and enlarge the following block.
1230 * Re-allocate at most @len bytes from the end of @block to the
1231 * beginning of the following block.
1232 * If @block is virtual, this will effectively devirtualize the range.
1233 * If @block is not virtual, this will change the backing store of
1234 * the bytes in the range.
1235 * Returns: the number of bytes actually moved.
1238 shrink_at_end(struct hed_file
*file
, struct file_block
*block
,
1239 size_t len
, long state
)
1241 struct file_block
*next
= next_block(block
);
1244 /* Basic assumptions */
1245 assert(!(state
& HED_BLOCK_VIRTUAL
));
1247 /* The next block must exist: */
1248 if (hed_block_is_eof(block
))
1251 /* The block flags must match the requested @state: */
1252 if ((next
->flags
& HED_BLOCK_STATEMASK
) != state
)
1255 /* No deletions at end, or similar: */
1256 if (block
->phys_pos
+ block
->t
.size
!= next
->phys_pos
)
1259 /* Prepend less bytes than requested if not all are available */
1260 if (len
> next
->dataoff
)
1261 len
= next
->dataoff
;
1264 off
= block
->t
.size
- len
;
1266 BDEBUG("Prepending %llx:%lx to the next block\n", off
, len
);
1268 /* Shift blockoffs in the new physical block */
1269 update_blockoffs(next
, next
, len
);
1271 /* Move blockoffs away from the to-be-chopped end */
1272 move_blockoffs(block
, next
, off
, UOFF_MAX
, -off
);
1274 /* Enlarge the next block */
1275 next
->dataoff
-= len
;
1276 next
->phys_pos
-= len
;
1277 next
->t
.size
+= len
;
1278 recalc_block_recursive(next
);
1280 /* Shorten the original block */
1281 block
->t
.size
-= len
;
1282 recalc_block_recursive(block
);
1286 /* Search for an existing data object within the same physical block
1287 * as @curs, and having the given @state flags.
1289 static struct hed_block_data
*
1290 search_data(struct hed_file
*file
, const hed_cursor_t
*curs
, long state
)
1292 struct file_block
*block
;
1295 physpos
= FILE_BLOCK_ROUND(curs
->block
->phys_pos
+ curs
->off
);
1296 BDEBUG("Search for already loaded data at %llx starting at %llx...",
1297 physpos
, curs
->block
->phys_pos
);
1299 /* Search backwards */
1300 block
= curs
->block
;
1301 while (!hed_block_is_eof(block
= prev_block(block
))) {
1302 if (block
->phys_pos
< physpos
)
1304 if ((block
->flags
& HED_BLOCK_STATEMASK
) == state
) {
1305 BDEBUG(" found at %llx\n", block
->phys_pos
);
1306 assert(block
->dataobj
);
1307 return block
->dataobj
;
1311 /* Search forwards */
1312 block
= curs
->block
;
1313 while (!hed_block_is_eof(block
)) {
1314 block
= next_block(block
);
1315 if (block
->phys_pos
>= physpos
+ FILE_BLOCK_SIZE
)
1317 if ((block
->flags
& HED_BLOCK_STATEMASK
) == state
) {
1318 BDEBUG(" found at %llx\n", block
->phys_pos
);
1319 assert(block
->dataobj
);
1320 return block
->dataobj
;
1324 BDEBUG(" not found\n");
1329 reuse_loaded_data(struct hed_file
*file
, const blockoff_t
*blockoff
,
1330 struct hed_block_data
*data
)
1332 struct file_block
*physblock
;
1333 struct file_block
*block
= blockoff
->block
;
1334 hed_uoff_t block_offset
= blockoff
->off
;
1335 hed_uoff_t physpos
= block
->phys_pos
+ block_offset
;
1336 size_t part
= FILE_BLOCK_OFF(physpos
);
1338 FILE_BLOCK_SIZE
- part
<= block
->t
.size
- block_offset
1339 ? FILE_BLOCK_SIZE
- part
1340 : block
->t
.size
- block_offset
;
1342 if (part
> block_offset
)
1343 part
= block_offset
;
1346 block_offset
-= part
;
1348 if (! (physblock
= new_data_block(file
, physpos
, len
, data
)) )
1351 BDEBUG("Add physical block at %llx, length %llx\n",
1352 physblock
->phys_pos
, physblock
->t
.size
);
1353 if (replace_chunk(file
, block
, block_offset
, physblock
)) {
1354 file_free_block(file
, physblock
);
1362 /* Replace a part of a virtual block with content loaded
1363 * from disk. The amount of data loaded from the disk depends
1364 * on various factors with the goal to choose the most efficient
1365 * ratio. The only guarantee is that the byte at @blockoff will
1366 * be in a non-virtual block when this function returns 0.
1369 devirtualize_clean(struct hed_file
*file
, const blockoff_t
*blockoff
)
1371 struct file_block
*block
= blockoff
->block
;
1372 hed_uoff_t block_offset
= blockoff
->off
;
1373 hed_uoff_t remain
= block
->t
.size
- block_offset
;
1374 struct hed_block_data
*data
;
1376 BDEBUG("punch a clean hole at %llx into %llx:%llx\n", block_offset
,
1377 block_offset(block
), block
->t
.size
);
1378 assert(hed_block_is_virtual(block
));
1380 /* Check if we can combine with a neighbouring block */
1381 if (shrink_at_begin(file
, block
, SIZE_MAX
, 0) > block_offset
||
1382 shrink_at_end(file
, block
, SIZE_MAX
, 0) >= remain
) {
1383 kill_block_if_empty(file
, block
);
1388 /* Check if the block is already loaded elsewhere */
1389 data
= search_data(file
, blockoff
, 0);
1391 ? reuse_loaded_data(file
, blockoff
, data
)
1392 : load_blocks(file
, blockoff
);
1395 /* Replace at most @len bytes of a virtual block with a newly
1396 * allocated out-of-cache block. The block is marked dirty
1397 * and its data is left uninitialized.
1398 * If the block at @blockoff is not virtual, make it dirty.
1399 * Note that this function may devirtualize less than @len bytes.
1400 * In the worst case only 1 byte at @blockoff will be available.
1403 prepare_modify(struct hed_file
*file
, blockoff_t
*blockoff
, size_t len
)
1405 struct file_block
*block
= blockoff
->block
;
1406 hed_uoff_t block_offset
= blockoff
->off
;
1407 hed_uoff_t remain
= block
->t
.size
- block_offset
;
1408 struct file_block
*newblock
;
1410 if (hed_block_is_dirty(block
))
1416 BDEBUG("punch a dirty hole at %llx:%lx into %llx:%llx\n",
1418 block_offset(block
), block
->t
.size
);
1420 /* Check if we can combine with a neighbouring block */
1421 if ((block_offset
== 0 &&
1422 shrink_at_begin(file
, block
, len
, HED_BLOCK_DIRTY
)) ||
1424 shrink_at_end(file
, block
, len
, HED_BLOCK_DIRTY
) >= len
)) {
1425 kill_block_if_empty(file
, block
);
1430 /* Initialize a new block */
1431 newblock
= new_block(file
, HED_BLOCK_EXCACHE
| HED_BLOCK_DIRTY
);
1436 if ( (newblock
->dataobj
= search_data(file
, blockoff
,
1438 cache_get(newblock
->dataobj
);
1439 else if (! (newblock
->dataobj
= block_data_new(file
->cache
,
1443 newblock
->phys_pos
= block
->phys_pos
+ block_offset
;
1444 newblock
->dataoff
= FILE_BLOCK_OFF(newblock
->phys_pos
);
1445 if (len
> FILE_BLOCK_SIZE
- newblock
->dataoff
)
1446 len
= FILE_BLOCK_SIZE
- newblock
->dataoff
;
1447 newblock
->t
.size
= len
;
1449 if (replace_chunk(file
, block
, block_offset
, newblock
))
1456 file_free_block(file
, newblock
);
1461 /* Ensure that blockoff points to an up-to-date non-virtual block.
1462 * Load the data from disk if necessary, return 0 on success. */
1464 hed_prepare_read(struct hed_file
*file
, const hed_cursor_t
*curs
, size_t len
)
1466 struct file_block
*block
= curs
->block
;
1467 if (hed_block_is_inner_virtual(block
) &&
1468 devirtualize_clean(file
, curs
) < 0)
1471 return hed_cursor_chunk_len(curs
, len
);
1474 /* Move the block pointer to the next block */
1475 static struct hed_block
*
1476 cursor_next_block(struct hed_file
*file
, hed_cursor_t
*curs
)
1478 struct hed_block
*block
=
1479 next_nonzero_block(file_blocks(file
), curs
->block
);
1482 curs
->block
= block
;
1484 list_move(&curs
->list
, &block
->refs
);
1489 /* This is an optimized way of doing:
1491 * hed_move_relative(blockoff, blockoff->block->t.size);
1493 * for the case when blockoff->off == 0.
1495 static struct hed_block
*
1496 move_to_next(struct hed_file
*file
, hed_cursor_t
*curs
)
1498 curs
->pos
+= hed_block_size(curs
->block
);
1499 return cursor_next_block(file
, curs
);
1502 /* Copy in @count bytes from @pos.
1503 * Returns the number of bytes that were not read (i.e. zero on success).
1504 * The @pos blockoff is moved by the amount of data read.
1505 * CAUTION: If you read up to MAX_BLOCKOFF, then @pos points one byte
1506 * beyond the EOF block upon return.
1509 copy_in(struct hed_file
*file
, void *buf
, size_t count
, blockoff_t
*pos
)
1514 while (count
&& (cpylen
= hed_prepare_read(file
, pos
, count
))) {
1515 if (hed_block_is_virtual(pos
->block
))
1516 memset(buf
, 0, cpylen
);
1518 memcpy(buf
, hed_cursor_data(pos
), cpylen
);
1522 if ( (pos
->off
+= cpylen
) >= hed_block_size(pos
->block
) )
1523 if (!cursor_next_block(file
, pos
))
1531 hed_file_cpin(struct hed_file
*file
, void *buf
, size_t count
,
1532 const hed_cursor_t
*pos
)
1537 hed_dup_cursor(pos
, &mypos
);
1538 ret
= copy_in(file
, buf
, count
, &mypos
);
1543 /* Set the modified flag */
1545 set_modified(struct hed_file
*file
)
1547 file
->modified
= true;
1550 /* Clear the modified flag */
1552 clear_modified(struct hed_file
*file
)
1554 file
->modified
= false;
1558 hed_file_set_byte(struct hed_file
*file
, blockoff_t
*blockoff
,
1561 hed_uoff_t offset
= blockoff
->pos
;
1563 if (prepare_modify(file
, blockoff
, 1))
1567 if (offset
>= file
->size
)
1568 file
->size
= offset
+ 1;
1570 hed_block_data(blockoff
->block
)[blockoff
->off
] = byte
;
1575 hed_file_set_block(struct hed_file
*file
, blockoff_t
*blockoff
,
1576 unsigned char *buf
, size_t len
)
1581 if (prepare_modify(file
, blockoff
, len
))
1585 span
= hed_cursor_chunk_len(blockoff
, len
);
1586 memcpy(hed_cursor_data(blockoff
), buf
, span
);
1589 move_rel_fast(blockoff
, span
);
1591 if (blockoff
->pos
> file
->size
)
1592 file
->size
= blockoff
->pos
;
1598 hed_file_set_bytes(struct hed_file
*file
, blockoff_t
*blockoff
,
1599 unsigned char byte
, hed_uoff_t rep
)
1604 if (prepare_modify(file
, blockoff
, rep
))
1608 span
= hed_cursor_chunk_len(blockoff
, rep
);
1609 memset(hed_cursor_data(blockoff
), byte
, span
);
1611 move_rel_fast(blockoff
, span
);
1613 if (blockoff
->pos
> file
->size
)
1614 file
->size
= blockoff
->pos
;
1620 file_erase_continuous(struct hed_file
*file
, blockoff_t
*blockoff
, size_t len
)
1622 struct file_block
*block
= blockoff
->block
;
1623 hed_uoff_t block_offset
= blockoff
->off
;
1625 /* Find the new position */
1626 hed_move_relative(blockoff
, len
);
1628 /* Move all other cursors in the erased range to the new position */
1630 move_cursors_abs(block
, block_offset
,
1631 block_offset
+ len
- 1, blockoff
);
1633 if (!block_offset
) {
1634 block
->dataoff
+= len
;
1635 if (!hed_block_is_inserted(block
))
1636 block
->phys_pos
+= len
;
1637 } else if (block_offset
+ len
< block
->t
.size
&&
1638 !split_block(file
, block
, block_offset
+ len
))
1641 move_blockoffs(block
, block
, block_offset
, UOFF_MAX
, -(hed_uoff_t
)len
);
1643 block
->t
.size
-= len
;
1644 recalc_block_recursive(block
);
1646 kill_block_if_empty(file
, block
);
1651 hed_file_erase_block(struct hed_file
*file
, blockoff_t
*blockoff
,
1656 struct file_block
*eofblock
;
1658 offset
= blockoff
->pos
;
1659 if (offset
> file_size(file
))
1661 else if (len
> file_size(file
) - offset
)
1662 len
= file_size(file
) - offset
;
1666 size_t span
= hed_cursor_chunk_len(blockoff
, todo
);
1668 if (file_erase_continuous(file
, blockoff
, span
))
1678 eofblock
= last_block(file_blocks(file
));
1679 assert(hed_block_is_virtual(eofblock
));
1680 assert(hed_block_is_eof(eofblock
));
1681 eofblock
->t
.size
+= len
;
1682 recalc_block_recursive(eofblock
);
1684 struct hed_block
*slideblock
= prev_block(blockoff
->block
);
1685 if (hed_block_is_eof(slideblock
))
1686 slideblock
= blockoff
->block
;
1687 slide_blockoffs(file
, slideblock
, blockoff
->pos
, -len
);
1693 hed_file_insert_begin(struct hed_file
*file
, const hed_cursor_t
*curs
,
1694 hed_cursor_t
*curs_ins
)
1696 struct file_block
*block
, *newblock
;
1698 BDEBUG("Starting insert at %llx\n", curs
->pos
);
1700 newblock
= new_block(file
,
1701 HED_BLOCK_EXCACHE
| HED_BLOCK_INSERTED
);
1705 newblock
->phys_pos
= hed_cursor_phys_pos(curs
);
1706 newblock
->dataobj
= block_data_new(file
->cache
, FILE_BLOCK_SIZE
);
1707 if (!newblock
->dataobj
) {
1708 file_free_block(file
, newblock
);
1712 block
= curs
->block
;
1714 if (!split_block(file
, block
, curs
->off
)) {
1715 file_free_block(file
, newblock
);
1719 block
= prev_block(block
);
1721 chain_block_after(file_blocks(file
), block
, newblock
);
1723 curs_ins
->pos
= curs
->pos
;
1724 curs_ins
->off
= newblock
->t
.size
;
1725 curs_ins
->block
= newblock
;
1726 list_add(&curs_ins
->list
, &newblock
->refs
);
1733 hed_file_insert_end(struct hed_file
*file
, blockoff_t
*blockoff_ins
)
1735 struct file_block
*block
= blockoff_ins
->block
;
1737 BDEBUG("End insert at %llx\n", blockoff_ins
->pos
);
1739 blockoff_ins
->block
= NULL
;
1740 list_del(&blockoff_ins
->list
);
1741 if (!kill_block_if_empty(file
, block
))
1742 block_data_shrink(file
->cache
, block
->dataobj
,
1743 block
->dataoff
+ block
->t
.size
);
1749 insert_block(struct hed_file
*file
, blockoff_t
*blockoff
,
1750 unsigned char *buf
, size_t len
)
1752 struct file_block
*block
= blockoff
->block
;
1753 hed_uoff_t offset
= blockoff
->pos
;
1755 assert(file
&& offset
>= 0);
1757 assert(hed_block_is_excache(block
));
1758 hed_block_set_dirty(block
);
1761 memcpy(hed_block_data(block
) + blockoff
->off
, buf
, len
);
1762 block
->t
.size
+= len
;
1763 recalc_block_recursive(block
);
1764 blockoff
->off
+= len
;
1765 blockoff
->pos
+= len
;
1767 if (blockoff
->pos
> file
->size
)
1768 file
->size
= blockoff
->pos
;
1772 slide_blockoffs(file
, next_block(block
), offset
, len
);
1776 hed_file_insert_block(struct hed_file
*file
, blockoff_t
*blockoff
,
1777 unsigned char *buf
, size_t len
)
1780 struct file_block
*block
= blockoff
->block
;
1781 size_t remain
= block
->dataobj
->size
- blockoff
->off
;
1784 list_del(&blockoff
->list
);
1785 blockoff
->block
= next_block(block
);
1788 if (!hed_file_insert_begin(file
, blockoff
, blockoff
))
1791 blockoff
->block
= block
;
1792 blockoff
->off
= block
->t
.size
;
1793 list_add(&blockoff
->list
, &block
->refs
);
1800 BDEBUG("Append %ld bytes to the insert block\n",
1802 insert_block(file
, blockoff
, buf
, remain
);
1810 hed_file_insert_byte(struct hed_file
*file
, blockoff_t
*blockoff
,
1813 return hed_file_insert_block(file
, blockoff
, &byte
, 1);
1817 hed_file_insert_once(struct hed_file
*file
, blockoff_t
*blockoff
,
1818 unsigned char *buf
, size_t len
)
1822 if (!hed_file_insert_begin(file
, blockoff
, &insert
)) {
1823 len
= hed_file_insert_block(file
, &insert
, buf
, len
);
1824 hed_file_insert_end(file
, &insert
);
1829 struct commit_control
{
1830 struct hed_file
*file
;
1831 int wfd
; /* file descriptor for writing */
1832 int needwrite
; /* non-zero if write is needed */
1833 blockoff_t begoff
, endoff
;
1835 void *partbuf
; /* allocated 3*FILE_BLOCK_SIZE */
1836 void *partial
; /* pointer into partbuf */
1839 /* Get the logical<->physical shift value after the specified block.
1840 * It is essential to get the value _AFTER_ the block, because the
1841 * shift value is used to decide how the current block will be handled.
1844 get_shift(const blockoff_t
*blockoff
)
1846 struct file_block
*block
= blockoff
->block
;
1847 size_t curshift
= hed_block_is_inserted(block
) ? block
->t
.size
: 0;
1849 blockoff
->pos
- blockoff
->off
- block
->phys_pos
;
1853 switch_partial(struct commit_control
*cc
)
1855 cc
->partial
+= FILE_BLOCK_SIZE
;
1856 if (cc
->partial
>= cc
->partbuf
+ 3*FILE_BLOCK_SIZE
)
1857 cc
->partial
= cc
->partbuf
;
1860 /* Write @writelen bytes from the partial buffer at @cc->begoff. */
1862 commit_block(struct commit_control
*cc
, size_t len
)
1867 BDEBUG(" -> write %lx bytes at %llx\n",
1868 (unsigned long)len
, cc
->begoff
.pos
- len
);
1869 written
= pwrite(cc
->wfd
, cc
->partial
, len
, cc
->begoff
.pos
- len
);
1871 /* TODO: keep data in a new list of dirty blocks */
1877 commit_partial(struct commit_control
*cc
)
1879 size_t partoff
, remain
, left
;
1882 partoff
= FILE_BLOCK_OFF(cc
->begoff
.pos
);
1883 remain
= FILE_BLOCK_SIZE
- partoff
;
1884 if (remain
> cc
->endoff
.pos
- cc
->begoff
.pos
)
1885 remain
= cc
->endoff
.pos
- cc
->begoff
.pos
;
1886 if ((writelen
= partoff
+ remain
) == 0)
1889 BDEBUG("Fill partial %llx-%llx\n",
1890 cc
->begoff
.pos
, cc
->begoff
.pos
+ remain
);
1892 left
= copy_in(cc
->file
, cc
->partial
+ partoff
, remain
, &cc
->begoff
);
1894 hed_move_relative(&cc
->begoff
, left
);
1898 if (FILE_BLOCK_OFF(cc
->begoff
.pos
) &&
1899 !hed_block_is_eof(cc
->begoff
.block
))
1902 return commit_block(cc
, writelen
);
1906 * Beware, cc->begoff is undefined upon return!
1909 commit_forwards(struct commit_control
*cc
)
1911 hed_uoff_t endpos
= cc
->endoff
.pos
;
1914 BDEBUG("Writing forwards %llx-%llx\n",
1915 cc
->begoff
.pos
, cc
->endoff
.pos
);
1920 while (cc
->begoff
.pos
< endpos
)
1921 ret
|= commit_partial(cc
);
1927 * Beware, cc->begoff is undefined upon return!
1930 commit_backwards(struct commit_control
*cc
)
1932 void *retpartial
= cc
->partial
;
1933 hed_uoff_t begpos
= cc
->begoff
.pos
;
1934 hed_uoff_t blkpos
; /* start of current partial block */
1937 BDEBUG("Writing backwards %llx-%llx\n",
1938 cc
->begoff
.pos
, cc
->endoff
.pos
);
1943 blkpos
= FILE_BLOCK_ROUND(cc
->endoff
.pos
);
1944 if (blkpos
<= begpos
)
1947 /* Handle the trailing partial block */
1948 hed_update_cursor(cc
->file
, blkpos
, &cc
->begoff
);
1950 ret
|= commit_partial(cc
);
1951 retpartial
= cc
->partial
;
1953 /* Handle the middle part */
1955 while ( (blkpos
-= FILE_BLOCK_SIZE
) > begpos
) {
1956 hed_update_cursor(cc
->file
, blkpos
, &cc
->begoff
);
1957 ret
|= commit_partial(cc
);
1959 switch_partial(cc
); /* wrap around */
1962 /* Handle the first block (partiall or not) */
1963 hed_update_cursor(cc
->file
, begpos
, &cc
->begoff
);
1964 ret
|= commit_partial(cc
);
1966 cc
->partial
= retpartial
;
1970 /* Handle the partial block before a skipped one. */
1972 begin_skip(struct commit_control
*cc
)
1974 size_t minsize
= FILE_BLOCK_SIZE
- FILE_BLOCK_OFF(cc
->endoff
.pos
);
1978 /* Check if at least one complete physical block can be skipped */
1979 if (cc
->endoff
.block
->t
.size
< minsize
)
1982 /* Write out the partially dirty block */
1983 remain
= FILE_BLOCK_OFF(minsize
);
1984 hed_move_relative(&cc
->endoff
, remain
);
1986 ret
|= commit_forwards(cc
);
1988 ret
|= commit_backwards(cc
);
1989 hed_move_relative(&cc
->endoff
, -(hed_off_t
)remain
);
1990 hed_dup2_cursor(&cc
->endoff
, &cc
->begoff
);
1996 /* Handle the last partially skipped physical block. */
1998 end_skip(struct commit_control
*cc
)
2003 /* Find the beginning of the physical block */
2004 hed_dup2_cursor(&cc
->endoff
, &cc
->begoff
);
2005 partlen
= FILE_BLOCK_OFF(cc
->begoff
.pos
);
2006 hed_move_relative(&cc
->begoff
, -(hed_off_t
)partlen
);
2008 /* Read the partial data before this block */
2009 if (hed_file_cpin(cc
->file
, cc
->partial
, partlen
, &cc
->begoff
))
2017 undirty_blocks(struct hed_file
*file
)
2019 struct file_block
*block
, *next
;
2022 BDEBUG("Undirtying blocks:\n");
2025 next
= first_block(file_blocks(file
));
2026 while (!hed_block_is_eof(next
)) {
2028 next
= next_block(block
);
2030 if (kill_block_if_empty(file
, block
))
2033 if (!hed_block_is_virtual(block
)) {
2034 cache_put(file
->cache
, block
->dataobj
);
2035 block
->dataobj
= NULL
;
2036 list_del_init(&block
->lru
);
2037 block
->flags
= HED_BLOCK_EXCACHE
| HED_BLOCK_VIRTUAL
;
2040 block
->phys_pos
= pos
;
2041 pos
+= block
->t
.size
;
2044 block
= first_block(file_blocks(file
));
2045 while (!hed_block_is_eof(block
)) {
2046 next
= next_block(block
);
2047 file_kill_block(file
, block
);
2051 BDEBUG("After undirtying\n");
2056 commit_init(struct commit_control
*cc
, struct hed_file
*file
)
2060 cc
->partbuf
= malloc(3*FILE_BLOCK_SIZE
);
2064 cc
->wfd
= open(file
->name
,
2065 O_RDWR
| (file
->fd
< 0 ? O_CREAT
: 0), 0666);
2070 (file
->fd
= open(file
->name
, O_RDONLY
)) < 0)
2084 hed_file_commit(struct hed_file
*file
)
2086 struct commit_control cc
;
2089 if (commit_init(&cc
, file
))
2094 cc
.partial
= cc
.partbuf
;
2095 get_cursor(file
, 0,&cc
.begoff
);
2096 hed_dup_cursor(&cc
.begoff
, &cc
.endoff
);
2097 cc
.shift
= -cc
.begoff
.block
->phys_pos
;
2099 while(!hed_block_is_eof(cc
.endoff
.block
)) {
2100 hed_off_t newshift
= cc
.endoff
.pos
< file
->phys_size
2101 ? get_shift(&cc
.endoff
)
2104 if (cc
.shift
<= 0 && newshift
> 0) {
2105 ret
|= commit_forwards(&cc
);
2106 hed_dup2_cursor(&cc
.endoff
, &cc
.begoff
);
2107 } else if (cc
.shift
> 0 && newshift
<= 0) {
2108 ret
|= commit_backwards(&cc
);
2109 hed_dup2_cursor(&cc
.endoff
, &cc
.begoff
);
2111 cc
.shift
= newshift
;
2113 if (!newshift
&& !hed_block_is_dirty(cc
.endoff
.block
)) {
2115 ret
|= begin_skip(&cc
);
2116 } else if (!cc
.needwrite
)
2117 ret
|= end_skip(&cc
);
2119 if (!move_to_next(file
, &cc
.endoff
))
2122 assert(cc
.endoff
.pos
== file_size(file
));
2124 if (cc
.begoff
.pos
< file_size(file
)) {
2126 ret
|= commit_forwards(&cc
);
2128 ret
|= commit_backwards(&cc
);
2131 put_cursor(&cc
.begoff
);
2132 put_cursor(&cc
.endoff
);
2134 ftruncate(cc
.wfd
, file_size(file
));
2135 file
->phys_size
= file_size(file
);
2137 ret
|= close(cc
.wfd
);
2140 undirty_blocks(file
);
2143 clear_modified(file
);
2148 #ifdef HED_CONFIG_SWAP
2150 hed_file_write_swap(struct hed_file
*file
)
2152 return swp_write(file_swp(file
));
2156 do_read_swap(struct hed_file
*file
, struct swp_file
*swp
, blockoff_t
*pos
)
2158 struct hed_file
*swpfile
= swp_private(swp
);
2159 struct file_block
*cur
, block
;
2160 hed_uoff_t phys_pos
;
2162 if (file_stat(swpfile
)->st_size
!= file_stat(file
)->st_size
||
2163 file_stat(swpfile
)->st_mtime
!= file_stat(file
)->st_mtime
) {
2164 fprintf(stderr
, "stat info mismatch (you modified the file since hed ran on it; refusing to touch it)\n");
2168 BDEBUG("Swap header match\n");
2171 cur
= first_block(file_blocks(swpfile
));
2173 struct hed_block_data dataobj
;
2174 size_t (*mergefn
)(struct hed_file
*, blockoff_t
*,
2175 unsigned char*, size_t);
2179 if (swp_cpin(swp
, &block
, cur
, sizeof(struct file_block
))) {
2180 perror("Cannot read block descriptor");
2183 BDEBUG("BLOCK %p: flags %02lx phys 0x%02llx size 0x%llx\n",
2184 cur
, block
.flags
, (long long)block
.phys_pos
,
2185 (long long)hed_block_size(&block
));
2187 if (block
.phys_pos
- phys_pos
) {
2188 if (hed_file_erase_block(file
, pos
,
2189 block
.phys_pos
- phys_pos
)) {
2190 perror("Cannot erase");
2193 phys_pos
= block
.phys_pos
;
2196 if (!hed_block_is_inserted(&block
))
2197 phys_pos
+= hed_block_size(&block
);
2199 if (!hed_block_is_dirty(&block
)) {
2200 hed_move_relative(pos
, hed_block_size(&block
));
2204 if (swp_cpin(swp
, &dataobj
, block
.dataobj
,
2205 sizeof(struct hed_block_data
))) {
2206 perror("Cannot read data descriptor");
2209 BDEBUG("DATA %p: size 0x%lx\n",
2210 block
.dataobj
, (long)dataobj
.size
);
2212 if (! (data
= malloc(hed_block_size(&block
))) ) {
2213 perror("Cannot allocate data");
2217 if (swp_cpin(swp
, data
, dataobj
.data
+ block
.dataoff
,
2218 hed_block_size(&block
))) {
2219 perror("Cannot read data");
2223 mergefn
= hed_block_is_inserted(&block
)
2224 ? hed_file_insert_once
2225 : hed_file_set_block
;
2226 res
= mergefn(file
, pos
, data
, hed_block_size(&block
));
2229 perror("Cannot merge data");
2232 } while (cur
= next_block(&block
), !hed_block_is_eof(&block
));
2239 hed_file_read_swap(struct hed_file
*file
)
2241 struct swp_file
*swp
;
2245 if (! (swp
= swp_init_read(file
->swpname
)) )
2248 get_cursor(file
, 0, &pos
);
2249 ret
= do_read_swap(file
, swp
, &pos
);
2256 #endif /* HED_CONFIG_SWAP */
2258 struct ffb_hookdata
{
2259 struct hed_file
*file
;
2261 hed_expr_reg_cb base_ecb
;
2262 void *base_ecb_data
;
2266 eval_reg_cb(void *hookdata
, char reg
, hed_off_t ofs
,
2267 unsigned char *scramble
, size_t len
)
2269 struct ffb_hookdata
*data
= hookdata
;
2272 long ret
= HED_AEF_DYNAMIC
;
2274 hed_dup_cursor(data
->pos
, &pos
);
2275 hed_move_relative(&pos
, ofs
);
2276 if (copy_in(data
->file
, scramble
, len
, &pos
))
2277 ret
= HED_AEF_ERROR
;
2282 return data
->base_ecb(data
->base_ecb_data
, reg
, ofs
, scramble
, len
);
2286 reverse(unsigned char *p
, size_t len
)
2288 unsigned char *q
= p
+ len
;
2290 unsigned char x
= *p
;
2297 compute_badchar(ssize_t
*badchar
, const unsigned char *s
, ssize_t len
)
2301 badchar
[*s
++] = i
++;
2305 compute_sfx(ssize_t
*sfx
, const unsigned char *s
, ssize_t len
)
2311 for (i
= len
- 2; i
>= 0; --i
) {
2312 if (i
> g
&& sfx
[i
+ len
- 1 - f
] < i
- g
)
2313 sfx
[i
] = sfx
[i
+ len
- 1 - f
];
2318 while (g
>= 0 && s
[g
] == s
[g
+ len
- 1 - f
])
2326 compute_goodsfx(ssize_t
*goodsfx
, const unsigned char *s
, ssize_t len
)
2328 ssize_t i
, j
, *sfx
= goodsfx
+ len
;
2330 compute_sfx(sfx
, s
, len
);
2332 for (i
= 0; i
< len
; ++i
)
2335 for (i
= len
- 1; i
>= 0; --i
)
2336 if (sfx
[i
] == i
+ 1)
2337 for (; j
< len
- 1 - i
; ++j
)
2338 if (goodsfx
[j
] == len
)
2339 goodsfx
[j
] = len
- 1 - i
;
2340 for (i
= 0; i
<= len
- 2; ++i
)
2341 goodsfx
[len
- 1 - sfx
[i
]] = len
- 1 - i
;
2344 /* Search for a constant byte string using the Boyer-Moore algorithm. */
2345 static inline unsigned char*
2346 bm_find(unsigned char *buf
, size_t buflen
, unsigned char *needle
,
2347 size_t maxidx
, ssize_t
*badchar
, ssize_t
*goodsfx
)
2349 while (buflen
> maxidx
) {
2354 for (p
= buf
+ maxidx
, i
= maxidx
; p
>= buf
; --p
, --i
)
2355 if (needle
[i
] != *p
)
2360 shift
= i
+ 1 - badchar
[*p
];
2361 if (shift
< goodsfx
[i
])
2370 /* Search for a constant byte string backwards. */
2371 static inline unsigned char*
2372 bm_find_rev(unsigned char *buf
, size_t buflen
, unsigned char *needle
,
2373 size_t maxidx
, ssize_t
*badchar
, ssize_t
*goodsfx
)
2376 while (buflen
> maxidx
) {
2381 for (p
= buf
, i
= maxidx
; p
<= buf
+ maxidx
; ++p
, --i
)
2382 if (needle
[i
] != *p
)
2384 if (p
> buf
+ maxidx
)
2387 shift
= i
+ 1 - badchar
[*p
];
2388 if (shift
< goodsfx
[i
])
2397 /* Search for a constant byte string in @buf.
2398 * If @buflen is negative, search backwards, otherwise search forwards.
2400 static inline unsigned char*
2401 find_bytestr_buf(unsigned char *buf
, ssize_t buflen
,
2402 unsigned char *needle
, ssize_t maxidx
,
2403 ssize_t
*badchar
, ssize_t
*goodsfx
)
2408 return memrchr(buf
- buflen
+ 1, *needle
, buflen
);
2409 return bm_find_rev(buf
, buflen
, needle
, maxidx
,
2413 return memchr(buf
, *needle
, buflen
);
2414 return bm_find(buf
, buflen
, needle
, maxidx
,
2419 /* Search for a constant byte string using the Boyer-Moore algorithm. */
2421 find_bytestr(struct hed_file
*file
, blockoff_t
*from
, int dir
,
2422 unsigned char *needle
, ssize_t len
)
2425 ssize_t
*badchar
, *goodsfx
;
2426 unsigned char *readbuf
;
2431 dynalloc
= calloc(sizeof(ssize_t
) * (256 + 2*len
)
2434 return HED_FINDOFF_ERROR
;
2436 goodsfx
= badchar
+ 256;
2437 readbuf
= dynalloc
+ sizeof(ssize_t
) * (256 + 2*len
);
2440 reverse(needle
, len
);
2441 compute_badchar(badchar
, needle
, len
);
2442 compute_goodsfx(goodsfx
, needle
, len
);
2445 badchar
= goodsfx
= NULL
;
2449 --len
; /* simplify offset computing */
2457 ret
= HED_FINDOFF_NO_MATCH
;
2458 while (from
->pos
>= 0) {
2461 fixup_blockoff(from
);
2462 if (hed_block_is_eof(from
->block
))
2465 remain
= hed_prepare_read(file
, from
, SSIZE_MAX
);
2467 ret
= HED_FINDOFF_ERROR
;
2471 remain
= -(from
->off
+ 1);
2473 if (!hed_block_is_bad(from
->block
)) {
2474 unsigned char *p
, *q
;
2476 if ((dir
>= 0 && remain
> slen
) ||
2477 (dir
< 0 && remain
< slen
)) {
2478 assert(!hed_block_is_virtual(from
->block
));
2479 assert(from
->block
->dataobj
);
2480 p
= from
->block
->dataobj
->data
+ from
->off
;
2481 from
->off
+= remain
;
2482 from
->pos
+= remain
;
2483 } else if (dir
>= 0) {
2485 if (copy_in(file
, readbuf
, remain
, from
)) {
2486 ret
= HED_FINDOFF_ERROR
;
2492 from
->off
+= remain
+ 1;
2493 from
->pos
+= remain
+ 1;
2496 fixup_blockoff_slow(from
);
2497 if (copy_in(file
, readbuf
, -remain
, from
)) {
2498 ret
= HED_FINDOFF_ERROR
;
2501 from
->off
-= -remain
+ 1;
2502 from
->pos
-= -remain
+ 1;
2503 p
= readbuf
+ (-remain
- 1);
2506 q
= find_bytestr_buf(p
, remain
, needle
, len
,
2509 move_rel_fast(from
, q
- p
- remain
);
2516 /* bad blocks cannot match anything */
2517 from
->off
+= remain
;
2518 from
->pos
+= remain
;
2528 find_expr(struct hed_file
*file
, blockoff_t
*from
, int dir
,
2529 struct hed_expr
*expr
, struct ffb_hookdata
*data
)
2531 int len
= hed_expr_len(expr
);
2536 if (len
> file_size(file
))
2537 return HED_FINDOFF_NO_MATCH
;
2538 if ((hed_off_t
)file_size(file
) - from
->pos
- len
< 0)
2539 hed_move_relative(from
,
2540 (hed_off_t
)file_size(file
) - from
->pos
- len
);
2548 buf
= hed_expr_eval(expr
, eval_reg_cb
, NULL
, data
);
2550 return HED_FINDOFF_ERROR
;
2552 hed_dup_cursor(from
, &match
);
2554 for (pos
= 0; pos
< len
; pos
++) {
2556 remain
= hed_prepare_read(file
, &match
,
2559 hed_block_is_bad(match
.block
))
2561 p
= hed_cursor_data(&match
);
2562 cursor_next_block(file
, &match
);
2564 if (*p
++ != buf
[pos
])
2573 return HED_FINDOFF_ERROR
;
2577 if (0 > from
->pos
|| from
->pos
> file_size(file
) - len
)
2579 fixup_blockoff(from
);
2581 if (! (hed_expr_flags(expr
) & HED_AEF_DYNAMIC
) )
2582 return find_bytestr(file
, from
, dir
, buf
, len
);
2585 return HED_FINDOFF_NO_MATCH
;
2589 hed_file_find_expr(struct hed_file
*file
, blockoff_t
*pos
, int dir
,
2590 struct hed_expr
*expr
,
2591 hed_expr_reg_cb expr_cb
, void *expr_cb_data
)
2593 struct ffb_hookdata data
;
2596 assert(dir
== 1 || dir
== -1);
2600 data
.base_ecb
= expr_cb
;
2601 data
.base_ecb_data
= expr_cb_data
;
2603 hed_file_set_readahead(file
,
2604 dir
> 0 ? HED_RA_FORWARD
: HED_RA_BACKWARD
);
2605 res
= find_expr(file
, pos
, dir
, expr
, &data
);
2606 hed_file_set_readahead(file
, HED_RA_NONE
);