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 |
Linus Torvalds | d857da7 | 2015-06-25 14:06:55 -0700 | [diff] [blame] | 26 | static const char *ext4_encrypted_follow_link(struct dentry *dentry, void **cookie) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 27 | { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 28 | struct page *cpage = NULL; |
| 29 | char *caddr, *paddr = NULL; |
| 30 | struct ext4_str cstr, pstr; |
Linus Torvalds | 9ec3a64 | 2015-04-26 15:48:49 -0700 | [diff] [blame] | 31 | struct inode *inode = d_inode(dentry); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | c936e1e | 2015-05-31 13:34:22 -0400 | [diff] [blame] | 37 | res = ext4_get_encryption_info(inode); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 38 | if (res) |
| 39 | return ERR_PTR(res); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 40 | |
| 41 | if (ext4_inode_is_fast_symlink(inode)) { |
Linus Torvalds | 9ec3a64 | 2015-04-26 15:48:49 -0700 | [diff] [blame] | 42 | caddr = (char *) EXT4_I(inode)->i_data; |
| 43 | max_size = sizeof(EXT4_I(inode)->i_data); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 44 | } else { |
| 45 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 46 | if (IS_ERR(cpage)) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 47 | return ERR_CAST(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 48 | caddr = kmap(cpage); |
| 49 | caddr[size] = 0; |
| 50 | } |
| 51 | |
| 52 | /* Symlink is encrypted */ |
| 53 | sd = (struct ext4_encrypted_symlink_data *)caddr; |
| 54 | cstr.name = sd->encrypted_path; |
| 55 | cstr.len = le32_to_cpu(sd->len); |
| 56 | if ((cstr.len + |
| 57 | sizeof(struct ext4_encrypted_symlink_data) - 1) > |
| 58 | max_size) { |
| 59 | /* Symlink data on the disk is corrupted */ |
| 60 | res = -EIO; |
| 61 | goto errout; |
| 62 | } |
| 63 | plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ? |
| 64 | EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len; |
| 65 | paddr = kmalloc(plen + 1, GFP_NOFS); |
| 66 | if (!paddr) { |
| 67 | res = -ENOMEM; |
| 68 | goto errout; |
| 69 | } |
| 70 | pstr.name = paddr; |
Theodore Ts'o | d229959 | 2015-05-18 13:15:47 -0400 | [diff] [blame] | 71 | pstr.len = plen; |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 72 | res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 73 | if (res < 0) |
| 74 | goto errout; |
| 75 | /* Null-terminate the name */ |
| 76 | if (res <= plen) |
| 77 | paddr[res] = '\0'; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 78 | if (cpage) { |
| 79 | kunmap(cpage); |
| 80 | page_cache_release(cpage); |
| 81 | } |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 82 | return *cookie = paddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 83 | errout: |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 84 | if (cpage) { |
| 85 | kunmap(cpage); |
| 86 | page_cache_release(cpage); |
| 87 | } |
| 88 | kfree(paddr); |
| 89 | return ERR_PTR(res); |
| 90 | } |
| 91 | |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 92 | const struct inode_operations ext4_encrypted_symlink_inode_operations = { |
| 93 | .readlink = generic_readlink, |
Linus Torvalds | d857da7 | 2015-06-25 14:06:55 -0700 | [diff] [blame] | 94 | .follow_link = ext4_encrypted_follow_link, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 95 | .put_link = kfree_put_link, |
| 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, |
| 106 | .follow_link = page_follow_link_light, |
| 107 | .put_link = page_put_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 108 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 109 | .setxattr = generic_setxattr, |
| 110 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 111 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 112 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 115 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 116 | .readlink = generic_readlink, |
Al Viro | 75e7566 | 2015-05-02 10:13:58 -0400 | [diff] [blame] | 117 | .follow_link = simple_follow_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 118 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 119 | .setxattr = generic_setxattr, |
| 120 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 121 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 122 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 123 | }; |