Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/symlink.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 17 | * ext4 symlink handling code |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 21 | #include <linux/namei.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 22 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 23 | #include "xattr.h" |
| 24 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 25 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 26 | static const char *ext4_encrypted_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 27 | struct inode *inode, |
| 28 | struct delayed_call *done) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 30 | struct page *cpage = NULL; |
| 31 | char *caddr, *paddr = NULL; |
| 32 | struct ext4_str cstr, pstr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 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 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 38 | if (!dentry) |
| 39 | return ERR_PTR(-ECHILD); |
| 40 | |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 41 | res = ext4_get_encryption_info(inode); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 42 | if (res) |
| 43 | return ERR_PTR(res); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 44 | |
| 45 | if (ext4_inode_is_fast_symlink(inode)) { |
Linus Torvalds | 9ec3a64 | 2015-04-26 15:48:49 -0700 | [diff] [blame] | 46 | caddr = (char *) EXT4_I(inode)->i_data; |
| 47 | max_size = sizeof(EXT4_I(inode)->i_data); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 48 | } else { |
| 49 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 50 | if (IS_ERR(cpage)) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 51 | return ERR_CAST(cpage); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 52 | caddr = page_address(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 53 | caddr[size] = 0; |
| 54 | } |
| 55 | |
| 56 | /* Symlink is encrypted */ |
| 57 | sd = (struct ext4_encrypted_symlink_data *)caddr; |
| 58 | cstr.name = sd->encrypted_path; |
Al Viro | 5a1c7f4 | 2015-11-26 15:20:50 -0500 | [diff] [blame] | 59 | cstr.len = le16_to_cpu(sd->len); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 60 | if ((cstr.len + |
| 61 | sizeof(struct ext4_encrypted_symlink_data) - 1) > |
| 62 | max_size) { |
| 63 | /* Symlink data on the disk is corrupted */ |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 64 | res = -EFSCORRUPTED; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 65 | 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'o | d229959 | 2015-05-18 13:15:47 -0400 | [diff] [blame] | 75 | pstr.len = plen; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 76 | res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 77 | if (res < 0) |
| 78 | goto errout; |
| 79 | /* Null-terminate the name */ |
| 80 | if (res <= plen) |
| 81 | paddr[res] = '\0'; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 82 | if (cpage) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 83 | page_cache_release(cpage); |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 84 | set_delayed_call(done, kfree_link, paddr); |
| 85 | return paddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 86 | errout: |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 87 | if (cpage) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 88 | page_cache_release(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 89 | kfree(paddr); |
| 90 | return ERR_PTR(res); |
| 91 | } |
| 92 | |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 93 | const struct inode_operations ext4_encrypted_symlink_inode_operations = { |
| 94 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 95 | .get_link = ext4_encrypted_get_link, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 96 | .setattr = ext4_setattr, |
| 97 | .setxattr = generic_setxattr, |
| 98 | .getxattr = generic_getxattr, |
| 99 | .listxattr = ext4_listxattr, |
| 100 | .removexattr = generic_removexattr, |
| 101 | }; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 102 | #endif |
| 103 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 104 | const struct inode_operations ext4_symlink_inode_operations = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 106 | .get_link = page_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 107 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 108 | .setxattr = generic_setxattr, |
| 109 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 110 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 111 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 114 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 115 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 116 | .get_link = simple_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 117 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 118 | .setxattr = generic_setxattr, |
| 119 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 120 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 121 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 122 | }; |