blob: a2d68f828d5306ce96e3e11b67be54621f92c1c0 [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 *);
Al Viro6fa67e72016-07-31 16:37:25 -040017static int affs_compare_dentry(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 *);
Al Viro6fa67e72016-07-31 16:37:25 -040021static int affs_intl_compare_dentry(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{
Fabian Frederick79bda4d2015-04-16 12:48:24 -070056 return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
57 affs_intl_toupper : affs_toupper;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60/*
61 * Note: the dentry argument is the parent dentry.
62 */
63static inline int
Linus Torvalds8387ff22016-06-10 07:51:30 -070064__affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 const u8 *name = qstr->name;
67 unsigned long hash;
Fabian Frederickeeb36f82015-02-17 13:46:20 -080068 int retval;
69 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Fabian Frederickeeb36f82015-02-17 13:46:20 -080071 retval = affs_check_name(qstr->name, qstr->len, notruncate);
72 if (retval)
73 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds8387ff22016-06-10 07:51:30 -070075 hash = init_name_hash(dentry);
Fabian Frederickf1578532015-02-17 13:46:23 -080076 len = min(qstr->len, AFFSNAMEMAX);
Fabian Frederickeeb36f82015-02-17 13:46:20 -080077 for (; len > 0; name++, len--)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 hash = partial_name_hash(toupper(*name), hash);
79 qstr->hash = end_name_hash(hash);
80
81 return 0;
82}
83
84static int
Linus Torvaldsda53be12013-05-21 15:22:44 -070085affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvalds8387ff22016-06-10 07:51:30 -070087 return __affs_hash_dentry(dentry, qstr, affs_toupper,
Fabian Frederick8ca57722014-04-07 15:39:01 -070088 affs_nofilenametruncate(dentry));
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Fabian Frederick8ca57722014-04-07 15:39:01 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int
Linus Torvaldsda53be12013-05-21 15:22:44 -070093affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvalds8387ff22016-06-10 07:51:30 -070095 return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
Fabian Frederick8ca57722014-04-07 15:39:01 -070096 affs_nofilenametruncate(dentry));
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Nick Piggin621e1552011-01-07 17:49:27 +1100100static inline int __affs_compare_dentry(unsigned int len,
Fabian Frederick8ca57722014-04-07 15:39:01 -0700101 const char *str, const struct qstr *name, toupper_t toupper,
102 bool notruncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Nick Piggin621e1552011-01-07 17:49:27 +1100104 const u8 *aname = str;
105 const u8 *bname = name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Nick Piggin621e1552011-01-07 17:49:27 +1100107 /*
108 * 'str' is the name of an already existing dentry, so the name
109 * must be valid. 'name' must be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
111
Fabian Frederick8ca57722014-04-07 15:39:01 -0700112 if (affs_check_name(name->name, name->len, notruncate))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 1;
114
Nick Piggin621e1552011-01-07 17:49:27 +1100115 /*
116 * If the names are longer than the allowed 30 chars,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * the excess is ignored, so their length may differ.
118 */
Fabian Frederickf1578532015-02-17 13:46:23 -0800119 if (len >= AFFSNAMEMAX) {
120 if (name->len < AFFSNAMEMAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 1;
Fabian Frederickf1578532015-02-17 13:46:23 -0800122 len = AFFSNAMEMAX;
Nick Piggin621e1552011-01-07 17:49:27 +1100123 } else if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 1;
125
126 for (; len > 0; len--)
127 if (toupper(*aname++) != toupper(*bname++))
128 return 1;
129
130 return 0;
131}
132
133static int
Al Viro6fa67e72016-07-31 16:37:25 -0400134affs_compare_dentry(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100135 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Fabian Frederick8ca57722014-04-07 15:39:01 -0700137
138 return __affs_compare_dentry(len, str, name, affs_toupper,
Al Viroe0b3f592016-07-29 18:22:49 -0400139 affs_nofilenametruncate(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
Fabian Frederick8ca57722014-04-07 15:39:01 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int
Al Viro6fa67e72016-07-31 16:37:25 -0400143affs_intl_compare_dentry(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100144 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Fabian Frederick8ca57722014-04-07 15:39:01 -0700146 return __affs_compare_dentry(len, str, name, affs_intl_toupper,
Al Viroe0b3f592016-07-29 18:22:49 -0400147 affs_nofilenametruncate(dentry));
Fabian Frederick8ca57722014-04-07 15:39:01 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151/*
152 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
153 */
154
155static inline int
156affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
157{
158 const u8 *name = dentry->d_name.name;
159 int len = dentry->d_name.len;
160
Fabian Frederickf1578532015-02-17 13:46:23 -0800161 if (len >= AFFSNAMEMAX) {
162 if (*name2 < AFFSNAMEMAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
Fabian Frederickf1578532015-02-17 13:46:23 -0800164 len = AFFSNAMEMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else if (len != *name2)
166 return 0;
167
168 for (name2++; len > 0; len--)
169 if (toupper(*name++) != toupper(*name2++))
170 return 0;
171 return 1;
172}
173
174int
175affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
176{
177 toupper_t toupper = affs_get_toupper(sb);
Fabian Frederickeeb36f82015-02-17 13:46:20 -0800178 u32 hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Fabian Frederickf1578532015-02-17 13:46:23 -0800180 hash = len = min(len, AFFSNAMEMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 for (; len > 0; len--)
182 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
183
184 return hash % AFFS_SB(sb)->s_hashsize;
185}
186
187static struct buffer_head *
188affs_find_entry(struct inode *dir, struct dentry *dentry)
189{
190 struct super_block *sb = dir->i_sb;
191 struct buffer_head *bh;
192 toupper_t toupper = affs_get_toupper(sb);
193 u32 key;
194
Al Viroa4555892014-10-21 20:11:25 -0400195 pr_debug("%s(\"%pd\")\n", __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 bh = affs_bread(sb, dir->i_ino);
198 if (!bh)
199 return ERR_PTR(-EIO);
200
201 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
202
203 for (;;) {
204 affs_brelse(bh);
205 if (key == 0)
206 return NULL;
207 bh = affs_bread(sb, key);
208 if (!bh)
209 return ERR_PTR(-EIO);
210 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
211 return bh;
212 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
213 }
214}
215
216struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -0400217affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct super_block *sb = dir->i_sb;
220 struct buffer_head *bh;
221 struct inode *inode = NULL;
222
Al Viroa4555892014-10-21 20:11:25 -0400223 pr_debug("%s(\"%pd\")\n", __func__, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 affs_lock_dir(dir);
226 bh = affs_find_entry(dir, dentry);
227 affs_unlock_dir(dir);
David Howells210f8552008-02-07 00:15:29 -0800228 if (IS_ERR(bh))
David Howellse231c2e2008-02-07 00:15:26 -0800229 return ERR_CAST(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (bh) {
231 u32 ino = bh->b_blocknr;
232
233 /* store the real header ino in d_fsdata for faster lookups */
234 dentry->d_fsdata = (void *)(long)ino;
235 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
236 //link to dirs disabled
237 //case ST_LINKDIR:
238 case ST_LINKFILE:
239 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
240 }
241 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800242 inode = affs_iget(sb, ino);
243 if (IS_ERR(inode))
Julia Lawallcccad8f2010-05-26 14:44:23 -0700244 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 d_add(dentry, inode);
247 return NULL;
248}
249
250int
251affs_unlink(struct inode *dir, struct dentry *dentry)
252{
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800253 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
David Howells2b0143b2015-03-17 22:25:59 +0000254 d_inode(dentry)->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return affs_remove_header(dentry);
257}
258
259int
Al Viroebfc3b42012-06-10 18:05:36 -0400260affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct super_block *sb = dir->i_sb;
263 struct inode *inode;
264 int error;
265
Al Viroa4555892014-10-21 20:11:25 -0400266 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
267 __func__, dir->i_ino, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 inode = affs_new_inode(dir);
270 if (!inode)
271 return -ENOSPC;
272
273 inode->i_mode = mode;
274 mode_to_prot(inode);
275 mark_inode_dirty(inode);
276
277 inode->i_op = &affs_file_inode_operations;
278 inode->i_fop = &affs_file_operations;
Fabian Frederick79bda4d2015-04-16 12:48:24 -0700279 inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
Fabian Fredericka0016ff2015-04-16 12:48:15 -0700280 &affs_aops_ofs : &affs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 error = affs_add_entry(dir, inode, dentry, ST_FILE);
282 if (error) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200283 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 iput(inode);
285 return error;
286 }
287 return 0;
288}
289
290int
Al Viro18bb1db2011-07-26 01:41:39 -0400291affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct inode *inode;
294 int error;
295
Al Viroa4555892014-10-21 20:11:25 -0400296 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
297 __func__, dir->i_ino, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 inode = affs_new_inode(dir);
300 if (!inode)
301 return -ENOSPC;
302
303 inode->i_mode = S_IFDIR | mode;
304 mode_to_prot(inode);
305
306 inode->i_op = &affs_dir_inode_operations;
307 inode->i_fop = &affs_dir_operations;
308
309 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
310 if (error) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200311 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 mark_inode_dirty(inode);
313 iput(inode);
314 return error;
315 }
316 return 0;
317}
318
319int
320affs_rmdir(struct inode *dir, struct dentry *dentry)
321{
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800322 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
David Howells2b0143b2015-03-17 22:25:59 +0000323 d_inode(dentry)->i_ino, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return affs_remove_header(dentry);
326}
327
328int
329affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
330{
331 struct super_block *sb = dir->i_sb;
332 struct buffer_head *bh;
333 struct inode *inode;
334 char *p;
335 int i, maxlen, error;
336 char c, lc;
337
Al Viroa4555892014-10-21 20:11:25 -0400338 pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
339 __func__, dir->i_ino, dentry, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
342 inode = affs_new_inode(dir);
343 if (!inode)
344 return -ENOSPC;
345
346 inode->i_op = &affs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500347 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 inode->i_data.a_ops = &affs_symlink_aops;
349 inode->i_mode = S_IFLNK | 0777;
350 mode_to_prot(inode);
351
352 error = -EIO;
353 bh = affs_bread(sb, inode->i_ino);
354 if (!bh)
355 goto err;
356 i = 0;
357 p = (char *)AFFS_HEAD(bh)->table;
358 lc = '/';
359 if (*symname == '/') {
Al Viro29333922010-01-24 00:04:07 -0500360 struct affs_sb_info *sbi = AFFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 while (*symname == '/')
362 symname++;
Al Viro29333922010-01-24 00:04:07 -0500363 spin_lock(&sbi->symlink_lock);
364 while (sbi->s_volume[i]) /* Cannot overflow */
365 *p++ = sbi->s_volume[i++];
366 spin_unlock(&sbi->symlink_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 while (i < maxlen && (c = *symname++)) {
369 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
370 *p++ = '/';
371 i++;
372 symname += 2;
373 lc = '/';
374 } else if (c == '.' && lc == '/' && *symname == '/') {
375 symname++;
376 lc = '/';
377 } else {
378 *p++ = c;
379 lc = c;
380 i++;
381 }
382 if (lc == '/')
383 while (*symname == '/')
384 symname++;
385 }
386 *p = 0;
387 mark_buffer_dirty_inode(bh, inode);
388 affs_brelse(bh);
389 mark_inode_dirty(inode);
390
391 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
392 if (error)
393 goto err;
394
395 return 0;
396
397err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200398 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 mark_inode_dirty(inode);
400 iput(inode);
401 return error;
402}
403
404int
405affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
406{
David Howells2b0143b2015-03-17 22:25:59 +0000407 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800409 pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
Al Viroa4555892014-10-21 20:11:25 -0400410 dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
413}
414
415int
416affs_rename(struct inode *old_dir, struct dentry *old_dentry,
417 struct inode *new_dir, struct dentry *new_dentry)
418{
419 struct super_block *sb = old_dir->i_sb;
420 struct buffer_head *bh = NULL;
421 int retval;
422
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800423 pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
424 old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Fabian Frederick8ca57722014-04-07 15:39:01 -0700426 retval = affs_check_name(new_dentry->d_name.name,
427 new_dentry->d_name.len,
428 affs_nofilenametruncate(old_dentry));
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (retval)
431 return retval;
432
433 /* Unlink destination if it already exists */
David Howells2b0143b2015-03-17 22:25:59 +0000434 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = affs_remove_header(new_dentry);
436 if (retval)
437 return retval;
438 }
439
David Howells2b0143b2015-03-17 22:25:59 +0000440 bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!bh)
Florin Malita3ac81412006-05-25 18:44:23 -0700442 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Remove header from its parent directory. */
445 affs_lock_dir(old_dir);
446 retval = affs_remove_hash(old_dir, bh);
447 affs_unlock_dir(old_dir);
448 if (retval)
449 goto done;
450
451 /* And insert it into the new directory with the new name. */
452 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
453 affs_fix_checksum(sb, bh);
454 affs_lock_dir(new_dir);
455 retval = affs_insert_hash(new_dir, bh);
456 affs_unlock_dir(new_dir);
457 /* TODO: move it back to old_dir, if error? */
458
459done:
460 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
461 affs_brelse(bh);
462 return retval;
463}