symlink.c 3.2 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 32
#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 <linux/buffer_head.h>
#include "udf_i.h"

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

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

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 已提交
101
	unsigned char *symlink;
102
	int err;
A
Al Viro 已提交
103
	unsigned char *p = kmap(page);
104
	struct udf_inode_info *iinfo;
105
	uint32_t pos;
L
Linus Torvalds 已提交
106

107 108 109 110 111 112
	/* We don't support symlinks longer than one block */
	if (inode->i_size > inode->i_sb->s_blocksize) {
		err = -ENAMETOOLONG;
		goto out_unmap;
	}

113
	iinfo = UDF_I(inode);
114 115 116
	pos = udf_block_map(inode, 0);

	down_read(&iinfo->i_data_sem);
117 118
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
		symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
119
	} else {
120
		bh = sb_bread(inode->i_sb, pos);
L
Linus Torvalds 已提交
121

122 123 124 125
		if (!bh) {
			err = -EIO;
			goto out_unlock_inode;
		}
L
Linus Torvalds 已提交
126 127 128 129

		symlink = bh->b_data;
	}

130
	err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
J
Jan Kara 已提交
131
	brelse(bh);
132 133
	if (err)
		goto out_unlock_inode;
L
Linus Torvalds 已提交
134

135
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
136 137 138 139
	SetPageUptodate(page);
	kunmap(page);
	unlock_page(page);
	return 0;
140

141
out_unlock_inode:
142
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
143
	SetPageError(page);
144
out_unmap:
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
	kunmap(page);
	unlock_page(page);
	return err;
}

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