symlink.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * linux/fs/ext4/symlink.c
  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. *
  17. * ext4 symlink handling code
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/jbd2.h>
  21. #include <linux/namei.h>
  22. #include "ext4.h"
  23. #include "xattr.h"
  24. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  25. static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
  26. {
  27. struct page *cpage = NULL;
  28. char *caddr, *paddr = NULL;
  29. struct ext4_str cstr, pstr;
  30. struct inode *inode = dentry->d_inode;
  31. struct ext4_encrypted_symlink_data *sd;
  32. loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
  33. int res;
  34. u32 plen, max_size = inode->i_sb->s_blocksize;
  35. if (!ext4_encrypted_inode(inode))
  36. return page_follow_link_light(dentry, nd);
  37. res = ext4_get_encryption_info(inode);
  38. if (res)
  39. return ERR_PTR(res);
  40. if (ext4_inode_is_fast_symlink(inode)) {
  41. caddr = (char *) EXT4_I(dentry->d_inode)->i_data;
  42. max_size = sizeof(EXT4_I(dentry->d_inode)->i_data);
  43. } else {
  44. cpage = read_mapping_page(inode->i_mapping, 0, NULL);
  45. if (IS_ERR(cpage))
  46. return cpage;
  47. caddr = kmap(cpage);
  48. caddr[size] = 0;
  49. }
  50. /* Symlink is encrypted */
  51. sd = (struct ext4_encrypted_symlink_data *)caddr;
  52. cstr.name = sd->encrypted_path;
  53. cstr.len = le32_to_cpu(sd->len);
  54. if ((cstr.len +
  55. sizeof(struct ext4_encrypted_symlink_data) - 1) >
  56. max_size) {
  57. /* Symlink data on the disk is corrupted */
  58. res = -EIO;
  59. goto errout;
  60. }
  61. plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
  62. EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
  63. paddr = kmalloc(plen + 1, GFP_NOFS);
  64. if (!paddr) {
  65. res = -ENOMEM;
  66. goto errout;
  67. }
  68. pstr.name = paddr;
  69. pstr.len = plen;
  70. res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
  71. if (res < 0)
  72. goto errout;
  73. /* Null-terminate the name */
  74. if (res <= plen)
  75. paddr[res] = '\0';
  76. nd_set_link(nd, paddr);
  77. if (cpage) {
  78. kunmap(cpage);
  79. page_cache_release(cpage);
  80. }
  81. return NULL;
  82. errout:
  83. if (cpage) {
  84. kunmap(cpage);
  85. page_cache_release(cpage);
  86. }
  87. kfree(paddr);
  88. return ERR_PTR(res);
  89. }
  90. static void ext4_put_link(struct dentry *dentry, struct nameidata *nd,
  91. void *cookie)
  92. {
  93. struct page *page = cookie;
  94. if (!page) {
  95. kfree(nd_get_link(nd));
  96. } else {
  97. kunmap(page);
  98. page_cache_release(page);
  99. }
  100. }
  101. #endif
  102. static void *ext4_follow_fast_link(struct dentry *dentry, struct nameidata *nd)
  103. {
  104. struct ext4_inode_info *ei = EXT4_I(dentry->d_inode);
  105. nd_set_link(nd, (char *) ei->i_data);
  106. return NULL;
  107. }
  108. const struct inode_operations ext4_symlink_inode_operations = {
  109. .readlink = generic_readlink,
  110. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  111. .follow_link = ext4_follow_link,
  112. .put_link = ext4_put_link,
  113. #else
  114. .follow_link = page_follow_link_light,
  115. .put_link = page_put_link,
  116. #endif
  117. .setattr = ext4_setattr,
  118. .setxattr = generic_setxattr,
  119. .getxattr = generic_getxattr,
  120. .listxattr = ext4_listxattr,
  121. .removexattr = generic_removexattr,
  122. };
  123. const struct inode_operations ext4_fast_symlink_inode_operations = {
  124. .readlink = generic_readlink,
  125. .follow_link = ext4_follow_fast_link,
  126. .setattr = ext4_setattr,
  127. .setxattr = generic_setxattr,
  128. .getxattr = generic_getxattr,
  129. .listxattr = ext4_listxattr,
  130. .removexattr = generic_removexattr,
  131. };