Fork me on GitHub

root/fs/procfs/file.c

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

DEFINITIONS

This source file includes following definitions.
  1. procfs_file_open
  2. procfs_file_close
  3. procfs_file_read
  4. procfs_file_lseek

   1 /*
   2  * fiwix/fs/procfs/file.c
   3  *
   4  * Copyright 2018-2021, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/types.h>
   9 #include <fiwix/errno.h>
  10 #include <fiwix/fs.h>
  11 #include <fiwix/filesystems.h>
  12 #include <fiwix/fs_proc.h>
  13 #include <fiwix/fcntl.h>
  14 #include <fiwix/mm.h>
  15 #include <fiwix/stdio.h>
  16 #include <fiwix/string.h>
  17 
  18 struct fs_operations procfs_file_fsop = {
  19         0,
  20         0,
  21 
  22         procfs_file_open,
  23         procfs_file_close,
  24         procfs_file_read,
  25         NULL,                   /* write */
  26         NULL,                   /* ioctl */
  27         procfs_file_lseek,
  28         NULL,                   /* readdir */
  29         NULL,                   /* mmap */
  30         NULL,                   /* select */
  31 
  32         NULL,                   /* readlink */
  33         NULL,                   /* followlink */
  34         procfs_bmap,
  35         NULL,                   /* lookup */
  36         NULL,                   /* rmdir */
  37         NULL,                   /* link */
  38         NULL,                   /* unlink */
  39         NULL,                   /* symlink */
  40         NULL,                   /* mkdir */
  41         NULL,                   /* mknod */
  42         NULL,                   /* truncate */
  43         NULL,                   /* create */
  44         NULL,                   /* rename */
  45 
  46         NULL,                   /* read_block */
  47         NULL,                   /* write_block */
  48 
  49         NULL,                   /* read_inode */
  50         NULL,                   /* write_inode */
  51         NULL,                   /* ialloc */
  52         NULL,                   /* ifree */
  53         NULL,                   /* statfs */
  54         NULL,                   /* read_superblock */
  55         NULL,                   /* remount_fs */
  56         NULL,                   /* write_superblock */
  57         NULL                    /* release_superblock */
  58 };
  59 
  60 int procfs_file_open(struct inode *i, struct fd *fd_table)
  61 {
  62         if(fd_table->flags & (O_WRONLY | O_RDWR | O_TRUNC | O_APPEND)) {
  63                 return -EINVAL;
  64         }
  65         fd_table->offset = 0;
  66         return 0;
  67 }
  68 
  69 int procfs_file_close(struct inode *i, struct fd *fd_table)
  70 {
  71         return 0;
  72 }
  73 
  74 int procfs_file_read(struct inode *i, struct fd *fd_table, char *buffer, __size_t count)
  75 {
  76         __off_t total_read;
  77         unsigned int boffset, bytes, size;
  78         int blksize;
  79         struct procfs_dir_entry *d;
  80         char *buf;
  81 
  82         if(!(d = get_procfs_by_inode(i))) {
  83                 return -EINVAL;
  84         }
  85         if(!d->data_fn) {
  86                 return -EINVAL;
  87         }
  88         if(!(buf = (void *)kmalloc())) {
  89                 return -ENOMEM;
  90         }
  91 
  92         size = d->data_fn(buf, (i->inode >> 12) & 0xFFFF);
  93         blksize = i->sb->s_blocksize;
  94         if(fd_table->offset > size) {
  95                 fd_table->offset = size;
  96         }
  97 
  98         total_read = 0;
  99 
 100         for(;;) {
 101                 count = (fd_table->offset + count > size) ? size - fd_table->offset : count;
 102                 if(!count) {
 103                         break;
 104                 }
 105 
 106                 boffset = fd_table->offset % blksize;
 107                 bytes = blksize - boffset;
 108                 bytes = MIN(bytes, count);
 109                 memcpy_b(buffer + total_read, buf + boffset, bytes);
 110                 total_read += bytes;
 111                 count -= bytes;
 112                 fd_table->offset += bytes;
 113         }
 114 
 115         kfree((unsigned int)buf);
 116         return total_read;
 117 }
 118 
 119 int procfs_file_lseek(struct inode *i, __off_t offset)
 120 {
 121         return offset;
 122 }

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