root/fs/ext/namei.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext_match
  2. ext_find_entry
  3. ext_lookup
  4. ext_add_entry
  5. ext_create
  6. ext_mknod
  7. ext_mkdir
  8. empty_dir
  9. ext_merge_entries
  10. ext_rmdir
  11. ext_unlink
  12. ext_symlink
  13. ext_link
  14. subdir
  15. do_ext_rename
  16. ext_rename

   1 /*
   2  *  linux/fs/ext/namei.c
   3  *
   4  *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
   5  *
   6  *  from
   7  *
   8  *  linux/fs/minix/namei.c
   9  *
  10  *  Copyright (C) 1991, 1992  Linus Torvalds
  11  */
  12 
  13 #include <linux/sched.h>
  14 #include <linux/ext_fs.h>
  15 #include <linux/kernel.h>
  16 #include <linux/string.h>
  17 #include <linux/stat.h>
  18 #include <linux/fcntl.h>
  19 #include <linux/errno.h>
  20 
  21 #include <asm/segment.h>
  22 
  23 /*
  24  * comment out this line if you want names > EXT_NAME_LEN chars to be
  25  * truncated. Else they will be disallowed.
  26  */
  27 /* #define NO_TRUNCATE */
  28 
  29 /*
  30  * EXT_DIR_PAD defines the directory entries boundaries
  31  *
  32  * NOTE: It must be a power of 2 and must be greater or equal than 8
  33  * because a directory entry needs 8 bytes for its fixed part
  34  * (4 bytes for the inode, 2 bytes for the entry length and 2 bytes
  35  * for the name length)
  36  */
  37 #define EXT_DIR_PAD 8
  38 
  39 /*
  40  *
  41  * EXT_DIR_MIN_SIZE is the minimal size of a directory entry
  42  *
  43  * During allocations, a directory entry is split into 2 ones
  44  * *ONLY* if the size of the unused part is greater than or 
  45  * equal to EXT_DIR_MIN_SIZE
  46  */
  47 #define EXT_DIR_MIN_SIZE 12
  48 
  49 /*
  50  * ok, we cannot use strncmp, as the name is not in our data space.
  51  * Thus we'll have to use ext_match. No big problem. Match also makes
  52  * some sanity tests.
  53  *
  54  * NOTE! unlike strncmp, ext_match returns 1 for success, 0 for failure.
  55  */
  56 static int ext_match(int len,const char * name,struct ext_dir_entry * de)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58         register int same __asm__("ax");
  59 
  60         if (!de || !de->inode || len > EXT_NAME_LEN)
  61                 return 0;
  62         /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
  63         if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
  64                 return 1;
  65         if (len < EXT_NAME_LEN && len != de->name_len)
  66                 return 0;
  67         __asm__("cld\n\t"
  68                 "repe ; cmpsb\n\t"
  69                 "setz %%al"
  70                 :"=a" (same)
  71                 :"0" (0),"S" ((long) name),"D" ((long) de->name),"c" (len)
  72                 :"cx","di","si");
  73         return same;
  74 }
  75 
  76 /*
  77  *      ext_find_entry()
  78  *
  79  * finds an entry in the specified directory with the wanted name. It
  80  * returns the cache buffer in which the entry was found, and the entry
  81  * itself (as a parameter - res_dir). It does NOT read the inode of the
  82  * entry - you'll have to do that yourself if you want to.
  83  *
  84  * addition for the ext file system : this function returns the previous
  85  * and next directory entries in the parameters prev_dir and next_dir
  86  */
  87 static struct buffer_head * ext_find_entry(struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
  88         const char * name, int namelen, struct ext_dir_entry ** res_dir,
  89         struct ext_dir_entry ** prev_dir, struct ext_dir_entry ** next_dir)
  90 {
  91         long offset;
  92         struct buffer_head * bh;
  93         struct ext_dir_entry * de;
  94 
  95         *res_dir = NULL;
  96         if (!dir)
  97                 return NULL;
  98 #ifdef NO_TRUNCATE
  99         if (namelen > EXT_NAME_LEN)
 100                 return NULL;
 101 #else
 102         if (namelen > EXT_NAME_LEN)
 103                 namelen = EXT_NAME_LEN;
 104 #endif
 105         bh = ext_bread(dir,0,0);
 106         if (!bh)
 107                 return NULL;
 108         if (prev_dir)
 109                 *prev_dir = NULL;
 110         if (next_dir)
 111                 *next_dir = NULL;
 112         offset = 0;
 113         de = (struct ext_dir_entry *) bh->b_data;
 114         while (offset < dir->i_size) {
 115                 if ((char *)de >= BLOCK_SIZE+bh->b_data) {
 116                         brelse(bh);
 117                         bh = NULL;
 118                         bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
 119                         if (!bh)
 120                                 continue;
 121                         de = (struct ext_dir_entry *) bh->b_data;
 122                         if (prev_dir)
 123                                 *prev_dir = NULL;
 124                 }
 125                 if (de->rec_len < 8 || de->rec_len % 8 != 0 ||
 126                     de->rec_len < de->name_len + 8 ||
 127                     (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
 128                         printk ("ext_find_entry: bad dir entry\n");
 129                         printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n",
 130                                 dir->i_dev, dir->i_ino, offset, de->rec_len, de->name_len);
 131                         de = (struct ext_dir_entry *) (bh->b_data+BLOCK_SIZE);
 132                         offset = ((offset / BLOCK_SIZE) + 1) * BLOCK_SIZE;
 133                         continue;
 134 /*                      brelse (bh);
 135                         return NULL; */
 136                 }
 137                 if (ext_match(namelen,name,de)) {
 138                         *res_dir = de;
 139                         if (next_dir)
 140                                 if (offset + de->rec_len < dir->i_size &&
 141                                     ((char *)de) + de->rec_len < BLOCK_SIZE+bh->b_data)
 142                                         *next_dir = (struct ext_dir_entry *)
 143                                                 ((char *) de + de->rec_len);
 144                                 else
 145                                         *next_dir = NULL;
 146                         return bh;
 147                 }
 148                 offset += de->rec_len;
 149                 if (prev_dir)
 150                         *prev_dir = de;
 151                 de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 152         }
 153         brelse(bh);
 154         return NULL;
 155 }
 156 
 157 int ext_lookup(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 158         struct inode ** result)
 159 {
 160         int ino;
 161         struct ext_dir_entry * de;
 162         struct buffer_head * bh;
 163 
 164         *result = NULL;
 165         if (!dir)
 166                 return -ENOENT;
 167         if (!S_ISDIR(dir->i_mode)) {
 168                 iput(dir);
 169                 return -ENOENT;
 170         }
 171         if (!(bh = ext_find_entry(dir,name,len,&de,NULL,NULL))) {
 172                 iput(dir);
 173                 return -ENOENT;
 174         }
 175         ino = de->inode;
 176         brelse(bh);
 177         if (!(*result = iget(dir->i_sb,ino))) {
 178                 iput(dir);
 179                 return -EACCES;
 180         }
 181         iput(dir);
 182         return 0;
 183 }
 184 
 185 /*
 186  *      ext_add_entry()
 187  *
 188  * adds a file entry to the specified directory, using the same
 189  * semantics as ext_find_entry(). It returns NULL if it failed.
 190  *
 191  * NOTE!! The inode part of 'de' is left at 0 - which means you
 192  * may not sleep between calling this and putting something into
 193  * the entry, as someone else might have used it while you slept.
 194  */
 195 static struct buffer_head * ext_add_entry(struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
 196         const char * name, int namelen, struct ext_dir_entry ** res_dir)
 197 {
 198         int i;
 199         long offset;
 200         unsigned short rec_len;
 201         struct buffer_head * bh;
 202         struct ext_dir_entry * de, * de1;
 203 
 204         *res_dir = NULL;
 205         if (!dir)
 206                 return NULL;
 207 #ifdef NO_TRUNCATE
 208         if (namelen > EXT_NAME_LEN)
 209                 return NULL;
 210 #else
 211         if (namelen > EXT_NAME_LEN)
 212                 namelen = EXT_NAME_LEN;
 213 #endif
 214         if (!namelen)
 215                 return NULL;
 216         bh = ext_bread(dir,0,0);
 217         if (!bh)
 218                 return NULL;
 219         rec_len = ((8 + namelen + EXT_DIR_PAD - 1) / EXT_DIR_PAD) * EXT_DIR_PAD;
 220         offset = 0;
 221         de = (struct ext_dir_entry *) bh->b_data;
 222         while (1) {
 223                 if ((char *)de >= BLOCK_SIZE+bh->b_data && offset < dir->i_size) {
 224 #ifdef EXTFS_DEBUG
 225 printk ("ext_add_entry: skipping to next block\n");
 226 #endif
 227                         brelse(bh);
 228                         bh = NULL;
 229                         bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
 230                         if (!bh)
 231                                 return NULL;
 232                         de = (struct ext_dir_entry *) bh->b_data;
 233                 }
 234                 if (offset >= dir->i_size) {
 235                         /* Check that the directory entry fits in the block */
 236                         if (offset % BLOCK_SIZE == 0  ||
 237                             (BLOCK_SIZE - (offset % BLOCK_SIZE)) < rec_len) {
 238                                 if ((offset % BLOCK_SIZE) != 0) {
 239                                         /* If the entry does not fit in the
 240                                            block, the remainder of the block
 241                                            becomes an unused entry */
 242                                         de->inode = 0;
 243                                         de->rec_len = BLOCK_SIZE
 244                                                 - (offset & (BLOCK_SIZE - 1));
 245                                         de->name_len = 0;
 246                                         offset += de->rec_len;
 247                                         dir->i_size += de->rec_len;
 248                                         dir->i_dirt = 1;
 249 #if 0
 250                                         dir->i_ctime = CURRENT_TIME;
 251 #endif
 252                                         mark_buffer_dirty(bh, 1);
 253                                 }
 254                                 brelse (bh);
 255                                 bh = NULL;
 256 #ifdef EXTFS_DEBUG
 257 printk ("ext_add_entry : creating next block\n");
 258 #endif
 259                                 bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,1);
 260                                 if (!bh)
 261                                         return NULL; /* Other thing to do ??? */
 262                                 de = (struct ext_dir_entry *) bh->b_data;
 263                         }
 264                         /* Allocate the entry */
 265                         de->inode=0;
 266                         de->rec_len = rec_len;
 267                         dir->i_size += de->rec_len;
 268                         dir->i_dirt = 1;
 269 #if 0
 270                         dir->i_ctime = CURRENT_TIME;
 271 #endif
 272                 }
 273                 if (de->rec_len < 8 || de->rec_len % 4 != 0 ||
 274                     de->rec_len < de->name_len + 8 ||
 275                     (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
 276                         printk ("ext_addr_entry: bad dir entry\n");
 277                         printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n",
 278                                 dir->i_dev, dir->i_ino, offset, de->rec_len, de->name_len);
 279                         brelse (bh);
 280                         return NULL;
 281                 }
 282                 if (!de->inode && de->rec_len >= rec_len) {
 283                         if (de->rec_len > rec_len
 284                             && de->rec_len - rec_len >= EXT_DIR_MIN_SIZE) {
 285                                 /* The found entry is too big : it is split
 286                                    into 2 ones :
 287                                    - the 1st one will be used to hold the name,
 288                                    - the 2nd one is unused */
 289                                 de1 = (struct ext_dir_entry *) ((char *) de + rec_len);
 290                                 de1->inode = 0;
 291                                 de1->rec_len = de->rec_len - rec_len;
 292                                 de1->name_len = 0;
 293                                 de->rec_len = rec_len;
 294                         }
 295                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
 296                         de->name_len = namelen;
 297                         for (i=0; i < namelen ; i++)
 298                                 de->name[i] = name[i];
 299                         mark_buffer_dirty(bh, 1);
 300                         *res_dir = de;
 301                         return bh;
 302                 }
 303                 offset += de->rec_len;
 304                 de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 305         }
 306         brelse(bh);
 307         return NULL;
 308 }
 309 
 310 int ext_create(struct inode * dir,const char * name, int len, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 311         struct inode ** result)
 312 {
 313         struct inode * inode;
 314         struct buffer_head * bh;
 315         struct ext_dir_entry * de;
 316 
 317         *result = NULL;
 318         if (!dir)
 319                 return -ENOENT;
 320         inode = ext_new_inode(dir);
 321         if (!inode) {
 322                 iput(dir);
 323                 return -ENOSPC;
 324         }
 325         inode->i_op = &ext_file_inode_operations;
 326         inode->i_mode = mode;
 327         inode->i_dirt = 1;
 328         bh = ext_add_entry(dir,name,len,&de);
 329         if (!bh) {
 330                 inode->i_nlink--;
 331                 inode->i_dirt = 1;
 332                 iput(inode);
 333                 iput(dir);
 334                 return -ENOSPC;
 335         }
 336         de->inode = inode->i_ino;
 337         mark_buffer_dirty(bh, 1);
 338         brelse(bh);
 339         iput(dir);
 340         *result = inode;
 341         return 0;
 342 }
 343 
 344 int ext_mknod(struct inode * dir, const char * name, int len, int mode, int rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         struct inode * inode;
 347         struct buffer_head * bh;
 348         struct ext_dir_entry * de;
 349 
 350         if (!dir)
 351                 return -ENOENT;
 352         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 353         if (bh) {
 354                 brelse(bh);
 355                 iput(dir);
 356                 return -EEXIST;
 357         }
 358         inode = ext_new_inode(dir);
 359         if (!inode) {
 360                 iput(dir);
 361                 return -ENOSPC;
 362         }
 363         inode->i_uid = current->euid;
 364         inode->i_mode = mode;
 365         inode->i_op = NULL;
 366         if (S_ISREG(inode->i_mode))
 367                 inode->i_op = &ext_file_inode_operations;
 368         else if (S_ISDIR(inode->i_mode)) {
 369                 inode->i_op = &ext_dir_inode_operations;
 370                 if (dir->i_mode & S_ISGID)
 371                         inode->i_mode |= S_ISGID;
 372         }
 373         else if (S_ISLNK(inode->i_mode))
 374                 inode->i_op = &ext_symlink_inode_operations;
 375         else if (S_ISCHR(inode->i_mode))
 376                 inode->i_op = &chrdev_inode_operations;
 377         else if (S_ISBLK(inode->i_mode))
 378                 inode->i_op = &blkdev_inode_operations;
 379         else if (S_ISFIFO(inode->i_mode))
 380                 init_fifo(inode);
 381         if (S_ISBLK(mode) || S_ISCHR(mode))
 382                 inode->i_rdev = rdev;
 383 #if 0
 384         inode->i_mtime = inode->i_atime = CURRENT_TIME;
 385 #endif
 386         inode->i_dirt = 1;
 387         bh = ext_add_entry(dir,name,len,&de);
 388         if (!bh) {
 389                 inode->i_nlink--;
 390                 inode->i_dirt = 1;
 391                 iput(inode);
 392                 iput(dir);
 393                 return -ENOSPC;
 394         }
 395         de->inode = inode->i_ino;
 396         mark_buffer_dirty(bh, 1);
 397         brelse(bh);
 398         iput(dir);
 399         iput(inode);
 400         return 0;
 401 }
 402 
 403 int ext_mkdir(struct inode * dir, const char * name, int len, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 404 {
 405         struct inode * inode;
 406         struct buffer_head * bh, *dir_block;
 407         struct ext_dir_entry * de;
 408         
 409         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 410         if (bh) {
 411                 brelse(bh);
 412                 iput(dir);
 413                 return -EEXIST;
 414         }
 415         inode = ext_new_inode(dir);
 416         if (!inode) {
 417                 iput(dir);
 418                 return -ENOSPC;
 419         }
 420         inode->i_op = &ext_dir_inode_operations;
 421         inode->i_size = 2 * 16; /* Each entry is coded on 16 bytes for "." and ".."
 422                                         - 4 bytes for the inode number,
 423                                         - 2 bytes for the record length
 424                                         - 2 bytes for the name length
 425                                         - 8 bytes for the name */
 426 #if 0
 427         inode->i_mtime = inode->i_atime = CURRENT_TIME;
 428 #endif
 429         dir_block = ext_bread(inode,0,1);
 430         if (!dir_block) {
 431                 iput(dir);
 432                 inode->i_nlink--;
 433                 inode->i_dirt = 1;
 434                 iput(inode);
 435                 return -ENOSPC;
 436         }
 437         de = (struct ext_dir_entry *) dir_block->b_data;
 438         de->inode=inode->i_ino;
 439         de->rec_len=16;
 440         de->name_len=1;
 441         strcpy(de->name,".");
 442         de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 443         de->inode = dir->i_ino;
 444         de->rec_len=16;
 445         de->name_len=2;
 446         strcpy(de->name,"..");
 447         inode->i_nlink = 2;
 448         mark_buffer_dirty(dir_block, 1);
 449         brelse(dir_block);
 450         inode->i_mode = S_IFDIR | (mode & 0777 & ~current->fs->umask);
 451         if (dir->i_mode & S_ISGID)
 452                 inode->i_mode |= S_ISGID;
 453         inode->i_dirt = 1;
 454         bh = ext_add_entry(dir,name,len,&de);
 455         if (!bh) {
 456                 iput(dir);
 457                 inode->i_nlink=0;
 458                 iput(inode);
 459                 return -ENOSPC;
 460         }
 461         de->inode = inode->i_ino;
 462         mark_buffer_dirty(bh, 1);
 463         dir->i_nlink++;
 464         dir->i_dirt = 1;
 465         iput(dir);
 466         iput(inode);
 467         brelse(bh);
 468         return 0;
 469 }
 470 
 471 /*
 472  * routine to check that the specified directory is empty (for rmdir)
 473  */
 474 static int empty_dir(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476         unsigned long offset;
 477         struct buffer_head * bh;
 478         struct ext_dir_entry * de, * de1;
 479 
 480         if (inode->i_size < 2 * 12 || !(bh = ext_bread(inode,0,0))) {
 481                 printk("warning - bad directory on dev %04x\n",inode->i_dev);
 482                 return 1;
 483         }
 484         de = (struct ext_dir_entry *) bh->b_data;
 485         de1 = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 486         if (de->inode != inode->i_ino || !de1->inode || 
 487             strcmp(".",de->name) || strcmp("..",de1->name)) {
 488                 printk("warning - bad directory on dev %04x\n",inode->i_dev);
 489                 return 1;
 490         }
 491         offset = de->rec_len + de1->rec_len;
 492         de = (struct ext_dir_entry *) ((char *) de1 + de1->rec_len);
 493         while (offset < inode->i_size ) {
 494                 if ((void *) de >= (void *) (bh->b_data+BLOCK_SIZE)) {
 495                         brelse(bh);
 496                         bh = ext_bread(inode, offset >> BLOCK_SIZE_BITS,1);
 497                         if (!bh) {
 498                                 offset += BLOCK_SIZE;
 499                                 continue;
 500                         }
 501                         de = (struct ext_dir_entry *) bh->b_data;
 502                 }
 503                 if (de->rec_len < 8 || de->rec_len %4 != 0 ||
 504                     de->rec_len < de->name_len + 8) {
 505                         printk ("empty_dir: bad dir entry\n");
 506                         printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n",
 507                                 inode->i_dev, inode->i_ino, offset, de->rec_len, de->name_len);
 508                         brelse (bh);
 509                         return 1;
 510                 }
 511                 if (de->inode) {
 512                         brelse(bh);
 513                         return 0;
 514                 }
 515                 offset += de->rec_len;
 516                 de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 517         }
 518         brelse(bh);
 519         return 1;
 520 }
 521 
 522 static inline void ext_merge_entries (struct ext_dir_entry * de,
     /* [previous][next][first][last][top][bottom][index][help] */
 523         struct ext_dir_entry * pde, struct ext_dir_entry * nde)
 524 {
 525         if (nde && !nde->inode)
 526                 de->rec_len += nde->rec_len;
 527         if (pde && !pde->inode)
 528                 pde->rec_len += de->rec_len;
 529 }
 530 
 531 int ext_rmdir(struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 532 {
 533         int retval;
 534         struct inode * inode;
 535         struct buffer_head * bh;
 536         struct ext_dir_entry * de, * pde, * nde;
 537 
 538         inode = NULL;
 539         bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
 540         retval = -ENOENT;
 541         if (!bh)
 542                 goto end_rmdir;
 543         retval = -EPERM;
 544         if (!(inode = iget(dir->i_sb, de->inode)))
 545                 goto end_rmdir;
 546         if ((dir->i_mode & S_ISVTX) && !suser() &&
 547             current->euid != inode->i_uid &&
 548             current->euid != dir->i_uid)
 549                 goto end_rmdir;
 550         if (inode->i_dev != dir->i_dev)
 551                 goto end_rmdir;
 552         if (inode == dir)       /* we may not delete ".", but "../dir" is ok */
 553                 goto end_rmdir;
 554         if (!S_ISDIR(inode->i_mode)) {
 555                 retval = -ENOTDIR;
 556                 goto end_rmdir;
 557         }
 558         if (!empty_dir(inode)) {
 559                 retval = -ENOTEMPTY;
 560                 goto end_rmdir;
 561         }
 562         if (inode->i_count > 1) {
 563                 retval = -EBUSY;
 564                 goto end_rmdir;
 565         }
 566         if (inode->i_nlink != 2)
 567                 printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink);
 568         de->inode = 0;
 569         de->name_len = 0;
 570         ext_merge_entries (de, pde, nde);
 571         mark_buffer_dirty(bh, 1);
 572         inode->i_nlink=0;
 573         inode->i_dirt=1;
 574         dir->i_nlink--;
 575         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 576         dir->i_dirt=1;
 577         retval = 0;
 578 end_rmdir:
 579         iput(dir);
 580         iput(inode);
 581         brelse(bh);
 582         return retval;
 583 }
 584 
 585 int ext_unlink(struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 586 {
 587         int retval;
 588         struct inode * inode;
 589         struct buffer_head * bh;
 590         struct ext_dir_entry * de, * pde, * nde;
 591 
 592         retval = -ENOENT;
 593         inode = NULL;
 594         bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
 595         if (!bh)
 596                 goto end_unlink;
 597         if (!(inode = iget(dir->i_sb, de->inode)))
 598                 goto end_unlink;
 599         retval = -EPERM;
 600         if ((dir->i_mode & S_ISVTX) && !suser() &&
 601             current->euid != inode->i_uid &&
 602             current->euid != dir->i_uid)
 603                 goto end_unlink;
 604         if (S_ISDIR(inode->i_mode))
 605                 goto end_unlink;
 606         if (!inode->i_nlink) {
 607                 printk("Deleting nonexistent file (%04x:%d), %d\n",
 608                         inode->i_dev,inode->i_ino,inode->i_nlink);
 609                 inode->i_nlink=1;
 610         }
 611         de->inode = 0;
 612         de->name_len = 0;
 613         ext_merge_entries (de, pde, nde);
 614         mark_buffer_dirty(bh, 1);
 615         inode->i_nlink--;
 616         inode->i_dirt = 1;
 617         inode->i_ctime = CURRENT_TIME;
 618         dir->i_ctime = dir->i_mtime = inode->i_ctime;
 619         dir->i_dirt = 1;
 620         retval = 0;
 621 end_unlink:
 622         brelse(bh);
 623         iput(inode);
 624         iput(dir);
 625         return retval;
 626 }
 627 
 628 int ext_symlink(struct inode * dir, const char * name, int len, const char * symname)
     /* [previous][next][first][last][top][bottom][index][help] */
 629 {
 630         struct ext_dir_entry * de;
 631         struct inode * inode = NULL;
 632         struct buffer_head * bh = NULL, * name_block = NULL;
 633         int i;
 634         char c;
 635 
 636         if (!(inode = ext_new_inode(dir))) {
 637                 iput(dir);
 638                 return -ENOSPC;
 639         }
 640         inode->i_mode = S_IFLNK | 0777;
 641         inode->i_op = &ext_symlink_inode_operations;
 642         name_block = ext_bread(inode,0,1);
 643         if (!name_block) {
 644                 iput(dir);
 645                 inode->i_nlink--;
 646                 inode->i_dirt = 1;
 647                 iput(inode);
 648                 return -ENOSPC;
 649         }
 650         i = 0;
 651         while (i < 1023 && (c = *(symname++)))
 652                 name_block->b_data[i++] = c;
 653         name_block->b_data[i] = 0;
 654         mark_buffer_dirty(name_block, 1);
 655         brelse(name_block);
 656         inode->i_size = i;
 657         inode->i_dirt = 1;
 658         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 659         if (bh) {
 660                 inode->i_nlink--;
 661                 inode->i_dirt = 1;
 662                 iput(inode);
 663                 brelse(bh);
 664                 iput(dir);
 665                 return -EEXIST;
 666         }
 667         bh = ext_add_entry(dir,name,len,&de);
 668         if (!bh) {
 669                 inode->i_nlink--;
 670                 inode->i_dirt = 1;
 671                 iput(inode);
 672                 iput(dir);
 673                 return -ENOSPC;
 674         }
 675         de->inode = inode->i_ino;
 676         mark_buffer_dirty(bh, 1);
 677         brelse(bh);
 678         iput(dir);
 679         iput(inode);
 680         return 0;
 681 }
 682 
 683 int ext_link(struct inode * oldinode, struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685         struct ext_dir_entry * de;
 686         struct buffer_head * bh;
 687 
 688         if (S_ISDIR(oldinode->i_mode)) {
 689                 iput(oldinode);
 690                 iput(dir);
 691                 return -EPERM;
 692         }
 693         if (oldinode->i_nlink > 32000) {
 694                 iput(oldinode);
 695                 iput(dir);
 696                 return -EMLINK;
 697         }
 698         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 699         if (bh) {
 700                 brelse(bh);
 701                 iput(dir);
 702                 iput(oldinode);
 703                 return -EEXIST;
 704         }
 705         bh = ext_add_entry(dir,name,len,&de);
 706         if (!bh) {
 707                 iput(dir);
 708                 iput(oldinode);
 709                 return -ENOSPC;
 710         }
 711         de->inode = oldinode->i_ino;
 712         mark_buffer_dirty(bh, 1);
 713         brelse(bh);
 714         iput(dir);
 715         oldinode->i_nlink++;
 716         oldinode->i_ctime = CURRENT_TIME;
 717         oldinode->i_dirt = 1;
 718         iput(oldinode);
 719         return 0;
 720 }
 721 
 722 static int subdir(struct inode * new_inode, struct inode * old_inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 723 {
 724         int ino;
 725         int result;
 726 
 727         new_inode->i_count++;
 728         result = 0;
 729         for (;;) {
 730                 if (new_inode == old_inode) {
 731                         result = 1;
 732                         break;
 733                 }
 734                 if (new_inode->i_dev != old_inode->i_dev)
 735                         break;
 736                 ino = new_inode->i_ino;
 737                 if (ext_lookup(new_inode,"..",2,&new_inode))
 738                         break;
 739                 if (new_inode->i_ino == ino)
 740                         break;
 741         }
 742         iput(new_inode);
 743         return result;
 744 }
 745 
 746 #define PARENT_INO(buffer) \
 747 ((struct ext_dir_entry *) ((char *) buffer + \
 748 ((struct ext_dir_entry *) buffer)->rec_len))->inode
 749 
 750 #define PARENT_NAME(buffer) \
 751 ((struct ext_dir_entry *) ((char *) buffer + \
 752 ((struct ext_dir_entry *) buffer)->rec_len))->name
 753 
 754 /*
 755  * rename uses retrying to avoid race-conditions: at least they should be minimal.
 756  * it tries to allocate all the blocks, then sanity-checks, and if the sanity-
 757  * checks fail, it tries to restart itself again. Very practical - no changes
 758  * are done until we know everything works ok.. and then all the changes can be
 759  * done in one fell swoop when we have claimed all the buffers needed.
 760  *
 761  * Anybody can rename anything with this: the permission checks are left to the
 762  * higher-level routines.
 763  */
 764 static int do_ext_rename(struct inode * old_dir, const char * old_name, int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
 765         struct inode * new_dir, const char * new_name, int new_len)
 766 {
 767         struct inode * old_inode, * new_inode;
 768         struct buffer_head * old_bh, * new_bh, * dir_bh;
 769         struct ext_dir_entry * old_de, * new_de, * pde, * nde;
 770         int retval;
 771 
 772         goto start_up;
 773 try_again:
 774         brelse(old_bh);
 775         brelse(new_bh);
 776         brelse(dir_bh);
 777         iput(old_inode);
 778         iput(new_inode);
 779         current->counter = 0;
 780         schedule();
 781 start_up:
 782         old_inode = new_inode = NULL;
 783         old_bh = new_bh = dir_bh = NULL;
 784         old_bh = ext_find_entry(old_dir,old_name,old_len,&old_de,&pde,&nde);
 785         retval = -ENOENT;
 786         if (!old_bh)
 787                 goto end_rename;
 788         old_inode = __iget(old_dir->i_sb, old_de->inode,0); /* don't cross mnt-points */
 789         if (!old_inode)
 790                 goto end_rename;
 791         retval = -EPERM;
 792         if ((old_dir->i_mode & S_ISVTX) && 
 793             current->euid != old_inode->i_uid &&
 794             current->euid != old_dir->i_uid && !suser())
 795                 goto end_rename;
 796         new_bh = ext_find_entry(new_dir,new_name,new_len,&new_de,NULL,NULL);
 797         if (new_bh) {
 798                 new_inode = __iget(new_dir->i_sb, new_de->inode,0); /* don't cross mnt-points */
 799                 if (!new_inode) {
 800                         brelse(new_bh);
 801                         new_bh = NULL;
 802                 }
 803         }
 804         if (new_inode == old_inode) {
 805                 retval = 0;
 806                 goto end_rename;
 807         }
 808         if (new_inode && S_ISDIR(new_inode->i_mode)) {
 809                 retval = -EEXIST;
 810                 goto end_rename;
 811         }
 812         retval = -EPERM;
 813         if (new_inode && (new_dir->i_mode & S_ISVTX) && 
 814             current->euid != new_inode->i_uid &&
 815             current->euid != new_dir->i_uid && !suser())
 816                 goto end_rename;
 817         if (S_ISDIR(old_inode->i_mode)) {
 818                 retval = -EEXIST;
 819                 if (new_bh)
 820                         goto end_rename;
 821                 retval = -EACCES;
 822                 if (!permission(old_inode, MAY_WRITE))
 823                         goto end_rename;
 824                 retval = -EINVAL;
 825                 if (subdir(new_dir, old_inode))
 826                         goto end_rename;
 827                 retval = -EIO;
 828                 dir_bh = ext_bread(old_inode,0,0);
 829                 if (!dir_bh)
 830                         goto end_rename;
 831                 if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
 832                         goto end_rename;
 833         }
 834         if (!new_bh)
 835                 new_bh = ext_add_entry(new_dir,new_name,new_len,&new_de);
 836         retval = -ENOSPC;
 837         if (!new_bh)
 838                 goto end_rename;
 839 /* sanity checking before doing the rename - avoid races */
 840         if (new_inode && (new_de->inode != new_inode->i_ino))
 841                 goto try_again;
 842         if (new_de->inode && !new_inode)
 843                 goto try_again;
 844         if (old_de->inode != old_inode->i_ino)
 845                 goto try_again;
 846 /* ok, that's it */
 847         old_de->inode = 0;
 848         old_de->name_len = 0;
 849         new_de->inode = old_inode->i_ino;
 850         ext_merge_entries (old_de, pde, nde);
 851         if (new_inode) {
 852                 new_inode->i_nlink--;
 853                 new_inode->i_dirt = 1;
 854         }
 855         mark_buffer_dirty(old_bh, 1);
 856         mark_buffer_dirty(new_bh, 1);
 857         if (dir_bh) {
 858                 PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
 859                 mark_buffer_dirty(dir_bh, 1);
 860                 old_dir->i_nlink--;
 861                 new_dir->i_nlink++;
 862                 old_dir->i_dirt = 1;
 863                 new_dir->i_dirt = 1;
 864         }
 865         retval = 0;
 866 end_rename:
 867         brelse(dir_bh);
 868         brelse(old_bh);
 869         brelse(new_bh);
 870         iput(old_inode);
 871         iput(new_inode);
 872         iput(old_dir);
 873         iput(new_dir);
 874         return retval;
 875 }
 876 
 877 /*
 878  * Ok, rename also locks out other renames, as they can change the parent of
 879  * a directory, and we don't want any races. Other races are checked for by
 880  * "do_rename()", which restarts if there are inconsistencies.
 881  *
 882  * Note that there is no race between different filesystems: it's only within
 883  * the same device that races occur: many renames can happen at once, as long
 884  * as they are on different partitions.
 885  */
 886 int ext_rename(struct inode * old_dir, const char * old_name, int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
 887         struct inode * new_dir, const char * new_name, int new_len)
 888 {
 889         static struct wait_queue * wait = NULL;
 890         static int lock = 0;
 891         int result;
 892 
 893         while (lock)
 894                 sleep_on(&wait);
 895         lock = 1;
 896         result = do_ext_rename(old_dir, old_name, old_len,
 897                 new_dir, new_name, new_len);
 898         lock = 0;
 899         wake_up(&wait);
 900         return result;
 901 }

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