Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/symlink.c |
ac27a0ec DK |
3 | * |
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 | * | |
617ba13b | 17 | * ext4 symlink handling code |
ac27a0ec DK |
18 | */ |
19 | ||
20 | #include <linux/fs.h> | |
ac27a0ec | 21 | #include <linux/namei.h> |
3dcf5451 | 22 | #include "ext4.h" |
ac27a0ec DK |
23 | #include "xattr.h" |
24 | ||
f348c252 | 25 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
af5bc92d | 26 | static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd) |
f348c252 TT |
27 | { |
28 | struct page *cpage = NULL; | |
29 | char *caddr, *paddr = NULL; | |
30 | struct ext4_str cstr, pstr; | |
9ec3a646 | 31 | struct inode *inode = d_inode(dentry); |
f348c252 TT |
32 | struct ext4_encrypted_symlink_data *sd; |
33 | loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); | |
34 | int res; | |
35 | u32 plen, max_size = inode->i_sb->s_blocksize; | |
36 | ||
37 | if (!ext4_encrypted_inode(inode)) | |
38 | return page_follow_link_light(dentry, nd); | |
39 | ||
b7236e21 TT |
40 | res = ext4_setup_fname_crypto(inode); |
41 | if (res) | |
42 | return ERR_PTR(res); | |
f348c252 TT |
43 | |
44 | if (ext4_inode_is_fast_symlink(inode)) { | |
9ec3a646 LT |
45 | caddr = (char *) EXT4_I(inode)->i_data; |
46 | max_size = sizeof(EXT4_I(inode)->i_data); | |
f348c252 TT |
47 | } else { |
48 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); | |
b7236e21 | 49 | if (IS_ERR(cpage)) |
f348c252 | 50 | return cpage; |
f348c252 TT |
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; | |
d2299590 | 74 | pstr.len = plen; |
b7236e21 | 75 | res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
f348c252 TT |
76 | if (res < 0) |
77 | goto errout; | |
78 | /* Null-terminate the name */ | |
79 | if (res <= plen) | |
80 | paddr[res] = '\0'; | |
81 | nd_set_link(nd, paddr); | |
f348c252 TT |
82 | if (cpage) { |
83 | kunmap(cpage); | |
84 | page_cache_release(cpage); | |
85 | } | |
86 | return NULL; | |
87 | errout: | |
f348c252 TT |
88 | if (cpage) { |
89 | kunmap(cpage); | |
90 | page_cache_release(cpage); | |
91 | } | |
92 | kfree(paddr); | |
93 | return ERR_PTR(res); | |
94 | } | |
95 | ||
96 | static void ext4_put_link(struct dentry *dentry, struct nameidata *nd, | |
97 | void *cookie) | |
98 | { | |
99 | struct page *page = cookie; | |
100 | ||
101 | if (!page) { | |
102 | kfree(nd_get_link(nd)); | |
103 | } else { | |
104 | kunmap(page); | |
105 | page_cache_release(page); | |
106 | } | |
107 | } | |
108 | #endif | |
109 | ||
110 | static void *ext4_follow_fast_link(struct dentry *dentry, struct nameidata *nd) | |
ac27a0ec | 111 | { |
2b0143b5 | 112 | struct ext4_inode_info *ei = EXT4_I(d_inode(dentry)); |
af5bc92d | 113 | nd_set_link(nd, (char *) ei->i_data); |
ac27a0ec DK |
114 | return NULL; |
115 | } | |
116 | ||
754661f1 | 117 | const struct inode_operations ext4_symlink_inode_operations = { |
ac27a0ec | 118 | .readlink = generic_readlink, |
f348c252 TT |
119 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
120 | .follow_link = ext4_follow_link, | |
121 | .put_link = ext4_put_link, | |
122 | #else | |
ac27a0ec DK |
123 | .follow_link = page_follow_link_light, |
124 | .put_link = page_put_link, | |
f348c252 | 125 | #endif |
256a4535 | 126 | .setattr = ext4_setattr, |
ac27a0ec DK |
127 | .setxattr = generic_setxattr, |
128 | .getxattr = generic_getxattr, | |
617ba13b | 129 | .listxattr = ext4_listxattr, |
ac27a0ec | 130 | .removexattr = generic_removexattr, |
ac27a0ec DK |
131 | }; |
132 | ||
754661f1 | 133 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
ac27a0ec | 134 | .readlink = generic_readlink, |
f348c252 | 135 | .follow_link = ext4_follow_fast_link, |
256a4535 | 136 | .setattr = ext4_setattr, |
ac27a0ec DK |
137 | .setxattr = generic_setxattr, |
138 | .getxattr = generic_getxattr, | |
617ba13b | 139 | .listxattr = ext4_listxattr, |
ac27a0ec | 140 | .removexattr = generic_removexattr, |
ac27a0ec | 141 | }; |