blob: 8d619773056b5ef0b0a3f2d7dd27b74a6eb76434 [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);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +020085 if (comp_len < 0)
86 return comp_len;
87
Jan Kara0e5cc9a2014-12-18 22:37:50 +010088 p += comp_len;
89 tolen -= comp_len;
90 if (tolen == 0)
91 return -ENAMETOOLONG;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092 *p++ = '/';
Jan Kara0e5cc9a2014-12-18 22:37:50 +010093 tolen--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070097 if (p > to + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 p[-1] = '\0';
99 else
100 p[0] = '\0';
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static int udf_symlink_filler(struct file *file, struct page *page)
105{
106 struct inode *inode = page->mapping->host;
107 struct buffer_head *bh = NULL;
Al Viro391e8bb2010-01-31 21:28:48 -0500108 unsigned char *symlink;
Jan Karaa1d47b22014-12-19 12:21:47 +0100109 int err;
Al Viro21fc61c2015-11-17 01:07:57 -0500110 unsigned char *p = page_address(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800111 struct udf_inode_info *iinfo;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100112 uint32_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jan Karaa1d47b22014-12-19 12:21:47 +0100114 /* We don't support symlinks longer than one block */
115 if (inode->i_size > inode->i_sb->s_blocksize) {
116 err = -ENAMETOOLONG;
117 goto out_unmap;
118 }
119
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800120 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100121 pos = udf_block_map(inode, 0);
122
123 down_read(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800124 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
125 symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700126 } else {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100127 bh = sb_bread(inode->i_sb, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jan Karaa1d47b22014-12-19 12:21:47 +0100129 if (!bh) {
130 err = -EIO;
131 goto out_unlock_inode;
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 symlink = bh->b_data;
135 }
136
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100137 err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700138 brelse(bh);
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100139 if (err)
140 goto out_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100142 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unlock_page(page);
145 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700146
Jan Karaa1d47b22014-12-19 12:21:47 +0100147out_unlock_inode:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100148 up_read(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 SetPageError(page);
Jan Karaa1d47b22014-12-19 12:21:47 +0100150out_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unlock_page(page);
152 return err;
153}
154
155/*
156 * symlinks can't do much...
157 */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700158const struct address_space_operations udf_symlink_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700159 .readpage = udf_symlink_filler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};