root/fs/vfat/namei.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. vfat_put_super
  2. vfat_read_super
  3. dump_fat
  4. dump_de
  5. vfat_valid_longname
  6. vfat_valid_shortname
  7. vfat_format_name
  8. vfat_create_shortname
  9. vfat_find_free_slots
  10. vfat_build_slots
  11. vfat_get_longname
  12. vfat_find
  13. vfat_lookup
  14. vfat_create_entry
  15. vfat_create
  16. vfat_create_a_dotdir
  17. vfat_create_dotdirs
  18. vfat_empty
  19. vfat_rmdir_free_ino
  20. vfat_unlink_free_ino
  21. vfat_remove_entry
  22. vfat_rmdirx
  23. vfat_rmdir
  24. vfat_unlinkx
  25. vfat_mkdir
  26. vfat_unlink
  27. vfat_rename
  28. vfat_read_inode
  29. init_vfat_fs
  30. init_module
  31. cleanup_module

   1 /*
   2  *  linux/fs/vfat/namei.c
   3  *
   4  *  Written 1992,1993 by Werner Almesberger
   5  *
   6  *  Windows95/Windows NT compatible extended MSDOS filesystem
   7  *    by Gordon Chaffee Copyright (C) 1995
   8  */
   9 
  10 #include <linux/module.h>
  11 
  12 #include <linux/sched.h>
  13 #include <linux/msdos_fs.h>
  14 #include <linux/kernel.h>
  15 #include <linux/errno.h>
  16 #include <linux/string.h>
  17 #include <linux/stat.h>
  18 
  19 #include <asm/segment.h>
  20 
  21 #include "../fat/msbuffer.h"
  22 #include "../fat/tables.h"
  23 
  24 #if 0
  25 #define PRINTK(x) printk x
  26 #else
  27 #define PRINTK(x)
  28 #endif
  29 
  30 
  31 void vfat_read_inode(struct inode *inode);
  32 
  33 
  34 void vfat_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         fat_put_super(sb);
  37         MOD_DEC_USE_COUNT;
  38 }
  39 
  40 
  41 static struct super_operations vfat_sops = { 
  42         vfat_read_inode,
  43         fat_notify_change,
  44         fat_write_inode,
  45         fat_put_inode,
  46         vfat_put_super,
  47         NULL, /* added in 0.96c */
  48         fat_statfs,
  49         NULL
  50 };
  51 
  52 struct super_block *vfat_read_super(struct super_block *sb,void *data,
     /* [previous][next][first][last][top][bottom][index][help] */
  53                                     int silent)
  54 {
  55         struct super_block *res;
  56   
  57         MOD_INC_USE_COUNT;
  58         
  59         sb->s_op = &vfat_sops;
  60         res = fat_read_super(sb, data, silent);
  61         if (res == NULL) {
  62           MOD_DEC_USE_COUNT;
  63         } else {
  64           MSDOS_SB(sb)->vfat = 1;
  65           MSDOS_SB(sb)->dotsOK = 0;
  66         }
  67 
  68         return res;
  69 }
  70 
  71 
  72 
  73 #ifdef DEBUG
  74 
  75 static void dump_fat(struct super_block *sb,int start)
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77         printk("[");
  78         while (start) {
  79                 printk("%d ",start);
  80                 start = fat_access(sb,start,-1);
  81                 if (!start) {
  82                         printk("ERROR");
  83                         break;
  84                 }
  85                 if (start == -1) break;
  86         }
  87         printk("]\n");
  88 }
  89 
  90 static void dump_de(struct msdos_dir_entry *de)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         int i;
  93         unsigned char *p = (unsigned char *) de;
  94         printk("[");
  95 
  96         for (i = 0; i < 32; i++, p++) {
  97                 printk("%02x ", *p);
  98         }
  99         printk("]\n");
 100 }
 101 
 102 #endif
 103 
 104 /* MS-DOS "device special files" */
 105 
 106 static const char *reserved_names[] = {
 107         "CON     ","PRN     ","NUL     ","AUX     ",
 108         "LPT1    ","LPT2    ","LPT3    ","LPT4    ",
 109         "COM1    ","COM2    ","COM3    ","COM4    ",
 110         NULL };
 111 
 112 
 113 /* Characters that are undesirable in an MS-DOS file name */
 114 
 115 #if 1  /* Steve Searle's characters */
 116 static char bad_chars[] = "*?<>|\":/\\";
 117 static char bad_if_strict[] = "+=,; []";
 118 #else
 119 static char bad_chars[] = "*?<>|\"";
 120 static char bad_if_strict[] = "+=,; ";
 121 #endif
 122 static char replace_chars[] = "[];,+=";
 123 
 124 static int vfat_find(struct inode *dir,const char *name,int len,
 125                       int find_long,int new_filename,int is_dir,
 126                       struct slot_info *sinfo_out);
 127 
 128 /* Checks the validity of an long MS-DOS filename */
 129 /* Returns negative number on error, 0 for a normal
 130  * return, and 1 for . or .. */
 131 
 132 static int vfat_valid_longname(const char *name, int len, int dot_dirs)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134         const char **reserved;
 135         unsigned char c;
 136         int i;
 137 
 138         if (IS_FREE(name)) return -EINVAL;
 139         if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.'))) {
 140                 if (!dot_dirs) return -EEXIST;
 141                 return 1;
 142         }
 143 
 144         if (len && name[len-1] == ' ') return -EINVAL;
 145         if (len >= 256) return -EINVAL;
 146         for (i = 0; i < len; i++) {
 147                 c = name[i];
 148                 if (strchr(bad_chars,c)) return -EINVAL;
 149         }
 150         if (len == 3 || len == 4) {
 151                 for (reserved = reserved_names; *reserved; reserved++)
 152                         if (!strncmp(name,*reserved,8)) return -EINVAL;
 153         }
 154         return 0;
 155 }
 156 
 157 static int vfat_valid_shortname(char conv,const char *name,int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 158                                  int dot_dirs)
 159 {
 160         const char *walk, **reserved;
 161         unsigned char c;
 162         int space;
 163 
 164         if (IS_FREE(name)) return -EINVAL;
 165         if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.'))) {
 166                 if (!dot_dirs) return -EEXIST;
 167                 return 1;
 168         }
 169 
 170         space = 1; /* disallow names starting with a dot */
 171         c = 0;
 172         for (walk = name; len && walk-name < 8;) {
 173                 c = *walk++;
 174                 len--;
 175                 if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
 176                 if (conv == 'x' && strchr(replace_chars,c)) return -EINVAL;
 177                 if (conv == 's' && strchr(bad_if_strict,c)) return -EINVAL;
 178                 if (c >= 'A' && c <= 'Z' && (conv == 's' || conv == 'x')) return -EINVAL;
 179                 if (c < ' ' || c == ':' || c == '\\') return -EINVAL;
 180                 if ((walk == name) && (c == 0xE5)) c = 0x05;
 181                 if (c == '.') break;
 182                 space = c == ' ';
 183         }
 184         if (space) return -EINVAL;
 185         if ((conv == 's' || conv == 'x') && len && c != '.') {
 186                 c = *walk++;
 187                 len--;
 188                 if (c != '.') return -EINVAL;
 189         }
 190         while (c != '.' && len--) c = *walk++;
 191         if (c == '.') {
 192                 if (len >= 4) return -EINVAL;
 193                 while (len > 0 && walk-name < (MSDOS_NAME+1)) {
 194                         c = *walk++;
 195                         len--;
 196                         if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
 197                         if (conv == 's' && strchr(bad_if_strict,c))
 198                                 return -EINVAL;
 199                         if (conv == 'x' && strchr(replace_chars,c))
 200                                 return -EINVAL;
 201                         if (c < ' ' || c == ':' || c == '\\' || c == '.')
 202                                 return -EINVAL;
 203                         if (c >= 'A' && c <= 'Z' && (conv == 's' || conv == 'x')) return -EINVAL;
 204                         space = c == ' ';
 205                 }
 206                 if (space) return -EINVAL;
 207                 if ((conv == 's' || conv == 'x') && len) return -EINVAL;
 208         }
 209         for (reserved = reserved_names; *reserved; reserved++)
 210                 if (!strncmp(name,*reserved,8)) return -EINVAL;
 211 
 212         return 0;
 213 }
 214 
 215 /* Takes a short filename and converts it to a formatted MS-DOS filename.
 216  * If the short filename is not a valid MS-DOS filename, an error is 
 217  * returned.  The formatted short filename is returned in 'res'.
 218  */
 219 
 220 static int vfat_format_name(char conv,const char *name,int len,char *res,
     /* [previous][next][first][last][top][bottom][index][help] */
 221   int dot_dirs)
 222 {
 223         char *walk;
 224         const char **reserved;
 225         unsigned char c;
 226         int space;
 227 
 228         if (IS_FREE(name)) return -EINVAL;
 229         if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.'))) {
 230                 if (!dot_dirs) return -EEXIST;
 231                 memset(res+1,' ',10);
 232                 while (len--) *res++ = '.';
 233                 return 0;
 234         }
 235 
 236         space = 1; /* disallow names starting with a dot */
 237         c = 0;
 238         for (walk = res; len && walk-res < 8; walk++) {
 239                 c = *name++;
 240                 len--;
 241                 if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
 242                 if (conv == 's' && strchr(bad_if_strict,c)) return -EINVAL;
 243                 if (conv == 'x' && strchr(replace_chars,c)) return -EINVAL;
 244                 if (c >= 'A' && c <= 'Z' && (conv == 's' || conv == 'x')) return -EINVAL;
 245                 if (c < ' ' || c == ':' || c == '\\') return -EINVAL;
 246                 if (c == '.') break;
 247                 space = c == ' ';
 248                 *walk = c >= 'a' && c <= 'z' ? c-32 : c;
 249         }
 250         if (space) return -EINVAL;
 251         if ((conv == 's' || conv == 'x') && len && c != '.') {
 252                 c = *name++;
 253                 len--;
 254                 if (c != '.') return -EINVAL;
 255         }
 256         while (c != '.' && len--) c = *name++;
 257         if (c == '.') {
 258                 while (walk-res < 8) *walk++ = ' ';
 259                 while (len > 0 && walk-res < MSDOS_NAME) {
 260                         c = *name++;
 261                         len--;
 262                         if (conv != 'r' && strchr(bad_chars,c)) return -EINVAL;
 263                         if (conv == 's' && strchr(bad_if_strict,c))
 264                                 return -EINVAL;
 265                         if (conv == 'x' && strchr(replace_chars,c))
 266                                 return -EINVAL;
 267                         if (c < ' ' || c == ':' || c == '\\' || c == '.')
 268                                 return -EINVAL;
 269                         if (c >= 'A' && c <= 'Z' && (conv == 's' || conv == 'x')) return -EINVAL;
 270                         space = c == ' ';
 271                         *walk++ = c >= 'a' && c <= 'z' ? c-32 : c;
 272                 }
 273                 if (space) return -EINVAL;
 274                 if ((conv == 's' || conv == 'x') && len) return -EINVAL;
 275         }
 276         while (walk-res < MSDOS_NAME) *walk++ = ' ';
 277         for (reserved = reserved_names; *reserved; reserved++)
 278                 if (!strncmp(res,*reserved,8)) return -EINVAL;
 279 
 280         return 0;
 281 }
 282 
 283 static char skip_chars[] = ".:\"?<>| ";
 284 
 285 /* Given a valid longname, create a unique shortname.  Make sure the
 286  * shortname does not exist
 287  */
 288 static int vfat_create_shortname(struct inode *dir, const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
 289      int len, char *name_res)
 290 {
 291         const char *ip, *ext_start, *end;
 292         char *p;
 293         int valid;
 294         int sz, extlen, baselen, totlen;
 295         char msdos_name[13];
 296         char base[9], ext[4];
 297         int i;
 298         int res;
 299         int ino;
 300         int spaces;
 301         int count;
 302         char buf[8];
 303         struct slot_info sinfo;
 304         const char *name_start;
 305 
 306         PRINTK(("Entering vfat_create_shortname: name=%s, len=%d\n", name, len));
 307         sz = 0;                 /* Make compiler happy */
 308         valid = 1;
 309         if (len && name[len-1]==' ') return -EINVAL;
 310         if (len <= 12) {
 311                 /* Do a case insensitive search if the name would be a valid
 312                  * shortname if is were all capitalized */
 313                 for (i = 0, p = msdos_name, ip = name; i < len; i++, p++, ip++)
 314                 {
 315                         if (*ip >= 'A' && *ip <= 'Z') {
 316                                 *p = *ip + 32;
 317                         } else {
 318                                 *p = *ip;
 319                         }
 320                 }
 321                 res = vfat_format_name('x', msdos_name, len, name_res, 1);
 322                 if (res > -1) {
 323                         PRINTK(("vfat_create_shortname 1\n"));
 324                         res = vfat_find(dir, msdos_name, len, 0, 0, 0, &sinfo);
 325                         ino = sinfo.ino;
 326                         PRINTK(("vfat_create_shortname 2\n"));
 327                         if (res > -1) return -EEXIST;
 328                         return 0;
 329                 }
 330         }
 331 
 332         PRINTK(("vfat_create_shortname 3\n"));
 333         /* Now, we need to create a shortname from the long name */
 334         ext_start = end = &name[len];
 335         while (--ext_start >= name) {
 336                 if (*ext_start == '.') {
 337                         if (ext_start == end - 1) {
 338                                 sz = len;
 339                                 ext_start = NULL;
 340                         }
 341                         break;
 342                 }
 343         }
 344         if (ext_start == name - 1) {
 345                 sz = len;
 346                 ext_start = NULL;
 347         } else if (ext_start) {
 348                 /*
 349                  * Names which start with a dot could be just
 350                  * an extension eg. "...test".  In this case Win95
 351                  * uses the extension as the name and sets no extension.
 352                  */
 353                 name_start = &name[0];
 354                 while (name_start < ext_start)
 355                 {
 356                         if (!strchr(skip_chars,*name_start)) break;
 357                         name_start++;
 358                 }
 359                 if (name_start != ext_start) {
 360                         sz = ext_start - name;
 361                         ext_start++;
 362                 } else {
 363                         sz = len;
 364                         ext_start=NULL;
 365                 }
 366         }
 367 
 368         for (baselen = i = 0, p = base, ip = name; i < sz && baselen < 8; i++)
 369         {
 370                 if (!strchr(skip_chars, *ip)) {
 371                         if (*ip >= 'A' && *ip <= 'Z') {
 372                                 *p = *ip + 32;
 373                         } else {
 374                                 *p = *ip;
 375                         }
 376                         p++; baselen++;
 377                 }
 378                 ip++;
 379         }
 380         if (baselen == 0) {
 381                 return -EINVAL;
 382         }
 383                 
 384         spaces = 8 - baselen;
 385 
 386         if (ext_start) {
 387                 extlen = 0;
 388                 for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
 389                         if (!strchr(skip_chars, *ip)) {
 390                                 if (*ip >= 'A' && *ip <= 'Z') {
 391                                         *p = *ip + 32;
 392                                 } else {
 393                                         *p = *ip;
 394                                 }
 395                                 if (strchr(replace_chars, *p)) *p='_';
 396                                 extlen++;
 397                                 p++;
 398                         }
 399                 }
 400         } else {
 401                 extlen = 0;
 402         }
 403         ext[extlen] = '\0';
 404 
 405         count = 0;
 406         strcpy(msdos_name, base);
 407         msdos_name[baselen] = '.';
 408         strcpy(&msdos_name[baselen+1], ext);
 409 
 410         totlen = baselen + extlen + 1;
 411         res = 0;
 412         while (res > -1) {
 413                 /* Create the next shortname to try */
 414                 count++;
 415                 if (count == 10000000) return -EEXIST;
 416                 sprintf(buf, "%d", count);
 417                 sz = strlen(buf);
 418                 if (sz + 1 > spaces) {
 419                         baselen = baselen - (sz + 1 - spaces);
 420                         spaces = sz + 1;
 421                 }
 422 
 423                 strncpy(msdos_name, base, baselen);
 424                 msdos_name[baselen] = '~';
 425                 strcpy(&msdos_name[baselen+1], buf);
 426                 msdos_name[baselen+sz+1] = '.';
 427                 strcpy(&msdos_name[baselen+sz+2], ext);
 428 
 429                 totlen = baselen + sz + 2 + extlen;
 430                 res = vfat_find(dir, msdos_name, totlen, 0, 0, 0, &sinfo);
 431         }
 432         res = vfat_format_name('x', msdos_name, totlen, name_res, 1);
 433         return res;
 434 }
 435 
 436 static loff_t vfat_find_free_slots(struct inode *dir,int slots)
     /* [previous][next][first][last][top][bottom][index][help] */
 437 {
 438         struct super_block *sb = dir->i_sb;
 439         loff_t offset, curr;
 440         struct msdos_dir_entry *de;
 441         struct buffer_head *bh;
 442         struct inode *inode;
 443         int ino;
 444         int row;
 445         int done;
 446         int res;
 447         int added;
 448 
 449         PRINTK(("vfat_find_free_slots: find %d free slots\n", slots));
 450         offset = curr = 0;
 451         bh = NULL;
 452         row = 0;
 453         ino = fat_get_entry(dir,&curr,&bh,&de);
 454 
 455         for (added = 0; added < 2; added++) {
 456                 while (ino > -1) {
 457                         done = IS_FREE(de->name);
 458                         if (done) {
 459                                 inode = iget(sb,ino);
 460                                 if (inode) {
 461                                         /* Directory slots of busy deleted files aren't available yet. */
 462                                         done = !MSDOS_I(inode)->i_busy;
 463                                         /* PRINTK(("inode %d still busy\n", ino)); */
 464                                 }
 465                                 iput(inode);
 466                         }
 467                         if (done) {
 468                                 row++;
 469                                 if (row == slots) {
 470                                         brelse(bh);
 471                                         /* printk("----- Free offset at %d\n", offset); */
 472                                         return offset;
 473                                 }
 474                         } else {
 475                                 row = 0;
 476                                 offset = curr;
 477                         }
 478                         ino = fat_get_entry(dir,&curr,&bh,&de);
 479                 }
 480 
 481                 if (dir->i_ino == MSDOS_ROOT_INO) return -ENOSPC;
 482                 if ((res = fat_add_cluster(dir)) < 0) return res;
 483                 ino = fat_get_entry(dir,&curr,&bh,&de);
 484         }
 485         /* Should never get here, but if it does */
 486         printk("vfat_find_free_slots: Unable to find any\n");
 487         return -ENOSPC;
 488 }
 489                 
 490 static int vfat_build_slots(struct inode *dir,const char *name,int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 491      int find_long, int new_filename,
 492      struct msdos_dir_slot *ds, struct msdos_dir_slot *ds_mask,
 493      int *slots, int *is_long)
 494 {
 495         struct msdos_dir_slot *ps, *ps_mask;
 496         struct msdos_dir_entry *de, *de_mask;
 497         char msdos_name[MSDOS_NAME];
 498         int res;
 499         int slot;
 500         int i;
 501         const unsigned char *ip;
 502         loff_t offset;
 503         unsigned char alias_checksum;
 504 
 505         PRINTK(("Entering vfat_build_slots: name=%s, len=%d\n", name, len));
 506         de = (struct msdos_dir_entry *) ds;
 507         de_mask = (struct msdos_dir_entry *) ds_mask;
 508 
 509         *slots = 1;
 510         *is_long = 0;
 511         memset(ds_mask, 0, sizeof(struct msdos_dir_slot) * MSDOS_SLOTS);
 512         if (len == 1 && name[0] == '.') {
 513                 strncpy(de->name, MSDOS_DOT, MSDOS_NAME);
 514                 memset(de_mask, 0xff, MSDOS_NAME);
 515         } else if (len == 2 && name[0] == '.' && name[1] == '.') {
 516                 strncpy(de->name, MSDOS_DOT, MSDOS_NAME);
 517                 memset(de_mask, 0xff, MSDOS_NAME);
 518         } else {
 519                 PRINTK(("vfat_build_slots 4\n"));
 520                 res = vfat_valid_shortname('x', name, len, 1);
 521                 if (res > -1) {
 522                         PRINTK(("vfat_build_slots 5a\n"));
 523                         res = vfat_format_name('x', name, len, de->name, 1);
 524                         PRINTK(("vfat_build_slots 5b\n"));
 525                         memset(de_mask->name, 0xff, MSDOS_NAME);
 526                         PRINTK(("vfat_build_slots 5c\n"));
 527                 } else {
 528                         PRINTK(("vfat_build_slots 5A: %s (len=%d) is an invalid shortname\n", name, len));
 529                         if (new_filename) {
 530                                 unsigned char sum;
 531 
 532                                 PRINTK(("vfat_build_slots 5Z\n"));
 533                                 res = vfat_create_shortname(dir, name, len, msdos_name);
 534                                 PRINTK(("vfat_build_slots 5Y\n"));
 535                                 if (res < 0) {
 536                                         return res;
 537                                 }
 538 
 539                                 for (sum = i = 0; i < 11; i++) {
 540                                         sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + msdos_name[i];
 541                                 }
 542                                 PRINTK(("vfat_build_slots 5X: msdos_name=%s\n", msdos_name));
 543                                 alias_checksum = sum;
 544                         } else {
 545                                 alias_checksum = 0;
 546                         }
 547 
 548                         if (!find_long) return -EINVAL;
 549                         res = vfat_valid_longname(name, len, 1);
 550                         if (res < 0) return res;
 551 
 552                         *is_long = 1;
 553                         *slots = (len + 12) / 13;
 554                         PRINTK(("vfat_build_slots 6: slots=%d\n",*slots));
 555 
 556                         for (ps = ds, slot = *slots, ps_mask = ds_mask;
 557                              slot > 0; slot--, ps++, ps_mask++)
 558                         {
 559                                 int end, j;
 560 
 561                                 PRINTK(("vfat_build_slots 6a\n"));
 562                                 ps->id = slot; ps_mask->id = 0xff;
 563                                 ps->attr = ATTR_EXT; ps_mask->attr = 0xff;
 564                                 ps->reserved = 0; ps_mask->reserved = 0xff;
 565                                 ps->alias_checksum = alias_checksum;
 566                                 ps_mask->alias_checksum = 0;
 567                                 ps->start[0] = 0; ps_mask->start[0] = 0xff;
 568                                 ps->start[1] = 0; ps_mask->start[1] = 0xff;
 569                                 PRINTK(("vfat_build_slots 6b: name=%s\n",name));
 570                                 offset = (slot - 1) * 13;
 571                                 ip = &name[offset];
 572                                 j = offset;
 573                                 end = 0;
 574                                 for (i = 0; i < 10; i += 2) {
 575                                         if (!end && j == len) {
 576                                                 end = 1;
 577                                                 ps->name0_4[i] = 0;
 578                                                 ps_mask->name0_4[i] = 0xff;
 579                                                 ps->name0_4[i+1] = 0;
 580                                                 ps_mask->name0_4[i] = 0xff;
 581                                                 continue;
 582                                         } else if (end) {
 583                                                 ps->name0_4[i] = 0xff;
 584                                                 ps_mask->name0_4[i] = 0xff;
 585                                                 ps->name0_4[i+1] = 0xff;
 586                                                 ps_mask->name0_4[i+1] = 0xff;
 587                                                 continue;
 588                                         }
 589                                         ps->name0_4[i] = fat_a2uni[*ip].uni1;
 590                                         ps->name0_4[i+1] = fat_a2uni[*ip].uni2;
 591                                         if ((*ip >= 'a' && *ip <= 'z') ||
 592                                             (*ip >= 'A' && *ip <= 'Z')) {
 593                                                 ps_mask->name0_4[i] = 0xdf;
 594                                         } else {
 595                                                 ps_mask->name0_4[i] = 0xff;
 596                                         }
 597                                         ps_mask->name0_4[i+1] = 0xff;
 598                                         j++; ip++;
 599                                 }
 600                                 PRINTK(("vfat_build_slots 6c\n"));
 601                                 for (i = 0; i < 12; i += 2) {
 602                                         if (!end && j == len) {
 603                                                 end = 1;
 604                                                 ps->name5_10[i] = 0;
 605                                                 ps->name5_10[i+1] = 0;
 606                                                 continue;
 607                                         } else if (end) {
 608                                                 ps->name5_10[i] = 0xff;
 609                                                 ps_mask->name5_10[i] = 0xff;
 610                                                 ps->name5_10[i+1] = 0xff;
 611                                                 ps_mask->name5_10[i+1] = 0xff;
 612                                                 continue;
 613                                         }
 614                                         ps->name5_10[i] = fat_a2uni[*ip].uni1;
 615                                         ps->name5_10[i+1] = fat_a2uni[*ip].uni2;
 616                                         if ((*ip >= 'a' && *ip <= 'z') ||
 617                                             (*ip >= 'A' && *ip <= 'Z')) {
 618                                                 ps_mask->name5_10[i] = 0xdf;
 619                                         } else {
 620                                                 ps_mask->name5_10[i] = 0xff;
 621                                         }
 622                                         ps_mask->name5_10[i+1] = 0xff;
 623                                         j++; ip++;
 624                                 }
 625                                 PRINTK(("vfat_build_slots 6d\n"));
 626                                 for (i = 0; i < 4; i += 2) {
 627                                         if (!end && j == len) {
 628                                                 end = 1;
 629                                                 ps->name11_12[i] = 0;
 630                                                 ps->name11_12[i+1] = 0;
 631                                                 continue;
 632                                         } else if (end) {
 633                                                 ps->name11_12[i] = 0xff;
 634                                                 ps_mask->name11_12[i] = 0xff;
 635                                                 ps->name11_12[i+1] = 0xff;
 636                                                 ps_mask->name11_12[i+1] = 0xff;
 637                                                 continue;
 638                                         }
 639                                         ps->name11_12[i] = fat_a2uni[*ip].uni1;
 640                                         ps->name11_12[i+1] = fat_a2uni[*ip].uni2;
 641                                         if ((*ip >= 'a' && *ip <= 'z') ||
 642                                             (*ip >= 'A' && *ip <= 'Z')) {
 643                                                 ps_mask->name11_12[i] = 0xdf;
 644                                         } else {
 645                                                 ps_mask->name11_12[i] = 0xff;
 646                                         }
 647                                         ps_mask->name11_12[i+1] = 0xff;
 648                                         j++; ip++;
 649                                 }
 650                         }
 651                         PRINTK(("vfat_build_slots 6e\n"));
 652                         ds[0].id |= 0x40;
 653 
 654                         if (new_filename) {
 655                                 de = (struct msdos_dir_entry *) ps;
 656                                 de_mask = (struct msdos_dir_entry *) ps_mask;
 657 
 658                                 PRINTK(("vfat_build_slots 10\n"));
 659                                 strncpy(de->name, msdos_name, MSDOS_NAME);
 660                                 memset(de_mask->name, 0xff, MSDOS_NAME);
 661                         }
 662                 }
 663         }
 664         return 0;
 665 }
 666 
 667 /* Given a shortname offset, see if there is an associated longname.  Returns
 668  * the number of slots in the longname if one is found, else 0 */
 669 static int vfat_get_longname(struct inode *dir,loff_t short_offset,
     /* [previous][next][first][last][top][bottom][index][help] */
 670      unsigned char checksum, loff_t *pos_out)
 671 {
 672         struct super_block *sb = dir->i_sb;
 673         struct msdos_dir_slot *ps;
 674         struct msdos_dir_entry *de;
 675         struct buffer_head *bh;
 676         loff_t offset, temp;
 677         int id, res, slots;
 678 
 679         /* printk("Short offset: %d\n", short_offset); */
 680         if (short_offset == 0) {
 681                 return 0;
 682         }
 683 
 684         slots = 0;
 685         id = 1;
 686         bh = NULL;
 687         offset = short_offset - sizeof(struct msdos_dir_slot);
 688         while (offset > 0) {
 689                 temp = offset;
 690                 res = fat_get_entry(dir,&temp,&bh,&de);
 691                 if (res < 0) goto finish;
 692                 ps = (struct msdos_dir_slot *) de;
 693                 if (ps->alias_checksum != checksum) goto finish;
 694                 if ((ps->id &~ 0x40) != id) goto finish;
 695                 if (IS_FREE(de->name)) goto finish;
 696                 if (ps->id & 0x40) {
 697                         *pos_out = offset;
 698                         slots = ps->id &~ 0x40;
 699                         /* printk("Found a longname for the shortname: long_offset=%ld\n", offset); */
 700                         goto finish;
 701                 }
 702                 offset -= sizeof(struct msdos_dir_slot);
 703                 id++;
 704         }
 705  finish:
 706         if (bh) brelse(bh);
 707         return slots;
 708 }
 709 
 710 static int vfat_find(struct inode *dir,const char *name,int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 711     int find_long, int new_filename,int is_dir,struct slot_info *sinfo_out)
 712 {
 713         struct super_block *sb = dir->i_sb;
 714         const char *ip;
 715         char *op, *op_mask;
 716         int res;
 717         struct msdos_dir_slot ds[MSDOS_SLOTS], ds_mask[MSDOS_SLOTS];
 718         struct msdos_dir_slot *ps;
 719         struct msdos_dir_entry *de, *de_mask;
 720         struct buffer_head *bh;
 721         int i, slot, slots;
 722         loff_t offset, start;
 723         int match;
 724         int is_long;
 725         unsigned char alias_checksum = 0;
 726 
 727         PRINTK(("vfat_find 1: name=%s, len=%d\n",name,len));
 728 
 729         res = vfat_build_slots(dir, name, len, find_long, new_filename,
 730                                 ds, ds_mask, &slots, &is_long);
 731         if (res < 0) return res;
 732 
 733         de = (struct msdos_dir_entry *) ds;
 734         de_mask = (struct msdos_dir_entry *) ds_mask;
 735 
 736         PRINTK(("vfat_find 7\n"));
 737         offset = start = 0;
 738         bh = NULL;
 739         sinfo_out->ino = fat_get_entry(dir,&offset,&bh,&de);
 740         while (sinfo_out->ino > -1 && slots > 0) {
 741                 match = 1;
 742 
 743                 ps = (struct msdos_dir_slot *) de;
 744                 alias_checksum = ps->alias_checksum;
 745 
 746                 for (slot = 0; slot < slots; slot++) {
 747                         ip = (char *) de;
 748                         ps = (struct msdos_dir_slot *) de;
 749                         if (is_long && ps->alias_checksum != alias_checksum) {
 750                                 printk("Checksums don't match 1\n");
 751                                 match = 0;
 752                                 start = offset;
 753                                 break;
 754                         }
 755 
 756                         for (i = 0, ip = (char *) de, op = (char *) &ds[slot], op_mask = (char *) &ds_mask[slot];
 757                              i < sizeof(struct msdos_dir_entry);
 758                              i++, ip++, op++, op_mask++)
 759                         {
 760 #if 0
 761                                 if (is_long && de->attr == ATTR_EXT)
 762                                         printk("%02x?%02x ",
 763                                                (unsigned char) *ip,
 764                                                (unsigned char) *op);
 765 #endif
 766                                 if ((*ip & *op_mask) != (*op & *op_mask)) {
 767                                         start = offset;
 768                                         match = 0;
 769                                         break;
 770                                 }
 771                         }
 772 #if 0
 773                         if (is_long && de->attr == ATTR_EXT) printk("\n");
 774 #endif
 775                         if ((!is_long && !match) ||
 776                             (de->attr == ATTR_VOLUME) ||
 777                             (is_long && (match || slot == 0)))
 778                         {
 779                                 sinfo_out->ino = fat_get_entry(dir,&offset,&bh,&de);
 780                                 /* if (ino >=0 && de->attr == ATTR_EXT) dump_de(de); */
 781                         }
 782                         if (!match) {
 783                                 break;
 784                         }
 785                         if (sinfo_out->ino == -1) {
 786                                 match = 0;
 787                                 goto breakout;
 788                         }
 789                 }
 790                 if (match) {
 791                         unsigned char sum;
 792 
 793                         for (sum = i = 0; i < 11; i++) {
 794                                 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
 795                         }
 796 
 797                         if (is_long) {
 798                                 if (sum != alias_checksum) {
 799                                         PRINTK(("Checksums don't match %d != %d\n", sum, alias_checksum));
 800                                         match = 0;
 801                                 }
 802                         } else {
 803                                 int long_slots;
 804                                 long_slots = vfat_get_longname(dir, offset - sizeof(struct msdos_dir_entry), sum, &start);
 805                                 if (long_slots > 0) {
 806                                         slots = long_slots;
 807                                         is_long = 1;
 808                                 }
 809                         }
 810 
 811                                 
 812                         if (match) {
 813                                 PRINTK(("name: %s, alias: %c%c%c%c%c%c%c%c%c%c%c\n",
 814                                         name,
 815                                         de->name[0], de->name[1], de->name[2],
 816                                         de->name[3], de->name[4], de->name[5],
 817                                         de->name[6], de->name[7], de->name[8],
 818                                         de->name[9], de->name[10]));
 819                                 PRINTK(("vfat_find 10\n"));
 820                                 res = CF_LE_W(de->start);
 821 
 822                                 sinfo_out->shortname_offset = offset - sizeof(struct msdos_dir_slot);
 823                                 sinfo_out->longname_offset = start;
 824                                 sinfo_out->is_long = is_long;
 825                                 if (is_long) {
 826                                         sinfo_out->long_slots = slots;
 827                                         slots++;
 828                                 } else {
 829                                         sinfo_out->long_slots = 0;
 830                                 }
 831 
 832                                 sinfo_out->total_slots = slots;
 833                                 if (new_filename) {
 834                                         if (bh) brelse(bh);
 835                                         return -EEXIST;
 836                                 }
 837                                 if (bh) brelse(bh);
 838                                 return res;
 839                         }
 840                 }
 841         }
 842  breakout:
 843         PRINTK(("breakout\n"));
 844 
 845         if (bh) brelse(bh);
 846         if (new_filename) {
 847                 PRINTK(("vfat_find: create file 1\n"));
 848                 if (is_long) slots++;
 849                 offset = vfat_find_free_slots(dir, slots);
 850                 if (offset < 0) {
 851                         return offset;
 852                 }
 853 
 854                 PRINTK(("vfat_find: create file 2\n"));
 855                 /* Now create the new entry */
 856                 bh = NULL;
 857                 for (slot = 0, ps = ds; slot < slots; slot++, ps++) {
 858                         PRINTK(("vfat_find: create file 3, slot=%d\n",slot));
 859                         sinfo_out->ino = fat_get_entry(dir,&offset,&bh,&de);
 860                         if (sinfo_out->ino < 0) {
 861                                 PRINTK(("vfat_find: problem\n"));
 862                                 return sinfo_out->ino;
 863                         }
 864                         memcpy(de, ps, sizeof(struct msdos_dir_slot));
 865                         mark_buffer_dirty(bh, 1);
 866                 }
 867 
 868                 PRINTK(("vfat_find: create file 4\n"));
 869                 dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
 870                 dir->i_dirt = 1;
 871 
 872                 PRINTK(("vfat_find: create file 5\n"));
 873 
 874                 memset(de->unused, 0, sizeof(de->unused));
 875                 fat_date_unix2dos(dir->i_mtime,&de->time,&de->date);
 876                 de->ctime_ms = 0;
 877                 de->ctime = de->time;
 878                 de->adate = de->cdate = de->date;
 879                 de->start = 0;
 880                 de->size = 0;
 881                 de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
 882                 de->lcase = CASE_LOWER_BASE | CASE_LOWER_EXT;
 883 
 884 
 885                 mark_buffer_dirty(bh, 1);
 886                 brelse(bh);
 887 
 888                 sinfo_out->is_long = (slots > 1) ? 1 : 0;
 889                 if (sinfo_out->is_long) {
 890                         sinfo_out->long_slots = slots - 1;
 891                 } else {
 892                         sinfo_out->long_slots = 0;
 893                 }
 894                 sinfo_out->total_slots = slots;
 895                 sinfo_out->shortname_offset = offset - sizeof(struct msdos_dir_slot);
 896                 sinfo_out->longname_offset = offset - sizeof(struct msdos_dir_slot) * slots;
 897                 return 0;
 898         }
 899 
 900         return -ENOENT;
 901 }
 902 
 903 int vfat_lookup(struct inode *dir,const char *name,int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 904     struct inode **result)
 905 {
 906         int res, ino;
 907         struct inode *next;
 908         struct slot_info sinfo;
 909         
 910         PRINTK (("vfat_lookup: name=%s, len=%d\n", name, len));
 911 
 912         *result = NULL;
 913         if (!dir) return -ENOENT;
 914         if (!S_ISDIR(dir->i_mode)) {
 915                 iput(dir);
 916                 return -ENOENT;
 917         }
 918         PRINTK (("vfat_lookup 2\n"));
 919         if (len == 1 && name[0] == '.') {
 920                 *result = dir;
 921                 return 0;
 922         }
 923         if (len == 2 && name[0] == '.' && name[1] == '.') {
 924                 ino = fat_parent_ino(dir,0);
 925                 iput(dir);
 926                 if (ino < 0) return ino;
 927                 if (!(*result = iget(dir->i_sb,ino))) return -EACCES;
 928                 return 0;
 929         }
 930         PRINTK (("vfat_lookup 3\n"));
 931         if ((res = vfat_find(dir,name,len,1,0,0,&sinfo)) < 0) {
 932                 iput(dir);
 933                 return res;
 934         }
 935         PRINTK (("vfat_lookup 4.5\n"));
 936         if (!(*result = iget(dir->i_sb,sinfo.ino))) {
 937                 iput(dir);
 938                 return -EACCES;
 939         }
 940         PRINTK (("vfat_lookup 5\n"));
 941         if (!(*result)->i_sb ||
 942             ((*result)->i_sb->s_magic != MSDOS_SUPER_MAGIC)) {
 943                 /* crossed a mount point into a non-msdos fs */
 944                 iput(dir);
 945                 return 0;
 946         }
 947         if (MSDOS_I(*result)->i_busy) { /* mkdir in progress */
 948                 iput(*result);
 949                 iput(dir);
 950                 return -ENOENT;
 951         }
 952         PRINTK (("vfat_lookup 6\n"));
 953         while (MSDOS_I(*result)->i_old) {
 954                 next = MSDOS_I(*result)->i_old;
 955                 iput(*result);
 956                 if (!(*result = iget(next->i_sb,next->i_ino))) {
 957                         fat_fs_panic(dir->i_sb,"vfat_lookup: Can't happen");
 958                         iput(dir);
 959                         return -ENOENT;
 960                 }
 961         }
 962         iput(dir);
 963         return 0;
 964 }
 965 
 966 
 967 static int vfat_create_entry(struct inode *dir,const char *name,int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 968     int is_dir, struct inode **result)
 969 {
 970         struct super_block *sb = dir->i_sb;
 971         int res,ino;
 972         loff_t offset;
 973         struct buffer_head *bh;
 974         struct msdos_dir_entry *de;
 975         struct slot_info sinfo;
 976 
 977         PRINTK(("vfat_create_entry 1\n"));
 978         res = vfat_find(dir, name, len, 1, 1, is_dir, &sinfo);
 979         if (res < 0) {
 980                 return res;
 981         }
 982 
 983         offset = sinfo.shortname_offset;
 984 
 985         PRINTK(("vfat_create_entry 2\n"));
 986         bh = NULL;
 987         ino = fat_get_entry(dir, &offset, &bh, &de);
 988         if (ino < 0) {
 989                 PRINTK(("vfat_mkdir problem\n"));
 990                 if (bh) brelse(bh);
 991                 return ino;
 992         }
 993         PRINTK(("vfat_create_entry 3\n"));
 994 
 995         if ((*result = iget(dir->i_sb,ino)) != NULL)
 996                 vfat_read_inode(*result);
 997         brelse(bh);
 998         if (!*result) return -EIO;
 999         (*result)->i_mtime = (*result)->i_atime = (*result)->i_ctime =
1000             CURRENT_TIME;
1001         (*result)->i_dirt = 1;
1002 
1003         return 0;
1004 }
1005 
1006 int vfat_create(struct inode *dir,const char *name,int len,int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
1007         struct inode **result)
1008 {
1009         int res;
1010 
1011         if (!dir) return -ENOENT;
1012 
1013         fat_lock_creation();
1014         if ((res = vfat_create_entry(dir,name,len,0,result)) < 0) {
1015                 printk("vfat_create: unable to get new entry\n");
1016                 fat_unlock_creation();
1017                 iput(dir);
1018                 return res;
1019         }
1020 
1021         fat_unlock_creation();
1022         iput(dir);
1023         return res;
1024 }
1025 
1026 static int vfat_create_a_dotdir(struct inode *dir,struct inode *parent,
     /* [previous][next][first][last][top][bottom][index][help] */
1027      struct buffer_head *bh,
1028      struct msdos_dir_entry *de,int ino,const char *name, int isdot)
1029 {
1030         struct super_block *sb = dir->i_sb;
1031         struct inode *dot;
1032 
1033         PRINTK(("vfat_create_a_dotdir 1\n"));
1034 
1035         /*
1036          * XXX all times should be set by caller upon successful completion.
1037          */
1038         dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1039         dir->i_dirt = 1;
1040         memcpy(de->name,name,MSDOS_NAME);
1041         memset(de->unused, 0, sizeof(de->unused));
1042         de->lcase = 0;
1043         de->attr = ATTR_DIR;
1044         de->start = 0;
1045         fat_date_unix2dos(dir->i_mtime,&de->time,&de->date);
1046         de->ctime_ms = 0;
1047         de->ctime = de->time;
1048         de->adate = de->cdate = de->date;
1049         de->size = 0;
1050         mark_buffer_dirty(bh, 1);
1051         if ((dot = iget(dir->i_sb,ino)) != NULL)
1052                 vfat_read_inode(dot);
1053         if (!dot) return -EIO;
1054         dot->i_mtime = dot->i_atime = CURRENT_TIME;
1055         dot->i_dirt = 1;
1056         if (isdot) {
1057                 dot->i_size = dir->i_size;
1058                 MSDOS_I(dot)->i_start = MSDOS_I(dir)->i_start;
1059                 dot->i_nlink = dir->i_nlink;
1060         } else {
1061                 dot->i_size = parent->i_size;
1062                 MSDOS_I(dot)->i_start = MSDOS_I(parent)->i_start;
1063                 dot->i_nlink = parent->i_nlink;
1064         }
1065 
1066         iput(dot);
1067 
1068         PRINTK(("vfat_create_a_dotdir 2\n"));
1069         return 0;
1070 }
1071 
1072 static int vfat_create_dotdirs(struct inode *dir, struct inode *parent)
     /* [previous][next][first][last][top][bottom][index][help] */
1073 {
1074         struct super_block *sb = dir->i_sb;
1075         int res;
1076         struct buffer_head *bh;
1077         struct msdos_dir_entry *de;
1078         loff_t offset;
1079 
1080         PRINTK(("vfat_create_dotdirs 1\n"));
1081         if ((res = fat_add_cluster(dir)) < 0) return res;
1082 
1083         PRINTK(("vfat_create_dotdirs 2\n"));
1084         offset = 0;
1085         bh = NULL;
1086         if ((res = fat_get_entry(dir,&offset,&bh,&de)) < 0) return res;
1087         
1088         PRINTK(("vfat_create_dotdirs 3\n"));
1089         res = vfat_create_a_dotdir(dir, parent, bh, de, res, MSDOS_DOT, 1);
1090         PRINTK(("vfat_create_dotdirs 4\n"));
1091         if (res < 0) {
1092                 brelse(bh);
1093                 return res;
1094         }
1095         PRINTK(("vfat_create_dotdirs 5\n"));
1096 
1097         if ((res = fat_get_entry(dir,&offset,&bh,&de)) < 0) {
1098                 brelse(bh);
1099                 return res;
1100         }
1101         PRINTK(("vfat_create_dotdirs 6\n"));
1102 
1103         res = vfat_create_a_dotdir(dir, parent, bh, de, res, MSDOS_DOTDOT, 0);
1104         PRINTK(("vfat_create_dotdirs 7\n"));
1105         brelse(bh);
1106 
1107         return res;
1108 }
1109 
1110 /***** See if directory is empty */
1111 static int vfat_empty(struct inode *dir)
     /* [previous][next][first][last][top][bottom][index][help] */
1112 {
1113         struct super_block *sb = dir->i_sb;
1114         loff_t pos;
1115         struct buffer_head *bh;
1116         struct msdos_dir_entry *de;
1117 
1118         if (dir->i_count > 1)
1119                 return -EBUSY;
1120         if (MSDOS_I(dir)->i_start) { /* may be zero in mkdir */
1121                 pos = 0;
1122                 bh = NULL;
1123                 while (fat_get_entry(dir,&pos,&bh,&de) > -1)
1124                         /* Skip extended filename entries */
1125                         if (de->attr == ATTR_EXT) continue;
1126 
1127                         if (!IS_FREE(de->name) && strncmp(de->name,MSDOS_DOT,
1128                             MSDOS_NAME) && strncmp(de->name,MSDOS_DOTDOT,
1129                             MSDOS_NAME)) {
1130                                 brelse(bh);
1131                                 return -ENOTEMPTY;
1132                         }
1133                 if (bh)
1134                         brelse(bh);
1135         }
1136         return 0;
1137 }
1138 
1139 static int vfat_rmdir_free_ino(struct inode *dir,struct buffer_head *bh,
     /* [previous][next][first][last][top][bottom][index][help] */
1140      struct msdos_dir_entry *de,int ino)
1141 {
1142         struct super_block *sb = dir->i_sb;
1143         struct inode *inode;
1144         int res;
1145 
1146         if (ino < 0) return -EINVAL;
1147         if (!(inode = iget(dir->i_sb,ino))) return -ENOENT;
1148         if (!S_ISDIR(inode->i_mode)) {
1149                 iput(inode);
1150                 return -ENOTDIR;
1151         }
1152         if (dir->i_dev != inode->i_dev || dir == inode) {
1153                 iput(inode);
1154                 return -EBUSY;
1155         }
1156         res = vfat_empty(inode);
1157         if (res) {
1158                 iput(inode);
1159                 return res;
1160         }
1161         inode->i_nlink = 0;
1162         inode->i_mtime = dir->i_mtime = CURRENT_TIME;
1163         inode->i_atime = dir->i_atime = CURRENT_TIME;
1164         dir->i_nlink--;
1165         inode->i_dirt = dir->i_dirt = 1;
1166         de->name[0] = DELETED_FLAG;
1167         mark_buffer_dirty(bh, 1);
1168         iput(inode);
1169 
1170         return 0;
1171 }
1172 
1173 static int vfat_unlink_free_ino(struct inode *dir,struct buffer_head *bh,
     /* [previous][next][first][last][top][bottom][index][help] */
1174      struct msdos_dir_entry *de,int ino,int nospc)
1175 {
1176         struct super_block *sb = dir->i_sb;
1177         struct inode *inode;
1178         if (!(inode = iget(dir->i_sb,ino))) return -ENOENT;
1179         if (!S_ISREG(inode->i_mode) && nospc) {
1180                 iput(inode);
1181                 return -EPERM;
1182         }
1183         if (IS_IMMUTABLE(inode)){
1184                 iput(inode);
1185                 return -EPERM;
1186         }
1187         inode->i_nlink = 0;
1188         inode->i_mtime = dir->i_mtime = CURRENT_TIME;
1189         inode->i_atime = dir->i_atime = CURRENT_TIME;
1190         MSDOS_I(inode)->i_busy = 1;
1191         inode->i_dirt = dir->i_dirt = 1;
1192         de->name[0] = DELETED_FLAG;
1193         mark_buffer_dirty(bh, 1);
1194 
1195         iput(inode);
1196         return 0;
1197 }
1198 
1199 static int vfat_remove_entry(struct inode *dir,struct slot_info *sinfo,
     /* [previous][next][first][last][top][bottom][index][help] */
1200      struct buffer_head **bh,struct msdos_dir_entry **de,
1201      int is_dir,int nospc)
1202 {
1203         struct super_block *sb = dir->i_sb;
1204         loff_t offset;
1205         int res, i;
1206 
1207         /* remove the shortname */
1208         offset = sinfo->shortname_offset;
1209         res = fat_get_entry(dir, &offset, bh, de);
1210         if (res < 0) return res;
1211         if (is_dir) {
1212                 res = vfat_rmdir_free_ino(dir,*bh,*de,res);
1213         } else {
1214                 res = vfat_unlink_free_ino(dir,*bh,*de,res,nospc);
1215         }
1216         if (res < 0) return res;
1217                 
1218         /* remove the longname */
1219         offset = sinfo->longname_offset;
1220         for (i = sinfo->long_slots; i > 0; --i) {
1221                 res = fat_get_entry(dir, &offset, bh, de);
1222                 if (res < 0) {
1223                         printk("vfat_remove_entry: problem 1\n");
1224                         continue;
1225                 }
1226                 (*de)->name[0] = DELETED_FLAG;
1227                 (*de)->attr = 0;
1228                 mark_buffer_dirty(*bh, 1);
1229         }
1230         return 0;
1231 }
1232 
1233 
1234 static int vfat_rmdirx(struct inode *dir,const char *name,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1235 {
1236         struct super_block *sb = dir->i_sb;
1237         int res;
1238         struct buffer_head *bh;
1239         struct msdos_dir_entry *de;
1240         struct slot_info sinfo;
1241 
1242         bh = NULL;
1243         res = -EPERM;
1244         if (name[0] == '.' && (len == 1 || (len == 2 && name[1] == '.')))
1245                 goto rmdir_done;
1246         res = vfat_find(dir,name,len,1,0,0,&sinfo);
1247 
1248         if (res >= 0 && sinfo.total_slots > 0) {
1249                 res = vfat_remove_entry(dir,&sinfo,&bh,&de,1,0);
1250                 if (res > 0) {
1251                         res = 0;
1252                 }
1253         } else {
1254                 printk("Problem in vfat_rmdirx\n");
1255         }
1256 
1257 rmdir_done:
1258         brelse(bh);
1259         return res;
1260 }
1261 
1262 /***** Remove a directory */
1263 int vfat_rmdir(struct inode *dir,const char *name,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1264 {
1265         int res;
1266 
1267         res = vfat_rmdirx(dir, name, len);
1268         iput(dir);
1269         return res;
1270 }
1271 
1272 static int vfat_unlinkx(
     /* [previous][next][first][last][top][bottom][index][help] */
1273         struct inode *dir,
1274         const char *name,
1275         int len,
1276         int nospc)      /* Flag special file ? */
1277 {
1278         struct super_block *sb = dir->i_sb;
1279         int res;
1280         struct buffer_head *bh;
1281         struct msdos_dir_entry *de;
1282         struct slot_info sinfo;
1283 
1284         bh = NULL;
1285         if ((res = vfat_find(dir,name,len,1,0,0,&sinfo)) < 0)
1286                 goto unlink_done;
1287 
1288         if (res >= 0 && sinfo.total_slots > 0) {
1289                 res = vfat_remove_entry(dir,&sinfo,&bh,&de,0,nospc);
1290                 if (res > 0) {
1291                         res = 0;
1292                 }
1293         } else {
1294                 printk("Problem in vfat_unlinkx: res=%d, total_slots=%d\n",res, sinfo.total_slots);
1295         }
1296 
1297 unlink_done:
1298         brelse(bh);
1299         return res;
1300 }
1301 
1302 
1303 int vfat_mkdir(struct inode *dir,const char *name,int len,int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1304 {
1305         struct inode *inode;
1306         int res;
1307 
1308         fat_lock_creation();
1309         if ((res = vfat_create_entry(dir,name,len,1,&inode)) < 0) {
1310                 fat_unlock_creation();
1311                 iput(dir);
1312                 return res;
1313         }
1314 
1315         dir->i_nlink++;
1316         inode->i_nlink = 2; /* no need to mark them dirty */
1317         MSDOS_I(inode)->i_busy = 1; /* prevent lookups */
1318 
1319         res = vfat_create_dotdirs(inode, dir);
1320         fat_unlock_creation();
1321         MSDOS_I(inode)->i_busy = 0;
1322         iput(inode);
1323         iput(dir);
1324         if (res < 0) {
1325                 if (vfat_rmdir(dir,name,len) < 0)
1326                         fat_fs_panic(dir->i_sb,"rmdir in mkdir failed");
1327         }
1328         return res;
1329 }
1330 
1331 /***** Unlink, as called for msdosfs */
1332 int vfat_unlink(struct inode *dir,const char *name,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1333 {
1334         int res;
1335 
1336         res = vfat_unlinkx (dir,name,len,1);
1337         iput(dir);
1338         return res;
1339 }
1340 
1341 
1342 int vfat_rename(struct inode *old_dir,const char *old_name,int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
1343         struct inode *new_dir,const char *new_name,int new_len)
1344 {
1345         struct super_block *sb = old_dir->i_sb;
1346         struct buffer_head *old_bh,*new_bh,*dotdot_bh;
1347         struct msdos_dir_entry *old_de,*new_de,*dotdot_de;
1348         loff_t old_offset,new_offset,old_longname_offset;
1349         int old_slots,old_ino,new_ino,dotdot_ino,ino;
1350         struct inode *old_inode, *new_inode, *dotdot_inode, *walk;
1351         int res, is_dir, i;
1352         int locked = 0;
1353         struct slot_info sinfo;
1354 
1355         PRINTK(("vfat_rename 1\n"));
1356         if (old_dir == new_dir && old_len == new_len &&
1357             strncmp(old_name, new_name, old_len) == 0)
1358                 return 0;
1359 
1360         old_bh = new_bh = NULL;
1361         old_inode = new_inode = NULL;
1362         res = vfat_find(old_dir,old_name,old_len,1,0,0,&sinfo);
1363         PRINTK(("vfat_rename 2\n"));
1364         if (res < 0) goto rename_done;
1365 
1366         old_slots = sinfo.total_slots;
1367         old_longname_offset = sinfo.longname_offset;
1368         old_offset = sinfo.shortname_offset;
1369         old_ino = sinfo.ino;
1370         res = fat_get_entry(old_dir, &old_offset, &old_bh, &old_de);
1371         PRINTK(("vfat_rename 3\n"));
1372         if (res < 0) goto rename_done;
1373 
1374         if (!(old_inode = iget(old_dir->i_sb,old_ino))) goto rename_done;
1375         is_dir = S_ISDIR(old_inode->i_mode);
1376         if (is_dir) {
1377                 if ((old_dir->i_dev != new_dir->i_dev) ||
1378                     (old_ino == new_dir->i_ino)) {
1379                         res = -EINVAL;
1380                         goto rename_done;
1381                 }
1382                 if (!(walk = iget(new_dir->i_sb,new_dir->i_ino))) return -EIO;
1383                 /* prevent moving directory below itself */
1384                 while (walk->i_ino != MSDOS_ROOT_INO) {
1385                         ino = fat_parent_ino(walk,1);
1386                         iput(walk);
1387                         if (ino < 0) return ino;
1388                         if (ino == old_ino) return -EINVAL;
1389                         if (!(walk = iget(new_dir->i_sb,ino))) return -EIO;
1390                 }
1391                 iput(walk);
1392         }
1393 
1394         res = vfat_find(new_dir,new_name,new_len,1,0,is_dir,&sinfo);
1395 
1396         PRINTK(("vfat_rename 4\n"));
1397         if (res > -1) {
1398                 int new_is_dir;
1399 
1400                 PRINTK(("vfat_rename 5\n"));
1401                 /* Filename currently exists.  Need to delete it */
1402                 new_offset = sinfo.shortname_offset;
1403                 res = fat_get_entry(new_dir, &new_offset, &new_bh, &new_de);
1404                 PRINTK(("vfat_rename 6\n"));
1405                 if (res < 0) goto rename_done;
1406 
1407                 if (!(new_inode = iget(new_dir->i_sb,res)))
1408                         goto rename_done;
1409                 new_is_dir = S_ISDIR(new_inode->i_mode);
1410                 iput(new_inode);
1411                 if (new_is_dir) {
1412                         PRINTK(("vfat_rename 7\n"));
1413                         res = vfat_rmdirx(new_dir,new_name,new_len);
1414                         PRINTK(("vfat_rename 8\n"));
1415                         if (res < 0) goto rename_done;
1416                 } else {
1417                         PRINTK(("vfat_rename 9\n"));
1418                         res = vfat_unlinkx(new_dir,new_name,new_len,1);
1419                         PRINTK(("vfat_rename 10\n"));
1420                         if (res < 0) goto rename_done;
1421                 }
1422         }
1423 
1424         PRINTK(("vfat_rename 11\n"));
1425         fat_lock_creation(); locked = 1;
1426         res = vfat_find(new_dir,new_name,new_len,1,1,is_dir,&sinfo);
1427 
1428         PRINTK(("vfat_rename 12\n"));
1429         if (res < 0) goto rename_done;
1430 
1431         new_offset = sinfo.shortname_offset;
1432         new_ino = sinfo.ino;
1433         res = fat_get_entry(new_dir, &new_offset, &new_bh, &new_de);
1434         PRINTK(("vfat_rename 13\n"));
1435         if (res < 0) goto rename_done;
1436 
1437         new_de->attr = old_de->attr;
1438         new_de->time = old_de->time;
1439         new_de->date = old_de->date;
1440         new_de->ctime_ms = old_de->ctime_ms;
1441         new_de->cdate = old_de->cdate;
1442         new_de->adate = old_de->adate;
1443         new_de->start = old_de->start;
1444         new_de->size = old_de->size;
1445 
1446         if (!(new_inode = iget(new_dir->i_sb,new_ino))) goto rename_done;
1447         PRINTK(("vfat_rename 14\n"));
1448 
1449         /* At this point, we have the inodes of the old file and the
1450          * new file.  We need to transfer all information from the old
1451          * inode to the new inode and then delete the slots of the old
1452          * entry
1453          */
1454 
1455         vfat_read_inode(new_inode);
1456         MSDOS_I(old_inode)->i_busy = 1;
1457         fat_cache_inval_inode(old_inode);
1458         PRINTK(("vfat_rename 15: old_slots=%d\n",old_slots));
1459         old_inode->i_dirt = 1;
1460 
1461         /* remove the old entry */
1462         for (i = old_slots; i > 0; --i) {
1463                 res = fat_get_entry(old_dir, &old_longname_offset, &old_bh, &old_de);
1464                 if (res < 0) {
1465                         printk("vfat_unlinkx: problem 1\n");
1466                         continue;
1467                 }
1468                 old_de->name[0] = DELETED_FLAG;
1469                 old_de->attr = 0;
1470                 mark_buffer_dirty(old_bh, 1);
1471         }
1472         PRINTK(("vfat_rename 15b\n"));
1473 
1474         mark_buffer_dirty(new_bh, 1);
1475         iput(new_inode);
1476         /* XXX: There is some code in the original MSDOS rename that
1477          * is not duplicated here and it might cause a problem in
1478          * certain circumstances.
1479          */
1480         
1481         if (S_ISDIR(old_inode->i_mode)) {
1482                 if ((res = fat_scan(old_inode,MSDOS_DOTDOT,&dotdot_bh,
1483                     &dotdot_de,&dotdot_ino,SCAN_ANY)) < 0) goto rename_done;
1484                 if (!(dotdot_inode = iget(old_inode->i_sb,dotdot_ino))) {
1485                         brelse(dotdot_bh);
1486                         res = -EIO;
1487                         goto rename_done;
1488                 }
1489                 dotdot_de->start = MSDOS_I(dotdot_inode)->i_start =
1490                     MSDOS_I(new_dir)->i_start;
1491                 dotdot_inode->i_dirt = 1;
1492                 mark_buffer_dirty(dotdot_bh, 1);
1493                 old_dir->i_nlink--;
1494                 new_dir->i_nlink++;
1495                 /* no need to mark them dirty */
1496                 dotdot_inode->i_nlink = new_dir->i_nlink;
1497                 iput(dotdot_inode);
1498                 brelse(dotdot_bh);
1499         }
1500 
1501         if (res > 0) res = 0;
1502 
1503 rename_done:
1504         if (locked) fat_unlock_creation();
1505         if (old_bh) brelse(old_bh);
1506         if (new_bh) brelse(new_bh);
1507         if (old_inode) iput(old_inode);
1508         iput(old_dir);
1509         iput(new_dir);
1510         return res;
1511 }
1512 
1513 
1514 
1515 /* Public inode operations for the VFAT fs */
1516 struct inode_operations vfat_dir_inode_operations = {
1517         &fat_dir_operations,    /* default directory file-ops */
1518         vfat_create,            /* create */
1519         vfat_lookup,            /* lookup */
1520         NULL,                   /* link */
1521         vfat_unlink,            /* unlink */
1522         NULL,                   /* symlink */
1523         vfat_mkdir,             /* mkdir */
1524         vfat_rmdir,             /* rmdir */
1525         NULL,                   /* mknod */
1526         vfat_rename,            /* rename */
1527         NULL,                   /* readlink */
1528         NULL,                   /* follow_link */
1529         NULL,                   /* readpage */
1530         NULL,                   /* writepage */
1531         fat_bmap,               /* bmap */
1532         NULL,                   /* truncate */
1533         NULL                    /* permission */
1534 };
1535 
1536 
1537 void vfat_read_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
1538 {
1539         fat_read_inode(inode, &vfat_dir_inode_operations);
1540 }
1541 
1542 
1543 
1544 
1545 static struct file_system_type vfat_fs_type = {
1546         vfat_read_super, "vfat", 1, NULL
1547 };
1548 
1549 int init_vfat_fs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1550 {
1551         int status;
1552 
1553         if ((status = register_filesystem(&vfat_fs_type)) == 0)
1554                 status = register_symtab(0);
1555         return status;
1556 }
1557 
1558 #ifdef MODULE
1559 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1560 {
1561         return init_vfat_fs();
1562 }
1563 
1564 
1565 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1566 {
1567         unregister_filesystem(&vfat_fs_type);
1568 }
1569 
1570 #endif /* ifdef MODULE */
1571 
1572 
1573 
1574 /*
1575  * Overrides for Emacs so that we follow Linus's tabbing style.
1576  * Emacs will notice this stuff at the end of the file and automatically
1577  * adjust the settings for this buffer only.  This must remain at the end
1578  * of the file.
1579  * ---------------------------------------------------------------------------
1580  * Local variables:
1581  * c-indent-level: 8
1582  * c-brace-imaginary-offset: 0
1583  * c-brace-offset: -8
1584  * c-argdecl-indent: 8
1585  * c-label-offset: -8
1586  * c-continued-statement-offset: 8
1587  * c-continued-brace-offset: 0
1588  * End:
1589  */

/* [previous][next][first][last][top][bottom][index][help] */