forked from kvaneesh/samplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode.c
117 lines (101 loc) · 3.2 KB
/
inode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* fs/samplefs/inode.c
*
* Copyright (C) International Business Machines Corp., 2006, 2007
* Author(s): Steve French ([email protected])
* Aneesh Kumar K.V ([email protected])
*
* Sample File System
*
* Primitive example to show how to create a Linux filesystem module
*
* Inode related functions
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/fs.h>
#include "samplefs.h"
extern struct inode *samplefs_get_inode(struct super_block *sb,
int mode, dev_t dev);
static int sfs_mknod(struct inode *dir, struct dentry *dentry,
int mode, dev_t dev)
{
struct inode * inode = samplefs_get_inode(dir->i_sb, mode, dev);
int error = -ENOSPC;
printk(KERN_INFO "samplefs: mknod\n");
if (inode) {
if (dir->i_mode & S_ISGID) {
inode->i_gid = dir->i_gid;
if (S_ISDIR(mode))
inode->i_mode |= S_ISGID;
}
d_instantiate(dentry, inode);
dget(dentry); /* Extra count - pin the dentry in core */
error = 0;
dir->i_mtime = dir->i_ctime = CURRENT_TIME;
/* real filesystems would normally use i_size_write function */
dir->i_size += 0x20; /* bogus small size for each dir entry */
}
return error;
}
static int sfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
{
int retval = 0;
retval = sfs_mknod(dir, dentry, mode | S_IFDIR, 0);
/* link count is two for dir, for dot and dot dot */
if (!retval)
dir->i_nlink++;
return retval;
}
static int sfs_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *nd)
{
return sfs_mknod(dir, dentry, mode | S_IFREG, 0);
}
static int sfs_symlink(struct inode * dir, struct dentry *dentry,
const char * symname)
{
struct inode *inode;
int error = -ENOSPC;
inode = samplefs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
if (inode) {
int l = strlen(symname)+1;
error = page_symlink(inode, symname, l);
if (!error) {
if (dir->i_mode & S_ISGID)
inode->i_gid = dir->i_gid;
d_instantiate(dentry, inode);
dget(dentry);
dir->i_mtime = dir->i_ctime = CURRENT_TIME;
} else
iput(inode);
}
return error;
}
struct inode_operations sfs_file_inode_ops = {
.getattr = simple_getattr,
};
struct inode_operations sfs_dir_inode_ops = {
.create = sfs_create,
.lookup = simple_lookup,
.link = simple_link,
.unlink = simple_unlink,
.symlink = sfs_symlink,
.mkdir = sfs_mkdir,
.rmdir = simple_rmdir,
.mknod = sfs_mknod,
.rename = simple_rename,
};