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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/* symlink.c
*
* Symbolic link handling for EFS
*
* (C)1995,96 Christian Vogelgsang
*
* Based on the symlink.c from minix-fs by Linus
*/
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/efs_fs.h>
#include <linux/stat.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
static int efs_readlink(struct inode *, char *, int);
static struct dentry * efs_follow_link(struct inode *, struct dentry *);
struct inode_operations efs_symlink_in_ops = {
NULL, /* no file-operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
efs_readlink, /* readlink */
efs_follow_link, /* follow_link */
NULL,
NULL,
NULL, /* bmap */
NULL, /* truncate */
NULL /* permission */
};
/* ----- efs_getlinktarget -----
read the target of the link from the data zone of the file
*/
static char *efs_getlinktarget(struct inode *in)
{
struct buffer_head * bh;
char *name;
__u32 size = in->i_size;
__u32 block;
/* link data longer than 1024 not supported */
if(size>2*EFS_BLOCK_SIZE) {
printk("efs_getlinktarget: name too long: %lu\n",in->i_size);
return NULL;
}
/* get some memory from the kernel to store the name */
name = kmalloc(size+1,GFP_KERNEL);
if(!name) return NULL;
/* read first 512 bytes of target */
block = efs_bmap(in,0);
bh = bread(in->i_dev,block,EFS_BLOCK_SIZE);
if(!bh) {
kfree(name);
return NULL;
}
memcpy(name,bh->b_data,(size>EFS_BLOCK_SIZE)?EFS_BLOCK_SIZE:size);
brelse(bh);
/* if the linktarget is long, read the next block */
if(size>EFS_BLOCK_SIZE) {
bh = bread(in->i_dev,block+1,EFS_BLOCK_SIZE);
if(!bh) {
kfree(name);
return NULL;
}
memcpy(name+EFS_BLOCK_SIZE,bh->b_data,size-EFS_BLOCK_SIZE);
brelse(bh);
}
/* terminate string and return it */
name[size]=0;
return name;
}
/* ----- efs_follow_link -----
get the inode of the link target
*/
static struct dentry * efs_follow_link(struct inode * dir, struct dentry *base)
{
char * name;
UPDATE_ATIME(dir);
name = efs_getlinktarget(dir);
#ifdef DEBUG_EFS
printk("EFS: efs_getlinktarget(%d) returned \"%s\"\n",
dir->i_ino, name);
#endif
base = lookup_dentry(name, base, 1);
kfree(name);
return base;
}
/* ----- efs_readlink -----
read the target of a link and return the name
*/
static int efs_readlink(struct inode * dir, char * buffer, int buflen)
{
int i;
struct buffer_head * bh;
if (buflen > 1023)
buflen = 1023;
bh = bread(dir->i_dev,efs_bmap(dir,0),EFS_BLOCK_SIZE);
if (!bh)
return 0;
/* copy the link target to the given buffer */
i = 0;
#ifdef DEBUG_EFS
printk("EFS: efs_readlink returning ");
#endif
while (i<buflen && bh->b_data[i] && i < dir->i_size) {
#ifdef DEBUG_EFS
printk("%c", bh->b_data[i]);
#endif
i++;
}
#ifdef DEBUG_EFS
printk("\n");
#endif
if (copy_to_user(buffer, bh->b_data, i))
i = -EFAULT;
brelse(bh);
return i;
}
|