summaryrefslogtreecommitdiffstats
path: root/fs/affs/symlink.c
blob: 18c03730f4789ac862dbffb93135427fb4f6f584 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *  linux/fs/affs/symlink.c
 *
 *  1995  Hans-Joachim Widmaier - Modified for affs.
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  affs symlink handling code
 */

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/affs_fs.h>
#include <linux/amigaffs.h>
#include <asm/uaccess.h>

#define MIN(a,b) (((a) < (b)) ? (a) : (b))

static int		 affs_readlink(struct dentry *, char *, int);
static struct dentry	*affs_follow_link(struct dentry *dentry, struct dentry *base, unsigned int);

struct inode_operations affs_symlink_inode_operations = {
	NULL,			/* no file-operations */
	NULL,			/* create */
	NULL,			/* lookup */
	NULL,			/* link */
	NULL,			/* unlink */
	NULL,			/* symlink */
	NULL,			/* mkdir */
	NULL,			/* rmdir */
	NULL,			/* mknod */
	NULL,			/* rename */
	affs_readlink,		/* readlink */
	affs_follow_link,	/* follow_link */
	NULL,			/* get_block */
	NULL,			/* readpage */
	NULL,			/* writepage */
	NULL,			/* flushpage */
	NULL,			/* truncate */
	NULL,			/* permission */
	NULL,			/* smap */
	NULL			/* revalidate */
};

static int
affs_readlink(struct dentry *dentry, char *buffer, int buflen)
{
	struct inode *inode = dentry->d_inode;
	struct buffer_head	*bh;
	struct slink_front	*lf;
	int			 i, j;
	char			 c;
	char			 lc;
	char			*pf;

	pr_debug("AFFS: readlink(ino=%lu,buflen=%d)\n",inode->i_ino,buflen);

	bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
	if (!bh) {
		affs_warning(inode->i_sb,"follow_link","Unable to read i-node block %lu\n",
				inode->i_ino);
		return -EIO;
	}
	lf = (struct slink_front *)bh->b_data;
	lc = 0;
	i  = 0;
	j  = 0;
	pf = inode->i_sb->u.affs_sb.s_prefix ? inode->i_sb->u.affs_sb.s_prefix : "/";
	
	if (strchr(lf->symname,':')) {		/* Handle assign or volume name */
		while (i < buflen && (c = pf[i])) {
			put_user(c,buffer++);
			i++;
		}
		while (i < buflen && (c = lf->symname[j]) != ':') {
			put_user(c,buffer++);
			i++, j++;
		}
		if (i < buflen) {
			put_user('/',buffer++);
			i++, j++;
		}
		lc = '/';
	}
	while (i < buflen && (c = lf->symname[j])) {
		if (c == '/' && lc == '/' && (i + 3 < buflen)) {	/* parent dir */
			put_user('.',buffer++);
			put_user('.',buffer++);
			i += 2;
		}
		put_user(c,buffer++);
		lc = c;
		i++, j++;
	}
	affs_brelse(bh);
	return i;
}

static struct dentry *
affs_follow_link(struct dentry *dentry, struct dentry *base, unsigned int follow)
{
	struct inode *inode = dentry->d_inode;
	struct buffer_head	*bh;
	struct slink_front	*lf;
	char			*buffer;
	int			 i, j;
	char			 c;
	char			 lc;
	char			*pf;

	pr_debug("AFFS: follow_link(ino=%lu)\n",inode->i_ino);

	if (!(buffer = kmalloc(1024,GFP_KERNEL))) {
		dput(base);
		return ERR_PTR(-ENOSPC);
	}
	bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
	if (!bh) {
		affs_warning(inode->i_sb,"follow_link","Unable to read i-node block %lu\n",
				inode->i_ino);
		kfree(buffer);
		dput(base);
		return ERR_PTR(-EIO);
	}
	i  = 0;
	j  = 0;
	lf = (struct slink_front *)bh->b_data;
	lc = 0;
	pf = inode->i_sb->u.affs_sb.s_prefix ? inode->i_sb->u.affs_sb.s_prefix : "/";

	if (strchr(lf->symname,':')) {		/* Handle assign or volume name */
		while (i < 1023 && (c = pf[i]))
			buffer[i++] = c;
		while (i < 1023 && lf->symname[j] != ':')
			buffer[i++] = lf->symname[j++];
		if (i < 1023)
			 buffer[i++] = '/';
		j++;
		lc = '/';
	}
	while (i < 1023 && (c = lf->symname[j])) {
		if (c == '/' && lc == '/' && i < 1020) {	/* parent dir */
			buffer[i++] = '.';
			buffer[i++] = '.';
		}
		buffer[i++] = c;
		lc = c;
		j++;
	}
	buffer[i] = '\0';
	affs_brelse(bh);
	base = lookup_dentry(buffer,base,follow);
	kfree(buffer);
	return base;
}