blob: afec475aaf5c5d4a57a47bffe9a26c866fde7dce [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 Viro680baac2015-05-02 13:32:22 -040026static const char *ext4_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027{
Theodore Ts'of348c252015-04-16 01:55:00 -040028 struct page *cpage = NULL;
29 char *caddr, *paddr = NULL;
30 struct ext4_str cstr, pstr;
Linus Torvalds9ec3a642015-04-26 15:48:49 -070031 struct inode *inode = d_inode(dentry);
Theodore Ts'of348c252015-04-16 01:55:00 -040032 struct ext4_fname_crypto_ctx *ctx = NULL;
33 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
Theodore Ts'of348c252015-04-16 01:55:00 -040038 ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize);
39 if (IS_ERR(ctx))
Al Viro680baac2015-05-02 13:32:22 -040040 return ERR_CAST(ctx);
Theodore Ts'of348c252015-04-16 01:55:00 -040041
42 if (ext4_inode_is_fast_symlink(inode)) {
Linus Torvalds9ec3a642015-04-26 15:48:49 -070043 caddr = (char *) EXT4_I(inode)->i_data;
44 max_size = sizeof(EXT4_I(inode)->i_data);
Theodore Ts'of348c252015-04-16 01:55:00 -040045 } else {
46 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
47 if (IS_ERR(cpage)) {
48 ext4_put_fname_crypto_ctx(&ctx);
Al Viro680baac2015-05-02 13:32:22 -040049 return ERR_CAST(cpage);
Theodore Ts'of348c252015-04-16 01:55:00 -040050 }
51 caddr = kmap(cpage);
52 caddr[size] = 0;
53 }
54
55 /* Symlink is encrypted */
56 sd = (struct ext4_encrypted_symlink_data *)caddr;
57 cstr.name = sd->encrypted_path;
58 cstr.len = le32_to_cpu(sd->len);
59 if ((cstr.len +
60 sizeof(struct ext4_encrypted_symlink_data) - 1) >
61 max_size) {
62 /* Symlink data on the disk is corrupted */
63 res = -EIO;
64 goto errout;
65 }
66 plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
67 EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
68 paddr = kmalloc(plen + 1, GFP_NOFS);
69 if (!paddr) {
70 res = -ENOMEM;
71 goto errout;
72 }
73 pstr.name = paddr;
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -040074 res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr);
Theodore Ts'of348c252015-04-16 01:55:00 -040075 if (res < 0)
76 goto errout;
77 /* Null-terminate the name */
78 if (res <= plen)
79 paddr[res] = '\0';
Theodore Ts'of348c252015-04-16 01:55:00 -040080 ext4_put_fname_crypto_ctx(&ctx);
81 if (cpage) {
82 kunmap(cpage);
83 page_cache_release(cpage);
84 }
Al Viro680baac2015-05-02 13:32:22 -040085 return *cookie = paddr;
Theodore Ts'of348c252015-04-16 01:55:00 -040086errout:
87 ext4_put_fname_crypto_ctx(&ctx);
88 if (cpage) {
89 kunmap(cpage);
90 page_cache_release(cpage);
91 }
92 kfree(paddr);
93 return ERR_PTR(res);
94}
95
Al Viroa7a67e82015-04-27 17:51:30 -040096const struct inode_operations ext4_encrypted_symlink_inode_operations = {
97 .readlink = generic_readlink,
98 .follow_link = ext4_follow_link,
99 .put_link = kfree_put_link,
100 .setattr = ext4_setattr,
101 .setxattr = generic_setxattr,
102 .getxattr = generic_getxattr,
103 .listxattr = ext4_listxattr,
104 .removexattr = generic_removexattr,
105};
Theodore Ts'of348c252015-04-16 01:55:00 -0400106#endif
107
Arjan van de Ven754661f2007-02-12 00:55:38 -0800108const struct inode_operations ext4_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700109 .readlink = generic_readlink,
110 .follow_link = page_follow_link_light,
111 .put_link = page_put_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};
118
Arjan van de Ven754661f2007-02-12 00:55:38 -0800119const struct inode_operations ext4_fast_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120 .readlink = generic_readlink,
Al Viro75e75662015-05-02 10:13:58 -0400121 .follow_link = simple_follow_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400122 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 .setxattr = generic_setxattr,
124 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700125 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700127};