xref: /minix/minix/lib/libvtreefs/extra.c (revision e3b78ef1)
1 /* VTreeFS - extra.c - per-inode storage of arbitrary extra data */
2 
3 #include "inc.h"
4 
5 /*
6  * Right now, we maintain the extra data (if requested) as a separate buffer,
7  * so that we don't have to make the inode structure variable in size.  Later,
8  * if for example the maximum node name length becomes a runtime setting, we
9  * could reconsider this.
10  */
11 static char *extra_ptr = NULL;
12 static size_t extra_size = 0; /* per inode */
13 
14 /*
15  * Initialize memory to store extra data.
16  */
17 int
18 init_extra(unsigned int nr_inodes, size_t inode_extra)
19 {
20 
21 	if (extra_size == 0)
22 		return OK;
23 
24 	if ((extra_ptr = malloc(nr_inodes * inode_extra)) == NULL)
25 		return ENOMEM;
26 
27 	extra_size = inode_extra;
28 
29 	return OK;
30 }
31 
32 /*
33  * Initialize the extra data for the given inode to zero.
34  */
35 void
36 clear_inode_extra(struct inode * node)
37 {
38 
39 	if (extra_size == 0)
40 		return;
41 
42 	memset(&extra_ptr[node->i_num * extra_size], 0, extra_size);
43 }
44 
45 /*
46  * Retrieve a pointer to the extra data for the given inode.
47  */
48 void *
49 get_inode_extra(const struct inode * node)
50 {
51 
52 	if (extra_size == 0)
53 		return NULL;
54 
55 	return (void *)&extra_ptr[node->i_num * extra_size];
56 }
57