home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / ext2 / namei.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  25.3 KB  |  1,068 lines

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