[1] | 1 | /* |
---|
| 2 | * devfs/devfs_node.c - devfs node related operations |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 6 | * |
---|
| 7 | * This file is part of ALMOS-kernel. |
---|
| 8 | * |
---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
---|
| 10 | * under the terms of the GNU General Public License as published by |
---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 12 | * |
---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 16 | * General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <config.h> |
---|
| 24 | #include <kmem.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <kdmsg.h> |
---|
[9] | 27 | #include <chdev.h> |
---|
[1] | 28 | #include <vfs.h> |
---|
| 29 | #include <cpu.h> |
---|
| 30 | |
---|
| 31 | #include <devfs.h> |
---|
| 32 | #include <devfs-private.h> |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | VFS_INIT_NODE(devfs_init_node) |
---|
| 37 | { |
---|
| 38 | inode->i_pv = NULL; |
---|
| 39 | return 0; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | VFS_RELEASE_NODE(devfs_release_node) |
---|
| 44 | { |
---|
| 45 | inode->i_pv = NULL; |
---|
| 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | VFS_CREATE_NODE(devfs_create_node) |
---|
| 51 | { |
---|
| 52 | return ENOTSUPPORTED; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | error_t devfs_inode_create(struct vfs_dirent_s *dirent, |
---|
| 56 | struct vfs_inode_s *parent, |
---|
| 57 | struct device_s *dev, size_t size) |
---|
| 58 | { |
---|
| 59 | struct vfs_inode_s *inode; |
---|
| 60 | |
---|
| 61 | inode = vfs_inode_new(dirent->d_ctx);//allocate inode |
---|
| 62 | if(!inode) return ENOMEM; |
---|
| 63 | |
---|
| 64 | inode->i_links = 1; |
---|
| 65 | inode->i_attr = dirent->d_attr; |
---|
| 66 | inode->i_pv = (struct device_s*) dev; |
---|
| 67 | inode->i_size = size; |
---|
| 68 | inode->i_parent.ptr = parent; |
---|
| 69 | inode->i_parent.cid = cpu_get_cid(); |
---|
| 70 | inode->i_parent.gc = parent->i_gc; |
---|
| 71 | inode->i_number = vfs_inum_new(dirent->d_ctx); |
---|
| 72 | |
---|
| 73 | vfs_icache_add(inode); |
---|
| 74 | |
---|
| 75 | VFS_INODE_REF_SET(dirent->d_inode, inode, cpu_get_cid()); |
---|
| 76 | |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | VFS_LOOKUP_NODE(devfs_lookup_node) |
---|
| 81 | { |
---|
| 82 | register struct metafs_s *meta_node; |
---|
| 83 | register struct device_s *dev; |
---|
| 84 | register error_t err; |
---|
| 85 | dev_params_t params; |
---|
| 86 | |
---|
| 87 | if(!(parent->i_attr & VFS_DIR)) |
---|
| 88 | return ENOTDIR; |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | if((meta_node = metafs_lookup(&devfs_db.root, dirent->d_name)) == NULL) |
---|
| 92 | return VFS_NOT_FOUND; |
---|
| 93 | |
---|
| 94 | dev = metafs_container(meta_node, struct device_s, node); |
---|
| 95 | |
---|
| 96 | if((err=dev->op.dev.get_params(dev, ¶ms))) |
---|
| 97 | { |
---|
| 98 | printk(ERROR,"ERROR: devfs_lookup_node: error %d while getting device parameters\n", err); |
---|
| 99 | return err; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | switch(dev->type) |
---|
| 103 | { |
---|
| 104 | case DEV_BLK: |
---|
| 105 | dirent->d_attr |= VFS_DEV_BLK; |
---|
| 106 | break; |
---|
| 107 | case DEV_CHR: |
---|
| 108 | dirent->d_attr |= VFS_DEV_CHR; |
---|
| 109 | break; |
---|
| 110 | default: |
---|
| 111 | dirent->d_attr |= VFS_DEV; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | //Do a list of inode within the device struct to avoid |
---|
| 115 | //creating each time a new inode. |
---|
| 116 | devfs_inode_create(dirent, parent, dev, params.size); |
---|
| 117 | |
---|
| 118 | return VFS_FOUND; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | VFS_STAT_NODE(devfs_stat_node) |
---|
| 122 | { |
---|
| 123 | struct device_s *dev; |
---|
| 124 | uint_t mode; |
---|
| 125 | |
---|
| 126 | dev = (struct device_s*)inode->i_pv; |
---|
| 127 | mode = 0; |
---|
| 128 | |
---|
| 129 | inode->i_stat.st_dev = (uint_t)dev; |
---|
| 130 | inode->i_stat.st_ino = (uint_t)dev; |
---|
| 131 | inode->i_stat.st_nlink = inode->i_links; |
---|
| 132 | inode->i_stat.st_uid = 0; |
---|
| 133 | inode->i_stat.st_gid = 0; |
---|
| 134 | inode->i_stat.st_rdev = FS_TYPE_DEVFS; |
---|
| 135 | inode->i_stat.st_size = inode->i_size; |
---|
| 136 | inode->i_stat.st_blksize = 0; |
---|
| 137 | inode->i_stat.st_blocks = 0; |
---|
| 138 | inode->i_stat.st_atime = 0; |
---|
| 139 | inode->i_stat.st_mtime = 0; |
---|
| 140 | inode->i_stat.st_ctime = 0; |
---|
| 141 | |
---|
| 142 | if(inode->i_attr & VFS_DIR) |
---|
| 143 | { |
---|
| 144 | VFS_SET(mode, VFS_IFDIR); |
---|
| 145 | } |
---|
| 146 | else if(inode->i_attr & VFS_FIFO) |
---|
| 147 | { |
---|
| 148 | VFS_SET(mode, VFS_IFIFO); |
---|
| 149 | } |
---|
| 150 | else if(inode->i_attr & VFS_PIPE) |
---|
| 151 | { |
---|
| 152 | VFS_SET(mode, VFS_IFIFO); |
---|
| 153 | } |
---|
| 154 | else if(inode->i_attr & VFS_DEV_CHR) |
---|
| 155 | { |
---|
| 156 | VFS_SET(mode, VFS_IFCHR); |
---|
| 157 | } |
---|
| 158 | else if(inode->i_attr & VFS_DEV_BLK) |
---|
| 159 | { |
---|
| 160 | VFS_SET(mode, VFS_IFBLK); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | inode->i_stat.st_mode = mode; |
---|
| 164 | return 0; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | VFS_WRITE_NODE(devfs_write_node) |
---|
| 169 | { |
---|
| 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | VFS_UNLINK_NODE(devfs_unlink_node) |
---|
| 174 | { |
---|
| 175 | return ENOTSUPPORTED; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | const struct vfs_inode_op_s devfs_i_op = |
---|
| 179 | { |
---|
| 180 | .init = devfs_init_node, |
---|
| 181 | .create = devfs_create_node, |
---|
| 182 | .lookup = devfs_lookup_node, |
---|
| 183 | .write = devfs_write_node, |
---|
| 184 | .release = devfs_release_node, |
---|
| 185 | .unlink = devfs_unlink_node, |
---|
| 186 | .stat = devfs_stat_node |
---|
| 187 | }; |
---|