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

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