symlink.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * symlink.c
  3. *
  4. * PURPOSE
  5. * Symlink handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998-2001 Ben Fennema
  14. * (C) 1999 Stelias Computing Inc
  15. *
  16. * HISTORY
  17. *
  18. * 04/16/99 blf Created.
  19. *
  20. */
  21. #include "udfdecl.h"
  22. #include <asm/uaccess.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/time.h>
  26. #include <linux/mm.h>
  27. #include <linux/stat.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/buffer_head.h>
  30. #include "udf_i.h"
  31. static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
  32. int fromlen, unsigned char *to, int tolen)
  33. {
  34. struct pathComponent *pc;
  35. int elen = 0;
  36. int comp_len;
  37. unsigned char *p = to;
  38. /* Reserve one byte for terminating \0 */
  39. tolen--;
  40. while (elen < fromlen) {
  41. pc = (struct pathComponent *)(from + elen);
  42. elen += sizeof(struct pathComponent);
  43. switch (pc->componentType) {
  44. case 1:
  45. /*
  46. * Symlink points to some place which should be agreed
  47. * upon between originator and receiver of the media. Ignore.
  48. */
  49. if (pc->lengthComponentIdent > 0) {
  50. elen += pc->lengthComponentIdent;
  51. break;
  52. }
  53. /* Fall through */
  54. case 2:
  55. if (tolen == 0)
  56. return -ENAMETOOLONG;
  57. p = to;
  58. *p++ = '/';
  59. tolen--;
  60. break;
  61. case 3:
  62. if (tolen < 3)
  63. return -ENAMETOOLONG;
  64. memcpy(p, "../", 3);
  65. p += 3;
  66. tolen -= 3;
  67. break;
  68. case 4:
  69. if (tolen < 2)
  70. return -ENAMETOOLONG;
  71. memcpy(p, "./", 2);
  72. p += 2;
  73. tolen -= 2;
  74. /* that would be . - just ignore */
  75. break;
  76. case 5:
  77. elen += pc->lengthComponentIdent;
  78. if (elen > fromlen)
  79. return -EIO;
  80. comp_len = udf_get_filename(sb, pc->componentIdent,
  81. pc->lengthComponentIdent,
  82. p, tolen);
  83. p += comp_len;
  84. tolen -= comp_len;
  85. if (tolen == 0)
  86. return -ENAMETOOLONG;
  87. *p++ = '/';
  88. tolen--;
  89. break;
  90. }
  91. }
  92. if (p > to + 1)
  93. p[-1] = '\0';
  94. else
  95. p[0] = '\0';
  96. return 0;
  97. }
  98. static int udf_symlink_filler(struct file *file, struct page *page)
  99. {
  100. struct inode *inode = page->mapping->host;
  101. struct buffer_head *bh = NULL;
  102. unsigned char *symlink;
  103. int err;
  104. unsigned char *p = kmap(page);
  105. struct udf_inode_info *iinfo;
  106. uint32_t pos;
  107. /* We don't support symlinks longer than one block */
  108. if (inode->i_size > inode->i_sb->s_blocksize) {
  109. err = -ENAMETOOLONG;
  110. goto out_unmap;
  111. }
  112. iinfo = UDF_I(inode);
  113. pos = udf_block_map(inode, 0);
  114. down_read(&iinfo->i_data_sem);
  115. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  116. symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
  117. } else {
  118. bh = sb_bread(inode->i_sb, pos);
  119. if (!bh) {
  120. err = -EIO;
  121. goto out_unlock_inode;
  122. }
  123. symlink = bh->b_data;
  124. }
  125. err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
  126. brelse(bh);
  127. if (err)
  128. goto out_unlock_inode;
  129. up_read(&iinfo->i_data_sem);
  130. SetPageUptodate(page);
  131. kunmap(page);
  132. unlock_page(page);
  133. return 0;
  134. out_unlock_inode:
  135. up_read(&iinfo->i_data_sem);
  136. SetPageError(page);
  137. out_unmap:
  138. kunmap(page);
  139. unlock_page(page);
  140. return err;
  141. }
  142. /*
  143. * symlinks can't do much...
  144. */
  145. const struct address_space_operations udf_symlink_aops = {
  146. .readpage = udf_symlink_filler,
  147. };