]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/affs/namei.c
Merge tag 'ceph-for-4.11-rc1' of git://github.com/ceph/ceph-client
[mirror_ubuntu-artful-kernel.git] / fs / affs / namei.c
CommitLineData
1da177e4
LT
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"
ed4433d7 12#include <linux/exportfs.h>
1da177e4
LT
13
14typedef int (*toupper_t)(int);
15
1da177e4
LT
16/* Simple toupper() for DOS\1 */
17
18static int
19affs_toupper(int ch)
20{
21 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
22}
23
24/* International toupper() for DOS\3 ("international") */
25
26static int
27affs_intl_toupper(int ch)
28{
29 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
30 && ch <= 0xFE && ch != 0xF7) ?
31 ch - ('a' - 'A') : ch;
32}
33
34static inline toupper_t
35affs_get_toupper(struct super_block *sb)
36{
79bda4d5
FF
37 return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
38 affs_intl_toupper : affs_toupper;
1da177e4
LT
39}
40
41/*
42 * Note: the dentry argument is the parent dentry.
43 */
44static inline int
8387ff25 45__affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
1da177e4
LT
46{
47 const u8 *name = qstr->name;
48 unsigned long hash;
eeb36f8e
FF
49 int retval;
50 u32 len;
1da177e4 51
eeb36f8e
FF
52 retval = affs_check_name(qstr->name, qstr->len, notruncate);
53 if (retval)
54 return retval;
1da177e4 55
8387ff25 56 hash = init_name_hash(dentry);
f157853e 57 len = min(qstr->len, AFFSNAMEMAX);
eeb36f8e 58 for (; len > 0; name++, len--)
1da177e4
LT
59 hash = partial_name_hash(toupper(*name), hash);
60 qstr->hash = end_name_hash(hash);
61
62 return 0;
63}
64
65static int
da53be12 66affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
1da177e4 67{
8387ff25 68 return __affs_hash_dentry(dentry, qstr, affs_toupper,
8ca57722
FF
69 affs_nofilenametruncate(dentry));
70
1da177e4 71}
8ca57722 72
1da177e4 73static int
da53be12 74affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
1da177e4 75{
8387ff25 76 return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
8ca57722
FF
77 affs_nofilenametruncate(dentry));
78
1da177e4
LT
79}
80
621e155a 81static inline int __affs_compare_dentry(unsigned int len,
8ca57722
FF
82 const char *str, const struct qstr *name, toupper_t toupper,
83 bool notruncate)
1da177e4 84{
621e155a
NP
85 const u8 *aname = str;
86 const u8 *bname = name->name;
1da177e4 87
621e155a
NP
88 /*
89 * 'str' is the name of an already existing dentry, so the name
90 * must be valid. 'name' must be validated first.
1da177e4
LT
91 */
92
8ca57722 93 if (affs_check_name(name->name, name->len, notruncate))
1da177e4
LT
94 return 1;
95
621e155a
NP
96 /*
97 * If the names are longer than the allowed 30 chars,
1da177e4
LT
98 * the excess is ignored, so their length may differ.
99 */
f157853e
FF
100 if (len >= AFFSNAMEMAX) {
101 if (name->len < AFFSNAMEMAX)
1da177e4 102 return 1;
f157853e 103 len = AFFSNAMEMAX;
621e155a 104 } else if (len != name->len)
1da177e4
LT
105 return 1;
106
107 for (; len > 0; len--)
108 if (toupper(*aname++) != toupper(*bname++))
109 return 1;
110
111 return 0;
112}
113
114static int
6fa67e70 115affs_compare_dentry(const struct dentry *dentry,
621e155a 116 unsigned int len, const char *str, const struct qstr *name)
1da177e4 117{
8ca57722
FF
118
119 return __affs_compare_dentry(len, str, name, affs_toupper,
e0b3f595 120 affs_nofilenametruncate(dentry));
1da177e4 121}
8ca57722 122
1da177e4 123static int
6fa67e70 124affs_intl_compare_dentry(const struct dentry *dentry,
621e155a 125 unsigned int len, const char *str, const struct qstr *name)
1da177e4 126{
8ca57722 127 return __affs_compare_dentry(len, str, name, affs_intl_toupper,
e0b3f595 128 affs_nofilenametruncate(dentry));
8ca57722 129
1da177e4
LT
130}
131
132/*
133 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
134 */
135
136static inline int
137affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
138{
139 const u8 *name = dentry->d_name.name;
140 int len = dentry->d_name.len;
141
f157853e
FF
142 if (len >= AFFSNAMEMAX) {
143 if (*name2 < AFFSNAMEMAX)
1da177e4 144 return 0;
f157853e 145 len = AFFSNAMEMAX;
1da177e4
LT
146 } else if (len != *name2)
147 return 0;
148
149 for (name2++; len > 0; len--)
150 if (toupper(*name++) != toupper(*name2++))
151 return 0;
152 return 1;
153}
154
155int
156affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
157{
158 toupper_t toupper = affs_get_toupper(sb);
eeb36f8e 159 u32 hash;
1da177e4 160
f157853e 161 hash = len = min(len, AFFSNAMEMAX);
1da177e4
LT
162 for (; len > 0; len--)
163 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
164
165 return hash % AFFS_SB(sb)->s_hashsize;
166}
167
168static struct buffer_head *
169affs_find_entry(struct inode *dir, struct dentry *dentry)
170{
171 struct super_block *sb = dir->i_sb;
172 struct buffer_head *bh;
173 toupper_t toupper = affs_get_toupper(sb);
174 u32 key;
175
a455589f 176 pr_debug("%s(\"%pd\")\n", __func__, dentry);
1da177e4
LT
177
178 bh = affs_bread(sb, dir->i_ino);
179 if (!bh)
180 return ERR_PTR(-EIO);
181
182 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
183
184 for (;;) {
185 affs_brelse(bh);
186 if (key == 0)
187 return NULL;
188 bh = affs_bread(sb, key);
189 if (!bh)
190 return ERR_PTR(-EIO);
191 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
192 return bh;
193 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
194 }
195}
196
197struct dentry *
00cd8dd3 198affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1da177e4
LT
199{
200 struct super_block *sb = dir->i_sb;
201 struct buffer_head *bh;
202 struct inode *inode = NULL;
203
a455589f 204 pr_debug("%s(\"%pd\")\n", __func__, dentry);
1da177e4
LT
205
206 affs_lock_dir(dir);
207 bh = affs_find_entry(dir, dentry);
208 affs_unlock_dir(dir);
210f8559 209 if (IS_ERR(bh))
e231c2ee 210 return ERR_CAST(bh);
1da177e4
LT
211 if (bh) {
212 u32 ino = bh->b_blocknr;
213
214 /* store the real header ino in d_fsdata for faster lookups */
215 dentry->d_fsdata = (void *)(long)ino;
216 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
217 //link to dirs disabled
218 //case ST_LINKDIR:
219 case ST_LINKFILE:
220 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
221 }
222 affs_brelse(bh);
210f8559
DH
223 inode = affs_iget(sb, ino);
224 if (IS_ERR(inode))
cccad8f9 225 return ERR_CAST(inode);
1da177e4 226 }
1da177e4
LT
227 d_add(dentry, inode);
228 return NULL;
229}
230
231int
232affs_unlink(struct inode *dir, struct dentry *dentry)
233{
08fe100d 234 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
2b0143b5 235 d_inode(dentry)->i_ino, dentry);
1da177e4
LT
236
237 return affs_remove_header(dentry);
238}
239
240int
ebfc3b49 241affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
1da177e4
LT
242{
243 struct super_block *sb = dir->i_sb;
244 struct inode *inode;
245 int error;
246
a455589f
AV
247 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
248 __func__, dir->i_ino, dentry, mode);
1da177e4
LT
249
250 inode = affs_new_inode(dir);
251 if (!inode)
252 return -ENOSPC;
253
254 inode->i_mode = mode;
c1618208 255 affs_mode_to_prot(inode);
1da177e4
LT
256 mark_inode_dirty(inode);
257
258 inode->i_op = &affs_file_inode_operations;
259 inode->i_fop = &affs_file_operations;
79bda4d5 260 inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
a0016ff2 261 &affs_aops_ofs : &affs_aops;
1da177e4
LT
262 error = affs_add_entry(dir, inode, dentry, ST_FILE);
263 if (error) {
6d6b77f1 264 clear_nlink(inode);
1da177e4
LT
265 iput(inode);
266 return error;
267 }
268 return 0;
269}
270
271int
18bb1db3 272affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4
LT
273{
274 struct inode *inode;
275 int error;
276
a455589f
AV
277 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
278 __func__, dir->i_ino, dentry, mode);
1da177e4
LT
279
280 inode = affs_new_inode(dir);
281 if (!inode)
282 return -ENOSPC;
283
284 inode->i_mode = S_IFDIR | mode;
c1618208 285 affs_mode_to_prot(inode);
1da177e4
LT
286
287 inode->i_op = &affs_dir_inode_operations;
288 inode->i_fop = &affs_dir_operations;
289
290 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
291 if (error) {
6d6b77f1 292 clear_nlink(inode);
1da177e4
LT
293 mark_inode_dirty(inode);
294 iput(inode);
295 return error;
296 }
297 return 0;
298}
299
300int
301affs_rmdir(struct inode *dir, struct dentry *dentry)
302{
08fe100d 303 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
2b0143b5 304 d_inode(dentry)->i_ino, dentry);
1da177e4
LT
305
306 return affs_remove_header(dentry);
307}
308
309int
310affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
311{
312 struct super_block *sb = dir->i_sb;
313 struct buffer_head *bh;
314 struct inode *inode;
315 char *p;
316 int i, maxlen, error;
317 char c, lc;
318
a455589f
AV
319 pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
320 __func__, dir->i_ino, dentry, symname);
1da177e4
LT
321
322 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
323 inode = affs_new_inode(dir);
324 if (!inode)
325 return -ENOSPC;
326
327 inode->i_op = &affs_symlink_inode_operations;
21fc61c7 328 inode_nohighmem(inode);
1da177e4
LT
329 inode->i_data.a_ops = &affs_symlink_aops;
330 inode->i_mode = S_IFLNK | 0777;
c1618208 331 affs_mode_to_prot(inode);
1da177e4
LT
332
333 error = -EIO;
334 bh = affs_bread(sb, inode->i_ino);
335 if (!bh)
336 goto err;
337 i = 0;
338 p = (char *)AFFS_HEAD(bh)->table;
339 lc = '/';
340 if (*symname == '/') {
29333920 341 struct affs_sb_info *sbi = AFFS_SB(sb);
1da177e4
LT
342 while (*symname == '/')
343 symname++;
29333920
AV
344 spin_lock(&sbi->symlink_lock);
345 while (sbi->s_volume[i]) /* Cannot overflow */
346 *p++ = sbi->s_volume[i++];
347 spin_unlock(&sbi->symlink_lock);
1da177e4
LT
348 }
349 while (i < maxlen && (c = *symname++)) {
350 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
351 *p++ = '/';
352 i++;
353 symname += 2;
354 lc = '/';
355 } else if (c == '.' && lc == '/' && *symname == '/') {
356 symname++;
357 lc = '/';
358 } else {
359 *p++ = c;
360 lc = c;
361 i++;
362 }
363 if (lc == '/')
364 while (*symname == '/')
365 symname++;
366 }
367 *p = 0;
368 mark_buffer_dirty_inode(bh, inode);
369 affs_brelse(bh);
370 mark_inode_dirty(inode);
371
372 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
373 if (error)
374 goto err;
375
376 return 0;
377
378err:
6d6b77f1 379 clear_nlink(inode);
1da177e4
LT
380 mark_inode_dirty(inode);
381 iput(inode);
382 return error;
383}
384
385int
386affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
387{
2b0143b5 388 struct inode *inode = d_inode(old_dentry);
1da177e4 389
08fe100d 390 pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
a455589f 391 dentry);
1da177e4
LT
392
393 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
394}
395
396int
397affs_rename(struct inode *old_dir, struct dentry *old_dentry,
f03b8ad8
MS
398 struct inode *new_dir, struct dentry *new_dentry,
399 unsigned int flags)
1da177e4
LT
400{
401 struct super_block *sb = old_dir->i_sb;
402 struct buffer_head *bh = NULL;
403 int retval;
404
f03b8ad8
MS
405 if (flags & ~RENAME_NOREPLACE)
406 return -EINVAL;
407
08fe100d
GU
408 pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
409 old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
1da177e4 410
8ca57722
FF
411 retval = affs_check_name(new_dentry->d_name.name,
412 new_dentry->d_name.len,
413 affs_nofilenametruncate(old_dentry));
414
1da177e4
LT
415 if (retval)
416 return retval;
417
418 /* Unlink destination if it already exists */
2b0143b5 419 if (d_really_is_positive(new_dentry)) {
1da177e4
LT
420 retval = affs_remove_header(new_dentry);
421 if (retval)
422 return retval;
423 }
424
2b0143b5 425 bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
1da177e4 426 if (!bh)
3ac81413 427 return -EIO;
1da177e4
LT
428
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}
ed4433d7 449
b3b42c0d
FF
450static struct dentry *affs_get_parent(struct dentry *child)
451{
452 struct inode *parent;
453 struct buffer_head *bh;
454
455 bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
456 if (!bh)
457 return ERR_PTR(-EIO);
458
459 parent = affs_iget(child->d_sb,
460 be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
461 brelse(bh);
462 if (IS_ERR(parent))
463 return ERR_CAST(parent);
464
465 return d_obtain_alias(parent);
466}
467
ed4433d7
FF
468static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
469 u32 generation)
470{
471 struct inode *inode;
472
473 if (!affs_validblock(sb, ino))
474 return ERR_PTR(-ESTALE);
475
476 inode = affs_iget(sb, ino);
477 if (IS_ERR(inode))
478 return ERR_CAST(inode);
479
480 if (generation && inode->i_generation != generation) {
481 iput(inode);
482 return ERR_PTR(-ESTALE);
483 }
484
485 return inode;
486}
487
488static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
489 int fh_len, int fh_type)
490{
491 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
492 affs_nfs_get_inode);
493}
494
495static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
496 int fh_len, int fh_type)
497{
498 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
499 affs_nfs_get_inode);
500}
501
502const struct export_operations affs_export_ops = {
503 .fh_to_dentry = affs_fh_to_dentry,
504 .fh_to_parent = affs_fh_to_parent,
b3b42c0d 505 .get_parent = affs_get_parent,
ed4433d7 506};
f567accb
FF
507
508const struct dentry_operations affs_dentry_operations = {
509 .d_hash = affs_hash_dentry,
510 .d_compare = affs_compare_dentry,
511};
512
513const struct dentry_operations affs_intl_dentry_operations = {
514 .d_hash = affs_intl_hash_dentry,
515 .d_compare = affs_intl_compare_dentry,
516};