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 
9 #include <linux/sched.h>
10 #include <linux/iso_fs.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/fcntl.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/config.h>	/* Joliet? */
18 
19 #include <asm/uaccess.h>
20 
21 /*
22  * ok, we cannot use strncmp, as the name is not in our data space.
23  * Thus we'll have to use isofs_match. No big problem. Match also makes
24  * some sanity tests.
25  */
26 static int
isofs_cmp(struct dentry * dentry,const char * compare,int dlen)27 isofs_cmp(struct dentry * dentry, const char * compare, int dlen)
28 {
29 	struct qstr qstr;
30 
31 	if (!compare)
32 		return 1;
33 
34 	/* check special "." and ".." files */
35 	if (dlen == 1) {
36 		/* "." */
37 		if (compare[0] == 0) {
38 			if (!dentry->d_name.len)
39 				return 0;
40 			compare = ".";
41 		} else if (compare[0] == 1) {
42 			compare = "..";
43 			dlen = 2;
44 		}
45 	}
46 
47 	qstr.name = compare;
48 	qstr.len = dlen;
49 	return dentry->d_op->d_compare(dentry, &dentry->d_name, &qstr);
50 }
51 
52 /*
53  *	isofs_find_entry()
54  *
55  * finds an entry in the specified directory with the wanted name. It
56  * returns the inode number of the found entry, or 0 on error.
57  */
58 static unsigned long
isofs_find_entry(struct inode * dir,struct dentry * dentry,char * tmpname,struct iso_directory_record * tmpde)59 isofs_find_entry(struct inode *dir, struct dentry *dentry,
60 	char * tmpname, struct iso_directory_record * tmpde)
61 {
62 	unsigned long inode_number;
63 	unsigned long bufsize = ISOFS_BUFFER_SIZE(dir);
64 	unsigned char bufbits = ISOFS_BUFFER_BITS(dir);
65 	unsigned int block, f_pos, offset;
66 	struct buffer_head * bh = NULL;
67 
68 	if (!dir->u.isofs_i.i_first_extent)
69 		return 0;
70 
71 	f_pos = 0;
72 	offset = 0;
73 	block = 0;
74 
75 	while (f_pos < dir->i_size) {
76 		struct iso_directory_record * de;
77 		int de_len, match, i, dlen;
78 		char *dpnt;
79 
80 		if (!bh) {
81 			bh = isofs_bread(dir, block);
82 			if (!bh)
83 				return 0;
84 		}
85 
86 		de = (struct iso_directory_record *) (bh->b_data + offset);
87 		inode_number = (bh->b_blocknr << bufbits) + offset;
88 
89 		de_len = *(unsigned char *) de;
90 		if (!de_len) {
91 			brelse(bh);
92 			bh = NULL;
93 			f_pos = (f_pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
94 			block = f_pos >> bufbits;
95 			offset = 0;
96 			continue;
97 		}
98 
99 		offset += de_len;
100 		f_pos += de_len;
101 
102 		/* Make sure we have a full directory entry */
103 		if (offset >= bufsize) {
104 			int slop = bufsize - offset + de_len;
105 			memcpy(tmpde, de, slop);
106 			offset &= bufsize - 1;
107 			block++;
108 			brelse(bh);
109 			bh = NULL;
110 			if (offset) {
111 				bh = isofs_bread(dir, block);
112 				if (!bh)
113 					return 0;
114 				memcpy((void *) tmpde + slop, bh->b_data, offset);
115 			}
116 			de = tmpde;
117 		}
118 
119 		dlen = de->name_len[0];
120 		dpnt = de->name;
121 
122 		if (dir->i_sb->u.isofs_sb.s_rock &&
123 		    ((i = get_rock_ridge_filename(de, tmpname, dir)))) {
124 			dlen = i; 	/* possibly -1 */
125 			dpnt = tmpname;
126 #ifdef CONFIG_JOLIET
127 		} else if (dir->i_sb->u.isofs_sb.s_joliet_level) {
128 			dlen = get_joliet_filename(de, tmpname, dir);
129 			dpnt = tmpname;
130 #endif
131 		} else if (dir->i_sb->u.isofs_sb.s_mapping == 'a') {
132 			dlen = get_acorn_filename(de, tmpname, dir);
133 			dpnt = tmpname;
134 		} else if (dir->i_sb->u.isofs_sb.s_mapping == 'n') {
135 			dlen = isofs_name_translate(de, tmpname, dir);
136 			dpnt = tmpname;
137 		}
138 
139 		/*
140 		 * Skip hidden or associated files unless unhide is set
141 		 */
142 		match = 0;
143 		if (dlen > 0 &&
144 		    (!(de->flags[-dir->i_sb->u.isofs_sb.s_high_sierra] & 5)
145 		     || dir->i_sb->u.isofs_sb.s_unhide == 'y'))
146 		{
147 			match = (isofs_cmp(dentry,dpnt,dlen) == 0);
148 		}
149 		if (match) {
150 			if (bh) brelse(bh);
151 			return inode_number;
152 		}
153 	}
154 	if (bh) brelse(bh);
155 	return 0;
156 }
157 
isofs_lookup(struct inode * dir,struct dentry * dentry)158 struct dentry *isofs_lookup(struct inode * dir, struct dentry * dentry)
159 {
160 	unsigned long ino;
161 	struct inode *inode;
162 	struct page *page;
163 
164 	dentry->d_op = dir->i_sb->s_root->d_op;
165 
166 	page = alloc_page(GFP_USER);
167 	if (!page)
168 		return ERR_PTR(-ENOMEM);
169 
170 	ino = isofs_find_entry(dir, dentry, page_address(page),
171 			       1024 + page_address(page));
172 	__free_page(page);
173 
174 	inode = NULL;
175 	if (ino) {
176 		inode = iget(dir->i_sb, ino);
177 		if (!inode)
178 			return ERR_PTR(-EACCES);
179 	}
180 	d_add(dentry, inode);
181 	return NULL;
182 }
183