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, 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/file.c
  12  *
  13  *  Copyright (C) 1991, 1992  Linus Torvalds
  14  *
  15  *  ext2 fs regular file handling primitives
  16  */
  17 
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 
  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 static int ext2_file_read (struct inode *, struct file *, char *, int);
  38 static int ext2_file_write (struct inode *, struct file *, char *, int);
  39 static void ext2_release_file (struct inode *, struct file *);
  40 
  41 /*
  42  * We have mostly NULL's here: the current defaults are ok for
  43  * the ext2 filesystem.
  44  */
  45 static struct file_operations ext2_file_operations = {
  46         NULL,                   /* lseek - default */
  47         ext2_file_read,         /* read */
  48         ext2_file_write,        /* write */
  49         NULL,                   /* readdir - bad */
  50         NULL,                   /* select - default */
  51         ext2_ioctl,             /* ioctl */
  52         generic_mmap,           /* mmap */
  53         NULL,                   /* no special open is needed */
  54         ext2_release_file,      /* release */
  55         ext2_sync_file,         /* fsync */
  56         NULL,                   /* fasync */
  57         NULL,                   /* check_media_change */
  58         NULL                    /* revalidate */
  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         NULL                    /* smap */
  78 };
  79 
  80 static int ext2_file_read (struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
  81                     char * buf, int count)
  82 {
  83         int read, left, chars;
  84         int block, blocks, offset;
  85         int bhrequest, uptodate;
  86         int clusterblocks;
  87         struct buffer_head ** bhb, ** bhe;
  88         struct buffer_head * bhreq[NBUF];
  89         struct buffer_head * buflist[NBUF];
  90         struct super_block * sb;
  91         unsigned int size;
  92         int err;
  93 
  94         if (!inode) {
  95                 printk ("ext2_file_read: inode = NULL\n");
  96                 return -EINVAL;
  97         }
  98         sb = inode->i_sb;
  99         if (!S_ISREG(inode->i_mode)) {
 100                 ext2_warning (sb, "ext2_file_read", "mode = %07o",
 101                               inode->i_mode);
 102                 return -EINVAL;
 103         }
 104         offset = filp->f_pos;
 105         size = inode->i_size;
 106         if (offset > size)
 107                 left = 0;
 108         else
 109                 left = size - offset;
 110         if (left > count)
 111                 left = count;
 112         if (left <= 0)
 113                 return 0;
 114         read = 0;
 115         block = offset >> EXT2_BLOCK_SIZE_BITS(sb);
 116         offset &= (sb->s_blocksize - 1);
 117         size = (size + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
 118         blocks = (left + offset + sb->s_blocksize - 1) >> EXT2_BLOCK_SIZE_BITS(sb);
 119         bhb = bhe = buflist;
 120         if (filp->f_reada) {
 121                 if (blocks < read_ahead[MAJOR(inode->i_dev)] >> (EXT2_BLOCK_SIZE_BITS(sb) - 9))
 122                     blocks = read_ahead[MAJOR(inode->i_dev)] >> (EXT2_BLOCK_SIZE_BITS(sb) - 9);
 123                 if (block + blocks > size)
 124                         blocks = size - block;
 125         }
 126 
 127         /*
 128          * We do this in a two stage process.  We first try and request
 129          * as many blocks as we can, then we wait for the first one to
 130          * complete, and then we try and wrap up as many as are actually
 131          * done.  This routine is rather generic, in that it can be used
 132          * in a filesystem by substituting the appropriate function in
 133          * for getblk
 134          *
 135          * This routine is optimized to make maximum use of the various
 136          * buffers and caches.
 137          */
 138 
 139         clusterblocks = 0;
 140 
 141         do {
 142                 bhrequest = 0;
 143                 uptodate = 1;
 144                 while (blocks) {
 145                         --blocks;
 146 #if 1
 147                         if(!clusterblocks) clusterblocks = ext2_getcluster(inode, block);
 148                         if(clusterblocks) clusterblocks--;
 149 #endif
 150 
 151                         *bhb = ext2_getblk (inode, block++, 0, &err);
 152                         if (*bhb && !(*bhb)->b_uptodate) {
 153                                 uptodate = 0;
 154                                 bhreq[bhrequest++] = *bhb;
 155                         }
 156 
 157                         if (++bhb == &buflist[NBUF])
 158                                 bhb = buflist;
 159 
 160                         /*
 161                          * If the block we have on hand is uptodate, go ahead
 162                          * and complete processing
 163                          */
 164                         if (uptodate)
 165                                 break;
 166 
 167                         if (bhb == bhe)
 168                                 break;
 169                 }
 170 
 171                 /*
 172                  * Now request them all
 173                  */
 174                 if (bhrequest)
 175                         ll_rw_block (READ, bhrequest, bhreq);
 176 
 177                 do {
 178                         /*
 179                          * Finish off all I/O that has actually completed
 180                          */
 181                         if (*bhe) {
 182                                 wait_on_buffer (*bhe);
 183                                 if (!(*bhe)->b_uptodate) { /* read error? */
 184                                         brelse(*bhe);
 185                                         if (++bhe == &buflist[NBUF])
 186                                           bhe = buflist;
 187                                         left = 0;
 188                                         break;
 189                                 }
 190                         }
 191                         if (left < sb->s_blocksize - offset)
 192                                 chars = left;
 193                         else
 194                                 chars = sb->s_blocksize - offset;
 195                         filp->f_pos += chars;
 196                         left -= chars;
 197                         read += chars;
 198                         if (*bhe) {
 199                                 memcpy_tofs (buf, offset + (*bhe)->b_data,
 200                                              chars);
 201                                 brelse (*bhe);
 202                                 buf += chars;
 203                         } else {
 204                                 while (chars-- > 0)
 205                                         put_fs_byte (0, buf++);
 206                         }
 207                         offset = 0;
 208                         if (++bhe == &buflist[NBUF])
 209                                 bhe = buflist;
 210                 } while (left > 0 && bhe != bhb && (!*bhe || !(*bhe)->b_lock));
 211         } while (left > 0);
 212 
 213         /*
 214          * Release the read-ahead blocks
 215          */
 216         while (bhe != bhb) {
 217                 brelse (*bhe);
 218                 if (++bhe == &buflist[NBUF])
 219                         bhe = buflist;
 220         }
 221         if (!read)
 222                 return -EIO;
 223         filp->f_reada = 1;
 224         if (!IS_RDONLY(inode)) {
 225                 inode->i_atime = CURRENT_TIME;
 226                 inode->i_dirt = 1;
 227         }
 228         return read;
 229 }
 230 
 231 static int ext2_file_write (struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 232                             char * buf, int count)
 233 {
 234         const loff_t two_gb = 2147483647;
 235         loff_t pos;
 236         off_t pos2;
 237         int written, c;
 238         struct buffer_head * bh, *bufferlist[NBUF];
 239         char * p;
 240         struct super_block * sb;
 241         int err;
 242         int i,buffercount,write_error;
 243 
 244         write_error = buffercount = 0;
 245         if (!inode) {
 246                 printk("ext2_file_write: inode = NULL\n");
 247                 return -EINVAL;
 248         }
 249         sb = inode->i_sb;
 250         if (sb->s_flags & MS_RDONLY)
 251                 /*
 252                  * This fs has been automatically remounted ro because of errors
 253                  */
 254                 return -ENOSPC;
 255 
 256         if (!S_ISREG(inode->i_mode)) {
 257                 ext2_warning (sb, "ext2_file_write", "mode = %07o",
 258                               inode->i_mode);
 259                 return -EINVAL;
 260         }
 261         down(&inode->i_sem);
 262         if (filp->f_flags & O_APPEND)
 263                 pos = inode->i_size;
 264         else
 265                 pos = filp->f_pos;
 266         pos2 = (off_t) pos;
 267         /*
 268          * If a file has been opened in synchronous mode, we have to ensure
 269          * that meta-data will also be written synchronously.  Thus, we
 270          * set the i_osync field.  This field is tested by the allocation
 271          * routines.
 272          */
 273         if (filp->f_flags & O_SYNC)
 274                 inode->u.ext2_i.i_osync++;
 275         written = 0;
 276         while (written < count) {
 277                 if (pos > two_gb) {
 278                         if (!written)
 279                                 written = -EFBIG;
 280                         break;
 281                 }
 282                 bh = ext2_getblk (inode, pos2 / sb->s_blocksize, 1, &err);
 283                 if (!bh) {
 284                         if (!written)
 285                                 written = err;
 286                         break;
 287                 }
 288                 c = sb->s_blocksize - (pos2 % sb->s_blocksize);
 289                 if (c > count-written)
 290                         c = count - written;
 291                 if (c != sb->s_blocksize && !bh->b_uptodate) {
 292                         ll_rw_block (READ, 1, &bh);
 293                         wait_on_buffer (bh);
 294                         if (!bh->b_uptodate) {
 295                                 brelse (bh);
 296                                 if (!written)
 297                                         written = -EIO;
 298                                 break;
 299                         }
 300                 }
 301                 p = (pos2 % sb->s_blocksize) + bh->b_data;
 302                 pos2 += c;
 303                 pos += c;
 304                 written += c;
 305                 memcpy_fromfs (p, buf, c);
 306                 buf += c;
 307                 bh->b_uptodate = 1;
 308                 mark_buffer_dirty(bh, 0);
 309                 if (filp->f_flags & O_SYNC)
 310                         bufferlist[buffercount++] = bh;
 311                 else
 312                         brelse(bh);
 313                 if (buffercount == NBUF){
 314                         ll_rw_block(WRITE, buffercount, bufferlist);
 315                         for(i=0; i<buffercount; i++){
 316                                 wait_on_buffer(bufferlist[i]);
 317                                 if (!bufferlist[i]->b_uptodate)
 318                                         write_error=1;
 319                                 brelse(bufferlist[i]);
 320                         }
 321                         buffercount=0;
 322                 }
 323                 if(write_error)
 324                         break;
 325         }
 326         if ( buffercount ){
 327                 ll_rw_block(WRITE, buffercount, bufferlist);
 328                 for(i=0; i<buffercount; i++){
 329                         wait_on_buffer(bufferlist[i]);
 330                         if (!bufferlist[i]->b_uptodate)
 331                                 write_error=1;
 332                         brelse(bufferlist[i]);
 333                 }
 334         }               
 335         if (pos > inode->i_size)
 336                 inode->i_size = pos;
 337         if (filp->f_flags & O_SYNC)
 338                 inode->u.ext2_i.i_osync--;
 339         up(&inode->i_sem);
 340         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
 341         filp->f_pos = pos;
 342         inode->i_dirt = 1;
 343         return written;
 344 }
 345 
 346 /*
 347  * Called when a inode is released. Note that this is different
 348  * from ext2_open: open gets called at every open, but release
 349  * gets called only when /all/ the files are closed.
 350  */
 351 static void ext2_release_file (struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353         if (filp->f_mode & 2)
 354                 ext2_discard_prealloc (inode);
 355 }

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