[1] | 1 | /* |
---|
| 2 | * fatfs.c - FATFS file system API implementation. |
---|
| 3 | * |
---|
[625] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_special.h> |
---|
| 27 | #include <printk.h> |
---|
[401] | 28 | #include <thread.h> |
---|
[1] | 29 | #include <kmem.h> |
---|
| 30 | #include <ppm.h> |
---|
| 31 | #include <vfs.h> |
---|
[238] | 32 | #include <string.h> |
---|
[1] | 33 | #include <rpc.h> |
---|
| 34 | #include <mapper.h> |
---|
[23] | 35 | #include <cluster.h> |
---|
[1] | 36 | #include <dev_ioc.h> |
---|
| 37 | #include <fatfs.h> |
---|
| 38 | |
---|
[626] | 39 | #define LITTLE_ENDIAN 1 |
---|
[50] | 40 | |
---|
[23] | 41 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 42 | // Extern variables |
---|
| 43 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 44 | |
---|
[602] | 45 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in kernel_init.c file |
---|
[23] | 46 | |
---|
[1] | 47 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 48 | // FATFS specific static functions |
---|
[1] | 49 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | |
---|
[188] | 51 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 52 | // These functions return the "offset" and "length" values of an |
---|
| 53 | // [offset,length] constant defined in the fatfs.h file. |
---|
| 54 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 55 | |
---|
[568] | 56 | static inline int get_length( int offset __attribute__((unused)), int length ) { return length; } |
---|
[238] | 57 | |
---|
[568] | 58 | static inline int get_offset( int offset, int length __attribute__((unused)) ) { return offset; } |
---|
[238] | 59 | |
---|
| 60 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[440] | 61 | // This static function returns the LBA of the first sector of a FAT cluster. |
---|
[188] | 62 | // This function can be called by any thread running in any cluster. |
---|
| 63 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 64 | // @ ctx : pointer on FATFS context. |
---|
| 65 | // @ cluster : cluster index in FATFS. |
---|
| 66 | // @ return the lba value. |
---|
| 67 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 68 | static inline uint32_t fatfs_lba_from_cluster( fatfs_ctx_t * ctx, |
---|
| 69 | uint32_t cluster ) |
---|
[1] | 70 | { |
---|
[23] | 71 | return (ctx->cluster_begin_lba + ((cluster - 2) << 3)); |
---|
[1] | 72 | } |
---|
| 73 | |
---|
[246] | 74 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 75 | // This function return an integer record value (one, two, or four bytes) from a local |
---|
| 76 | // array of bytes, taking into account the global LITTLE_ENDIAN parameter: |
---|
| 77 | // if LITTLE_ENDIAN is true, the most significant byte has the highest address. |
---|
[238] | 78 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 79 | // @ offset : first byte in array. |
---|
| 80 | // @ nbytes : record length in bytes (1/2/4). |
---|
| 81 | // @ buffer : local pointer on byte array. |
---|
[23] | 82 | // @ return the integer value in a 32 bits word. |
---|
[238] | 83 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 84 | static uint32_t fatfs_get_record( uint32_t offset, |
---|
[626] | 85 | uint32_t nbytes, |
---|
| 86 | uint8_t * buffer ) |
---|
[23] | 87 | { |
---|
[626] | 88 | uint32_t i; |
---|
| 89 | uint32_t res = 0; |
---|
[1] | 90 | |
---|
[626] | 91 | if ( LITTLE_ENDIAN ) |
---|
[23] | 92 | { |
---|
[626] | 93 | for( i = nbytes ; i > 0 ; i-- ) res = (res<<8) | buffer[offset+i-1]; |
---|
[23] | 94 | } |
---|
[626] | 95 | else |
---|
[23] | 96 | { |
---|
[626] | 97 | for( i = 0 ; i < nbytes ; i++ ) res = (res<<8) | buffer[offset+i]; |
---|
[23] | 98 | } |
---|
| 99 | return res; |
---|
| 100 | |
---|
[238] | 101 | } // end fatfs_get_record() |
---|
[23] | 102 | |
---|
[238] | 103 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 104 | // This function return an integer record value (one, two, or four bytes) from a remote |
---|
| 105 | // array of bytes, taking into account the global LITTLE_ENDIAN parameter: |
---|
| 106 | // if LITTLE_ENDIAN is true, the most significant byte has the highest address. |
---|
[602] | 107 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 108 | // @ offset : first byte in array. |
---|
| 109 | // @ nbytes : record length in bytes (1/2/4). |
---|
| 110 | // @ buffer_xp : extended pointer on byte array. |
---|
[602] | 111 | // @ return the integer value in a 32 bits word. |
---|
| 112 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 113 | static uint32_t fatfs_get_remote_record( uint32_t offset, |
---|
| 114 | uint32_t nbytes, |
---|
| 115 | xptr_t buffer_xp ) |
---|
| 116 | { |
---|
| 117 | uint32_t i; |
---|
| 118 | uint32_t res = 0; |
---|
| 119 | |
---|
| 120 | if ( LITTLE_ENDIAN ) |
---|
| 121 | { |
---|
| 122 | for( i = nbytes ; i > 0 ; i-- ) |
---|
| 123 | { |
---|
| 124 | res = (res<<8) | hal_remote_lb( buffer_xp+offset+i-1 ); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | for( i = 0 ; i < nbytes ; i++ ) |
---|
| 130 | { |
---|
| 131 | res = (res<<8) | hal_remote_lb( buffer_xp+offset+i ); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | return res; |
---|
| 135 | |
---|
| 136 | } // end fatfs_get_remote_record() |
---|
| 137 | |
---|
| 138 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 139 | // This function writes one, two, or four bytes from a 32 bits integer to a local |
---|
| 140 | // array of bytes, taking into account the global LITTLE_ENDIAN parameter: |
---|
| 141 | // if LITTLE_ENDIAN is true, the most significant byte has the highest address. |
---|
| 142 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 143 | // @ offset : first byte in array. |
---|
| 144 | // @ nbytes : record length in bytes (1/2/4). |
---|
| 145 | // @ buffer : local pointer on byte array. |
---|
| 146 | // @ value : 32 bits integer value. |
---|
| 147 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 148 | static void fatfs_set_record( uint32_t offset, |
---|
[626] | 149 | uint32_t nbytes, |
---|
[602] | 150 | uint8_t * buffer, |
---|
| 151 | uint32_t value ) |
---|
| 152 | { |
---|
[626] | 153 | uint32_t i; |
---|
[602] | 154 | |
---|
[626] | 155 | if ( LITTLE_ENDIAN ) |
---|
[602] | 156 | { |
---|
[626] | 157 | for( i = nbytes ; i > 0 ; i-- ) buffer[offset+i-1] = (uint8_t)(value>>((i-1)<<3)); |
---|
[602] | 158 | } |
---|
[626] | 159 | else |
---|
[602] | 160 | { |
---|
[626] | 161 | for( i = 0 ; i < nbytes ; i++ ) buffer[offset+i] = (uint8_t)(value>>((nbytes-1-i)<<3)); |
---|
[602] | 162 | } |
---|
| 163 | |
---|
| 164 | } // end fatfs_set_record() |
---|
| 165 | |
---|
| 166 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 167 | // This function writes one, two, or four bytes from a 32 bits integer to a remote |
---|
| 168 | // array of bytes, taking into account the global LITTLE_ENDIAN parameter: |
---|
| 169 | // if LITTLE_ENDIAN is true, the most significant byte has the highest address. |
---|
| 170 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 171 | // @ offset : first byte in array. |
---|
| 172 | // @ nbytes : record length in bytes (1/2/4). |
---|
| 173 | // @ buffer_xp : extended pointer on byte array. |
---|
| 174 | // @ value : 32 bits integer value. |
---|
| 175 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 176 | static void fatfs_set_remote_record( uint32_t offset, |
---|
| 177 | uint32_t nbytes, |
---|
| 178 | xptr_t buffer_xp, |
---|
| 179 | uint32_t value ) |
---|
| 180 | { |
---|
| 181 | uint32_t i; |
---|
| 182 | |
---|
| 183 | if ( LITTLE_ENDIAN ) |
---|
| 184 | { |
---|
| 185 | for( i = nbytes ; i > 0 ; i-- ) |
---|
| 186 | { |
---|
| 187 | hal_remote_sb( (buffer_xp+offset+i-1) , (uint8_t)(value>>((i-1)<<3)) ); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
| 192 | for( i = 0 ; i < nbytes ; i++ ) |
---|
| 193 | { |
---|
| 194 | hal_remote_sb( (buffer_xp+offset+i) , (uint8_t)(value>>((nbytes-1-i)<<3)) ); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | } // end fatfs_set_record() |
---|
| 199 | |
---|
| 200 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 201 | // This static function retun in the <name> buffer a short name stored in |
---|
| 202 | // a SFN FATFS directory entry. |
---|
| 203 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 204 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 205 | // @ name : [out] buffer allocated by the caller. |
---|
| 206 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 207 | static void fatfs_get_name_from_short( uint8_t * buffer, |
---|
| 208 | char * name ) |
---|
| 209 | { |
---|
| 210 | uint32_t i; |
---|
| 211 | uint32_t j = 0; |
---|
[23] | 212 | |
---|
[238] | 213 | // get name |
---|
| 214 | for ( i = 0; i < 8 && buffer[i] != ' '; i++ ) |
---|
| 215 | { |
---|
| 216 | name[j] = to_lower( buffer[i] ); |
---|
| 217 | j++; |
---|
| 218 | } |
---|
[23] | 219 | |
---|
[238] | 220 | // get extension |
---|
| 221 | for ( i = 8; i < 8 + 3 && buffer[i] != ' '; i++ ) |
---|
| 222 | { |
---|
| 223 | // we entered the loop so there is an extension. add the dot |
---|
| 224 | if ( i == 8 ) |
---|
| 225 | { |
---|
| 226 | name[j] = '.'; |
---|
| 227 | j++; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | name[j] = to_lower( buffer[i] ); |
---|
| 231 | j++; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | name[j] = '\0'; |
---|
| 235 | |
---|
[602] | 236 | } // fatfs_get_name_from_short() |
---|
| 237 | |
---|
[238] | 238 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 239 | // This static function retun in the <name> buffer a partial name stored in |
---|
| 240 | // a LFN FATFS directory entry. |
---|
| 241 | /////////////////////////i//////////////////////////////////////////////////////////////// |
---|
| 242 | // @ buffer : pointer on buffer containing the directory entry. |
---|
| 243 | // @ name : [out] buffer allocated by the caller. |
---|
| 244 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 245 | static void fatfs_get_name_from_long( uint8_t * buffer, |
---|
| 246 | char * name ) |
---|
| 247 | { |
---|
| 248 | uint32_t name_offset = 0; |
---|
| 249 | uint32_t buffer_offset = get_length(LDIR_ORD); |
---|
| 250 | uint32_t l_name_1 = get_length(LDIR_NAME_1); |
---|
| 251 | uint32_t l_name_2 = get_length(LDIR_NAME_2); |
---|
| 252 | uint32_t l_name_3 = get_length(LDIR_NAME_3); |
---|
| 253 | uint32_t l_attr = get_length(LDIR_ATTR); |
---|
| 254 | uint32_t l_type = get_length(LDIR_TYPE); |
---|
| 255 | uint32_t l_chksum = get_length(LDIR_CHKSUM); |
---|
| 256 | uint32_t l_rsvd = get_length(LDIR_RSVD); |
---|
| 257 | |
---|
| 258 | uint32_t j = 0; |
---|
| 259 | uint32_t eof = 0; |
---|
| 260 | |
---|
| 261 | while ( (buffer_offset != DIR_ENTRY_SIZE) && (!eof) ) |
---|
| 262 | { |
---|
| 263 | while (j != l_name_1 && !eof ) |
---|
| 264 | { |
---|
| 265 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 266 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 267 | { |
---|
| 268 | eof = 1; |
---|
| 269 | continue; |
---|
| 270 | } |
---|
| 271 | name[name_offset] = buffer[buffer_offset]; |
---|
| 272 | buffer_offset += 2; |
---|
| 273 | j += 2; |
---|
| 274 | name_offset++; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | buffer_offset += (l_attr + l_type + l_chksum); |
---|
| 278 | j = 0; |
---|
| 279 | |
---|
| 280 | while (j != l_name_2 && !eof ) |
---|
| 281 | { |
---|
| 282 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 283 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 284 | { |
---|
| 285 | eof = 1; |
---|
| 286 | continue; |
---|
| 287 | } |
---|
| 288 | name[name_offset] = buffer[buffer_offset]; |
---|
| 289 | buffer_offset += 2; |
---|
| 290 | j += 2; |
---|
| 291 | name_offset++; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | buffer_offset += l_rsvd; |
---|
| 295 | j = 0; |
---|
| 296 | |
---|
| 297 | while (j != l_name_3 && !eof ) |
---|
| 298 | { |
---|
| 299 | if ( (buffer[buffer_offset] == 0x00) || |
---|
| 300 | (buffer[buffer_offset] == 0xFF) ) |
---|
| 301 | { |
---|
| 302 | eof = 1; |
---|
| 303 | continue; |
---|
| 304 | } |
---|
| 305 | name[name_offset] = buffer[buffer_offset]; |
---|
| 306 | buffer_offset += 2; |
---|
| 307 | j += 2; |
---|
| 308 | name_offset++; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | name[name_offset] = 0; |
---|
| 312 | |
---|
[602] | 313 | } // end fatfs_get_name_from_long() |
---|
[238] | 314 | |
---|
[602] | 315 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 316 | // This static function analyse the <name> input argument, and returns in other |
---|
| 317 | // output arguments various informations required to store the name in FATFS directory. |
---|
| 318 | // The <name> length cannot be larger than 31 characters : |
---|
| 319 | // - Short name (less than 13 characters) require 1 LFN entry. |
---|
| 320 | // - Medium names (from 14 to 26 characters require 2 LFN entries. |
---|
| 321 | // - Large names (up to 31 characters) require 3 LFN entries. |
---|
| 322 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 323 | // @ name : [in] complete directory entry name. |
---|
| 324 | // @ length : [out] total number of characters in name. |
---|
| 325 | // @ nb_lfn : [out] number of LFN entries required to store the name. |
---|
| 326 | // @ sfn : [out] a legal SFN name extracted from name / upper case and 8-3 format. |
---|
| 327 | // @ checksum : [out] checksum to be stored in SFN. |
---|
| 328 | // @ returns 0 on success / returns 1 if the name length is larger than 31 characters. |
---|
| 329 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 330 | static error_t fatfs_name_format( const char * name, |
---|
| 331 | uint32_t * length, |
---|
| 332 | uint32_t * nb_lfn, |
---|
| 333 | char * sfn, |
---|
| 334 | uint8_t * checksum ) |
---|
| 335 | { |
---|
| 336 | // compute name length |
---|
| 337 | uint32_t name_length = strlen( name ); |
---|
| 338 | *length = name_length; |
---|
[1] | 339 | |
---|
[602] | 340 | uint32_t suffix_length = 0; |
---|
| 341 | uint32_t prefix_length = 0; |
---|
| 342 | uint32_t dot_found = 0; |
---|
| 343 | uint32_t i; |
---|
| 344 | |
---|
| 345 | // compute prefix and suffix length |
---|
| 346 | // only the last '.' is taken into account |
---|
| 347 | for ( i=0 ; i<name_length ; i++ ) |
---|
| 348 | { |
---|
| 349 | if (name[i] == '.' ) |
---|
| 350 | { |
---|
| 351 | if ( dot_found ) |
---|
| 352 | { |
---|
| 353 | prefix_length += suffix_length + 1; |
---|
| 354 | suffix_length = 0; |
---|
| 355 | } |
---|
| 356 | else |
---|
| 357 | { |
---|
| 358 | dot_found = 1; |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | else |
---|
| 362 | { |
---|
| 363 | if ( dot_found) suffix_length++; |
---|
| 364 | else prefix_length++; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | // build SFN prefix (8bits) |
---|
| 369 | if (prefix_length <= 8) |
---|
| 370 | { |
---|
| 371 | for( i=0 ; i<8 ; i++) |
---|
| 372 | { |
---|
| 373 | if ( i<prefix_length ) sfn[i] = to_upper( name[i] ); |
---|
| 374 | else sfn[i] = 0x20; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | else |
---|
| 378 | { |
---|
| 379 | for( i=0 ; i<6 ; i++) |
---|
| 380 | { |
---|
| 381 | sfn[i] = to_upper( name[i] ); |
---|
| 382 | } |
---|
| 383 | sfn[6] = 0x7E; |
---|
| 384 | sfn[7] = 0x31; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // build SFN suffix (3 bits) |
---|
| 388 | if ( suffix_length == 0 ) |
---|
| 389 | { |
---|
| 390 | sfn[8] = 0x20; |
---|
| 391 | sfn[9] = 0x20; |
---|
| 392 | sfn[10] = 0x20; |
---|
| 393 | } |
---|
| 394 | else if ( suffix_length == 1 ) |
---|
| 395 | { |
---|
| 396 | sfn[8] = to_upper( name[name_length-1] ); |
---|
| 397 | sfn[9] = 0x20; |
---|
| 398 | sfn[10] = 0x20; |
---|
| 399 | } |
---|
| 400 | else if ( suffix_length == 2 ) |
---|
| 401 | { |
---|
| 402 | sfn[8] = to_upper( name[name_length-2] ); |
---|
| 403 | sfn[9] = to_upper( name[name_length-1] ); |
---|
| 404 | sfn[10] = 0x20; |
---|
| 405 | } |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | sfn[8] = to_upper( name[name_length-suffix_length] ); |
---|
| 409 | sfn[9] = to_upper( name[name_length-suffix_length+1] ); |
---|
| 410 | sfn[10] = to_upper( name[name_length-suffix_length+2] ); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | // compute 8 bits checksum |
---|
| 414 | uint8_t sum = 0; |
---|
| 415 | for ( i=0 ; i<11 ; i++ ) |
---|
| 416 | { |
---|
| 417 | sum = (((sum & 0x01)<<7) | ((sum & 0xFE)>>1)) + sfn[i]; |
---|
| 418 | } |
---|
| 419 | *checksum = sum; |
---|
| 420 | |
---|
| 421 | // set nb_lfn and length values |
---|
| 422 | if ( name_length <= 13 ) |
---|
| 423 | { |
---|
| 424 | *nb_lfn = 1; |
---|
| 425 | return 0; |
---|
| 426 | } |
---|
| 427 | else if ( name_length <= 26 ) |
---|
| 428 | { |
---|
| 429 | *nb_lfn = 2; |
---|
| 430 | return 0; |
---|
| 431 | } |
---|
| 432 | else if ( name_length <= 31 ) |
---|
| 433 | { |
---|
| 434 | *nb_lfn = 3; |
---|
| 435 | return 0; |
---|
| 436 | } |
---|
| 437 | else |
---|
| 438 | { |
---|
| 439 | return 1; |
---|
| 440 | } |
---|
| 441 | } // end fatfs_name_format() |
---|
| 442 | |
---|
[238] | 443 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 444 | // This static function synchronously updates the FAT on IOC device. |
---|
| 445 | // It scan the FAT mapper to copy on IOC device all dirty pages in the interval |
---|
| 446 | // defined by the <page_min> & <page_max> arguments. |
---|
| 447 | // It can be called by a thread running in any cluster. |
---|
| 448 | // WARNING : We don't take the lock protecting the FAT mapper, because the FAT lock |
---|
| 449 | // (in FATFS context) must be taken by the calling function. |
---|
[626] | 450 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 451 | // @ fatfs_ctx_xp : extended pointer on FATFS context in FAT cluster. |
---|
| 452 | // @ page_min : first page to be checked. |
---|
| 453 | // @ page_max : last page to be checked |
---|
[626] | 454 | // @ return 0 if success, return -1 if the FS_INFO sector cannot be updated. |
---|
| 455 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 456 | static error_t fatfs_update_ioc_fat( xptr_t fatfs_ctx_xp, |
---|
| 457 | uint32_t page_min, |
---|
| 458 | uint32_t page_max ) |
---|
[626] | 459 | { |
---|
[628] | 460 | |
---|
| 461 | #if DEBUG_FATFS_UPDATE_IOC |
---|
| 462 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 463 | thread_t * this = CURRENT_THREAD; |
---|
| 464 | if( DEBUG_FATFS_UPDATE_IOC < cycle ) |
---|
| 465 | printk("\n[%s] thread[%x,%x] enter / page_min %d / page_max %d / cycle %d\n", |
---|
| 466 | __FUNCTION__ , this->process->pid, this->trdid, page_min, page_max, cycle ); |
---|
| 467 | #endif |
---|
| 468 | |
---|
| 469 | error_t error; |
---|
| 470 | cxy_t fat_cxy; // FAT cluster identifier |
---|
| 471 | fatfs_ctx_t * fatfs_ctx; // local pointer on FATFS context in FAT cluster |
---|
| 472 | xptr_t fat_mapper_xp; // extended pointer on FAT mapper |
---|
| 473 | mapper_t * fat_mapper_ptr; // local pointer on FAT mapper |
---|
| 474 | uint32_t page_id; // current page index in FAT mapper |
---|
| 475 | xptr_t rt_xp; // extended pointer on FAT mapper radix tree |
---|
| 476 | xptr_t page_xp; // extended pointer on current page in FAT mapper |
---|
| 477 | page_t * page_ptr; // local pointer on current page |
---|
| 478 | uint32_t flags; // current page flags |
---|
| 479 | |
---|
| 480 | // get pointer and cluster on FATFS context in FAT cluster |
---|
| 481 | fat_cxy = GET_CXY( fatfs_ctx_xp ); |
---|
| 482 | fatfs_ctx = GET_PTR( fatfs_ctx_xp ); |
---|
| 483 | |
---|
| 484 | // get FAT mapper pointers from FATFS context |
---|
| 485 | fat_mapper_xp = hal_remote_l64( XPTR( fat_cxy , &fatfs_ctx->fat_mapper_xp ) ); |
---|
| 486 | fat_mapper_ptr = GET_PTR( fat_mapper_xp ); |
---|
| 487 | |
---|
| 488 | // check FAT cluster |
---|
| 489 | assert( (fat_cxy == GET_CXY( fat_mapper_xp )) , "unconsistent FAT cluster" ); |
---|
| 490 | |
---|
| 491 | // build extended pointer on FAT mapper radix tree |
---|
| 492 | rt_xp = XPTR( fat_cxy , &fat_mapper_ptr->rt ); |
---|
| 493 | |
---|
| 494 | // scan all pages in [min,max] interval |
---|
| 495 | for( page_id = page_min ; page_id <= page_max ; page_id++ ) |
---|
| 496 | { |
---|
| 497 | // get extended pointer on page descriptor from FAT mapper |
---|
| 498 | page_xp = grdxt_remote_lookup( rt_xp , page_id ); |
---|
| 499 | |
---|
| 500 | // check only existing pages |
---|
| 501 | if ( page_xp != XPTR_NULL ) |
---|
| 502 | { |
---|
| 503 | page_ptr = GET_PTR( page_xp ); |
---|
| 504 | flags = hal_remote_l32( XPTR( fat_cxy , &page_ptr->flags ) ); |
---|
| 505 | |
---|
| 506 | // copy only dirty pages |
---|
| 507 | if ( flags & PG_DIRTY ) |
---|
| 508 | { |
---|
| 509 | |
---|
| 510 | #if (DEBUG_FATFS_UPDATE_IOC & 1) |
---|
| 511 | if( DEBUG_FATFS_UPDATE_IOC < cycle ) |
---|
| 512 | printk("\n[%s] thread[%x,%x] copy page %d from FAT mapper to IOC device\n", |
---|
| 513 | __FUNCTION__, page_id ); |
---|
| 514 | #endif |
---|
| 515 | // move page from mapper to device |
---|
| 516 | error = fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
| 517 | |
---|
| 518 | if ( error ) return -1; |
---|
| 519 | |
---|
| 520 | // reset page dirty flag |
---|
| 521 | ppm_page_undo_dirty( page_xp ); |
---|
| 522 | } |
---|
| 523 | } |
---|
| 524 | } // end loop on pages |
---|
| 525 | |
---|
| 526 | #if DEBUG_FATFS_UPDATE_IOC |
---|
| 527 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 528 | if( DEBUG_FATFS_UPDATE_IOC < cycle ) |
---|
| 529 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 530 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
| 531 | #endif |
---|
| 532 | |
---|
| 533 | return 0; |
---|
| 534 | |
---|
| 535 | } // end fatfs_update_ioc_fat() |
---|
| 536 | |
---|
| 537 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 538 | // This static function synchronously updates the FS_INFO sector on IOC device, |
---|
| 539 | // from values contained in the FATFS context in FAT cluster. |
---|
| 540 | // It uses and updates the FS_INFO buffer allocated in the FAT cluster. |
---|
| 541 | // It can be called by a thread running in any cluster. |
---|
| 542 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 543 | // @ fatfs_ctx_xp : extended pointer on FATFS context in FAT cluster. |
---|
| 544 | // @ return 0 if success, return -1 if the FS_INFO sector cannot be updated. |
---|
| 545 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 546 | static error_t fatfs_update_ioc_fsinfo( xptr_t fatfs_ctx_xp ) |
---|
| 547 | { |
---|
[626] | 548 | cxy_t fat_cxy; // FAT cluster identifier |
---|
| 549 | fatfs_ctx_t * fatfs_ctx_ptr; // local pointer on fatfs context in FAT cluster |
---|
[628] | 550 | uint32_t free_clusters; // current vale of "free_clusters" in fatfs context |
---|
| 551 | uint32_t free_cluster_hint; // current vale of "free_cluster_hint" in fatfs context |
---|
[626] | 552 | uint8_t * fs_info_buffer_ptr; // local pointer on FS_INFO buffer in FAT cluster |
---|
| 553 | xptr_t fs_info_buffer_xp; // extended pointer on FS_INFO buffer in FAT cluster |
---|
| 554 | uint32_t fs_info_lba; // FS_INFO sector lba on IOC device |
---|
| 555 | |
---|
| 556 | // get cluster and local pointer on FAT cluster context |
---|
| 557 | fat_cxy = GET_CXY( fatfs_ctx_xp ); |
---|
| 558 | fatfs_ctx_ptr = GET_PTR( fatfs_ctx_xp ); |
---|
| 559 | |
---|
[628] | 560 | // force FATFS context update |
---|
| 561 | hal_fence(); |
---|
| 562 | |
---|
| 563 | // get relevant info from fatfs context in FAT cluster |
---|
| 564 | fs_info_lba = hal_remote_l32( XPTR( fat_cxy , &fatfs_ctx_ptr->fs_info_lba ) ); |
---|
| 565 | free_clusters = hal_remote_l32( XPTR( fat_cxy , &fatfs_ctx_ptr->free_clusters ) ); |
---|
| 566 | free_cluster_hint = hal_remote_l32( XPTR( fat_cxy , &fatfs_ctx_ptr->free_cluster_hint ) ); |
---|
[626] | 567 | fs_info_buffer_ptr = hal_remote_lpt( XPTR( fat_cxy , &fatfs_ctx_ptr->fs_info_buffer ) ); |
---|
[628] | 568 | |
---|
| 569 | // build extended pointer on FS_INFO buffer in FAT cluster |
---|
[626] | 570 | fs_info_buffer_xp = XPTR( fat_cxy , fs_info_buffer_ptr ); |
---|
[628] | 571 | |
---|
[626] | 572 | // update the FS_INFO buffer in FAT cluster |
---|
| 573 | fatfs_set_remote_record( FS_FREE_CLUSTERS , fs_info_buffer_xp , free_clusters ); |
---|
| 574 | fatfs_set_remote_record( FS_FREE_CLUSTER_HINT , fs_info_buffer_xp , free_cluster_hint ); |
---|
| 575 | |
---|
| 576 | // update the FS_INFO sector on IOC device |
---|
| 577 | return dev_ioc_sync_write( fs_info_buffer_xp , fs_info_lba , 1 ); |
---|
| 578 | |
---|
[628] | 579 | } // end fatfs_update_ioc_fsinfo() |
---|
[626] | 580 | |
---|
| 581 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 582 | // This static function decrements the "free_clusters" variable, and updates the |
---|
[626] | 583 | // "free_cluster_hint" variable in the FATFS context in FAT cluster, identified |
---|
| 584 | // by the <fat_ctx_xp> argument, when a new <cluster> has been allocated from FAT. |
---|
[625] | 585 | // It scan all slots in the FAT mapper seen as an array of 32 bits words, looking for the |
---|
| 586 | // first free slot larger than the <cluster> argument, to update "free_cluster_hint". |
---|
[628] | 587 | // It synchronously updates the FS_INFO sector on the IOC device. |
---|
| 588 | // It can be called by a thead running in any cluster. |
---|
| 589 | // The lock protecting exclusive access to the FAT must be taken by the calling function. |
---|
[602] | 590 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 591 | // @ fatfs_ctx_xp : extended pointer on FATFS context in FAT cluster. |
---|
| 592 | // @ cluster : recently allocated cluster index in FAT. |
---|
| 593 | // @ return 0 if success, return -1 if the FS_INFO sector cannot be updated. |
---|
[602] | 594 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 595 | static error_t fatfs_free_clusters_decrement( xptr_t fatfs_ctx_xp, |
---|
| 596 | uint32_t cluster ) |
---|
[602] | 597 | { |
---|
[626] | 598 | error_t error; |
---|
[628] | 599 | cxy_t fat_cxy; // FAT cluster identifier |
---|
| 600 | fatfs_ctx_t * fat_ctx_ptr; // local pointer on fatfs context in FAT cluster |
---|
| 601 | xptr_t fat_mapper_xp; // extended pointer on FAT mapper |
---|
| 602 | xptr_t hint_xp; // extended pointer on "free_cluster_hint" shared variable |
---|
| 603 | xptr_t numb_xp; // extended pointer on "free_clusters" shared variable |
---|
| 604 | uint32_t numb; // "free_clusters" variable current value |
---|
| 605 | uint32_t hint; // "free_cluster_hint" variable current value |
---|
| 606 | uint32_t page_id; // page index in FAT mapper |
---|
| 607 | uint32_t slot_id; // slot index in one page of FAT (1024 slots per page) |
---|
| 608 | uint32_t page_max; // max number of pages in FAT mapper |
---|
| 609 | xptr_t page_xp; // extended pointer on current page in FAT mapper |
---|
| 610 | xptr_t base_xp; // extended pointer on current page base |
---|
| 611 | xptr_t slot_xp; // extended pointer on current slot in FAT mapper |
---|
[602] | 612 | |
---|
| 613 | #if DEBUG_FATFS_FREE_CLUSTERS |
---|
| 614 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 615 | thread_t * this = CURRENT_THREAD; |
---|
[626] | 616 | if( DEBUG_FATFS_FREE_CLUSTERS < cycle ) |
---|
[602] | 617 | printk("\n[%s] thread[%x,%x] enter for allocated cluster %x / cycle %d\n", |
---|
| 618 | __FUNCTION__, this->process->pid, this->trdid, cluster , cycle ); |
---|
| 619 | #endif |
---|
| 620 | |
---|
[626] | 621 | // get FAT cluster an local pointer on fatfs context in FAT cluster |
---|
| 622 | fat_cxy = GET_CXY( fatfs_ctx_xp ); |
---|
| 623 | fat_ctx_ptr = GET_PTR( fatfs_ctx_xp ); |
---|
| 624 | |
---|
| 625 | // build extended pointers on free_clusters, and free_cluster_hint in fatfs context |
---|
| 626 | hint_xp = XPTR( fat_cxy , &fat_ctx_ptr->free_cluster_hint ); |
---|
| 627 | numb_xp = XPTR( fat_cxy , &fat_ctx_ptr->free_clusters ); |
---|
[602] | 628 | |
---|
[626] | 629 | // update "free_clusters" value |
---|
| 630 | numb = hal_remote_l32( numb_xp ) - 1; |
---|
| 631 | hal_remote_s32( numb_xp , numb ); |
---|
[602] | 632 | |
---|
[625] | 633 | // get extended pointer on FAT mapper |
---|
[628] | 634 | fat_mapper_xp = hal_remote_l64( XPTR( fat_cxy , &fat_ctx_ptr->fat_mapper_xp ) ); |
---|
[625] | 635 | |
---|
| 636 | // initialise variables to scan the FAT mapper |
---|
| 637 | // and find the first free slot > cluster |
---|
[602] | 638 | page_id = (cluster + 1) >> 10; |
---|
| 639 | slot_id = (cluster + 1) & 0x3FF; |
---|
[626] | 640 | page_max = hal_remote_l32( XPTR( fat_cxy, &fat_ctx_ptr->fat_sectors_count ) ) >> 3; |
---|
[602] | 641 | |
---|
| 642 | // scan FAT mapper / loop on pages |
---|
| 643 | while ( page_id < page_max ) |
---|
| 644 | { |
---|
| 645 | // get current page from mapper |
---|
[628] | 646 | page_xp = mapper_remote_get_page( fat_mapper_xp , page_id ); |
---|
[602] | 647 | |
---|
| 648 | if( page_xp == XPTR_NULL ) |
---|
| 649 | { |
---|
| 650 | printk("\n[ERROR] in %s : cannot access FAT mapper\n", __FUNCTION__ ); |
---|
| 651 | return -1; |
---|
| 652 | } |
---|
| 653 | |
---|
[625] | 654 | // get extended pointer on page |
---|
| 655 | base_xp = ppm_page2base( page_xp ); |
---|
| 656 | |
---|
[602] | 657 | // scan FAT mapper / loop on slots |
---|
| 658 | while ( slot_id < 1024 ) |
---|
| 659 | { |
---|
| 660 | // get extended pointer on current slot |
---|
[625] | 661 | slot_xp = base_xp + (slot_id << 2); |
---|
[602] | 662 | |
---|
[625] | 663 | // test slot value |
---|
[602] | 664 | if ( hal_remote_l32( slot_xp ) == FREE_CLUSTER ) |
---|
| 665 | { |
---|
[626] | 666 | // update "free_cluster_hint" value |
---|
| 667 | hint = (page_id << 10) + slot_id - 1; |
---|
| 668 | hal_remote_s32( hint_xp , hint ); |
---|
[602] | 669 | |
---|
[626] | 670 | // update FS_INFO sector on IOC device |
---|
[628] | 671 | error = fatfs_update_ioc_fat( fatfs_ctx_xp, |
---|
| 672 | page_id, |
---|
| 673 | page_id ); |
---|
[626] | 674 | |
---|
| 675 | if( error ) |
---|
| 676 | { |
---|
| 677 | printk("\n[ERROR] in %s : cannot update FS_INFO on IOC\n", __FUNCTION__ ); |
---|
| 678 | return -1; |
---|
| 679 | } |
---|
| 680 | |
---|
[602] | 681 | #if DEBUG_FATFS_FREE_CLUSTERS |
---|
| 682 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 683 | if( DEBUG_FATFS_FREE_CLUSTERS < (uint32_t)hal_get_cycles() ) |
---|
[628] | 684 | printk("\n[%s] thread[%x,%x] exit / hint %x / free %x / cycle %d\n", |
---|
[626] | 685 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[628] | 686 | hal_remote_l32(hint_xp), hal_remote_l32(numb_xp), cycle ); |
---|
[602] | 687 | #endif |
---|
| 688 | return 0; |
---|
| 689 | } |
---|
| 690 | |
---|
[625] | 691 | // update slot_id |
---|
| 692 | slot_id = 0; |
---|
[602] | 693 | |
---|
| 694 | } // end loop on slots |
---|
| 695 | |
---|
[626] | 696 | // update page_id & slot_id variables |
---|
[602] | 697 | page_id++; |
---|
| 698 | slot_id = 0; |
---|
| 699 | |
---|
| 700 | } // end loop on pages |
---|
| 701 | |
---|
| 702 | // return error if no free cluster found |
---|
| 703 | printk("\n[ERROR] in %s : No free cluster found\n", __FUNCTION__ ); |
---|
| 704 | return -1; |
---|
| 705 | |
---|
| 706 | } // end fatfs_free_clusters_decrement() |
---|
| 707 | |
---|
| 708 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 709 | // This static function increments the "free_clusters" variable, and updates the |
---|
[626] | 710 | // "free_cluster_hint" variables in the FATFS context in FAT cluster, identified |
---|
[628] | 711 | // by the <fat_ctx_xp> argument, when a FATFS cluster is released. |
---|
[625] | 712 | // If the released cluster index is smaller than the current (hint) value, |
---|
| 713 | // it set "free_cluster_hint" <= cluster. |
---|
[628] | 714 | // It does NOT update the FS_INFO sector on the IOC device. |
---|
| 715 | // It can be called by a thead running in any cluster. |
---|
| 716 | // The lock protecting exclusive access to the FAT must be taken by the calling function. |
---|
[602] | 717 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 718 | // @ fatfs_ctx_xp : extended pointer on FATFS context in FAT cluster. |
---|
| 719 | // @ cluster : recently released cluster index in FAT. |
---|
| 720 | // @ return 0 if success, return -1 if the FS_INFO sector cannot be updated. |
---|
[602] | 721 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[626] | 722 | static error_t fatfs_free_clusters_increment( xptr_t fatfs_ctx_xp, |
---|
| 723 | uint32_t cluster ) |
---|
[602] | 724 | { |
---|
[626] | 725 | error_t error; |
---|
| 726 | cxy_t fat_cxy; // FAT cluster identifier |
---|
| 727 | fatfs_ctx_t * fat_ctx_ptr; // local pointer on fatfs context in FAT cluster |
---|
[602] | 728 | xptr_t hint_xp; // extended pointer on "free_cluster_hint" shared variable |
---|
| 729 | xptr_t numb_xp; // extended pointer on "free_clusters" shared variable |
---|
| 730 | uint32_t hint; // "free_cluster_hint" variable current value |
---|
| 731 | uint32_t numb; // "free_clusters" variable current value |
---|
| 732 | |
---|
[626] | 733 | #if DEBUG_FATFS_FREE_CLUSTERS |
---|
| 734 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 735 | thread_t * this = CURRENT_THREAD; |
---|
| 736 | if( DEBUG_FATFS_FREE_CLUSTERS < cycle ) |
---|
| 737 | printk("\n[%s] thread[%x,%x] enter for released cluster %x / cycle %d\n", |
---|
| 738 | __FUNCTION__, this->process->pid, this->trdid, cluster , cycle ); |
---|
| 739 | #endif |
---|
| 740 | |
---|
| 741 | // get FAT cluster an local pointer on fatfs context in FAT cluster |
---|
| 742 | fat_cxy = GET_CXY( fatfs_ctx_xp ); |
---|
| 743 | fat_ctx_ptr = GET_PTR( fatfs_ctx_xp ); |
---|
| 744 | |
---|
[625] | 745 | // build extended pointers on free_clusters, and free_cluster_hint |
---|
[626] | 746 | hint_xp = XPTR( fat_cxy , &fat_ctx_ptr->free_cluster_hint ); |
---|
| 747 | numb_xp = XPTR( fat_cxy , &fat_ctx_ptr->free_clusters ); |
---|
[602] | 748 | |
---|
| 749 | // get current value of free_cluster_hint and free_clusters |
---|
| 750 | hint = hal_remote_l32( hint_xp ); |
---|
| 751 | numb = hal_remote_l32( numb_xp ); |
---|
| 752 | |
---|
[626] | 753 | // update "numb" and "hint" variables as required |
---|
| 754 | numb++; |
---|
| 755 | if ( (cluster - 1) < hint ) hint = cluster - 1; |
---|
[602] | 756 | |
---|
| 757 | // update free_clusters |
---|
[626] | 758 | hal_remote_s32( numb_xp , numb ); |
---|
| 759 | hal_remote_s32( hint_xp , hint ); |
---|
[602] | 760 | |
---|
[626] | 761 | // update FS_INFO sector on IOC device |
---|
[628] | 762 | error = fatfs_update_ioc_fsinfo( fatfs_ctx_xp ); |
---|
[626] | 763 | |
---|
| 764 | if( error ) |
---|
| 765 | { |
---|
| 766 | printk("\n[ERROR] in %s : cannot update FS_INFO on IOC\n", __FUNCTION__ ); |
---|
| 767 | return -1; |
---|
| 768 | } |
---|
| 769 | |
---|
[602] | 770 | #if DEBUG_FATFS_FREE_CLUSTERS |
---|
[628] | 771 | cycle = (uint32_t)hal_get_cycles(); |
---|
[602] | 772 | if( DEBUG_FATFS_FREE_CLUSTERS < (uint32_t)hal_get_cycles() ) |
---|
[628] | 773 | printk("\n[%s] thread[%x,%x] exit / hint %x / free %x / cycle %d\n", |
---|
[602] | 774 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[628] | 775 | hal_remote_l32( hint_xp ), hal_remote_l32( numb_xp ), cycle ); |
---|
[602] | 776 | #endif |
---|
| 777 | |
---|
[626] | 778 | return 0; |
---|
| 779 | |
---|
[602] | 780 | } // end fatfs_free_clusters_increment() |
---|
| 781 | |
---|
| 782 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 783 | // This recursive function is called by the generic function fatfs_release_inode(). |
---|
| 784 | // It release all FATFS clusters allocated to a given inode to the FAT mapper. |
---|
| 785 | // It can be called by a thread running in any cluster, as it use remote pointers |
---|
| 786 | // to access both the FAT mapper and the FATFS context in the FAT cluster, defined |
---|
| 787 | // by the <fat_mapper_xp> and <fatfs_ctx_xp> arguments. |
---|
| 788 | // The removal is done in reverse order of the linked list (from last cluster to first). |
---|
| 789 | // It returns in the <dirty_page_min> and <dirty_page_max> buffers the indexes of the |
---|
| 790 | // modified pages in the FAT mapper. |
---|
| 791 | // It updates the FAT mapper and the free_cluster info in the FATFS context, but does NOT |
---|
| 792 | // update the FAT and the FS_INFO on IOC device. |
---|
[602] | 793 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 794 | // @ fat_mapper_xp : [in] extended pointer on FAT mapper. |
---|
| 795 | // @ fatfs_ctx_xp : [in] extended pointer on FATFS context in FAT cluster. |
---|
| 796 | // @ cluster : [in] index of cluster to be released from FAT mapper. |
---|
| 797 | // @ dirty_page_min : [out] pointer on buffer for min dirty page index. |
---|
| 798 | // @ dirty_page_max : [out] pointer on buffer for max dirty page index. |
---|
| 799 | // @ return 0 if success / return -1 if error. |
---|
[602] | 800 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[628] | 801 | static error_t fatfs_recursive_release( xptr_t fat_mapper_xp, |
---|
| 802 | xptr_t fatfs_ctx_xp, |
---|
| 803 | uint32_t cluster, |
---|
| 804 | uint32_t * dirty_page_min, |
---|
| 805 | uint32_t * dirty_page_max ) |
---|
[602] | 806 | { |
---|
[628] | 807 | uint32_t next; // next cluster index |
---|
| 808 | uint32_t page_id; // page index in FAT mapper |
---|
| 809 | uint32_t word_id; // word index in page |
---|
[602] | 810 | |
---|
[628] | 811 | // get page index and word index from cluster |
---|
| 812 | page_id = cluster >> 10; |
---|
| 813 | word_id = cluster & 0x3FF; |
---|
[602] | 814 | |
---|
[625] | 815 | // get next cluster index from FAT mapper |
---|
[628] | 816 | if ( mapper_remote_get_32( fat_mapper_xp, |
---|
| 817 | page_id, |
---|
| 818 | word_id, |
---|
[625] | 819 | &next ) ) return -1; |
---|
| 820 | |
---|
[602] | 821 | #if (DEBUG_FATFS_RELEASE_INODE & 1) |
---|
| 822 | thread_t * this = CURRENT_THREAD; |
---|
| 823 | if ( DEBUG_FATFS_RELEASE_INODE < (uint32_t)hal_get_cycles() ) |
---|
| 824 | printk("\n[%s] thread[%x,%x] access FAT for cluster %x / next %x\n", |
---|
| 825 | __FUNCTION__, this->process->pid, this->trdid, cluster, next ); |
---|
| 826 | #endif |
---|
| 827 | |
---|
| 828 | if ( next < END_OF_CHAIN_CLUSTER_MIN ) // non terminal case |
---|
| 829 | { |
---|
| 830 | // call fatfs_recursive_release() on next cluster |
---|
[628] | 831 | if ( fatfs_recursive_release( fat_mapper_xp, |
---|
| 832 | fatfs_ctx_xp, |
---|
| 833 | next, |
---|
| 834 | dirty_page_min, |
---|
| 835 | dirty_page_max ) ) return -1; |
---|
[602] | 836 | } |
---|
| 837 | |
---|
[628] | 838 | // update FAT mapper |
---|
| 839 | if ( mapper_remote_set_32( fat_mapper_xp, |
---|
| 840 | page_id, |
---|
| 841 | word_id, |
---|
[625] | 842 | FREE_CLUSTER ) ) return -1; |
---|
[602] | 843 | |
---|
[628] | 844 | // update dirty_page_min / dirty_page_max buffers |
---|
| 845 | if( page_id < *dirty_page_min ) *dirty_page_min = page_id; |
---|
| 846 | if( page_id > *dirty_page_max ) *dirty_page_max = page_id; |
---|
[602] | 847 | |
---|
[628] | 848 | // Update free_cluster info in FATFS context |
---|
| 849 | return fatfs_free_clusters_increment( fatfs_ctx_xp , cluster ); |
---|
| 850 | |
---|
[602] | 851 | } // end fatfs_recursive_release() |
---|
| 852 | |
---|
| 853 | |
---|
| 854 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[568] | 855 | // FATFS specific extern functions |
---|
[238] | 856 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 857 | |
---|
[626] | 858 | /////////////////////////////////// |
---|
| 859 | void fatfs_display_ctx( cxy_t cxy ) |
---|
[265] | 860 | { |
---|
[626] | 861 | // get pointer on local FATFS context |
---|
| 862 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 863 | fatfs_ctx_t * ctx = hal_remote_lpt( XPTR( cxy , &vfs_ctx->extend ) ); |
---|
| 864 | |
---|
| 865 | uint32_t fat_sectors = hal_remote_l32( XPTR( cxy , &ctx->fat_sectors_count ) ); |
---|
| 866 | uint32_t sector_size = hal_remote_l32( XPTR( cxy , &ctx->bytes_per_sector ) ); |
---|
| 867 | uint32_t sec_per_clus = hal_remote_l32( XPTR( cxy , &ctx->sectors_per_cluster ) ); |
---|
| 868 | uint32_t fat_lba = hal_remote_l32( XPTR( cxy , &ctx->fat_begin_lba ) ); |
---|
| 869 | uint32_t data_lba = hal_remote_l32( XPTR( cxy , &ctx->cluster_begin_lba ) ); |
---|
| 870 | uint32_t fsinfo_lba = hal_remote_l32( XPTR( cxy , &ctx->fs_info_lba ) ); |
---|
| 871 | uint32_t root_dir_clus = hal_remote_l32( XPTR( cxy , &ctx->root_dir_cluster ) ); |
---|
| 872 | uint32_t free_clusters = hal_remote_l32( XPTR( cxy , &ctx->free_clusters ) ); |
---|
| 873 | uint32_t free_cluster_hint = hal_remote_l32( XPTR( cxy , &ctx->free_cluster_hint ) ); |
---|
| 874 | xptr_t mapper_xp = hal_remote_l64( XPTR( cxy , &ctx->fat_mapper_xp ) ); |
---|
| 875 | void * fs_info_buffer = hal_remote_lpt( XPTR( cxy , &ctx->fs_info_buffer ) ); |
---|
| 876 | |
---|
| 877 | printk("\n*** FAT context in cluster %x\n" |
---|
[602] | 878 | "- fat_sectors = %d\n" |
---|
| 879 | "- sector size = %d\n" |
---|
| 880 | "- cluster size = %d\n" |
---|
[626] | 881 | "- fat_lba = %x\n" |
---|
| 882 | "- data_lba = %x\n" |
---|
| 883 | "- fsinfo_lba = %x\n" |
---|
| 884 | "- root_dir_cluster = %x\n" |
---|
| 885 | "- free_clusters = %x\n" |
---|
| 886 | "- free_cluster_hint = %x\n" |
---|
| 887 | "- fat_mapper_ptr = %x\n" |
---|
| 888 | "- fs_info_buffer = %x\n", |
---|
| 889 | cxy, |
---|
| 890 | fat_sectors, |
---|
| 891 | sector_size, |
---|
| 892 | sector_size * sec_per_clus, |
---|
| 893 | fat_lba, |
---|
| 894 | data_lba, |
---|
| 895 | fsinfo_lba, |
---|
| 896 | root_dir_clus, |
---|
| 897 | free_clusters, |
---|
| 898 | free_cluster_hint, |
---|
| 899 | GET_PTR( mapper_xp ), |
---|
| 900 | fs_info_buffer ); |
---|
[265] | 901 | |
---|
[626] | 902 | } // end fatfs_ctx_display() |
---|
[602] | 903 | |
---|
| 904 | ////////////////////////////////////////// |
---|
| 905 | void fatfs_display_fat( uint32_t page_id, |
---|
| 906 | uint32_t nentries ) |
---|
| 907 | { |
---|
| 908 | uint32_t line; |
---|
| 909 | uint32_t maxline; |
---|
| 910 | |
---|
[626] | 911 | // compute number of lines to display |
---|
[602] | 912 | maxline = nentries >> 3; |
---|
| 913 | if( nentries & 0x7 ) maxline++; |
---|
| 914 | |
---|
| 915 | // get pointer on local FATFS context |
---|
[626] | 916 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 917 | fatfs_ctx_t * loc_fatfs_ctx = (fatfs_ctx_t *)vfs_ctx->extend; |
---|
[602] | 918 | |
---|
| 919 | // get extended pointer on FAT mapper |
---|
[626] | 920 | xptr_t fat_mapper_xp = loc_fatfs_ctx->fat_mapper_xp; |
---|
[602] | 921 | |
---|
[626] | 922 | // get FAT cluster identifier |
---|
| 923 | cxy_t fat_cxy = GET_CXY( fat_mapper_xp ); |
---|
| 924 | |
---|
| 925 | // get pointer on FATFS context in FAT cluster |
---|
| 926 | fatfs_ctx_t * fat_fatfs_ctx = hal_remote_lpt( XPTR( fat_cxy , &vfs_ctx->extend ) ); |
---|
| 927 | |
---|
| 928 | // get current value of hint and free_clusters |
---|
| 929 | uint32_t hint = hal_remote_l32( XPTR( fat_cxy , &fat_fatfs_ctx->free_cluster_hint ) ); |
---|
| 930 | uint32_t free = hal_remote_l32( XPTR( fat_cxy , &fat_fatfs_ctx->free_clusters ) ); |
---|
| 931 | |
---|
| 932 | // get extended pointer on requested page in FAT mapper |
---|
[602] | 933 | xptr_t page_xp = mapper_remote_get_page( fat_mapper_xp , page_id ); |
---|
| 934 | |
---|
| 935 | // get extended pointer on requested page base |
---|
| 936 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[626] | 937 | void * base = GET_PTR( base_xp ); |
---|
[602] | 938 | |
---|
[626] | 939 | printk("\n***** FAT mapper / cxy %x / page_id %d / base %x / free_clusters %x / hint %x\n", |
---|
| 940 | fat_cxy, page_id, base, free, hint ); |
---|
| 941 | |
---|
[602] | 942 | for( line = 0 ; line < maxline ; line++ ) |
---|
| 943 | { |
---|
| 944 | printk("%x : %X | %X | %X | %X | %X | %X | %X | %X\n", (line<<3), |
---|
| 945 | hal_remote_l32( base_xp + ((line<<5) ) ), |
---|
| 946 | hal_remote_l32( base_xp + ((line<<5) + 4 ) ), |
---|
| 947 | hal_remote_l32( base_xp + ((line<<5) + 8 ) ), |
---|
| 948 | hal_remote_l32( base_xp + ((line<<5) + 12 ) ), |
---|
| 949 | hal_remote_l32( base_xp + ((line<<5) + 16 ) ), |
---|
| 950 | hal_remote_l32( base_xp + ((line<<5) + 20 ) ), |
---|
| 951 | hal_remote_l32( base_xp + ((line<<5) + 24 ) ), |
---|
| 952 | hal_remote_l32( base_xp + ((line<<5) + 28 ) ) ); |
---|
| 953 | } |
---|
| 954 | |
---|
| 955 | } // end fatfs_display_fat() |
---|
| 956 | |
---|
| 957 | /////////////////////////////////////////////////////// |
---|
| 958 | error_t fatfs_get_cluster( uint32_t first_cluster_id, |
---|
[406] | 959 | uint32_t searched_page_index, |
---|
[265] | 960 | uint32_t * searched_cluster_id ) |
---|
[238] | 961 | { |
---|
[602] | 962 | xptr_t current_page_xp; // pointer on current page descriptor |
---|
| 963 | uint32_t * buffer; // pointer on current page (array of uint32_t) |
---|
[406] | 964 | uint32_t current_page_index; // index of current page in FAT |
---|
[625] | 965 | uint32_t current_slot_index; // index of slot in current page |
---|
[238] | 966 | uint32_t page_count_in_file; // index of page in file (index in linked list) |
---|
[406] | 967 | uint32_t next_cluster_id; // content of current FAT slot |
---|
[627] | 968 | xptr_t lock_xp; // extended pointer on FAT lock |
---|
[1] | 969 | |
---|
[602] | 970 | assert( (searched_page_index > 0) , |
---|
| 971 | "no FAT access required for first page\n"); |
---|
[246] | 972 | |
---|
[438] | 973 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[602] | 974 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 975 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 976 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[625] | 977 | printk("\n[%s] thread[%x,%x] enter / first_cluster_id %d / searched_index %d / cycle %d\n", |
---|
[602] | 978 | __FUNCTION__, this->process->pid, this->trdid, first_cluster_id, searched_page_index, cycle ); |
---|
[435] | 979 | #endif |
---|
[265] | 980 | |
---|
[627] | 981 | // get local pointer on VFS context (same in all clusters) |
---|
| 982 | vfs_ctx_t * vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 983 | |
---|
[602] | 984 | // get local pointer on local FATFS context |
---|
[627] | 985 | fatfs_ctx_t * loc_fatfs_ctx = vfs_ctx->extend; |
---|
[1] | 986 | |
---|
[602] | 987 | // get extended pointer and cluster on FAT mapper |
---|
[627] | 988 | xptr_t fat_mapper_xp = loc_fatfs_ctx->fat_mapper_xp; |
---|
| 989 | cxy_t fat_cxy = GET_CXY( fat_mapper_xp ); |
---|
[602] | 990 | |
---|
[627] | 991 | // get local pointer on FATFS context in FAT cluster |
---|
| 992 | fatfs_ctx_t * fat_fatfs_ctx = hal_remote_lpt( XPTR( fat_cxy , &vfs_ctx->extend ) ); |
---|
| 993 | |
---|
| 994 | // build extended pointer on FAT lock in FAT cluster |
---|
| 995 | lock_xp = XPTR( fat_cxy , &fat_fatfs_ctx->lock ); |
---|
| 996 | |
---|
| 997 | // take FAT lock in read mode |
---|
| 998 | remote_rwlock_rd_acquire( lock_xp ); |
---|
| 999 | |
---|
[602] | 1000 | // initialize loop variable (1024 slots per page) |
---|
| 1001 | current_page_index = first_cluster_id >> 10; |
---|
[625] | 1002 | current_slot_index = first_cluster_id & 0x3FF; |
---|
[238] | 1003 | page_count_in_file = 0; |
---|
[406] | 1004 | next_cluster_id = 0xFFFFFFFF; |
---|
[238] | 1005 | |
---|
[625] | 1006 | // scan FAT mapper (i.e. traverse FAT linked list) |
---|
[406] | 1007 | while( page_count_in_file < searched_page_index ) |
---|
[238] | 1008 | { |
---|
[625] | 1009 | // get pointer on current page descriptor in FAT mapper |
---|
| 1010 | current_page_xp = mapper_remote_get_page( fat_mapper_xp , current_page_index ); |
---|
[238] | 1011 | |
---|
[602] | 1012 | if( current_page_xp == XPTR_NULL ) |
---|
| 1013 | { |
---|
[625] | 1014 | printk("\n[ERROR] in %s : cannot get next page from FAT mapper\n", __FUNCTION__); |
---|
[627] | 1015 | remote_rwlock_rd_release( lock_xp ); |
---|
[602] | 1016 | return -1; |
---|
| 1017 | } |
---|
[238] | 1018 | |
---|
| 1019 | // get pointer on buffer for current page |
---|
[602] | 1020 | xptr_t base_xp = ppm_page2base( current_page_xp ); |
---|
| 1021 | buffer = (uint32_t *)GET_PTR( base_xp ); |
---|
[238] | 1022 | |
---|
| 1023 | // get FAT slot content |
---|
[627] | 1024 | next_cluster_id = hal_remote_l32( XPTR( fat_cxy, &buffer[current_slot_index] ) ); |
---|
[238] | 1025 | |
---|
[438] | 1026 | #if (DEBUG_FATFS_GET_CLUSTER & 1) |
---|
| 1027 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[602] | 1028 | printk("\n[%s] traverse FAT / current_page_index = %d\n" |
---|
[625] | 1029 | "current_slot_index = %d / next_cluster_id = %d\n", |
---|
| 1030 | __FUNCTION__, current_page_index, current_slot_index , next_cluster_id ); |
---|
[435] | 1031 | #endif |
---|
[238] | 1032 | // update loop variables |
---|
[625] | 1033 | current_page_index = next_cluster_id >> 10; |
---|
| 1034 | current_slot_index = next_cluster_id & 0x3FF; |
---|
[238] | 1035 | page_count_in_file++; |
---|
| 1036 | } |
---|
[246] | 1037 | |
---|
[625] | 1038 | if( next_cluster_id == 0xFFFFFFFF ) |
---|
| 1039 | { |
---|
| 1040 | printk("\n[ERROR] in %s : searched_cluster_id not found in FAT\n", __FUNCTION__ ); |
---|
[627] | 1041 | remote_rwlock_rd_release( lock_xp ); |
---|
[625] | 1042 | return -1; |
---|
| 1043 | } |
---|
[406] | 1044 | |
---|
[627] | 1045 | // release FAT lock |
---|
| 1046 | remote_rwlock_rd_release( lock_xp ); |
---|
| 1047 | |
---|
[438] | 1048 | #if DEBUG_FATFS_GET_CLUSTER |
---|
[435] | 1049 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1050 | if( DEBUG_FATFS_GET_CLUSTER < cycle ) |
---|
[602] | 1051 | printk("\n[%s] thread[%x,%x] exit / searched_cluster_id = %d / cycle %d\n", |
---|
| 1052 | __FUNCTION__, this->process->pid, this->trdid, next_cluster_id / cycle ); |
---|
[435] | 1053 | #endif |
---|
[406] | 1054 | |
---|
| 1055 | *searched_cluster_id = next_cluster_id; |
---|
[238] | 1056 | return 0; |
---|
| 1057 | |
---|
| 1058 | } // end fatfs_get_cluster() |
---|
| 1059 | |
---|
| 1060 | |
---|
| 1061 | |
---|
[1] | 1062 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[602] | 1063 | // Generic API : the following functions are called by the kernel VFS |
---|
[188] | 1064 | // and must be defined by all supported file systems. |
---|
[1] | 1065 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1066 | |
---|
[568] | 1067 | ///////////////////////////////////// |
---|
[484] | 1068 | fatfs_ctx_t * fatfs_ctx_alloc( void ) |
---|
[1] | 1069 | { |
---|
[23] | 1070 | kmem_req_t req; |
---|
[635] | 1071 | req.type = KMEM_KCM; |
---|
| 1072 | req.order = bits_log2( sizeof(fatfs_ctx_t) ); |
---|
[188] | 1073 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[1] | 1074 | |
---|
[635] | 1075 | return kmem_alloc( &req ); |
---|
[188] | 1076 | } |
---|
[23] | 1077 | |
---|
[188] | 1078 | ////////////////////////////////////////////// |
---|
| 1079 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ) |
---|
| 1080 | { |
---|
| 1081 | error_t error; |
---|
| 1082 | kmem_req_t req; |
---|
| 1083 | uint8_t * buffer; |
---|
[626] | 1084 | xptr_t buffer_xp; |
---|
[23] | 1085 | |
---|
[602] | 1086 | #if DEBUG_FATFS_CTX_INIT |
---|
[625] | 1087 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1088 | thread_t * this = CURRENT_THREAD; |
---|
[602] | 1089 | if( DEBUG_FATFS_CTX_INIT < cycle ) |
---|
| 1090 | printk("\n[%s] thread[%x,%x] enter for fatfs_ctx = %x / cycle %d\n", |
---|
| 1091 | __FUNCTION__ , this->process->pid, this->trdid, fatfs_ctx , cycle ); |
---|
[435] | 1092 | #endif |
---|
[23] | 1093 | |
---|
[602] | 1094 | // check argument |
---|
[625] | 1095 | assert( (fatfs_ctx != NULL) , "pointer on FATFS context is NULL" ); |
---|
[23] | 1096 | |
---|
[626] | 1097 | // check only cluster 0 does FATFS initialization |
---|
[625] | 1098 | assert( (local_cxy == 0) , "only cluster 0 can initialize FATFS"); |
---|
[602] | 1099 | |
---|
[626] | 1100 | // allocate a permanent 512 bytes buffer to store |
---|
| 1101 | // - temporarily the BOOT sector |
---|
| 1102 | // - permanently the FS_INFO sector |
---|
[635] | 1103 | req.type = KMEM_KCM; |
---|
| 1104 | req.order = 9; // 512 bytes |
---|
[50] | 1105 | req.flags = AF_KERNEL | AF_ZERO; |
---|
[635] | 1106 | buffer = kmem_alloc( &req ); |
---|
[188] | 1107 | |
---|
[602] | 1108 | if( buffer == NULL ) |
---|
| 1109 | { |
---|
| 1110 | printk("\n[PANIC] in %s : cannot allocate buffer\n", __FUNCTION__ ); |
---|
| 1111 | hal_core_sleep(); |
---|
| 1112 | } |
---|
[50] | 1113 | |
---|
[635] | 1114 | buffer_xp = XPTR( local_cxy , buffer ); |
---|
| 1115 | |
---|
[602] | 1116 | // load the BOOT record from device |
---|
[626] | 1117 | error = dev_ioc_sync_read( buffer_xp , 0 , 1 ); |
---|
[23] | 1118 | |
---|
[602] | 1119 | if ( error ) |
---|
| 1120 | { |
---|
| 1121 | printk("\n[PANIC] in %s : cannot access boot record\n", __FUNCTION__ ); |
---|
| 1122 | hal_core_sleep(); |
---|
| 1123 | } |
---|
[279] | 1124 | |
---|
[602] | 1125 | #if (DEBUG_FATFS_CTX_INIT & 0x1) |
---|
| 1126 | if( DEBUG_FATFS_CTX_INIT < cycle ) |
---|
[623] | 1127 | putb( "boot record", buffer , 256 ); |
---|
[50] | 1128 | #endif |
---|
| 1129 | |
---|
[602] | 1130 | // get sector size from boot record |
---|
[626] | 1131 | uint32_t sector_size = fatfs_get_record( BPB_BYTSPERSEC , buffer ); |
---|
[602] | 1132 | if ( sector_size != 512 ) |
---|
| 1133 | { |
---|
| 1134 | printk("\n[PANIC] in %s : sector size must be 512 bytes\n", __FUNCTION__ ); |
---|
| 1135 | hal_core_sleep(); |
---|
| 1136 | } |
---|
[50] | 1137 | |
---|
[602] | 1138 | // get cluster size from boot record |
---|
[626] | 1139 | uint32_t nb_sectors = fatfs_get_record( BPB_SECPERCLUS , buffer ); |
---|
[602] | 1140 | if ( nb_sectors != 8 ) |
---|
| 1141 | { |
---|
| 1142 | printk("\n[PANIC] in %s : cluster size must be 8 sectors\n", __FUNCTION__ ); |
---|
| 1143 | hal_core_sleep(); |
---|
| 1144 | } |
---|
[50] | 1145 | |
---|
[602] | 1146 | // get number of FAT copies from boot record |
---|
[626] | 1147 | uint32_t nb_fats = fatfs_get_record( BPB_NUMFATS , buffer ); |
---|
[602] | 1148 | if ( nb_fats != 1 ) |
---|
| 1149 | { |
---|
| 1150 | printk("\n[PANIC] in %s : number of FAT copies must be 1\n", __FUNCTION__ ); |
---|
| 1151 | hal_core_sleep(); |
---|
| 1152 | } |
---|
[50] | 1153 | |
---|
[602] | 1154 | // get number of sectors in FAT from boot record |
---|
[626] | 1155 | uint32_t fat_sectors = fatfs_get_record( BPB_FAT32_FATSZ32 , buffer ); |
---|
[602] | 1156 | if ( (fat_sectors & 0xF) != 0 ) |
---|
| 1157 | { |
---|
| 1158 | printk("\n[PANIC] in %s : FAT size not multiple of 16 sectors\n", __FUNCTION__ ); |
---|
| 1159 | hal_core_sleep(); |
---|
| 1160 | } |
---|
[50] | 1161 | |
---|
[602] | 1162 | // get root cluster from boot record |
---|
[626] | 1163 | uint32_t root_cluster = fatfs_get_record( BPB_FAT32_ROOTCLUS , buffer ); |
---|
[602] | 1164 | if ( root_cluster != 2 ) |
---|
| 1165 | { |
---|
| 1166 | printk("\n[PANIC] in %s : root cluster index must be 2\n", __FUNCTION__ ); |
---|
| 1167 | hal_core_sleep(); |
---|
| 1168 | } |
---|
[50] | 1169 | |
---|
[23] | 1170 | // get FAT lba from boot record |
---|
[626] | 1171 | uint32_t fat_lba = fatfs_get_record( BPB_RSVDSECCNT , buffer ); |
---|
[50] | 1172 | |
---|
[602] | 1173 | // get FS_INFO sector lba from boot record |
---|
[626] | 1174 | uint32_t fs_info_lba = fatfs_get_record( BPB_FAT32_FSINFO , buffer ); |
---|
[602] | 1175 | |
---|
| 1176 | // load the FS_INFO record from device |
---|
[626] | 1177 | error = dev_ioc_sync_read( buffer_xp , fs_info_lba , 1 ); |
---|
[602] | 1178 | |
---|
| 1179 | if ( error ) |
---|
| 1180 | { |
---|
| 1181 | printk("\n[PANIC] in %s : cannot access FS_INFO record\n", __FUNCTION__ ); |
---|
| 1182 | hal_core_sleep(); |
---|
| 1183 | } |
---|
| 1184 | |
---|
[626] | 1185 | // get free_clusters number from FS_INFO record |
---|
| 1186 | uint32_t free_clusters = fatfs_get_record( FS_FREE_CLUSTERS , buffer ); |
---|
[602] | 1187 | if ( free_clusters >= fat_sectors << 7 ) |
---|
| 1188 | { |
---|
| 1189 | printk("\n[PANIC] in %s : unconsistent free_clusters\n", __FUNCTION__ ); |
---|
| 1190 | hal_core_sleep(); |
---|
| 1191 | } |
---|
| 1192 | |
---|
[626] | 1193 | // get free_cluster_hint from FS_INFO record |
---|
| 1194 | uint32_t free_cluster_hint = fatfs_get_record( FS_FREE_CLUSTER_HINT , buffer ); |
---|
| 1195 | |
---|
[602] | 1196 | if ( free_cluster_hint >= fat_sectors << 7 ) |
---|
| 1197 | { |
---|
| 1198 | printk("\n[PANIC] in %s : unconsistent free_cluster_hint\n", __FUNCTION__ ); |
---|
| 1199 | hal_core_sleep(); |
---|
| 1200 | } |
---|
| 1201 | |
---|
[23] | 1202 | // allocate a mapper for the FAT itself |
---|
[246] | 1203 | mapper_t * fat_mapper = mapper_create( FS_TYPE_FATFS ); |
---|
[602] | 1204 | if ( fat_mapper == NULL ) |
---|
| 1205 | { |
---|
| 1206 | printk("\n[PANIC] in %s : no memory for FAT mapper\n", __FUNCTION__ ); |
---|
| 1207 | hal_core_sleep(); |
---|
| 1208 | } |
---|
[50] | 1209 | |
---|
[626] | 1210 | // the inode field is NULL for the FAT mapper |
---|
[246] | 1211 | fat_mapper->inode = NULL; |
---|
| 1212 | |
---|
[23] | 1213 | // initialize the FATFS context |
---|
| 1214 | fatfs_ctx->fat_begin_lba = fat_lba; |
---|
| 1215 | fatfs_ctx->fat_sectors_count = fat_sectors; |
---|
| 1216 | fatfs_ctx->bytes_per_sector = sector_size; |
---|
[188] | 1217 | fatfs_ctx->sectors_per_cluster = nb_sectors; |
---|
[23] | 1218 | fatfs_ctx->cluster_begin_lba = fat_lba + fat_sectors; |
---|
| 1219 | fatfs_ctx->root_dir_cluster = 2; |
---|
| 1220 | fatfs_ctx->fat_mapper_xp = XPTR( local_cxy , fat_mapper ); |
---|
[626] | 1221 | fatfs_ctx->fs_info_lba = fs_info_lba; |
---|
[602] | 1222 | fatfs_ctx->free_clusters = free_clusters; |
---|
| 1223 | fatfs_ctx->free_cluster_hint = free_cluster_hint; |
---|
[626] | 1224 | fatfs_ctx->fs_info_buffer = buffer; |
---|
[23] | 1225 | |
---|
[628] | 1226 | remote_rwlock_init( XPTR( local_cxy , &fatfs_ctx->lock ) , LOCK_FATFS_FAT ); |
---|
[602] | 1227 | |
---|
[625] | 1228 | #if (DEBUG_FATFS_CTX_INIT & 0x1) |
---|
| 1229 | if( DEBUG_FATFS_CTX_INIT < cycle ) |
---|
| 1230 | fatfs_ctx_display( fatfs_ctx ); |
---|
| 1231 | #endif |
---|
| 1232 | |
---|
[602] | 1233 | #if DEBUG_FATFS_CTX_INIT |
---|
[435] | 1234 | cycle = (uint32_t)hal_get_cycles(); |
---|
[602] | 1235 | if( DEBUG_FATFS_CTX_INIT < cycle ) |
---|
| 1236 | printk("\n[%s] thread[%x,%x] exit for fatfs_ctx = %x / cycle %d\n", |
---|
| 1237 | __FUNCTION__, this->process->pid, this->trdid, fatfs_ctx, cycle ); |
---|
[435] | 1238 | #endif |
---|
[279] | 1239 | |
---|
[23] | 1240 | } // end fatfs_ctx_init() |
---|
| 1241 | |
---|
[188] | 1242 | ///////////////////////////////////////////////// |
---|
| 1243 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ) |
---|
[23] | 1244 | { |
---|
| 1245 | kmem_req_t req; |
---|
[635] | 1246 | req.type = KMEM_KCM; |
---|
[23] | 1247 | req.ptr = fatfs_ctx; |
---|
| 1248 | kmem_free( &req ); |
---|
| 1249 | } |
---|
| 1250 | |
---|
[602] | 1251 | /////////////////////////////////////////////// |
---|
| 1252 | error_t fatfs_add_dentry( vfs_inode_t * inode, |
---|
| 1253 | vfs_dentry_t * dentry ) |
---|
[1] | 1254 | { |
---|
[401] | 1255 | error_t error; |
---|
[602] | 1256 | uint32_t length; // dentry name length |
---|
| 1257 | uint32_t nb_lfn; // number or required LFN |
---|
| 1258 | char sfn[11]; // buffer for SFN name |
---|
| 1259 | uint8_t checksum; // name checksum |
---|
| 1260 | mapper_t * mapper; // loal pointer on parent inode mapper |
---|
| 1261 | xptr_t mapper_xp; // extended pointer on parent inode mapper |
---|
| 1262 | xptr_t child_xp; // extended pointer on child inode |
---|
| 1263 | cxy_t child_cxy; // child inode cluster |
---|
| 1264 | vfs_inode_t * child_ptr; // child inode local pointer |
---|
| 1265 | uint32_t size; // child inode size |
---|
| 1266 | uint32_t type; // child inode type |
---|
| 1267 | uint32_t cluster; // child inode cluster index |
---|
[246] | 1268 | |
---|
[602] | 1269 | #if DEBUG_FATFS_ADD_DENTRY |
---|
| 1270 | char dir_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1271 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1272 | thread_t * this = CURRENT_THREAD; |
---|
| 1273 | vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name ); |
---|
| 1274 | if( DEBUG_FATFS_ADD_DENTRY < cycle ) |
---|
| 1275 | printk("\n[%s] thread[%x,%x] enter / parent <%s> / child <%s> / cycle %d\n", |
---|
| 1276 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle ); |
---|
| 1277 | #endif |
---|
[1] | 1278 | |
---|
[602] | 1279 | // check arguments |
---|
| 1280 | assert( (inode != NULL) , "inode pointer is NULL\n" ); |
---|
| 1281 | assert( (dentry != NULL) , "dentry pointer is NULL\n" ); |
---|
| 1282 | assert( (inode->mapper != NULL ) , "mapper pointer is NULL\n" ); |
---|
| 1283 | |
---|
| 1284 | // get pointers on directory mapper |
---|
| 1285 | mapper = inode->mapper; |
---|
| 1286 | mapper_xp = XPTR( local_cxy , mapper ); |
---|
[1] | 1287 | |
---|
[602] | 1288 | // get extended pointers on remote child inode |
---|
| 1289 | child_xp = dentry->child_xp; |
---|
| 1290 | child_cxy = GET_CXY( child_xp ); |
---|
| 1291 | child_ptr = GET_PTR( child_xp ); |
---|
[1] | 1292 | |
---|
[602] | 1293 | // get relevant infos from child inode |
---|
| 1294 | type = hal_remote_l32( XPTR( child_cxy , &child_ptr->type ) ); |
---|
| 1295 | size = hal_remote_l32( XPTR( child_cxy , &child_ptr->size ) ); |
---|
| 1296 | cluster = (uint32_t)(intptr_t)hal_remote_lpt( XPTR( child_cxy , &child_ptr->extend ) ); |
---|
[1] | 1297 | |
---|
[602] | 1298 | // analyse dentry name |
---|
| 1299 | error = fatfs_name_format( dentry->name, |
---|
| 1300 | &length, |
---|
| 1301 | &nb_lfn, |
---|
| 1302 | sfn, |
---|
| 1303 | &checksum ); |
---|
| 1304 | if ( error ) |
---|
[246] | 1305 | { |
---|
[602] | 1306 | printk("\n[ERROR] in %s : dentry name > 31 bytes\n", __FUNCTION__ ); |
---|
| 1307 | return -1; |
---|
| 1308 | } |
---|
| 1309 | |
---|
| 1310 | // Search end of directory with two embedded loops: |
---|
| 1311 | // - scan the pages in the mapper |
---|
| 1312 | // - scan the entries in each page to find NO_MORE_ENTRY |
---|
[1] | 1313 | |
---|
[602] | 1314 | xptr_t page_xp; // extended pointer on page descriptor |
---|
| 1315 | xptr_t base_xp; // extended pointer on page base |
---|
| 1316 | uint8_t * base; // local pointer on page base (array of bytes) |
---|
| 1317 | uint32_t page_id = 0; // page index in mapper |
---|
| 1318 | uint32_t offset = 0; // position in page |
---|
| 1319 | uint32_t found = 0; // NO_MORE_ENTRY found |
---|
[1] | 1320 | |
---|
[602] | 1321 | // loop on pages in mapper |
---|
| 1322 | while ( found == 0 ) |
---|
[1] | 1323 | { |
---|
[602] | 1324 | // get extended pointer on page descriptor in mapper |
---|
| 1325 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
[1] | 1326 | |
---|
[602] | 1327 | if ( page_xp == XPTR_NULL ) |
---|
| 1328 | { |
---|
| 1329 | printk("\n[ERROR] in %s : cannot extend directory mapper\n", __FUNCTION__ ); |
---|
| 1330 | return -1; |
---|
| 1331 | } |
---|
| 1332 | |
---|
| 1333 | // get pointer on page base |
---|
| 1334 | base_xp = ppm_page2base( page_xp ); |
---|
| 1335 | base = GET_PTR( base_xp ); |
---|
[246] | 1336 | |
---|
[602] | 1337 | // loop on directory entries in this page |
---|
| 1338 | while ( (offset < 4096) && (found == 0) ) |
---|
[265] | 1339 | { |
---|
[626] | 1340 | if ( fatfs_get_record( LDIR_ORD, (base + offset) ) == NO_MORE_ENTRY ) |
---|
[602] | 1341 | { |
---|
| 1342 | found = 1; |
---|
| 1343 | } |
---|
| 1344 | else |
---|
| 1345 | { |
---|
| 1346 | offset = offset + 32; |
---|
| 1347 | } |
---|
| 1348 | } // end loop on entries |
---|
| 1349 | |
---|
| 1350 | if ( found == 0 ) |
---|
| 1351 | { |
---|
| 1352 | page_id++; |
---|
| 1353 | offset = 0; |
---|
[265] | 1354 | } |
---|
[602] | 1355 | } // end loop on pages |
---|
| 1356 | |
---|
| 1357 | // Modify the directory mapper: depending on the name length, |
---|
| 1358 | // the new child requires to write (3, 4, or 5) directory entries. |
---|
| 1359 | // To actually register the new child, we use a 5 steps FSM |
---|
| 1360 | // (one state per entry to be written), that is traversed as: |
---|
| 1361 | // LFN3 -> LFN2 -> LFN1 -> NORMAL -> NOMORE |
---|
| 1362 | // At most two pages are modified: |
---|
| 1363 | // - the page containing the NO_MORE_ENTRY is always modified |
---|
| 1364 | // - the following page can be modified if the name spread on to pages. |
---|
| 1365 | |
---|
| 1366 | char * name = dentry->name; |
---|
| 1367 | |
---|
| 1368 | uint32_t step; // FSM state |
---|
| 1369 | |
---|
| 1370 | if ( nb_lfn == 1 ) step = 3; |
---|
| 1371 | else if ( nb_lfn == 2 ) step = 4; |
---|
| 1372 | else if ( nb_lfn == 3 ) step = 5; |
---|
| 1373 | |
---|
| 1374 | uint8_t * entry; // pointer on directory entry to be written |
---|
| 1375 | uint32_t i; // byte index in one 32 bytes directory |
---|
| 1376 | uint32_t c; // character index in name |
---|
| 1377 | |
---|
| 1378 | while ( step ) |
---|
| 1379 | { |
---|
| 1380 | // when the new child is split on two pages, |
---|
| 1381 | // we need to access a new page in mapper |
---|
| 1382 | if ( offset >= 4096 ) |
---|
[265] | 1383 | { |
---|
[602] | 1384 | // copy the modified page to IOC device |
---|
[614] | 1385 | fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[265] | 1386 | |
---|
[602] | 1387 | // get the next page in FAT mapper |
---|
| 1388 | page_xp = mapper_remote_get_page( mapper_xp , page_id + 1 ); |
---|
| 1389 | |
---|
| 1390 | if ( page_xp == XPTR_NULL ) |
---|
[265] | 1391 | { |
---|
[602] | 1392 | printk("\n[ERROR] in %s : cannot extend directory mapper\n", __FUNCTION__ ); |
---|
| 1393 | return -1; |
---|
| 1394 | } |
---|
| 1395 | |
---|
| 1396 | // get pointer on page base |
---|
| 1397 | base_xp = ppm_page2base( page_xp ); |
---|
| 1398 | base = GET_PTR( base_xp ); |
---|
| 1399 | |
---|
| 1400 | // update offset |
---|
| 1401 | offset = 0; |
---|
| 1402 | } |
---|
[407] | 1403 | |
---|
[602] | 1404 | // compute directory entry address |
---|
| 1405 | entry = base + offset; |
---|
| 1406 | |
---|
| 1407 | #if (DEBUG_FATFS_ADD_DENTRY & 1) |
---|
[630] | 1408 | cycle = (uint32_t)hal_get_cycles(); |
---|
[602] | 1409 | if( DEBUG_FATFS_ADD_DENTRY < cycle ) |
---|
[630] | 1410 | printk("\n[%s] FSM step = %d / offset = %x / nb_lfn = %d / cycle %d\n", |
---|
| 1411 | __FUNCTION__, step, offset, nb_lfn, cycle ); |
---|
[435] | 1412 | #endif |
---|
[602] | 1413 | |
---|
| 1414 | // write 32 bytes (one directory entry) per iteration |
---|
| 1415 | switch ( step ) |
---|
| 1416 | { |
---|
| 1417 | case 5: // write LFN3 entry |
---|
| 1418 | { |
---|
| 1419 | c = 26; |
---|
| 1420 | // scan the 32 bytes in dir_entry |
---|
| 1421 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1422 | { |
---|
| 1423 | if (i == 0) |
---|
| 1424 | { |
---|
| 1425 | if ( nb_lfn == 3) entry[i] = 0x43; |
---|
| 1426 | else entry[i] = 0x03; |
---|
| 1427 | } |
---|
| 1428 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1429 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1430 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1431 | ( c < length ) ) |
---|
| 1432 | { |
---|
| 1433 | entry[i] = name[c]; |
---|
| 1434 | c++; |
---|
| 1435 | } |
---|
| 1436 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1437 | else if (i == 13) entry[i] = checksum; |
---|
| 1438 | else entry[i] = 0x00; |
---|
| 1439 | } |
---|
| 1440 | step--; |
---|
| 1441 | break; |
---|
[265] | 1442 | } |
---|
[602] | 1443 | case 4: // write LFN2 entry |
---|
[265] | 1444 | { |
---|
[602] | 1445 | c = 13; |
---|
| 1446 | // scan the 32 bytes in dir_entry |
---|
| 1447 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1448 | { |
---|
| 1449 | if (i == 0) |
---|
| 1450 | { |
---|
| 1451 | if ( nb_lfn == 2) entry[i] = 0x42; |
---|
| 1452 | else entry[i] = 0x02; |
---|
| 1453 | } |
---|
| 1454 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1455 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1456 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1457 | ( c < length ) ) |
---|
| 1458 | { |
---|
| 1459 | entry[i] = name[c]; |
---|
| 1460 | c++; |
---|
| 1461 | } |
---|
| 1462 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1463 | else if (i == 13) entry[i] = checksum; |
---|
| 1464 | else entry[i] = 0x00; |
---|
| 1465 | } |
---|
| 1466 | step--; |
---|
| 1467 | break; |
---|
| 1468 | } |
---|
| 1469 | case 3: // Write LFN1 entry |
---|
| 1470 | { |
---|
| 1471 | c = 0; |
---|
| 1472 | // scan the 32 bytes in dir_entry |
---|
| 1473 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1474 | { |
---|
| 1475 | if (i == 0) |
---|
| 1476 | { |
---|
| 1477 | if ( nb_lfn == 1) entry[i] = 0x41; |
---|
| 1478 | else entry[i] = 0x01; |
---|
| 1479 | } |
---|
| 1480 | else if ( ( ((i >= 1 ) && (i<=10) && ((i&1)==1)) || |
---|
| 1481 | ((i >= 14) && (i<=25) && ((i&1)==0)) || |
---|
| 1482 | ((i >= 28) && (i<=31) && ((i&1)==0)) ) && |
---|
| 1483 | ( c < length ) ) |
---|
| 1484 | { |
---|
| 1485 | entry[i] = name[c]; |
---|
| 1486 | c++; |
---|
| 1487 | } |
---|
| 1488 | else if (i == 11) entry[i] = 0x0F; |
---|
| 1489 | else if (i == 13) entry[i] = checksum; |
---|
| 1490 | else entry[i] = 0x00; |
---|
| 1491 | } |
---|
| 1492 | step--; |
---|
| 1493 | break; |
---|
| 1494 | } |
---|
| 1495 | case 2: // write NORMAL entry |
---|
| 1496 | { |
---|
| 1497 | // scan the 32 bytes in dir_entry |
---|
| 1498 | for ( i = 0 ; i < 32 ; i++ ) |
---|
| 1499 | { |
---|
| 1500 | if ( i < 11 ) // 8.3 SFN |
---|
| 1501 | { |
---|
| 1502 | entry[i] = sfn[i]; |
---|
| 1503 | } |
---|
| 1504 | else if (i == 11) // ATTR |
---|
| 1505 | { |
---|
| 1506 | if (type == INODE_TYPE_DIR) entry[i] = 0x10; |
---|
| 1507 | else entry[i] = 0x20; |
---|
| 1508 | } |
---|
| 1509 | else if (i == 20) entry[i] = cluster>>16; // cluster.B2 |
---|
| 1510 | else if (i == 21) entry[i] = cluster>>24; // cluster.B3 |
---|
| 1511 | else if (i == 26) entry[i] = cluster>>0; // cluster.B0 |
---|
| 1512 | else if (i == 27) entry[i] = cluster>>8; // cluster.B1 |
---|
| 1513 | else if (i == 28) entry[i] = size>>0; // size.B0 |
---|
| 1514 | else if (i == 29) entry[i] = size>>8; // size.B1 |
---|
| 1515 | else if (i == 30) entry[i] = size>>16; // size.B2 |
---|
| 1516 | else if (i == 31) entry[i] = size>>24; // size.B3 |
---|
| 1517 | else entry[i] = 0x00; |
---|
| 1518 | } |
---|
[407] | 1519 | |
---|
[602] | 1520 | // update the "extend" field in dentry descriptor |
---|
| 1521 | dentry->extend = (void*)(intptr_t)(((page_id<<12) + offset)>>5); |
---|
| 1522 | |
---|
| 1523 | step--; |
---|
| 1524 | break; |
---|
[265] | 1525 | } |
---|
[602] | 1526 | case 1: // write NOMORE entry |
---|
| 1527 | { |
---|
| 1528 | entry [0] = 0x00; |
---|
| 1529 | step--; |
---|
| 1530 | break; |
---|
| 1531 | } |
---|
| 1532 | } // end switch step |
---|
[265] | 1533 | |
---|
[602] | 1534 | offset += 32; |
---|
[265] | 1535 | |
---|
[602] | 1536 | } // exit while |
---|
| 1537 | |
---|
[630] | 1538 | #if (DEBUG_FATFS_ADD_DENTRY & 1) |
---|
| 1539 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1540 | if( DEBUG_FATFS_ADD_DENTRY < cycle ) |
---|
| 1541 | printk("\n[%s] thread[%x,%x] before IOC access / cycle %d\n", |
---|
| 1542 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
| 1543 | #endif |
---|
| 1544 | |
---|
[602] | 1545 | // copy the modified page to the IOC device |
---|
[614] | 1546 | fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[602] | 1547 | |
---|
| 1548 | #if DEBUG_FATFS_ADD_DENTRY |
---|
| 1549 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1550 | if( DEBUG_FATFS_ADD_DENTRY < cycle ) |
---|
[614] | 1551 | printk("\n[%s] thread[%x,%x] exit / parent <%s> / child <%s> / cycle %d\n", |
---|
[602] | 1552 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle ); |
---|
[435] | 1553 | #endif |
---|
[406] | 1554 | |
---|
[602] | 1555 | return 0; |
---|
[265] | 1556 | |
---|
[602] | 1557 | } // end fatfs_add_dentry() |
---|
[246] | 1558 | |
---|
[602] | 1559 | ////////////////////////////////////////////////// |
---|
| 1560 | error_t fatfs_remove_dentry( vfs_inode_t * inode, |
---|
| 1561 | vfs_dentry_t * dentry ) |
---|
| 1562 | { |
---|
| 1563 | xptr_t mapper_xp; // extended pointer on mapper |
---|
| 1564 | mapper_t * mapper; // local pointer on mapper |
---|
| 1565 | xptr_t page_xp; // extended pointer on mapper page descriptor |
---|
| 1566 | xptr_t base_xp; // extended pointer on mapper page base |
---|
| 1567 | uint8_t * base; // local pointer on mapper page base |
---|
[246] | 1568 | |
---|
[602] | 1569 | #if DEBUG_FATFS_REMOVE_DENTRY |
---|
| 1570 | char dir_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1571 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1572 | thread_t * this = CURRENT_THREAD; |
---|
| 1573 | vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name ); |
---|
| 1574 | if( DEBUG_FATFS_REMOVE_DENTRY < cycle ) |
---|
[610] | 1575 | printk("\n[%s] thread[%x,%x] enter / parent <%s> / child <%s> / cycle %d\n", |
---|
[602] | 1576 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle ); |
---|
[435] | 1577 | #endif |
---|
[401] | 1578 | |
---|
[602] | 1579 | // check arguments |
---|
| 1580 | assert( (inode != NULL) , "inode pointer is NULL\n" ); |
---|
| 1581 | assert( (dentry != NULL) , "dentry pointer is NULL\n" ); |
---|
| 1582 | assert( (inode->type == INODE_TYPE_DIR) , "inode is not a directory\n" ); |
---|
| 1583 | assert( (inode->mapper != NULL ) , "mapper pointer is NULL\n" ); |
---|
| 1584 | |
---|
| 1585 | // get pointers on directory mapper |
---|
| 1586 | mapper = inode->mapper; |
---|
| 1587 | mapper_xp = XPTR( local_cxy , mapper ); |
---|
| 1588 | |
---|
| 1589 | // compute number of LFN entries |
---|
| 1590 | uint32_t nb_lfn; |
---|
| 1591 | uint32_t name_length = strlen( dentry->name ); |
---|
| 1592 | |
---|
| 1593 | if ( name_length <= 13 ) nb_lfn = 1; |
---|
| 1594 | else if ( name_length <= 26 ) nb_lfn = 2; |
---|
| 1595 | else nb_lfn = 3; |
---|
| 1596 | |
---|
| 1597 | // we must invalidate (2, 3 or 4) 32 bytes entries: |
---|
| 1598 | // the NORMAL entry (registered in dentry->extend) and all preceding LFN entries |
---|
| 1599 | // At most two pages are modified: |
---|
| 1600 | // - the page containing the NORMAL entry is always modified. |
---|
| 1601 | // - the preceding page is modified when the name spread on two pages. |
---|
| 1602 | |
---|
| 1603 | // get 32 bytes directory entry index from dentry->extend |
---|
| 1604 | uint32_t dentry_id = (uint32_t)(intptr_t)dentry->extend; |
---|
| 1605 | |
---|
| 1606 | // get page index and offset in parent directory mapper |
---|
| 1607 | uint32_t page_id = dentry_id >> 7; |
---|
| 1608 | uint32_t offset = (dentry_id & 0x7F)<<5; |
---|
| 1609 | |
---|
[610] | 1610 | #if DEBUG_FATFS_REMOVE_DENTRY & 1 |
---|
| 1611 | if( DEBUG_FATFS_REMOVE_DENTRY < cycle ) |
---|
| 1612 | printk("\n[%s] dentry_id %x / page_id %x / offset %x\n", |
---|
| 1613 | __FUNCTION__, dentry_id, page_id, offset ); |
---|
| 1614 | #endif |
---|
| 1615 | |
---|
[602] | 1616 | // get extended pointer on page descriptor from parent directory mapper |
---|
| 1617 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
| 1618 | |
---|
| 1619 | if ( page_xp == XPTR_NULL ) |
---|
[406] | 1620 | { |
---|
[602] | 1621 | printk("\n[ERROR] in %s : cannot extend directory mapper\n", __FUNCTION__ ); |
---|
| 1622 | return -1; |
---|
[406] | 1623 | } |
---|
[602] | 1624 | |
---|
| 1625 | // get pointers on page base |
---|
| 1626 | base_xp = ppm_page2base( page_xp ); |
---|
| 1627 | base = GET_PTR( base_xp ); |
---|
| 1628 | |
---|
| 1629 | // invalidate NORMAL entry in directory cache |
---|
| 1630 | base[offset] = 0xE5; |
---|
| 1631 | |
---|
| 1632 | // invalidate LFN entries |
---|
| 1633 | while ( nb_lfn ) |
---|
| 1634 | { |
---|
| 1635 | if (offset == 0) // we must load page (page_id - 1) |
---|
| 1636 | { |
---|
| 1637 | |
---|
| 1638 | // check page_id |
---|
| 1639 | assert( (page_id > 0), "page_id and offset cannot be both 0\n" ); |
---|
| 1640 | |
---|
| 1641 | // copy the modified page to the IOC device |
---|
[614] | 1642 | fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[602] | 1643 | |
---|
| 1644 | // get extended pointer on page descriptor from parent directory mapper |
---|
| 1645 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
| 1646 | |
---|
| 1647 | if ( page_xp == XPTR_NULL ) |
---|
| 1648 | { |
---|
| 1649 | printk("\n[ERROR] in %s : cannot access directory mapper\n", __FUNCTION__ ); |
---|
| 1650 | return -1; |
---|
| 1651 | } |
---|
| 1652 | |
---|
| 1653 | // get pointers on page base |
---|
| 1654 | base_xp = ppm_page2base( page_xp ); |
---|
| 1655 | base = GET_PTR( base_xp ); |
---|
| 1656 | |
---|
| 1657 | // update offset |
---|
| 1658 | offset = 4096; |
---|
| 1659 | } |
---|
| 1660 | |
---|
| 1661 | offset = offset - 32; |
---|
| 1662 | |
---|
| 1663 | // check for LFN entry |
---|
[626] | 1664 | assert( (fatfs_get_record( DIR_ATTR, base + offset ) == ATTR_LONG_NAME_MASK ), |
---|
[602] | 1665 | "this directory entry must be a LFN\n"); |
---|
| 1666 | |
---|
| 1667 | // invalidate LFN entry |
---|
| 1668 | base[offset] = 0xE5; |
---|
| 1669 | |
---|
| 1670 | nb_lfn--; |
---|
| 1671 | } |
---|
| 1672 | |
---|
| 1673 | // copy the modified page to the IOC device |
---|
[614] | 1674 | fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[602] | 1675 | |
---|
| 1676 | |
---|
| 1677 | #if DEBUG_FATFS_REMOVE_DENTRY |
---|
| 1678 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1679 | if( DEBUG_FATFS_REMOVE_DENTRY < cycle ) |
---|
[610] | 1680 | printk("\n[%s] thread[%x,%x] exit / parent %s / child %s / cycle %d\n", |
---|
[602] | 1681 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle ); |
---|
[406] | 1682 | #endif |
---|
| 1683 | |
---|
[1] | 1684 | return 0; |
---|
| 1685 | |
---|
[602] | 1686 | } // end fatfs_remove_dentry |
---|
[246] | 1687 | |
---|
[623] | 1688 | |
---|
| 1689 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1690 | // This static function scan the pages of a mapper containing a FAT32 directory, identified |
---|
| 1691 | // by the <mapper> argument, to find the directory entry identified by the <name> argument, |
---|
| 1692 | // and return a pointer on the directory entry, described as and array of 32 bytes, and the |
---|
| 1693 | // incex of this entry in the FAT32 mapper, seen as an array of 32 bytes entries. |
---|
| 1694 | // It is called by the fatfs_new_dentry() and fatfs_update_dentry() functions. |
---|
| 1695 | // It must be called by a thread running in the cluster containing the mapper. |
---|
| 1696 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1697 | // @ mapper : [in] local pointer on directory mapper. |
---|
| 1698 | // @ name : [in] searched directory entry name. |
---|
| 1699 | // @ entry : [out] buffer for the pointer on the 32 bytes directory entry (when found). |
---|
| 1700 | // @ index : [out] buffer for the directory entry index in mapper. |
---|
| 1701 | // @ return 0 if found / return 1 if not found / return -1 if mapper access error. |
---|
| 1702 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1703 | error_t fatfs_scan_directory( mapper_t * mapper, |
---|
| 1704 | char * name, |
---|
| 1705 | uint8_t ** entry, |
---|
| 1706 | uint32_t * index ) |
---|
[1] | 1707 | { |
---|
[610] | 1708 | // Two embedded loops to scan the directory mapper: |
---|
[602] | 1709 | // - scan the parent directory mapper pages |
---|
[238] | 1710 | // - scan the directory entries in each 4 Kbytes page |
---|
[1] | 1711 | |
---|
[623] | 1712 | // check parent_inode and child_inode |
---|
| 1713 | assert( (mapper != NULL) , "mapper pointer is NULL\n" ); |
---|
| 1714 | assert( (name != NULL ), "child name is undefined\n" ); |
---|
| 1715 | assert( (entry != NULL ), "entry buffer undefined\n" ); |
---|
| 1716 | |
---|
| 1717 | #if DEBUG_FATFS_SCAN_DIRECTORY |
---|
[602] | 1718 | char parent_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1719 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1720 | thread_t * this = CURRENT_THREAD; |
---|
[623] | 1721 | vfs_inode_get_name( XPTR( local_cxy , mapper->inode ) , parent_name ); |
---|
| 1722 | if( DEBUG_FATFS_SCAN_DIRECTORY < cycle ) |
---|
| 1723 | printk("\n[%s] thread[%x,%x] enter to search child <%s> in parent <%s> / cycle %d\n", |
---|
[602] | 1724 | __FUNCTION__, this->process->pid, this->trdid, name , parent_name , cycle ); |
---|
[435] | 1725 | #endif |
---|
[1] | 1726 | |
---|
[623] | 1727 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracted from each directory entry |
---|
[238] | 1728 | |
---|
| 1729 | char lfn1[16]; // buffer for one partial cname |
---|
| 1730 | char lfn2[16]; // buffer for one partial cname |
---|
| 1731 | char lfn3[16]; // buffer for one partial cname |
---|
[623] | 1732 | xptr_t mapper_xp; // extended pointer on mapper descriptor |
---|
[602] | 1733 | xptr_t page_xp; // extended pointer on page descriptor |
---|
| 1734 | xptr_t base_xp; // extended pointer on page base |
---|
| 1735 | uint8_t * base; // local pointer on page base |
---|
[614] | 1736 | uint8_t attr; // directory entry ATTR field |
---|
| 1737 | uint8_t ord; // directory entry ORD field |
---|
[238] | 1738 | uint32_t seq; // sequence index |
---|
[602] | 1739 | uint32_t lfn = 0; // LFN entries number |
---|
[623] | 1740 | int32_t found = 0; // not yet = 0 / success = 1 / not found = 2 / error = -1 |
---|
[602] | 1741 | uint32_t page_id = 0; // page index in mapper |
---|
| 1742 | uint32_t offset = 0; // byte offset in page |
---|
[238] | 1743 | |
---|
[623] | 1744 | mapper_xp = XPTR( local_cxy , mapper ); |
---|
| 1745 | |
---|
| 1746 | // scan the mapper pages |
---|
[238] | 1747 | while ( found == 0 ) |
---|
| 1748 | { |
---|
| 1749 | // get one page |
---|
[602] | 1750 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
[238] | 1751 | |
---|
[623] | 1752 | if( page_xp == XPTR_NULL) |
---|
| 1753 | { |
---|
| 1754 | found = -1; |
---|
| 1755 | } |
---|
[238] | 1756 | |
---|
| 1757 | // get page base |
---|
[602] | 1758 | base_xp = ppm_page2base( page_xp ); |
---|
| 1759 | base = (uint8_t *)GET_PTR( base_xp ); |
---|
[238] | 1760 | |
---|
[623] | 1761 | #if (DEBUG_FATFS_SCAN_DIRECTORY & 0x1) |
---|
| 1762 | if( DEBUG_FATFS_SCAN_DIRECTORY < cycle ) |
---|
[614] | 1763 | mapper_display_page( mapper_xp , page_id , 256 ); |
---|
[265] | 1764 | #endif |
---|
[238] | 1765 | // scan this page until end of directory, end of page, or name found |
---|
| 1766 | while( (offset < 4096) && (found == 0) ) |
---|
| 1767 | { |
---|
[626] | 1768 | attr = fatfs_get_record( DIR_ATTR , base + offset ); |
---|
| 1769 | ord = fatfs_get_record( LDIR_ORD , base + offset ); |
---|
[238] | 1770 | |
---|
| 1771 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 1772 | { |
---|
[623] | 1773 | found = 2; |
---|
[238] | 1774 | } |
---|
| 1775 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 1776 | { |
---|
| 1777 | offset = offset + 32; |
---|
| 1778 | } |
---|
[614] | 1779 | else if ( attr == 0x28 ) // volune_id => skip |
---|
| 1780 | { |
---|
| 1781 | offset = offset + 32; |
---|
| 1782 | } |
---|
[238] | 1783 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry => get partial cname |
---|
| 1784 | { |
---|
| 1785 | seq = ord & 0x3; |
---|
| 1786 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 1787 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
| 1788 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
| 1789 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
| 1790 | offset = offset + 32; |
---|
| 1791 | } |
---|
| 1792 | else // NORMAL entry |
---|
| 1793 | { |
---|
| 1794 | // build the extracted name |
---|
| 1795 | if ( lfn == 0 ) |
---|
| 1796 | { |
---|
| 1797 | fatfs_get_name_from_short( base + offset , cname ); |
---|
| 1798 | } |
---|
| 1799 | else if ( lfn == 1 ) |
---|
| 1800 | { |
---|
| 1801 | strcpy( cname , lfn1 ); |
---|
| 1802 | } |
---|
| 1803 | else if ( lfn == 2 ) |
---|
| 1804 | { |
---|
| 1805 | strcpy( cname , lfn1 ); |
---|
| 1806 | strcpy( cname + 13 , lfn2 ); |
---|
| 1807 | } |
---|
| 1808 | else if ( lfn == 3 ) |
---|
| 1809 | { |
---|
| 1810 | strcpy( cname , lfn1 ); |
---|
| 1811 | strcpy( cname + 13 , lfn2 ); |
---|
| 1812 | strcpy( cname + 26 , lfn3 ); |
---|
| 1813 | } |
---|
| 1814 | |
---|
| 1815 | // get dentry arguments if extracted cname == searched name |
---|
| 1816 | if ( strcmp( name , cname ) == 0 ) |
---|
| 1817 | { |
---|
[623] | 1818 | *entry = base + offset; |
---|
| 1819 | *index = ((page_id<<12) + offset)>>5; |
---|
[602] | 1820 | found = 1; |
---|
[238] | 1821 | } |
---|
| 1822 | offset = offset + 32; |
---|
| 1823 | lfn = 0; |
---|
| 1824 | } |
---|
[602] | 1825 | } // end loop on directory entries in page |
---|
| 1826 | |
---|
| 1827 | page_id++; |
---|
[238] | 1828 | offset = 0; |
---|
[602] | 1829 | |
---|
[238] | 1830 | } // end loop on pages |
---|
| 1831 | |
---|
[623] | 1832 | if( found == 1 ) |
---|
| 1833 | { |
---|
[238] | 1834 | |
---|
[623] | 1835 | #if DEBUG_FATFS_SCAN_DIRECTORY |
---|
| 1836 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1837 | if( DEBUG_FATFS_SCAN_DIRECTORY < cycle ) |
---|
| 1838 | printk("\n[%s] thread[%x,%x] exit / found child <%s> in <%s>\n", |
---|
| 1839 | __FUNCTION__, this->process->pid, this->trdid, name, parent_name ); |
---|
| 1840 | #endif |
---|
| 1841 | return 0; |
---|
| 1842 | } |
---|
| 1843 | else if( found == 2 ) |
---|
[238] | 1844 | { |
---|
| 1845 | |
---|
[623] | 1846 | #if DEBUG_FATFS_SCAN_DIRECTORY |
---|
[435] | 1847 | cycle = (uint32_t)hal_get_cycles(); |
---|
[623] | 1848 | if( DEBUG_FATFS_SCAN_DIRECTORY < cycle ) |
---|
| 1849 | printk("\n[%s] thread[%x,%x] exit / child <%s> in <%s> not found\n", |
---|
| 1850 | __FUNCTION__, this->process->pid, this->trdid, name, parent_name ); |
---|
[435] | 1851 | #endif |
---|
[623] | 1852 | return 1; |
---|
| 1853 | } |
---|
| 1854 | else |
---|
| 1855 | { |
---|
| 1856 | printk("\n[ERROR] in %s : cannot get page %d from mapper\n", |
---|
| 1857 | __FUNCTION__, page_id ); |
---|
[407] | 1858 | |
---|
[602] | 1859 | return -1; |
---|
[238] | 1860 | } |
---|
[623] | 1861 | } // end fatfs_scan_directory() |
---|
[602] | 1862 | |
---|
| 1863 | |
---|
[610] | 1864 | |
---|
[623] | 1865 | ///////////////////////////////////////////////////// |
---|
| 1866 | error_t fatfs_new_dentry( vfs_inode_t * parent_inode, |
---|
| 1867 | char * name, |
---|
| 1868 | xptr_t child_inode_xp ) |
---|
| 1869 | { |
---|
[625] | 1870 | uint8_t * entry; // pointer on FAT32 directory entry (array of 32 bytes) |
---|
| 1871 | uint32_t index; // index of FAT32 directory entry in mapper |
---|
| 1872 | mapper_t * mapper; // pointer on directory mapper |
---|
| 1873 | uint32_t cluster; // directory entry cluster |
---|
| 1874 | uint32_t size; // directory entry size |
---|
| 1875 | bool_t is_dir; // directory entry type (file/dir) |
---|
| 1876 | xptr_t root_xp; // extended pointer on root of parent dentries |
---|
| 1877 | xptr_t iter_xp; // iterator for this list |
---|
| 1878 | cxy_t child_inode_cxy; // child inode cluster |
---|
| 1879 | vfs_inode_t * child_inode_ptr; // child inode local pointer |
---|
| 1880 | xptr_t dentry_xp; // extended pointer on searched dentry descriptor |
---|
| 1881 | cxy_t dentry_cxy; // cluster identifier of dentry (must be local_cxy) |
---|
| 1882 | vfs_dentry_t * dentry_ptr; // local pointer |
---|
| 1883 | error_t error; |
---|
[623] | 1884 | |
---|
[628] | 1885 | char dir_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
[625] | 1886 | |
---|
[623] | 1887 | // check arguments |
---|
[626] | 1888 | assert( (parent_inode != NULL) , "parent_inode is NULL\n" ); |
---|
| 1889 | assert( (name != NULL) , "name is NULL\n" ); |
---|
| 1890 | assert( (child_inode_xp != XPTR_NULL ) , "child_inode is NULL\n" ); |
---|
[623] | 1891 | |
---|
[626] | 1892 | // get child inode cluster and local pointer |
---|
| 1893 | child_inode_cxy = GET_CXY( child_inode_xp ); |
---|
| 1894 | child_inode_ptr = GET_PTR( child_inode_xp ); |
---|
| 1895 | |
---|
| 1896 | // build extended pointer on root of list of parent dentries |
---|
| 1897 | root_xp = XPTR( child_inode_cxy , &child_inode_ptr->parents ); |
---|
| 1898 | |
---|
| 1899 | // check child inode has at least one parent |
---|
| 1900 | assert( (xlist_is_empty( root_xp ) == false ), "child inode must have one parent\n"); |
---|
| 1901 | |
---|
[623] | 1902 | #if DEBUG_FATFS_GET_DENTRY |
---|
| 1903 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1904 | thread_t * this = CURRENT_THREAD; |
---|
[625] | 1905 | vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , dir_name ); |
---|
[623] | 1906 | if( DEBUG_FATFS_GET_DENTRY < cycle ) |
---|
| 1907 | printk("\n[%s] thread[%x,%x] enter for child <%s> in parent <%s> / cycle %d\n", |
---|
[625] | 1908 | __FUNCTION__, this->process->pid, this->trdid, name , dir_name , cycle ); |
---|
[623] | 1909 | #endif |
---|
| 1910 | |
---|
[625] | 1911 | // get local pointer on parent mapper |
---|
[623] | 1912 | mapper = parent_inode->mapper; |
---|
[625] | 1913 | |
---|
| 1914 | // get pointer and index in mapper for searched directory entry |
---|
[623] | 1915 | error = fatfs_scan_directory( mapper, name , &entry , &index ); |
---|
| 1916 | |
---|
[626] | 1917 | // return non fatal error if not found |
---|
| 1918 | if( error ) return -1; |
---|
[623] | 1919 | |
---|
[625] | 1920 | // get relevant infos from FAT32 directory entry |
---|
[626] | 1921 | cluster = (fatfs_get_record( DIR_FST_CLUS_HI , entry ) << 16) | |
---|
| 1922 | (fatfs_get_record( DIR_FST_CLUS_LO , entry ) ) ; |
---|
| 1923 | is_dir = (fatfs_get_record( DIR_ATTR , entry ) & ATTR_DIRECTORY); |
---|
| 1924 | size = fatfs_get_record( DIR_FILE_SIZE , entry ); |
---|
[623] | 1925 | |
---|
[625] | 1926 | // scan list of parent dentries to search the parent_inode |
---|
| 1927 | bool_t found = false; |
---|
| 1928 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 1929 | { |
---|
| 1930 | // get pointers on dentry |
---|
| 1931 | dentry_xp = XLIST_ELEMENT( iter_xp , vfs_dentry_t , parents ); |
---|
| 1932 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
| 1933 | dentry_ptr = GET_PTR( dentry_xp ); |
---|
[602] | 1934 | |
---|
[625] | 1935 | // get local pointer on current parent directory inode |
---|
| 1936 | vfs_inode_t * current = hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->parent ) ); |
---|
[602] | 1937 | |
---|
[625] | 1938 | // check if current parent is the searched parent |
---|
| 1939 | if( XPTR( dentry_cxy , current ) == XPTR( local_cxy , parent_inode ) ) |
---|
| 1940 | { |
---|
| 1941 | found = true; |
---|
| 1942 | break; |
---|
| 1943 | } |
---|
| 1944 | } |
---|
[602] | 1945 | |
---|
[625] | 1946 | if( found == false ) |
---|
| 1947 | { |
---|
| 1948 | vfs_inode_get_name( XPTR( local_cxy , parent_inode ) , dir_name ); |
---|
| 1949 | printk("\n[ERROR] in %s : cannot find <%s> directory in list of parents for <%s>\n", |
---|
| 1950 | __FUNCTION__, dir_name, name ); |
---|
[623] | 1951 | return -1; |
---|
| 1952 | } |
---|
| 1953 | |
---|
[625] | 1954 | // update the child inode "type", "size", and "extend" fields |
---|
| 1955 | vfs_inode_type_t type = (is_dir) ? INODE_TYPE_DIR : INODE_TYPE_FILE; |
---|
| 1956 | |
---|
| 1957 | hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->type ) , type ); |
---|
| 1958 | hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->size ) , size ); |
---|
| 1959 | hal_remote_s32( XPTR( child_inode_cxy , &child_inode_ptr->extend ) , cluster ); |
---|
| 1960 | |
---|
| 1961 | // update the dentry "extend" field |
---|
| 1962 | dentry_ptr->extend = (void *)(intptr_t)index; |
---|
| 1963 | |
---|
| 1964 | #if DEBUG_FATFS_GET_DENTRY |
---|
| 1965 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1966 | if( DEBUG_FATFS_GET_DENTRY < cycle ) |
---|
| 1967 | printk("\n[%s] thread[%x,%x] exit / intialised inode & dentry for <%s> in <%s> / cycle %d\n", |
---|
| 1968 | __FUNCTION__, this->process->pid, this->trdid, name, dir_name, cycle ); |
---|
| 1969 | #endif |
---|
| 1970 | |
---|
| 1971 | return 0; |
---|
| 1972 | |
---|
[623] | 1973 | } // end fatfs_new_dentry() |
---|
| 1974 | |
---|
| 1975 | ////////////////////////////////////////////////// |
---|
| 1976 | error_t fatfs_update_dentry( vfs_inode_t * inode, |
---|
| 1977 | vfs_dentry_t * dentry, |
---|
| 1978 | uint32_t size ) |
---|
| 1979 | { |
---|
| 1980 | uint8_t * entry; // pointer on FAT32 directory entry (array of 32 bytes) |
---|
| 1981 | uint32_t index; // index of FAT32 directory entry in mapper |
---|
| 1982 | mapper_t * mapper; // pointer on directory mapper |
---|
| 1983 | error_t error; |
---|
| 1984 | |
---|
[625] | 1985 | char dir_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 1986 | |
---|
[623] | 1987 | // check arguments |
---|
| 1988 | assert( (inode != NULL) , "inode is NULL\n" ); |
---|
| 1989 | assert( (dentry != NULL) , "dentry is NULL\n" ); |
---|
| 1990 | assert( (size != 0 ) , "size is 0\n" ); |
---|
| 1991 | |
---|
| 1992 | #if DEBUG_FATFS_UPDATE_DENTRY |
---|
| 1993 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1994 | thread_t * this = CURRENT_THREAD; |
---|
| 1995 | vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name ); |
---|
| 1996 | if( DEBUG_FATFS_UPDATE_DENTRY < cycle ) |
---|
[625] | 1997 | printk("\n[%s] thread[%x,%x] enter for <%s/%s> / size %d / cycle %d\n", |
---|
| 1998 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, size, cycle ); |
---|
[623] | 1999 | #endif |
---|
| 2000 | |
---|
[625] | 2001 | // get local pointer on mapper |
---|
[623] | 2002 | mapper = inode->mapper; |
---|
[625] | 2003 | |
---|
| 2004 | // get pointer and index in mapper for searched directory entry |
---|
[623] | 2005 | error = fatfs_scan_directory( mapper, dentry->name , &entry , &index ); |
---|
| 2006 | |
---|
[625] | 2007 | if( error ) |
---|
[623] | 2008 | { |
---|
[625] | 2009 | vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name ); |
---|
| 2010 | printk("\n[ERROR] in %s : cannot find <%s> in parent mapper <%s>\n", |
---|
| 2011 | __FUNCTION__, dentry->name, dir_name ); |
---|
| 2012 | return -1; |
---|
| 2013 | } |
---|
[623] | 2014 | |
---|
[625] | 2015 | // set size in FAT32 directory entry |
---|
[626] | 2016 | fatfs_set_record( DIR_FILE_SIZE , entry , size ); |
---|
[602] | 2017 | |
---|
[625] | 2018 | // get local pointer on modified page base |
---|
| 2019 | void * base = (void *)((intptr_t)entry & (~CONFIG_PPM_PAGE_MASK)); |
---|
[602] | 2020 | |
---|
[625] | 2021 | // get extended pointer on modified page descriptor |
---|
| 2022 | xptr_t page_xp = ppm_base2page( XPTR( local_cxy , base ) ); |
---|
[602] | 2023 | |
---|
[625] | 2024 | // synchronously update the modified page on device |
---|
| 2025 | error = fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[623] | 2026 | |
---|
[625] | 2027 | if( error ) |
---|
| 2028 | { |
---|
| 2029 | vfs_inode_get_name( XPTR( local_cxy , inode ) , dir_name ); |
---|
| 2030 | printk("\n[ERROR] in %s : cannot update parent directory <%s> on device\n", |
---|
| 2031 | __FUNCTION__, dir_name ); |
---|
[623] | 2032 | return -1; |
---|
| 2033 | } |
---|
| 2034 | |
---|
[625] | 2035 | #if DEBUG_FATFS_UPDATE_DENTRY |
---|
| 2036 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2037 | if( DEBUG_FATFS_UPDATE_DENTRY < cycle ) |
---|
| 2038 | printk("\n[%s] thread[%x,%x] exit / updated size for <%s/%s> / cycle %d\n", |
---|
| 2039 | __FUNCTION__, this->process->pid, this->trdid, dir_name, dentry->name, cycle ); |
---|
| 2040 | #endif |
---|
| 2041 | |
---|
| 2042 | return 0; |
---|
| 2043 | |
---|
[623] | 2044 | } // end fatfs_update_dentry() |
---|
| 2045 | |
---|
[612] | 2046 | /////////////////////////////////////////////////////// |
---|
| 2047 | error_t fatfs_get_user_dir( struct vfs_inode_s * inode, |
---|
| 2048 | struct dirent * array, |
---|
| 2049 | uint32_t max_dirent, |
---|
| 2050 | uint32_t min_dentry, |
---|
| 2051 | bool_t detailed, |
---|
| 2052 | uint32_t * entries, |
---|
| 2053 | bool_t * done ) |
---|
| 2054 | { |
---|
| 2055 | // Two embedded loops to scan the directory mapper: |
---|
| 2056 | // - scan the parent directory mapper pages starting always from page 0 |
---|
| 2057 | // - scan the 32 bytes NORMAL/LFN directory entries in each page |
---|
| 2058 | // Only valid dentries are copied : dentry_id >= min_dentry && dirent_id < dirent_max |
---|
| 2059 | |
---|
| 2060 | #if DEBUG_FATFS_GET_USER_DIR |
---|
| 2061 | char inode_name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2062 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2063 | thread_t * this = CURRENT_THREAD; |
---|
| 2064 | vfs_inode_get_name( XPTR( local_cxy , inode ) , inode_name ); |
---|
| 2065 | if( DEBUG_FATFS_GET_USER_DIR < cycle ) |
---|
| 2066 | printk("\n[%s] thread[%x,%x] enter for inode <%s> / cycle %d\n", |
---|
| 2067 | __FUNCTION__, this->process->pid, this->trdid, inode_name , cycle ); |
---|
| 2068 | #endif |
---|
| 2069 | |
---|
| 2070 | mapper_t * mapper = inode->mapper; |
---|
| 2071 | xptr_t mapper_xp = XPTR( local_cxy , mapper ); |
---|
| 2072 | |
---|
| 2073 | // check mapper pointer |
---|
| 2074 | assert( (mapper != NULL) , "mapper is NULL\n"); |
---|
| 2075 | |
---|
| 2076 | // TODO handle the detailed flag |
---|
| 2077 | assert( (detailed == false), "detailed argument not supported/n"); |
---|
| 2078 | |
---|
[614] | 2079 | char cname[CONFIG_VFS_MAX_NAME_LENGTH]; // name extracted from each dentry |
---|
[612] | 2080 | |
---|
| 2081 | char lfn1[16]; // buffer for one partial cname |
---|
| 2082 | char lfn2[16]; // buffer for one partial cname |
---|
| 2083 | char lfn3[16]; // buffer for one partial cname |
---|
| 2084 | xptr_t page_xp; // extended pointer on page descriptor |
---|
| 2085 | xptr_t base_xp; // extended pointer on page base |
---|
| 2086 | uint8_t * base; // local pointer on page base |
---|
[614] | 2087 | uint8_t attr; // directory entry ATTR field |
---|
| 2088 | uint8_t ord; // directory entry ORD field |
---|
[612] | 2089 | uint32_t seq; // sequence index |
---|
| 2090 | uint32_t lfn = 0; // LFN entries number |
---|
| 2091 | uint32_t offset = 0; // byte offset in page |
---|
| 2092 | uint32_t page_id = 0; // page index in mapper |
---|
| 2093 | uint32_t dentry_id = 0; // valid (i.e. copied) dentry index in mapper |
---|
| 2094 | uint32_t dirent_id = 0; // slot index in dirent array to initialize |
---|
| 2095 | bool_t end = false; // true if end of directory found |
---|
| 2096 | |
---|
| 2097 | // loop on mapper pages |
---|
| 2098 | while ( (end == false) && (dirent_id < max_dirent) ) |
---|
| 2099 | { |
---|
| 2100 | // get one page from mapper |
---|
| 2101 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
| 2102 | |
---|
| 2103 | if( page_xp == XPTR_NULL) return -1; |
---|
| 2104 | |
---|
| 2105 | // get page base |
---|
| 2106 | base_xp = ppm_page2base( page_xp ); |
---|
| 2107 | base = (uint8_t *)GET_PTR( base_xp ); |
---|
| 2108 | |
---|
| 2109 | #if (DEBUG_FATFS_GET_USER_DIR & 0x1) |
---|
| 2110 | if( DEBUG_FATFS_GET_USER_DIR < cycle ) |
---|
[614] | 2111 | mapper_display_page( mapper_xp , page_id , 256 ); |
---|
[612] | 2112 | #endif |
---|
| 2113 | // loop on NORMAL/LFN (32 bytes) directory entries in this page |
---|
| 2114 | while( (end == false) && (offset < 4096) ) |
---|
| 2115 | { |
---|
| 2116 | // compute condition to copy one dentry to dirent array |
---|
| 2117 | bool_t valid = (dentry_id >= min_dentry) && (dirent_id < max_dirent ); |
---|
| 2118 | |
---|
[626] | 2119 | attr = fatfs_get_record( DIR_ATTR , base + offset ); |
---|
| 2120 | ord = fatfs_get_record( LDIR_ORD , base + offset ); |
---|
[612] | 2121 | |
---|
| 2122 | if (ord == NO_MORE_ENTRY) // no more entry => break |
---|
| 2123 | { |
---|
| 2124 | end = true; |
---|
| 2125 | } |
---|
| 2126 | else if ( ord == FREE_ENTRY ) // free entry => skip |
---|
| 2127 | { |
---|
| 2128 | offset = offset + 32; |
---|
| 2129 | } |
---|
[614] | 2130 | else if ( attr == 0x28 ) // volune_id => skip |
---|
| 2131 | { |
---|
| 2132 | offset = offset + 32; |
---|
| 2133 | } |
---|
[612] | 2134 | else if ( attr == ATTR_LONG_NAME_MASK ) // LFN entry |
---|
| 2135 | { |
---|
| 2136 | if( valid ) |
---|
| 2137 | { |
---|
| 2138 | // get partial cname |
---|
| 2139 | seq = ord & 0x3; |
---|
| 2140 | lfn = (seq > lfn) ? seq : lfn; |
---|
| 2141 | if ( seq == 1 ) fatfs_get_name_from_long( base + offset, lfn1 ); |
---|
| 2142 | else if ( seq == 2 ) fatfs_get_name_from_long( base + offset, lfn2 ); |
---|
| 2143 | else if ( seq == 3 ) fatfs_get_name_from_long( base + offset, lfn3 ); |
---|
| 2144 | } |
---|
| 2145 | offset = offset + 32; |
---|
| 2146 | } |
---|
| 2147 | else // NORMAL entry |
---|
| 2148 | { |
---|
| 2149 | // increment dentry_id |
---|
| 2150 | dentry_id++; |
---|
| 2151 | |
---|
| 2152 | if( valid ) |
---|
| 2153 | { |
---|
| 2154 | // build the complete cname |
---|
| 2155 | if ( lfn == 0 ) |
---|
| 2156 | { |
---|
| 2157 | fatfs_get_name_from_short( base + offset , cname ); |
---|
| 2158 | } |
---|
| 2159 | else if ( lfn == 1 ) |
---|
| 2160 | { |
---|
| 2161 | strcpy( cname , lfn1 ); |
---|
| 2162 | } |
---|
| 2163 | else if ( lfn == 2 ) |
---|
| 2164 | { |
---|
| 2165 | strcpy( cname , lfn1 ); |
---|
| 2166 | strcpy( cname + 13 , lfn2 ); |
---|
| 2167 | } |
---|
| 2168 | else if ( lfn == 3 ) |
---|
| 2169 | { |
---|
| 2170 | strcpy( cname , lfn1 ); |
---|
| 2171 | strcpy( cname + 13 , lfn2 ); |
---|
| 2172 | strcpy( cname + 26 , lfn3 ); |
---|
| 2173 | } |
---|
| 2174 | |
---|
| 2175 | // copy cname into dirent array |
---|
| 2176 | strcpy( array[dirent_id].d_name , cname ); |
---|
| 2177 | |
---|
| 2178 | // increment dirent_id |
---|
| 2179 | dirent_id++; |
---|
| 2180 | } |
---|
| 2181 | offset = offset + 32; |
---|
| 2182 | lfn = 0; |
---|
| 2183 | } |
---|
| 2184 | } // end loop on directory entries in page |
---|
| 2185 | |
---|
| 2186 | page_id++; |
---|
| 2187 | offset = 0; |
---|
| 2188 | |
---|
| 2189 | } // end loop on pages |
---|
| 2190 | |
---|
| 2191 | // return result of scan |
---|
| 2192 | *done = end; |
---|
| 2193 | *entries = dirent_id; |
---|
| 2194 | |
---|
| 2195 | #if DEBUG_FATFS_GET_USER_DIR |
---|
| 2196 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2197 | if( DEBUG_FATFS_GET_USER_DIR < cycle ) |
---|
| 2198 | printk("\n[%s] thread[%x,%x] exit for inode <%s> / %d entries / cycle %d\n", |
---|
[614] | 2199 | __FUNCTION__, this->process->pid, this->trdid, inode_name, dirent_id, cycle ); |
---|
[612] | 2200 | #endif |
---|
| 2201 | |
---|
| 2202 | return 0; |
---|
| 2203 | |
---|
| 2204 | } // end fatfs_get_user_dir() |
---|
| 2205 | |
---|
[602] | 2206 | /////////////////////////////////////////////// |
---|
| 2207 | error_t fatfs_sync_inode( vfs_inode_t * inode ) |
---|
| 2208 | { |
---|
| 2209 | |
---|
| 2210 | // check inode pointer and cluster index |
---|
| 2211 | assert( (inode != NULL) , "inode pointer undefined\n" ); |
---|
| 2212 | assert( (inode->mapper != NULL ) , "mapper pointer undefined\n" ); |
---|
| 2213 | assert( (inode->type == INODE_TYPE_FILE) , "inode must be a file\n" ); |
---|
| 2214 | |
---|
| 2215 | #if DEBUG_FATFS_SYNC_INODE |
---|
| 2216 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2217 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2218 | thread_t * this = CURRENT_THREAD; |
---|
| 2219 | vfs_inode_get_name( XPTR( local_cxy , inode ) , name ); |
---|
| 2220 | if( DEBUG_FATFS_SYNC_INODE < cycle ) |
---|
| 2221 | printk("\n[%s] thread[%x,%x] enter for <%s> / cycle %d\n", |
---|
| 2222 | __FUNCTION__ , this->process->pid, this->trdid, name, cycle ); |
---|
| 2223 | #endif |
---|
| 2224 | |
---|
| 2225 | error_t error; |
---|
| 2226 | mapper_t * mapper; |
---|
| 2227 | page_t * page; |
---|
| 2228 | uint32_t page_id; |
---|
| 2229 | |
---|
| 2230 | // get mapper from inode |
---|
| 2231 | mapper = inode->mapper; |
---|
| 2232 | |
---|
| 2233 | // compute max number of pages in mapper from file size |
---|
| 2234 | uint32_t size = inode->size; |
---|
| 2235 | uint32_t pages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 2236 | if( size & CONFIG_PPM_PAGE_MASK ) pages++; |
---|
| 2237 | |
---|
| 2238 | // get pointer on mapper radix tree |
---|
| 2239 | grdxt_t * rt = &mapper->rt; |
---|
| 2240 | |
---|
| 2241 | // scan all pages |
---|
| 2242 | for( page_id = 0 ; page_id < pages ; page_id++ ) |
---|
[238] | 2243 | { |
---|
[602] | 2244 | // get page descriptor from mapper |
---|
| 2245 | page = grdxt_lookup( rt , page_id ); |
---|
[238] | 2246 | |
---|
[602] | 2247 | // check all existing pages |
---|
| 2248 | if ( page != NULL ) |
---|
| 2249 | { |
---|
| 2250 | if ( page->flags & PG_DIRTY ) |
---|
| 2251 | { |
---|
[238] | 2252 | |
---|
[602] | 2253 | #if (DEBUG_FATFS_SYNC_INODE & 1) |
---|
| 2254 | if( DEBUG_FATFS_SYNC_INODE < cycle ) |
---|
| 2255 | printk("\n[%s] thread[%x,%x] synchronizes page %d from <%s> mapper to IOC device\n", |
---|
| 2256 | __FUNCTION__, page_id, name ); |
---|
| 2257 | #endif |
---|
| 2258 | // build extended pointer on page descriptor |
---|
| 2259 | xptr_t page_xp = XPTR( local_cxy , page ); |
---|
[238] | 2260 | |
---|
[602] | 2261 | // move page from mapper to device |
---|
[614] | 2262 | error = fatfs_move_page( page_xp , IOC_WRITE ); |
---|
[602] | 2263 | |
---|
| 2264 | if ( error ) return -1; |
---|
| 2265 | |
---|
| 2266 | // reset page dirty flag |
---|
| 2267 | ppm_page_undo_dirty( page_xp ); |
---|
| 2268 | } |
---|
| 2269 | } |
---|
| 2270 | } // end loop on pages |
---|
| 2271 | |
---|
| 2272 | #if DEBUG_FATFS_SYNC_INODE |
---|
[435] | 2273 | cycle = (uint32_t)hal_get_cycles(); |
---|
[602] | 2274 | if( DEBUG_FATFS_SYNC_INODE < cycle ) |
---|
| 2275 | printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n", |
---|
| 2276 | __FUNCTION__ , this->process->pid, this->trdid, name, cycle ); |
---|
[435] | 2277 | #endif |
---|
[246] | 2278 | |
---|
[602] | 2279 | return 0; |
---|
| 2280 | |
---|
| 2281 | } // end fatfs_sync_inode() |
---|
| 2282 | |
---|
[628] | 2283 | |
---|
| 2284 | |
---|
| 2285 | |
---|
| 2286 | |
---|
| 2287 | |
---|
[602] | 2288 | ////////////////////////////// |
---|
| 2289 | error_t fatfs_sync_fat( void ) |
---|
| 2290 | { |
---|
| 2291 | |
---|
| 2292 | #if DEBUG_FATFS_SYNC_FAT |
---|
| 2293 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2294 | thread_t * this = CURRENT_THREAD; |
---|
| 2295 | if( DEBUG_FATFS_SYNC_FAT < cycle ) |
---|
| 2296 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
| 2297 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
| 2298 | #endif |
---|
| 2299 | |
---|
| 2300 | uint32_t page_id; |
---|
| 2301 | error_t error; |
---|
| 2302 | |
---|
| 2303 | // get FAT mapper pointers an cluster |
---|
| 2304 | fatfs_ctx_t * fatfs_ctx = fs_context[FS_TYPE_FATFS].extend; |
---|
| 2305 | xptr_t mapper_xp = fatfs_ctx->fat_mapper_xp; |
---|
| 2306 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
| 2307 | mapper_t * mapper_ptr = GET_PTR( mapper_xp ); |
---|
| 2308 | |
---|
| 2309 | // compute max number of 4 Kbytes pages in FAT mapper |
---|
| 2310 | // TODO : this could be improved (see fatfs.h) [AG] |
---|
| 2311 | uint32_t pages = fatfs_ctx->fat_sectors_count >> 3; |
---|
| 2312 | |
---|
| 2313 | // get pointers on remote FAT mapper radix tree |
---|
| 2314 | grdxt_t * rt_ptr = &mapper_ptr->rt; |
---|
| 2315 | xptr_t rt_xp = XPTR( mapper_cxy , rt_ptr ); |
---|
| 2316 | |
---|
| 2317 | // scan all pages |
---|
| 2318 | for( page_id = 0 ; page_id < pages ; page_id++ ) |
---|
| 2319 | { |
---|
| 2320 | // get extended pointer on page descriptor from FAT mapper |
---|
| 2321 | xptr_t page_xp = grdxt_remote_lookup( rt_xp , page_id ); |
---|
| 2322 | |
---|
| 2323 | // check all existing pages |
---|
| 2324 | if ( page_xp != XPTR_NULL ) |
---|
| 2325 | { |
---|
| 2326 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
| 2327 | uint32_t flags = hal_remote_l32( XPTR( mapper_cxy , &page_ptr->flags ) ); |
---|
| 2328 | |
---|
| 2329 | if ( flags & PG_DIRTY ) |
---|
| 2330 | { |
---|
| 2331 | |
---|
| 2332 | #if (DEBUG_FATFS_SYNC_FAT & 1) |
---|
| 2333 | if( DEBUG_FATFS_SYNC_FAT < cycle ) |
---|
| 2334 | printk("\n[%s] thread[%x,%x] synchronizes page %d from FAT mapper to IOC device\n", |
---|
| 2335 | __FUNCTION__, page_id ); |
---|
| 2336 | #endif |
---|
| 2337 | // move page from mapper to device |
---|
[614] | 2338 | error = fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[602] | 2339 | |
---|
| 2340 | if ( error ) return -1; |
---|
| 2341 | |
---|
| 2342 | // reset page dirty flag |
---|
| 2343 | ppm_page_undo_dirty( page_xp ); |
---|
| 2344 | } |
---|
| 2345 | } |
---|
| 2346 | } // end loop on pages |
---|
| 2347 | |
---|
| 2348 | #if DEBUG_FATFS_SYNC_FAT |
---|
| 2349 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2350 | if( DEBUG_FATFS_SYNC_FAT < cycle ) |
---|
| 2351 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 2352 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
| 2353 | #endif |
---|
| 2354 | |
---|
| 2355 | return 0; |
---|
| 2356 | |
---|
| 2357 | } // end fatfs_sync_fat() |
---|
| 2358 | |
---|
| 2359 | //////////////////////////////////// |
---|
| 2360 | error_t fatfs_sync_free_info( void ) |
---|
| 2361 | { |
---|
[626] | 2362 | error_t error; |
---|
| 2363 | fatfs_ctx_t * fatfs_ctx_ptr; // local pointer on fatfs context in cluster 0 |
---|
| 2364 | uint32_t ctx_free_clusters; // number of free clusters from fatfs context |
---|
| 2365 | uint32_t ctx_free_cluster_hint; // free cluster hint from fatfs context |
---|
| 2366 | uint32_t ioc_free_clusters; // number of free clusters from fatfs context |
---|
| 2367 | uint32_t ioc_free_cluster_hint; // free cluster hint from fatfs context |
---|
| 2368 | uint32_t fs_info_lba; // lba of FS_INFO sector on IOC device |
---|
| 2369 | uint8_t * fs_info_buffer; // local pointer on FS_INFO buffer in cluster 0 |
---|
| 2370 | xptr_t fs_info_buffer_xp; // extended pointer on FS_INFO buffer in cluster 0 |
---|
| 2371 | uint8_t tmp_buf[512]; // 512 bytes temporary buffer |
---|
| 2372 | xptr_t tmp_buf_xp; // extended pointer on temporary buffer |
---|
[602] | 2373 | |
---|
| 2374 | #if DEBUG_FATFS_SYNC_FSINFO |
---|
| 2375 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2376 | thread_t * this = CURRENT_THREAD; |
---|
| 2377 | if( DEBUG_FATFS_SYNC_FSINFO < cycle ) |
---|
| 2378 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
| 2379 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
| 2380 | #endif |
---|
| 2381 | |
---|
[626] | 2382 | // get pointer on fatfs context in cluster 0 |
---|
| 2383 | fatfs_ctx_ptr = hal_remote_lpt( XPTR( 0 , &fs_context[FS_TYPE_FATFS].extend ) ); |
---|
[602] | 2384 | |
---|
[626] | 2385 | // get "free_clusters" and "free_cluster_hint" from fatfs context in cluster 0 |
---|
| 2386 | ctx_free_clusters = hal_remote_l32( XPTR( 0 , &fatfs_ctx_ptr->free_clusters ) ); |
---|
| 2387 | ctx_free_cluster_hint = hal_remote_l32( XPTR( 0 , &fatfs_ctx_ptr->free_cluster_hint ) ); |
---|
[602] | 2388 | |
---|
[626] | 2389 | // get fs_info_lba |
---|
| 2390 | fs_info_lba = hal_remote_l32( XPTR( 0 , &fatfs_ctx_ptr->fs_info_lba ) ); |
---|
| 2391 | |
---|
| 2392 | // build extended pointer on temporary buffer |
---|
| 2393 | tmp_buf_xp = XPTR( local_cxy , tmp_buf ); |
---|
| 2394 | |
---|
| 2395 | // copy FS_INFO sector from IOC to local buffer |
---|
| 2396 | error = dev_ioc_sync_read( tmp_buf_xp , fs_info_lba , 1 ); |
---|
| 2397 | |
---|
[602] | 2398 | if ( error ) |
---|
| 2399 | { |
---|
[626] | 2400 | printk("\n[ERROR] in %s : cannot access FS_INFO on IOC device\n", __FUNCTION__ ); |
---|
| 2401 | return -1; |
---|
[602] | 2402 | } |
---|
| 2403 | |
---|
[626] | 2404 | // get current values of "free_clusters" and "free_cluster_hint" from FS_INFO on IOC |
---|
| 2405 | ioc_free_clusters = fatfs_get_remote_record( FS_FREE_CLUSTERS , tmp_buf_xp ); |
---|
| 2406 | ioc_free_cluster_hint = fatfs_get_remote_record( FS_FREE_CLUSTER_HINT , tmp_buf_xp ); |
---|
[602] | 2407 | |
---|
[628] | 2408 | #if DEBUG_FATFS_SYNC_FSINFO |
---|
| 2409 | if( DEBUG_FATFS_SYNC_FSINFO < cycle ) |
---|
| 2410 | printk("\n[%s] thread[%x,%x] / ctx_free %x / ioc_free %x / ctx_hint %x / ioc_hint %x\n", |
---|
| 2411 | __FUNCTION__ , this->process->pid, this->trdid, |
---|
| 2412 | ctx_free_clusters, ioc_free_clusters, ctx_free_cluster_hint, ioc_free_cluster_hint ); |
---|
| 2413 | #endif |
---|
| 2414 | |
---|
[626] | 2415 | // check values |
---|
| 2416 | if( (ioc_free_clusters != ctx_free_clusters) || |
---|
| 2417 | (ioc_free_cluster_hint != ctx_free_cluster_hint) ) |
---|
[602] | 2418 | { |
---|
[626] | 2419 | printk("\n[WARNING] in %s : unconsistent free clusters info\n" |
---|
| 2420 | " ioc_free %x / ctx_free %x / ioc_hint %x / ctx_hint %x\n", |
---|
| 2421 | __FUNCTION__, ioc_free_clusters, ctx_free_clusters, |
---|
| 2422 | ioc_free_cluster_hint, ctx_free_cluster_hint ); |
---|
| 2423 | |
---|
| 2424 | // get pointers on FS_INFO buffer in cluster 0 |
---|
| 2425 | fs_info_buffer = hal_remote_lpt( XPTR( 0 , &fatfs_ctx_ptr->fs_info_buffer ) ); |
---|
| 2426 | fs_info_buffer_xp = XPTR( 0 , fs_info_buffer ); |
---|
| 2427 | |
---|
| 2428 | // update FS_INFO buffer in cluster 0 |
---|
| 2429 | fatfs_set_remote_record(FS_FREE_CLUSTERS ,fs_info_buffer_xp,ctx_free_clusters ); |
---|
| 2430 | fatfs_set_remote_record(FS_FREE_CLUSTER_HINT,fs_info_buffer_xp,ctx_free_cluster_hint); |
---|
| 2431 | |
---|
| 2432 | // update the FS_INFO sector on IOC device |
---|
| 2433 | error = dev_ioc_sync_write( fs_info_buffer_xp , fs_info_lba , 1 ); |
---|
| 2434 | |
---|
| 2435 | if ( error ) |
---|
| 2436 | { |
---|
| 2437 | printk("\n[ERROR] in %s : cannot update FS_INFO on IOC device\n", __FUNCTION__ ); |
---|
| 2438 | return -1; |
---|
| 2439 | } |
---|
[602] | 2440 | } |
---|
| 2441 | |
---|
| 2442 | #if DEBUG_FATFS_SYNC_FSINFO |
---|
| 2443 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2444 | if( DEBUG_FATFS_SYNC_FSINFO < cycle ) |
---|
| 2445 | printk("\n[%s] thread[%x,%x] exit / cycle %d\n", |
---|
| 2446 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
| 2447 | #endif |
---|
| 2448 | |
---|
| 2449 | return 0; |
---|
| 2450 | |
---|
[626] | 2451 | } // end fatfs_sync_free_info() |
---|
[602] | 2452 | |
---|
| 2453 | ////////////////////////////////////////////////////////// |
---|
| 2454 | error_t fatfs_cluster_alloc( uint32_t * searched_cluster ) |
---|
| 2455 | { |
---|
[626] | 2456 | error_t error; |
---|
[625] | 2457 | uint32_t page_id; // page index in FAT mapper |
---|
[602] | 2458 | uint32_t slot_id; // slot index in page (1024 slots per page) |
---|
[625] | 2459 | uint32_t cluster; // first free cluster index in FAT |
---|
[602] | 2460 | uint32_t free_clusters; // total number of free clusters |
---|
| 2461 | vfs_ctx_t * vfs_ctx; // local pointer on VFS context (same in all clusters) |
---|
| 2462 | fatfs_ctx_t * loc_fatfs_ctx; // local pointer on local FATFS context |
---|
| 2463 | fatfs_ctx_t * fat_fatfs_ctx; // local pointer on FATFS context in FAT cluster |
---|
[628] | 2464 | xptr_t fat_mapper_xp; // extended pointer on FAT mapper |
---|
[626] | 2465 | cxy_t fat_cxy; // Fat mapper cluster identifier |
---|
[602] | 2466 | xptr_t page_xp; // extended pointer on current page descriptor in mapper |
---|
| 2467 | xptr_t slot_xp; // extended pointer on FAT slot defined by hint |
---|
| 2468 | xptr_t lock_xp; // extended pointer on lock protecting free clusters info |
---|
| 2469 | xptr_t hint_xp; // extended pointer on free_cluster_hint in FAT cluster |
---|
[626] | 2470 | xptr_t free_xp; // extended pointer on free_clusters_number in FAT cluster |
---|
[602] | 2471 | |
---|
| 2472 | #if DEBUG_FATFS_CLUSTER_ALLOC |
---|
| 2473 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2474 | thread_t * this = CURRENT_THREAD; |
---|
| 2475 | if( DEBUG_FATFS_CLUSTER_ALLOC < cycle ) |
---|
| 2476 | printk("\n[%s] thread[%x,%x] enter / cycle = %d\n", |
---|
| 2477 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
| 2478 | #endif |
---|
| 2479 | |
---|
| 2480 | // get local pointer on VFS context (same in all clusters) |
---|
| 2481 | vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 2482 | |
---|
| 2483 | // get local pointer on local FATFS context |
---|
| 2484 | loc_fatfs_ctx = vfs_ctx->extend; |
---|
| 2485 | |
---|
[626] | 2486 | // get extended pointer on FAT mapper |
---|
[628] | 2487 | fat_mapper_xp = loc_fatfs_ctx->fat_mapper_xp; |
---|
[626] | 2488 | |
---|
| 2489 | // get FAT cluster |
---|
[628] | 2490 | fat_cxy = GET_CXY( fat_mapper_xp ); |
---|
[602] | 2491 | |
---|
| 2492 | // get local pointer on FATFS context in FAT cluster |
---|
[626] | 2493 | fat_fatfs_ctx = hal_remote_lpt( XPTR( fat_cxy , &vfs_ctx->extend ) ); |
---|
[602] | 2494 | |
---|
[625] | 2495 | // build relevant extended pointers on free clusters info in mapper cluster |
---|
[627] | 2496 | lock_xp = XPTR( fat_cxy , &fat_fatfs_ctx->lock ); |
---|
[626] | 2497 | hint_xp = XPTR( fat_cxy , &fat_fatfs_ctx->free_cluster_hint ); |
---|
| 2498 | free_xp = XPTR( fat_cxy , &fat_fatfs_ctx->free_clusters ); |
---|
[602] | 2499 | |
---|
[627] | 2500 | // take the FAT lock in write mode |
---|
| 2501 | remote_rwlock_wr_acquire( lock_xp ); |
---|
[602] | 2502 | |
---|
[626] | 2503 | // get hint and free_clusters values from FATFS context in FAT cluster |
---|
[625] | 2504 | cluster = hal_remote_l32( hint_xp ) + 1; |
---|
[626] | 2505 | free_clusters = hal_remote_l32( free_xp ); |
---|
[602] | 2506 | |
---|
| 2507 | #if (DEBUG_FATFS_CLUSTER_ALLOC & 1) |
---|
| 2508 | if( DEBUG_FATFS_CLUSTER_ALLOC < cycle ) |
---|
[625] | 2509 | printk("\n[%s] thread[%x,%x] get free info : hint %x / free_clusters %x\n", |
---|
| 2510 | __FUNCTION__, this->process->pid, this->trdid, (cluster - 1), free_clusters ); |
---|
[602] | 2511 | #endif |
---|
| 2512 | |
---|
| 2513 | // check "free_clusters" |
---|
| 2514 | if ( free_clusters == 0 ) |
---|
| 2515 | { |
---|
| 2516 | printk("\n[ERROR] in %s : no more free FATFS clusters\n", __FUNCTION__ ); |
---|
[627] | 2517 | remote_rwlock_wr_release( lock_xp ); |
---|
[602] | 2518 | return -1; |
---|
| 2519 | } |
---|
| 2520 | else if ( free_clusters < CONFIG_VFS_FREE_CLUSTERS_MIN ) |
---|
| 2521 | { |
---|
| 2522 | printk("\n[WARNING] in %s : only %n free FATFS clusters\n", |
---|
| 2523 | __FUNCTION__, CONFIG_VFS_FREE_CLUSTERS_MIN ); |
---|
| 2524 | } |
---|
| 2525 | |
---|
[625] | 2526 | // get page index & slot index for selected cluster |
---|
| 2527 | page_id = cluster >> 10; |
---|
| 2528 | slot_id = cluster & 0x3FF; |
---|
| 2529 | |
---|
[626] | 2530 | // get relevant page descriptor from FAT mapper |
---|
[628] | 2531 | page_xp = mapper_remote_get_page( fat_mapper_xp , page_id ); |
---|
[625] | 2532 | |
---|
| 2533 | if( page_xp == XPTR_NULL ) |
---|
| 2534 | { |
---|
| 2535 | printk("\n[ERROR] in %s : cannot acces FAT mapper\n", __FUNCTION__ ); |
---|
[627] | 2536 | remote_rwlock_wr_release( lock_xp ); |
---|
[625] | 2537 | return -1; |
---|
| 2538 | } |
---|
| 2539 | |
---|
| 2540 | // build extended pointer on selected cluster slot in FAT mapper |
---|
| 2541 | slot_xp = ppm_page2base( page_xp ) + (slot_id << 2); |
---|
| 2542 | |
---|
| 2543 | // check selected cluster actually free |
---|
[602] | 2544 | if( hal_remote_l32( slot_xp ) != FREE_CLUSTER ) |
---|
| 2545 | { |
---|
[625] | 2546 | printk("\n[ERROR] in %s : selected cluster %x not free\n", __FUNCTION__, cluster ); |
---|
[627] | 2547 | remote_rwlock_wr_release( lock_xp ); |
---|
[602] | 2548 | return -1; |
---|
| 2549 | } |
---|
| 2550 | |
---|
[626] | 2551 | // update free cluster info in FATFS context and in FS_INFO sector |
---|
| 2552 | error = fatfs_free_clusters_decrement( XPTR( fat_cxy , fat_fatfs_ctx ) , cluster ); |
---|
[602] | 2553 | |
---|
[626] | 2554 | if( error ) |
---|
| 2555 | { |
---|
| 2556 | printk("\n[ERROR] in %s : cannot update free cluster info\n", __FUNCTION__ ); |
---|
[627] | 2557 | remote_rwlock_wr_release( lock_xp ); |
---|
[626] | 2558 | return -1; |
---|
| 2559 | } |
---|
[602] | 2560 | |
---|
[625] | 2561 | // update FAT mapper |
---|
| 2562 | hal_remote_s32( slot_xp , END_OF_CHAIN_CLUSTER_MAX ); |
---|
| 2563 | |
---|
[628] | 2564 | // we don't mark the FAT mapper page as dirty, |
---|
| 2565 | // because we synchronously update FAT on IOC device |
---|
[626] | 2566 | error = fatfs_move_page( page_xp , IOC_SYNC_WRITE ); |
---|
[625] | 2567 | |
---|
[626] | 2568 | if( error ) |
---|
| 2569 | { |
---|
| 2570 | printk("\n[ERROR] in %s : cannot update FAT on IOC device\n", __FUNCTION__ ); |
---|
[627] | 2571 | remote_rwlock_wr_release( lock_xp ); |
---|
[626] | 2572 | return -1; |
---|
| 2573 | } |
---|
| 2574 | |
---|
[627] | 2575 | // release FAT lock |
---|
| 2576 | remote_rwlock_wr_release( lock_xp ); |
---|
[626] | 2577 | |
---|
[602] | 2578 | #if DEBUG_FATFS_CLUSTER_ALLOC |
---|
| 2579 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2580 | if( DEBUG_FATFS_CLUSTER_ALLOC < cycle ) |
---|
[626] | 2581 | printk("\n[%s] thread[%x,%x] exit / allocated cluster %x in FAT / cycle %d\n", |
---|
[625] | 2582 | __FUNCTION__, this->process->pid, this->trdid, cluster, cycle ); |
---|
[602] | 2583 | #endif |
---|
| 2584 | |
---|
[625] | 2585 | *searched_cluster = cluster; |
---|
[602] | 2586 | return 0; |
---|
| 2587 | |
---|
| 2588 | } // end fat_cluster_alloc() |
---|
| 2589 | |
---|
| 2590 | ////////////////////////////////////////////// |
---|
| 2591 | error_t fatfs_release_inode( xptr_t inode_xp ) |
---|
| 2592 | { |
---|
| 2593 | vfs_ctx_t * vfs_ctx; // local pointer on VFS context (same in all clusters). |
---|
| 2594 | fatfs_ctx_t * loc_fatfs_ctx; // local pointer on local FATFS context |
---|
[628] | 2595 | cxy_t fat_cxy; // FAT cluster identifier |
---|
| 2596 | xptr_t fatfs_ctx_xp; // extended pointer on FATFS context in FAT cluster |
---|
| 2597 | fatfs_ctx_t * fatfs_ctx_ptr; // local pointer on FATFS context in FAT cluster |
---|
| 2598 | xptr_t fat_mapper_xp; // extended pointer on FAT mapper |
---|
| 2599 | xptr_t lock_xp; // extended pointer on lock protecting FAT. |
---|
[602] | 2600 | xptr_t first_xp; // extended pointer on inode extension |
---|
| 2601 | uint32_t first_cluster; // first cluster index for released inode |
---|
[626] | 2602 | vfs_inode_t * inode_ptr; // local pointer on target inode |
---|
| 2603 | cxy_t inode_cxy; // target inode cluster identifier |
---|
[628] | 2604 | error_t error; |
---|
[602] | 2605 | |
---|
| 2606 | // check inode pointer |
---|
| 2607 | assert( (inode_xp != XPTR_NULL) , "inode pointer is NULL\n" ); |
---|
| 2608 | |
---|
[623] | 2609 | // get inode cluster and local pointer |
---|
[602] | 2610 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 2611 | inode_cxy = GET_CXY( inode_xp ); |
---|
[623] | 2612 | |
---|
| 2613 | // get first_cluster from inode extension |
---|
[602] | 2614 | first_xp = XPTR( inode_cxy , &inode_ptr->extend ); |
---|
| 2615 | first_cluster = (uint32_t)(intptr_t)hal_remote_lpt( first_xp ); |
---|
| 2616 | |
---|
| 2617 | // check first cluster index |
---|
| 2618 | assert( (first_cluster != 0) , "inode extend is NULL\n" ); |
---|
| 2619 | |
---|
| 2620 | #if DEBUG_FATFS_RELEASE_INODE |
---|
| 2621 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2622 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2623 | thread_t * this = CURRENT_THREAD; |
---|
| 2624 | vfs_inode_get_name( inode_xp , name ); |
---|
| 2625 | if( DEBUG_FATFS_RELEASE_INODE < cycle ) |
---|
| 2626 | printk("\n[%s] thread[%x,%x] enter for <%s> / first_cluster %x / cycle %d\n", |
---|
| 2627 | __FUNCTION__ , this->process->pid, this->trdid, name, first_cluster, cycle ); |
---|
| 2628 | #endif |
---|
| 2629 | |
---|
| 2630 | // get local pointer on VFS context (same in all clusters) |
---|
| 2631 | vfs_ctx = &fs_context[FS_TYPE_FATFS]; |
---|
| 2632 | |
---|
| 2633 | // get local pointer on local FATFS context |
---|
| 2634 | loc_fatfs_ctx = vfs_ctx->extend; |
---|
| 2635 | |
---|
[633] | 2636 | // get FAT mapper cluster |
---|
[628] | 2637 | fat_mapper_xp = loc_fatfs_ctx->fat_mapper_xp; |
---|
| 2638 | fat_cxy = GET_CXY( fat_mapper_xp ); |
---|
[602] | 2639 | |
---|
[628] | 2640 | // get pointers on FATFS context in FAT cluster |
---|
| 2641 | fatfs_ctx_ptr = hal_remote_lpt( XPTR( fat_cxy , &vfs_ctx->extend ) ); |
---|
| 2642 | fatfs_ctx_xp = XPTR( fat_cxy , fatfs_ctx_ptr ); |
---|
[602] | 2643 | |
---|
[627] | 2644 | // get extended pointer on FAT lock in FAT cluster |
---|
[628] | 2645 | lock_xp = XPTR( fat_cxy , &fatfs_ctx_ptr->lock ); |
---|
[602] | 2646 | |
---|
[627] | 2647 | // take FAT lock in write mode |
---|
| 2648 | remote_rwlock_wr_acquire( lock_xp ); |
---|
[602] | 2649 | |
---|
[628] | 2650 | #if (DEBUG_FATFS_RELEASE_INODE & 0x11 == 0x11) |
---|
| 2651 | mapper_display_page( fat_mapper_xp , 0 , 4096 ); |
---|
| 2652 | #endif |
---|
| 2653 | |
---|
[602] | 2654 | // call the recursive function to release all clusters from FAT mapper |
---|
[628] | 2655 | uint32_t dirty_page_min = 0xFFFFFFFF; |
---|
| 2656 | uint32_t dirty_page_max = 0; |
---|
| 2657 | |
---|
| 2658 | if ( fatfs_recursive_release( fat_mapper_xp, |
---|
| 2659 | fatfs_ctx_xp, |
---|
| 2660 | first_cluster, |
---|
| 2661 | &dirty_page_min, |
---|
| 2662 | &dirty_page_max ) ) |
---|
[602] | 2663 | { |
---|
| 2664 | printk("\n[ERROR] in %s : cannot update FAT mapper\n", __FUNCTION__ ); |
---|
[627] | 2665 | remote_rwlock_wr_release( lock_xp ); |
---|
[602] | 2666 | return -1; |
---|
| 2667 | } |
---|
| 2668 | |
---|
| 2669 | #if (DEBUG_FATFS_RELEASE_INODE & 1) |
---|
| 2670 | if( DEBUG_FATFS_RELEASE_INODE < cycle ) |
---|
| 2671 | printk("\n[%s] inode <%s> removed from FAT mapper\n", __FUNCTION__, name ); |
---|
| 2672 | #endif |
---|
| 2673 | |
---|
[628] | 2674 | #if (DEBUG_FATFS_RELEASE_INODE & 0x11 == 0x11) |
---|
| 2675 | mapper_display_page( fat_mapper_xp , 0 , 4096 ); |
---|
| 2676 | #endif |
---|
| 2677 | |
---|
[602] | 2678 | // update FAT on IOC device (from FAT mapper) |
---|
[628] | 2679 | error = fatfs_update_ioc_fat( fatfs_ctx_xp, |
---|
| 2680 | dirty_page_min, |
---|
| 2681 | dirty_page_max ); |
---|
| 2682 | |
---|
| 2683 | if( error ) |
---|
[602] | 2684 | { |
---|
[628] | 2685 | printk("\n[ERROR] in %s : cannot update FAT on IOC device\n", __FUNCTION__ ); |
---|
[627] | 2686 | remote_rwlock_wr_release( lock_xp ); |
---|
[602] | 2687 | return -1; |
---|
| 2688 | } |
---|
| 2689 | |
---|
| 2690 | #if (DEBUG_FATFS_RELEASE_INODE & 1) |
---|
| 2691 | if( DEBUG_FATFS_RELEASE_INODE < cycle ) |
---|
| 2692 | printk("\n[%s] inode <%s> removed from FAT on IOC device\n", __FUNCTION__, name ); |
---|
| 2693 | #endif |
---|
| 2694 | |
---|
[628] | 2695 | // update FS-INFO on IOC device (from FATFS context) |
---|
| 2696 | error = fatfs_update_ioc_fsinfo( fatfs_ctx_xp ); |
---|
| 2697 | |
---|
| 2698 | if( error ) |
---|
[602] | 2699 | { |
---|
[628] | 2700 | printk("\n[ERROR] in %s: cannot update FSINFO on IOC device\n", __FUNCTION__ ); |
---|
[627] | 2701 | remote_rwlock_wr_release( lock_xp ); |
---|
[602] | 2702 | return -1; |
---|
| 2703 | } |
---|
| 2704 | |
---|
[628] | 2705 | #if (DEBUG_FATFS_RELEASE_INODE & 1) |
---|
| 2706 | if( DEBUG_FATFS_RELEASE_INODE < cycle ) |
---|
| 2707 | printk("\n[%s] updated FS_INFO on IOC device for >%s>\n", __FUNCTION__, name ); |
---|
| 2708 | #endif |
---|
| 2709 | |
---|
[627] | 2710 | // release FAT lock |
---|
| 2711 | remote_rwlock_wr_release( lock_xp ); |
---|
| 2712 | |
---|
[602] | 2713 | #if DEBUG_FATFS_RELEASE_INODE |
---|
| 2714 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 2715 | if( DEBUG_FATFS_RELEASE_INODE < cycle ) |
---|
| 2716 | printk("\n[%s] thread[%x,%x] removed <%s> inode from FATFS / cycle %d\n", |
---|
| 2717 | __FUNCTION__ , this->process->pid, this->trdid, name, cycle ); |
---|
| 2718 | #endif |
---|
| 2719 | |
---|
| 2720 | return 0; |
---|
| 2721 | |
---|
| 2722 | } // end fatfs_release_inode() |
---|
| 2723 | |
---|
[614] | 2724 | //////////////////////////////////////////// |
---|
| 2725 | error_t fatfs_move_page( xptr_t page_xp, |
---|
| 2726 | cmd_type_t cmd_type ) |
---|
[602] | 2727 | { |
---|
| 2728 | error_t error; |
---|
| 2729 | vfs_inode_t * inode_ptr; |
---|
| 2730 | mapper_t * mapper_ptr; |
---|
| 2731 | uint32_t page_id; // page index in mapper |
---|
| 2732 | |
---|
| 2733 | #if DEBUG_FATFS_MOVE_PAGE |
---|
| 2734 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2735 | thread_t * this = CURRENT_THREAD; |
---|
| 2736 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
| 2737 | #endif |
---|
| 2738 | |
---|
| 2739 | // get page cluster an local pointer |
---|
| 2740 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
| 2741 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
| 2742 | |
---|
| 2743 | // get mapper pointer and page index from page descriptor |
---|
| 2744 | mapper_ptr = hal_remote_lpt( XPTR( page_cxy , &page_ptr->mapper ) ); |
---|
| 2745 | page_id = hal_remote_l32( XPTR( page_cxy , &page_ptr->index ) ); |
---|
| 2746 | |
---|
| 2747 | // get pointer on local FATFS context |
---|
[611] | 2748 | fatfs_ctx_t * fatfs_ctx = fs_context[FS_TYPE_FATFS].extend; |
---|
[602] | 2749 | |
---|
| 2750 | // get page base address |
---|
[626] | 2751 | xptr_t buffer_xp = ppm_page2base( page_xp ); |
---|
| 2752 | uint8_t * buffer_ptr = (uint8_t *)GET_PTR( buffer_xp ); |
---|
[602] | 2753 | |
---|
| 2754 | // get inode pointer from mapper |
---|
| 2755 | inode_ptr = hal_remote_lpt( XPTR( page_cxy , &mapper_ptr->inode ) ); |
---|
| 2756 | |
---|
[626] | 2757 | #if DEBUG_FATFS_MOVE_PAGE |
---|
| 2758 | if( DEBUG_FATFS_MOVE_PAGE < cycle ) |
---|
| 2759 | printk("\n[%s] thread[%x,%x] enters : %s / cxy %x / mapper %x / inode %x / page %x\n", |
---|
| 2760 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 2761 | dev_ioc_cmd_str( cmd_type ), page_cxy, mapper_ptr, inode_ptr, buffer_ptr ); |
---|
| 2762 | #endif |
---|
| 2763 | |
---|
| 2764 | ////////////////////////////// FAT mapper |
---|
[602] | 2765 | if( inode_ptr == NULL ) |
---|
| 2766 | { |
---|
| 2767 | // get lba from FATFS context and page_id |
---|
[625] | 2768 | uint32_t lba = fatfs_ctx->fat_begin_lba + (page_id << 3); |
---|
[602] | 2769 | |
---|
| 2770 | // access device |
---|
[626] | 2771 | if (cmd_type == IOC_SYNC_READ ) error = dev_ioc_sync_read ( buffer_xp , lba , 8 ); |
---|
| 2772 | else if(cmd_type == IOC_SYNC_WRITE) error = dev_ioc_sync_write( buffer_xp , lba , 8 ); |
---|
| 2773 | else if(cmd_type == IOC_READ ) error = dev_ioc_read ( buffer_ptr , lba , 8 ); |
---|
| 2774 | else if(cmd_type == IOC_WRITE ) error = dev_ioc_write ( buffer_ptr , lba , 8 ); |
---|
| 2775 | else error = -1; |
---|
[602] | 2776 | |
---|
[626] | 2777 | if( error ) |
---|
| 2778 | { |
---|
| 2779 | printk("\n[ERROR] in %s : cannot access device\n", __FUNCTION__ ); |
---|
| 2780 | return -1; |
---|
| 2781 | } |
---|
[602] | 2782 | |
---|
| 2783 | #if DEBUG_FATFS_MOVE_PAGE |
---|
| 2784 | if( DEBUG_FATFS_MOVE_PAGE < cycle ) |
---|
| 2785 | { |
---|
[614] | 2786 | if ( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) ) |
---|
[626] | 2787 | printk("\n[%s] thread[%x,%x] load FAT mapper page %d from IOC / cycle %d\n", |
---|
| 2788 | __FUNCTION__, this->process->pid, this->trdid, page_id, cycle ); |
---|
[602] | 2789 | else |
---|
[626] | 2790 | printk("\n[%s] thread[%x,%x] sync FAT mapper page %d to IOC / cycle %d\n", |
---|
[602] | 2791 | __FUNCTION__, this->process->pid, this->trdid, page_id, cycle ); |
---|
| 2792 | } |
---|
| 2793 | #endif |
---|
| 2794 | |
---|
| 2795 | } |
---|
[626] | 2796 | ///////////////////////// inode mapper |
---|
[602] | 2797 | else |
---|
| 2798 | { |
---|
| 2799 | |
---|
| 2800 | #if DEBUG_FATFS_MOVE_PAGE |
---|
| 2801 | vfs_inode_get_name( XPTR( page_cxy , inode_ptr ) , name ); |
---|
| 2802 | #endif |
---|
| 2803 | |
---|
| 2804 | uint32_t searched_cluster; |
---|
| 2805 | uint32_t first_cluster; |
---|
| 2806 | |
---|
| 2807 | // get first_cluster from inode extension |
---|
| 2808 | void * extend = hal_remote_lpt( XPTR( page_cxy , &inode_ptr->extend ) ); |
---|
| 2809 | first_cluster = (uint32_t)(intptr_t)extend; |
---|
| 2810 | |
---|
| 2811 | // compute searched_cluster |
---|
| 2812 | if( page_id == 0 ) // no need to access FAT mapper |
---|
| 2813 | { |
---|
| 2814 | // searched cluster is first cluster |
---|
| 2815 | searched_cluster = first_cluster; |
---|
| 2816 | } |
---|
| 2817 | else // FAT mapper access required |
---|
| 2818 | { |
---|
| 2819 | // access FAT mapper to get searched cluster |
---|
| 2820 | error = fatfs_get_cluster( first_cluster, |
---|
| 2821 | page_id, |
---|
| 2822 | &searched_cluster ); |
---|
[625] | 2823 | if( error ) |
---|
| 2824 | { |
---|
| 2825 | printk("\n[ERROR] in %s : cannot access FAT mapper\n", __FUNCTION__ ); |
---|
| 2826 | return -1; |
---|
| 2827 | } |
---|
[602] | 2828 | } |
---|
| 2829 | |
---|
[626] | 2830 | // get lba for searched_cluster |
---|
[602] | 2831 | uint32_t lba = fatfs_lba_from_cluster( fatfs_ctx , searched_cluster ); |
---|
| 2832 | |
---|
[626] | 2833 | // access device |
---|
| 2834 | if (cmd_type == IOC_SYNC_READ ) error = dev_ioc_sync_read ( buffer_xp , lba , 8 ); |
---|
| 2835 | else if(cmd_type == IOC_SYNC_WRITE) error = dev_ioc_sync_write( buffer_xp , lba , 8 ); |
---|
| 2836 | else if(cmd_type == IOC_READ ) error = dev_ioc_read ( buffer_ptr , lba , 8 ); |
---|
| 2837 | else if(cmd_type == IOC_WRITE ) error = dev_ioc_write ( buffer_ptr , lba , 8 ); |
---|
| 2838 | else error = -1; |
---|
| 2839 | |
---|
| 2840 | if( error ) |
---|
| 2841 | { |
---|
| 2842 | printk("\n[ERROR] in %s : cannot access device\n", __FUNCTION__ ); |
---|
| 2843 | return -1; |
---|
| 2844 | } |
---|
| 2845 | |
---|
[625] | 2846 | #if DEBUG_FATFS_MOVE_PAGE |
---|
| 2847 | if( DEBUG_FATFS_MOVE_PAGE < cycle ) |
---|
| 2848 | { |
---|
| 2849 | if ( (cmd_type == IOC_READ) || (cmd_type == IOC_SYNC_READ) ) |
---|
| 2850 | printk("\n[%s] thread[%x,%x] load page %d of <%s> / cluster_id %x / cycle %d\n", |
---|
| 2851 | __FUNCTION__, this->process->pid, this->trdid, page_id, name, searched_cluster, cycle ); |
---|
| 2852 | else |
---|
| 2853 | printk("\n[%s] thread[%x,%x] sync page %d of <%s> / cluster_id %x / cycle %d\n", |
---|
| 2854 | __FUNCTION__, this->process->pid, this->trdid, page_id, name, searched_cluster, cycle ); |
---|
| 2855 | } |
---|
| 2856 | #endif |
---|
| 2857 | |
---|
[602] | 2858 | } |
---|
| 2859 | |
---|
| 2860 | return 0; |
---|
| 2861 | |
---|
| 2862 | } // end fatfs_move_page() |
---|
| 2863 | |
---|
| 2864 | |
---|