source: trunk/kernel/fs/sysfs/sysfs_node.c @ 9

Last change on this file since 9 was 1, checked in by alain, 7 years ago

First import

File size: 3.6 KB
Line 
1/*
2 * sysfs/sysfs_node.c - sysfs 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>
27#include <vfs.h>
28#include <cpu.h>
29
30#include <sysfs.h>
31#include <sysfs-private.h>
32
33KMEM_OBJATTR_INIT(sysfs_kmem_node_init)
34{
35        attr->type   = KMEM_SYSFS_NODE;
36        attr->name   = "KCM SysFs Node";
37        attr->size   = sizeof(struct sysfs_node_s);
38        attr->aligne = 0;
39        attr->min    = CONFIG_SYSFS_NODE_MIN;
40        attr->max    = CONFIG_SYSFS_NODE_MAX;
41        attr->ctor   = NULL;
42        attr->dtor   = NULL;
43        return 0;
44}
45
46
47VFS_INIT_NODE(sysfs_init_node)
48{
49        register struct sysfs_node_s *node_info;
50        kmem_req_t req;
51 
52        node_info = inode->i_pv;
53
54        if(inode->i_pv == NULL)
55        {
56                req.type  = KMEM_SYSFS_NODE;
57                req.size  = sizeof(*node_info);
58                req.flags = AF_KERNEL;
59
60                node_info = kmem_alloc(&req);
61        }
62
63        if(node_info == NULL)
64                return ENOMEM;
65 
66        inode->i_pv = node_info;
67        return 0;
68}
69
70
71VFS_RELEASE_NODE(sysfs_release_node)
72{
73        kmem_req_t req;
74
75        req.type   = KMEM_SYSFS_NODE;
76        req.ptr    = inode->i_pv;
77        kmem_free(&req);
78        inode->i_pv = NULL;
79        return 0;
80}
81
82
83VFS_CREATE_NODE(sysfs_create_node)
84{ 
85        return ENOTSUPPORTED;
86}
87
88
89error_t sysfs_inode_create(struct vfs_dirent_s *dirent, 
90                                struct vfs_inode_s *parent,
91                                struct metafs_s *meta, size_t size)
92{
93        struct vfs_inode_s *inode;
94
95        inode = vfs_inode_new(dirent->d_ctx);//allocate inode
96        if(!inode) return ENOMEM;
97
98        inode->i_links  = 1;
99        inode->i_attr = dirent->d_attr;
100        ((struct sysfs_node_s *)inode->i_pv)->node = meta;
101        inode->i_size = size;
102        inode->i_parent.ptr = parent;
103        inode->i_parent.cid = cpu_get_cid();
104        inode->i_parent.gc = parent->i_gc;
105        inode->i_number = vfs_inum_new(dirent->d_ctx);
106       
107        vfs_icache_add(inode);
108
109        dirent->d_inode.cid = cpu_get_cid();
110        dirent->d_inode.ptr = inode;
111
112        return 0;
113}
114
115VFS_LOOKUP_NODE(sysfs_lookup_node)
116{
117        register struct sysfs_node_s *parent_info;
118        register struct metafs_s *meta;
119        register uint_t hasChild;
120        size_t size;
121 
122        size = 0;
123        parent_info = parent->i_pv;
124 
125        sysfs_dmsg(1, "DEBUG: sysfs_lookup: %s, attr %x\n", dirent->d_name, dirent->d_attr);
126
127        if((meta = metafs_lookup(parent_info->node, dirent->d_name)) == NULL)
128                return VFS_NOT_FOUND;
129
130        hasChild = metafs_hasChild(meta);
131
132        if(hasChild && !(dirent->d_attr & VFS_DIR))
133                return EISDIR;
134 
135        if(!(hasChild) && (dirent->d_attr & VFS_DIR))
136                return ENOTDIR;
137
138        if(hasChild)
139                dirent->d_attr |= VFS_DIR;
140        else
141                size = SYSFS_BUFFER_SIZE;
142
143        sysfs_inode_create(dirent, parent, meta, size);
144 
145        return VFS_FOUND;
146}
147
148
149VFS_WRITE_NODE(sysfs_write_node)
150{
151        return 0;
152}
153
154VFS_UNLINK_NODE(sysfs_unlink_node)
155{
156        return ENOTSUPPORTED;
157}
158
159const struct vfs_inode_op_s sysfs_i_op = 
160{
161        .init    = sysfs_init_node,
162        .create  = sysfs_create_node,
163        .lookup  = sysfs_lookup_node,
164        .write   = sysfs_write_node,
165        .release = sysfs_release_node,
166        .unlink  = sysfs_unlink_node,
167        .stat    = NULL,
168        .delete  = NULL
169};
Note: See TracBrowser for help on using the repository browser.