|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* fsys_reiser4.c -- reiser4 filesystem support. |
| 3 |
* Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
| 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_MINIMAL |
| 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))) |
| 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 |
if (objplug(object)->o.object_ops->seek) { |
| 127 |
plug_call(objplug(object)->o.object_ops, |
| 128 |
seek, object->ent, filepos); |
| 129 |
} |
| 130 |
|
| 131 |
/* Reading current file data starting from @filepos */ |
| 132 |
disk_read_func = disk_read_hook; |
| 133 |
read = objplug(object)->o.object_ops->read ? |
| 134 |
plug_call(objplug(object)->o.object_ops, read, |
| 135 |
object->ent, buf, len) : -EINVAL; |
| 136 |
disk_read_func = NULL; |
| 137 |
|
| 138 |
if (read < 0) { |
| 139 |
errnum = ERR_FSYS_CORRUPT; |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
filepos += read; |
| 144 |
return read; |
| 145 |
} |
| 146 |
|
| 147 |
/* Reiser4 file open() routine */ |
| 148 |
int reiser4_dir(char *dirname) { |
| 149 |
char *ch; |
| 150 |
|
| 151 |
if (fs == NULL) |
| 152 |
return 0; |
| 153 |
|
| 154 |
if (object != NULL) { |
| 155 |
plug_call(objplug(object)->o.object_ops, |
| 156 |
close, object->ent); |
| 157 |
aal_free(object); |
| 158 |
object = NULL; |
| 159 |
} |
| 160 |
|
| 161 |
/* Cutting out string after first space character */ |
| 162 |
if ((ch = aal_strchr(dirname, ' '))) |
| 163 |
*ch = '\0'; |
| 164 |
|
| 165 |
/* This function is also called for getting directory list for |
| 166 |
maintaining the bash-like completion. */ |
| 167 |
#ifndef STAGE1_5 |
| 168 |
if (print_possibilities) { |
| 169 |
char entry[256]; |
| 170 |
entry_hint_t entry_hint; |
| 171 |
|
| 172 |
/* Getting last part of name (jsut after last '/') */ |
| 173 |
if (*(dirname + aal_strlen(dirname) - 1) != '/') { |
| 174 |
|
| 175 |
if (!(ch = aal_strrchr(dirname, '/'))) { |
| 176 |
errnum = ERR_BAD_FILETYPE; |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
aal_strncpy(entry, ch + 1, sizeof(entry)); |
| 181 |
*(ch + 1) = '\0'; |
| 182 |
} else { |
| 183 |
aal_memset(entry, 0, sizeof(entry)); |
| 184 |
} |
| 185 |
|
| 186 |
/* Open obejct by @dirname */ |
| 187 |
if (!(object = reiser4_semantic_open(fs->tree, dirname, |
| 188 |
NULL, 1))) |
| 189 |
{ |
| 190 |
errnum = ERR_FILE_NOT_FOUND; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
/* Checking if it is a directory object */ |
| 195 |
if (object->ent->opset.plug[OPSET_OBJ]->id.group != DIR_OBJECT) |
| 196 |
{ |
| 197 |
/* If not, cutting out last '/' character */ |
| 198 |
if ((ch = aal_strrchr(dirname, '/'))) |
| 199 |
*ch = '\0'; |
| 200 |
|
| 201 |
/* Close current object */ |
| 202 |
plug_call(objplug(object)->o.object_ops, |
| 203 |
close, object->ent); |
| 204 |
aal_free(object); |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
/* Reading the opened directory to build the completion list. */ |
| 209 |
if (objplug(object)->o.object_ops->readdir) { |
| 210 |
while (plug_call(objplug(object)->o.object_ops, readdir, |
| 211 |
object->ent, &entry_hint) > 0) |
| 212 |
{ |
| 213 |
if (substring(entry, entry_hint.name) <= 0) { |
| 214 |
if (print_possibilities > 0) |
| 215 |
print_possibilities = |
| 216 |
-print_possibilities; |
| 217 |
|
| 218 |
print_a_completion(entry_hint.name); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} else { |
| 223 |
#endif |
| 224 |
/* This is the case when resier4_dir() is called for open the |
| 225 |
file @dirname, not for building completion list. */ |
| 226 |
if (!(object = reiser4_semantic_open(fs->tree, dirname, |
| 227 |
NULL, 1))) |
| 228 |
{ |
| 229 |
errnum = ERR_FILE_NOT_FOUND; |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
if (object->ent->opset.plug[OPSET_OBJ]->id.group != REG_OBJECT) |
| 234 |
{ |
| 235 |
errnum = ERR_BAD_FILETYPE; |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
/* Initializing GRUB global variables @filepos and @filemax. */ |
| 240 |
filepos = 0; |
| 241 |
filemax = reiser4_object_size(object); |
| 242 |
|
| 243 |
return 1; |
| 244 |
#ifndef STAGE1_5 |
| 245 |
} |
| 246 |
|
| 247 |
return 1; |
| 248 |
#endif |
| 249 |
|
| 250 |
errnum = ERR_FILE_NOT_FOUND; |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
/* Returns how many sectors may be used for embeding reiser4_stage1_5 in teh |
| 255 |
case of installing GRUB to partition instead of MBR. */ |
| 256 |
int reiser4_embed (int *start_sector, int needed_sectors) { |
| 257 |
*start_sector = 1; |
| 258 |
return needed_sectors <= ((REISER4_MASTER_OFFSET >> SECTOR_BITS) - 1); |
| 259 |
} |
| 260 |
#endif /* FSYS_REISER4 */ |