2017-08-11 20:11:57 +05:30
|
|
|
(library
|
2017-09-14 16:17:23 +05:30
|
|
|
(thin mapping-tree)
|
2017-08-11 20:11:57 +05:30
|
|
|
|
|
|
|
(export mapping-tree-open
|
|
|
|
mapping-tree-lookup
|
|
|
|
mapping-tree-each)
|
|
|
|
|
2017-09-14 16:17:23 +05:30
|
|
|
(import (persistent-data btree)
|
2017-08-11 20:11:57 +05:30
|
|
|
(chezscheme)
|
2017-08-12 23:57:21 +05:30
|
|
|
(srfi s8 receive))
|
2017-08-11 20:11:57 +05:30
|
|
|
|
|
|
|
(define-record-type mapping-tree (fields dev-tree))
|
|
|
|
|
|
|
|
(define (mapping-tree-open dev root)
|
2017-09-08 20:21:33 +05:30
|
|
|
(make-mapping-tree (btree-open le64-vt dev root)))
|
2017-08-11 20:11:57 +05:30
|
|
|
|
2017-08-14 14:35:38 +05:30
|
|
|
;; (values <block> <time>)
|
|
|
|
(define time-mask (- (fxsll 1 24) 1))
|
|
|
|
|
|
|
|
(define (unpack-block-time bt)
|
|
|
|
(values (fxsrl bt 24) (fxlogand bt time-mask)))
|
|
|
|
|
|
|
|
;; FIXME: unpack the block time
|
2017-08-11 20:11:57 +05:30
|
|
|
(define (mapping-tree-lookup mtree dev-id vblock default)
|
|
|
|
(let* ((unique (gensym))
|
|
|
|
(dev-tree (mapping-tree-dev-tree mtree))
|
|
|
|
(root2 (btree-lookup dev-tree dev-id unique)))
|
|
|
|
(if (eq? unique root2)
|
|
|
|
default
|
2017-09-08 20:21:33 +05:30
|
|
|
(btree-lookup (btree-open le64-vt (btree-bcache dev-tree) root2) vblock default))))
|
2017-08-11 20:11:57 +05:30
|
|
|
|
2017-08-12 23:57:21 +05:30
|
|
|
;;; Visits every entry in the mapping tree calling (fn dev-id vblock pblock time).
|
2017-08-11 20:11:57 +05:30
|
|
|
(define (mapping-tree-each mtree fn)
|
|
|
|
(let ((dev-tree (mapping-tree-dev-tree mtree)))
|
|
|
|
|
|
|
|
(define (visit-dev dev-id mapping-root)
|
2017-09-08 20:21:33 +05:30
|
|
|
(btree-each (btree-open le64-vt (btree-bcache dev-tree) mapping-root)
|
2017-09-14 16:17:23 +05:30
|
|
|
(lambda (vblock mapping)
|
|
|
|
(receive (block time) (unpack-block-time mapping)
|
|
|
|
(fn dev-id vblock block time)))))
|
2017-08-11 20:11:57 +05:30
|
|
|
|
|
|
|
(btree-each dev-tree visit-dev))))
|