fs: change d_compare for rcu-walk
[pandora-kernel.git] / fs / affs / namei.c
1 /*
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
13 typedef int (*toupper_t)(int);
14
15 static int       affs_toupper(int ch);
16 static int       affs_hash_dentry(struct dentry *, struct qstr *);
17 static 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);
21 static int       affs_intl_toupper(int ch);
22 static int       affs_intl_hash_dentry(struct dentry *, struct qstr *);
23 static 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);
27
28 const struct dentry_operations affs_dentry_operations = {
29         .d_hash         = affs_hash_dentry,
30         .d_compare      = affs_compare_dentry,
31 };
32
33 static const struct dentry_operations affs_intl_dentry_operations = {
34         .d_hash         = affs_intl_hash_dentry,
35         .d_compare      = affs_intl_compare_dentry,
36 };
37
38
39 /* Simple toupper() for DOS\1 */
40
41 static int
42 affs_toupper(int ch)
43 {
44         return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
45 }
46
47 /* International toupper() for DOS\3 ("international") */
48
49 static int
50 affs_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
57 static inline toupper_t
58 affs_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  */
66 static 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
86 static int
87 affs_hash_dentry(struct dentry *dentry, struct qstr *qstr)
88 {
89         return __affs_hash_dentry(dentry, qstr, affs_toupper);
90 }
91 static int
92 affs_intl_hash_dentry(struct dentry *dentry, struct qstr *qstr)
93 {
94         return __affs_hash_dentry(dentry, qstr, affs_intl_toupper);
95 }
96
97 static inline int __affs_compare_dentry(unsigned int len,
98                 const char *str, const struct qstr *name, toupper_t toupper)
99 {
100         const u8 *aname = str;
101         const u8 *bname = name->name;
102
103         /*
104          * 'str' is the name of an already existing dentry, so the name
105          * must be valid. 'name' must be validated first.
106          */
107
108         if (affs_check_name(name->name, name->len))
109                 return 1;
110
111         /*
112          * If the names are longer than the allowed 30 chars,
113          * the excess is ignored, so their length may differ.
114          */
115         if (len >= 30) {
116                 if (name->len < 30)
117                         return 1;
118                 len = 30;
119         } else if (len != name->len)
120                 return 1;
121
122         for (; len > 0; len--)
123                 if (toupper(*aname++) != toupper(*bname++))
124                         return 1;
125
126         return 0;
127 }
128
129 static int
130 affs_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)
133 {
134         return __affs_compare_dentry(len, str, name, affs_toupper);
135 }
136 static int
137 affs_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)
140 {
141         return __affs_compare_dentry(len, str, name, affs_intl_toupper);
142 }
143
144 /*
145  * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
146  */
147
148 static inline int
149 affs_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
167 int
168 affs_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
180 static struct buffer_head *
181 affs_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
209 struct dentry *
210 affs_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);
221         if (IS_ERR(bh))
222                 return ERR_CAST(bh);
223         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);
235                 inode = affs_iget(sb, ino);
236                 if (IS_ERR(inode))
237                         return ERR_CAST(inode);
238         }
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
244 int
245 affs_unlink(struct inode *dir, struct dentry *dentry)
246 {
247         pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino,
248                  dentry->d_inode->i_ino,
249                  (int)dentry->d_name.len, dentry->d_name.name);
250
251         return affs_remove_header(dentry);
252 }
253
254 int
255 affs_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
284 int
285 affs_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
313 int
314 affs_rmdir(struct inode *dir, struct dentry *dentry)
315 {
316         pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino,
317                  dentry->d_inode->i_ino,
318                  (int)dentry->d_name.len, dentry->d_name.name);
319
320         return affs_remove_header(dentry);
321 }
322
323 int
324 affs_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 == '/') {
354                 struct affs_sb_info *sbi = AFFS_SB(sb);
355                 while (*symname == '/')
356                         symname++;
357                 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);
361         }
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
391 err:
392         inode->i_nlink = 0;
393         mark_inode_dirty(inode);
394         iput(inode);
395         return error;
396 }
397
398 int
399 affs_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
409 int
410 affs_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
432         bh = affs_bread(sb, old_dentry->d_inode->i_ino);
433         if (!bh)
434                 return -EIO;
435
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
451 done:
452         mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
453         affs_brelse(bh);
454         return retval;
455 }