1 // KFileSystem.cpp 2 3 #include <fcntl.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 7 #include <fs_interface.h> 8 9 #include "ddm_modules.h" 10 #include "KDiskDeviceUtils.h" 11 #include "KFileSystem.h" 12 #include "KPartition.h" 13 14 15 // constructor 16 KFileSystem::KFileSystem(const char *name) 17 : KDiskSystem(name), 18 fModule(NULL) 19 { 20 } 21 22 // destructor 23 KFileSystem::~KFileSystem() 24 { 25 } 26 27 // Init 28 status_t 29 KFileSystem::Init() 30 { 31 status_t error = KDiskSystem::Init(); 32 if (error != B_OK) 33 return error; 34 error = Load(); 35 if (error != B_OK) 36 return error; 37 error = SetPrettyName(fModule->pretty_name); 38 SetFlags(/*fModule->flags |*/ B_DISK_SYSTEM_IS_FILE_SYSTEM); 39 Unload(); 40 return error; 41 } 42 43 // Identify 44 float 45 KFileSystem::Identify(KPartition *partition, void **cookie) 46 { 47 if (!partition || !cookie || !fModule || !fModule->identify_partition) 48 return -1; 49 int fd = -1; 50 if (partition->Open(O_RDONLY, &fd) != B_OK) 51 return -1; 52 float result = fModule->identify_partition(fd, partition->PartitionData(), 53 cookie); 54 close(fd); 55 return result; 56 } 57 58 // Scan 59 status_t 60 KFileSystem::Scan(KPartition *partition, void *cookie) 61 { 62 if (!partition || !fModule || !fModule->scan_partition) 63 return B_ERROR; 64 int fd = -1; 65 status_t result = partition->Open(O_RDONLY, &fd); 66 if (result != B_OK) 67 return result; 68 result = fModule->scan_partition(fd, partition->PartitionData(), cookie); 69 close(fd); 70 return result; 71 } 72 73 // FreeIdentifyCookie 74 void 75 KFileSystem::FreeIdentifyCookie(KPartition *partition, void *cookie) 76 { 77 if (!partition || !fModule || !fModule->free_identify_partition_cookie) 78 return; 79 fModule->free_identify_partition_cookie(partition->PartitionData(), 80 cookie); 81 } 82 83 // FreeContentCookie 84 void 85 KFileSystem::FreeContentCookie(KPartition *partition) 86 { 87 if (!partition || !fModule || !fModule->free_partition_content_cookie) 88 return; 89 fModule->free_partition_content_cookie(partition->PartitionData()); 90 } 91 92 // SupportsDefragmenting 93 bool 94 KFileSystem::SupportsDefragmenting(KPartition *partition, bool *whileMounted) 95 { 96 bool _whileMounted = false; 97 if (!whileMounted) 98 whileMounted = &_whileMounted; 99 if (!partition || partition->DiskSystem() != this || !fModule 100 || !fModule->supports_defragmenting) { 101 return (*whileMounted = false); 102 } 103 return fModule->supports_defragmenting(partition->PartitionData(), 104 whileMounted); 105 } 106 107 // SupportsRepairing 108 bool 109 KFileSystem::SupportsRepairing(KPartition *partition, bool checkOnly, 110 bool *whileMounted) 111 { 112 bool _whileMounted = false; 113 if (!whileMounted) 114 whileMounted = &_whileMounted; 115 if (!partition || partition->DiskSystem() != this || !fModule 116 || !fModule->supports_repairing) { 117 return (*whileMounted = false); 118 } 119 return fModule->supports_repairing(partition->PartitionData(), checkOnly, 120 whileMounted); 121 } 122 123 // SupportsResizing 124 bool 125 KFileSystem::SupportsResizing(KPartition *partition, bool *whileMounted) 126 { 127 bool _whileMounted = false; 128 if (!whileMounted) 129 whileMounted = &_whileMounted; 130 if (!partition || partition->DiskSystem() != this || !fModule 131 || !fModule->supports_resizing) { 132 return (*whileMounted = false); 133 } 134 return fModule->supports_resizing(partition->PartitionData(), 135 whileMounted); 136 } 137 138 // SupportsMoving 139 bool 140 KFileSystem::SupportsMoving(KPartition *partition, bool *isNoOp) 141 { 142 bool _isNoOp = false; 143 if (!isNoOp) 144 isNoOp = &_isNoOp; 145 if (!partition || partition->DiskSystem() != this || !fModule 146 || !fModule->supports_moving) { 147 return (*isNoOp = false); 148 } 149 return fModule->supports_moving(partition->PartitionData(), isNoOp); 150 } 151 152 // SupportsSettingContentName 153 bool 154 KFileSystem::SupportsSettingContentName(KPartition *partition, 155 bool *whileMounted) 156 { 157 bool _whileMounted = false; 158 if (!whileMounted) 159 whileMounted = &_whileMounted; 160 if (!partition || partition->DiskSystem() != this || !fModule 161 || !fModule->supports_setting_content_name) { 162 return (*whileMounted = false); 163 } 164 return fModule->supports_setting_content_name(partition->PartitionData(), 165 whileMounted); 166 } 167 168 // SupportsSettingContentParameters 169 bool 170 KFileSystem::SupportsSettingContentParameters(KPartition *partition, 171 bool *whileMounted) 172 { 173 bool _whileMounted = false; 174 if (!whileMounted) 175 whileMounted = &_whileMounted; 176 if (!partition || partition->DiskSystem() != this || !fModule 177 || !fModule->supports_setting_content_parameters) { 178 return (*whileMounted = false); 179 } 180 return fModule->supports_setting_content_parameters( 181 partition->PartitionData(), whileMounted); 182 } 183 184 // SupportsInitializing 185 bool 186 KFileSystem::SupportsInitializing(KPartition *partition) 187 { 188 return (partition && fModule && fModule->supports_initializing 189 && fModule->supports_initializing(partition->PartitionData())); 190 } 191 192 // ValidateResize 193 bool 194 KFileSystem::ValidateResize(KPartition *partition, off_t *size) 195 { 196 return (partition && size && partition->DiskSystem() == this && fModule 197 && fModule->validate_resize 198 && fModule->validate_resize(partition->PartitionData(), size)); 199 } 200 201 // ValidateMove 202 bool 203 KFileSystem::ValidateMove(KPartition *partition, off_t *start) 204 { 205 return (partition && start && partition->DiskSystem() == this && fModule 206 && fModule->validate_move 207 && fModule->validate_move(partition->PartitionData(), start)); 208 } 209 210 // ValidateSetContentName 211 bool 212 KFileSystem::ValidateSetContentName(KPartition *partition, char *name) 213 { 214 return (partition && name && partition->DiskSystem() == this 215 && fModule && fModule->validate_set_content_name 216 && fModule->validate_set_content_name(partition->PartitionData(), 217 name)); 218 } 219 220 // ValidateSetContentParameters 221 bool 222 KFileSystem::ValidateSetContentParameters(KPartition *partition, 223 const char *parameters) 224 { 225 return (partition && partition->DiskSystem() == this && fModule 226 && fModule->validate_set_content_parameters 227 && fModule->validate_set_content_parameters( 228 partition->PartitionData(), parameters)); 229 } 230 231 // ValidateInitialize 232 bool 233 KFileSystem::ValidateInitialize(KPartition *partition, char *name, 234 const char *parameters) 235 { 236 return (partition && name && fModule && fModule->validate_initialize 237 && fModule->validate_initialize(partition->PartitionData(), name, 238 parameters)); 239 } 240 241 // ShadowPartitionChanged 242 status_t 243 KFileSystem::ShadowPartitionChanged(KPartition *partition, uint32 operation) 244 { 245 if (!partition) 246 return B_BAD_VALUE; 247 if (!fModule) 248 return B_ERROR; 249 // If not implemented, we assume, that the file system doesn't have to 250 // make any additional changes. 251 if (!fModule->shadow_changed) 252 return B_OK; 253 return fModule->shadow_changed(partition->PartitionData(), operation); 254 } 255 256 // Defragment 257 status_t 258 KFileSystem::Defragment(KPartition *partition, KDiskDeviceJob *job) 259 { 260 // to be implemented 261 return B_ERROR; 262 } 263 264 // Repair 265 status_t 266 KFileSystem::Repair(KPartition *partition, bool checkOnly, KDiskDeviceJob *job) 267 { 268 // to be implemented 269 return B_ERROR; 270 } 271 272 // Resize 273 status_t 274 KFileSystem::Resize(KPartition *partition, off_t size, KDiskDeviceJob *job) 275 { 276 // to be implemented 277 return B_ERROR; 278 } 279 280 // Move 281 status_t 282 KFileSystem::Move(KPartition *partition, off_t offset, KDiskDeviceJob *job) 283 { 284 // to be implemented 285 return B_ERROR; 286 } 287 288 // SetContentName 289 status_t 290 KFileSystem::SetContentName(KPartition *partition, char *name, 291 KDiskDeviceJob *job) 292 { 293 // to be implemented 294 return B_ERROR; 295 } 296 297 298 status_t 299 KFileSystem::SetContentParameters(KPartition *partition, 300 const char *parameters, KDiskDeviceJob *job) 301 { 302 // to be implemented 303 return B_ERROR; 304 } 305 306 307 status_t 308 KFileSystem::Initialize(KPartition *partition, const char *name, 309 const char *parameters, KDiskDeviceJob *job) 310 { 311 // to be implemented 312 return B_ERROR; 313 } 314 315 316 status_t 317 KFileSystem::LoadModule() 318 { 319 if (fModule) // shouldn't happen 320 return B_OK; 321 322 return get_module(Name(), (module_info **)&fModule); 323 } 324 325 // UnloadModule 326 void 327 KFileSystem::UnloadModule() 328 { 329 if (fModule) { 330 put_module(fModule->info.name); 331 fModule = NULL; 332 } 333 } 334 335