blob: 557b3b0d668c3f12d458a194d819ecb1da407645 [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 int res;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040034 u32 max_size = inode->i_sb->s_blocksize;
Theodore Ts'of348c252015-04-16 01:55:00 -040035
Al Viro6b255392015-11-17 10:20:54 -050036 if (!dentry)
37 return ERR_PTR(-ECHILD);
38
Jaegeuk Kima7550b32016-07-10 14:01:03 -040039 res = fscrypt_get_encryption_info(inode);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040040 if (res)
41 return ERR_PTR(res);
Theodore Ts'of348c252015-04-16 01:55:00 -040042
43 if (ext4_inode_is_fast_symlink(inode)) {
Linus Torvalds9ec3a642015-04-26 15:48:49 -070044 caddr = (char *) EXT4_I(inode)->i_data;
45 max_size = sizeof(EXT4_I(inode)->i_data);
Theodore Ts'of348c252015-04-16 01:55:00 -040046 } else {
47 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
Theodore Ts'ob7236e22015-05-18 13:17:47 -040048 if (IS_ERR(cpage))
Al Viro680baac2015-05-02 13:32:22 -040049 return ERR_CAST(cpage);
Al Viro21fc61c2015-11-17 01:07:57 -050050 caddr = page_address(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040051 }
52
53 /* Symlink is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -040054 sd = (struct fscrypt_symlink_data *)caddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040055 cstr.name = sd->encrypted_path;
Al Viro5a1c7f42015-11-26 15:20:50 -050056 cstr.len = le16_to_cpu(sd->len);
Jaegeuk Kima7550b32016-07-10 14:01:03 -040057 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
Theodore Ts'of348c252015-04-16 01:55:00 -040058 /* Symlink data on the disk is corrupted */
Darrick J. Wong6a797d22015-10-17 16:16:04 -040059 res = -EFSCORRUPTED;
Theodore Ts'of348c252015-04-16 01:55:00 -040060 goto errout;
61 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -040062
63 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
64 if (res)
Theodore Ts'of348c252015-04-16 01:55:00 -040065 goto errout;
Eric Biggersdcce7a42016-09-15 13:13:13 -040066 paddr = pstr.name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040067
68 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -040069 if (res)
Theodore Ts'of348c252015-04-16 01:55:00 -040070 goto errout;
Jaegeuk Kima7550b32016-07-10 14:01:03 -040071
Theodore Ts'of348c252015-04-16 01:55:00 -040072 /* Null-terminate the name */
Eric Biggersef1eb3a2016-09-15 17:25:55 -040073 paddr[pstr.len] = '\0';
Al Viro21fc61c2015-11-17 01:07:57 -050074 if (cpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030075 put_page(cpage);
Al Virofceef392015-12-29 15:58:39 -050076 set_delayed_call(done, kfree_link, paddr);
77 return paddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040078errout:
Al Viro21fc61c2015-11-17 01:07:57 -050079 if (cpage)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030080 put_page(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040081 kfree(paddr);
82 return ERR_PTR(res);
83}
84
Al Viroa7a67e82015-04-27 17:51:30 -040085const struct inode_operations ext4_encrypted_symlink_inode_operations = {
86 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050087 .get_link = ext4_encrypted_get_link,
Al Viroa7a67e82015-04-27 17:51:30 -040088 .setattr = ext4_setattr,
Al Viroa7a67e82015-04-27 17:51:30 -040089 .listxattr = ext4_listxattr,
Al Viroa7a67e82015-04-27 17:51:30 -040090};
Theodore Ts'of348c252015-04-16 01:55:00 -040091
Arjan van de Ven754661f2007-02-12 00:55:38 -080092const struct inode_operations ext4_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050094 .get_link = page_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -040095 .setattr = ext4_setattr,
Mingming Cao617ba132006-10-11 01:20:53 -070096 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097};
98
Arjan van de Ven754661f2007-02-12 00:55:38 -080099const struct inode_operations ext4_fast_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700100 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500101 .get_link = simple_get_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400102 .setattr = ext4_setattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700103 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104};