blob: 687ef43e220698e16149acc95b745483babd5fb4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * symlink.c
3 *
4 * PURPOSE
5 * Symlink handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998-2001 Ben Fennema
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070014 * (C) 1999 Stelias Computing Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * HISTORY
17 *
18 * 04/16/99 blf Created.
19 *
20 */
21
22#include "udfdecl.h"
23#include <asm/uaccess.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
27#include <linux/mm.h>
28#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/buffer_head.h>
31#include "udf_i.h"
32
Jan Karaadadc8e2014-12-18 22:37:00 +010033static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
34 int fromlen, unsigned char *to, int tolen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 struct pathComponent *pc;
37 int elen = 0;
Jan Karaadadc8e2014-12-18 22:37:00 +010038 int comp_len;
Al Viro391e8bb2010-01-31 21:28:48 -050039 unsigned char *p = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jan Karaadadc8e2014-12-18 22:37:00 +010041 /* Reserve one byte for terminating \0 */
42 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070043 while (elen < fromlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 pc = (struct pathComponent *)(from + elen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070045 switch (pc->componentType) {
46 case 1:
Jan Karafef2e9f2011-12-12 15:13:50 +010047 /*
48 * Symlink points to some place which should be agreed
49 * upon between originator and receiver of the media. Ignore.
50 */
51 if (pc->lengthComponentIdent > 0)
52 break;
53 /* Fall through */
54 case 2:
Jan Karaadadc8e2014-12-18 22:37:00 +010055 if (tolen == 0)
56 return -ENAMETOOLONG;
Jan Karafef2e9f2011-12-12 15:13:50 +010057 p = to;
58 *p++ = '/';
Jan Karaadadc8e2014-12-18 22:37:00 +010059 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070060 break;
61 case 3:
Jan Karaadadc8e2014-12-18 22:37:00 +010062 if (tolen < 3)
63 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070064 memcpy(p, "../", 3);
65 p += 3;
Jan Karaadadc8e2014-12-18 22:37:00 +010066 tolen -= 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 break;
68 case 4:
Jan Karaadadc8e2014-12-18 22:37:00 +010069 if (tolen < 2)
70 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071 memcpy(p, "./", 2);
72 p += 2;
Jan Karaadadc8e2014-12-18 22:37:00 +010073 tolen -= 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 /* that would be . - just ignore */
75 break;
76 case 5:
Jan Karaadadc8e2014-12-18 22:37:00 +010077 comp_len = udf_get_filename(sb, pc->componentIdent,
78 pc->lengthComponentIdent,
79 p, tolen);
80 p += comp_len;
81 tolen -= comp_len;
82 if (tolen == 0)
83 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084 *p++ = '/';
Jan Karaadadc8e2014-12-18 22:37:00 +010085 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
89 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090 if (p > to + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 p[-1] = '\0';
92 else
93 p[0] = '\0';
Jan Karaadadc8e2014-12-18 22:37:00 +010094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static int udf_symlink_filler(struct file *file, struct page *page)
98{
99 struct inode *inode = page->mapping->host;
100 struct buffer_head *bh = NULL;
Al Viro391e8bb2010-01-31 21:28:48 -0500101 unsigned char *symlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int err = -EIO;
Al Viro391e8bb2010-01-31 21:28:48 -0500103 unsigned char *p = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800104 struct udf_inode_info *iinfo;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100105 uint32_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800107 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100108 pos = udf_block_map(inode, 0);
109
110 down_read(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800111 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
112 symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700113 } else {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100114 bh = sb_bread(inode->i_sb, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!bh)
117 goto out;
118
119 symlink = bh->b_data;
120 }
121
Jan Karaadadc8e2014-12-18 22:37:00 +0100122 err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700123 brelse(bh);
Jan Karaadadc8e2014-12-18 22:37:00 +0100124 if (err)
125 goto out_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100127 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 SetPageUptodate(page);
129 kunmap(page);
130 unlock_page(page);
131 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700132
133out:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100134 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 SetPageError(page);
136 kunmap(page);
137 unlock_page(page);
138 return err;
139}
140
141/*
142 * symlinks can't do much...
143 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700144const struct address_space_operations udf_symlink_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700145 .readpage = udf_symlink_filler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};