Line 0
Link Here
|
|
|
1 |
/* |
2 |
* fsys_reiser4.c -- reiser4 filesystem support. |
3 |
* Copyright (C) 2001, 2002, 2003 Hans Reiser. |
4 |
* |
5 |
* GRUB -- GRand Unified Bootloader |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#ifdef FSYS_REISER4 |
23 |
#include "shared.h" |
24 |
#include "filesys.h" |
25 |
|
26 |
#define ENABLE_STAND_ALONE |
27 |
#include <reiser4/libreiser4.h> |
28 |
|
29 |
static reiser4_fs_t *fs = NULL; |
30 |
static aal_device_t *dev = NULL; |
31 |
static reiser4_object_t *object = NULL; |
32 |
|
33 |
/* Read callback of grub specific device. It uses devread() for reading passed |
34 |
@count of device blocks starting from @blk to passed @buff. */ |
35 |
static errno_t grub_dev_read(aal_device_t *device, |
36 |
void *buff, blk_t blk, |
37 |
count_t count) |
38 |
{ |
39 |
unsigned int size; |
40 |
unsigned int factor; |
41 |
unsigned int sector; |
42 |
|
43 |
/* Calculating actual sector and size in bytes to be read from |
44 |
device. */ |
45 |
factor = device->blksize / SECTOR_SIZE; |
46 |
sector = (unsigned int)blk << aal_log2(factor); |
47 |
size = (unsigned int)count * (SECTOR_SIZE * factor); |
48 |
|
49 |
/* Reading from the current device */ |
50 |
if (!devread(sector, 0, size, buff)) |
51 |
return -EIO; |
52 |
|
53 |
return 0; |
54 |
} |
55 |
|
56 |
/* Length callback of grub device */ |
57 |
static count_t grub_dev_len(aal_device_t *device) { |
58 |
unsigned int factor; |
59 |
|
60 |
/* Getting partition length in device blocks */ |
61 |
factor = device->blksize / SECTOR_SIZE; |
62 |
return (part_length >> aal_log2(factor)); |
63 |
} |
64 |
|
65 |
/* |
66 |
Initializing grub device abstraction instance. It will use devread and friends |
67 |
for providing needed functionality. |
68 |
*/ |
69 |
struct aal_device_ops grub_dev_ops = { |
70 |
.read = grub_dev_read, |
71 |
.len = grub_dev_len |
72 |
}; |
73 |
|
74 |
/* Initializes reiser4 */ |
75 |
static int reiser4_init(void) { |
76 |
extern aal_hash_table_t *plugins; |
77 |
|
78 |
plugins = NULL; |
79 |
|
80 |
/* Initializing memory manager */ |
81 |
aal_mem_init((void *)FSYS_BUF, FSYS_BUFLEN); |
82 |
|
83 |
/* Initializing device abstraction on current device GRUB uses. */ |
84 |
if (!(dev = aal_device_open(&grub_dev_ops, NULL, |
85 |
SECTOR_SIZE, 0))) |
86 |
{ |
87 |
return 0; |
88 |
} |
89 |
|
90 |
/* Initializing libreiser4 (plugins, etc) */ |
91 |
return !libreiser4_init(); |
92 |
} |
93 |
|
94 |
#define MEMORY_WATERMARK 8192 |
95 |
|
96 |
/* Memory pressure detect function. */ |
97 |
static int mpressure_detect(reiser4_tree_t *tree) { |
98 |
return (aal_mem_free() <= MEMORY_WATERMARK); |
99 |
} |
100 |
|
101 |
/* Reiser4 mount() routine */ |
102 |
int reiser4_mount(void) { |
103 |
|
104 |
/* Initialize all reiser4 related stuff first */ |
105 |
if (!reiser4_init()) |
106 |
return 0; |
107 |
|
108 |
/* Open filesystem on @dev. */ |
109 |
if (!(fs = reiser4_fs_open(dev, 0))) |
110 |
return 0; |
111 |
|
112 |
fs->tree->mpc_func = mpressure_detect; |
113 |
|
114 |
object = NULL; |
115 |
return 1; |
116 |
} |
117 |
|
118 |
/* Reiser4 read() handler */ |
119 |
int reiser4_read(char *buf, int len) { |
120 |
int64_t read; |
121 |
|
122 |
if (object == NULL) |
123 |
return 0; |
124 |
|
125 |
/* Seet at current position denoted by @filepos */ |
126 |
reiser4_object_seek(object, filepos); |
127 |
|
128 |
/* Reading current file data starting from @filepos */ |
129 |
disk_read_func = disk_read_hook; |
130 |
read = reiser4_object_read(object, buf, len); |
131 |
disk_read_func = NULL; |
132 |
|
133 |
if (read < 0) { |
134 |
errnum = ERR_FSYS_CORRUPT; |
135 |
return 0; |
136 |
} |
137 |
|
138 |
filepos += read; |
139 |
return read; |
140 |
} |
141 |
|
142 |
/* Reiser4 file open() routine */ |
143 |
int reiser4_dir(char *dirname) { |
144 |
char *ch; |
145 |
|
146 |
if (fs == NULL) |
147 |
return 0; |
148 |
|
149 |
if (object != NULL) { |
150 |
reiser4_object_close(object); |
151 |
object = NULL; |
152 |
} |
153 |
|
154 |
/* Cutting out string after first space character */ |
155 |
if ((ch = aal_strchr(dirname, ' '))) |
156 |
*ch = '\0'; |
157 |
|
158 |
/* This function is also called for getting directory list for |
159 |
maintaining the bash-like completion. */ |
160 |
#ifndef STAGE1_5 |
161 |
if (print_possibilities) { |
162 |
char entry[256]; |
163 |
entry_hint_t entry_hint; |
164 |
|
165 |
/* Getting last part of name (jsut after last '/') */ |
166 |
if (*(dirname + aal_strlen(dirname) - 1) != '/') { |
167 |
|
168 |
if (!(ch = aal_strrchr(dirname, '/'))) { |
169 |
errnum = ERR_BAD_FILETYPE; |
170 |
return 0; |
171 |
} |
172 |
|
173 |
aal_strncpy(entry, ch + 1, sizeof(entry)); |
174 |
*(ch + 1) = '\0'; |
175 |
} else { |
176 |
aal_memset(entry, 0, sizeof(entry)); |
177 |
} |
178 |
|
179 |
/* Open obejct by @dirname */ |
180 |
if (!(object = reiser4_object_open(fs->tree, dirname, 1))) { |
181 |
errnum = ERR_FILE_NOT_FOUND; |
182 |
return 0; |
183 |
} |
184 |
|
185 |
/* Checking if it is a directory object */ |
186 |
if (object->entity->plug->id.group != DIR_OBJECT) { |
187 |
|
188 |
/* If not, cutting out last '/' character */ |
189 |
if ((ch = aal_strrchr(dirname, '/'))) |
190 |
*ch = '\0'; |
191 |
|
192 |
/* Close current object */ |
193 |
reiser4_object_close(object); |
194 |
|
195 |
/* Getting out */ |
196 |
return 0; |
197 |
} |
198 |
|
199 |
/* Reading opened directory in order to build completion |
200 |
list. */ |
201 |
while (reiser4_object_readdir(object, &entry_hint) > 0) { |
202 |
if (substring(entry, entry_hint.name) <= 0) { |
203 |
|
204 |
if (print_possibilities > 0) |
205 |
print_possibilities = -print_possibilities; |
206 |
|
207 |
print_a_completion(entry_hint.name); |
208 |
} |
209 |
} |
210 |
} else { |
211 |
#endif |
212 |
/* This is the case when resier4_dir() is called for open the |
213 |
file @dirname, not for building completion list. */ |
214 |
if (!(object = reiser4_object_open(fs->tree, dirname, 1))) { |
215 |
errnum = ERR_FILE_NOT_FOUND; |
216 |
return 0; |
217 |
} |
218 |
|
219 |
if (object->entity->plug->id.group != REG_OBJECT) { |
220 |
errnum = ERR_BAD_FILETYPE; |
221 |
return 0; |
222 |
} |
223 |
|
224 |
/* Initializing GRUB global variables @filepos and @filemax. */ |
225 |
filepos = 0; |
226 |
filemax = reiser4_object_size(object); |
227 |
|
228 |
return 1; |
229 |
#ifndef STAGE1_5 |
230 |
} |
231 |
|
232 |
return 1; |
233 |
#endif |
234 |
|
235 |
errnum = ERR_FILE_NOT_FOUND; |
236 |
return 0; |
237 |
} |
238 |
|
239 |
/* Returns how many sectors may be used for embeding reiser4_stage1_5 in teh |
240 |
case of installing GRUB to partition instead of MBR. */ |
241 |
int reiser4_embed (int *start_sector, int needed_sectors) { |
242 |
*start_sector = 1; |
243 |
return needed_sectors <= ((REISER4_MASTER_OFFSET >> SECTOR_BITS) - 1); |
244 |
} |
245 |
#endif /* FSYS_REISER4 */ |