symlink.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * symlink.c
 *
 * PURPOSE
 *	Symlink handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-2001 Ben Fennema
14
 *  (C) 1999 Stelias Computing Inc
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22
 *
 * HISTORY
 *
 *  04/16/99 blf  Created.
 *
 */

#include "udfdecl.h"
F
Fabian Frederick 已提交
23
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/time.h>
#include <linux/mm.h>
#include <linux/stat.h>
#include <linux/pagemap.h>
#include "udf_i.h"

32 33
static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
			  int fromlen, unsigned char *to, int tolen)
L
Linus Torvalds 已提交
34 35 36
{
	struct pathComponent *pc;
	int elen = 0;
37
	int comp_len;
A
Al Viro 已提交
38
	unsigned char *p = to;
L
Linus Torvalds 已提交
39

40 41
	/* Reserve one byte for terminating \0 */
	tolen--;
42
	while (elen < fromlen) {
L
Linus Torvalds 已提交
43
		pc = (struct pathComponent *)(from + elen);
44
		elen += sizeof(struct pathComponent);
45 46
		switch (pc->componentType) {
		case 1:
47 48 49 50
			/*
			 * Symlink points to some place which should be agreed
 			 * upon between originator and receiver of the media. Ignore.
			 */
51 52
			if (pc->lengthComponentIdent > 0) {
				elen += pc->lengthComponentIdent;
53
				break;
54
			}
55 56
			/* Fall through */
		case 2:
57 58
			if (tolen == 0)
				return -ENAMETOOLONG;
59 60
			p = to;
			*p++ = '/';
61
			tolen--;
62 63
			break;
		case 3:
64 65
			if (tolen < 3)
				return -ENAMETOOLONG;
66 67
			memcpy(p, "../", 3);
			p += 3;
68
			tolen -= 3;
69 70
			break;
		case 4:
71 72
			if (tolen < 2)
				return -ENAMETOOLONG;
73 74
			memcpy(p, "./", 2);
			p += 2;
75
			tolen -= 2;
76 77 78
			/* that would be . - just ignore */
			break;
		case 5:
79 80 81
			elen += pc->lengthComponentIdent;
			if (elen > fromlen)
				return -EIO;
82 83 84
			comp_len = udf_get_filename(sb, pc->componentIdent,
						    pc->lengthComponentIdent,
						    p, tolen);
85 86 87
			if (comp_len < 0)
				return comp_len;

88 89 90 91
			p += comp_len;
			tolen -= comp_len;
			if (tolen == 0)
				return -ENAMETOOLONG;
92
			*p++ = '/';
93
			tolen--;
94
			break;
L
Linus Torvalds 已提交
95 96
		}
	}
97
	if (p > to + 1)
L
Linus Torvalds 已提交
98 99 100
		p[-1] = '\0';
	else
		p[0] = '\0';
101
	return 0;
L
Linus Torvalds 已提交
102 103 104 105 106 107
}

static int udf_symlink_filler(struct file *file, struct page *page)
{
	struct inode *inode = page->mapping->host;
	struct buffer_head *bh = NULL;
A
Al Viro 已提交
108
	unsigned char *symlink;
109
	int err;
A
Al Viro 已提交
110
	unsigned char *p = kmap(page);
111
	struct udf_inode_info *iinfo;
112
	uint32_t pos;
L
Linus Torvalds 已提交
113

114 115 116 117 118 119
	/* We don't support symlinks longer than one block */
	if (inode->i_size > inode->i_sb->s_blocksize) {
		err = -ENAMETOOLONG;
		goto out_unmap;
	}

120
	iinfo = UDF_I(inode);
121 122 123
	pos = udf_block_map(inode, 0);

	down_read(&iinfo->i_data_sem);
124 125
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
		symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
126
	} else {
127
		bh = sb_bread(inode->i_sb, pos);
L
Linus Torvalds 已提交
128

129 130 131 132
		if (!bh) {
			err = -EIO;
			goto out_unlock_inode;
		}
L
Linus Torvalds 已提交
133 134 135 136

		symlink = bh->b_data;
	}

137
	err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
J
Jan Kara 已提交
138
	brelse(bh);
139 140
	if (err)
		goto out_unlock_inode;
L
Linus Torvalds 已提交
141

142
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
143 144 145 146
	SetPageUptodate(page);
	kunmap(page);
	unlock_page(page);
	return 0;
147

148
out_unlock_inode:
149
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
150
	SetPageError(page);
151
out_unmap:
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159
	kunmap(page);
	unlock_page(page);
	return err;
}

/*
 * symlinks can't do much...
 */
160
const struct address_space_operations udf_symlink_aops = {
161
	.readpage		= udf_symlink_filler,
L
Linus Torvalds 已提交
162
};