symlink.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/namei.h>
  21. #include "ext4.h"
  22. #include "xattr.h"
  23. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  24. static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
  25. {
  26. struct page *cpage = NULL;
  27. char *caddr, *paddr = NULL;
  28. struct ext4_str cstr, pstr;
  29. struct inode *inode = d_inode(dentry);
  30. struct ext4_fname_crypto_ctx *ctx = NULL;
  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. ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize);
  38. if (IS_ERR(ctx))
  39. return ctx;
  40. if (ext4_inode_is_fast_symlink(inode)) {
  41. caddr = (char *) EXT4_I(inode)->i_data;
  42. max_size = sizeof(EXT4_I(inode)->i_data);
  43. } else {
  44. cpage = read_mapping_page(inode->i_mapping, 0, NULL);
  45. if (IS_ERR(cpage)) {
  46. ext4_put_fname_crypto_ctx(&ctx);
  47. return cpage;
  48. }
  49. caddr = kmap(cpage);
  50. caddr[size] = 0;
  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;
  71. res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr);
  72. if (res < 0)
  73. goto errout;
  74. /* Null-terminate the name */
  75. if (res <= plen)
  76. paddr[res] = '\0';
  77. nd_set_link(nd, paddr);
  78. ext4_put_fname_crypto_ctx(&ctx);
  79. if (cpage) {
  80. kunmap(cpage);
  81. page_cache_release(cpage);
  82. }
  83. return NULL;
  84. errout:
  85. ext4_put_fname_crypto_ctx(&ctx);
  86. if (cpage) {
  87. kunmap(cpage);
  88. page_cache_release(cpage);
  89. }
  90. kfree(paddr);
  91. return ERR_PTR(res);
  92. }
  93. static void ext4_put_link(struct dentry *dentry, struct nameidata *nd,
  94. void *cookie)
  95. {
  96. struct page *page = cookie;
  97. if (!page) {
  98. kfree(nd_get_link(nd));
  99. } else {
  100. kunmap(page);
  101. page_cache_release(page);
  102. }
  103. }
  104. #endif
  105. static void *ext4_follow_fast_link(struct dentry *dentry, struct nameidata *nd)
  106. {
  107. struct ext4_inode_info *ei = EXT4_I(d_inode(dentry));
  108. nd_set_link(nd, (char *) ei->i_data);
  109. return NULL;
  110. }
  111. const struct inode_operations ext4_symlink_inode_operations = {
  112. .readlink = generic_readlink,
  113. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  114. .follow_link = ext4_follow_link,
  115. .put_link = ext4_put_link,
  116. #else
  117. .follow_link = page_follow_link_light,
  118. .put_link = page_put_link,
  119. #endif
  120. .setattr = ext4_setattr,
  121. .setxattr = generic_setxattr,
  122. .getxattr = generic_getxattr,
  123. .listxattr = ext4_listxattr,
  124. .removexattr = generic_removexattr,
  125. };
  126. const struct inode_operations ext4_fast_symlink_inode_operations = {
  127. .readlink = generic_readlink,
  128. .follow_link = ext4_follow_fast_link,
  129. .setattr = ext4_setattr,
  130. .setxattr = generic_setxattr,
  131. .getxattr = generic_getxattr,
  132. .listxattr = ext4_listxattr,
  133. .removexattr = generic_removexattr,
  134. };