-
Notifications
You must be signed in to change notification settings - Fork 1
/
linix.c
146 lines (122 loc) · 3.79 KB
/
linix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* linear index.
*
* Copyright 2023-2024 Regents of the University of Harbin Institute of Technology, Shenzhen
* Computer science and technology, Yanqi Pan <[email protected]>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <[email protected]>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "hunter.h"
int linix_init(struct hk_sb_info *sbi, struct linix *ix, u64 num_slots)
{
ix->num_slots = num_slots;
ix->sbi = sbi;
if (num_slots == 0) {
ix->slots = NULL;
} else {
ix->slots = kvcalloc(ix->num_slots, sizeof(struct linslot), GFP_KERNEL);
}
return 0;
}
int linix_destroy(struct linix *ix)
{
if (ix->slots) {
kvfree(ix->slots);
}
return 0;
}
void *__must_check kvrealloc(void *old_ptr, size_t old_size, size_t new_size, gfp_t mode)
{
void *buf;
buf = kvmalloc(new_size, mode);
if (buf) {
memcpy(buf, old_ptr, ((old_size < new_size) ? old_size : new_size));
kvfree(old_ptr);
}
return buf;
}
/* This should be changed to kvmalloc */
int linix_extend(struct linix *ix)
{
struct linslot *new_slots;
new_slots = kvrealloc(ix->slots, ix->num_slots * IX_SLOT_SZ, 2 * ix->num_slots * IX_SLOT_SZ, GFP_KERNEL);
if (new_slots == NULL) {
return -1;
}
memset(new_slots + ix->num_slots, 0,
ix->num_slots * IX_SLOT_SZ);
ix->num_slots = 2 * ix->num_slots;
ix->slots = new_slots;
return 0;
}
int linix_shrink(struct linix *ix)
{
struct linslot *new_slots;
new_slots = kvrealloc(ix->slots, ix->num_slots * IX_SLOT_SZ,
ix->num_slots / 2 * IX_SLOT_SZ, GFP_KERNEL);
if (new_slots == NULL) {
return -1;
}
ix->num_slots = ix->num_slots / 2;
ix->slots = new_slots;
return 0;
}
/* return the value of index */
u64 linix_get(struct linix *ix, u64 index)
{
u64 blk_addr;
INIT_TIMING(index_time);
HK_START_TIMING(linix_get_t, index_time);
if (index >= ix->num_slots) {
return 0;
}
blk_addr = ix->slots[index].blk_addr;
HK_END_TIMING(linix_get_t, index_time);
return blk_addr;
}
/* Inode Lock must be held before linix insert, and blk_addr */
int linix_insert(struct linix *ix, u64 index, u64 blk_addr, bool extend)
{
struct hk_sb_info *sbi = ix->sbi;
INIT_TIMING(insert_time);
HK_START_TIMING(linix_set_t, insert_time);
if (extend) {
while (index >= ix->num_slots) {
linix_extend(ix);
}
}
if (index >= ix->num_slots) {
return -1;
}
ix->slots[index].blk_addr = TRANS_ADDR_TO_OFS(sbi, blk_addr);
HK_END_TIMING(linix_set_t, insert_time);
return 0;
}
/* last_index is the last valid index determined by user */
int linix_delete(struct linix *ix, u64 index, u64 last_index, bool shrink)
{
struct hk_sb_info *sbi = ix->sbi;
ix->slots[index].blk_addr = 0;
if (shrink && ix->num_slots > HK_LINIX_SLOTS) {
if (last_index + 1 <= ix->num_slots / 2) {
linix_shrink(ix);
}
}
return 0;
}