📄 misc.c
字号:
/* * linux/fs/fat/misc.c * * Written 1992,1993 by Werner Almesberger */#include <linux/fs.h>#include <linux/msdos_fs.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/stat.h>#include "msbuffer.h"#if 0# define PRINTK(x) printk x#else# define PRINTK(x)#endif#define Printk(x) printk x/* Well-known binary file extensions - of course there are many more */static char ascii_extensions[] = "TXT" "ME " "HTM" "1ST" "LOG" " " /* text files */ "C " "H " "CPP" "LIS" "PAS" "FOR" /* programming languages */ "F " "MAK" "INC" "BAS" /* programming languages */ "BAT" "SH " /* program code :) */ "INI" /* config files */ "PBM" "PGM" "DXF" /* graphics */ "TEX"; /* TeX *//* * fat_fs_panic reports a severe file system problem and sets the file system * read-only. The file system can be made writable again by remounting it. */void fat_fs_panic(struct super_block *s,const char *msg){ int not_ro; not_ro = !(s->s_flags & MS_RDONLY); if (not_ro) s->s_flags |= MS_RDONLY; printk("Filesystem panic (dev %s).\n %s\n", kdevname(s->s_dev), msg); if (not_ro) printk(" File system has been set read-only\n");}/* * fat_is_binary selects optional text conversion based on the conversion mode * and the extension part of the file name. */int fat_is_binary(char conversion,char *extension){ char *walk; switch (conversion) { case 'b': return 1; case 't': return 0; case 'a': for (walk = ascii_extensions; *walk; walk += 3) if (!strncmp(extension,walk,3)) return 0; return 1; /* default binary conversion */ default: printk("Invalid conversion mode - defaulting to " "binary.\n"); return 1; }}/* File creation lock. This is system-wide to avoid deadlocks in rename. *//* (rename might deadlock before detecting cross-FS moves.) */static DECLARE_MUTEX(creation_lock);void fat_lock_creation(void){ down(&creation_lock);}void fat_unlock_creation(void){ up(&creation_lock);}void lock_fat(struct super_block *sb){ down(&(MSDOS_SB(sb)->fat_lock));}void unlock_fat(struct super_block *sb){ up(&(MSDOS_SB(sb)->fat_lock));}/* Flushes the number of free clusters on FAT32 *//* XXX: Need to write one per FSINFO block. Currently only writes 1 */void fat_clusters_flush(struct super_block *sb){ int offset; struct buffer_head *bh; struct fat_boot_fsinfo *fsinfo; /* The fat32 boot fs info is at offset 0x3e0 by observation */ offset = MSDOS_SB(sb)->fsinfo_offset; bh = fat_bread(sb, (offset >> SECTOR_BITS)); if (bh == NULL) { printk("FAT bread failed in fat_clusters_flush\n"); return; } fsinfo = (struct fat_boot_fsinfo *) &bh->b_data[offset & (SECTOR_SIZE-1)]; /* Sanity check */ if (CF_LE_L(fsinfo->signature) != 0x61417272) { printk("fat_clusters_flush: Did not find valid FSINFO signature. Found 0x%x. offset=0x%x\n", CF_LE_L(fsinfo->signature), offset); fat_brelse(sb, bh); return; } fsinfo->free_clusters = CF_LE_L(MSDOS_SB(sb)->free_clusters); fat_mark_buffer_dirty(sb, bh); fat_brelse(sb, bh);}/* * fat_add_cluster tries to allocate a new cluster and adds it to the file * represented by inode. The cluster is zero-initialized. *//* not a directory */int fat_add_cluster(struct inode *inode){ struct super_block *sb = inode->i_sb; int count,nr,limit,last,curr,file_cluster; int res = -ENOSPC; int cluster_size = MSDOS_SB(sb)->cluster_size; if (!MSDOS_SB(sb)->free_clusters) return res; lock_fat(sb); limit = MSDOS_SB(sb)->clusters; nr = limit; /* to keep GCC happy */ for (count = 0; count < limit; count++) { nr = ((count+MSDOS_SB(sb)->prev_free) % limit)+2; if (fat_access(sb,nr,-1) == 0) break; } MSDOS_SB(sb)->prev_free = (count+MSDOS_SB(sb)->prev_free+1) % limit; if (count >= limit) { MSDOS_SB(sb)->free_clusters = 0; unlock_fat(sb); return res; } fat_access(sb,nr,EOF_FAT(sb)); if (MSDOS_SB(sb)->free_clusters != -1) MSDOS_SB(sb)->free_clusters--; if (MSDOS_SB(sb)->fat_bits == 32) fat_clusters_flush(sb); unlock_fat(sb); last = 0; /* We must locate the last cluster of the file to add this new one (nr) to the end of the link list (the FAT). Here file_cluster will be the number of the last cluster of the file (before we add nr). last is the corresponding cluster number on the disk. We will use last to plug the nr cluster. We will use file_cluster to update the cache. */ file_cluster = 0; if ((curr = MSDOS_I(inode)->i_start) != 0) { fat_cache_lookup(inode,INT_MAX,&last,&curr); file_cluster = last; while (curr && curr != -1){ file_cluster++; if (!(curr = fat_access(sb, last = curr,-1))) { fat_fs_panic(sb,"File without EOF"); return res; } } } if (last) fat_access(sb,last,nr); else { MSDOS_I(inode)->i_start = nr; MSDOS_I(inode)->i_logstart = nr; mark_inode_dirty(inode); } fat_cache_add(inode,file_cluster,nr); inode->i_blocks += cluster_size; return 0;}struct buffer_head *fat_extend_dir(struct inode *inode){ struct super_block *sb = inode->i_sb; int count,nr,limit,last,curr,sector,last_sector,file_cluster; struct buffer_head *bh, *res=NULL; int cluster_size = MSDOS_SB(sb)->cluster_size; if (MSDOS_SB(sb)->fat_bits != 32) { if (inode->i_ino == MSDOS_ROOT_INO) return res; } if (!MSDOS_SB(sb)->free_clusters) return res; lock_fat(sb); limit = MSDOS_SB(sb)->clusters; nr = limit; /* to keep GCC happy */ for (count = 0; count < limit; count++) { nr = ((count+MSDOS_SB(sb)->prev_free) % limit)+2; if (fat_access(sb,nr,-1) == 0) break; } PRINTK (("cnt = %d --",count));#ifdef DEBUGprintk("free cluster: %d\n",nr);#endif MSDOS_SB(sb)->prev_free = (count+MSDOS_SB(sb)->prev_free+1) % limit; if (count >= limit) { MSDOS_SB(sb)->free_clusters = 0; unlock_fat(sb); return res; } fat_access(sb,nr,EOF_FAT(sb)); if (MSDOS_SB(sb)->free_clusters != -1) MSDOS_SB(sb)->free_clusters--; if (MSDOS_SB(sb)->fat_bits == 32) fat_clusters_flush(sb); unlock_fat(sb);#ifdef DEBUGprintk("set to %x\n",fat_access(sb,nr,-1));#endif last = 0; /* We must locate the last cluster of the file to add this new one (nr) to the end of the link list (the FAT). Here file_cluster will be the number of the last cluster of the file (before we add nr). last is the corresponding cluster number on the disk. We will use last to plug the nr cluster. We will use file_cluster to update the cache. */ file_cluster = 0; if ((curr = MSDOS_I(inode)->i_start) != 0) { fat_cache_lookup(inode,INT_MAX,&last,&curr); file_cluster = last; while (curr && curr != -1){ PRINTK ((".")); file_cluster++; if (!(curr = fat_access(sb, last = curr,-1))) { fat_fs_panic(sb,"File without EOF"); return res; } } PRINTK ((" -- ")); }#ifdef DEBUGprintk("last = %d\n",last);#endif if (last) fat_access(sb,last,nr); else { MSDOS_I(inode)->i_start = nr; MSDOS_I(inode)->i_logstart = nr; mark_inode_dirty(inode); }#ifdef DEBUGif (last) printk("next set to %d\n",fat_access(sb,last,-1));#endif sector = MSDOS_SB(sb)->data_start+(nr-2)*cluster_size; last_sector = sector + cluster_size; if (MSDOS_SB(sb)->cvf_format && MSDOS_SB(sb)->cvf_format->zero_out_cluster) MSDOS_SB(sb)->cvf_format->zero_out_cluster(inode,nr); else for ( ; sector < last_sector; sector++) { #ifdef DEBUG printk("zeroing sector %d\n",sector); #endif if (!(bh = fat_getblk(sb, sector))) printk("getblk failed\n"); else { memset(bh->b_data,0,SECTOR_SIZE); fat_set_uptodate(sb, bh, 1); fat_mark_buffer_dirty(sb, bh); if (!res) res=bh; else fat_brelse(sb, bh); } } if (file_cluster != inode->i_blocks/cluster_size){ printk ("file_cluster badly computed!!! %d <> %ld\n" ,file_cluster,inode->i_blocks/cluster_size); }else{ fat_cache_add(inode,file_cluster,nr); } inode->i_blocks += cluster_size; if (inode->i_size & (SECTOR_SIZE-1)) { fat_fs_panic(sb,"Odd directory size"); inode->i_size = (inode->i_size+SECTOR_SIZE) & ~(SECTOR_SIZE-1); } inode->i_size += SECTOR_SIZE*cluster_size; MSDOS_I(inode)->mmu_private += SECTOR_SIZE*cluster_size; mark_inode_dirty(inode); return res;}/* Linear day numbers of the respective 1sts in non-leap years. */static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 }; /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */extern struct timezone sys_tz;/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */int date_dos2unix(unsigned short time,unsigned short date){ int month,year,secs; month = ((date >> 5) & 15)-1; year = date >> 9;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -