From 201a5bf04105b6701a9830cb6841b69b5d2a3e07 Mon Sep 17 00:00:00 2001 From: pathoswithin Date: Sun, 22 Jan 2017 16:19:39 +0000 Subject: [PATCH] fs: prevalidation for root folder, code despaghettizing git-svn-id: svn://kolibrios.org@6845 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/blkdev/disk.inc | 35 +++-- kernel/trunk/fs/ext.inc | 8 +- kernel/trunk/fs/fat.inc | 281 ++++++++++++++--------------------- kernel/trunk/fs/iso9660.inc | 106 +++++-------- kernel/trunk/fs/ntfs.inc | 110 +++----------- 5 files changed, 193 insertions(+), 347 deletions(-) diff --git a/kernel/trunk/blkdev/disk.inc b/kernel/trunk/blkdev/disk.inc index 343045c985..0101544ecb 100644 --- a/kernel/trunk/blkdev/disk.inc +++ b/kernel/trunk/blkdev/disk.inc @@ -1393,10 +1393,16 @@ dyndisk_handler: ; 11c. Let the procedure from fs_lfn.inc do the job. jmp file_system_lfn.maindir_noesi +.root: + pop ecx edx + xor eax, eax + cmp byte [ebx], 9 + jz .cleanup_ecx .access_denied: - mov dword [esp+32], ERROR_ACCESS_DENIED + movi eax, ERROR_ACCESS_DENIED +.cleanup_ecx: + mov [esp+32], eax mov esi, ecx ; disk*dereference assume that esi points to DISK -.cleanup_esi: test edx, edx ; if there are no media, we didn't reference it jz @f call disk_media_dereference @@ -1406,15 +1412,13 @@ dyndisk_handler: ret .dyndisk_cleanup: - pop esi - pop edx - mov dword [esp+32], ERROR_FILE_NOT_FOUND - jmp .cleanup_esi + pop ecx edx + movi eax, ERROR_FILE_NOT_FOUND + jmp .cleanup_ecx .haspartition: ; 12. The fs operation has specified some partition. - push edx - push ecx + push edx ecx xor eax, eax lodsb sub eax, '0' @@ -1428,6 +1432,13 @@ dyndisk_handler: test eax, eax jnz .dyndisk_cleanup dec esi +@@: + cmp byte [esi], 0 + jnz @f + cmp byte [ebx], 1 + jz @f + cmp byte [ebx], 5 + jnz .root @@: dec ecx ; convert to zero-based partition index pop edx ; edx = pointer to DISK, dword [esp] = NULL or edx @@ -1479,9 +1490,9 @@ dyndisk_handler: call dword [edi+8+ecx*4] pop ebp pop edx - mov dword [esp+32], eax mov dword [esp+20], ebx .cleanup: + mov dword [esp+32], eax mov esi, edx call disk_media_dereference @@: @@ -1490,14 +1501,14 @@ dyndisk_handler: ret .unsupported: - mov dword [esp+32], ERROR_UNKNOWN_FS + movi eax, ERROR_UNKNOWN_FS cmp edi, default_fs_functions jz .cleanup - mov dword [esp+32], ERROR_UNSUPPORTED_FS + movi eax, ERROR_UNSUPPORTED_FS jmp .cleanup .notfound2: - mov dword [esp+32], ERROR_FILE_NOT_FOUND + movi eax, ERROR_FILE_NOT_FOUND jmp .cleanup .nomedia: diff --git a/kernel/trunk/fs/ext.inc b/kernel/trunk/fs/ext.inc index fc3baaa158..6b49d21ff5 100644 --- a/kernel/trunk/fs/ext.inc +++ b/kernel/trunk/fs/ext.inc @@ -1753,9 +1753,6 @@ writeSuperblock: ret extfsWritingInit: - movi eax, ERROR_ACCESS_DENIED - cmp byte [esi], 0 - jz @f movi eax, ERROR_UNSUPPORTED_FS test [ebp+EXTFS.mountType], READ_ONLY jnz @f @@ -1987,10 +1984,7 @@ ext_ReadFolder: ;---------------------------------------------------------------- ext_ReadFile: call ext_lock - pushd 0 ERROR_ACCESS_DENIED - cmp byte [esi], 0 - jz .ret ; root - mov [esp], ebx + pushd 0 ebx call findInode pop ebx push eax diff --git a/kernel/trunk/fs/fat.inc b/kernel/trunk/fs/fat.inc index 674aeb1b34..bbc1e68245 100644 --- a/kernel/trunk/fs/fat.inc +++ b/kernel/trunk/fs/fat.inc @@ -1525,34 +1525,12 @@ hd_find_lfn: fat_Read: call fat_lock push edi - cmp byte [esi], 0 - jnz @f -.noaccess: - pop edi - call fat_unlock - or ebx, -1 - mov eax, ERROR_ACCESS_DENIED - ret -@@: call hd_find_lfn - jnc .found - pop edi - push eax - call fat_unlock - pop eax - or ebx, -1 - ret -.found: + jc .notFound test byte [edi+11], 0x10 ; do not allow read directories jnz .noaccess cmp dword [ebx+8], 0 - jz @f - xor ebx, ebx - call fat_unlock - mov eax, ERROR_END_OF_FILE - pop edi - ret -@@: + jnz .endOfFile mov edx, [ebx+4] ; file offset mov ecx, [ebx+12] ; size mov ebx, [ebx+16] ; buffer @@ -1583,6 +1561,23 @@ fat_Read: call get_FAT jc .noaccess2 jmp @b + +.notFound: + push eax + jmp .ret + +.noaccess: + push ERROR_ACCESS_DENIED + jmp .ret + +.endOfFile: + push ERROR_END_OF_FILE +.ret: + call fat_unlock + pop eax edi + xor ebx, ebx + ret + @@: mov esi, eax dec eax @@ -1674,6 +1669,7 @@ fat_Read: xor edx, edx jecxz .done jmp .alignedCluster + .readEnd: add ecx, edi mov edi, ecx @@ -1688,6 +1684,7 @@ fat_Read: push eax mov ecx, eax jmp .readFragment + .noaccess3: pop eax .noaccess2: @@ -1697,12 +1694,15 @@ fat_Read: pop eax edx edi sub ebx, edx ret + .fileEnd: mov byte [esp], ERROR_END_OF_FILE jmp .done + .noaccess4: mov byte [esp], ERROR_DEVICE jmp @f + .fileEnd2: mov byte [esp], ERROR_END_OF_FILE @@: @@ -2101,12 +2101,6 @@ fat_get_sector: pop ecx ret -fshrad: - call fat_unlock - mov eax, ERROR_ACCESS_DENIED - xor ebx, ebx - ret - ;---------------------------------------------------------------- fat_CreateFolder: push 1 @@ -2117,8 +2111,6 @@ fat_CreateFile: @@: call fat_lock pop eax - cmp byte [esi], 0 - jz fshrad mov ecx, [ebx+12] mov edx, [ebx+16] pushad @@ -2661,33 +2653,12 @@ fat_read_symbols: ;---------------------------------------------------------------- fat_Write: - cmp byte [esi], 0 - jz .access_denied call fat_lock push edi call hd_find_lfn - jnc .found - pop edi - push eax - call fat_unlock -.ret0: - pop eax - xor ebx, ebx - ret - -.access_denied: - push ERROR_ACCESS_DENIED - jmp .ret0 - -.found: ; FAT does not support files larger than 4GB + jc .error cmp dword [ebx+8], 0 - jz @f -.eof: - pop edi - push ERROR_END_OF_FILE - call fat_unlock - jmp .ret0 -@@: + jnz .eof ; FAT does not support files larger than 4GB mov ecx, [ebx+12] mov edx, [ebx+16] mov ebx, [ebx+4] @@ -2715,16 +2686,7 @@ fat_Write: ; hd_extend_file can return three error codes: FAT table error, device error or disk full. ; First two cases are fatal errors, in third case we may write some data cmp al, ERROR_DISK_FULL - jz .disk_full - call fat_unlock - pop eax - pop eax - pop ecx - pop edx - pop edi - xor ebx, ebx - ret -.disk_full: + jnz @f ; correct number of bytes to write mov ecx, [edi+28] cmp ecx, ebx @@ -2738,14 +2700,30 @@ fat_Write: test eax, eax jz @f mov byte [esp+4], ERROR_DEVICE +@@: + pop eax eax ecx edx +.error: + push eax @@: call fat_unlock - pop eax - pop eax - pop ecx - pop edx - pop edi + pop eax edi + xor ebx, ebx ret + +.eof: + push ERROR_END_OF_FILE + jmp @b + +.device_err2: + pop ecx +.device_err: + mov byte [esp+8], ERROR_DEVICE + jmp .ret + +.fat_err: + mov byte [esp+8], ERROR_FS_FAIL + jmp .ret + .length_ok: mov esi, [edi+28] mov eax, [edi+20-2] @@ -2759,15 +2737,7 @@ fat_Write: call fs_write32_sys pop ebx test eax, eax - jz @f -.device_err: - mov byte [esp+8], ERROR_DEVICE - jmp .ret -.fat_err: - mov byte [esp+8], ERROR_FS_FAIL - jmp .ret -@@: - + jnz .device_err ; now ebx=start pos, ecx=end pos, both lie inside file sub ecx, ebx jz .ret @@ -2811,11 +2781,7 @@ fat_Write: call fs_read32_app test eax, eax pop ebx eax - jz @f -.device_err2: - pop ecx - jmp .device_err -@@: + jnz .device_err2 .noread: ; zero uninitialized data if file was extended (because hd_extend_file does not this) push eax ecx edi @@ -2996,31 +2962,14 @@ fat_update_datetime: fat_SetFileEnd: call fat_lock push edi - cmp byte [esi], 0 - jnz @f -.access_denied: - push ERROR_ACCESS_DENIED -.ret: - call fat_unlock - pop eax - pop edi - ret -@@: call hd_find_lfn - jnc @f -.reteax: - push eax - jmp .ret -@@: + jc .reteax ; must not be directory test byte [edi+11], 10h jnz .access_denied ; file size must not exceed 4 Gb cmp dword [ebx+8], 0 - jz @f - push ERROR_END_OF_FILE - jmp .ret -@@: + jnz .endOfFile push eax ; save directory sector ; set file modification date/time to current call fat_update_datetime @@ -3032,12 +2981,22 @@ fat_SetFileEnd: lea ebx, [ebp+FAT.buffer] call fs_write32_sys test eax, eax - jz @f - push ERROR_DEVICE - jmp .ret -@@: + jnz .errorDevice push 0 jmp .ret + +.access_denied: + push ERROR_ACCESS_DENIED + jmp .ret + +.endOfFile: + push ERROR_END_OF_FILE + jmp .ret + +.errorDevice: + push ERROR_DEVICE + jmp .ret + .expand: push ebx ebp ecx push dword [edi+28] ; save old size @@ -3046,14 +3005,8 @@ fat_SetFileEnd: push eax ; return code jnc .expand_ok cmp al, ERROR_DISK_FULL - jz .disk_full -.pop_ret: - call update_disk - pop eax ecx ecx ebp ebx ecx - jmp .reteax -.expand_ok: -.disk_full: -; save directory + jnz .pop_ret +.expand_ok: ; save directory mov eax, [edi+28] xchg eax, [esp+20] lea ebx, [ebp+FAT.buffer] @@ -3062,11 +3015,7 @@ fat_SetFileEnd: mov eax, [edi+20-2] mov ax, [edi+26] mov edi, eax - jz @f -.pop_ret11: - mov byte [esp], ERROR_DEVICE - jmp .pop_ret -@@: + jnz .pop_ret11 test edi, edi jz .pop_ret ; now zero new data @@ -3123,7 +3072,29 @@ fat_SetFileEnd: mov edi, eax jnc .zero_loop pop eax - jmp .pop_ret11 +.pop_ret11: + mov byte [esp], ERROR_DEVICE +.pop_ret: + call update_disk + pop eax ecx ecx ebp ebx ecx +.reteax: + push eax +.ret: + call fat_unlock + pop eax edi + ret + +.error_fat: + pop eax + mov byte [esp], ERROR_FS_FAIL + jmp .pop_ret + +.error_fat2: + pop eax ecx eax + call update_disk + push ERROR_FS_FAIL + jmp .ret + .truncate: mov [edi+28], eax push ecx @@ -3132,8 +3103,7 @@ fat_SetFileEnd: push eax test eax, eax jz .zero_size -; find new last cluster -@@: +@@: ; find new last cluster cmp ecx, 2 jb .error_fat2 cmp ecx, [ebp+FAT.fatRESERVED] @@ -3147,11 +3117,11 @@ fat_SetFileEnd: mov ecx, eax jnc @b .device_err3: - pop eax ecx eax edi + pop eax ecx eax call update_disk - call fat_unlock - movi eax, ERROR_DEVICE - ret + push ERROR_DEVICE + jmp .ret + @@: ; we will zero data at the end of last sector - remember it push ecx @@ -3166,6 +3136,7 @@ fat_SetFileEnd: .device_err4: pop ecx jmp .device_err3 + .zero_size: and word [edi+20], 0 and word [edi+26], 0 @@ -3215,16 +3186,6 @@ fat_SetFileEnd: call fat_unlock xor eax, eax ret -.error_fat: - pop eax - mov byte [esp], ERROR_FS_FAIL - jmp .pop_ret -.error_fat2: - pop eax ecx eax edi - call update_disk - call fat_unlock - movi eax, ERROR_FS_FAIL - ret ;---------------------------------------------------------------- fat_GetFileInfo: @@ -3256,11 +3217,6 @@ fat_GetFileInfo: ;---------------------------------------------------------------- fat_SetFileInfo: - cmp byte [esi], 0 - jnz @f - mov eax, 2 - ret -@@: push edi call fat_lock call hd_find_lfn @@ -3286,26 +3242,11 @@ fat_SetFileInfo: ;---------------------------------------------------------------- fat_Delete: call fat_lock - cmp byte [esi], 0 - jnz @f -; cannot delete root! -.access_denied: - push ERROR_ACCESS_DENIED -.pop_ret: - call fat_unlock - pop eax - xor ebx, ebx - ret -@@: and [ebp+FAT.longname_sec1], 0 and [ebp+FAT.longname_sec2], 0 push edi call hd_find_lfn - jnc .found - pop edi - push ERROR_FILE_NOT_FOUND - jmp .pop_ret -.found: + jc .notFound cmp dword [edi], '. ' jz .access_denied2 cmp dword [edi], '.. ' @@ -3359,23 +3300,27 @@ fat_Delete: .err1: popad .err2: - pop edi + push ERROR_DEVICE +.ret: call fat_unlock - movi eax, ERROR_DEVICE + pop eax edi ret + +.notFound: + push ERROR_FILE_NOT_FOUND + jmp .ret + .error_fat: popad - pop edi - call fat_unlock - movi eax, ERROR_FS_FAIL - ret + push ERROR_FS_FAIL + jmp .ret + .notempty: popad .access_denied2: - pop edi - call fat_unlock - movi eax, ERROR_ACCESS_DENIED - ret + push ERROR_ACCESS_DENIED + jmp .ret + .empty: popad push eax ebx diff --git a/kernel/trunk/fs/iso9660.inc b/kernel/trunk/fs/iso9660.inc index 5576594491..e4eb10c596 100644 --- a/kernel/trunk/fs/iso9660.inc +++ b/kernel/trunk/fs/iso9660.inc @@ -160,112 +160,88 @@ free_cd_channel: ;----------------------------------------------------------------------------- fs_CdRead: - push edi - cmp byte [esi], 0 - jnz @f -;-------------------------------------- -.noaccess: - pop edi -;-------------------------------------- -.noaccess_2: - or ebx, -1 - mov eax, ERROR_ACCESS_DENIED - ret -;-------------------------------------- -.noaccess_3: - pop eax edx ecx edi - jmp .noaccess_2 -;-------------------------------------- -@@: call cd_find_lfn - jnc .found - - pop edi - cmp [DevErrorCode], 0 - jne .noaccess_2 - - or ebx, -1 - mov eax, ERROR_FILE_NOT_FOUND - ret -;-------------------------------------- -.found: + jc .notFound mov edi, [cd_current_pointer_of_input] - test byte [edi+25], 10b; do not allow read directories + test byte [edi+25], 10b ; do not allow read directories jnz .noaccess - test ebx, ebx jz .l1 - cmp dword [ebx+4], 0 jz @f - xor ebx, ebx -;-------------------------------------- -.reteof: - mov eax, 6; end of file - pop edi + movi eax, ERROR_END_OF_FILE ret -;-------------------------------------- + +.notFound: + cmp [DevErrorCode], 0 + jne .noaccess + xor ebx, ebx + movi eax, ERROR_FILE_NOT_FOUND + ret + +.noaccess_3: + pop eax edx ecx +.noaccess: + xor ebx, ebx + movi eax, ERROR_ACCESS_DENIED + ret + @@: mov ebx, [ebx] -;-------------------------------------- .l1: - push ecx edx - push 0 - mov eax, [edi+10] ; real size of the file section + push ecx edx 0 + mov eax, [edi+10] ; real size of the file section sub eax, ebx jb .eof - cmp eax, ecx jae @f - mov ecx, eax - mov byte [esp], 6 -;-------------------------------------- + pop eax + push ERROR_END_OF_FILE @@: mov eax, [edi+2] mov [CDSectorAddress], eax -;-------------------------------------- ; now eax=cluster, ebx=position, ecx=count, edx=buffer for data .new_sector: test ecx, ecx jz .done - sub ebx, 2048 jae .next - add ebx, 2048 jnz .incomplete_sector - cmp ecx, 2048 jb .incomplete_sector ; we may read and memmove complete sector mov [CDDataBuf_pointer], edx - call ReadCDWRetr ; read sector of file + call ReadCDWRetr cmp [DevErrorCode], 0 jne .noaccess_3 - add edx, 2048 sub ecx, 2048 -;-------------------------------------- .next: inc dword [CDSectorAddress] jmp .new_sector -;-------------------------------------- -.incomplete_sector: -; we must read and memmove incomplete sector + +.eof: + pop eax + push ERROR_END_OF_FILE +.done: + mov ebx, edx + pop eax edx ecx + sub ebx, edx + ret + +.incomplete_sector: ; we must read and memmove incomplete sector mov [CDDataBuf_pointer], CDDataBuf - call ReadCDWRetr ; read sector of file + call ReadCDWRetr cmp [DevErrorCode], 0 jne .noaccess_3 - push ecx add ecx, ebx cmp ecx, 2048 jbe @f - mov ecx, 2048 -;-------------------------------------- @@: sub ecx, ebx push edi esi ecx @@ -279,18 +255,6 @@ fs_CdRead: pop ecx xor ebx, ebx jmp .next -;-------------------------------------- -.done: - mov ebx, edx - pop eax edx ecx edi - sub ebx, edx - ret -;-------------------------------------- -.eof: - mov ebx, edx - pop eax edx ecx - sub ebx, edx - jmp .reteof ;----------------------------------------------------------------------------- fs_CdReadFolder: diff --git a/kernel/trunk/fs/ntfs.inc b/kernel/trunk/fs/ntfs.inc index 0add6b4792..1c65372c6a 100644 --- a/kernel/trunk/fs/ntfs.inc +++ b/kernel/trunk/fs/ntfs.inc @@ -1320,47 +1320,18 @@ ntfs_find_lfn: ;---------------------------------------------------------------- ntfs_ReadFile: - cmp byte [esi], 0 - jnz @f - or ebx, -1 - movi eax, ERROR_ACCESS_DENIED - ret - -@@: call ntfs_lock call ntfs_find_lfn - jnc .found - call ntfs_unlock - or ebx, -1 - movi eax, ERROR_FILE_NOT_FOUND - ret - -.found: + jc ntfsNotFound mov [ebp+NTFS.cur_attr], 0x80 ; $DATA and [ebp+NTFS.cur_offs], 0 and [ebp+NTFS.cur_size], 0 call ntfs_read_attr - jnc @f - call ntfs_unlock - or ebx, -1 - movi eax, ERROR_ACCESS_DENIED - ret - -@@: - pushad - and dword [esp+10h], 0 + jc ntfsDenied xor eax, eax + push eax cmp dword [ebx+8], 0x200 - jb @f -.eof0: - popad - xor ebx, ebx -.eof: - call ntfs_unlock - movi eax, ERROR_END_OF_FILE - ret - -@@: + jnc .eof mov ecx, [ebx+12] mov edx, [ebx+16] mov eax, [ebx+4] @@ -1379,33 +1350,22 @@ ntfs_ReadFile: and eax, 0x1FF lea esi, [ebp+NTFS.bitmap_buf+eax] sub eax, [ebp+NTFS.cur_read] - jae .eof0 + jae .eof neg eax push ecx cmp ecx, eax jb @f mov ecx, eax @@: - mov [esp+10h+4], ecx + mov [esp+4], ecx mov edi, edx rep movsb mov edx, edi pop ecx - sub ecx, [esp+10h] - jnz @f -.retok: - popad - call ntfs_unlock - xor eax, eax - ret - -@@: + sub ecx, [esp] + jz .retok cmp [ebp+NTFS.cur_read], 0x200 - jz .alignedstart -.eof_ebx: - popad - jmp .eof - + jnz .eof .alignedstart: mov eax, [ebx+4] push edx @@ -1424,11 +1384,11 @@ ntfs_ReadFile: call ntfs_read_attr.continue pop [ebp+NTFS.cur_offs] mov eax, [ebp+NTFS.cur_read] - add [esp+10h], eax + add [esp], eax mov eax, ecx and eax, not 0x1FF cmp [ebp+NTFS.cur_read], eax - jnz .eof_ebx + jnz .eof and ecx, 0x1FF jz .retok add edx, [ebp+NTFS.cur_read] @@ -1444,19 +1404,22 @@ ntfs_ReadFile: push ecx mov edi, edx lea esi, [ebp+NTFS.bitmap_buf] - add [esp+10h+4], ecx + add [esp+4], ecx rep movsb pop ecx - xor eax, eax cmp ecx, [ebp+NTFS.cur_read] - jz @f - mov al, ERROR_END_OF_FILE -@@: - mov [esp+1Ch], eax + jnz .eof +.retok: + pushd 0 +.ret: call ntfs_unlock - popad + pop eax ebx ret +.eof: + push ERROR_END_OF_FILE + jmp .ret + ;---------------------------------------------------------------- ntfs_ReadFolder: call ntfs_lock @@ -1854,13 +1817,6 @@ ntfs_CreateFolder: ntfs_CreateFile: mov [ebp+NTFS.bFolder], 0 -@@: - cmp byte [esi], 0 - jnz @f - xor ebx, ebx - movi eax, ERROR_ACCESS_DENIED - ret - @@: ; 1. Search file call ntfs_lock call ntfs_find_lfn @@ -3483,12 +3439,6 @@ bitmapBuffering: ;---------------------------------------------------------------- ntfs_WriteFile: - cmp byte [esi], 0 - jnz @f - xor ebx, ebx - movi eax, ERROR_ACCESS_DENIED - ret -@@: call ntfs_lock call ntfs_find_lfn jc ntfsNotFound @@ -3662,13 +3612,6 @@ ntfs_WriteFile: ;---------------------------------------------------------------- ntfs_Delete: - cmp byte [esi], 0 - jnz @f - xor ebx, ebx - movi eax, ERROR_ACCESS_DENIED - ret - -@@: call ntfs_lock call ntfs_find_lfn jc ntfsNotFound @@ -3981,12 +3924,6 @@ ntfs_Delete: ;---------------------------------------------------------------- ntfs_SetFileEnd: - cmp byte [esi], 0 - jnz @f - xor ebx, ebx - movi eax, ERROR_ACCESS_DENIED - ret -@@: call ntfs_lock call ntfs_find_lfn jc ntfsNotFound @@ -4116,11 +4053,6 @@ ntfsCalculateTime: ;---------------------------------------------------------------- ntfs_SetFileInfo: - cmp byte [esi], 0 - jnz @f - movi eax, ERROR_UNSUPPORTED_FS - ret -@@: call ntfs_lock call ntfs_find_lfn jnc @f