root/fs/ext2/namei.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext2_match
  2. ext2_find_entry
  3. ext2_lookup
  4. ext2_add_entry
  5. ext2_delete_entry
  6. ext2_create
  7. ext2_mknod
  8. ext2_mkdir
  9. empty_dir
  10. ext2_rmdir
  11. ext2_unlink
  12. ext2_symlink
  13. ext2_link
  14. subdir
  15. do_ext2_rename
  16. ext2_rename

   1 /*
   2  *  linux/fs/ext2/namei.c
   3  *
   4  *  Copyright (C) 1992, 1993, 1994  Remy Card (card@masi.ibp.fr)
   5  *                                  Laboratoire MASI - Institut Blaise Pascal
   6  *                                  Universite Pierre et Marie Curie (Paris VI)
   7  *
   8  *  from
   9  *
  10  *  linux/fs/minix/namei.c
  11  *
  12  *  Copyright (C) 1991, 1992  Linus Torvalds
  13  */
  14 
  15 #include <asm/segment.h>
  16 
  17 #include <linux/errno.h>
  18 #include <linux/fs.h>
  19 #include <linux/ext2_fs.h>
  20 #include <linux/fcntl.h>
  21 #include <linux/sched.h>
  22 #include <linux/stat.h>
  23 #include <linux/string.h>
  24 #include <linux/locks.h>
  25 
  26 /*
  27  * comment out this line if you want names > EXT2_NAME_LEN chars to be
  28  * truncated. Else they will be disallowed.
  29  */
  30 /* #define NO_TRUNCATE */
  31 
  32 /*
  33  * define how far ahead to read directories while searching them.
  34  */
  35 #define NAMEI_RA_CHUNKS  2
  36 #define NAMEI_RA_BLOCKS  4
  37 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  38 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
  39 
  40 /*
  41  * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
  42  */
  43 static int ext2_match (int len, const char * const name,
     /* [previous][next][first][last][top][bottom][index][help] */
  44                        struct ext2_dir_entry * de)
  45 {
  46         unsigned char same;
  47 
  48         if (!de || !de->inode || len > EXT2_NAME_LEN)
  49                 return 0;
  50         /*
  51          * "" means "." ---> so paths like "/usr/lib//libc.a" work
  52          */
  53         if (!len && de->name_len == 1 && (de->name[0] == '.') &&
  54            (de->name[1] == '\0'))
  55                 return 1;
  56         if (len != de->name_len)
  57                 return 0;
  58         __asm__("cld\n\t"
  59                 "repe ; cmpsb\n\t"
  60                 "setz %0"
  61                 :"=q" (same)
  62                 :"S" ((long) name), "D" ((long) de->name), "c" (len)
  63                 :"cx", "di", "si");
  64         return (int) same;
  65 }
  66 
  67 /*
  68  *      ext2_find_entry()
  69  *
  70  * finds an entry in the specified directory with the wanted name. It
  71  * returns the cache buffer in which the entry was found, and the entry
  72  * itself (as a parameter - res_dir). It does NOT read the inode of the
  73  * entry - you'll have to do that yourself if you want to.
  74  */
  75 static struct buffer_head * ext2_find_entry (struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
  76                                              const char * const name, int namelen,
  77                                              struct ext2_dir_entry ** res_dir)
  78 {
  79         struct super_block * sb;
  80         struct buffer_head * bh_use[NAMEI_RA_SIZE];
  81         struct buffer_head * bh_read[NAMEI_RA_SIZE];
  82         unsigned long offset;
  83         int block, toread, i, err;
  84 
  85         *res_dir = NULL;
  86         if (!dir)
  87                 return NULL;
  88         sb = dir->i_sb;
  89 
  90 #ifdef NO_TRUNCATE
  91         if (namelen > EXT2_NAME_LEN)
  92                 return NULL;
  93 #else
  94         if (namelen > EXT2_NAME_LEN)
  95                 namelen = EXT2_NAME_LEN;
  96 #endif
  97 
  98         memset (bh_use, 0, sizeof (bh_use));
  99         toread = 0;
 100         for (block = 0; block < NAMEI_RA_SIZE; ++block) {
 101                 struct buffer_head * bh;
 102 
 103                 if ((block << EXT2_BLOCK_SIZE_BITS (sb)) >= dir->i_size)
 104                         break;
 105                 bh = ext2_getblk (dir, block, 0, &err);
 106                 bh_use[block] = bh;
 107                 if (bh && !bh->b_uptodate)
 108                         bh_read[toread++] = bh;
 109         }
 110 
 111         block = 0;
 112         offset = 0;
 113         while (offset < dir->i_size) {
 114                 struct buffer_head * bh;
 115                 struct ext2_dir_entry * de;
 116                 char * dlimit;
 117 
 118                 if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
 119                         ll_rw_block (READ, toread, bh_read);
 120                         toread = 0;
 121                 }
 122                 bh = bh_use[block % NAMEI_RA_SIZE];
 123                 if (!bh)
 124                         ext2_panic (sb, "ext2_find_entry",
 125                                     "buffer head pointer is NULL");
 126                 wait_on_buffer (bh);
 127                 if (!bh->b_uptodate) {
 128                         /*
 129                          * read error: all bets are off
 130                          */
 131                         break;
 132                 }
 133 
 134                 de = (struct ext2_dir_entry *) bh->b_data;
 135                 dlimit = bh->b_data + sb->s_blocksize;
 136                 while ((char *) de < dlimit) {
 137                         if (!ext2_check_dir_entry ("ext2_find_entry", dir,
 138                                                    de, bh, offset))
 139                                 goto failure;
 140                         if (de->inode != 0 && ext2_match (namelen, name, de)) {
 141                                 for (i = 0; i < NAMEI_RA_SIZE; ++i) {
 142                                         if (bh_use[i] != bh)
 143                                                 brelse (bh_use[i]);
 144                                 }
 145                                 *res_dir = de;
 146                                 return bh;
 147                         }
 148                         offset += de->rec_len;
 149                         de = (struct ext2_dir_entry *)
 150                                 ((char *) de + de->rec_len);
 151                 }
 152 
 153                 brelse (bh);
 154                 if (((block + NAMEI_RA_SIZE) << EXT2_BLOCK_SIZE_BITS (sb)) >=
 155                     dir->i_size)
 156                         bh = NULL;
 157                 else
 158                         bh = ext2_getblk (dir, block + NAMEI_RA_SIZE, 0, &err);
 159                 bh_use[block++ % NAMEI_RA_SIZE] = bh;
 160                 if (bh && !bh->b_uptodate)
 161                         bh_read[toread++] = bh;
 162         }
 163 
 164 failure:
 165         for (i = 0; i < NAMEI_RA_SIZE; ++i)
 166                 brelse (bh_use[i]);
 167         return NULL;
 168 }
 169 
 170 int ext2_lookup (struct inode * dir, const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 171                  struct inode ** result)
 172 {
 173         unsigned long ino;
 174         struct ext2_dir_entry * de;
 175         struct buffer_head * bh;
 176 
 177         *result = NULL;
 178         if (!dir)
 179                 return -ENOENT;
 180         if (!S_ISDIR(dir->i_mode)) {
 181                 iput (dir);
 182                 return -ENOENT;
 183         }
 184 #ifndef DONT_USE_DCACHE
 185         if (!(ino = ext2_dcache_lookup (dir->i_dev, dir->i_ino, name, len))) {
 186 #endif
 187                 if (!(bh = ext2_find_entry (dir, name, len, &de))) {
 188                         iput (dir);
 189                         return -ENOENT;
 190                 }
 191                 ino = de->inode;
 192 #ifndef DONT_USE_DCACHE
 193                 ext2_dcache_add (dir->i_dev, dir->i_ino, de->name,
 194                                  de->name_len, ino);
 195 #endif
 196                 brelse (bh);
 197 #ifndef DONT_USE_DCACHE
 198         }
 199 #endif
 200         if (!(*result = iget (dir->i_sb, ino))) {
 201                 iput (dir);
 202                 return -EACCES;
 203         }
 204         iput (dir);
 205         return 0;
 206 }
 207 
 208 /*
 209  *      ext2_add_entry()
 210  *
 211  * adds a file entry to the specified directory, using the same
 212  * semantics as ext2_find_entry(). It returns NULL if it failed.
 213  *
 214  * NOTE!! The inode part of 'de' is left at 0 - which means you
 215  * may not sleep between calling this and putting something into
 216  * the entry, as someone else might have used it while you slept.
 217  */
 218 static struct buffer_head * ext2_add_entry (struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
 219                                             const char * name, int namelen,
 220                                             struct ext2_dir_entry ** res_dir,
 221                                             int *err)
 222 {
 223         unsigned long offset;
 224         unsigned short rec_len;
 225         struct buffer_head * bh;
 226         struct ext2_dir_entry * de, * de1;
 227         struct super_block * sb;
 228 
 229         *err = -EINVAL;
 230         *res_dir = NULL;
 231         if (!dir)
 232                 return NULL;
 233         sb = dir->i_sb;
 234 #ifdef NO_TRUNCATE
 235         if (namelen > EXT2_NAME_LEN)
 236                 return NULL;
 237 #else
 238         if (namelen > EXT2_NAME_LEN)
 239                 namelen = EXT2_NAME_LEN;
 240 #endif
 241         if (!namelen)
 242                 return NULL;
 243         /*
 244          * Is this a busy deleted directory?  Can't create new files if so
 245          */
 246         if (dir->i_size == 0)
 247         {
 248                 *err = -ENOENT;
 249                 return NULL;
 250         }
 251         bh = ext2_bread (dir, 0, 0, err);
 252         if (!bh)
 253                 return NULL;
 254         rec_len = EXT2_DIR_REC_LEN(namelen);
 255         offset = 0;
 256         de = (struct ext2_dir_entry *) bh->b_data;
 257         *err = -ENOSPC;
 258         while (1) {
 259                 if ((char *)de >= sb->s_blocksize + bh->b_data) {
 260                         brelse (bh);
 261                         bh = NULL;
 262                         bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
 263                         if (!bh)
 264                                 return NULL;
 265                         if (dir->i_size <= offset) {
 266                                 if (dir->i_size == 0) {
 267                                         *err = -ENOENT;
 268                                         return NULL;
 269                                 }
 270 
 271                                 ext2_debug ("creating next block\n");
 272 
 273                                 de = (struct ext2_dir_entry *) bh->b_data;
 274                                 de->inode = 0;
 275                                 de->rec_len = sb->s_blocksize;
 276                                 dir->i_size = offset + sb->s_blocksize;
 277                                 dir->i_dirt = 1;
 278                         } else {
 279 
 280                                 ext2_debug ("skipping to next block\n");
 281 
 282                                 de = (struct ext2_dir_entry *) bh->b_data;
 283                         }
 284                 }
 285                 if (!ext2_check_dir_entry ("ext2_add_entry", dir, de, bh,
 286                                            offset)) {
 287                         *err = -ENOENT;
 288                         brelse (bh);
 289                         return NULL;
 290                 }
 291                 if (de->inode != 0 && ext2_match (namelen, name, de)) {
 292                                 *err = -EEXIST;
 293                                 brelse (bh);
 294                                 return NULL;
 295                 }
 296                 if ((de->inode == 0 && de->rec_len >= rec_len) ||
 297                     (de->rec_len >= EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
 298                         offset += de->rec_len;
 299                         if (de->inode) {
 300                                 de1 = (struct ext2_dir_entry *) ((char *) de +
 301                                         EXT2_DIR_REC_LEN(de->name_len));
 302                                 de1->rec_len = de->rec_len -
 303                                         EXT2_DIR_REC_LEN(de->name_len);
 304                                 de->rec_len = EXT2_DIR_REC_LEN(de->name_len);
 305                                 de = de1;
 306                         }
 307                         de->inode = 0;
 308                         de->name_len = namelen;
 309                         memcpy (de->name, name, namelen);
 310                         /*
 311                          * XXX shouldn't update any times until successful
 312                          * completion of syscall, but too many callers depend
 313                          * on this.
 314                          *
 315                          * XXX similarly, too many callers depend on
 316                          * ext2_new_inode() setting the times, but error
 317                          * recovery deletes the inode, so the worst that can
 318                          * happen is that the times are slightly out of date
 319                          * and/or different from the directory change time.
 320                          */
 321                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
 322                         dir->i_dirt = 1;
 323                         mark_buffer_dirty(bh, 1);
 324                         *res_dir = de;
 325                         *err = 0;
 326                         return bh;
 327                 }
 328                 offset += de->rec_len;
 329                 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
 330         }
 331         brelse (bh);
 332         return NULL;
 333 }
 334 
 335 /*
 336  * ext2_delete_entry deletes a directory entry by merging it with the
 337  * previous entry
 338  */
 339 static int ext2_delete_entry (struct ext2_dir_entry * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
 340                               struct buffer_head * bh)
 341 {
 342         struct ext2_dir_entry * de, * pde;
 343         int i;
 344 
 345         i = 0;
 346         pde = NULL;
 347         de = (struct ext2_dir_entry *) bh->b_data;
 348         while (i < bh->b_size) {
 349                 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL, 
 350                                            de, bh, i))
 351                         return -EIO;
 352                 if (de == dir)  {
 353                         if (pde)
 354                                 pde->rec_len += dir->rec_len;
 355                         dir->inode = 0;
 356                         return 0;
 357                 }
 358                 i += de->rec_len;
 359                 pde = de;
 360                 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
 361         }
 362         return -ENOENT;
 363 }
 364 
 365 int ext2_create (struct inode * dir,const char * name, int len, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 366                  struct inode ** result)
 367 {
 368         struct inode * inode;
 369         struct buffer_head * bh;
 370         struct ext2_dir_entry * de;
 371         int err;
 372 
 373         *result = NULL;
 374         if (!dir)
 375                 return -ENOENT;
 376         inode = ext2_new_inode (dir, mode);
 377         if (!inode) {
 378                 iput (dir);
 379                 return -ENOSPC;
 380         }
 381         inode->i_op = &ext2_file_inode_operations;
 382         inode->i_mode = mode;
 383         inode->i_dirt = 1;
 384         bh = ext2_add_entry (dir, name, len, &de, &err);
 385         if (!bh) {
 386                 inode->i_nlink--;
 387                 inode->i_dirt = 1;
 388                 iput (inode);
 389                 iput (dir);
 390                 return err;
 391         }
 392         de->inode = inode->i_ino;
 393 #ifndef DONT_USE_DCACHE
 394         ext2_dcache_add (dir->i_dev, dir->i_ino, de->name, de->name_len,
 395                          de->inode);
 396 #endif
 397         mark_buffer_dirty(bh, 1);
 398         if (IS_SYNC(dir)) {
 399                 ll_rw_block (WRITE, 1, &bh);
 400                 wait_on_buffer (bh);
 401         }
 402         brelse (bh);
 403         iput (dir);
 404         *result = inode;
 405         return 0;
 406 }
 407 
 408 int ext2_mknod (struct inode * dir, const char * name, int len, int mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 409                 int rdev)
 410 {
 411         struct inode * inode;
 412         struct buffer_head * bh;
 413         struct ext2_dir_entry * de;
 414         int err;
 415 
 416         if (!dir)
 417                 return -ENOENT;
 418         bh = ext2_find_entry (dir, name, len, &de);
 419         if (bh) {
 420                 brelse (bh);
 421                 iput (dir);
 422                 return -EEXIST;
 423         }
 424         inode = ext2_new_inode (dir, mode);
 425         if (!inode) {
 426                 iput (dir);
 427                 return -ENOSPC;
 428         }
 429         inode->i_uid = current->euid;
 430         inode->i_mode = mode;
 431         inode->i_op = NULL;
 432         if (S_ISREG(inode->i_mode))
 433                 inode->i_op = &ext2_file_inode_operations;
 434         else if (S_ISDIR(inode->i_mode)) {
 435                 inode->i_op = &ext2_dir_inode_operations;
 436                 if (dir->i_mode & S_ISGID)
 437                         inode->i_mode |= S_ISGID;
 438         }
 439         else if (S_ISLNK(inode->i_mode))
 440                 inode->i_op = &ext2_symlink_inode_operations;
 441         else if (S_ISCHR(inode->i_mode))
 442                 inode->i_op = &chrdev_inode_operations;
 443         else if (S_ISBLK(inode->i_mode))
 444                 inode->i_op = &blkdev_inode_operations;
 445         else if (S_ISFIFO(inode->i_mode)) 
 446                 init_fifo(inode);
 447         if (S_ISBLK(mode) || S_ISCHR(mode))
 448                 inode->i_rdev = rdev;
 449         inode->i_dirt = 1;
 450         bh = ext2_add_entry (dir, name, len, &de, &err);
 451         if (!bh) {
 452                 inode->i_nlink--;
 453                 inode->i_dirt = 1;
 454                 iput (inode);
 455                 iput (dir);
 456                 return err;
 457         }
 458         de->inode = inode->i_ino;
 459 #ifndef DONT_USE_DCACHE
 460         ext2_dcache_add (dir->i_dev, dir->i_ino, de->name, de->name_len,
 461                          de->inode);
 462 #endif
 463         mark_buffer_dirty(bh, 1);
 464         if (IS_SYNC(dir)) {
 465                 ll_rw_block (WRITE, 1, &bh);
 466                 wait_on_buffer (bh);
 467         }
 468         brelse (bh);
 469         iput (dir);
 470         iput (inode);
 471         return 0;
 472 }
 473 
 474 int ext2_mkdir (struct inode * dir, const char * name, int len, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476         struct inode * inode;
 477         struct buffer_head * bh, * dir_block;
 478         struct ext2_dir_entry * de;
 479         int err;
 480 
 481         if (!dir)
 482                 return -ENOENT;
 483         bh = ext2_find_entry (dir, name, len, &de);
 484         if (bh) {
 485                 brelse (bh);
 486                 iput (dir);
 487                 return -EEXIST;
 488         }
 489         if (dir->i_nlink >= EXT2_LINK_MAX) {
 490                 iput (dir);
 491                 return -EMLINK;
 492         }
 493         inode = ext2_new_inode (dir, S_IFDIR);
 494         if (!inode) {
 495                 iput (dir);
 496                 return -ENOSPC;
 497         }
 498         inode->i_op = &ext2_dir_inode_operations;
 499         inode->i_size = inode->i_sb->s_blocksize;
 500         dir_block = ext2_bread (inode, 0, 1, &err);
 501         if (!dir_block) {
 502                 iput (dir);
 503                 inode->i_nlink--;
 504                 inode->i_dirt = 1;
 505                 iput (inode);
 506                 return err;
 507         }
 508         inode->i_blocks = inode->i_sb->s_blocksize / 512;
 509         de = (struct ext2_dir_entry *) dir_block->b_data;
 510         de->inode = inode->i_ino;
 511         de->name_len = 1;
 512         de->rec_len = EXT2_DIR_REC_LEN(de->name_len);
 513         strcpy (de->name, ".");
 514         de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
 515         de->inode = dir->i_ino;
 516         de->rec_len = inode->i_sb->s_blocksize - EXT2_DIR_REC_LEN(1);
 517         de->name_len = 2;
 518         strcpy (de->name, "..");
 519         inode->i_nlink = 2;
 520         mark_buffer_dirty(dir_block, 1);
 521         brelse (dir_block);
 522         inode->i_mode = S_IFDIR | (mode & S_IRWXUGO & ~current->fs->umask);
 523         if (dir->i_mode & S_ISGID)
 524                 inode->i_mode |= S_ISGID;
 525         inode->i_dirt = 1;
 526         bh = ext2_add_entry (dir, name, len, &de, &err);
 527         if (!bh) {
 528                 iput (dir);
 529                 inode->i_nlink = 0;
 530                 inode->i_dirt = 1;
 531                 iput (inode);
 532                 return err;
 533         }
 534         de->inode = inode->i_ino;
 535 #ifndef DONT_USE_DCACHE
 536         ext2_dcache_add (dir->i_dev, dir->i_ino, de->name, de->name_len,
 537                          de->inode);
 538 #endif
 539         mark_buffer_dirty(bh, 1);
 540         if (IS_SYNC(dir)) {
 541                 ll_rw_block (WRITE, 1, &bh);
 542                 wait_on_buffer (bh);
 543         }
 544         dir->i_nlink++;
 545         dir->i_dirt = 1;
 546         iput (dir);
 547         iput (inode);
 548         brelse (bh);
 549         return 0;
 550 }
 551 
 552 /*
 553  * routine to check that the specified directory is empty (for rmdir)
 554  */
 555 static int empty_dir (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 556 {
 557         unsigned long offset;
 558         struct buffer_head * bh;
 559         struct ext2_dir_entry * de, * de1;
 560         struct super_block * sb;
 561         int err;
 562 
 563         sb = inode->i_sb;
 564         if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
 565             !(bh = ext2_bread (inode, 0, 0, &err))) {
 566                 ext2_warning (inode->i_sb, "empty_dir",
 567                               "bad directory (dir %lu)", inode->i_ino);
 568                 return 1;
 569         }
 570         de = (struct ext2_dir_entry *) bh->b_data;
 571         de1 = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
 572         if (de->inode != inode->i_ino || !de1->inode || 
 573             strcmp (".", de->name) || strcmp ("..", de1->name)) {
 574                 ext2_warning (inode->i_sb, "empty_dir",
 575                               "bad directory (dir %lu)", inode->i_ino);
 576                 return 1;
 577         }
 578         offset = de->rec_len + de1->rec_len;
 579         de = (struct ext2_dir_entry *) ((char *) de1 + de1->rec_len);
 580         while (offset < inode->i_size ) {
 581                 if ((void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
 582                         brelse (bh);
 583                         bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);
 584                         if (!bh) {
 585                                 offset += sb->s_blocksize;
 586                                 continue;
 587                         }
 588                         de = (struct ext2_dir_entry *) bh->b_data;
 589                 }
 590                 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
 591                                            offset)) {
 592                         brelse (bh);
 593                         return 1;
 594                 }
 595                 if (de->inode) {
 596                         brelse (bh);
 597                         return 0;
 598                 }
 599                 offset += de->rec_len;
 600                 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
 601         }
 602         brelse (bh);
 603         return 1;
 604 }
 605 
 606 int ext2_rmdir (struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608         int retval;
 609         struct inode * inode;
 610         struct buffer_head * bh;
 611         struct ext2_dir_entry * de;
 612 
 613 repeat:
 614         if (!dir)
 615                 return -ENOENT;
 616         inode = NULL;
 617         bh = ext2_find_entry (dir, name, len, &de);
 618         retval = -ENOENT;
 619         if (!bh)
 620                 goto end_rmdir;
 621         retval = -EPERM;
 622         if (!(inode = iget (dir->i_sb, de->inode)))
 623                 goto end_rmdir;
 624         if (inode->i_dev != dir->i_dev)
 625                 goto end_rmdir;
 626         if (de->inode != inode->i_ino) {
 627                 iput(inode);
 628                 brelse(bh);
 629                 current->counter = 0;
 630                 schedule();
 631                 goto repeat;
 632         }
 633         if ((dir->i_mode & S_ISVTX) && !suser() &&
 634             current->euid != inode->i_uid &&
 635             current->euid != dir->i_uid)
 636                 goto end_rmdir;
 637         if (inode == dir)       /* we may not delete ".", but "../dir" is ok */
 638                 goto end_rmdir;
 639         if (!S_ISDIR(inode->i_mode)) {
 640                 retval = -ENOTDIR;
 641                 goto end_rmdir;
 642         }
 643         down(&inode->i_sem);
 644         if (!empty_dir (inode))
 645                 retval = -ENOTEMPTY;
 646         else if (de->inode != inode->i_ino)
 647                 retval = -ENOENT;
 648         else {
 649                 if (inode->i_count > 1) {
 650                 /*
 651                  * Are we deleting the last instance of a busy directory?
 652                  * Better clean up if so.
 653                  *
 654                  * Make directory empty (it will be truncated when finally
 655                  * dereferenced).  This also inhibits ext2_add_entry.
 656                  */
 657                         inode->i_size = 0;
 658                 }
 659                 retval = ext2_delete_entry (de, bh);
 660         }
 661         up(&inode->i_sem);
 662         if (retval)
 663                 goto end_rmdir;
 664         mark_buffer_dirty(bh, 1);
 665         if (IS_SYNC(dir)) {
 666                 ll_rw_block (WRITE, 1, &bh);
 667                 wait_on_buffer (bh);
 668         }
 669 #ifndef DONT_USE_DCACHE
 670         ext2_dcache_remove(inode->i_dev, inode->i_ino, ".", 1);
 671         ext2_dcache_remove(inode->i_dev, inode->i_ino, "..", 2);
 672 #endif
 673         if (inode->i_nlink != 2)
 674                 ext2_warning (inode->i_sb, "ext2_rmdir",
 675                               "empty directory has nlink!=2 (%d)",
 676                               inode->i_nlink);
 677 #ifndef DONT_USE_DCACHE
 678         ext2_dcache_remove (dir->i_dev, dir->i_ino, de->name, de->name_len);
 679 #endif
 680         inode->i_nlink = 0;
 681         inode->i_dirt = 1;
 682         dir->i_nlink--;
 683         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 684         dir->i_dirt = 1;
 685 end_rmdir:
 686         iput (dir);
 687         iput (inode);
 688         brelse (bh);
 689         return retval;
 690 }
 691 
 692 int ext2_unlink (struct inode * dir, const char * name, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 693 {
 694         int retval;
 695         struct inode * inode;
 696         struct buffer_head * bh;
 697         struct ext2_dir_entry * de;
 698 
 699 repeat:
 700         if (!dir)
 701                 return -ENOENT;
 702         retval = -ENOENT;
 703         inode = NULL;
 704         bh = ext2_find_entry (dir, name, len, &de);
 705         if (!bh)
 706                 goto end_unlink;
 707         if (!(inode = iget (dir->i_sb, de->inode)))
 708                 goto end_unlink;
 709         retval = -EPERM;
 710         if (S_ISDIR(inode->i_mode))
 711                 goto end_unlink;
 712         if (de->inode != inode->i_ino) {
 713                 iput(inode);
 714                 brelse(bh);
 715                 current->counter = 0;
 716                 schedule();
 717                 goto repeat;
 718         }
 719         if ((dir->i_mode & S_ISVTX) && !suser() &&
 720             current->euid != inode->i_uid &&
 721             current->euid != dir->i_uid)
 722                 goto end_unlink;
 723         if (!inode->i_nlink) {
 724                 ext2_warning (inode->i_sb, "ext2_unlink",
 725                               "Deleting nonexistent file (%lu), %d",
 726                               inode->i_ino, inode->i_nlink);
 727                 inode->i_nlink = 1;
 728         }
 729         retval = ext2_delete_entry (de, bh);
 730         if (retval)
 731                 goto end_unlink;
 732         mark_buffer_dirty(bh, 1);
 733         if (IS_SYNC(dir)) {
 734                 ll_rw_block (WRITE, 1, &bh);
 735                 wait_on_buffer (bh);
 736         }
 737 #ifndef DONT_USE_DCACHE
 738         ext2_dcache_remove (dir->i_dev, dir->i_ino, de->name, de->name_len);
 739 #endif
 740         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 741         dir->i_dirt = 1;
 742         inode->i_nlink--;
 743         inode->i_dirt = 1;
 744         inode->i_ctime = dir->i_ctime;
 745         retval = 0;
 746 end_unlink:
 747         brelse (bh);
 748         iput (inode);
 749         iput (dir);
 750         return retval;
 751 }
 752 
 753 int ext2_symlink (struct inode * dir, const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 754                   const char * symname)
 755 {
 756         struct ext2_dir_entry * de;
 757         struct inode * inode = NULL;
 758         struct buffer_head * bh = NULL, * name_block = NULL;
 759         char * link;
 760         int i, err;
 761         int l;
 762         char c;
 763 
 764         if (!(inode = ext2_new_inode (dir, S_IFLNK))) {
 765                 iput (dir);
 766                 return -ENOSPC;
 767         }
 768         inode->i_mode = S_IFLNK | S_IRWXUGO;
 769         inode->i_op = &ext2_symlink_inode_operations;
 770         for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
 771              symname [l]; l++)
 772                 ;
 773         if (l >= EXT2_N_BLOCKS * sizeof (unsigned long)) {
 774 
 775                 ext2_debug ("l=%d, normal symlink\n", l);
 776 
 777                 name_block = ext2_bread (inode, 0, 1, &err);
 778                 if (!name_block) {
 779                         iput (dir);
 780                         inode->i_nlink--;
 781                         inode->i_dirt = 1;
 782                         iput (inode);
 783                         return err;
 784                 }
 785                 link = name_block->b_data;
 786         } else {
 787                 link = (char *) inode->u.ext2_i.i_data;
 788 
 789                 ext2_debug ("l=%d, fast symlink\n", l);
 790 
 791         }
 792         i = 0;
 793         while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
 794                 link[i++] = c;
 795         link[i] = 0;
 796         if (name_block) {
 797                 mark_buffer_dirty(name_block, 1);
 798                 brelse (name_block);
 799         }
 800         inode->i_size = i;
 801         inode->i_dirt = 1;
 802         bh = ext2_find_entry (dir, name, len, &de);
 803         if (bh) {
 804                 inode->i_nlink--;
 805                 inode->i_dirt = 1;
 806                 iput (inode);
 807                 brelse (bh);
 808                 iput (dir);
 809                 return -EEXIST;
 810         }
 811         bh = ext2_add_entry (dir, name, len, &de, &err);
 812         if (!bh) {
 813                 inode->i_nlink--;
 814                 inode->i_dirt = 1;
 815                 iput (inode);
 816                 iput (dir);
 817                 return err;
 818         }
 819         de->inode = inode->i_ino;
 820 #ifndef DONT_USE_DCACHE
 821         ext2_dcache_add (dir->i_dev, dir->i_ino, de->name, de->name_len,
 822                          de->inode);
 823 #endif
 824         mark_buffer_dirty(bh, 1);
 825         if (IS_SYNC(dir)) {
 826                 ll_rw_block (WRITE, 1, &bh);
 827                 wait_on_buffer (bh);
 828         }
 829         brelse (bh);
 830         iput (dir);
 831         iput (inode);
 832         return 0;
 833 }
 834 
 835 int ext2_link (struct inode * oldinode, struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
 836                const char * name, int len)
 837 {
 838         struct ext2_dir_entry * de;
 839         struct buffer_head * bh;
 840         int err;
 841 
 842         if (S_ISDIR(oldinode->i_mode)) {
 843                 iput (oldinode);
 844                 iput (dir);
 845                 return -EPERM;
 846         }
 847         if (oldinode->i_nlink >= EXT2_LINK_MAX) {
 848                 iput (oldinode);
 849                 iput (dir);
 850                 return -EMLINK;
 851         }
 852         bh = ext2_find_entry (dir, name, len, &de);
 853         if (bh) {
 854                 brelse (bh);
 855                 iput (dir);
 856                 iput (oldinode);
 857                 return -EEXIST;
 858         }
 859         bh = ext2_add_entry (dir, name, len, &de, &err);
 860         if (!bh) {
 861                 iput (dir);
 862                 iput (oldinode);
 863                 return err;
 864         }
 865         de->inode = oldinode->i_ino;
 866 #ifndef DONT_USE_DCACHE
 867         ext2_dcache_add (dir->i_dev, dir->i_ino, de->name, de->name_len,
 868                          de->inode);
 869 #endif
 870         mark_buffer_dirty(bh, 1);
 871         if (IS_SYNC(dir)) {
 872                 ll_rw_block (WRITE, 1, &bh);
 873                 wait_on_buffer (bh);
 874         }
 875         brelse (bh);
 876         iput (dir);
 877         oldinode->i_nlink++;
 878         oldinode->i_ctime = CURRENT_TIME;
 879         oldinode->i_dirt = 1;
 880         iput (oldinode);
 881         return 0;
 882 }
 883 
 884 static int subdir (struct inode * new_inode, struct inode * old_inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 885 {
 886         int ino;
 887         int result;
 888 
 889         new_inode->i_count++;
 890         result = 0;
 891         for (;;) {
 892                 if (new_inode == old_inode) {
 893                         result = 1;
 894                         break;
 895                 }
 896                 if (new_inode->i_dev != old_inode->i_dev)
 897                         break;
 898                 ino = new_inode->i_ino;
 899                 if (ext2_lookup (new_inode, "..", 2, &new_inode))
 900                         break;
 901                 if (new_inode->i_ino == ino)
 902                         break;
 903         }
 904         iput (new_inode);
 905         return result;
 906 }
 907 
 908 #define PARENT_INO(buffer) \
 909         ((struct ext2_dir_entry *) ((char *) buffer + \
 910         ((struct ext2_dir_entry *) buffer)->rec_len))->inode
 911 
 912 #define PARENT_NAME(buffer) \
 913         ((struct ext2_dir_entry *) ((char *) buffer + \
 914         ((struct ext2_dir_entry *) buffer)->rec_len))->name
 915 
 916 /*
 917  * rename uses retrying to avoid race-conditions: at least they should be
 918  * minimal.
 919  * it tries to allocate all the blocks, then sanity-checks, and if the sanity-
 920  * checks fail, it tries to restart itself again. Very practical - no changes
 921  * are done until we know everything works ok.. and then all the changes can be
 922  * done in one fell swoop when we have claimed all the buffers needed.
 923  *
 924  * Anybody can rename anything with this: the permission checks are left to the
 925  * higher-level routines.
 926  */
 927 static int do_ext2_rename (struct inode * old_dir, const char * old_name,
     /* [previous][next][first][last][top][bottom][index][help] */
 928                            int old_len, struct inode * new_dir,
 929                            const char * new_name, int new_len)
 930 {
 931         struct inode * old_inode, * new_inode;
 932         struct buffer_head * old_bh, * new_bh, * dir_bh;
 933         struct ext2_dir_entry * old_de, * new_de;
 934         int retval;
 935 
 936         goto start_up;
 937 try_again:
 938         if (new_bh && new_de)
 939                 ext2_delete_entry(new_de, new_bh);
 940         brelse (old_bh);
 941         brelse (new_bh);
 942         brelse (dir_bh);
 943         iput (old_inode);
 944         iput (new_inode);
 945         current->counter = 0;
 946         schedule ();
 947 start_up:
 948         old_inode = new_inode = NULL;
 949         old_bh = new_bh = dir_bh = NULL;
 950         new_de = NULL;
 951         old_bh = ext2_find_entry (old_dir, old_name, old_len, &old_de);
 952         retval = -ENOENT;
 953         if (!old_bh)
 954                 goto end_rename;
 955         old_inode = __iget (old_dir->i_sb, old_de->inode, 0); /* don't cross mnt-points */
 956         if (!old_inode)
 957                 goto end_rename;
 958         retval = -EPERM;
 959         if ((old_dir->i_mode & S_ISVTX) && 
 960             current->euid != old_inode->i_uid &&
 961             current->euid != old_dir->i_uid && !suser())
 962                 goto end_rename;
 963         new_bh = ext2_find_entry (new_dir, new_name, new_len, &new_de);
 964         if (new_bh) {
 965                 new_inode = __iget (new_dir->i_sb, new_de->inode, 0); /* no mntp cross */
 966                 if (!new_inode) {
 967                         brelse (new_bh);
 968                         new_bh = NULL;
 969                 }
 970         }
 971         if (new_inode == old_inode) {
 972                 retval = 0;
 973                 goto end_rename;
 974         }
 975         if (new_inode && S_ISDIR(new_inode->i_mode)) {
 976                 retval = -EISDIR;
 977                 if (!S_ISDIR(old_inode->i_mode))
 978                         goto end_rename;
 979                 retval = -EINVAL;
 980                 if (subdir (new_dir, old_inode))
 981                         goto end_rename;
 982                 retval = -ENOTEMPTY;
 983                 if (!empty_dir (new_inode))
 984                         goto end_rename;
 985                 retval = -EBUSY;
 986                 if (new_inode->i_count > 1)
 987                         goto end_rename;
 988         }
 989         retval = -EPERM;
 990         if (new_inode && (new_dir->i_mode & S_ISVTX) &&
 991             current->euid != new_inode->i_uid &&
 992             current->euid != new_dir->i_uid && !suser())
 993                 goto end_rename;
 994         if (S_ISDIR(old_inode->i_mode)) {
 995                 retval = -ENOTDIR;
 996                 if (new_inode && !S_ISDIR(new_inode->i_mode))
 997                         goto end_rename;
 998                 retval = -EINVAL;
 999                 if (subdir (new_dir, old_inode))
1000                         goto end_rename;
1001                 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
1002                 if (!dir_bh)
1003                         goto end_rename;
1004                 if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
1005                         goto end_rename;
1006                 retval = -EMLINK;
1007                 if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)
1008                         goto end_rename;
1009         }
1010         if (!new_bh)
1011                 new_bh = ext2_add_entry (new_dir, new_name, new_len, &new_de,
1012                                          &retval);
1013         if (!new_bh)
1014                 goto end_rename;
1015         /*
1016          * sanity checking before doing the rename - avoid races
1017          */
1018         if (new_inode && (new_de->inode != new_inode->i_ino))
1019                 goto try_again;
1020         if (new_de->inode && !new_inode)
1021                 goto try_again;
1022         if (old_de->inode != old_inode->i_ino)
1023                 goto try_again;
1024         /*
1025          * ok, that's it
1026          */
1027         new_de->inode = old_inode->i_ino;
1028 #ifndef DONT_USE_DCACHE
1029         ext2_dcache_remove (old_dir->i_dev, old_dir->i_ino, old_de->name,
1030                             old_de->name_len);
1031         ext2_dcache_add (new_dir->i_dev, new_dir->i_ino, new_de->name,
1032                          new_de->name_len, new_de->inode);
1033 #endif
1034         retval = ext2_delete_entry (old_de, old_bh);
1035         if (retval == -ENOENT)
1036                 goto try_again;
1037         if (retval)
1038                 goto end_rename;
1039         if (new_inode) {
1040                 new_inode->i_nlink--;
1041                 new_inode->i_ctime = CURRENT_TIME;
1042                 new_inode->i_dirt = 1;
1043         }
1044         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1045         old_dir->i_dirt = 1;
1046         mark_buffer_dirty(old_bh,  1);
1047         if (IS_SYNC(old_dir)) {
1048                 ll_rw_block (WRITE, 1, &old_bh);
1049                 wait_on_buffer (old_bh);
1050         }
1051         mark_buffer_dirty(new_bh, 1);
1052         if (IS_SYNC(new_dir)) {
1053                 ll_rw_block (WRITE, 1, &new_bh);
1054                 wait_on_buffer (new_bh);
1055         }
1056         if (dir_bh) {
1057                 PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
1058                 mark_buffer_dirty(dir_bh, 1);
1059                 old_dir->i_nlink--;
1060                 old_dir->i_dirt = 1;
1061                 if (new_inode) {
1062                         new_inode->i_nlink--;
1063                         new_inode->i_dirt = 1;
1064                 } else {
1065                         new_dir->i_nlink++;
1066                         new_dir->i_dirt = 1;
1067                 }
1068         }
1069         retval = 0;
1070 end_rename:
1071         brelse (dir_bh);
1072         brelse (old_bh);
1073         brelse (new_bh);
1074         iput (old_inode);
1075         iput (new_inode);
1076         iput (old_dir);
1077         iput (new_dir);
1078         return retval;
1079 }
1080 
1081 /*
1082  * Ok, rename also locks out other renames, as they can change the parent of
1083  * a directory, and we don't want any races. Other races are checked for by
1084  * "do_rename()", which restarts if there are inconsistencies.
1085  *
1086  * Note that there is no race between different filesystems: it's only within
1087  * the same device that races occur: many renames can happen at once, as long
1088  * as they are on different partitions.
1089  *
1090  * In the second extended file system, we use a lock flag stored in the memory
1091  * super-block.  This way, we really lock other renames only if they occur
1092  * on the same file system
1093  */
1094 int ext2_rename (struct inode * old_dir, const char * old_name, int old_len,
     /* [previous][next][first][last][top][bottom][index][help] */
1095                  struct inode * new_dir, const char * new_name, int new_len)
1096 {
1097         int result;
1098 
1099         while (old_dir->i_sb->u.ext2_sb.s_rename_lock)
1100                 sleep_on (&old_dir->i_sb->u.ext2_sb.s_rename_wait);
1101         old_dir->i_sb->u.ext2_sb.s_rename_lock = 1;
1102         result = do_ext2_rename (old_dir, old_name, old_len, new_dir,
1103                                  new_name, new_len);
1104         old_dir->i_sb->u.ext2_sb.s_rename_lock = 0;
1105         wake_up (&old_dir->i_sb->u.ext2_sb.s_rename_wait);
1106         return result;
1107 }

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