blob: 8dfbc4025e2f39afcb204d5abe8b0dff5f5a8f7b [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"
Fabian Fredericke9736062014-06-18 19:38:24 +020023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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 "udf_i.h"
31
Jan Kara0e5cc9a2014-12-18 22:37:50 +010032static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
33 int fromlen, unsigned char *to, int tolen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct pathComponent *pc;
36 int elen = 0;
Jan Kara0e5cc9a2014-12-18 22:37:50 +010037 int comp_len;
Al Viro391e8bb2010-01-31 21:28:48 -050038 unsigned char *p = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jan Kara0e5cc9a2014-12-18 22:37:50 +010040 /* Reserve one byte for terminating \0 */
41 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070042 while (elen < fromlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pc = (struct pathComponent *)(from + elen);
Jan Karae237ec32014-12-19 14:27:55 +010044 elen += sizeof(struct pathComponent);
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 */
Jan Karae237ec32014-12-19 14:27:55 +010051 if (pc->lengthComponentIdent > 0) {
52 elen += pc->lengthComponentIdent;
Jan Karafef2e9f2011-12-12 15:13:50 +010053 break;
Jan Karae237ec32014-12-19 14:27:55 +010054 }
Jan Karafef2e9f2011-12-12 15:13:50 +010055 /* Fall through */
56 case 2:
Jan Kara0e5cc9a2014-12-18 22:37:50 +010057 if (tolen == 0)
58 return -ENAMETOOLONG;
Jan Karafef2e9f2011-12-12 15:13:50 +010059 p = to;
60 *p++ = '/';
Jan Kara0e5cc9a2014-12-18 22:37:50 +010061 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070062 break;
63 case 3:
Jan Kara0e5cc9a2014-12-18 22:37:50 +010064 if (tolen < 3)
65 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070066 memcpy(p, "../", 3);
67 p += 3;
Jan Kara0e5cc9a2014-12-18 22:37:50 +010068 tolen -= 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070069 break;
70 case 4:
Jan Kara0e5cc9a2014-12-18 22:37:50 +010071 if (tolen < 2)
72 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 memcpy(p, "./", 2);
74 p += 2;
Jan Kara0e5cc9a2014-12-18 22:37:50 +010075 tolen -= 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070076 /* that would be . - just ignore */
77 break;
78 case 5:
Jan Karae237ec32014-12-19 14:27:55 +010079 elen += pc->lengthComponentIdent;
80 if (elen > fromlen)
81 return -EIO;
Jan Kara0e5cc9a2014-12-18 22:37:50 +010082 comp_len = udf_get_filename(sb, pc->componentIdent,
83 pc->lengthComponentIdent,
84 p, tolen);
85 p += comp_len;
86 tolen -= comp_len;
87 if (tolen == 0)
88 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089 *p++ = '/';
Jan Kara0e5cc9a2014-12-18 22:37:50 +010090 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 if (p > to + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 p[-1] = '\0';
96 else
97 p[0] = '\0';
Jan Kara0e5cc9a2014-12-18 22:37:50 +010098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int udf_symlink_filler(struct file *file, struct page *page)
102{
103 struct inode *inode = page->mapping->host;
104 struct buffer_head *bh = NULL;
Al Viro391e8bb2010-01-31 21:28:48 -0500105 unsigned char *symlink;
Jan Karaa1d47b22014-12-19 12:21:47 +0100106 int err;
Al Viro391e8bb2010-01-31 21:28:48 -0500107 unsigned char *p = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800108 struct udf_inode_info *iinfo;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100109 uint32_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Jan Karaa1d47b22014-12-19 12:21:47 +0100111 /* We don't support symlinks longer than one block */
112 if (inode->i_size > inode->i_sb->s_blocksize) {
113 err = -ENAMETOOLONG;
114 goto out_unmap;
115 }
116
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800117 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100118 pos = udf_block_map(inode, 0);
119
120 down_read(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800121 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
122 symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700123 } else {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100124 bh = sb_bread(inode->i_sb, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jan Karaa1d47b22014-12-19 12:21:47 +0100126 if (!bh) {
127 err = -EIO;
128 goto out_unlock_inode;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 symlink = bh->b_data;
132 }
133
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100134 err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700135 brelse(bh);
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100136 if (err)
137 goto out_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100139 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 SetPageUptodate(page);
141 kunmap(page);
142 unlock_page(page);
143 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700144
Jan Karaa1d47b22014-12-19 12:21:47 +0100145out_unlock_inode:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100146 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 SetPageError(page);
Jan Karaa1d47b22014-12-19 12:21:47 +0100148out_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 kunmap(page);
150 unlock_page(page);
151 return err;
152}
153
154/*
155 * symlinks can't do much...
156 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700157const struct address_space_operations udf_symlink_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700158 .readpage = udf_symlink_filler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};