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_create
  6. ext2_mknod
  7. ext2_mkdir
  8. empty_dir
  9. ext2_rmdir
  10. ext2_unlink
  11. ext2_symlink
  12. ext2_link
  13. subdir
  14. do_ext2_rename
  15. ext2_rename

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

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