| 1 |
1628 |
jcastillo |
/*
|
| 2 |
|
|
* linux/fs/fat/misc.c
|
| 3 |
|
|
*
|
| 4 |
|
|
* Written 1992,1993 by Werner Almesberger
|
| 5 |
|
|
*/
|
| 6 |
|
|
|
| 7 |
|
|
#include <linux/fs.h>
|
| 8 |
|
|
#include <linux/msdos_fs.h>
|
| 9 |
|
|
#include <linux/sched.h>
|
| 10 |
|
|
#include <linux/kernel.h>
|
| 11 |
|
|
#include <linux/errno.h>
|
| 12 |
|
|
#include <linux/string.h>
|
| 13 |
|
|
#include <linux/stat.h>
|
| 14 |
|
|
|
| 15 |
|
|
#include "msbuffer.h"
|
| 16 |
|
|
|
| 17 |
|
|
#if 0
|
| 18 |
|
|
# define PRINTK(x) printk x
|
| 19 |
|
|
#else
|
| 20 |
|
|
# define PRINTK(x)
|
| 21 |
|
|
#endif
|
| 22 |
|
|
#define Printk(x) printk x
|
| 23 |
|
|
|
| 24 |
|
|
/* Well-known binary file extensions - of course there are many more */
|
| 25 |
|
|
|
| 26 |
|
|
static char bin_extensions[] =
|
| 27 |
|
|
"EXE" "COM" "BIN" "APP" "SYS" "DRV" "OVL" "OVR" "OBJ" "LIB" "DLL" "PIF" /* program code */
|
| 28 |
|
|
"ARC" "ZIP" "LHA" "LZH" "ZOO" "TAR" "Z " "ARJ" /* common archivers */
|
| 29 |
|
|
"TZ " "TAZ" "TZP" "TPZ" /* abbreviations of tar.Z and tar.zip */
|
| 30 |
|
|
"GZ " "TGZ" "DEB" "RPM" /* .gz, .tar.gz and Debian packages */
|
| 31 |
|
|
"GIF" "BMP" "TIF" "GL " "JPG" "PCX" /* graphics */
|
| 32 |
|
|
"TFM" "VF " "GF " "PK " "PXL" "DVI"; /* TeX */
|
| 33 |
|
|
|
| 34 |
|
|
|
| 35 |
|
|
/*
|
| 36 |
|
|
* fat_fs_panic reports a severe file system problem and sets the file system
|
| 37 |
|
|
* read-only. The file system can be made writable again by remounting it.
|
| 38 |
|
|
*/
|
| 39 |
|
|
|
| 40 |
|
|
void fat_fs_panic(struct super_block *s,const char *msg)
|
| 41 |
|
|
{
|
| 42 |
|
|
int not_ro;
|
| 43 |
|
|
|
| 44 |
|
|
not_ro = !(s->s_flags & MS_RDONLY);
|
| 45 |
|
|
if (not_ro) s->s_flags |= MS_RDONLY;
|
| 46 |
|
|
printk("Filesystem panic (dev %s, ", kdevname(s->s_dev));
|
| 47 |
|
|
printk("mounted on %s:%ld)\n %s\n", /* note: kdevname returns & static char[] */
|
| 48 |
|
|
kdevname(s->s_covered->i_dev), s->s_covered->i_ino, msg);
|
| 49 |
|
|
if (not_ro)
|
| 50 |
|
|
printk(" File system has been set read-only\n");
|
| 51 |
|
|
}
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
/*
|
| 55 |
|
|
* is_binary selects optional text conversion based on the conversion mode and
|
| 56 |
|
|
* the extension part of the file name.
|
| 57 |
|
|
*/
|
| 58 |
|
|
|
| 59 |
|
|
int is_binary(char conversion,char *extension)
|
| 60 |
|
|
{
|
| 61 |
|
|
char *walk;
|
| 62 |
|
|
|
| 63 |
|
|
switch (conversion) {
|
| 64 |
|
|
case 'b':
|
| 65 |
|
|
return 1;
|
| 66 |
|
|
case 't':
|
| 67 |
|
|
return 0;
|
| 68 |
|
|
case 'a':
|
| 69 |
|
|
for (walk = bin_extensions; *walk; walk += 3)
|
| 70 |
|
|
if (!strncmp(extension,walk,3)) return 1;
|
| 71 |
|
|
return 0;
|
| 72 |
|
|
default:
|
| 73 |
|
|
printk("Invalid conversion mode - defaulting to "
|
| 74 |
|
|
"binary.\n");
|
| 75 |
|
|
return 1;
|
| 76 |
|
|
}
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
|
| 80 |
|
|
/* File creation lock. This is system-wide to avoid deadlocks in rename. */
|
| 81 |
|
|
/* (rename might deadlock before detecting cross-FS moves.) */
|
| 82 |
|
|
|
| 83 |
|
|
static struct wait_queue *creation_wait = NULL;
|
| 84 |
|
|
static int creation_lock = 0;
|
| 85 |
|
|
|
| 86 |
|
|
|
| 87 |
|
|
void fat_lock_creation(void)
|
| 88 |
|
|
{
|
| 89 |
|
|
while (creation_lock) sleep_on(&creation_wait);
|
| 90 |
|
|
creation_lock = 1;
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
|
| 94 |
|
|
void fat_unlock_creation(void)
|
| 95 |
|
|
{
|
| 96 |
|
|
creation_lock = 0;
|
| 97 |
|
|
wake_up(&creation_wait);
|
| 98 |
|
|
}
|
| 99 |
|
|
|
| 100 |
|
|
|
| 101 |
|
|
void lock_fat(struct super_block *sb)
|
| 102 |
|
|
{
|
| 103 |
|
|
while (MSDOS_SB(sb)->fat_lock) sleep_on(&MSDOS_SB(sb)->fat_wait);
|
| 104 |
|
|
MSDOS_SB(sb)->fat_lock = 1;
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
|
| 108 |
|
|
void unlock_fat(struct super_block *sb)
|
| 109 |
|
|
{
|
| 110 |
|
|
MSDOS_SB(sb)->fat_lock = 0;
|
| 111 |
|
|
wake_up(&MSDOS_SB(sb)->fat_wait);
|
| 112 |
|
|
}
|
| 113 |
|
|
|
| 114 |
|
|
/* Flushes the number of free clusters on FAT32 */
|
| 115 |
|
|
/* XXX: Need to write one per FSINFO block. Currently only writes 1 */
|
| 116 |
|
|
void fat_clusters_flush(struct super_block *sb)
|
| 117 |
|
|
{
|
| 118 |
|
|
int offset;
|
| 119 |
|
|
struct buffer_head *bh;
|
| 120 |
|
|
struct fat_boot_fsinfo *fsinfo;
|
| 121 |
|
|
|
| 122 |
|
|
/* The fat32 boot fs info is at offset 0x3e0 by observation */
|
| 123 |
|
|
offset = MSDOS_SB(sb)->fsinfo_offset;
|
| 124 |
|
|
bh = fat_bread(sb, (offset >> SECTOR_BITS));
|
| 125 |
|
|
if (bh == NULL) {
|
| 126 |
|
|
printk("FAT bread failed in fat_clusters_flush\n");
|
| 127 |
|
|
return;
|
| 128 |
|
|
}
|
| 129 |
|
|
fsinfo = (struct fat_boot_fsinfo *)
|
| 130 |
|
|
&bh->b_data[offset & (SECTOR_SIZE-1)];
|
| 131 |
|
|
|
| 132 |
|
|
/* Sanity check */
|
| 133 |
|
|
if (CF_LE_L(fsinfo->signature) != 0x61417272) {
|
| 134 |
|
|
printk("fat_clusters_flush: Did not find valid FSINFO signature. Found 0x%x. offset=0x%x\n", CF_LE_L(fsinfo->signature), offset);
|
| 135 |
|
|
fat_brelse(sb, bh);
|
| 136 |
|
|
return;
|
| 137 |
|
|
}
|
| 138 |
|
|
fsinfo->free_clusters = CF_LE_L(MSDOS_SB(sb)->free_clusters);
|
| 139 |
|
|
fat_mark_buffer_dirty(sb, bh, 1);
|
| 140 |
|
|
fat_brelse(sb, bh);
|
| 141 |
|
|
}
|
| 142 |
|
|
|
| 143 |
|
|
/*
|
| 144 |
|
|
* fat_add_cluster tries to allocate a new cluster and adds it to the file
|
| 145 |
|
|
* represented by inode. The cluster is zero-initialized.
|
| 146 |
|
|
*/
|
| 147 |
|
|
|
| 148 |
|
|
int fat_add_cluster(struct inode *inode)
|
| 149 |
|
|
{
|
| 150 |
|
|
struct super_block *sb = inode->i_sb;
|
| 151 |
|
|
int count,nr,limit,last,curr,sector,last_sector,file_cluster;
|
| 152 |
|
|
struct buffer_head *bh;
|
| 153 |
|
|
int cluster_size = MSDOS_SB(sb)->cluster_size;
|
| 154 |
|
|
|
| 155 |
|
|
if (MSDOS_SB(sb)->fat_bits != 32) {
|
| 156 |
|
|
if (inode->i_ino == MSDOS_ROOT_INO) return -ENOSPC;
|
| 157 |
|
|
}
|
| 158 |
|
|
if (!MSDOS_SB(sb)->free_clusters) return -ENOSPC;
|
| 159 |
|
|
lock_fat(sb);
|
| 160 |
|
|
limit = MSDOS_SB(sb)->clusters;
|
| 161 |
|
|
nr = limit; /* to keep GCC happy */
|
| 162 |
|
|
for (count = 0; count < limit; count++) {
|
| 163 |
|
|
nr = ((count+MSDOS_SB(sb)->prev_free) % limit)+2;
|
| 164 |
|
|
if (fat_access(sb,nr,-1) == 0) break;
|
| 165 |
|
|
}
|
| 166 |
|
|
PRINTK (("cnt = %d --",count));
|
| 167 |
|
|
#ifdef DEBUG
|
| 168 |
|
|
printk("free cluster: %d\n",nr);
|
| 169 |
|
|
#endif
|
| 170 |
|
|
MSDOS_SB(sb)->prev_free = (count+MSDOS_SB(sb)->prev_free+1) % limit;
|
| 171 |
|
|
if (count >= limit) {
|
| 172 |
|
|
MSDOS_SB(sb)->free_clusters = 0;
|
| 173 |
|
|
unlock_fat(sb);
|
| 174 |
|
|
return -ENOSPC;
|
| 175 |
|
|
}
|
| 176 |
|
|
fat_access(sb,nr,MSDOS_SB(sb)->fat_bits == 12 ? EOF_FAT12 :
|
| 177 |
|
|
MSDOS_SB(sb)->fat_bits == 16 ? EOF_FAT16 : EOF_FAT32);
|
| 178 |
|
|
if (MSDOS_SB(sb)->free_clusters != -1) {
|
| 179 |
|
|
MSDOS_SB(sb)->free_clusters--;
|
| 180 |
|
|
}
|
| 181 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
| 182 |
|
|
fat_clusters_flush(sb);
|
| 183 |
|
|
}
|
| 184 |
|
|
unlock_fat(sb);
|
| 185 |
|
|
#ifdef DEBUG
|
| 186 |
|
|
printk("set to %x\n",fat_access(sb,nr,-1));
|
| 187 |
|
|
#endif
|
| 188 |
|
|
last = 0;
|
| 189 |
|
|
/* We must locate the last cluster of the file to add this
|
| 190 |
|
|
new one (nr) to the end of the link list (the FAT).
|
| 191 |
|
|
|
| 192 |
|
|
Here file_cluster will be the number of the last cluster of the
|
| 193 |
|
|
file (before we add nr).
|
| 194 |
|
|
|
| 195 |
|
|
last is the corresponding cluster number on the disk. We will
|
| 196 |
|
|
use last to plug the nr cluster. We will use file_cluster to
|
| 197 |
|
|
update the cache.
|
| 198 |
|
|
*/
|
| 199 |
|
|
file_cluster = 0;
|
| 200 |
|
|
if ((curr = MSDOS_I(inode)->i_start) != 0) {
|
| 201 |
|
|
cache_lookup(inode,INT_MAX,&last,&curr);
|
| 202 |
|
|
file_cluster = last;
|
| 203 |
|
|
while (curr && curr != -1){
|
| 204 |
|
|
PRINTK (("."));
|
| 205 |
|
|
file_cluster++;
|
| 206 |
|
|
if (!(curr = fat_access(sb,
|
| 207 |
|
|
last = curr,-1))) {
|
| 208 |
|
|
fat_fs_panic(sb,"File without EOF");
|
| 209 |
|
|
return -ENOSPC;
|
| 210 |
|
|
}
|
| 211 |
|
|
}
|
| 212 |
|
|
PRINTK ((" -- "));
|
| 213 |
|
|
}
|
| 214 |
|
|
#ifdef DEBUG
|
| 215 |
|
|
printk("last = %d\n",last);
|
| 216 |
|
|
#endif
|
| 217 |
|
|
if (last) fat_access(sb,last,nr);
|
| 218 |
|
|
else {
|
| 219 |
|
|
MSDOS_I(inode)->i_start = nr;
|
| 220 |
|
|
MSDOS_I(inode)->i_logstart = nr;
|
| 221 |
|
|
inode->i_dirt = 1;
|
| 222 |
|
|
}
|
| 223 |
|
|
#ifdef DEBUG
|
| 224 |
|
|
if (last) printk("next set to %d\n",fat_access(sb,last,-1));
|
| 225 |
|
|
#endif
|
| 226 |
|
|
sector = MSDOS_SB(sb)->data_start+(nr-2)*cluster_size;
|
| 227 |
|
|
last_sector = sector + cluster_size;
|
| 228 |
|
|
for ( ; sector < last_sector; sector++) {
|
| 229 |
|
|
#ifdef DEBUG
|
| 230 |
|
|
printk("zeroing sector %d\n",sector);
|
| 231 |
|
|
#endif
|
| 232 |
|
|
if (!(bh = fat_getblk(sb, sector)))
|
| 233 |
|
|
printk("getblk failed\n");
|
| 234 |
|
|
else {
|
| 235 |
|
|
memset(bh->b_data,0,SECTOR_SIZE);
|
| 236 |
|
|
fat_set_uptodate(sb, bh, 1);
|
| 237 |
|
|
fat_mark_buffer_dirty(sb, bh, 1);
|
| 238 |
|
|
fat_brelse(sb, bh);
|
| 239 |
|
|
}
|
| 240 |
|
|
}
|
| 241 |
|
|
if (file_cluster != inode->i_blocks/cluster_size){
|
| 242 |
|
|
printk ("file_cluster badly computed!!! %d <> %ld\n"
|
| 243 |
|
|
,file_cluster,inode->i_blocks/cluster_size);
|
| 244 |
|
|
}else{
|
| 245 |
|
|
cache_add(inode,file_cluster,nr);
|
| 246 |
|
|
}
|
| 247 |
|
|
inode->i_blocks += cluster_size;
|
| 248 |
|
|
if (S_ISDIR(inode->i_mode)) {
|
| 249 |
|
|
if (inode->i_size & (SECTOR_SIZE-1)) {
|
| 250 |
|
|
fat_fs_panic(sb,"Odd directory size");
|
| 251 |
|
|
inode->i_size = (inode->i_size+SECTOR_SIZE) &
|
| 252 |
|
|
~(SECTOR_SIZE-1);
|
| 253 |
|
|
}
|
| 254 |
|
|
inode->i_size += SECTOR_SIZE*cluster_size;
|
| 255 |
|
|
#ifdef DEBUG
|
| 256 |
|
|
printk("size is %d now (%x)\n",inode->i_size,inode);
|
| 257 |
|
|
#endif
|
| 258 |
|
|
inode->i_dirt = 1;
|
| 259 |
|
|
}
|
| 260 |
|
|
return 0;
|
| 261 |
|
|
}
|
| 262 |
|
|
|
| 263 |
|
|
|
| 264 |
|
|
/* Linear day numbers of the respective 1sts in non-leap years. */
|
| 265 |
|
|
|
| 266 |
|
|
static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
|
| 267 |
|
|
/* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
|
| 268 |
|
|
|
| 269 |
|
|
|
| 270 |
|
|
extern struct timezone sys_tz;
|
| 271 |
|
|
|
| 272 |
|
|
|
| 273 |
|
|
/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
|
| 274 |
|
|
|
| 275 |
|
|
int date_dos2unix(unsigned short time,unsigned short date)
|
| 276 |
|
|
{
|
| 277 |
|
|
int month,year,secs;
|
| 278 |
|
|
|
| 279 |
|
|
month = ((date >> 5) & 15)-1;
|
| 280 |
|
|
year = date >> 9;
|
| 281 |
|
|
secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
|
| 282 |
|
|
((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
|
| 283 |
|
|
month < 2 ? 1 : 0)+3653);
|
| 284 |
|
|
/* days since 1.1.70 plus 80's leap day */
|
| 285 |
|
|
secs += sys_tz.tz_minuteswest*60;
|
| 286 |
|
|
if (sys_tz.tz_dsttime) {
|
| 287 |
|
|
secs -= 3600;
|
| 288 |
|
|
}
|
| 289 |
|
|
return secs;
|
| 290 |
|
|
}
|
| 291 |
|
|
|
| 292 |
|
|
|
| 293 |
|
|
/* Convert linear UNIX date to a MS-DOS time/date pair. */
|
| 294 |
|
|
|
| 295 |
|
|
void fat_date_unix2dos(int unix_date,unsigned short *time,
|
| 296 |
|
|
unsigned short *date)
|
| 297 |
|
|
{
|
| 298 |
|
|
int day,year,nl_day,month;
|
| 299 |
|
|
|
| 300 |
|
|
unix_date -= sys_tz.tz_minuteswest*60;
|
| 301 |
|
|
if (sys_tz.tz_dsttime) unix_date += 3600;
|
| 302 |
|
|
|
| 303 |
|
|
*time = (unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
|
| 304 |
|
|
(((unix_date/3600) % 24) << 11);
|
| 305 |
|
|
day = unix_date/86400-3652;
|
| 306 |
|
|
year = day/365;
|
| 307 |
|
|
if ((year+3)/4+365*year > day) year--;
|
| 308 |
|
|
day -= (year+3)/4+365*year;
|
| 309 |
|
|
if (day == 59 && !(year & 3)) {
|
| 310 |
|
|
nl_day = day;
|
| 311 |
|
|
month = 2;
|
| 312 |
|
|
}
|
| 313 |
|
|
else {
|
| 314 |
|
|
nl_day = (year & 3) || day <= 59 ? day : day-1;
|
| 315 |
|
|
for (month = 0; month < 12; month++)
|
| 316 |
|
|
if (day_n[month] > nl_day) break;
|
| 317 |
|
|
}
|
| 318 |
|
|
*date = nl_day-day_n[month-1]+1+(month << 5)+(year << 9);
|
| 319 |
|
|
}
|
| 320 |
|
|
|
| 321 |
|
|
|
| 322 |
|
|
/* Returns the inode number of the directory entry at offset pos. If bh is
|
| 323 |
|
|
non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
|
| 324 |
|
|
returned in bh. */
|
| 325 |
|
|
|
| 326 |
|
|
int fat_get_entry(struct inode *dir, loff_t *pos,struct buffer_head **bh,
|
| 327 |
|
|
struct msdos_dir_entry **de)
|
| 328 |
|
|
{
|
| 329 |
|
|
struct super_block *sb = dir->i_sb;
|
| 330 |
|
|
int sector, offset;
|
| 331 |
|
|
|
| 332 |
|
|
while (1) {
|
| 333 |
|
|
offset = *pos;
|
| 334 |
|
|
PRINTK (("get_entry offset %d\n",offset));
|
| 335 |
|
|
if ((sector = fat_smap(dir,offset >> SECTOR_BITS)) == -1)
|
| 336 |
|
|
return -1;
|
| 337 |
|
|
PRINTK (("get_entry sector %d %p\n",sector,*bh));
|
| 338 |
|
|
if (!sector)
|
| 339 |
|
|
return -1; /* beyond EOF */
|
| 340 |
|
|
*pos += sizeof(struct msdos_dir_entry);
|
| 341 |
|
|
if (*bh)
|
| 342 |
|
|
fat_brelse(sb, *bh);
|
| 343 |
|
|
PRINTK (("get_entry sector apres brelse\n"));
|
| 344 |
|
|
if (!(*bh = fat_bread(sb, sector))) {
|
| 345 |
|
|
printk("Directory sread (sector 0x%x) failed\n",sector);
|
| 346 |
|
|
continue;
|
| 347 |
|
|
}
|
| 348 |
|
|
PRINTK (("get_entry apres sread\n"));
|
| 349 |
|
|
*de = (struct msdos_dir_entry *) ((*bh)->b_data+(offset &
|
| 350 |
|
|
(SECTOR_SIZE-1)));
|
| 351 |
|
|
return (sector << MSDOS_DPS_BITS)+((offset & (SECTOR_SIZE-1)) >>
|
| 352 |
|
|
MSDOS_DIR_BITS);
|
| 353 |
|
|
}
|
| 354 |
|
|
}
|
| 355 |
|
|
|
| 356 |
|
|
|
| 357 |
|
|
/*
|
| 358 |
|
|
* Now an ugly part: this set of directory scan routines works on clusters
|
| 359 |
|
|
* rather than on inodes and sectors. They are necessary to locate the '..'
|
| 360 |
|
|
* directory "inode". raw_scan_sector operates in four modes:
|
| 361 |
|
|
*
|
| 362 |
|
|
* name number ino action
|
| 363 |
|
|
* -------- -------- -------- -------------------------------------------------
|
| 364 |
|
|
* non-NULL - X Find an entry with that name
|
| 365 |
|
|
* NULL non-NULL non-NULL Find an entry whose data starts at *number
|
| 366 |
|
|
* NULL non-NULL NULL Count subdirectories in *number. (*)
|
| 367 |
|
|
* NULL NULL non-NULL Find an empty entry
|
| 368 |
|
|
*
|
| 369 |
|
|
* (*) The return code should be ignored. It DOES NOT indicate success or
|
| 370 |
|
|
* failure. *number has to be initialized to zero.
|
| 371 |
|
|
*
|
| 372 |
|
|
* - = not used, X = a value is returned unless NULL
|
| 373 |
|
|
*
|
| 374 |
|
|
* If res_bh is non-NULL, the buffer is not deallocated but returned to the
|
| 375 |
|
|
* caller on success. res_de is set accordingly.
|
| 376 |
|
|
*
|
| 377 |
|
|
* If cont is non-zero, raw_found continues with the entry after the one
|
| 378 |
|
|
* res_bh/res_de point to.
|
| 379 |
|
|
*/
|
| 380 |
|
|
|
| 381 |
|
|
|
| 382 |
|
|
#define RSS_NAME /* search for name */ \
|
| 383 |
|
|
done = !strncmp(data[entry].name,name,MSDOS_NAME) && \
|
| 384 |
|
|
!(data[entry].attr & ATTR_VOLUME);
|
| 385 |
|
|
|
| 386 |
|
|
#define RSS_START /* search for start cluster */ \
|
| 387 |
|
|
done = !IS_FREE(data[entry].name) \
|
| 388 |
|
|
&& ( \
|
| 389 |
|
|
( \
|
| 390 |
|
|
(MSDOS_SB(sb)->fat_bits != 32) ? 0 : (CF_LE_W(data[entry].starthi) << 16) \
|
| 391 |
|
|
) \
|
| 392 |
|
|
| CF_LE_W(data[entry].start) \
|
| 393 |
|
|
) == *number;
|
| 394 |
|
|
|
| 395 |
|
|
#define RSS_FREE /* search for free entry */ \
|
| 396 |
|
|
{ \
|
| 397 |
|
|
done = IS_FREE(data[entry].name); \
|
| 398 |
|
|
if (done) { \
|
| 399 |
|
|
inode = iget(sb,sector*MSDOS_DPS+entry); \
|
| 400 |
|
|
if (inode) { \
|
| 401 |
|
|
/* Directory slots of busy deleted files aren't available yet. */ \
|
| 402 |
|
|
done = !MSDOS_I(inode)->i_busy; \
|
| 403 |
|
|
iput(inode); \
|
| 404 |
|
|
} \
|
| 405 |
|
|
} \
|
| 406 |
|
|
}
|
| 407 |
|
|
|
| 408 |
|
|
#define RSS_COUNT /* count subdirectories */ \
|
| 409 |
|
|
{ \
|
| 410 |
|
|
done = 0; \
|
| 411 |
|
|
if (!IS_FREE(data[entry].name) && (data[entry].attr & ATTR_DIR)) \
|
| 412 |
|
|
(*number)++; \
|
| 413 |
|
|
}
|
| 414 |
|
|
|
| 415 |
|
|
static int raw_scan_sector(struct super_block *sb,int sector,const char *name,
|
| 416 |
|
|
int *number,int *ino,struct buffer_head **res_bh,
|
| 417 |
|
|
struct msdos_dir_entry **res_de,char scantype)
|
| 418 |
|
|
{
|
| 419 |
|
|
struct buffer_head *bh;
|
| 420 |
|
|
struct msdos_dir_entry *data;
|
| 421 |
|
|
struct inode *inode;
|
| 422 |
|
|
int entry,start,done;
|
| 423 |
|
|
|
| 424 |
|
|
if (!(bh = fat_bread(sb,sector)))
|
| 425 |
|
|
return -EIO;
|
| 426 |
|
|
data = (struct msdos_dir_entry *) bh->b_data;
|
| 427 |
|
|
for (entry = 0; entry < MSDOS_DPS; entry++) {
|
| 428 |
|
|
/* RSS_COUNT: if (data[entry].name == name) done=true else done=false. */
|
| 429 |
|
|
if (name) {
|
| 430 |
|
|
RSS_NAME
|
| 431 |
|
|
if (done && scantype) { /* scantype != SCAN_ANY */
|
| 432 |
|
|
done = (data[entry].attr & ATTR_HIDDEN)
|
| 433 |
|
|
? (scantype==SCAN_HID)
|
| 434 |
|
|
: (scantype==SCAN_NOTHID);
|
| 435 |
|
|
}
|
| 436 |
|
|
} else {
|
| 437 |
|
|
if (!ino) RSS_COUNT
|
| 438 |
|
|
else {
|
| 439 |
|
|
if (number) RSS_START
|
| 440 |
|
|
else RSS_FREE
|
| 441 |
|
|
}
|
| 442 |
|
|
}
|
| 443 |
|
|
if (done) {
|
| 444 |
|
|
if (ino) *ino = sector*MSDOS_DPS+entry;
|
| 445 |
|
|
start = CF_LE_W(data[entry].start);
|
| 446 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
| 447 |
|
|
start |= (CF_LE_W(data[entry].starthi) << 16);
|
| 448 |
|
|
}
|
| 449 |
|
|
if (!res_bh)
|
| 450 |
|
|
fat_brelse(sb, bh);
|
| 451 |
|
|
else {
|
| 452 |
|
|
*res_bh = bh;
|
| 453 |
|
|
*res_de = &data[entry];
|
| 454 |
|
|
}
|
| 455 |
|
|
return start;
|
| 456 |
|
|
}
|
| 457 |
|
|
}
|
| 458 |
|
|
fat_brelse(sb, bh);
|
| 459 |
|
|
return -ENOENT;
|
| 460 |
|
|
}
|
| 461 |
|
|
|
| 462 |
|
|
|
| 463 |
|
|
/*
|
| 464 |
|
|
* raw_scan_root performs raw_scan_sector on the root directory until the
|
| 465 |
|
|
* requested entry is found or the end of the directory is reached.
|
| 466 |
|
|
*/
|
| 467 |
|
|
|
| 468 |
|
|
static int raw_scan_root(struct super_block *sb,const char *name,int *number,int *ino,
|
| 469 |
|
|
struct buffer_head **res_bh,struct msdos_dir_entry **res_de,char scantype)
|
| 470 |
|
|
{
|
| 471 |
|
|
int count,cluster;
|
| 472 |
|
|
|
| 473 |
|
|
for (count = 0; count < MSDOS_SB(sb)->dir_entries/MSDOS_DPS; count++) {
|
| 474 |
|
|
if ((cluster = raw_scan_sector(sb,MSDOS_SB(sb)->dir_start+count,
|
| 475 |
|
|
name,number,ino,res_bh,res_de,scantype)) >= 0) return cluster;
|
| 476 |
|
|
}
|
| 477 |
|
|
return -ENOENT;
|
| 478 |
|
|
}
|
| 479 |
|
|
|
| 480 |
|
|
|
| 481 |
|
|
/*
|
| 482 |
|
|
* raw_scan_nonroot performs raw_scan_sector on a non-root directory until the
|
| 483 |
|
|
* requested entry is found or the end of the directory is reached.
|
| 484 |
|
|
*/
|
| 485 |
|
|
|
| 486 |
|
|
static int raw_scan_nonroot(struct super_block *sb,int start,const char *name,
|
| 487 |
|
|
int *number,int *ino,struct buffer_head **res_bh,struct msdos_dir_entry
|
| 488 |
|
|
**res_de,char scantype)
|
| 489 |
|
|
{
|
| 490 |
|
|
int count,cluster;
|
| 491 |
|
|
|
| 492 |
|
|
#ifdef DEBUG
|
| 493 |
|
|
printk("raw_scan_nonroot: start=%d\n",start);
|
| 494 |
|
|
#endif
|
| 495 |
|
|
do {
|
| 496 |
|
|
for (count = 0; count < MSDOS_SB(sb)->cluster_size; count++) {
|
| 497 |
|
|
if ((cluster = raw_scan_sector(sb,(start-2)*
|
| 498 |
|
|
MSDOS_SB(sb)->cluster_size+MSDOS_SB(sb)->data_start+
|
| 499 |
|
|
count,name,number,ino,res_bh,res_de,scantype)) >= 0)
|
| 500 |
|
|
return cluster;
|
| 501 |
|
|
}
|
| 502 |
|
|
if (!(start = fat_access(sb,start,-1))) {
|
| 503 |
|
|
fat_fs_panic(sb,"FAT error");
|
| 504 |
|
|
break;
|
| 505 |
|
|
}
|
| 506 |
|
|
#ifdef DEBUG
|
| 507 |
|
|
printk("next start: %d\n",start);
|
| 508 |
|
|
#endif
|
| 509 |
|
|
}
|
| 510 |
|
|
while (start != -1);
|
| 511 |
|
|
return -ENOENT;
|
| 512 |
|
|
}
|
| 513 |
|
|
|
| 514 |
|
|
|
| 515 |
|
|
/*
|
| 516 |
|
|
* raw_scan performs raw_scan_sector on any sector.
|
| 517 |
|
|
*
|
| 518 |
|
|
* NOTE: raw_scan must not be used on a directory that is the process of
|
| 519 |
|
|
* being created.
|
| 520 |
|
|
*/
|
| 521 |
|
|
|
| 522 |
|
|
static int raw_scan(struct super_block *sb, int start, const char *name,
|
| 523 |
|
|
int *number, int *ino, struct buffer_head **res_bh,
|
| 524 |
|
|
struct msdos_dir_entry **res_de, char scantype)
|
| 525 |
|
|
{
|
| 526 |
|
|
if (start) return raw_scan_nonroot
|
| 527 |
|
|
(sb,start,name,number,ino,res_bh,res_de,scantype);
|
| 528 |
|
|
else return raw_scan_root
|
| 529 |
|
|
(sb,name,number,ino,res_bh,res_de,scantype);
|
| 530 |
|
|
}
|
| 531 |
|
|
|
| 532 |
|
|
|
| 533 |
|
|
/*
|
| 534 |
|
|
* fat_parent_ino returns the inode number of the parent directory of dir.
|
| 535 |
|
|
* File creation has to be deferred while fat_parent_ino is running to
|
| 536 |
|
|
* prevent renames.
|
| 537 |
|
|
*/
|
| 538 |
|
|
|
| 539 |
|
|
int fat_parent_ino(struct inode *dir,int locked)
|
| 540 |
|
|
{
|
| 541 |
|
|
static int zero = 0;
|
| 542 |
|
|
int error,curr,prev,nr;
|
| 543 |
|
|
|
| 544 |
|
|
PRINTK(("fat_parent_ino: Debug 0\n"));
|
| 545 |
|
|
if (!S_ISDIR(dir->i_mode)) panic("Non-directory fed to m_p_i");
|
| 546 |
|
|
if (dir->i_ino == MSDOS_ROOT_INO) return dir->i_ino;
|
| 547 |
|
|
if (!locked) fat_lock_creation(); /* prevent renames */
|
| 548 |
|
|
if ((curr = raw_scan(dir->i_sb,MSDOS_I(dir)->i_start,MSDOS_DOTDOT,
|
| 549 |
|
|
&zero,NULL,NULL,NULL,SCAN_ANY)) < 0) {
|
| 550 |
|
|
if (!locked) fat_unlock_creation();
|
| 551 |
|
|
return curr;
|
| 552 |
|
|
}
|
| 553 |
|
|
PRINTK(("fat_parent_ino: Debug 1 curr=%d\n", curr));
|
| 554 |
|
|
if (!curr) nr = MSDOS_ROOT_INO;
|
| 555 |
|
|
else {
|
| 556 |
|
|
PRINTK(("fat_parent_ino: Debug 2\n"));
|
| 557 |
|
|
if ((prev = raw_scan(dir->i_sb,curr,MSDOS_DOTDOT,&zero,NULL,
|
| 558 |
|
|
NULL,NULL,SCAN_ANY)) < 0) {
|
| 559 |
|
|
PRINTK(("fat_parent_ino: Debug 3 prev=%d\n", prev));
|
| 560 |
|
|
if (!locked) fat_unlock_creation();
|
| 561 |
|
|
return prev;
|
| 562 |
|
|
}
|
| 563 |
|
|
PRINTK(("fat_parent_ino: Debug 4 prev=%d\n", prev));
|
| 564 |
|
|
if (prev == 0 && MSDOS_SB(dir->i_sb)->fat_bits == 32) {
|
| 565 |
|
|
prev = MSDOS_SB(dir->i_sb)->root_cluster;
|
| 566 |
|
|
}
|
| 567 |
|
|
if ((error = raw_scan(dir->i_sb,prev,NULL,&curr,&nr,NULL,
|
| 568 |
|
|
NULL,SCAN_ANY)) < 0) {
|
| 569 |
|
|
PRINTK(("fat_parent_ino: Debug 5 error=%d\n", error));
|
| 570 |
|
|
if (!locked) fat_unlock_creation();
|
| 571 |
|
|
return error;
|
| 572 |
|
|
}
|
| 573 |
|
|
PRINTK(("fat_parent_ino: Debug 6 nr=%d\n", nr));
|
| 574 |
|
|
}
|
| 575 |
|
|
if (!locked) fat_unlock_creation();
|
| 576 |
|
|
return nr;
|
| 577 |
|
|
}
|
| 578 |
|
|
|
| 579 |
|
|
|
| 580 |
|
|
/*
|
| 581 |
|
|
* fat_subdirs counts the number of sub-directories of dir. It can be run
|
| 582 |
|
|
* on directories being created.
|
| 583 |
|
|
*/
|
| 584 |
|
|
|
| 585 |
|
|
int fat_subdirs(struct inode *dir)
|
| 586 |
|
|
{
|
| 587 |
|
|
int count;
|
| 588 |
|
|
|
| 589 |
|
|
count = 0;
|
| 590 |
|
|
if ((dir->i_ino == MSDOS_ROOT_INO) &&
|
| 591 |
|
|
(MSDOS_SB(dir->i_sb)->fat_bits != 32)) {
|
| 592 |
|
|
(void) raw_scan_root(dir->i_sb,NULL,&count,NULL,NULL,NULL,SCAN_ANY);
|
| 593 |
|
|
} else {
|
| 594 |
|
|
if ((dir->i_ino != MSDOS_ROOT_INO) &&
|
| 595 |
|
|
!MSDOS_I(dir)->i_start) return 0; /* in mkdir */
|
| 596 |
|
|
else (void) raw_scan_nonroot(dir->i_sb,MSDOS_I(dir)->i_start,
|
| 597 |
|
|
NULL,&count,NULL,NULL,NULL,SCAN_ANY);
|
| 598 |
|
|
}
|
| 599 |
|
|
return count;
|
| 600 |
|
|
}
|
| 601 |
|
|
|
| 602 |
|
|
|
| 603 |
|
|
/*
|
| 604 |
|
|
* Scans a directory for a given file (name points to its formatted name) or
|
| 605 |
|
|
* for an empty directory slot (name is NULL). Returns an error code or zero.
|
| 606 |
|
|
*/
|
| 607 |
|
|
|
| 608 |
|
|
int fat_scan(struct inode *dir,const char *name,struct buffer_head **res_bh,
|
| 609 |
|
|
struct msdos_dir_entry **res_de,int *ino, char scantype)
|
| 610 |
|
|
{
|
| 611 |
|
|
int res;
|
| 612 |
|
|
|
| 613 |
|
|
res = raw_scan(dir->i_sb,MSDOS_I(dir)->i_start,
|
| 614 |
|
|
name, NULL, ino, res_bh, res_de, scantype);
|
| 615 |
|
|
return res<0 ? res : 0;
|
| 616 |
|
|
}
|