root/fs/ext2/file.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext2_file_read
  2. ext2_file_write
  3. ext2_release_file

   1 /*
   2  *  linux/fs/ext2/file.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/file.c
  11  *
  12  *  Copyright (C) 1991, 1992  Linus Torvalds
  13  *
  14  *  ext2 fs regular file handling primitives
  15  */
  16 
  17 #include <asm/segment.h>
  18 #include <asm/system.h>
  19 
  20 #include <linux/autoconf.h>
  21 #include <linux/errno.h>
  22 #include <linux/fs.h>
  23 #include <linux/ext2_fs.h>
  24 #include <linux/fcntl.h>
  25 #include <linux/sched.h>
  26 #include <linux/stat.h>
  27 #include <linux/locks.h>
  28 
  29 #define NBUF    32
  30 
  31 #define MIN(a,b) (((a)<(b))?(a):(b))
  32 #define MAX(a,b) (((a)>(b))?(a):(b))
  33 
  34 #include <linux/fs.h>
  35 #include <linux/ext2_fs.h>
  36 
  37 #ifndef CONFIG_EXT2_FS_DIR_READ
  38 static
  39 #endif
  40 int ext2_file_read (struct inode *, struct file *, char *, int);
  41 static int ext2_file_write (struct inode *, struct file *, char *, int);
  42 static void ext2_release_file (struct inode *, struct file *);
  43 
  44 /*
  45  * We have mostly NULL's here: the current defaults are ok for
  46  * the ext2 filesystem.
  47  */
  48 static struct file_operations ext2_file_operations = {
  49         NULL,                   /* lseek - default */
  50         ext2_file_read,         /* read */
  51         ext2_file_write,        /* write */
  52         NULL,                   /* readdir - bad */
  53         NULL,                   /* select - default */
  54         ext2_ioctl,             /* ioctl */
  55         generic_mmap,           /* mmap */
  56         NULL,                   /* no special open is needed */
  57         ext2_release_file,      /* release */
  58         ext2_sync_file          /* fsync */
  59 };
  60 
  61 struct inode_operations ext2_file_inode_operations = {
  62         &ext2_file_operations,/* default file operations */
  63         NULL,                   /* create */
  64         NULL,                   /* lookup */
  65         NULL,                   /* link */
  66         NULL,                   /* unlink */
  67         NULL,                   /* symlink */
  68         NULL,                   /* mkdir */
  69         NULL,                   /* rmdir */
  70         NULL,                   /* mknod */
  71         NULL,                   /* rename */
  72         NULL,                   /* readlink */
  73         NULL,                   /* follow_link */
  74         ext2_bmap,              /* bmap */
  75         ext2_truncate,          /* truncate */
  76         ext2_permission         /* permission */
  77 };
  78 
  79 #ifndef CONFIG_EXT2_FS_DIR_READ
  80 static
  81 #endif
  82 int ext2_file_read (struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
  83                     char * buf, int count)
  84 {
  85         int read, left, chars;
  86         int block, blocks, offset;
  87         int bhrequest, uptodate;
  88         struct buffer_head ** bhb, ** bhe;
  89         struct buffer_head * bhreq[NBUF];
  90         struct buffer_head * buflist[NBUF];
  91         struct super_block * sb;
  92         unsigned int size;
  93         int err;
  94 
  95         if (!inode) {
  96                 printk ("ext2_file_read: inode = NULL\n");
  97                 return -EINVAL;
  98         }
  99         sb = inode->i_sb;
 100         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) {
 101                 ext2_warning (sb, "ext2_file_read", "mode = %07o",
 102                               inode->i_mode);
 103                 return -EINVAL;
 104         }
 105         offset = filp->f_pos;
 106         size = inode->i_size;
 107         if (offset > size)
 108                 left = 0;
 109         else
 110                 left = size - offset;
 111         if (left > count)
 112                 left = count;
 113         if (left <= 0)
 114                 return 0;
 115         read = 0;
 116         block = offset >> EXT2_BLOCK_SIZE_BITS(sb);
 117         offset &= (sb->s_blocksize - 1);
 118         size = (size + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
 119         blocks = (left + offset + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
 120         bhb = bhe = buflist;
 121         if (filp->f_reada) {
 122                 blocks += read_ahead[MAJOR(inode->i_dev)] >>
 123                         (EXT2_BLOCK_SIZE_BITS(sb) - 9);
 124                 if (block + blocks > size)
 125                         blocks = size - block;
 126         }
 127 
 128         /*
 129          * We do this in a two stage process.  We first try and request
 130          * as many blocks as we can, then we wait for the first one to
 131          * complete, and then we try and wrap up as many as are actually
 132          * done.  This routine is rather generic, in that it can be used
 133          * in a filesystem by substituting the appropriate function in
 134          * for getblk
 135          *
 136          * This routine is optimized to make maximum use of the various
 137          * buffers and caches.
 138          */
 139 
 140         do {
 141                 bhrequest = 0;
 142                 uptodate = 1;
 143                 while (blocks) {
 144                         --blocks;
 145                         *bhb = ext2_getblk (inode, block++, 0, &err);
 146                         if (*bhb && !(*bhb)->b_uptodate) {
 147                                 uptodate = 0;
 148                                 bhreq[bhrequest++] = *bhb;
 149                         }
 150 
 151                         if (++bhb == &buflist[NBUF])
 152                                 bhb = buflist;
 153 
 154                         /*
 155                          * If the block we have on hand is uptodate, go ahead
 156                          * and complete processing
 157                          */
 158                         if (uptodate)
 159                                 break;
 160 
 161                         if (bhb == bhe)
 162                                 break;
 163                 }
 164 
 165                 /*
 166                  * Now request them all
 167                  */
 168                 if (bhrequest)
 169                         ll_rw_block (READ, bhrequest, bhreq);
 170 
 171                 do {
 172                         /*
 173                          * Finish off all I/O that has actually completed
 174                          */
 175                         if (*bhe) {
 176                                 wait_on_buffer (*bhe);
 177                                 if (!(*bhe)->b_uptodate) { /* read error? */
 178                                         brelse(*bhe);
 179                                         if (++bhe == &buflist[NBUF])
 180                                           bhe = buflist;
 181                                         left = 0;
 182                                         break;
 183                                 }
 184                         }
 185                         if (left < sb->s_blocksize - offset)
 186                                 chars = left;
 187                         else
 188                                 chars = sb->s_blocksize - offset;
 189                         filp->f_pos += chars;
 190                         left -= chars;
 191                         read += chars;
 192                         if (*bhe) {
 193                                 memcpy_tofs (buf, offset + (*bhe)->b_data,
 194                                              chars);
 195                                 brelse (*bhe);
 196                                 buf += chars;
 197                         } else {
 198                                 while (chars-- > 0)
 199                                         put_fs_byte (0, buf++);
 200                         }
 201                         offset = 0;
 202                         if (++bhe == &buflist[NBUF])
 203                                 bhe = buflist;
 204                 } while (left > 0 && bhe != bhb && (!*bhe || !(*bhe)->b_lock));
 205         } while (left > 0);
 206 
 207         /*
 208          * Release the read-ahead blocks
 209          */
 210         while (bhe != bhb) {
 211                 brelse (*bhe);
 212                 if (++bhe == &buflist[NBUF])
 213                         bhe = buflist;
 214         }
 215         if (!read)
 216                 return -EIO;
 217         filp->f_reada = 1;
 218         if (!IS_RDONLY(inode)) {
 219                 inode->i_atime = CURRENT_TIME;
 220                 inode->i_dirt = 1;
 221         }
 222         return read;
 223 }
 224 
 225 static int ext2_file_write (struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 226                             char * buf, int count)
 227 {
 228         off_t pos;
 229         int written, c;
 230         struct buffer_head * bh;
 231         char * p;
 232         struct super_block * sb;
 233         int err;
 234 
 235         if (!inode) {
 236                 printk("ext2_file_write: inode = NULL\n");
 237                 return -EINVAL;
 238         }
 239         sb = inode->i_sb;
 240         if (sb->s_flags & MS_RDONLY)
 241                 /*
 242                  * This fs has been automatically remounted ro because of errors
 243                  */
 244                 return -ENOSPC;
 245 
 246         if (!S_ISREG(inode->i_mode)) {
 247                 ext2_warning (sb, "ext2_file_write", "mode = %07o\n",
 248                               inode->i_mode);
 249                 return -EINVAL;
 250         }
 251 /*
 252  * ok, append may not work when many processes are writing at the same time
 253  * but so what. That way leads to madness anyway.
 254  */
 255         if (filp->f_flags & O_APPEND)
 256                 pos = inode->i_size;
 257         else
 258                 pos = filp->f_pos;
 259         written = 0;
 260         while (written < count) {
 261                 bh = ext2_getblk (inode, pos / sb->s_blocksize, 1, &err);
 262                 if (!bh) {
 263                         if (!written)
 264                                 written = err;
 265                         break;
 266                 }
 267                 c = sb->s_blocksize - (pos % sb->s_blocksize);
 268                 if (c > count-written)
 269                         c = count - written;
 270                 if (c != sb->s_blocksize && !bh->b_uptodate) {
 271                         ll_rw_block (READ, 1, &bh);
 272                         wait_on_buffer (bh);
 273                         if (!bh->b_uptodate) {
 274                                 brelse (bh);
 275                                 if (!written)
 276                                         written = -EIO;
 277                                 break;
 278                         }
 279                 }
 280                 p = (pos % sb->s_blocksize) + bh->b_data;
 281                 pos += c;
 282                 if (pos > inode->i_size) {
 283                         inode->i_size = pos;
 284                         inode->i_dirt = 1;
 285                 }
 286                 written += c;
 287                 memcpy_fromfs (p, buf, c);
 288                 buf += c;
 289                 bh->b_uptodate = 1;
 290                 bh->b_dirt = 1;
 291                 brelse (bh);
 292         }
 293         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
 294         filp->f_pos = pos;
 295         inode->i_dirt = 1;
 296         return written;
 297 }
 298 
 299 /*
 300  * Called when a inode is released. Note that this is different
 301  * from ext2_open: open gets called at every open, but release
 302  * gets called only when /all/ the files are closed.
 303  */
 304 static void ext2_release_file (struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306         if (filp->f_mode & 2)
 307                 ext2_discard_prealloc (inode);
 308 }

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