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