blob: c36cbb4537a26e0348a44f0ec7a65f64ac3204bf [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);
Linus Torvaldsda53be12013-05-21 15:22:44 -070016static int affs_hash_dentry(const struct dentry *, struct qstr *);
17static int affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +110018 unsigned int len, const char *str, const struct qstr *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static int affs_intl_toupper(int ch);
Linus Torvaldsda53be12013-05-21 15:22:44 -070020static int affs_intl_hash_dentry(const struct dentry *, struct qstr *);
21static int affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +110022 unsigned int len, const char *str, const struct qstr *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Al Viroe16404e2009-02-20 05:55:13 +000024const struct dentry_operations affs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 .d_hash = affs_hash_dentry,
26 .d_compare = affs_compare_dentry,
27};
28
Al Viroa1298802011-01-12 16:45:19 -050029const struct dentry_operations affs_intl_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .d_hash = affs_intl_hash_dentry,
31 .d_compare = affs_intl_compare_dentry,
32};
33
34
35/* Simple toupper() for DOS\1 */
36
37static int
38affs_toupper(int ch)
39{
40 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
41}
42
43/* International toupper() for DOS\3 ("international") */
44
45static int
46affs_intl_toupper(int ch)
47{
48 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
49 && ch <= 0xFE && ch != 0xF7) ?
50 ch - ('a' - 'A') : ch;
51}
52
53static inline toupper_t
54affs_get_toupper(struct super_block *sb)
55{
56 return AFFS_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
57}
58
59/*
60 * Note: the dentry argument is the parent dentry.
61 */
62static inline int
Nick Pigginb1e6a012011-01-07 17:49:28 +110063__affs_hash_dentry(struct qstr *qstr, toupper_t toupper)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 const u8 *name = qstr->name;
66 unsigned long hash;
67 int i;
68
Nick Pigginb1e6a012011-01-07 17:49:28 +110069 i = affs_check_name(qstr->name, qstr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (i)
71 return i;
72
73 hash = init_name_hash();
74 i = min(qstr->len, 30u);
75 for (; i > 0; name++, i--)
76 hash = partial_name_hash(toupper(*name), hash);
77 qstr->hash = end_name_hash(hash);
78
79 return 0;
80}
81
82static int
Linus Torvaldsda53be12013-05-21 15:22:44 -070083affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Nick Pigginb1e6a012011-01-07 17:49:28 +110085 return __affs_hash_dentry(qstr, affs_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87static int
Linus Torvaldsda53be12013-05-21 15:22:44 -070088affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Nick Pigginb1e6a012011-01-07 17:49:28 +110090 return __affs_hash_dentry(qstr, affs_intl_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Nick Piggin621e1552011-01-07 17:49:27 +110093static inline int __affs_compare_dentry(unsigned int len,
94 const char *str, const struct qstr *name, toupper_t toupper)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Nick Piggin621e1552011-01-07 17:49:27 +110096 const u8 *aname = str;
97 const u8 *bname = name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Nick Piggin621e1552011-01-07 17:49:27 +110099 /*
100 * 'str' is the name of an already existing dentry, so the name
101 * must be valid. 'name' must be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103
Nick Piggin621e1552011-01-07 17:49:27 +1100104 if (affs_check_name(name->name, name->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 1;
106
Nick Piggin621e1552011-01-07 17:49:27 +1100107 /*
108 * If the names are longer than the allowed 30 chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * the excess is ignored, so their length may differ.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (len >= 30) {
Nick Piggin621e1552011-01-07 17:49:27 +1100112 if (name->len < 30)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 1;
114 len = 30;
Nick Piggin621e1552011-01-07 17:49:27 +1100115 } else if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 1;
117
118 for (; len > 0; len--)
119 if (toupper(*aname++) != toupper(*bname++))
120 return 1;
121
122 return 0;
123}
124
125static int
Linus Torvaldsda53be12013-05-21 15:22:44 -0700126affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100127 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Nick Piggin621e1552011-01-07 17:49:27 +1100129 return __affs_compare_dentry(len, str, name, affs_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131static int
Linus Torvaldsda53be12013-05-21 15:22:44 -0700132affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100133 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Nick Piggin621e1552011-01-07 17:49:27 +1100135 return __affs_compare_dentry(len, str, name, affs_intl_toupper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/*
139 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
140 */
141
142static inline int
143affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
144{
145 const u8 *name = dentry->d_name.name;
146 int len = dentry->d_name.len;
147
148 if (len >= 30) {
149 if (*name2 < 30)
150 return 0;
151 len = 30;
152 } else if (len != *name2)
153 return 0;
154
155 for (name2++; len > 0; len--)
156 if (toupper(*name++) != toupper(*name2++))
157 return 0;
158 return 1;
159}
160
161int
162affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
163{
164 toupper_t toupper = affs_get_toupper(sb);
165 int hash;
166
167 hash = len = min(len, 30u);
168 for (; len > 0; len--)
169 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
170
171 return hash % AFFS_SB(sb)->s_hashsize;
172}
173
174static struct buffer_head *
175affs_find_entry(struct inode *dir, struct dentry *dentry)
176{
177 struct super_block *sb = dir->i_sb;
178 struct buffer_head *bh;
179 toupper_t toupper = affs_get_toupper(sb);
180 u32 key;
181
182 pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name);
183
184 bh = affs_bread(sb, dir->i_ino);
185 if (!bh)
186 return ERR_PTR(-EIO);
187
188 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
189
190 for (;;) {
191 affs_brelse(bh);
192 if (key == 0)
193 return NULL;
194 bh = affs_bread(sb, key);
195 if (!bh)
196 return ERR_PTR(-EIO);
197 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
198 return bh;
199 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
200 }
201}
202
203struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -0400204affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct super_block *sb = dir->i_sb;
207 struct buffer_head *bh;
208 struct inode *inode = NULL;
209
210 pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name);
211
212 affs_lock_dir(dir);
213 bh = affs_find_entry(dir, dentry);
214 affs_unlock_dir(dir);
David Howells210f8552008-02-07 00:15:29 -0800215 if (IS_ERR(bh))
David Howellse231c2e2008-02-07 00:15:26 -0800216 return ERR_CAST(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (bh) {
218 u32 ino = bh->b_blocknr;
219
220 /* store the real header ino in d_fsdata for faster lookups */
221 dentry->d_fsdata = (void *)(long)ino;
222 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
223 //link to dirs disabled
224 //case ST_LINKDIR:
225 case ST_LINKFILE:
226 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
227 }
228 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800229 inode = affs_iget(sb, ino);
230 if (IS_ERR(inode))
Julia Lawallcccad8f2010-05-26 14:44:23 -0700231 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 d_add(dentry, inode);
234 return NULL;
235}
236
237int
238affs_unlink(struct inode *dir, struct dentry *dentry)
239{
Roman Zippeldca3c332008-04-29 17:02:20 +0200240 pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino,
241 dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 (int)dentry->d_name.len, dentry->d_name.name);
243
244 return affs_remove_header(dentry);
245}
246
247int
Al Viroebfc3b42012-06-10 18:05:36 -0400248affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct super_block *sb = dir->i_sb;
251 struct inode *inode;
252 int error;
253
Al Viro4acdaf22011-07-26 01:42:34 -0400254 pr_debug("AFFS: create(%lu,\"%.*s\",0%ho)\n",dir->i_ino,(int)dentry->d_name.len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dentry->d_name.name,mode);
256
257 inode = affs_new_inode(dir);
258 if (!inode)
259 return -ENOSPC;
260
261 inode->i_mode = mode;
262 mode_to_prot(inode);
263 mark_inode_dirty(inode);
264
265 inode->i_op = &affs_file_inode_operations;
266 inode->i_fop = &affs_file_operations;
267 inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
268 error = affs_add_entry(dir, inode, dentry, ST_FILE);
269 if (error) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200270 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 iput(inode);
272 return error;
273 }
274 return 0;
275}
276
277int
Al Viro18bb1db2011-07-26 01:41:39 -0400278affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct inode *inode;
281 int error;
282
Al Viro18bb1db2011-07-26 01:41:39 -0400283 pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%ho)\n",dir->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 (int)dentry->d_name.len,dentry->d_name.name,mode);
285
286 inode = affs_new_inode(dir);
287 if (!inode)
288 return -ENOSPC;
289
290 inode->i_mode = S_IFDIR | mode;
291 mode_to_prot(inode);
292
293 inode->i_op = &affs_dir_inode_operations;
294 inode->i_fop = &affs_dir_operations;
295
296 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
297 if (error) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200298 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mark_inode_dirty(inode);
300 iput(inode);
301 return error;
302 }
303 return 0;
304}
305
306int
307affs_rmdir(struct inode *dir, struct dentry *dentry)
308{
Roman Zippeldca3c332008-04-29 17:02:20 +0200309 pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino,
310 dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 (int)dentry->d_name.len, dentry->d_name.name);
312
313 return affs_remove_header(dentry);
314}
315
316int
317affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
318{
319 struct super_block *sb = dir->i_sb;
320 struct buffer_head *bh;
321 struct inode *inode;
322 char *p;
323 int i, maxlen, error;
324 char c, lc;
325
326 pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino,
327 (int)dentry->d_name.len,dentry->d_name.name,symname);
328
329 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
330 inode = affs_new_inode(dir);
331 if (!inode)
332 return -ENOSPC;
333
334 inode->i_op = &affs_symlink_inode_operations;
335 inode->i_data.a_ops = &affs_symlink_aops;
336 inode->i_mode = S_IFLNK | 0777;
337 mode_to_prot(inode);
338
339 error = -EIO;
340 bh = affs_bread(sb, inode->i_ino);
341 if (!bh)
342 goto err;
343 i = 0;
344 p = (char *)AFFS_HEAD(bh)->table;
345 lc = '/';
346 if (*symname == '/') {
Al Viro29333922010-01-24 00:04:07 -0500347 struct affs_sb_info *sbi = AFFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 while (*symname == '/')
349 symname++;
Al Viro29333922010-01-24 00:04:07 -0500350 spin_lock(&sbi->symlink_lock);
351 while (sbi->s_volume[i]) /* Cannot overflow */
352 *p++ = sbi->s_volume[i++];
353 spin_unlock(&sbi->symlink_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 while (i < maxlen && (c = *symname++)) {
356 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
357 *p++ = '/';
358 i++;
359 symname += 2;
360 lc = '/';
361 } else if (c == '.' && lc == '/' && *symname == '/') {
362 symname++;
363 lc = '/';
364 } else {
365 *p++ = c;
366 lc = c;
367 i++;
368 }
369 if (lc == '/')
370 while (*symname == '/')
371 symname++;
372 }
373 *p = 0;
374 mark_buffer_dirty_inode(bh, inode);
375 affs_brelse(bh);
376 mark_inode_dirty(inode);
377
378 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
379 if (error)
380 goto err;
381
382 return 0;
383
384err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200385 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mark_inode_dirty(inode);
387 iput(inode);
388 return error;
389}
390
391int
392affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
393{
394 struct inode *inode = old_dentry->d_inode;
395
396 pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino,
397 (int)dentry->d_name.len,dentry->d_name.name);
398
399 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
400}
401
402int
403affs_rename(struct inode *old_dir, struct dentry *old_dentry,
404 struct inode *new_dir, struct dentry *new_dentry)
405{
406 struct super_block *sb = old_dir->i_sb;
407 struct buffer_head *bh = NULL;
408 int retval;
409
410 pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
411 (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
412 (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
413
414 retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len);
415 if (retval)
416 return retval;
417
418 /* Unlink destination if it already exists */
419 if (new_dentry->d_inode) {
420 retval = affs_remove_header(new_dentry);
421 if (retval)
422 return retval;
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 bh = affs_bread(sb, old_dentry->d_inode->i_ino);
426 if (!bh)
Florin Malita3ac81412006-05-25 18:44:23 -0700427 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Remove header from its parent directory. */
430 affs_lock_dir(old_dir);
431 retval = affs_remove_hash(old_dir, bh);
432 affs_unlock_dir(old_dir);
433 if (retval)
434 goto done;
435
436 /* And insert it into the new directory with the new name. */
437 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
438 affs_fix_checksum(sb, bh);
439 affs_lock_dir(new_dir);
440 retval = affs_insert_hash(new_dir, bh);
441 affs_unlock_dir(new_dir);
442 /* TODO: move it back to old_dir, if error? */
443
444done:
445 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
446 affs_brelse(bh);
447 return retval;
448}