blob: 547d5deb0d42b2d6e80eeca9dd0f9e6526f913f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/affs/namei.c
3 *
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
5 *
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
7 *
8 * (C) 1991 Linus Torvalds - minix filesystem
9 */
10
11#include "affs.h"
12
13typedef int (*toupper_t)(int);
14
15static int affs_toupper(int ch);
16static int affs_hash_dentry(struct dentry *, struct qstr *);
Nick Piggin621e1552011-01-07 17:49:27 +110017static int affs_compare_dentry(const struct dentry *parent,
18 const struct inode *pinode,
19 const struct dentry *dentry, const struct inode *inode,
20 unsigned int len, const char *str, const struct qstr *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static int affs_intl_toupper(int ch);
22static int affs_intl_hash_dentry(struct dentry *, struct qstr *);
Nick Piggin621e1552011-01-07 17:49:27 +110023static int affs_intl_compare_dentry(const struct dentry *parent,
24 const struct inode *pinode,
25 const struct dentry *dentry, const struct inode *inode,
26 unsigned int len, const char *str, const struct qstr *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Al Viroe16404e2009-02-20 05:55:13 +000028const struct dentry_operations affs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .d_hash = affs_hash_dentry,
30 .d_compare = affs_compare_dentry,
31};
32
Al Viroe16404e2009-02-20 05:55:13 +000033static const struct dentry_operations affs_intl_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .d_hash = affs_intl_hash_dentry,
35 .d_compare = affs_intl_compare_dentry,
36};
37
38
39/* Simple toupper() for DOS\1 */
40
41static int
42affs_toupper(int ch)
43{
44 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
45}
46
47/* International toupper() for DOS\3 ("international") */
48
49static int
50affs_intl_toupper(int ch)
51{
52 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
53 && ch <= 0xFE && ch != 0xF7) ?
54 ch - ('a' - 'A') : ch;
55}
56
57static inline toupper_t
58affs_get_toupper(struct super_block *sb)
59{
60 return AFFS_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
61}
62
63/*
64 * Note: the dentry argument is the parent dentry.
65 */
66static inline int
67__affs_hash_dentry(struct dentry *dentry, struct qstr *qstr, toupper_t toupper)
68{
69 const u8 *name = qstr->name;
70 unsigned long hash;
71 int i;
72
73 i = affs_check_name(qstr->name,qstr->len);
74 if (i)
75 return i;
76
77 hash = init_name_hash();
78 i = min(qstr->len, 30u);
79 for (; i > 0; name++, i--)
80 hash = partial_name_hash(toupper(*name), hash);
81 qstr->hash = end_name_hash(hash);
82
83 return 0;
84}
85
86static int
87affs_hash_dentry(struct dentry *dentry, struct qstr *qstr)
88{
89 return __affs_hash_dentry(dentry, qstr, affs_toupper);
90}
91static int
92affs_intl_hash_dentry(struct dentry *dentry, struct qstr *qstr)
93{
94 return __affs_hash_dentry(dentry, qstr, affs_intl_toupper);
95}
96
Nick Piggin621e1552011-01-07 17:49:27 +110097static inline int __affs_compare_dentry(unsigned int len,
98 const char *str, const struct qstr *name, toupper_t toupper)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Nick Piggin621e1552011-01-07 17:49:27 +1100100 const u8 *aname = str;
101 const u8 *bname = name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Nick Piggin621e1552011-01-07 17:49:27 +1100103 /*
104 * 'str' is the name of an already existing dentry, so the name
105 * must be valid. 'name' must be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107
Nick Piggin621e1552011-01-07 17:49:27 +1100108 if (affs_check_name(name->name, name->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 1;
110
Nick Piggin621e1552011-01-07 17:49:27 +1100111 /*
112 * If the names are longer than the allowed 30 chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * the excess is ignored, so their length may differ.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (len >= 30) {
Nick Piggin621e1552011-01-07 17:49:27 +1100116 if (name->len < 30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118 len = 30;
Nick Piggin621e1552011-01-07 17:49:27 +1100119 } else if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 1;
121
122 for (; len > 0; len--)
123 if (toupper(*aname++) != toupper(*bname++))
124 return 1;
125
126 return 0;
127}
128
129static int
Nick Piggin621e1552011-01-07 17:49:27 +1100130affs_compare_dentry(const struct dentry *parent, const struct inode *pinode,
131 const struct dentry *dentry, const struct inode *inode,
132 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Nick Piggin621e1552011-01-07 17:49:27 +1100134 return __affs_compare_dentry(len, str, name, affs_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136static int
Nick Piggin621e1552011-01-07 17:49:27 +1100137affs_intl_compare_dentry(const struct dentry *parent,const struct inode *pinode,
138 const struct dentry *dentry, const struct inode *inode,
139 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Nick Piggin621e1552011-01-07 17:49:27 +1100141 return __affs_compare_dentry(len, str, name, affs_intl_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/*
145 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
146 */
147
148static inline int
149affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
150{
151 const u8 *name = dentry->d_name.name;
152 int len = dentry->d_name.len;
153
154 if (len >= 30) {
155 if (*name2 < 30)
156 return 0;
157 len = 30;
158 } else if (len != *name2)
159 return 0;
160
161 for (name2++; len > 0; len--)
162 if (toupper(*name++) != toupper(*name2++))
163 return 0;
164 return 1;
165}
166
167int
168affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
169{
170 toupper_t toupper = affs_get_toupper(sb);
171 int hash;
172
173 hash = len = min(len, 30u);
174 for (; len > 0; len--)
175 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
176
177 return hash % AFFS_SB(sb)->s_hashsize;
178}
179
180static struct buffer_head *
181affs_find_entry(struct inode *dir, struct dentry *dentry)
182{
183 struct super_block *sb = dir->i_sb;
184 struct buffer_head *bh;
185 toupper_t toupper = affs_get_toupper(sb);
186 u32 key;
187
188 pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name);
189
190 bh = affs_bread(sb, dir->i_ino);
191 if (!bh)
192 return ERR_PTR(-EIO);
193
194 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
195
196 for (;;) {
197 affs_brelse(bh);
198 if (key == 0)
199 return NULL;
200 bh = affs_bread(sb, key);
201 if (!bh)
202 return ERR_PTR(-EIO);
203 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
204 return bh;
205 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
206 }
207}
208
209struct dentry *
210affs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
211{
212 struct super_block *sb = dir->i_sb;
213 struct buffer_head *bh;
214 struct inode *inode = NULL;
215
216 pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name);
217
218 affs_lock_dir(dir);
219 bh = affs_find_entry(dir, dentry);
220 affs_unlock_dir(dir);
David Howells210f8552008-02-07 00:15:29 -0800221 if (IS_ERR(bh))
David Howellse231c2e2008-02-07 00:15:26 -0800222 return ERR_CAST(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (bh) {
224 u32 ino = bh->b_blocknr;
225
226 /* store the real header ino in d_fsdata for faster lookups */
227 dentry->d_fsdata = (void *)(long)ino;
228 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
229 //link to dirs disabled
230 //case ST_LINKDIR:
231 case ST_LINKFILE:
232 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
233 }
234 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800235 inode = affs_iget(sb, ino);
236 if (IS_ERR(inode))
Julia Lawallcccad8f2010-05-26 14:44:23 -0700237 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 dentry->d_op = AFFS_SB(sb)->s_flags & SF_INTL ? &affs_intl_dentry_operations : &affs_dentry_operations;
240 d_add(dentry, inode);
241 return NULL;
242}
243
244int
245affs_unlink(struct inode *dir, struct dentry *dentry)
246{
Roman Zippeldca3c332008-04-29 17:02:20 +0200247 pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino,
248 dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 (int)dentry->d_name.len, dentry->d_name.name);
250
251 return affs_remove_header(dentry);
252}
253
254int
255affs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
256{
257 struct super_block *sb = dir->i_sb;
258 struct inode *inode;
259 int error;
260
261 pr_debug("AFFS: create(%lu,\"%.*s\",0%o)\n",dir->i_ino,(int)dentry->d_name.len,
262 dentry->d_name.name,mode);
263
264 inode = affs_new_inode(dir);
265 if (!inode)
266 return -ENOSPC;
267
268 inode->i_mode = mode;
269 mode_to_prot(inode);
270 mark_inode_dirty(inode);
271
272 inode->i_op = &affs_file_inode_operations;
273 inode->i_fop = &affs_file_operations;
274 inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
275 error = affs_add_entry(dir, inode, dentry, ST_FILE);
276 if (error) {
277 inode->i_nlink = 0;
278 iput(inode);
279 return error;
280 }
281 return 0;
282}
283
284int
285affs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
286{
287 struct inode *inode;
288 int error;
289
290 pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%o)\n",dir->i_ino,
291 (int)dentry->d_name.len,dentry->d_name.name,mode);
292
293 inode = affs_new_inode(dir);
294 if (!inode)
295 return -ENOSPC;
296
297 inode->i_mode = S_IFDIR | mode;
298 mode_to_prot(inode);
299
300 inode->i_op = &affs_dir_inode_operations;
301 inode->i_fop = &affs_dir_operations;
302
303 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
304 if (error) {
305 inode->i_nlink = 0;
306 mark_inode_dirty(inode);
307 iput(inode);
308 return error;
309 }
310 return 0;
311}
312
313int
314affs_rmdir(struct inode *dir, struct dentry *dentry)
315{
Roman Zippeldca3c332008-04-29 17:02:20 +0200316 pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino,
317 dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (int)dentry->d_name.len, dentry->d_name.name);
319
320 return affs_remove_header(dentry);
321}
322
323int
324affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
325{
326 struct super_block *sb = dir->i_sb;
327 struct buffer_head *bh;
328 struct inode *inode;
329 char *p;
330 int i, maxlen, error;
331 char c, lc;
332
333 pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino,
334 (int)dentry->d_name.len,dentry->d_name.name,symname);
335
336 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
337 inode = affs_new_inode(dir);
338 if (!inode)
339 return -ENOSPC;
340
341 inode->i_op = &affs_symlink_inode_operations;
342 inode->i_data.a_ops = &affs_symlink_aops;
343 inode->i_mode = S_IFLNK | 0777;
344 mode_to_prot(inode);
345
346 error = -EIO;
347 bh = affs_bread(sb, inode->i_ino);
348 if (!bh)
349 goto err;
350 i = 0;
351 p = (char *)AFFS_HEAD(bh)->table;
352 lc = '/';
353 if (*symname == '/') {
Al Viro29333922010-01-24 00:04:07 -0500354 struct affs_sb_info *sbi = AFFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 while (*symname == '/')
356 symname++;
Al Viro29333922010-01-24 00:04:07 -0500357 spin_lock(&sbi->symlink_lock);
358 while (sbi->s_volume[i]) /* Cannot overflow */
359 *p++ = sbi->s_volume[i++];
360 spin_unlock(&sbi->symlink_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 while (i < maxlen && (c = *symname++)) {
363 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
364 *p++ = '/';
365 i++;
366 symname += 2;
367 lc = '/';
368 } else if (c == '.' && lc == '/' && *symname == '/') {
369 symname++;
370 lc = '/';
371 } else {
372 *p++ = c;
373 lc = c;
374 i++;
375 }
376 if (lc == '/')
377 while (*symname == '/')
378 symname++;
379 }
380 *p = 0;
381 mark_buffer_dirty_inode(bh, inode);
382 affs_brelse(bh);
383 mark_inode_dirty(inode);
384
385 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
386 if (error)
387 goto err;
388
389 return 0;
390
391err:
392 inode->i_nlink = 0;
393 mark_inode_dirty(inode);
394 iput(inode);
395 return error;
396}
397
398int
399affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
400{
401 struct inode *inode = old_dentry->d_inode;
402
403 pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino,
404 (int)dentry->d_name.len,dentry->d_name.name);
405
406 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
407}
408
409int
410affs_rename(struct inode *old_dir, struct dentry *old_dentry,
411 struct inode *new_dir, struct dentry *new_dentry)
412{
413 struct super_block *sb = old_dir->i_sb;
414 struct buffer_head *bh = NULL;
415 int retval;
416
417 pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
418 (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
419 (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
420
421 retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len);
422 if (retval)
423 return retval;
424
425 /* Unlink destination if it already exists */
426 if (new_dentry->d_inode) {
427 retval = affs_remove_header(new_dentry);
428 if (retval)
429 return retval;
430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 bh = affs_bread(sb, old_dentry->d_inode->i_ino);
433 if (!bh)
Florin Malita3ac81412006-05-25 18:44:23 -0700434 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Remove header from its parent directory. */
437 affs_lock_dir(old_dir);
438 retval = affs_remove_hash(old_dir, bh);
439 affs_unlock_dir(old_dir);
440 if (retval)
441 goto done;
442
443 /* And insert it into the new directory with the new name. */
444 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
445 affs_fix_checksum(sb, bh);
446 affs_lock_dir(new_dir);
447 retval = affs_insert_hash(new_dir, bh);
448 affs_unlock_dir(new_dir);
449 /* TODO: move it back to old_dir, if error? */
450
451done:
452 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
453 affs_brelse(bh);
454 return retval;
455}