~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/ext2/dir.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/fs/ext2/dir.c
  3  *
  4  * Copyright (C) 1992, 1993, 1994, 1995
  5  * Remy Card (card@masi.ibp.fr)
  6  * Laboratoire MASI - Institut Blaise Pascal
  7  * Universite Pierre et Marie Curie (Paris VI)
  8  *
  9  *  from
 10  *
 11  *  linux/fs/minix/dir.c
 12  *
 13  *  Copyright (C) 1991, 1992  Linus Torvalds
 14  *
 15  *  ext2 directory handling functions
 16  *
 17  *  Big-endian to little-endian byte-swapping/bitmaps by
 18  *        David S. Miller (davem@caip.rutgers.edu), 1995
 19  */
 20 
 21 #include <linux/fs.h>
 22 #include <linux/ext2_fs.h>
 23 
 24 static unsigned char ext2_filetype_table[] = {
 25         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
 26 };
 27 
 28 static int ext2_readdir(struct file *, void *, filldir_t);
 29 
 30 struct file_operations ext2_dir_operations = {
 31         read:           generic_read_dir,
 32         readdir:        ext2_readdir,
 33         ioctl:          ext2_ioctl,
 34         fsync:          ext2_sync_file,
 35 };
 36 
 37 int ext2_check_dir_entry (const char * function, struct inode * dir,
 38                           struct ext2_dir_entry_2 * de,
 39                           struct buffer_head * bh,
 40                           unsigned long offset)
 41 {
 42         const char * error_msg = NULL;
 43 
 44         if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
 45                 error_msg = "rec_len is smaller than minimal";
 46         else if (le16_to_cpu(de->rec_len) % 4 != 0)
 47                 error_msg = "rec_len % 4 != 0";
 48         else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
 49                 error_msg = "rec_len is too small for name_len";
 50         else if (dir && ((char *) de - bh->b_data) + le16_to_cpu(de->rec_len) >
 51                  dir->i_sb->s_blocksize)
 52                 error_msg = "directory entry across blocks";
 53         else if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
 54                 error_msg = "inode out of bounds";
 55 
 56         if (error_msg != NULL)
 57                 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
 58                             "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
 59                             dir->i_ino, error_msg, offset,
 60                             (unsigned long) le32_to_cpu(de->inode),
 61                             le16_to_cpu(de->rec_len), de->name_len);
 62         return error_msg == NULL ? 1 : 0;
 63 }
 64 
 65 static int ext2_readdir(struct file * filp,
 66                          void * dirent, filldir_t filldir)
 67 {
 68         int error = 0;
 69         unsigned long offset, blk;
 70         int i, num, stored;
 71         struct buffer_head * bh, * tmp, * bha[16];
 72         struct ext2_dir_entry_2 * de;
 73         struct super_block * sb;
 74         int err;
 75         struct inode *inode = filp->f_dentry->d_inode;
 76 
 77         sb = inode->i_sb;
 78 
 79         stored = 0;
 80         bh = NULL;
 81         offset = filp->f_pos & (sb->s_blocksize - 1);
 82 
 83         while (!error && !stored && filp->f_pos < inode->i_size) {
 84                 blk = (filp->f_pos) >> EXT2_BLOCK_SIZE_BITS(sb);
 85                 bh = ext2_bread (inode, blk, 0, &err);
 86                 if (!bh) {
 87                         ext2_error (sb, "ext2_readdir",
 88                                     "directory #%lu contains a hole at offset %lu",
 89                                     inode->i_ino, (unsigned long)filp->f_pos);
 90                         filp->f_pos += sb->s_blocksize - offset;
 91                         continue;
 92                 }
 93 
 94                 /*
 95                  * Do the readahead
 96                  */
 97                 if (!offset) {
 98                         for (i = 16 >> (EXT2_BLOCK_SIZE_BITS(sb) - 9), num = 0;
 99                              i > 0; i--) {
100                                 tmp = ext2_getblk (inode, ++blk, 0, &err);
101                                 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
102                                         bha[num++] = tmp;
103                                 else
104                                         brelse (tmp);
105                         }
106                         if (num) {
107                                 ll_rw_block (READA, num, bha);
108                                 for (i = 0; i < num; i++)
109                                         brelse (bha[i]);
110                         }
111                 }
112                 
113 revalidate:
114                 /* If the dir block has changed since the last call to
115                  * readdir(2), then we might be pointing to an invalid
116                  * dirent right now.  Scan from the start of the block
117                  * to make sure. */
118                 if (filp->f_version != inode->i_version) {
119                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
120                                 de = (struct ext2_dir_entry_2 *) 
121                                         (bh->b_data + i);
122                                 /* It's too expensive to do a full
123                                  * dirent test each time round this
124                                  * loop, but we do have to test at
125                                  * least that it is non-zero.  A
126                                  * failure will be detected in the
127                                  * dirent test below. */
128                                 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
129                                         break;
130                                 i += le16_to_cpu(de->rec_len);
131                         }
132                         offset = i;
133                         filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
134                                 | offset;
135                         filp->f_version = inode->i_version;
136                 }
137                 
138                 while (!error && filp->f_pos < inode->i_size 
139                        && offset < sb->s_blocksize) {
140                         de = (struct ext2_dir_entry_2 *) (bh->b_data + offset);
141                         if (!ext2_check_dir_entry ("ext2_readdir", inode, de,
142                                                    bh, offset)) {
143                                 /* On error, skip the f_pos to the
144                                    next block. */
145                                 filp->f_pos = (filp->f_pos | (sb->s_blocksize - 1))
146                                               + 1;
147                                 brelse (bh);
148                                 return stored;
149                         }
150                         offset += le16_to_cpu(de->rec_len);
151                         if (le32_to_cpu(de->inode)) {
152                                 /* We might block in the next section
153                                  * if the data destination is
154                                  * currently swapped out.  So, use a
155                                  * version stamp to detect whether or
156                                  * not the directory has been modified
157                                  * during the copy operation.
158                                  */
159                                 unsigned long version = filp->f_version;
160                                 unsigned char d_type = DT_UNKNOWN;
161 
162                                 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE)
163                                     && de->file_type < EXT2_FT_MAX)
164                                         d_type = ext2_filetype_table[de->file_type];
165                                 error = filldir(dirent, de->name,
166                                                 de->name_len,
167                                                 filp->f_pos, le32_to_cpu(de->inode),
168                                                 d_type);
169                                 if (error)
170                                         break;
171                                 if (version != filp->f_version)
172                                         goto revalidate;
173                                 stored ++;
174                         }
175                         filp->f_pos += le16_to_cpu(de->rec_len);
176                 }
177                 offset = 0;
178                 brelse (bh);
179         }
180         UPDATE_ATIME(inode);
181         return 0;
182 }
183 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.