Blame view

fs/affs/namei.c 10.8 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   *  linux/fs/affs/namei.c
   *
   *  (c) 1996  Hans-Joachim Widmaier - Rewritten
   *
   *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
   *
   *  (C) 1991  Linus Torvalds - minix filesystem
   */
  
  #include "affs.h"
  
  typedef int (*toupper_t)(int);
  
  static int	 affs_toupper(int ch);
b1e6a015a   Nick Piggin   fs: change d_hash...
16
17
  static int	 affs_hash_dentry(const struct dentry *,
  		const struct inode *, struct qstr *);
621e155a3   Nick Piggin   fs: change d_comp...
18
19
20
21
  static int       affs_compare_dentry(const struct dentry *parent,
  		const struct inode *pinode,
  		const struct dentry *dentry, const struct inode *inode,
  		unsigned int len, const char *str, const struct qstr *name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  static int	 affs_intl_toupper(int ch);
b1e6a015a   Nick Piggin   fs: change d_hash...
23
24
  static int	 affs_intl_hash_dentry(const struct dentry *,
  		const struct inode *, struct qstr *);
621e155a3   Nick Piggin   fs: change d_comp...
25
26
27
28
  static int       affs_intl_compare_dentry(const struct dentry *parent,
  		const struct inode *pinode,
  		const struct dentry *dentry, const struct inode *inode,
  		unsigned int len, const char *str, const struct qstr *name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29

e16404ed0   Al Viro   constify dentry_o...
30
  const struct dentry_operations affs_dentry_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
  	.d_hash		= affs_hash_dentry,
  	.d_compare	= affs_compare_dentry,
  };
a129880da   Al Viro   switch affs
34
  const struct dentry_operations affs_intl_dentry_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  	.d_hash		= affs_intl_hash_dentry,
  	.d_compare	= affs_intl_compare_dentry,
  };
  
  
  /* Simple toupper() for DOS\1 */
  
  static int
  affs_toupper(int ch)
  {
  	return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
  }
  
  /* International toupper() for DOS\3 ("international") */
  
  static int
  affs_intl_toupper(int ch)
  {
  	return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
  		&& ch <= 0xFE && ch != 0xF7) ?
  		ch - ('a' - 'A') : ch;
  }
  
  static inline toupper_t
  affs_get_toupper(struct super_block *sb)
  {
  	return AFFS_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
  }
  
  /*
   * Note: the dentry argument is the parent dentry.
   */
  static inline int
b1e6a015a   Nick Piggin   fs: change d_hash...
68
  __affs_hash_dentry(struct qstr *qstr, toupper_t toupper)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
  {
  	const u8 *name = qstr->name;
  	unsigned long hash;
  	int i;
b1e6a015a   Nick Piggin   fs: change d_hash...
73
  	i = affs_check_name(qstr->name, qstr->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
76
77
78
79
80
81
82
83
84
85
86
  	if (i)
  		return i;
  
  	hash = init_name_hash();
  	i = min(qstr->len, 30u);
  	for (; i > 0; name++, i--)
  		hash = partial_name_hash(toupper(*name), hash);
  	qstr->hash = end_name_hash(hash);
  
  	return 0;
  }
  
  static int
b1e6a015a   Nick Piggin   fs: change d_hash...
87
88
  affs_hash_dentry(const struct dentry *dentry, const struct inode *inode,
  		struct qstr *qstr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  {
b1e6a015a   Nick Piggin   fs: change d_hash...
90
  	return __affs_hash_dentry(qstr, affs_toupper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
  }
  static int
b1e6a015a   Nick Piggin   fs: change d_hash...
93
94
  affs_intl_hash_dentry(const struct dentry *dentry, const struct inode *inode,
  		struct qstr *qstr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
  {
b1e6a015a   Nick Piggin   fs: change d_hash...
96
  	return __affs_hash_dentry(qstr, affs_intl_toupper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  }
621e155a3   Nick Piggin   fs: change d_comp...
98
99
  static inline int __affs_compare_dentry(unsigned int len,
  		const char *str, const struct qstr *name, toupper_t toupper)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  {
621e155a3   Nick Piggin   fs: change d_comp...
101
102
  	const u8 *aname = str;
  	const u8 *bname = name->name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103

621e155a3   Nick Piggin   fs: change d_comp...
104
105
106
  	/*
  	 * 'str' is the name of an already existing dentry, so the name
  	 * must be valid. 'name' must be validated first.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  	 */
621e155a3   Nick Piggin   fs: change d_comp...
108
  	if (affs_check_name(name->name, name->len))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  		return 1;
621e155a3   Nick Piggin   fs: change d_comp...
110
111
  	/*
  	 * If the names are longer than the allowed 30 chars,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
  	 * the excess is ignored, so their length may differ.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  	if (len >= 30) {
621e155a3   Nick Piggin   fs: change d_comp...
115
  		if (name->len < 30)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
  			return 1;
  		len = 30;
621e155a3   Nick Piggin   fs: change d_comp...
118
  	} else if (len != name->len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
126
127
128
  		return 1;
  
  	for (; len > 0; len--)
  		if (toupper(*aname++) != toupper(*bname++))
  			return 1;
  
  	return 0;
  }
  
  static int
621e155a3   Nick Piggin   fs: change d_comp...
129
130
131
  affs_compare_dentry(const struct dentry *parent, const struct inode *pinode,
  		const struct dentry *dentry, const struct inode *inode,
  		unsigned int len, const char *str, const struct qstr *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
  {
621e155a3   Nick Piggin   fs: change d_comp...
133
  	return __affs_compare_dentry(len, str, name, affs_toupper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
  }
  static int
621e155a3   Nick Piggin   fs: change d_comp...
136
137
138
  affs_intl_compare_dentry(const struct dentry *parent,const struct inode *pinode,
  		const struct dentry *dentry, const struct inode *inode,
  		unsigned int len, const char *str, const struct qstr *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  {
621e155a3   Nick Piggin   fs: change d_comp...
140
  	return __affs_compare_dentry(len, str, name, affs_intl_toupper);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  }
  
  /*
   * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
   */
  
  static inline int
  affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
  {
  	const u8 *name = dentry->d_name.name;
  	int len = dentry->d_name.len;
  
  	if (len >= 30) {
  		if (*name2 < 30)
  			return 0;
  		len = 30;
  	} else if (len != *name2)
  		return 0;
  
  	for (name2++; len > 0; len--)
  		if (toupper(*name++) != toupper(*name2++))
  			return 0;
  	return 1;
  }
  
  int
  affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
  {
  	toupper_t toupper = affs_get_toupper(sb);
  	int hash;
  
  	hash = len = min(len, 30u);
  	for (; len > 0; len--)
  		hash = (hash * 13 + toupper(*name++)) & 0x7ff;
  
  	return hash % AFFS_SB(sb)->s_hashsize;
  }
  
  static struct buffer_head *
  affs_find_entry(struct inode *dir, struct dentry *dentry)
  {
  	struct super_block *sb = dir->i_sb;
  	struct buffer_head *bh;
  	toupper_t toupper = affs_get_toupper(sb);
  	u32 key;
  
  	pr_debug("AFFS: find_entry(\"%.*s\")
  ", (int)dentry->d_name.len, dentry->d_name.name);
  
  	bh = affs_bread(sb, dir->i_ino);
  	if (!bh)
  		return ERR_PTR(-EIO);
  
  	key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
  
  	for (;;) {
  		affs_brelse(bh);
  		if (key == 0)
  			return NULL;
  		bh = affs_bread(sb, key);
  		if (!bh)
  			return ERR_PTR(-EIO);
  		if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
  			return bh;
  		key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  	}
  }
  
  struct dentry *
  affs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  {
  	struct super_block *sb = dir->i_sb;
  	struct buffer_head *bh;
  	struct inode *inode = NULL;
  
  	pr_debug("AFFS: lookup(\"%.*s\")
  ",(int)dentry->d_name.len,dentry->d_name.name);
  
  	affs_lock_dir(dir);
  	bh = affs_find_entry(dir, dentry);
  	affs_unlock_dir(dir);
210f85596   David Howells   iget: stop AFFS f...
222
  	if (IS_ERR(bh))
e231c2ee6   David Howells   Convert ERR_PTR(P...
223
  		return ERR_CAST(bh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
227
228
229
230
231
232
233
234
235
  	if (bh) {
  		u32 ino = bh->b_blocknr;
  
  		/* store the real header ino in d_fsdata for faster lookups */
  		dentry->d_fsdata = (void *)(long)ino;
  		switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
  		//link to dirs disabled
  		//case ST_LINKDIR:
  		case ST_LINKFILE:
  			ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
  		}
  		affs_brelse(bh);
210f85596   David Howells   iget: stop AFFS f...
236
237
  		inode = affs_iget(sb, ino);
  		if (IS_ERR(inode))
cccad8f9f   Julia Lawall   fs/affs: use ERR_...
238
  			return ERR_CAST(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
  	d_add(dentry, inode);
  	return NULL;
  }
  
  int
  affs_unlink(struct inode *dir, struct dentry *dentry)
  {
dca3c3365   Roman Zippel   [PATCH] fix reser...
247
248
249
  	pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")
  ", (u32)dir->i_ino,
  		 dentry->d_inode->i_ino,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
253
254
255
  		 (int)dentry->d_name.len, dentry->d_name.name);
  
  	return affs_remove_header(dentry);
  }
  
  int
4acdaf27e   Al Viro   switch ->create()...
256
  affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
259
260
  {
  	struct super_block *sb = dir->i_sb;
  	struct inode	*inode;
  	int		 error;
4acdaf27e   Al Viro   switch ->create()...
261
262
  	pr_debug("AFFS: create(%lu,\"%.*s\",0%ho)
  ",dir->i_ino,(int)dentry->d_name.len,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  		 dentry->d_name.name,mode);
  
  	inode = affs_new_inode(dir);
  	if (!inode)
  		return -ENOSPC;
  
  	inode->i_mode = mode;
  	mode_to_prot(inode);
  	mark_inode_dirty(inode);
  
  	inode->i_op = &affs_file_inode_operations;
  	inode->i_fop = &affs_file_operations;
  	inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
  	error = affs_add_entry(dir, inode, dentry, ST_FILE);
  	if (error) {
6d6b77f16   Miklos Szeredi   filesystems: add ...
278
  		clear_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
285
  		iput(inode);
  		return error;
  	}
  	return 0;
  }
  
  int
18bb1db3e   Al Viro   switch vfs_mkdir(...
286
  affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
  {
  	struct inode		*inode;
  	int			 error;
18bb1db3e   Al Viro   switch vfs_mkdir(...
290
291
  	pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%ho)
  ",dir->i_ino,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  		 (int)dentry->d_name.len,dentry->d_name.name,mode);
  
  	inode = affs_new_inode(dir);
  	if (!inode)
  		return -ENOSPC;
  
  	inode->i_mode = S_IFDIR | mode;
  	mode_to_prot(inode);
  
  	inode->i_op = &affs_dir_inode_operations;
  	inode->i_fop = &affs_dir_operations;
  
  	error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
  	if (error) {
6d6b77f16   Miklos Szeredi   filesystems: add ...
306
  		clear_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
314
315
316
  		mark_inode_dirty(inode);
  		iput(inode);
  		return error;
  	}
  	return 0;
  }
  
  int
  affs_rmdir(struct inode *dir, struct dentry *dentry)
  {
dca3c3365   Roman Zippel   [PATCH] fix reser...
317
318
319
  	pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")
  ", (u32)dir->i_ino,
  		 dentry->d_inode->i_ino,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  		 (int)dentry->d_name.len, dentry->d_name.name);
  
  	return affs_remove_header(dentry);
  }
  
  int
  affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  {
  	struct super_block	*sb = dir->i_sb;
  	struct buffer_head	*bh;
  	struct inode		*inode;
  	char			*p;
  	int			 i, maxlen, error;
  	char			 c, lc;
  
  	pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")
  ",dir->i_ino,
  		 (int)dentry->d_name.len,dentry->d_name.name,symname);
  
  	maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
  	inode  = affs_new_inode(dir);
  	if (!inode)
  		return -ENOSPC;
  
  	inode->i_op = &affs_symlink_inode_operations;
  	inode->i_data.a_ops = &affs_symlink_aops;
  	inode->i_mode = S_IFLNK | 0777;
  	mode_to_prot(inode);
  
  	error = -EIO;
  	bh = affs_bread(sb, inode->i_ino);
  	if (!bh)
  		goto err;
  	i  = 0;
  	p  = (char *)AFFS_HEAD(bh)->table;
  	lc = '/';
  	if (*symname == '/') {
29333920a   Al Viro   Fix remount races...
357
  		struct affs_sb_info *sbi = AFFS_SB(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
  		while (*symname == '/')
  			symname++;
29333920a   Al Viro   Fix remount races...
360
361
362
363
  		spin_lock(&sbi->symlink_lock);
  		while (sbi->s_volume[i])	/* Cannot overflow */
  			*p++ = sbi->s_volume[i++];
  		spin_unlock(&sbi->symlink_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  	}
  	while (i < maxlen && (c = *symname++)) {
  		if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
  			*p++ = '/';
  			i++;
  			symname += 2;
  			lc = '/';
  		} else if (c == '.' && lc == '/' && *symname == '/') {
  			symname++;
  			lc = '/';
  		} else {
  			*p++ = c;
  			lc   = c;
  			i++;
  		}
  		if (lc == '/')
  			while (*symname == '/')
  				symname++;
  	}
  	*p = 0;
  	mark_buffer_dirty_inode(bh, inode);
  	affs_brelse(bh);
  	mark_inode_dirty(inode);
  
  	error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
  	if (error)
  		goto err;
  
  	return 0;
  
  err:
6d6b77f16   Miklos Szeredi   filesystems: add ...
395
  	clear_nlink(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  	mark_inode_dirty(inode);
  	iput(inode);
  	return error;
  }
  
  int
  affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  {
  	struct inode *inode = old_dentry->d_inode;
  
  	pr_debug("AFFS: link(%u, %u, \"%.*s\")
  ", (u32)inode->i_ino, (u32)dir->i_ino,
  		 (int)dentry->d_name.len,dentry->d_name.name);
  
  	return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
  }
  
  int
  affs_rename(struct inode *old_dir, struct dentry *old_dentry,
  	    struct inode *new_dir, struct dentry *new_dentry)
  {
  	struct super_block *sb = old_dir->i_sb;
  	struct buffer_head *bh = NULL;
  	int retval;
  
  	pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")
  ",
  		 (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
  		 (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
  
  	retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len);
  	if (retval)
  		return retval;
  
  	/* Unlink destination if it already exists */
  	if (new_dentry->d_inode) {
  		retval = affs_remove_header(new_dentry);
  		if (retval)
  			return retval;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
  	bh = affs_bread(sb, old_dentry->d_inode->i_ino);
  	if (!bh)
3ac814136   Florin Malita   [PATCH] affs: pos...
438
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  
  	/* Remove header from its parent directory. */
  	affs_lock_dir(old_dir);
  	retval = affs_remove_hash(old_dir, bh);
  	affs_unlock_dir(old_dir);
  	if (retval)
  		goto done;
  
  	/* And insert it into the new directory with the new name. */
  	affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
  	affs_fix_checksum(sb, bh);
  	affs_lock_dir(new_dir);
  	retval = affs_insert_hash(new_dir, bh);
  	affs_unlock_dir(new_dir);
  	/* TODO: move it back to old_dir, if error? */
  
  done:
  	mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
  	affs_brelse(bh);
  	return retval;
  }