1 /* 2 * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2008-2010, Axel Dörfler, axeld@pinc-software.de. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 #include "BFSAddOn.h" 10 #include "InitializeParameterEditor.h" 11 12 #include <new> 13 14 #include <Directory.h> 15 #include <List.h> 16 #include <Path.h> 17 #include <Volume.h> 18 19 #include <DiskDeviceTypes.h> 20 #include <MutablePartition.h> 21 22 #include <AutoDeleter.h> 23 24 #ifdef ASSERT 25 # undef ASSERT 26 #endif 27 28 #include "bfs.h" 29 #include "bfs_control.h" 30 #include "bfs_disk_system.h" 31 32 33 using std::nothrow; 34 35 36 static const uint32 kDiskSystemFlags = 37 0 38 // | B_DISK_SYSTEM_SUPPORTS_CHECKING 39 // | B_DISK_SYSTEM_SUPPORTS_REPAIRING 40 // | B_DISK_SYSTEM_SUPPORTS_RESIZING 41 // | B_DISK_SYSTEM_SUPPORTS_MOVING 42 // | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME 43 // | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS 44 | B_DISK_SYSTEM_SUPPORTS_INITIALIZING 45 | B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME 46 // | B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING 47 // | B_DISK_SYSTEM_SUPPORTS_DEFRAGMENTING_WHILE_MOUNTED 48 | B_DISK_SYSTEM_SUPPORTS_CHECKING_WHILE_MOUNTED 49 | B_DISK_SYSTEM_SUPPORTS_REPAIRING_WHILE_MOUNTED 50 // | B_DISK_SYSTEM_SUPPORTS_RESIZING_WHILE_MOUNTED 51 // | B_DISK_SYSTEM_SUPPORTS_MOVING_WHILE_MOUNTED 52 // | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME_WHILE_MOUNTED 53 // | B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_PARAMETERS_WHILE_MOUNTED 54 ; 55 56 57 // #pragma mark - BFSAddOn 58 59 60 BFSAddOn::BFSAddOn() 61 : BDiskSystemAddOn(kPartitionTypeBFS, kDiskSystemFlags) 62 { 63 } 64 65 66 BFSAddOn::~BFSAddOn() 67 { 68 } 69 70 71 status_t 72 BFSAddOn::CreatePartitionHandle(BMutablePartition* partition, 73 BPartitionHandle** _handle) 74 { 75 BFSPartitionHandle* handle = new(nothrow) BFSPartitionHandle(partition); 76 if (!handle) 77 return B_NO_MEMORY; 78 79 status_t error = handle->Init(); 80 if (error != B_OK) { 81 delete handle; 82 return error; 83 } 84 85 *_handle = handle; 86 return B_OK; 87 } 88 89 90 bool 91 BFSAddOn::CanInitialize(const BMutablePartition* partition) 92 { 93 // TODO: Check partition size. 94 return true; 95 } 96 97 98 status_t 99 BFSAddOn::GetInitializationParameterEditor(const BMutablePartition* partition, 100 BPartitionParameterEditor** editor) 101 { 102 *editor = NULL; 103 104 try { 105 *editor = new InitializeBFSEditor(); 106 } catch (std::bad_alloc) { 107 return B_NO_MEMORY; 108 } 109 return B_OK; 110 } 111 112 113 status_t 114 BFSAddOn::ValidateInitialize(const BMutablePartition* partition, BString* name, 115 const char* parameterString) 116 { 117 if (!CanInitialize(partition) || !name) 118 return B_BAD_VALUE; 119 120 // validate name 121 122 // truncate, if it is too long 123 if (name->Length() >= BFS_DISK_NAME_LENGTH) 124 name->Truncate(BFS_DISK_NAME_LENGTH - 1); 125 126 // replace '/' by '-' 127 name->ReplaceAll('/', '-'); 128 129 // check parameters 130 initialize_parameters parameters; 131 status_t error = parse_initialize_parameters(parameterString, parameters); 132 if (error != B_OK) 133 return error; 134 135 return B_OK; 136 } 137 138 139 status_t 140 BFSAddOn::Initialize(BMutablePartition* partition, const char* name, 141 const char* parameterString, BPartitionHandle** _handle) 142 { 143 if (!CanInitialize(partition) || check_volume_name(name) != B_OK) 144 return B_BAD_VALUE; 145 146 initialize_parameters parameters; 147 status_t error = parse_initialize_parameters(parameterString, parameters); 148 if (error != B_OK) 149 return error; 150 151 // create the handle 152 BFSPartitionHandle* handle = new(nothrow) BFSPartitionHandle(partition); 153 if (!handle) 154 return B_NO_MEMORY; 155 ObjectDeleter<BFSPartitionHandle> handleDeleter(handle); 156 157 // init the partition 158 error = partition->SetContentType(Name()); 159 if (error != B_OK) 160 return error; 161 // TODO: The content type could as well be set by the caller. 162 163 partition->SetContentName(name); 164 partition->SetContentParameters(parameterString); 165 uint32 blockSize = parameters.blockSize; 166 partition->SetBlockSize(blockSize); 167 partition->SetContentSize(partition->Size() / blockSize * blockSize); 168 partition->Changed(B_PARTITION_CHANGED_INITIALIZATION); 169 170 *_handle = handleDeleter.Detach(); 171 172 return B_OK; 173 } 174 175 176 // #pragma mark - BFSPartitionHandle 177 178 179 BFSPartitionHandle::BFSPartitionHandle(BMutablePartition* partition) 180 : BPartitionHandle(partition) 181 { 182 } 183 184 185 BFSPartitionHandle::~BFSPartitionHandle() 186 { 187 } 188 189 190 status_t 191 BFSPartitionHandle::Init() 192 { 193 // TODO: Check parameters. 194 return B_OK; 195 } 196 197 198 uint32 199 BFSPartitionHandle::SupportedOperations(uint32 mask) 200 { 201 return kDiskSystemFlags & mask; 202 } 203 204 205 status_t 206 BFSPartitionHandle::Repair(bool checkOnly) 207 { 208 BVolume volume(Partition()->VolumeID()); 209 BDirectory directory; 210 volume.GetRootDirectory(&directory); 211 BPath path; 212 path.SetTo(&directory, "."); 213 214 int fd = open(path.Path(), O_RDONLY); 215 if (fd < 0) 216 return errno; 217 218 struct check_control result; 219 memset(&result, 0, sizeof(result)); 220 result.magic = BFS_IOCTL_CHECK_MAGIC; 221 result.flags = !checkOnly ? BFS_FIX_BITMAP_ERRORS : 0; 222 if (!checkOnly) { 223 //printf("will fix any severe errors!\n"); 224 result.flags |= BFS_REMOVE_WRONG_TYPES | BFS_REMOVE_INVALID 225 | BFS_FIX_NAME_MISMATCHES; 226 } 227 228 // start checking 229 if (ioctl(fd, BFS_IOCTL_START_CHECKING, &result, sizeof(result)) < 0) 230 return errno; 231 232 off_t attributeDirectories = 0, attributes = 0; 233 off_t files = 0, directories = 0, indices = 0; 234 off_t counter = 0; 235 236 // check all files and report errors 237 while (ioctl(fd, BFS_IOCTL_CHECK_NEXT_NODE, &result, 238 sizeof(result)) == 0) { 239 if (++counter % 50 == 0) 240 printf("%9Ld nodes processed\x1b[1A\n", counter); 241 242 if (result.errors) { 243 printf("%s (inode = %lld)", result.name, result.inode); 244 if (result.errors & BFS_MISSING_BLOCKS) 245 printf(", some blocks weren't allocated"); 246 if (result.errors & BFS_BLOCKS_ALREADY_SET) 247 printf(", has blocks already set"); 248 if (result.errors & BFS_INVALID_BLOCK_RUN) 249 printf(", has invalid block run(s)"); 250 if (result.errors & BFS_COULD_NOT_OPEN) 251 printf(", could not be opened"); 252 if (result.errors & BFS_WRONG_TYPE) 253 printf(", has wrong type"); 254 if (result.errors & BFS_NAMES_DONT_MATCH) 255 printf(", names don't match"); 256 putchar('\n'); 257 } 258 if ((result.mode & (S_INDEX_DIR | 0777)) == S_INDEX_DIR) 259 indices++; 260 else if (result.mode & S_ATTR_DIR) 261 attributeDirectories++; 262 else if (result.mode & S_ATTR) 263 attributes++; 264 else if (S_ISDIR(result.mode)) 265 directories++; 266 else 267 files++; 268 } 269 270 // stop checking 271 if (ioctl(fd, BFS_IOCTL_STOP_CHECKING, &result, sizeof(result)) != 0) { 272 close(fd); 273 return errno; 274 } 275 276 close(fd); 277 278 printf("\t%lld nodes checked,\n\t%lld blocks not allocated," 279 "\n\t%lld blocks already set,\n\t%lld blocks could be freed\n\n", 280 counter, result.stats.missing, result.stats.already_set, 281 result.stats.freed); 282 printf("\tfiles\t\t%lld\n\tdirectories\t%lld\n\tattributes\t%lld\n\tattr. " 283 "dirs\t%lld\n\tindices\t\t%lld\n", files, directories, attributes, 284 attributeDirectories, indices); 285 286 if (result.status == B_ENTRY_NOT_FOUND) 287 result.status = B_OK; 288 289 return result.status; 290 } 291 292 293 // #pragma mark - 294 295 296 status_t 297 get_disk_system_add_ons(BList* addOns) 298 { 299 BFSAddOn* addOn = new(nothrow) BFSAddOn; 300 if (!addOn) 301 return B_NO_MEMORY; 302 303 if (!addOns->AddItem(addOn)) { 304 delete addOn; 305 return B_NO_MEMORY; 306 } 307 308 return B_OK; 309 } 310