root/fs/ncpfs/file.c

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

DEFINITIONS

This source file includes following definitions.
  1. min
  2. ncp_fsync
  3. ncp_make_open
  4. ncp_file_read
  5. ncp_file_write

   1 /*
   2  *  file.c
   3  *
   4  *  Copyright (C) 1995, 1996 by Volker Lendecke
   5  *
   6  */
   7 
   8 #include <asm/segment.h>
   9 #include <asm/system.h>
  10 
  11 #include <linux/sched.h>
  12 #include <linux/kernel.h>
  13 #include <linux/errno.h>
  14 #include <linux/fcntl.h>
  15 #include <linux/stat.h>
  16 #include <linux/mm.h>
  17 #include <linux/ncp_fs.h>
  18 #include "ncplib_kernel.h"
  19 #include <linux/malloc.h>
  20 
  21 static inline int min(int a, int b)
     /* [previous][next][first][last][top][bottom][index][help] */
  22 {
  23         return a<b ? a : b;
  24 }
  25 
  26 static int 
  27 ncp_fsync(struct inode *inode, struct file *file)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         return 0;
  30 }
  31 
  32 int
  33 ncp_make_open(struct inode *i, int right)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35         struct nw_file_info *finfo;
  36 
  37         if (i == NULL)
  38         {
  39                 printk("ncp_make_open: got NULL inode\n");
  40                 return -EINVAL;
  41         }
  42 
  43         finfo = NCP_FINFO(i);
  44 
  45         DPRINTK("ncp_make_open: dirent->opened = %d\n", finfo->opened);
  46 
  47         if (finfo->opened == 0)
  48         {
  49                 /* tries max. rights */
  50                 if (ncp_open_create_file_or_subdir(NCP_SERVER(i),
  51                                                    NULL, NULL,
  52                                                    OC_MODE_OPEN, 0,
  53                                                    AR_READ | AR_WRITE,
  54                                                    finfo) == 0)
  55                 {
  56                         finfo->access = O_RDWR;
  57                 }
  58                 else if (ncp_open_create_file_or_subdir(NCP_SERVER(i),
  59                                                         NULL, NULL,
  60                                                         OC_MODE_OPEN, 0,
  61                                                         AR_READ,
  62                                                         finfo) == 0)
  63                 {
  64                         finfo->access = O_RDONLY;
  65                 }
  66                 else
  67                 {
  68                         return -EACCES;
  69                 }
  70         }
  71 
  72         if (   ((right == O_RDONLY) && (   (finfo->access == O_RDONLY)
  73                                         || (finfo->access == O_RDWR)))
  74             || ((right == O_WRONLY) && (   (finfo->access == O_WRONLY)
  75                                         || (finfo->access == O_RDWR)))
  76             || ((right == O_RDWR)   && (finfo->access == O_RDWR)))
  77                 return 0;
  78 
  79         return -EACCES;
  80 }
  81 
  82 static int 
  83 ncp_file_read(struct inode *inode, struct file *file, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85         int bufsize, already_read;
  86         off_t pos;
  87         int errno;
  88 
  89         DPRINTK("ncp_file_read: enter %s\n", NCP_ISTRUCT(inode)->entryName);
  90         
  91         if (inode == NULL)
  92         {
  93                 DPRINTK("ncp_file_read: inode = NULL\n");
  94                 return -EINVAL;
  95         }
  96         if (!ncp_conn_valid(NCP_SERVER(inode)))
  97         {
  98                 return -EIO;
  99         }
 100 
 101         if (!S_ISREG(inode->i_mode))
 102         {
 103                 DPRINTK("ncp_file_read: read from non-file, mode %07o\n",
 104                         inode->i_mode);
 105                 return -EINVAL;
 106         }
 107 
 108         pos = file->f_pos;
 109 
 110         if (pos + count > inode->i_size)
 111         {
 112                 count = inode->i_size - pos;
 113         }
 114 
 115         if (count <= 0)
 116         {
 117                 return 0;
 118         }
 119 
 120         if ((errno = ncp_make_open(inode, O_RDONLY)) != 0)
 121         {
 122                 return errno;
 123         }
 124         
 125         bufsize = NCP_SERVER(inode)->buffer_size;
 126 
 127         already_read = 0;
 128 
 129         /* First read in as much as possible for each bufsize. */
 130         while (already_read < count)
 131         {
 132                 int read_this_time;
 133                 int to_read = min(bufsize - (pos % bufsize),
 134                                   count - already_read);
 135 
 136                 if (ncp_read(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
 137                              pos, to_read, buf, &read_this_time) != 0)
 138                 {
 139                         return -EIO; /* This is not exact, i know.. */
 140                 }
 141 
 142                 pos += read_this_time;
 143                 buf += read_this_time;
 144                 already_read += read_this_time;
 145 
 146                 if (read_this_time < to_read)
 147                 {
 148                         break;
 149                 }
 150         }
 151 
 152         file->f_pos = pos;
 153 
 154         if (!IS_RDONLY(inode))
 155         {
 156                 inode->i_atime = CURRENT_TIME;
 157         }
 158 
 159         inode->i_dirt = 1;
 160 
 161         DPRINTK("ncp_file_read: exit %s\n", NCP_ISTRUCT(inode)->entryName);
 162 
 163         return already_read;
 164 }
 165 
 166 static int 
 167 ncp_file_write(struct inode *inode, struct file *file, const char *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
 168                int count)
 169 {
 170         int bufsize, already_written;
 171         off_t pos;
 172         int errno;
 173                           
 174         if (inode == NULL)
 175         {
 176                 DPRINTK("ncp_file_write: inode = NULL\n");
 177                 return -EINVAL;
 178         }
 179         if (!ncp_conn_valid(NCP_SERVER(inode)))
 180         {
 181                 return -EIO;
 182         }
 183 
 184         if (!S_ISREG(inode->i_mode))
 185         {
 186                 DPRINTK("ncp_file_write: write to non-file, mode %07o\n",
 187                        inode->i_mode);
 188                 return -EINVAL;
 189         }
 190 
 191         DPRINTK("ncp_file_write: enter %s\n", NCP_ISTRUCT(inode)->entryName);
 192 
 193         if (count <= 0)
 194         {
 195                 return 0;
 196         }
 197 
 198         if ((errno = ncp_make_open(inode, O_RDWR)) != 0)
 199         {
 200                 return errno;
 201         }
 202         
 203         pos = file->f_pos;
 204 
 205         if (file->f_flags & O_APPEND)
 206         {
 207                 pos = inode->i_size;
 208         }
 209 
 210         bufsize = NCP_SERVER(inode)->buffer_size;
 211 
 212         already_written = 0;
 213 
 214         while (already_written < count)
 215         {
 216                 int written_this_time;
 217                 int to_write = min(bufsize - (pos % bufsize),
 218                                    count - already_written);
 219 
 220                 if (ncp_write(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
 221                               pos, to_write, buf, &written_this_time) != 0)
 222                 {
 223                         return -EIO;
 224                 }
 225 
 226                 pos += written_this_time;
 227                 buf += written_this_time;
 228                 already_written += written_this_time;
 229 
 230                 if (written_this_time < to_write)
 231                 {
 232                         break;
 233                 }
 234         }
 235 
 236         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 237         inode->i_dirt = 1;
 238 
 239         file->f_pos = pos;
 240 
 241         if (pos > inode->i_size)
 242         {
 243                 inode->i_size = pos;
 244                 ncp_invalid_dir_cache(NCP_INOP(inode)->dir->inode);
 245         }
 246 
 247         DPRINTK("ncp_file_write: exit %s\n", NCP_ISTRUCT(inode)->entryName);
 248 
 249         return already_written;
 250 }
 251 
 252 static struct file_operations ncp_file_operations = {
 253         NULL,                   /* lseek - default */
 254         ncp_file_read,          /* read */
 255         ncp_file_write,         /* write */
 256         NULL,                   /* readdir - bad */
 257         NULL,                   /* select - default */
 258         ncp_ioctl,              /* ioctl */
 259         ncp_mmap,               /* mmap */
 260         NULL,                   /* open */
 261         NULL,                   /* release */
 262         ncp_fsync,              /* fsync */
 263 };
 264 
 265 struct inode_operations ncp_file_inode_operations = {
 266         &ncp_file_operations,   /* default file operations */
 267         NULL,                   /* create */
 268         NULL,                   /* lookup */
 269         NULL,                   /* link */
 270         NULL,                   /* unlink */
 271         NULL,                   /* symlink */
 272         NULL,                   /* mkdir */
 273         NULL,                   /* rmdir */
 274         NULL,                   /* mknod */
 275         NULL,                   /* rename */
 276         NULL,                   /* readlink */
 277         NULL,                   /* follow_link */
 278         NULL,                   /* bmap */
 279         NULL                    /* truncate */
 280 };

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