Coda Distributed File System
fso_cachefile.h
Go to the documentation of this file.
1/* BLURB gpl
2
3 Coda File System
4 Release 8
5
6 Copyright (c) 1987-2021 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU General Public Licence Version 2, as shown in the
11file LICENSE. The technical and financial contributors to Coda are
12listed in the file CREDITS.
13
14 Additional copyrights
15 Copyright (c) 2002-2003 Intel Corporation
16
17#*/
18
19/*
20 *
21 * Specification of the Venus File-System Object (fso) abstraction.
22 *
23 * ToDo:
24 *
25 */
26
27#ifndef _VENUS_FSO_CACHEFILE_H_
28#define _VENUS_FSO_CACHEFILE_H_ 1
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <lwp/lock.h>
35
36#ifdef __cplusplus
37}
38#endif
39
40/* from util */
41#include <bitmap7.h>
42#include <dlist.h>
43
44/* Representation of UFS file in cache directory. */
45/* Currently, CacheFiles and fsobjs are statically bound to each
46 other, one-to-one, by embedding */
47/* a single CacheFile in each fsobj. An fsobj may use its CacheFile
48 in several ways (or not at all). */
49/* We guarantee that these uses are mutually exclusive (in time,
50 per-fsobj), hence the static allocation */
51/* policy works. In the future we may choose not to make the uses
52 mutually exclusive, and will then */
53/* have to implement some sort of dynamic allocation/binding
54 scheme. */
55/* The different uses of CacheFile are: */
56/* 1. Copy of plain file */
57/* 2. Unix-format copy of directory */
58
59#define CACHEFILENAMELEN 16
60
61extern uint64_t CacheChunkBlockSize;
62extern uint64_t CacheChunkBlockSizeBits;
63extern uint64_t CacheChunkBlockSizeMax;
64extern uint64_t CacheChunkBlockBitmapSize;
65
73static inline uint64_t ccblocks_to_bytes(uint64_t ccblocks)
74{
75 return ccblocks << CacheChunkBlockSizeBits;
76}
77
85static inline uint64_t bytes_to_ccblocks(uint64_t bytes)
86{
88}
89
97static inline uint64_t bytes_to_ccblocks_floor(uint64_t bytes)
98{
99 return bytes_to_ccblocks(bytes);
100}
101
109static inline uint64_t bytes_to_ccblocks_ceil(uint64_t bytes)
110{
111 return bytes_to_ccblocks(bytes + CacheChunkBlockSizeMax);
112}
113
121static inline uint64_t align_to_ccblock_ceil(uint64_t bytes)
122{
123 return (bytes + CacheChunkBlockSizeMax) & ~CacheChunkBlockSizeMax;
124}
125
133static inline uint64_t align_to_ccblock_floor(uint64_t bytes)
134{
135 return (bytes & ~CacheChunkBlockSizeMax);
136}
137
145static inline uint64_t ccblock_start(uint64_t b_pos)
146{
147 return bytes_to_ccblocks_floor(b_pos);
148}
149
158static inline uint64_t ccblock_end(uint64_t b_pos, int64_t b_count)
159{
160 return bytes_to_ccblocks_ceil(b_pos + b_count);
161}
162
171static inline uint64_t ccblock_length(uint64_t b_pos, int64_t b_count)
172{
173 return ccblock_end(b_pos, b_count) - ccblock_start(b_pos);
174}
175
184static inline uint64_t pos_align_to_ccblock(uint64_t b_pos)
185{
186 return (b_pos & ~CacheChunkBlockSizeMax);
187}
188
198static inline uint64_t length_align_to_ccblock(uint64_t b_pos, int64_t b_count)
199{
200 return ccblocks_to_bytes(ccblock_length(b_pos, b_count));
201}
202
203#define FS_BLOCKS_SIZE_MAX (4095)
204#define FS_BLOCKS_SIZE_MASK (~FS_BLOCKS_SIZE_MAX)
205#define FS_BLOCKS_ALIGN(size) \
206 ((size + FS_BLOCKS_SIZE_MAX) & FS_BLOCKS_SIZE_MASK)
207
208class CacheChunk : private dlink {
209private:
210 uint64_t start;
211 int64_t len;
212 bool valid;
214public:
221 CacheChunk(uint64_t start, int64_t len)
222 : start(start)
223 , len(len)
224 , valid(true)
225 {
226 }
227
232 : start(0)
233 , len(0)
234 , valid(false)
235 {
236 }
237
243 uint64_t GetStart() { return start; }
244
250 int64_t GetLength() { return len; }
251
257 bool isValid() { return valid; }
258};
259
260class CacheChunkList : private dlist {
261 Lock rd_wr_lock;
262public:
267
272
279 void AddChunk(uint64_t start, int64_t len);
280
289 bool ReverseCheck(uint64_t start, int64_t len);
290
297 void ReverseRemove(uint64_t start, int64_t len);
298
305 void ForEach(void (*foreachcb)(uint64_t start, int64_t len,
306 void *usr_data_cb),
307 void *usr_data = NULL);
308
312 void ReadLock();
313
317 void ReadUnlock();
318
322 void WriteLock();
323
327 void WriteUnlock();
328
334 CacheChunk pop();
335
341 uint Length() { return (uint)this->count(); }
342};
343
345 friend class SegmentedCacheFile;
346 uint64_t length;
347 uint64_t
348 validdata;
349 int refcnt;
350 int numopens;
351 bitmap7 *cached_chunks;
352 int recoverable;
353 Lock rw_lock;
354 bool isPartial;
361 int ValidContainer();
362
366 void UpdateValidData();
367
377 CacheChunk GetNextHole(uint64_t start_b, uint64_t end_b);
378
379protected:
392 static int64_t CopySegment(CacheFile *from, CacheFile *to, uint64_t pos,
393 int64_t count) REQUIRES_TRANSACTION;
394
395public:
403 CacheFile(int i, int recoverable = 1, int partial = 0) REQUIRES_TRANSACTION;
404
408 CacheFile();
409
413 ~CacheFile();
414
421 void Create(int newlength = 0) REQUIRES_TRANSACTION;
422
431 int Open(int flags);
432
440 int Close(int fd);
441
449 FILE *FOpen(const char *mode);
450
458 int FClose(FILE *f);
459
464
469
477 int Copy(CacheFile *destination) REQUIRES_TRANSACTION;
478
487 int Copy(char *destname, int recovering = 0);
488
492 void IncRef() { refcnt++; }
493
499 int DecRef();
500
506 void Stat(struct stat *tstat);
507
514 void Utimes(const struct timeval times[2]);
515
521 void Truncate(uint64_t newlen) REQUIRES_TRANSACTION;
522
528 void SetLength(uint64_t newlen) REQUIRES_TRANSACTION;
529
535 void SetValidData(uint64_t len) REQUIRES_TRANSACTION;
536
543 void SetValidData(uint64_t start, int64_t len) REQUIRES_TRANSACTION;
544
553 CacheChunkList *GetHoles(uint64_t start, int64_t len);
554
563 CacheChunkList *GetValidChunks(uint64_t start, int64_t len);
564
570 char *Name() { return (name); }
571
577 uint64_t Length() { return (length); }
578
584 uint64_t ValidData() { return (validdata); }
585
592 uint64_t ConsecutiveValidData();
593
599 int IsComplete() { return (length == validdata); }
600
606 bool IsPartial() { return isPartial; }
607
613 void SetPartial(bool is_partial) REQUIRES_TRANSACTION;
614
618 void print() { print(stdout); }
619
625 void print(FILE *fp)
626 {
627 fflush(fp);
628 print(fileno(fp));
629 }
630
636 void print(int fdes);
637};
638
643 CacheFile *cf;
645public:
652
657
664
671 int64_t ExtractSegment(uint64_t pos, int64_t count) REQUIRES_TRANSACTION;
672
679 int64_t InjectSegment(uint64_t pos, int64_t count) REQUIRES_TRANSACTION;
680};
681
682#endif /* _VENUS_FSO_CACHEFILE_H_ */
Definition: fso_cachefile.h:260
void ReadLock()
Definition: fso_cachefile.cc:779
CacheChunkList()
Definition: fso_cachefile.cc:625
~CacheChunkList()
Definition: fso_cachefile.cc:630
void ReverseRemove(uint64_t start, int64_t len)
Definition: fso_cachefile.cc:830
void ForEach(void(*foreachcb)(uint64_t start, int64_t len, void *usr_data_cb), void *usr_data=NULL)
Definition: fso_cachefile.cc:858
void WriteLock()
Definition: fso_cachefile.cc:784
void ReadUnlock()
Definition: fso_cachefile.cc:789
CacheChunk pop()
Definition: fso_cachefile.cc:880
bool ReverseCheck(uint64_t start, int64_t len)
Definition: fso_cachefile.cc:799
void WriteUnlock()
Definition: fso_cachefile.cc:794
uint Length()
Definition: fso_cachefile.h:341
void AddChunk(uint64_t start, int64_t len)
Definition: fso_cachefile.cc:771
Definition: fso_cachefile.h:208
CacheChunk(uint64_t start, int64_t len)
Definition: fso_cachefile.h:221
CacheChunk()
Definition: fso_cachefile.h:231
int64_t GetLength()
Definition: fso_cachefile.h:250
uint64_t GetStart()
Definition: fso_cachefile.h:243
bool isValid()
Definition: fso_cachefile.h:257
Definition: fso_cachefile.h:344
void SetValidData(uint64_t len) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:418
int FClose(FILE *f)
Definition: fso_cachefile.cc:520
int Open(int flags)
Definition: fso_cachefile.cc:488
int DecRef()
Definition: fso_cachefile.cc:286
char name[CACHEFILENAMELEN]
Definition: fso_cachefile.h:380
void Utimes(const struct timeval times[2])
Definition: fso_cachefile.cc:306
void print(FILE *fp)
Definition: fso_cachefile.h:625
void SetPartial(bool is_partial) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:121
FILE * FOpen(const char *mode)
Definition: fso_cachefile.cc:505
int Copy(CacheFile *destination) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:223
uint64_t ConsecutiveValidData()
Definition: fso_cachefile.cc:528
uint64_t Length()
Definition: fso_cachefile.h:577
static int64_t CopySegment(CacheFile *from, CacheFile *to, uint64_t pos, int64_t count) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:550
char * Name()
Definition: fso_cachefile.h:570
void Validate() EXCLUDES_TRANSACTION
Definition: fso_cachefile.cc:114
bool IsPartial()
Definition: fso_cachefile.h:606
void SetLength(uint64_t newlen) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:387
CacheChunkList * GetValidChunks(uint64_t start, int64_t len)
Definition: fso_cachefile.cc:725
void IncRef()
Definition: fso_cachefile.h:492
CacheChunkList * GetHoles(uint64_t start, int64_t len)
Definition: fso_cachefile.cc:692
CacheFile()
Definition: fso_cachefile.cc:93
friend class SegmentedCacheFile
Definition: fso_cachefile.h:345
int IsComplete()
Definition: fso_cachefile.h:599
void Truncate(uint64_t newlen) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:312
void Stat(struct stat *tstat)
Definition: fso_cachefile.cc:301
void print()
Definition: fso_cachefile.h:618
void Reset() EXCLUDES_TRANSACTION
Definition: fso_cachefile.cc:149
uint64_t ValidData()
Definition: fso_cachefile.h:584
int Close(int fd)
Definition: fso_cachefile.cc:498
void Create(int newlength=0) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:186
Definition: fso_cachefile.h:642
int64_t ExtractSegment(uint64_t pos, int64_t count) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:925
int64_t InjectSegment(uint64_t pos, int64_t count) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:931
~SegmentedCacheFile()
Definition: fso_cachefile.cc:912
void Associate(CacheFile *cf) REQUIRES_TRANSACTION
Definition: fso_cachefile.cc:918
Definition: bitmap7.h:54
Definition: dlist.h:50
int count()
Definition: dlist.cc:212
#define REQUIRES_TRANSACTION
Definition: coda_tsa.h:107
#define EXCLUDES_TRANSACTION
Definition: coda_tsa.h:108
uint64_t CacheChunkBlockBitmapSize
Definition: fso_cachefile.cc:64
uint64_t CacheChunkBlockSizeBits
Definition: fso_cachefile.cc:63
uint64_t CacheChunkBlockSizeMax
Definition: fso_cachefile.cc:62
uint64_t CacheChunkBlockSize
Definition: fso_cachefile.cc:61
#define CACHEFILENAMELEN
Definition: fso_cachefile.h:59
#define bytes(bits)
Definition: grunt.h:29
stdout
Definition: volusage.py:12
@ f
Definition: rvm_private.h:416
Definition: lock.h:68
#define NULL
Definition: voltypes.h:44