symlink.c 3.26 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17 18 19 20 21 22
 *
 * HISTORY
 *
 *  04/16/99 blf  Created.
 *
 */

#include "udfdecl.h"
Fabian Frederick's avatar
Fabian Frederick committed
23
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36
{
	struct pathComponent *pc;
	int elen = 0;
37
	int comp_len;
Al Viro's avatar
Al Viro committed
38
	unsigned char *p = to;
Linus Torvalds's avatar
Linus Torvalds committed
39

40 41
	/* Reserve one byte for terminating \0 */
	tolen--;
42
	while (elen < fromlen) {
Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
95 96
		}
	}
97
	if (p > to + 1)
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100
		p[-1] = '\0';
	else
		p[0] = '\0';
101
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
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;
Al Viro's avatar
Al Viro committed
108
	unsigned char *symlink;
109
	int err;
110
	unsigned char *p = page_address(page);
111
	struct udf_inode_info *iinfo;
112
	uint32_t pos;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
128

129 130 131 132
		if (!bh) {
			err = -EIO;
			goto out_unlock_inode;
		}
Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136

		symlink = bh->b_data;
	}

137
	err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
Jan Kara's avatar
Jan Kara committed
138
	brelse(bh);
139 140
	if (err)
		goto out_unlock_inode;
Linus Torvalds's avatar
Linus Torvalds committed
141

142
	up_read(&iinfo->i_data_sem);
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145
	SetPageUptodate(page);
	unlock_page(page);
	return 0;
146

147
out_unlock_inode:
148
	up_read(&iinfo->i_data_sem);
Linus Torvalds's avatar
Linus Torvalds committed
149
	SetPageError(page);
150
out_unmap:
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157
	unlock_page(page);
	return err;
}

/*
 * symlinks can't do much...
 */
158
const struct address_space_operations udf_symlink_aops = {
159
	.readpage		= udf_symlink_filler,
Linus Torvalds's avatar
Linus Torvalds committed
160
};