home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / fs / ext2 / namei.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  26.0 KB  |  1,099 lines

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