namei.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * linux/fs/isofs/namei.c
  3. *
  4. * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
  5. *
  6. * (C) 1991 Linus Torvalds - minix filesystem
  7. */
  8. #include <linux/gfp.h>
  9. #include "isofs.h"
  10. /*
  11. * ok, we cannot use strncmp, as the name is not in our data space.
  12. * Thus we'll have to use isofs_match. No big problem. Match also makes
  13. * some sanity tests.
  14. */
  15. static int
  16. isofs_cmp(struct dentry *dentry, const char *compare, int dlen)
  17. {
  18. struct qstr qstr;
  19. if (!compare)
  20. return 1;
  21. /* check special "." and ".." files */
  22. if (dlen == 1) {
  23. /* "." */
  24. if (compare[0] == 0) {
  25. if (!dentry->d_name.len)
  26. return 0;
  27. compare = ".";
  28. } else if (compare[0] == 1) {
  29. compare = "..";
  30. dlen = 2;
  31. }
  32. }
  33. qstr.name = compare;
  34. qstr.len = dlen;
  35. return dentry->d_op->d_compare(NULL, NULL, NULL, NULL,
  36. dentry->d_name.len, dentry->d_name.name, &qstr);
  37. }
  38. /*
  39. * isofs_find_entry()
  40. *
  41. * finds an entry in the specified directory with the wanted name. It
  42. * returns the inode number of the found entry, or 0 on error.
  43. */
  44. static unsigned long
  45. isofs_find_entry(struct inode *dir, struct dentry *dentry,
  46. unsigned long *block_rv, unsigned long *offset_rv,
  47. char *tmpname, struct iso_directory_record *tmpde)
  48. {
  49. unsigned long bufsize = ISOFS_BUFFER_SIZE(dir);
  50. unsigned char bufbits = ISOFS_BUFFER_BITS(dir);
  51. unsigned long block, f_pos, offset, block_saved, offset_saved;
  52. struct buffer_head *bh = NULL;
  53. struct isofs_sb_info *sbi = ISOFS_SB(dir->i_sb);
  54. if (!ISOFS_I(dir)->i_first_extent)
  55. return 0;
  56. f_pos = 0;
  57. offset = 0;
  58. block = 0;
  59. while (f_pos < dir->i_size) {
  60. struct iso_directory_record *de;
  61. int de_len, match, i, dlen;
  62. char *dpnt;
  63. if (!bh) {
  64. bh = isofs_bread(dir, block);
  65. if (!bh)
  66. return 0;
  67. }
  68. de = (struct iso_directory_record *) (bh->b_data + offset);
  69. de_len = *(unsigned char *) de;
  70. if (!de_len) {
  71. brelse(bh);
  72. bh = NULL;
  73. f_pos = (f_pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
  74. block = f_pos >> bufbits;
  75. offset = 0;
  76. continue;
  77. }
  78. block_saved = bh->b_blocknr;
  79. offset_saved = offset;
  80. offset += de_len;
  81. f_pos += de_len;
  82. /* Make sure we have a full directory entry */
  83. if (offset >= bufsize) {
  84. int slop = bufsize - offset + de_len;
  85. memcpy(tmpde, de, slop);
  86. offset &= bufsize - 1;
  87. block++;
  88. brelse(bh);
  89. bh = NULL;
  90. if (offset) {
  91. bh = isofs_bread(dir, block);
  92. if (!bh)
  93. return 0;
  94. memcpy((void *) tmpde + slop, bh->b_data, offset);
  95. }
  96. de = tmpde;
  97. }
  98. dlen = de->name_len[0];
  99. dpnt = de->name;
  100. /* Basic sanity check, whether name doesn't exceed dir entry */
  101. if (de_len < dlen + sizeof(struct iso_directory_record)) {
  102. printk(KERN_NOTICE "iso9660: Corrupted directory entry"
  103. " in block %lu of inode %lu\n", block,
  104. dir->i_ino);
  105. return 0;
  106. }
  107. if (sbi->s_rock &&
  108. ((i = get_rock_ridge_filename(de, tmpname, dir)))) {
  109. dlen = i; /* possibly -1 */
  110. dpnt = tmpname;
  111. #ifdef CONFIG_JOLIET
  112. } else if (sbi->s_joliet_level) {
  113. dlen = get_joliet_filename(de, tmpname, dir);
  114. dpnt = tmpname;
  115. #endif
  116. } else if (sbi->s_mapping == 'a') {
  117. dlen = get_acorn_filename(de, tmpname, dir);
  118. dpnt = tmpname;
  119. } else if (sbi->s_mapping == 'n') {
  120. dlen = isofs_name_translate(de, tmpname, dir);
  121. dpnt = tmpname;
  122. }
  123. /*
  124. * Skip hidden or associated files unless hide or showassoc,
  125. * respectively, is set
  126. */
  127. match = 0;
  128. if (dlen > 0 &&
  129. (!sbi->s_hide ||
  130. (!(de->flags[-sbi->s_high_sierra] & 1))) &&
  131. (sbi->s_showassoc ||
  132. (!(de->flags[-sbi->s_high_sierra] & 4)))) {
  133. match = (isofs_cmp(dentry, dpnt, dlen) == 0);
  134. }
  135. if (match) {
  136. isofs_normalize_block_and_offset(de,
  137. &block_saved,
  138. &offset_saved);
  139. *block_rv = block_saved;
  140. *offset_rv = offset_saved;
  141. brelse(bh);
  142. return 1;
  143. }
  144. }
  145. brelse(bh);
  146. return 0;
  147. }
  148. struct dentry *isofs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  149. {
  150. int found;
  151. unsigned long uninitialized_var(block);
  152. unsigned long uninitialized_var(offset);
  153. struct inode *inode;
  154. struct page *page;
  155. page = alloc_page(GFP_USER);
  156. if (!page)
  157. return ERR_PTR(-ENOMEM);
  158. found = isofs_find_entry(dir, dentry,
  159. &block, &offset,
  160. page_address(page),
  161. 1024 + page_address(page));
  162. __free_page(page);
  163. inode = found ? isofs_iget(dir->i_sb, block, offset) : NULL;
  164. return d_splice_alias(inode, dentry);
  165. }