root/fs/sysv/dir.c

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

DEFINITIONS

This source file includes following definitions.
  1. sysv_dir_read
  2. sysv_readdir1
  3. sysv_readdir

   1 /*
   2  *  linux/fs/sysv/dir.c
   3  *
   4  *  minix/dir.c
   5  *  Copyright (C) 1991, 1992  Linus Torvalds
   6  *
   7  *  coh/dir.c
   8  *  Copyright (C) 1993  Pascal Haible, Bruno Haible
   9  *
  10  *  sysv/dir.c
  11  *  Copyright (C) 1993  Bruno Haible
  12  *
  13  *  SystemV/Coherent directory handling functions
  14  */
  15 
  16 #include <asm/segment.h>
  17 
  18 #include <linux/errno.h>
  19 #include <linux/fs.h>
  20 #include <linux/sysv_fs.h>
  21 #include <linux/stat.h>
  22 
  23 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
  24 #define ROUND_UP(x) (((x)+3) & ~3)
  25 
  26 static int sysv_dir_read(struct inode * inode, struct file * filp, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28         return -EISDIR;
  29 }
  30 
  31 static int sysv_readdir(struct inode *, struct file *, struct dirent *, int);
  32 
  33 static struct file_operations sysv_dir_operations = {
  34         NULL,                   /* lseek - default */
  35         sysv_dir_read,          /* read */
  36         NULL,                   /* write - bad */
  37         sysv_readdir,           /* readdir */
  38         NULL,                   /* select - default */
  39         NULL,                   /* ioctl - default */
  40         NULL,                   /* mmap */
  41         NULL,                   /* no special open code */
  42         NULL,                   /* no special release code */
  43         file_fsync              /* default fsync */
  44 };
  45 
  46 /*
  47  * directories can handle most operations...
  48  */
  49 struct inode_operations sysv_dir_inode_operations = {
  50         &sysv_dir_operations,   /* default directory file-ops */
  51         sysv_create,            /* create */
  52         sysv_lookup,            /* lookup */
  53         sysv_link,              /* link */
  54         sysv_unlink,            /* unlink */
  55         sysv_symlink,           /* symlink */
  56         sysv_mkdir,             /* mkdir */
  57         sysv_rmdir,             /* rmdir */
  58         sysv_mknod,             /* mknod */
  59         sysv_rename,            /* rename */
  60         NULL,                   /* readlink */
  61         NULL,                   /* follow_link */
  62         NULL,                   /* bmap */
  63         sysv_truncate,          /* truncate */
  64         NULL                    /* permission */
  65 };
  66 
  67 static int sysv_readdir1 (struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
  68         struct dirent * dirent)
  69 {
  70         struct super_block * sb;
  71         unsigned int offset,i;
  72         char c;
  73         struct buffer_head * bh;
  74         char* bh_data;
  75         struct sysv_dir_entry * de;
  76 
  77         if (!inode || !(sb = inode->i_sb) || !S_ISDIR(inode->i_mode))
  78                 return -EBADF;
  79         if ((unsigned long)(filp->f_pos) % SYSV_DIRSIZE)
  80                 return -EBADF;
  81         while (filp->f_pos < inode->i_size) {
  82                 offset = filp->f_pos & sb->sv_block_size_1;
  83                 bh = sysv_file_bread(inode, filp->f_pos >> sb->sv_block_size_bits, 0, &bh_data);
  84                 if (!bh) {
  85                         filp->f_pos += sb->sv_block_size - offset;
  86                         continue;
  87                 }
  88                 while (offset < sb->sv_block_size && filp->f_pos < inode->i_size) {
  89                         de = (struct sysv_dir_entry *) (offset + bh_data);
  90                         offset += SYSV_DIRSIZE;
  91                         filp->f_pos += SYSV_DIRSIZE;
  92                         if (de->inode) {
  93                                 struct sysv_dir_entry sde;
  94 
  95                                 /* Copy the directory entry first, because the directory
  96                                  * might be modified while we sleep in put_fs_byte...
  97                                  */
  98                                 memcpy(&sde, de, sizeof(struct sysv_dir_entry));
  99 
 100                                 for (i = 0; i < SYSV_NAMELEN; i++)
 101                                         if ((c = sde.name[i]) != 0)
 102                                                 put_fs_byte(c,i+dirent->d_name);
 103                                         else
 104                                                 break;
 105                                 if (i) {
 106                                         if (sde.inode > inode->i_sb->sv_ninodes)
 107                                                 printk("sysv_readdir: Bad inode number on dev 0x%04x, ino %ld, offset 0x%04lx: %d is out of range\n",
 108                                                         inode->i_dev, inode->i_ino, (off_t) filp->f_pos - SYSV_DIRSIZE, sde.inode);
 109                                         put_fs_long(sde.inode,&dirent->d_ino);
 110                                         put_fs_byte(0,i+dirent->d_name);
 111                                         put_fs_word(i,&dirent->d_reclen);
 112                                         brelse(bh);
 113                                         return ROUND_UP(NAME_OFFSET(dirent)+i+1);
 114                                 }
 115                         }
 116                 }
 117                 brelse(bh);
 118         }
 119         return 0;
 120 }
 121 
 122 static int sysv_readdir(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 123         struct dirent * dirent, int count)
 124 {
 125         int retval, stored;
 126 
 127         /* compatibility */
 128         if (count==1)
 129                 return sysv_readdir1(inode,filp,dirent);
 130 
 131         retval = verify_area(VERIFY_WRITE, dirent, count);
 132         if (retval)
 133                 return retval;
 134 
 135         stored = 0;
 136         while (count >= sizeof(struct dirent)) {
 137                 retval = sysv_readdir1(inode,filp,dirent);
 138                 if (retval < 0)
 139                         return retval;
 140                 if (!retval)
 141                         return stored;
 142                 dirent = (struct dirent *)((char *) dirent + retval);
 143                 stored += retval;
 144                 count -= retval;
 145         }
 146         return stored;
 147 }

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