root/fs/msdos/file.c

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

DEFINITIONS

This source file includes following definitions.
  1. msdos_file_read
  2. msdos_file_write
  3. msdos_truncate

   1 /*
   2  *  linux/fs/msdos/file.c
   3  *
   4  *  Written 1992 by Werner Almesberger
   5  *
   6  *  MS-DOS regular file handling primitives
   7  */
   8 
   9 #include <asm/segment.h>
  10 #include <asm/system.h>
  11 
  12 #include <linux/sched.h>
  13 #include <linux/fs.h>
  14 #include <linux/msdos_fs.h>
  15 #include <linux/errno.h>
  16 #include <linux/fcntl.h>
  17 #include <linux/stat.h>
  18 
  19 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  20 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  21 
  22 static int msdos_file_read(struct inode *inode,struct file *filp,char *buf,
  23     int count);
  24 static int msdos_file_write(struct inode *inode,struct file *filp,char *buf,
  25     int count);
  26 
  27 
  28 static struct file_operations msdos_file_operations = {
  29         NULL,                   /* lseek - default */
  30         msdos_file_read,        /* read */
  31         msdos_file_write,       /* write */
  32         NULL,                   /* readdir - bad */
  33         NULL,                   /* select - default */
  34         NULL,                   /* ioctl - default */
  35         NULL,                   /* mmap */
  36         NULL,                   /* no special open is needed */
  37         NULL                    /* release */
  38 };
  39 
  40 struct inode_operations msdos_file_inode_operations = {
  41         &msdos_file_operations, /* default file operations */
  42         NULL,                   /* create */
  43         NULL,                   /* lookup */
  44         NULL,                   /* link */
  45         NULL,                   /* unlink */
  46         NULL,                   /* symlink */
  47         NULL,                   /* mkdir */
  48         NULL,                   /* rmdir */
  49         NULL,                   /* mknod */
  50         NULL,                   /* rename */
  51         NULL,                   /* readlink */
  52         NULL,                   /* follow_link */
  53         msdos_bmap,             /* bmap */
  54         msdos_truncate          /* truncate */
  55 };
  56 
  57 /* No bmap for MS-DOS FS' that don't align data at kByte boundaries. */
  58 
  59 struct inode_operations msdos_file_inode_operations_no_bmap = {
  60         &msdos_file_operations, /* default file operations */
  61         NULL,                   /* create */
  62         NULL,                   /* lookup */
  63         NULL,                   /* link */
  64         NULL,                   /* unlink */
  65         NULL,                   /* symlink */
  66         NULL,                   /* mkdir */
  67         NULL,                   /* rmdir */
  68         NULL,                   /* mknod */
  69         NULL,                   /* rename */
  70         NULL,                   /* readlink */
  71         NULL,                   /* follow_link */
  72         NULL,                   /* bmap */
  73         msdos_truncate          /* truncate */
  74 };
  75 
  76 
  77 static int msdos_file_read(struct inode *inode,struct file *filp,char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
  78     int count)
  79 {
  80         char *start;
  81         int left,offset,size,sector,cnt;
  82         char ch;
  83         struct buffer_head *bh;
  84         void *data;
  85 
  86 /* printk("msdos_file_read\n"); */
  87         if (!inode) {
  88                 printk("msdos_file_read: inode = NULL\n");
  89                 return -EINVAL;
  90         }
  91         if (!S_ISREG(inode->i_mode)) {
  92                 printk("msdos_file_read: mode = %07o\n",inode->i_mode);
  93                 return -EINVAL;
  94         }
  95         if (filp->f_pos >= inode->i_size || count <= 0) return 0;
  96         start = buf;
  97         while ((left = MIN(inode->i_size-filp->f_pos,count-(buf-start))) > 0){
  98                 if (!(sector = msdos_smap(inode,filp->f_pos >> SECTOR_BITS)))
  99                         break;
 100                 offset = filp->f_pos & (SECTOR_SIZE-1);
 101                 if (!(bh = msdos_sread(inode->i_dev,sector,&data))) break;
 102                 filp->f_pos += (size = MIN(SECTOR_SIZE-offset,left));
 103                 if (MSDOS_I(inode)->i_binary) {
 104                         memcpy_tofs(buf,data+offset,size);
 105                         buf += size;
 106                 }
 107                 else for (cnt = size; cnt; cnt--) {
 108                                 if ((ch = *((char *) data+offset++)) == '\r')
 109                                         size--;
 110                                 else {
 111                                         if (ch != 26) put_fs_byte(ch,buf++);
 112                                         else {
 113                                                 filp->f_pos = inode->i_size;
 114                                                 brelse(bh);
 115                                                 return buf-start;
 116                                         }
 117                                 }
 118                         }
 119                 brelse(bh);
 120         }
 121         if (start == buf) return -EIO;
 122         return buf-start;
 123 }
 124 
 125 
 126 static int msdos_file_write(struct inode *inode,struct file *filp,char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
 127     int count)
 128 {
 129         int sector,offset,size,left,written;
 130         int error,carry;
 131         char *start,*to,ch;
 132         struct buffer_head *bh;
 133         void *data;
 134 
 135         if (!inode) {
 136                 printk("msdos_file_write: inode = NULL\n");
 137                 return -EINVAL;
 138         }
 139         if (!S_ISREG(inode->i_mode)) {
 140                 printk("msdos_file_write: mode = %07o\n",inode->i_mode);
 141                 return -EINVAL;
 142         }
 143 /*
 144  * ok, append may not work when many processes are writing at the same time
 145  * but so what. That way leads to madness anyway.
 146  */
 147         if (filp->f_flags & O_APPEND) filp->f_pos = inode->i_size;
 148         if (count <= 0) return 0;
 149         error = carry = 0;
 150         for (start = buf; count || carry; count -= size) {
 151                 while (!(sector = msdos_smap(inode,filp->f_pos >> SECTOR_BITS)))
 152                         if ((error = msdos_add_cluster(inode)) < 0) break;
 153                 if (error) {
 154                         msdos_truncate(inode);
 155                         break;
 156                 }
 157                 offset = filp->f_pos & (SECTOR_SIZE-1);
 158                 size = MIN(SECTOR_SIZE-offset,MAX(carry,count));
 159                 if (!(bh = msdos_sread(inode->i_dev,sector,&data))) {
 160                         error = -EIO;
 161                         break;
 162                 }
 163                 if (MSDOS_I(inode)->i_binary) {
 164                         memcpy_fromfs(data+(filp->f_pos & (SECTOR_SIZE-1)),
 165                             buf,written = size);
 166                         buf += size;
 167                 }
 168                 else {
 169                         written = left = SECTOR_SIZE-offset;
 170                         to = data+(filp->f_pos & (SECTOR_SIZE-1));
 171                         if (carry) {
 172                                 *to++ = '\n';
 173                                 left--;
 174                                 carry = 0;
 175                         }
 176                         for (size = 0; size < count && left; size++) {
 177                                 if ((ch = get_fs_byte(buf++)) == '\n') {
 178                                         *to++ = '\r';
 179                                         left--;
 180                                 }
 181                                 if (!left) carry = 1;
 182                                 else {
 183                                         *to++ = ch;
 184                                         left--;
 185                                 }
 186                         }
 187                         written -= left;
 188                 }
 189                 filp->f_pos += written;
 190                 if (filp->f_pos > inode->i_size) {
 191                         inode->i_size = filp->f_pos;
 192                         inode->i_dirt = 1;
 193                 }
 194                 bh->b_dirt = 1;
 195                 brelse(bh);
 196         }
 197         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 198         MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
 199         inode->i_dirt = 1;
 200         return start == buf ? error : buf-start;
 201 }
 202 
 203 
 204 void msdos_truncate(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         int cluster;
 207 
 208         cluster = SECTOR_SIZE*MSDOS_SB(inode->i_sb)->cluster_size;
 209         (void) fat_free(inode,(inode->i_size+(cluster-1))/cluster);
 210         MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
 211         inode->i_dirt = 1;
 212 }

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