| | 278 | |
| | 279 | === __void '''_display_one_block'''( unsigned char* buf , char* string , unsigned int block_id )__ === |
| | 280 | This debug function displays the content of a 512 bytes buffer <buf>, with an identifier defined by the <string> and <block_id> arguments. |
| | 281 | |
| | 282 | === __void '''_display_fat_descriptor'''()__ === |
| | 283 | This debug function displays the FAT descriptor content. |
| | 284 | |
| | 285 | === __void '''_get_name_from_long'''( unsigned char* buffer , char* name )__ === |
| | 286 | This function extract a (partial) name from a LFN directory entry. |
| | 287 | |
| | 288 | === __void '''_get_name_from_short'''( unsigned char* buffer , char* name )__ === |
| | 289 | This function extract a name from a NORMAL directory entry. |
| | 290 | |
| | 291 | === __unsigned int '''_get_levels_from_size'''( unsigned int size )__ === |
| | 292 | This function returns the number of levels of a File-Cache from the size of the file. |
| | 293 | |
| | 294 | === __unsigned int '''_get_name_from_path'''( char* pathname , char* name , unsigned int* nb_read )__ === |
| | 295 | This function analyses the <pathname> argument, from the character defined by the <nb_read> argument. |
| | 296 | It copies the found name in the <name> buffer (without '/'), and updates the <nb_read"> argument. |
| | 297 | It returns 0 on success. It returns 1 if one name length > NAME_MAX_SIZE characters. |
| | 298 | |
| | 299 | === __unsigned int '''_get_last_name'''( char* pathname , char* name )__ === |
| | 300 | This function scan the "pathname" argument, and copies in the <name> buffer the last name in <pathname>. |
| | 301 | It returns 0 on success.It returns 1 if one name length > NAME_MAX_SIZE characters. |
| | 302 | |
| | 303 | ////////////////////////////////////////////////////////////////////////////////// |
| | 304 | // The following function accesses the Fat-Cache and returns in the "value" |
| | 305 | // argument the content of the FAT slot identified by the "cluster" argument. |
| | 306 | // It loads the missing cluster from block device into cache in case of miss. |
| | 307 | // It returns 0 on success. |
| | 308 | // It returns 1 on error. |
| | 309 | ////////////////////////////////////////////////////////////////////////////////// |
| | 310 | |
| | 311 | static unsigned int _get_fat_entry( unsigned int cluster, |
| | 312 | unsigned int* value ); |
| | 313 | |
| | 314 | ////////////////////////////////////////////////////////////////////////////////// |
| | 315 | // The following function writes a new "value" in the Fat-Cache, in the slot |
| | 316 | // identified by the "cluster" argument. |
| | 317 | // It loads the missing cluster from block device into cache in case of miss. |
| | 318 | // It returns 0 on success, |
| | 319 | // It returns 1 on error. |
| | 320 | ////////////////////////////////////////////////////////////////////////////////// |
| | 321 | |
| | 322 | static unsigned int _set_fat_entry( unsigned int cluster, |
| | 323 | unsigned int value ); |
| | 324 | |
| | 325 | ////////////////////////////////////////////////////////////////////////////////// |
| | 326 | // The following function introduces the inode identified by the <child> argument, |
| | 327 | // as a new child of the <parent> inode in the Inode-Tree. |
| | 328 | // All checking are supposed to be done by the caller. |
| | 329 | // Nor the File-Cache, neither the block device are modified. |
| | 330 | ////////////////////////////////////////////////////////////////////////////////// |
| | 331 | |
| | 332 | static void _add_inode_in_tree( fat_inode_t* child, |
| | 333 | fat_inode_t* parent ); |
| | 334 | |
| | 335 | ////////////////////////////////////////////////////////////////////////////////// |
| | 336 | // The following function removes one inode identified by the <inode> argument |
| | 337 | // from the Inode-Tree. All checking are supposed to be done by the caller. |
| | 338 | // Nor the File-Cache, neither the block device are modified. |
| | 339 | ////////////////////////////////////////////////////////////////////////////////// |
| | 340 | |
| | 341 | static void _remove_inode_from_tree( fat_inode_t* inode ); |
| | 342 | |
| | 343 | ////////////////////////////////////////////////////////////////////////////////// |
| | 344 | // This recursive function scan one File-Cache (or Fat-Cache) from root to leaves, |
| | 345 | // to writes all dirty clusters to block device, and reset the dirty bits. |
| | 346 | // The cache is identified by the <root> an <levels> arguments. |
| | 347 | // The <string> argument is only used for debug : inode name or Fat. |
| | 348 | // It returns 0 on success. |
| | 349 | // It returns 1 on error. |
| | 350 | ////////////////////////////////////////////////////////////////////////////////// |
| | 351 | |
| | 352 | static unsigned int _update_device_from_cache( unsigned int levels, |
| | 353 | fat_cache_node_t* root, |
| | 354 | char* string ); |
| | 355 | |
| | 356 | ////////////////////////////////////////////////////////////////////////////////// |
| | 357 | // The following function accesses directly the FS_INFO block on the block device, |
| | 358 | // to update the "first_free_cluster" and "free_clusters_number" values, |
| | 359 | // using only the Fat-Descriptor single block buffer. |
| | 360 | // It return 0 on success. |
| | 361 | // It return 1 on error. |
| | 362 | ////////////////////////////////////////////////////////////////////////////////// |
| | 363 | |
| | 364 | static unsigned int _update_fs_info(); |
| | 365 | |
| | 366 | ////////////////////////////////////////////////////////////////////////////// |
| | 367 | // The following function read a data field (from one to four bytes) |
| | 368 | // from an unsigned char[] buffer, taking endianness into account. |
| | 369 | // The analysed field is defined by the <offset> and <size> arguments. |
| | 370 | ////////////////////////////////////////////////////////////////////////////// |
| | 371 | |
| | 372 | static unsigned int _read_entry( unsigned int offset, |
| | 373 | unsigned int size, |
| | 374 | unsigned char* buffer, |
| | 375 | unsigned int little_indian ); |
| | 376 | |
| | 377 | ////////////////////////////////////////////////////////////////////////////////// |
| | 378 | // The following function returns the lba of first sector in DATA region |
| | 379 | // from the cluster index. The cluster index must be larger than 2. |
| | 380 | ////////////////////////////////////////////////////////////////////////////////// |
| | 381 | |
| | 382 | static unsigned int _cluster_to_lba( unsigned int cluster ); |
| | 383 | |
| | 384 | ////////////////////////////////////////////////////////////////////////////////// |
| | 385 | // The following function returns in the "nb_entries" argument the number of files |
| | 386 | // (or directories) contained in a directory identified by the "inode " pointer. |
| | 387 | // It returns 0 on success. |
| | 388 | // It returns 1 on error. |
| | 389 | ////////////////////////////////////////////////////////////////////////////////// |
| | 390 | |
| | 391 | static unsigned int _get_nb_entries( fat_inode_t* inode, |
| | 392 | unsigned int* nb_entries ); |
| | 393 | |
| | 394 | ////////////////////////////////////////////////////////////////////////////////// |
| | 395 | // The following function search in the directory identified by the <parent> |
| | 396 | // inode pointer a child (file or directory) identified by its <name>. |
| | 397 | // It returns in the <inode> argument the searched child inode pointer. |
| | 398 | // If the searched name is not found in the Inode-Tree, the function accesses |
| | 399 | // the File-cache associated to the parent directory. |
| | 400 | // If the child exists on block device, the Inode-Tree is updated, and |
| | 401 | // a success code is returned. |
| | 402 | // If the file/dir does not exist on block device, a error code is returned. |
| | 403 | // It returns 0 if inode found. |
| | 404 | // It returns 1 if inode not found. |
| | 405 | // It returns 2 on error in cache access. |
| | 406 | ////////////////////////////////////////////////////////////////////////////////// |
| | 407 | |
| | 408 | static unsigned int _get_child_from_parent( fat_inode_t* parent, |
| | 409 | char* name, |
| | 410 | fat_inode_t** inode ); |
| | 411 | |
| | 412 | ///////////////////////////////////////////////////////////////////////////////// |
| | 413 | // For a file (or a directory) identified by the "pathname" argument, the |
| | 414 | // following function returns in the "inode" argument the inode pointer |
| | 415 | // associated to the searched file (or directory), with code (0). |
| | 416 | // If the searched file (or directory) is not found, but the parent directory |
| | 417 | // is found, it returns in the "inode" argument the pointer on the parent inode, |
| | 418 | // with code (1). Finally, code (2) and code (3) are error codes. |
| | 419 | // Both the Inode-Tree and the involved Cache-Files are updated from the block |
| | 420 | // device in case of miss on one inode during the search in path. |
| | 421 | // Neither the Fat-Cache, nor the block device are updated. |
| | 422 | // It returns 0 if searched inode found |
| | 423 | // It returns 1 if searched inode not found but parent directory found |
| | 424 | // It returns 2 if searched inode not found and parent directory not found |
| | 425 | // It returns 3 if one name too long |
| | 426 | ///////////////////////////////////////////////////////////////////////////////// |
| | 427 | |
| | 428 | static unsigned int _get_inode_from_path( char* pathname, |
| | 429 | fat_inode_t** inode ); |
| | 430 | |
| | 431 | ////////////////////////////////////////////////////////////////////////////////// |
| | 432 | // The following function checks if node "a" is an ancestor of inode "b". |
| | 433 | // It returns 0 on failure. |
| | 434 | // It returns 1 otherwise. |
| | 435 | ////////////////////////////////////////////////////////////////////////////////// |
| | 436 | |
| | 437 | static unsigned int _is_ancestor( fat_inode_t* a, |
| | 438 | fat_inode_t* b); |
| | 439 | |
| | 440 | ////////////////////////////////////////////////////////////////////////////////// |
| | 441 | // This function computes the length and the number of LFN entries required |
| | 442 | // to store a node name in the "length" and "nb_lfn" arguments. |
| | 443 | // Short name (less than 13 characters) require 1 LFN entry. |
| | 444 | // Medium names (from 14 to 26 characters require 2 LFN entries. |
| | 445 | // Large names (up to 31 characters) require 3 LFN entries. |
| | 446 | // It returns 0 on success. |
| | 447 | // It returns 1 if length larger than 31 characters. |
| | 448 | ////////////////////////////////////////////////////////////////////////////////// |
| | 449 | |
| | 450 | static unsigned int _check_name_length( char* name, |
| | 451 | unsigned int* length, |
| | 452 | unsigned int* nb_lfn ); |
| | 453 | |
| | 454 | ////////////////////////////////////////////////////////////////////////////////// |
| | 455 | // For a node identified by the "inode" argument, this function updates the |
| | 456 | // "size" and "cluster" values in the entry of the parent directory File-Cache. |
| | 457 | // It set the dirty bit in the modified buffer of the parent directory File-Cache. |
| | 458 | ////////////////////////////////////////////////////////////////////////////////// |
| | 459 | |
| | 460 | static unsigned int _update_dir_entry( fat_inode_t* inode ); |
| | 461 | |
| | 462 | ////////////////////////////////////////////////////////////////////////////////// |
| | 463 | // The following function add new "child" in Cache-File of "parent" directory. |
| | 464 | // It accesses the File_Cache associated to the parent directory, and scan the |
| | 465 | // clusters allocated to this directory to find the NO_MORE entry. |
| | 466 | // This entry will be the first modified entry in the directory. |
| | 467 | // Regarding the name storage, it uses LFN entries for all names. |
| | 468 | // Therefore, it writes 1, 2, or 3 LFN entries (depending on the child name |
| | 469 | // actual length, it writes one NORMAL entry, and writes the new NOMORE entry. |
| | 470 | // It updates the dentry field in the child inode. |
| | 471 | // It set the dirty bit for all modified File-Cache buffers. |
| | 472 | // The block device is not modified by this function. |
| | 473 | ////////////////////////////////////////////////////////////////////////////////// |
| | 474 | |
| | 475 | static unsigned int _add_dir_entry( fat_inode_t* child, |
| | 476 | fat_inode_t* parent ); |
| | 477 | |
| | 478 | ////////////////////////////////////////////////////////////////////////////////// |
| | 479 | // The following function invalidates all dir_entries associated to the "inode" |
| | 480 | // argument from its parent directory. |
| | 481 | // It set the dirty bit for all modified buffers in parent directory Cache-File. |
| | 482 | // The inode itself is not modified by this function. |
| | 483 | // The block device is not modified by this function. |
| | 484 | ////////////////////////////////////////////////////////////////////////////////// |
| | 485 | |
| | 486 | static unsigned int _remove_dir_entry( fat_inode_t* inode ); |
| | 487 | |
| | 488 | ////////////////////////////////////////////////////////////////////////////////// |
| | 489 | // The following function add the special entries "." and ".." in the File-Cache |
| | 490 | // of the directory identified by the "child" argument. |
| | 491 | // The parent directory is defined by the "parent" argument. |
| | 492 | // The child directory File-Cache is supposed to be empty. |
| | 493 | // We use two NORMAL entries for these "." and ".." entries. |
| | 494 | // The block device is not modified by this function. |
| | 495 | ////////////////////////////////////////////////////////////////////////////////// |
| | 496 | |
| | 497 | static void _add_special_directories( fat_inode_t* child, |
| | 498 | fat_inode_t* parent ); |
| | 499 | |
| | 500 | ////////////////////////////////////////////////////////////////////////////////// |
| | 501 | // The following function releases all clusters allocated to a file or directory, |
| | 502 | // from the cluster index defined by the <cluster> argument, until the end |
| | 503 | // of the FAT linked list. |
| | 504 | // It calls _get_fat_entry() and _set_fat_entry() functions to scan the FAT, |
| | 505 | // and to update the clusters chaining. |
| | 506 | // The FAT region on block device is updated. |
| | 507 | // It returns 0 on success. |
| | 508 | // It returns 1 on error. |
| | 509 | ////////////////////////////////////////////////////////////////////////////////// |
| | 510 | |
| | 511 | static unsigned int _clusters_release( unsigned int cluster ); |
| | 512 | |
| | 513 | ////////////////////////////////////////////////////////////////////////////////// |
| | 514 | // This function allocate one cluster in FAT to a file (or directory) identified |
| | 515 | // by the <inode> pointer. The allocated cluster index is returned in the |
| | 516 | // <cluster> argument. |
| | 517 | // It allocates also the associated buffers and buffer descriptors in Cache-File. |
| | 518 | // It calls _get_fat_entry() and _set_fat_entry() functions to update the |
| | 519 | // clusters chaining in the Cache-Fat. The FAT region on block device is updated. |
| | 520 | // It returns 0 on success. |
| | 521 | // It returns 1 on error. |
| | 522 | ////////////////////////////////////////////////////////////////////////////////// |
| | 523 | |
| | 524 | static unsigned int _cluster_allocate( fat_inode_t* inode, |
| | 525 | unsigned int* cluster ); |
| | 526 | |
| | 527 | ////////////////////////////////////////////////////////////////////////////////// |
| | 528 | // This recursive function scans one File-Cache (or Fat-Cache) from root to |
| | 529 | // leaves. All memory allocated for 4KB buffers, and buffer descriptors (in |
| | 530 | // leaves) is released, along with the 64-Tree structure (root node is kept). |
| | 531 | // The cache is identified by the "root" and "levels" arguments. |
| | 532 | // It should not contain any dirty clusters. |
| | 533 | ////////////////////////////////////////////////////////////////////////////////// |
| | 534 | |
| | 535 | static void _release_cache_memory( fat_cache_node_t* root, |
| | 536 | unsigned int levels ); |
| | 537 | |
| | 538 | ////////////////////////////////////////////////////////////////////////////////// |
| | 539 | // The following function allocates and initializes a new cache node. |
| | 540 | // Its first child can be specified (used when adding a cache level). |
| | 541 | // The 63 other children are set to NULL. |
| | 542 | // It returns a pointer to a new Fat-Cache node. |
| | 543 | ////////////////////////////////////////////////////////////////////////////////// |
| | 544 | |
| | 545 | static fat_cache_node_t* _allocate_one_cache_node( fat_cache_node_t* first_child ); |
| | 546 | |
| | 547 | ////////////////////////////////////////////////////////////////////////////////// |
| | 548 | // The following function allocates and initializes a new inode, |
| | 549 | // using the values defined by the arguments. |
| | 550 | // If the "cache_allocate" argument is true, an empty cache is allocated. |
| | 551 | // It returns a pointer on the new inode. |
| | 552 | ////////////////////////////////////////////////////////////////////////////////// |
| | 553 | |
| | 554 | static fat_inode_t* _allocate_one_inode( char* name, |
| | 555 | unsigned int is_dir, |
| | 556 | unsigned int cluster, |
| | 557 | unsigned int size, |
| | 558 | unsigned int count, |
| | 559 | unsigned int dentry, |
| | 560 | unsigned int cache_allocate ); |
| | 561 | |
| | 562 | ////////////////////////////////////////////////////////////////////////////////// |
| | 563 | // The following function allocates a 4 Kbytes buffer and the associated cluster |
| | 564 | // descriptor for the file (or directory) identified by the <inode> argument, |
| | 565 | // and updates the Cache_File slot identified by the <cluster_id> argument. |
| | 566 | // The File-Cache slot must be empty. |
| | 567 | // It updates the buffer descriptor, using the <cluster> argument, that is |
| | 568 | // the cluster index in FAT. The cluster descriptor dirty field is set. |
| | 569 | // It traverse the 64-tree Cache-file from top to bottom to find the last level. |
| | 570 | ////////////////////////////////////////////////////////////////////////////////// |
| | 571 | |
| | 572 | static void _allocate_one_buffer( fat_inode_t* inode, |
| | 573 | unsigned int cluster_id, |
| | 574 | unsigned int cluster ); |
| | 575 | |
| | 576 | ////////////////////////////////////////////////////////////////////////////////// |
| | 577 | // The following function allocates one free cluster from the FAT |
| | 578 | // and returns the cluster index in the <cluster> argument. |
| | 579 | // It updates the FAT slot, and the two FAT global variables: first_free_cluster, |
| | 580 | // and free_clusters_number. |
| | 581 | // It returns 0 on success. |
| | 582 | // It returns 1 on error. |
| | 583 | ////////////////////////////////////////////////////////////////////////////////// |
| | 584 | |
| | 585 | static unsigned int _allocate_one_cluster( unsigned int* cluster ); |
| | 586 | |
| | 587 | ///////////////////////////////////////////////////////////////////////////// |
| | 588 | // This function remove from the file system a file or a directory |
| | 589 | // identified by the <inode> argument. |
| | 590 | // The remove condition must be checked by the caller. |
| | 591 | // The relevant lock(s) must have been taken by te caller. |
| | 592 | // It returns 0 on success. |
| | 593 | // It returns 1 on error. |
| | 594 | ///////////////////////////////////////////////////////////////////////////// |
| | 595 | |
| | 596 | static unsigned int _remove_node_from_fs( fat_inode_t* inode ); |
| | 597 | |
| | 598 | ///////////////////////////////////////////////////////////////////////////// |
| | 599 | // This function return the cluster index and the size for a file |
| | 600 | // identified by the "pathname" argument, scanning directly the block |
| | 601 | // device DATA region. |
| | 602 | // It is intended to be called only by the _fat_load_no_cache() function, |
| | 603 | // it does not use the dynamically allocated File Caches, but uses only |
| | 604 | // the 4 Kbytes _fat_buffer_data. |
| | 605 | // It returns 0 on success. |
| | 606 | // It returns 1 on error. |
| | 607 | ///////////////////////////////////////////////////////////////////////////// |
| | 608 | |
| | 609 | static unsigned int _file_info_no_cache( char* pathname, |
| | 610 | unsigned int* file_cluster, |
| | 611 | unsigned int* file_size ); |
| | 612 | |
| | 613 | ///////////////////////////////////////////////////////////////////////////// |
| | 614 | // This function scan directly the FAT region on the block device, |
| | 615 | // and returns in the "next" argument the value stored in the fat slot |
| | 616 | // identified by the "cluster" argument. |
| | 617 | // It is intended to be called only by the _fat_load_no_cache() function, |
| | 618 | // as it does not use the dynamically allocated Fat-Cache, but uses only |
| | 619 | // the 4 Kbytes _fat_buffer_fat. |
| | 620 | // It returns 0 on success. |
| | 621 | // It returns 1 on error. |
| | 622 | ///////////////////////////////////////////////////////////////////////////// |
| | 623 | |
| | 624 | static unsigned int _next_cluster_no_cache( unsigned int cluster, |
| | 625 | unsigned int* next ); |
| | 626 | |
| | 627 | |
| | 628 | ////////////////////////////////////////////////////////////////////////////////// |
| | 629 | // The following functions return the the size (bytes) of a FAT field, |
| | 630 | // identified by an (offset,length) mnemonic defined in the fat32.h file. |
| | 631 | ////////////////////////////////////////////////////////////////////////////////// |
| | 632 | |
| | 633 | static inline int get_length( int offset , int length ) { return length; } |
| | 634 | |
| | 635 | static inline int get_offset( int offset , int length ) { return offset; } |
| | 636 | |
| | 637 | ////////////////////////////////////////////////////////////////////////////////// |
| | 638 | // The following function returns in the <desc> argument a pointer on a buffer |
| | 639 | // descriptor contained in the Fat_Cache. |
| | 640 | // The <cluster_id> argument is the buffer index in the FAT_Cache. |
| | 641 | // In case of miss, a 4 Kbytesbuffer and a buffer descriptor are allocated |
| | 642 | // from the local heap, and the missing cluster is loaded in the Fat_Cache. |
| | 643 | // It returns 0 on success. |
| | 644 | // It returns 1 on error. |
| | 645 | ////////////////////////////////////////////////////////////////////////////////// |
| | 646 | |
| | 647 | static unsigned int _get_fat_cache_buffer( unsigned int cluster_id, |
| | 648 | fat_cache_desc_t** desc ); |
| | 649 | |
| | 650 | |
| | 651 | |