blob: 4d83d9e05f2e84197640b2aaa0215cfea8214381 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/symlink.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Only fast symlinks left here - the rest is done by generic code. AV, 1999
5 *
6 * Copyright (C) 1992, 1993, 1994, 1995
7 * Remy Card (card@masi.ibp.fr)
8 * Laboratoire MASI - Institut Blaise Pascal
9 * Universite Pierre et Marie Curie (Paris VI)
10 *
11 * from
12 *
13 * linux/fs/minix/symlink.c
14 *
15 * Copyright (C) 1991, 1992 Linus Torvalds
16 *
Mingming Cao617ba132006-10-11 01:20:53 -070017 * ext4 symlink handling code
Dave Kleikampac27a0e2006-10-11 01:20:50 -070018 */
19
20#include <linux/fs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070021#include <linux/namei.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040022#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070023#include "xattr.h"
24
Al Viro6b255392015-11-17 10:20:54 -050025static const char *ext4_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -050026 struct inode *inode,
27 struct delayed_call *done)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028{
Theodore Ts'of348c252015-04-16 01:55:00 -040029 struct page *cpage = NULL;
30 char *caddr, *paddr = NULL;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040031 struct fscrypt_str cstr, pstr;
32 struct fscrypt_symlink_data *sd;
Theodore Ts'of348c252015-04-16 01:55:00 -040033 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
34 int res;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040035 u32 max_size = inode->i_sb->s_blocksize;
Theodore Ts'of348c252015-04-16 01:55:00 -040036
Al Viro6b255392015-11-17 10:20:54 -050037 if (!dentry)
38 return ERR_PTR(-ECHILD);
39
Jaegeuk Kima7550b32016-07-10 14:01:03 -040040 res = fscrypt_get_encryption_info(inode);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040041 if (res)
42 return ERR_PTR(res);
Theodore Ts'of348c252015-04-16 01:55:00 -040043
44 if (ext4_inode_is_fast_symlink(inode)) {
Linus Torvalds9ec3a642015-04-26 15:48:49 -070045 caddr = (char *) EXT4_I(inode)->i_data;
46 max_size = sizeof(EXT4_I(inode)->i_data);
Theodore Ts'of348c252015-04-16 01:55:00 -040047 } else {
48 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040049 if (IS_ERR(cpage))
Al Viro680baac2015-05-02 13:32:22 -040050 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -050051 caddr = page_address(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040052 caddr[size] = 0;
53 }
54
55 /* Symlink is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -040056 sd = (struct fscrypt_symlink_data *)caddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040057 cstr.name = sd->encrypted_path;
Al Viro5a1c7f42015-11-26 15:20:50 -050058 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kima7550b32016-07-10 14:01:03 -040059 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
Theodore Ts'of348c252015-04-16 01:55:00 -040060 /* Symlink data on the disk is corrupted */
Darrick J. Wong6a797d22015-10-17 16:16:04 -040061 res = -EFSCORRUPTED;
Theodore Ts'of348c252015-04-16 01:55:00 -040062 goto errout;
63 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -040064
65 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
66 if (res)
Theodore Ts'of348c252015-04-16 01:55:00 -040067 goto errout;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040068
69 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
Theodore Ts'of348c252015-04-16 01:55:00 -040070 if (res < 0)
71 goto errout;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040072
73 paddr = pstr.name;
74
Theodore Ts'of348c252015-04-16 01:55:00 -040075 /* Null-terminate the name */
Jaegeuk Kima7550b32016-07-10 14:01:03 -040076 if (res <= pstr.len)
Theodore Ts'of348c252015-04-16 01:55:00 -040077 paddr[res] = '\0';
Al Viro21fc61c2015-11-17 01:07:57 -050078 if (cpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030079 put_page(cpage);
Al Virofceef392015-12-29 15:58:39 -050080 set_delayed_call(done, kfree_link, paddr);
81 return paddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040082errout:
Al Viro21fc61c2015-11-17 01:07:57 -050083 if (cpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030084 put_page(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040085 kfree(paddr);
86 return ERR_PTR(res);
87}
88
Al Viroa7a67e82015-04-27 17:51:30 -040089const struct inode_operations ext4_encrypted_symlink_inode_operations = {
90 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050091 .get_link = ext4_encrypted_get_link,
Al Viroa7a67e82015-04-27 17:51:30 -040092 .setattr = ext4_setattr,
93 .setxattr = generic_setxattr,
94 .getxattr = generic_getxattr,
95 .listxattr = ext4_listxattr,
96 .removexattr = generic_removexattr,
97};
Theodore Ts'of348c252015-04-16 01:55:00 -040098
Arjan van de Ven754661f2007-02-12 00:55:38 -080099const struct inode_operations ext4_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500101 .get_link = page_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400102 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103 .setxattr = generic_setxattr,
104 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700105 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700106 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107};
108
Arjan van de Ven754661f2007-02-12 00:55:38 -0800109const struct inode_operations ext4_fast_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700110 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500111 .get_link = simple_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400112 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113 .setxattr = generic_setxattr,
114 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700115 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700116 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117};