blob: ca65d45c7f8de330b0f590e2b02b873a8d6c1ff6 [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
Theodore Ts'oaf5bc922008-09-08 22:25:24 -040026static void *ext4_follow_link(struct dentry *dentry, 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
38 if (!ext4_encrypted_inode(inode))
39 return page_follow_link_light(dentry, nd);
40
41 ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize);
42 if (IS_ERR(ctx))
43 return ctx;
44
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);
50 if (IS_ERR(cpage)) {
51 ext4_put_fname_crypto_ctx(&ctx);
52 return cpage;
53 }
54 caddr = kmap(cpage);
55 caddr[size] = 0;
56 }
57
58 /* Symlink is encrypted */
59 sd = (struct ext4_encrypted_symlink_data *)caddr;
60 cstr.name = sd->encrypted_path;
61 cstr.len = le32_to_cpu(sd->len);
62 if ((cstr.len +
63 sizeof(struct ext4_encrypted_symlink_data) - 1) >
64 max_size) {
65 /* Symlink data on the disk is corrupted */
66 res = -EIO;
67 goto errout;
68 }
69 plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
70 EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
71 paddr = kmalloc(plen + 1, GFP_NOFS);
72 if (!paddr) {
73 res = -ENOMEM;
74 goto errout;
75 }
76 pstr.name = paddr;
Theodore Ts'od2299592015-05-18 13:15:47 -040077 pstr.len = plen;
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -040078 res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr);
Theodore Ts'of348c252015-04-16 01:55:00 -040079 if (res < 0)
80 goto errout;
81 /* Null-terminate the name */
82 if (res <= plen)
83 paddr[res] = '\0';
84 nd_set_link(nd, paddr);
85 ext4_put_fname_crypto_ctx(&ctx);
86 if (cpage) {
87 kunmap(cpage);
88 page_cache_release(cpage);
89 }
90 return NULL;
91errout:
92 ext4_put_fname_crypto_ctx(&ctx);
93 if (cpage) {
94 kunmap(cpage);
95 page_cache_release(cpage);
96 }
97 kfree(paddr);
98 return ERR_PTR(res);
99}
100
101static void ext4_put_link(struct dentry *dentry, struct nameidata *nd,
102 void *cookie)
103{
104 struct page *page = cookie;
105
106 if (!page) {
107 kfree(nd_get_link(nd));
108 } else {
109 kunmap(page);
110 page_cache_release(page);
111 }
112}
113#endif
114
115static void *ext4_follow_fast_link(struct dentry *dentry, struct nameidata *nd)
116{
David Howells2b0143b2015-03-17 22:25:59 +0000117 struct ext4_inode_info *ei = EXT4_I(d_inode(dentry));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400118 nd_set_link(nd, (char *) ei->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 return NULL;
120}
121
Arjan van de Ven754661f2007-02-12 00:55:38 -0800122const struct inode_operations ext4_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 .readlink = generic_readlink,
Theodore Ts'of348c252015-04-16 01:55:00 -0400124#ifdef CONFIG_EXT4_FS_ENCRYPTION
125 .follow_link = ext4_follow_link,
126 .put_link = ext4_put_link,
127#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128 .follow_link = page_follow_link_light,
129 .put_link = page_put_link,
Theodore Ts'of348c252015-04-16 01:55:00 -0400130#endif
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400131 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700132 .setxattr = generic_setxattr,
133 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700134 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700135 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700136};
137
Arjan van de Ven754661f2007-02-12 00:55:38 -0800138const struct inode_operations ext4_fast_symlink_inode_operations = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139 .readlink = generic_readlink,
Theodore Ts'of348c252015-04-16 01:55:00 -0400140 .follow_link = ext4_follow_fast_link,
Dmitry Monakhov256a4532010-05-16 02:00:00 -0400141 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700142 .setxattr = generic_setxattr,
143 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -0700144 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145 .removexattr = generic_removexattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146};