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