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                                         dir->i_ctime = CURRENT_TIME;
 250                                         bh->b_dirt = 1;
 251                                 }
 252                                 brelse (bh);
 253                                 bh = NULL;
 254 #ifdef EXTFS_DEBUG
 255 printk ("ext_add_entry : creating next block\n");
 256 #endif
 257                                 bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,1);
 258                                 if (!bh)
 259                                         return NULL; /* Other thing to do ??? */
 260                                 de = (struct ext_dir_entry *) bh->b_data;
 261                         }
 262                         /* Allocate the entry */
 263                         de->inode=0;
 264                         de->rec_len = rec_len;
 265                         dir->i_size += de->rec_len;
 266                         dir->i_dirt = 1;
 267                         dir->i_ctime = CURRENT_TIME;
 268                 }
 269                 if (de->rec_len < 8 || de->rec_len % 4 != 0 ||
 270                     de->rec_len < de->name_len + 8 ||
 271                     (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
 272                         printk ("ext_addr_entry: bad dir entry\n");
 273                         printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n",
 274                                 dir->i_dev, dir->i_ino, offset, de->rec_len, de->name_len);
 275                         brelse (bh);
 276                         return NULL;
 277                 }
 278                 if (!de->inode && de->rec_len >= rec_len) {
 279                         if (de->rec_len > rec_len
 280                             && de->rec_len - rec_len >= EXT_DIR_MIN_SIZE) {
 281                                 /* The found entry is too big : it is split
 282                                    into 2 ones :
 283                                    - the 1st one will be used to hold the name,
 284                                    - the 2nd one is unused */
 285                                 de1 = (struct ext_dir_entry *) ((char *) de + rec_len);
 286                                 de1->inode = 0;
 287                                 de1->rec_len = de->rec_len - rec_len;
 288                                 de1->name_len = 0;
 289                                 de->rec_len = rec_len;
 290                         }
 291                         dir->i_mtime = CURRENT_TIME;
 292                         de->name_len = namelen;
 293                         for (i=0; i < namelen ; i++)
 294                                 de->name[i] = name[i];
 295                         bh->b_dirt = 1;
 296                         *res_dir = de;
 297                         return bh;
 298                 }
 299                 offset += de->rec_len;
 300                 de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 301         }
 302         brelse(bh);
 303         return NULL;
 304 }
 305 
 306 int ext_create(struct inode * dir,const char * name, int len, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 307         struct inode ** result)
 308 {
 309         struct inode * inode;
 310         struct buffer_head * bh;
 311         struct ext_dir_entry * de;
 312 
 313         *result = NULL;
 314         if (!dir)
 315                 return -ENOENT;
 316         inode = ext_new_inode(dir);
 317         if (!inode) {
 318                 iput(dir);
 319                 return -ENOSPC;
 320         }
 321         inode->i_op = &ext_file_inode_operations;
 322         inode->i_mode = mode;
 323         inode->i_dirt = 1;
 324         bh = ext_add_entry(dir,name,len,&de);
 325         if (!bh) {
 326                 inode->i_nlink--;
 327                 inode->i_dirt = 1;
 328                 iput(inode);
 329                 iput(dir);
 330                 return -ENOSPC;
 331         }
 332         de->inode = inode->i_ino;
 333         bh->b_dirt = 1;
 334         brelse(bh);
 335         iput(dir);
 336         *result = inode;
 337         return 0;
 338 }
 339 
 340 int ext_mknod(struct inode * dir, const char * name, int len, int mode, int rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
 341 {
 342         struct inode * inode;
 343         struct buffer_head * bh;
 344         struct ext_dir_entry * de;
 345 
 346         if (!dir)
 347                 return -ENOENT;
 348         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 349         if (bh) {
 350                 brelse(bh);
 351                 iput(dir);
 352                 return -EEXIST;
 353         }
 354         inode = ext_new_inode(dir);
 355         if (!inode) {
 356                 iput(dir);
 357                 return -ENOSPC;
 358         }
 359         inode->i_uid = current->euid;
 360         inode->i_mode = mode;
 361         inode->i_op = NULL;
 362         if (S_ISREG(inode->i_mode))
 363                 inode->i_op = &ext_file_inode_operations;
 364         else if (S_ISDIR(inode->i_mode)) {
 365                 inode->i_op = &ext_dir_inode_operations;
 366                 if (dir->i_mode & S_ISGID)
 367                         inode->i_mode |= S_ISGID;
 368         }
 369         else if (S_ISLNK(inode->i_mode))
 370                 inode->i_op = &ext_symlink_inode_operations;
 371         else if (S_ISCHR(inode->i_mode))
 372                 inode->i_op = &chrdev_inode_operations;
 373         else if (S_ISBLK(inode->i_mode))
 374                 inode->i_op = &blkdev_inode_operations;
 375         else if (S_ISFIFO(inode->i_mode))
 376                 init_fifo(inode);
 377         if (S_ISBLK(mode) || S_ISCHR(mode))
 378                 inode->i_rdev = rdev;
 379         inode->i_mtime = inode->i_atime = CURRENT_TIME;
 380         inode->i_dirt = 1;
 381         bh = ext_add_entry(dir,name,len,&de);
 382         if (!bh) {
 383                 inode->i_nlink--;
 384                 inode->i_dirt = 1;
 385                 iput(inode);
 386                 iput(dir);
 387                 return -ENOSPC;
 388         }
 389         de->inode = inode->i_ino;
 390         bh->b_dirt = 1;
 391         brelse(bh);
 392         iput(dir);
 393         iput(inode);
 394         return 0;
 395 }
 396 
 397 int ext_mkdir(struct inode * dir, const char * name, int len, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399         struct inode * inode;
 400         struct buffer_head * bh, *dir_block;
 401         struct ext_dir_entry * de;
 402         
 403         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 404         if (bh) {
 405                 brelse(bh);
 406                 iput(dir);
 407                 return -EEXIST;
 408         }
 409         inode = ext_new_inode(dir);
 410         if (!inode) {
 411                 iput(dir);
 412                 return -ENOSPC;
 413         }
 414         inode->i_op = &ext_dir_inode_operations;
 415         inode->i_size = 2 * 16; /* Each entry is coded on 16 bytes for "." and ".."
 416                                         - 4 bytes for the inode number,
 417                                         - 2 bytes for the record length
 418                                         - 2 bytes for the name length
 419                                         - 8 bytes for the name */
 420         inode->i_mtime = inode->i_atime = CURRENT_TIME;
 421         dir_block = ext_bread(inode,0,1);
 422         if (!dir_block) {
 423                 iput(dir);
 424                 inode->i_nlink--;
 425                 inode->i_dirt = 1;
 426                 iput(inode);
 427                 return -ENOSPC;
 428         }
 429         de = (struct ext_dir_entry *) dir_block->b_data;
 430         de->inode=inode->i_ino;
 431         de->rec_len=16;
 432         de->name_len=1;
 433         strcpy(de->name,".");
 434         de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 435         de->inode = dir->i_ino;
 436         de->rec_len=16;
 437         de->name_len=2;
 438         strcpy(de->name,"..");
 439         inode->i_nlink = 2;
 440         dir_block->b_dirt = 1;
 441         brelse(dir_block);
 442         inode->i_mode = S_IFDIR | (mode & 0777 & ~current->umask);
 443         if (dir->i_mode & S_ISGID)
 444                 inode->i_mode |= S_ISGID;
 445         inode->i_dirt = 1;
 446         bh = ext_add_entry(dir,name,len,&de);
 447         if (!bh) {
 448                 iput(dir);
 449                 inode->i_nlink=0;
 450                 iput(inode);
 451                 return -ENOSPC;
 452         }
 453         de->inode = inode->i_ino;
 454         bh->b_dirt = 1;
 455         dir->i_nlink++;
 456         dir->i_dirt = 1;
 457         iput(dir);
 458         iput(inode);
 459         brelse(bh);
 460         return 0;
 461 }
 462 
 463 /*
 464  * routine to check that the specified directory is empty (for rmdir)
 465  */
 466 static int empty_dir(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 467 {
 468         unsigned long offset;
 469         struct buffer_head * bh;
 470         struct ext_dir_entry * de, * de1;
 471 
 472         if (inode->i_size < 2 * 12 || !(bh = ext_bread(inode,0,0))) {
 473                 printk("warning - bad directory on dev %04x\n",inode->i_dev);
 474                 return 1;
 475         }
 476         de = (struct ext_dir_entry *) bh->b_data;
 477         de1 = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 478         if (de->inode != inode->i_ino || !de1->inode || 
 479             strcmp(".",de->name) || strcmp("..",de1->name)) {
 480                 printk("warning - bad directory on dev %04x\n",inode->i_dev);
 481                 return 1;
 482         }
 483         offset = de->rec_len + de1->rec_len;
 484         de = (struct ext_dir_entry *) ((char *) de1 + de1->rec_len);
 485         while (offset < inode->i_size ) {
 486                 if ((void *) de >= (void *) (bh->b_data+BLOCK_SIZE)) {
 487                         brelse(bh);
 488                         bh = ext_bread(inode, offset >> BLOCK_SIZE_BITS,1);
 489                         if (!bh) {
 490                                 offset += BLOCK_SIZE;
 491                                 continue;
 492                         }
 493                         de = (struct ext_dir_entry *) bh->b_data;
 494                 }
 495                 if (de->rec_len < 8 || de->rec_len %4 != 0 ||
 496                     de->rec_len < de->name_len + 8) {
 497                         printk ("empty_dir: bad dir entry\n");
 498                         printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n",
 499                                 inode->i_dev, inode->i_ino, offset, de->rec_len, de->name_len);
 500                         brelse (bh);
 501                         return 1;
 502                 }
 503                 if (de->inode) {
 504                         brelse(bh);
 505                         return 0;
 506                 }
 507                 offset += de->rec_len;
 508                 de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
 509         }
 510         brelse(bh);
 511         return 1;
 512 }
 513 
 514 static inline void ext_merge_entries (struct ext_dir_entry * de,
     /* [previous][next][first][last][top][bottom][index][help] */
 515         struct ext_dir_entry * pde, struct ext_dir_entry * nde)
 516 {
 517         if (nde && !nde->inode)
 518                 de->rec_len += nde->rec_len;
 519         if (pde && !pde->inode)
 520                 pde->rec_len += de->rec_len;
 521 }
 522 
 523 int ext_rmdir(struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 524 {
 525         int retval;
 526         struct inode * inode;
 527         struct buffer_head * bh;
 528         struct ext_dir_entry * de, * pde, * nde;
 529 
 530         inode = NULL;
 531         bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
 532         retval = -ENOENT;
 533         if (!bh)
 534                 goto end_rmdir;
 535         retval = -EPERM;
 536         if (!(inode = iget(dir->i_sb, de->inode)))
 537                 goto end_rmdir;
 538         if ((dir->i_mode & S_ISVTX) && current->euid &&
 539            inode->i_uid != current->euid)
 540                 goto end_rmdir;
 541         if (inode->i_dev != dir->i_dev)
 542                 goto end_rmdir;
 543         if (inode == dir)       /* we may not delete ".", but "../dir" is ok */
 544                 goto end_rmdir;
 545         if (!S_ISDIR(inode->i_mode)) {
 546                 retval = -ENOTDIR;
 547                 goto end_rmdir;
 548         }
 549         if (!empty_dir(inode)) {
 550                 retval = -ENOTEMPTY;
 551                 goto end_rmdir;
 552         }
 553         if (inode->i_count > 1) {
 554                 retval = -EBUSY;
 555                 goto end_rmdir;
 556         }
 557         if (inode->i_nlink != 2)
 558                 printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink);
 559         de->inode = 0;
 560         de->name_len = 0;
 561         ext_merge_entries (de, pde, nde);
 562         bh->b_dirt = 1;
 563         inode->i_nlink=0;
 564         inode->i_dirt=1;
 565         dir->i_nlink--;
 566         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 567         dir->i_dirt=1;
 568         retval = 0;
 569 end_rmdir:
 570         iput(dir);
 571         iput(inode);
 572         brelse(bh);
 573         return retval;
 574 }
 575 
 576 int ext_unlink(struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 577 {
 578         int retval;
 579         struct inode * inode;
 580         struct buffer_head * bh;
 581         struct ext_dir_entry * de, * pde, * nde;
 582 
 583         retval = -ENOENT;
 584         inode = NULL;
 585         bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
 586         if (!bh)
 587                 goto end_unlink;
 588         if (!(inode = iget(dir->i_sb, de->inode)))
 589                 goto end_unlink;
 590         retval = -EPERM;
 591         if ((dir->i_mode & S_ISVTX) && !suser() &&
 592             current->euid != inode->i_uid &&
 593             current->euid != dir->i_uid)
 594                 goto end_unlink;
 595         if (S_ISDIR(inode->i_mode))
 596                 goto end_unlink;
 597         if (!inode->i_nlink) {
 598                 printk("Deleting nonexistent file (%04x:%d), %d\n",
 599                         inode->i_dev,inode->i_ino,inode->i_nlink);
 600                 inode->i_nlink=1;
 601         }
 602         de->inode = 0;
 603         de->name_len = 0;
 604         ext_merge_entries (de, pde, nde);
 605         bh->b_dirt = 1;
 606         inode->i_nlink--;
 607         inode->i_dirt = 1;
 608         inode->i_ctime = CURRENT_TIME;
 609         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 610         dir->i_dirt = 1;
 611         retval = 0;
 612 end_unlink:
 613         brelse(bh);
 614         iput(inode);
 615         iput(dir);
 616         return retval;
 617 }
 618 
 619 int ext_symlink(struct inode * dir, const char * name, int len, const char * symname)
     /* [previous][next][first][last][top][bottom][index][help] */
 620 {
 621         struct ext_dir_entry * de;
 622         struct inode * inode = NULL;
 623         struct buffer_head * bh = NULL, * name_block = NULL;
 624         int i;
 625         char c;
 626 
 627         if (!(inode = ext_new_inode(dir))) {
 628                 iput(dir);
 629                 return -ENOSPC;
 630         }
 631         inode->i_mode = S_IFLNK | 0777;
 632         inode->i_op = &ext_symlink_inode_operations;
 633         name_block = ext_bread(inode,0,1);
 634         if (!name_block) {
 635                 iput(dir);
 636                 inode->i_nlink--;
 637                 inode->i_dirt = 1;
 638                 iput(inode);
 639                 return -ENOSPC;
 640         }
 641         i = 0;
 642         while (i < 1023 && (c = *(symname++)))
 643                 name_block->b_data[i++] = c;
 644         name_block->b_data[i] = 0;
 645         name_block->b_dirt = 1;
 646         brelse(name_block);
 647         inode->i_size = i;
 648         inode->i_dirt = 1;
 649         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 650         if (bh) {
 651                 inode->i_nlink--;
 652                 inode->i_dirt = 1;
 653                 iput(inode);
 654                 brelse(bh);
 655                 iput(dir);
 656                 return -EEXIST;
 657         }
 658         bh = ext_add_entry(dir,name,len,&de);
 659         if (!bh) {
 660                 inode->i_nlink--;
 661                 inode->i_dirt = 1;
 662                 iput(inode);
 663                 iput(dir);
 664                 return -ENOSPC;
 665         }
 666         de->inode = inode->i_ino;
 667         bh->b_dirt = 1;
 668         brelse(bh);
 669         iput(dir);
 670         iput(inode);
 671         return 0;
 672 }
 673 
 674 int ext_link(struct inode * oldinode, struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 675 {
 676         struct ext_dir_entry * de;
 677         struct buffer_head * bh;
 678 
 679         if (S_ISDIR(oldinode->i_mode)) {
 680                 iput(oldinode);
 681                 iput(dir);
 682                 return -EPERM;
 683         }
 684         if (oldinode->i_nlink > 32000) {
 685                 iput(oldinode);
 686                 iput(dir);
 687                 return -EMLINK;
 688         }
 689         bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
 690         if (bh) {
 691                 brelse(bh);
 692                 iput(dir);
 693                 iput(oldinode);
 694                 return -EEXIST;
 695         }
 696         bh = ext_add_entry(dir,name,len,&de);
 697         if (!bh) {
 698                 iput(dir);
 699                 iput(oldinode);
 700                 return -ENOSPC;
 701         }
 702         de->inode = oldinode->i_ino;
 703         bh->b_dirt = 1;
 704         brelse(bh);
 705         iput(dir);
 706         oldinode->i_nlink++;
 707         oldinode->i_ctime = CURRENT_TIME;
 708         oldinode->i_dirt = 1;
 709         iput(oldinode);
 710         return 0;
 711 }
 712 
 713 static int subdir(struct inode * new, struct inode * old)
     /* [previous][next][first][last][top][bottom][index][help] */
 714 {
 715         int ino;
 716         int result;
 717 
 718         new->i_count++;
 719         result = 0;
 720         for (;;) {
 721                 if (new == old) {
 722                         result = 1;
 723                         break;
 724                 }
 725                 if (new->i_dev != old->i_dev)
 726                         break;
 727                 ino = new->i_ino;
 728                 if (ext_lookup(new,"..",2,&new))
 729                         break;
 730                 if (new->i_ino == ino)
 731                         break;
 732         }
 733         iput(new);
 734         return result;
 735 }
 736 
 737 #define PARENT_INO(buffer) \
 738 ((struct ext_dir_entry *) ((char *) buffer + \
 739 ((struct ext_dir_entry *) buffer)->rec_len))->inode
 740 
 741 #define PARENT_NAME(buffer) \
 742 ((struct ext_dir_entry *) ((char *) buffer + \
 743 ((struct ext_dir_entry *) buffer)->rec_len))->name
 744 
 745 /*
 746  * rename uses retrying to avoid race-conditions: at least they should be minimal.
 747  * it tries to allocate all the blocks, then sanity-checks, and if the sanity-
 748  * checks fail, it tries to restart itself again. Very practical - no changes
 749  * are done until we know everything works ok.. and then all the changes can be
 750  * done in one fell swoop when we have claimed all the buffers needed.
 751  *
 752  * Anybody can rename anything with this: the permission checks are left to the
 753  * higher-level routines.
 754  */
 755 static int do_ext_rename(struct inode * old_dir, const char * old_name, int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
 756         struct inode * new_dir, const char * new_name, int new_len)
 757 {
 758         struct inode * old_inode, * new_inode;
 759         struct buffer_head * old_bh, * new_bh, * dir_bh;
 760         struct ext_dir_entry * old_de, * new_de, * pde, * nde;
 761         int retval;
 762 
 763         goto start_up;
 764 try_again:
 765         brelse(old_bh);
 766         brelse(new_bh);
 767         brelse(dir_bh);
 768         iput(old_inode);
 769         iput(new_inode);
 770         current->counter = 0;
 771         schedule();
 772 start_up:
 773         old_inode = new_inode = NULL;
 774         old_bh = new_bh = dir_bh = NULL;
 775         old_bh = ext_find_entry(old_dir,old_name,old_len,&old_de,&pde,&nde);
 776         retval = -ENOENT;
 777         if (!old_bh)
 778                 goto end_rename;
 779         old_inode = iget(old_dir->i_sb, old_de->inode);
 780         if (!old_inode)
 781                 goto end_rename;
 782         retval = -EPERM;
 783         if ((old_dir->i_mode & S_ISVTX) && 
 784             current->euid != old_inode->i_uid &&
 785             current->euid != old_dir->i_uid && !suser())
 786                 goto end_rename;
 787         new_bh = ext_find_entry(new_dir,new_name,new_len,&new_de,NULL,NULL);
 788         if (new_bh) {
 789                 new_inode = iget(new_dir->i_sb, new_de->inode);
 790                 if (!new_inode) {
 791                         brelse(new_bh);
 792                         new_bh = NULL;
 793                 }
 794         }
 795         if (new_inode == old_inode) {
 796                 retval = 0;
 797                 goto end_rename;
 798         }
 799         if (new_inode && S_ISDIR(new_inode->i_mode)) {
 800                 retval = -EEXIST;
 801                 goto end_rename;
 802         }
 803         retval = -EPERM;
 804         if (new_inode && (new_dir->i_mode & S_ISVTX) && 
 805             current->euid != new_inode->i_uid &&
 806             current->euid != new_dir->i_uid && !suser())
 807                 goto end_rename;
 808         if (S_ISDIR(old_inode->i_mode)) {
 809                 retval = -EEXIST;
 810                 if (new_bh)
 811                         goto end_rename;
 812                 retval = -EACCES;
 813                 if (!permission(old_inode, MAY_WRITE))
 814                         goto end_rename;
 815                 retval = -EINVAL;
 816                 if (subdir(new_dir, old_inode))
 817                         goto end_rename;
 818                 retval = -EIO;
 819                 dir_bh = ext_bread(old_inode,0,0);
 820                 if (!dir_bh)
 821                         goto end_rename;
 822                 if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
 823                         goto end_rename;
 824         }
 825         if (!new_bh)
 826                 new_bh = ext_add_entry(new_dir,new_name,new_len,&new_de);
 827         retval = -ENOSPC;
 828         if (!new_bh)
 829                 goto end_rename;
 830 /* sanity checking before doing the rename - avoid races */
 831         if (new_inode && (new_de->inode != new_inode->i_ino))
 832                 goto try_again;
 833         if (new_de->inode && !new_inode)
 834                 goto try_again;
 835         if (old_de->inode != old_inode->i_ino)
 836                 goto try_again;
 837 /* ok, that's it */
 838         old_de->inode = 0;
 839         old_de->name_len = 0;
 840         new_de->inode = old_inode->i_ino;
 841         ext_merge_entries (old_de, pde, nde);
 842         if (new_inode) {
 843                 new_inode->i_nlink--;
 844                 new_inode->i_dirt = 1;
 845         }
 846         old_bh->b_dirt = 1;
 847         new_bh->b_dirt = 1;
 848         if (dir_bh) {
 849                 PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
 850                 dir_bh->b_dirt = 1;
 851                 old_dir->i_nlink--;
 852                 new_dir->i_nlink++;
 853                 old_dir->i_dirt = 1;
 854                 new_dir->i_dirt = 1;
 855         }
 856         retval = 0;
 857 end_rename:
 858         brelse(dir_bh);
 859         brelse(old_bh);
 860         brelse(new_bh);
 861         iput(old_inode);
 862         iput(new_inode);
 863         iput(old_dir);
 864         iput(new_dir);
 865         return retval;
 866 }
 867 
 868 /*
 869  * Ok, rename also locks out other renames, as they can change the parent of
 870  * a directory, and we don't want any races. Other races are checked for by
 871  * "do_rename()", which restarts if there are inconsistencies.
 872  *
 873  * Note that there is no race between different filesystems: it's only within
 874  * the same device that races occur: many renames can happen at once, as long
 875  * as they are on different partitions.
 876  */
 877 int ext_rename(struct inode * old_dir, const char * old_name, int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
 878         struct inode * new_dir, const char * new_name, int new_len)
 879 {
 880         static struct wait_queue * wait = NULL;
 881         static int lock = 0;
 882         int result;
 883 
 884         while (lock)
 885                 sleep_on(&wait);
 886         lock = 1;
 887         result = do_ext_rename(old_dir, old_name, old_len,
 888                 new_dir, new_name, new_len);
 889         lock = 0;
 890         wake_up(&wait);
 891         return result;
 892 }

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