Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Al Viro | 94f2f715 | 2005-04-25 18:32:12 -0700 | [diff] [blame] | 10 | #include "isofs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * ok, we cannot use strncmp, as the name is not in our data space. |
| 14 | * Thus we'll have to use isofs_match. No big problem. Match also makes |
| 15 | * some sanity tests. |
| 16 | */ |
| 17 | static int |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 18 | isofs_cmp(struct dentry *dentry, const char *compare, int dlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
| 20 | struct qstr qstr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | qstr.name = compare; |
| 22 | qstr.len = dlen; |
Al Viro | b0afd8e | 2014-10-28 18:40:11 -0400 | [diff] [blame] | 23 | if (likely(!dentry->d_op)) |
| 24 | return dentry->d_name.len != dlen || memcmp(dentry->d_name.name, compare, dlen); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 25 | return dentry->d_op->d_compare(NULL, NULL, dentry->d_name.len, dentry->d_name.name, &qstr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /* |
| 29 | * isofs_find_entry() |
| 30 | * |
| 31 | * finds an entry in the specified directory with the wanted name. It |
| 32 | * returns the inode number of the found entry, or 0 on error. |
| 33 | */ |
| 34 | static unsigned long |
| 35 | isofs_find_entry(struct inode *dir, struct dentry *dentry, |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 36 | unsigned long *block_rv, unsigned long *offset_rv, |
| 37 | char *tmpname, struct iso_directory_record *tmpde) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | unsigned long bufsize = ISOFS_BUFFER_SIZE(dir); |
| 40 | unsigned char bufbits = ISOFS_BUFFER_BITS(dir); |
| 41 | unsigned long block, f_pos, offset, block_saved, offset_saved; |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 42 | struct buffer_head *bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct isofs_sb_info *sbi = ISOFS_SB(dir->i_sb); |
| 44 | |
| 45 | if (!ISOFS_I(dir)->i_first_extent) |
| 46 | return 0; |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | f_pos = 0; |
| 49 | offset = 0; |
| 50 | block = 0; |
| 51 | |
| 52 | while (f_pos < dir->i_size) { |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 53 | struct iso_directory_record *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int de_len, match, i, dlen; |
| 55 | char *dpnt; |
| 56 | |
| 57 | if (!bh) { |
| 58 | bh = isofs_bread(dir, block); |
| 59 | if (!bh) |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | de = (struct iso_directory_record *) (bh->b_data + offset); |
| 64 | |
| 65 | de_len = *(unsigned char *) de; |
| 66 | if (!de_len) { |
| 67 | brelse(bh); |
| 68 | bh = NULL; |
| 69 | f_pos = (f_pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1); |
| 70 | block = f_pos >> bufbits; |
| 71 | offset = 0; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | block_saved = bh->b_blocknr; |
| 76 | offset_saved = offset; |
| 77 | offset += de_len; |
| 78 | f_pos += de_len; |
| 79 | |
| 80 | /* Make sure we have a full directory entry */ |
| 81 | if (offset >= bufsize) { |
| 82 | int slop = bufsize - offset + de_len; |
| 83 | memcpy(tmpde, de, slop); |
| 84 | offset &= bufsize - 1; |
| 85 | block++; |
| 86 | brelse(bh); |
| 87 | bh = NULL; |
| 88 | if (offset) { |
| 89 | bh = isofs_bread(dir, block); |
| 90 | if (!bh) |
| 91 | return 0; |
| 92 | memcpy((void *) tmpde + slop, bh->b_data, offset); |
| 93 | } |
| 94 | de = tmpde; |
| 95 | } |
| 96 | |
| 97 | dlen = de->name_len[0]; |
| 98 | dpnt = de->name; |
Jan Kara | 2deb1ac | 2008-04-30 00:52:33 -0700 | [diff] [blame] | 99 | /* Basic sanity check, whether name doesn't exceed dir entry */ |
| 100 | if (de_len < dlen + sizeof(struct iso_directory_record)) { |
| 101 | printk(KERN_NOTICE "iso9660: Corrupted directory entry" |
| 102 | " in block %lu of inode %lu\n", block, |
| 103 | dir->i_ino); |
| 104 | return 0; |
| 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | if (sbi->s_rock && |
| 108 | ((i = get_rock_ridge_filename(de, tmpname, dir)))) { |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 109 | dlen = i; /* possibly -1 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
Jeremy White | 9769f4e | 2005-06-21 17:16:53 -0700 | [diff] [blame] | 125 | * Skip hidden or associated files unless hide or showassoc, |
| 126 | * respectively, is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | */ |
| 128 | match = 0; |
| 129 | if (dlen > 0 && |
Jan Kara | 5404ac8 | 2009-06-17 16:26:27 -0700 | [diff] [blame] | 130 | (!sbi->s_hide || |
Jeremy White | 9769f4e | 2005-06-21 17:16:53 -0700 | [diff] [blame] | 131 | (!(de->flags[-sbi->s_high_sierra] & 1))) && |
Jan Kara | 5404ac8 | 2009-06-17 16:26:27 -0700 | [diff] [blame] | 132 | (sbi->s_showassoc || |
Jeremy White | 9769f4e | 2005-06-21 17:16:53 -0700 | [diff] [blame] | 133 | (!(de->flags[-sbi->s_high_sierra] & 4)))) { |
Al Viro | f643ff5 | 2014-10-28 18:37:40 -0400 | [diff] [blame] | 134 | if (dpnt && (dlen > 1 || dpnt[0] > 1)) |
| 135 | match = (isofs_cmp(dentry, dpnt, dlen) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | if (match) { |
| 138 | isofs_normalize_block_and_offset(de, |
| 139 | &block_saved, |
| 140 | &offset_saved); |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 141 | *block_rv = block_saved; |
| 142 | *offset_rv = offset_saved; |
Jeremy White | 9769f4e | 2005-06-21 17:16:53 -0700 | [diff] [blame] | 143 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | } |
Jeremy White | 9769f4e | 2005-06-21 17:16:53 -0700 | [diff] [blame] | 147 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 151 | struct dentry *isofs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
| 153 | int found; |
Borislav Petkov | cd21523 | 2007-10-16 23:30:05 -0700 | [diff] [blame] | 154 | unsigned long uninitialized_var(block); |
| 155 | unsigned long uninitialized_var(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct inode *inode; |
| 157 | struct page *page; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | page = alloc_page(GFP_USER); |
| 160 | if (!page) |
| 161 | return ERR_PTR(-ENOMEM); |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | found = isofs_find_entry(dir, dentry, |
Dave Jones | c3ed85a | 2007-07-15 23:40:03 -0700 | [diff] [blame] | 164 | &block, &offset, |
| 165 | page_address(page), |
| 166 | 1024 + page_address(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | __free_page(page); |
| 168 | |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 169 | inode = found ? isofs_iget(dir->i_sb, block, offset) : NULL; |
| 170 | |
Pekka Enberg | 2ff6b1c | 2006-01-14 13:21:09 -0800 | [diff] [blame] | 171 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |