symlink.c 2.6 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 23 24 25 26 27 28 29 30 31 32
 *
 * HISTORY
 *
 *  04/16/99 blf  Created.
 *
 */

#include "udfdecl.h"
#include <asm/uaccess.h>
#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"

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

40
	while (elen < fromlen) {
L
Linus Torvalds 已提交
41
		pc = (struct pathComponent *)(from + elen);
42 43
		switch (pc->componentType) {
		case 1:
44 45 46 47 48 49 50 51 52 53
			/*
			 * 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:
			p = to;
			*p++ = '/';
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
			break;
		case 3:
			memcpy(p, "../", 3);
			p += 3;
			break;
		case 4:
			memcpy(p, "./", 2);
			p += 2;
			/* that would be . - just ignore */
			break;
		case 5:
			p += udf_get_filename(sb, pc->componentIdent, p,
					      pc->lengthComponentIdent);
			*p++ = '/';
			break;
L
Linus Torvalds 已提交
69 70 71
		}
		elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
	}
72
	if (p > to + 1)
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81
		p[-1] = '\0';
	else
		p[0] = '\0';
}

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 已提交
82
	unsigned char *symlink;
L
Linus Torvalds 已提交
83
	int err = -EIO;
A
Al Viro 已提交
84
	unsigned char *p = kmap(page);
85
	struct udf_inode_info *iinfo;
86
	uint32_t pos;
L
Linus Torvalds 已提交
87

88
	iinfo = UDF_I(inode);
89 90 91
	pos = udf_block_map(inode, 0);

	down_read(&iinfo->i_data_sem);
92 93
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
		symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
94
	} else {
95
		bh = sb_bread(inode->i_sb, pos);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103

		if (!bh)
			goto out;

		symlink = bh->b_data;
	}

	udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p);
J
Jan Kara 已提交
104
	brelse(bh);
L
Linus Torvalds 已提交
105

106
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
107 108 109 110
	SetPageUptodate(page);
	kunmap(page);
	unlock_page(page);
	return 0;
111 112

out:
113
	up_read(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122
	SetPageError(page);
	kunmap(page);
	unlock_page(page);
	return err;
}

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