blob: 6f7ee30a89ce806ec0e4fbe27d7a0538ccd1f5c7 [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
Theodore Ts'of348c252015-04-16 01:55:00 -040025#ifdef CONFIG_EXT4_FS_ENCRYPTION
Al Viro6b255392015-11-17 10:20:54 -050026static const char *ext4_encrypted_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -050027 struct inode *inode,
28 struct delayed_call *done)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029{
Theodore Ts'of348c252015-04-16 01:55:00 -040030 struct page *cpage = NULL;
31 char *caddr, *paddr = NULL;
32 struct ext4_str cstr, pstr;
Theodore Ts'of348c252015-04-16 01:55:00 -040033 struct ext4_encrypted_symlink_data *sd;
34 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
35 int res;
36 u32 plen, max_size = inode->i_sb->s_blocksize;
37
Al Viro6b255392015-11-17 10:20:54 -050038 if (!dentry)
39 return ERR_PTR(-ECHILD);
40
Theodore Ts'oc936e1e2015-05-31 13:34:22 -040041 res = ext4_get_encryption_info(inode);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040042 if (res)
43 return ERR_PTR(res);
Theodore Ts'of348c252015-04-16 01:55:00 -040044
45 if (ext4_inode_is_fast_symlink(inode)) {
Linus Torvalds9ec3a642015-04-26 15:48:49 -070046 caddr = (char *) EXT4_I(inode)->i_data;
47 max_size = sizeof(EXT4_I(inode)->i_data);
Theodore Ts'of348c252015-04-16 01:55:00 -040048 } else {
49 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040050 if (IS_ERR(cpage))
Al Viro680baac2015-05-02 13:32:22 -040051 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -050052 caddr = page_address(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040053 caddr[size] = 0;
54 }
55
56 /* Symlink is encrypted */
57 sd = (struct ext4_encrypted_symlink_data *)caddr;
58 cstr.name = sd->encrypted_path;
Al Viro5a1c7f42015-11-26 15:20:50 -050059 cstr.len = le16_to_cpu(sd->len);
Theodore Ts'of348c252015-04-16 01:55:00 -040060 if ((cstr.len +
61 sizeof(struct ext4_encrypted_symlink_data) - 1) >
62 max_size) {
63 /* Symlink data on the disk is corrupted */
Darrick J. Wong6a797d22015-10-17 16:16:04 -040064 res = -EFSCORRUPTED;
Theodore Ts'of348c252015-04-16 01:55:00 -040065 goto errout;
66 }
67 plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
68 EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
69 paddr = kmalloc(plen + 1, GFP_NOFS);
70 if (!paddr) {
71 res = -ENOMEM;
72 goto errout;
73 }
74 pstr.name = paddr;
Theodore Ts'od2299592015-05-18 13:15:47 -040075 pstr.len = plen;
Theodore Ts'ob7236e22015-05-18 13:17:47 -040076 res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
Theodore Ts'of348c252015-04-16 01:55:00 -040077 if (res < 0)
78 goto errout;
79 /* Null-terminate the name */
80 if (res <= plen)
81 paddr[res] = '\0';
Al Viro21fc61c2015-11-17 01:07:57 -050082 if (cpage)
Theodore Ts'of348c252015-04-16 01:55:00 -040083 page_cache_release(cpage);
Al Virofceef392015-12-29 15:58:39 -050084 set_delayed_call(done, kfree_link, paddr);
85 return paddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040086errout:
Al Viro21fc61c2015-11-17 01:07:57 -050087 if (cpage)
Theodore Ts'of348c252015-04-16 01:55:00 -040088 page_cache_release(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040089 kfree(paddr);
90 return ERR_PTR(res);
91}
92
Al Viroa7a67e82015-04-27 17:51:30 -040093const struct inode_operations ext4_encrypted_symlink_inode_operations = {
94 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050095 .get_link = ext4_encrypted_get_link,
Al Viroa7a67e82015-04-27 17:51:30 -040096 .setattr = ext4_setattr,
97 .setxattr = generic_setxattr,
98 .getxattr = generic_getxattr,
99 .listxattr = ext4_listxattr,
100 .removexattr = generic_removexattr,
101};
Theodore Ts'of348c252015-04-16 01:55:00 -0400102#endif
103
Arjan van de Ven754661f2007-02-12 00:55:38 -0800104const struct inode_operations ext4_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500106 .get_link = page_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400107 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 .setxattr = generic_setxattr,
109 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700110 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700112};
113
Arjan van de Ven754661f2007-02-12 00:55:38 -0800114const struct inode_operations ext4_fast_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700115 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500116 .get_link = simple_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400117 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700118 .setxattr = generic_setxattr,
119 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700120 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122};