xref: /linux/fs/hpfs/namei.c (revision 8c749ce93ee69e789e46b3be98de9e0cbfcf8ed8)
1 /*
2  *  linux/fs/hpfs/namei.c
3  *
4  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5  *
6  *  adding & removing files & directories
7  */
8 #include <linux/sched.h>
9 #include "hpfs_fn.h"
10 
11 static void hpfs_update_directory_times(struct inode *dir)
12 {
13 	time_t t = get_seconds();
14 	if (t == dir->i_mtime.tv_sec &&
15 	    t == dir->i_ctime.tv_sec)
16 		return;
17 	dir->i_mtime.tv_sec = dir->i_ctime.tv_sec = t;
18 	dir->i_mtime.tv_nsec = dir->i_ctime.tv_nsec = 0;
19 	hpfs_write_inode_nolock(dir);
20 }
21 
22 static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
23 {
24 	const unsigned char *name = dentry->d_name.name;
25 	unsigned len = dentry->d_name.len;
26 	struct quad_buffer_head qbh0;
27 	struct buffer_head *bh;
28 	struct hpfs_dirent *de;
29 	struct fnode *fnode;
30 	struct dnode *dnode;
31 	struct inode *result;
32 	fnode_secno fno;
33 	dnode_secno dno;
34 	int r;
35 	struct hpfs_dirent dee;
36 	int err;
37 	if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
38 	hpfs_lock(dir->i_sb);
39 	err = -ENOSPC;
40 	fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
41 	if (!fnode)
42 		goto bail;
43 	dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
44 	if (!dnode)
45 		goto bail1;
46 	memset(&dee, 0, sizeof dee);
47 	dee.directory = 1;
48 	if (!(mode & 0222)) dee.read_only = 1;
49 	/*dee.archive = 0;*/
50 	dee.hidden = name[0] == '.';
51 	dee.fnode = cpu_to_le32(fno);
52 	dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
53 	result = new_inode(dir->i_sb);
54 	if (!result)
55 		goto bail2;
56 	hpfs_init_inode(result);
57 	result->i_ino = fno;
58 	hpfs_i(result)->i_parent_dir = dir->i_ino;
59 	hpfs_i(result)->i_dno = dno;
60 	result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
61 	result->i_ctime.tv_nsec = 0;
62 	result->i_mtime.tv_nsec = 0;
63 	result->i_atime.tv_nsec = 0;
64 	hpfs_i(result)->i_ea_size = 0;
65 	result->i_mode |= S_IFDIR;
66 	result->i_op = &hpfs_dir_iops;
67 	result->i_fop = &hpfs_dir_ops;
68 	result->i_blocks = 4;
69 	result->i_size = 2048;
70 	set_nlink(result, 2);
71 	if (dee.read_only)
72 		result->i_mode &= ~0222;
73 
74 	r = hpfs_add_dirent(dir, name, len, &dee);
75 	if (r == 1)
76 		goto bail3;
77 	if (r == -1) {
78 		err = -EEXIST;
79 		goto bail3;
80 	}
81 	fnode->len = len;
82 	memcpy(fnode->name, name, len > 15 ? 15 : len);
83 	fnode->up = cpu_to_le32(dir->i_ino);
84 	fnode->flags |= FNODE_dir;
85 	fnode->btree.n_free_nodes = 7;
86 	fnode->btree.n_used_nodes = 1;
87 	fnode->btree.first_free = cpu_to_le16(0x14);
88 	fnode->u.external[0].disk_secno = cpu_to_le32(dno);
89 	fnode->u.external[0].file_secno = cpu_to_le32(-1);
90 	dnode->root_dnode = 1;
91 	dnode->up = cpu_to_le32(fno);
92 	de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
93 	de->creation_date = de->write_date = de->read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
94 	if (!(mode & 0222)) de->read_only = 1;
95 	de->first = de->directory = 1;
96 	/*de->hidden = de->system = 0;*/
97 	de->fnode = cpu_to_le32(fno);
98 	mark_buffer_dirty(bh);
99 	brelse(bh);
100 	hpfs_mark_4buffers_dirty(&qbh0);
101 	hpfs_brelse4(&qbh0);
102 	inc_nlink(dir);
103 	insert_inode_hash(result);
104 
105 	if (!uid_eq(result->i_uid, current_fsuid()) ||
106 	    !gid_eq(result->i_gid, current_fsgid()) ||
107 	    result->i_mode != (mode | S_IFDIR)) {
108 		result->i_uid = current_fsuid();
109 		result->i_gid = current_fsgid();
110 		result->i_mode = mode | S_IFDIR;
111 		hpfs_write_inode_nolock(result);
112 	}
113 	hpfs_update_directory_times(dir);
114 	d_instantiate(dentry, result);
115 	hpfs_unlock(dir->i_sb);
116 	return 0;
117 bail3:
118 	iput(result);
119 bail2:
120 	hpfs_brelse4(&qbh0);
121 	hpfs_free_dnode(dir->i_sb, dno);
122 bail1:
123 	brelse(bh);
124 	hpfs_free_sectors(dir->i_sb, fno, 1);
125 bail:
126 	hpfs_unlock(dir->i_sb);
127 	return err;
128 }
129 
130 static int hpfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
131 {
132 	const unsigned char *name = dentry->d_name.name;
133 	unsigned len = dentry->d_name.len;
134 	struct inode *result = NULL;
135 	struct buffer_head *bh;
136 	struct fnode *fnode;
137 	fnode_secno fno;
138 	int r;
139 	struct hpfs_dirent dee;
140 	int err;
141 	if ((err = hpfs_chk_name(name, &len)))
142 		return err==-ENOENT ? -EINVAL : err;
143 	hpfs_lock(dir->i_sb);
144 	err = -ENOSPC;
145 	fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
146 	if (!fnode)
147 		goto bail;
148 	memset(&dee, 0, sizeof dee);
149 	if (!(mode & 0222)) dee.read_only = 1;
150 	dee.archive = 1;
151 	dee.hidden = name[0] == '.';
152 	dee.fnode = cpu_to_le32(fno);
153 	dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
154 
155 	result = new_inode(dir->i_sb);
156 	if (!result)
157 		goto bail1;
158 
159 	hpfs_init_inode(result);
160 	result->i_ino = fno;
161 	result->i_mode |= S_IFREG;
162 	result->i_mode &= ~0111;
163 	result->i_op = &hpfs_file_iops;
164 	result->i_fop = &hpfs_file_ops;
165 	set_nlink(result, 1);
166 	hpfs_i(result)->i_parent_dir = dir->i_ino;
167 	result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
168 	result->i_ctime.tv_nsec = 0;
169 	result->i_mtime.tv_nsec = 0;
170 	result->i_atime.tv_nsec = 0;
171 	hpfs_i(result)->i_ea_size = 0;
172 	if (dee.read_only)
173 		result->i_mode &= ~0222;
174 	result->i_blocks = 1;
175 	result->i_size = 0;
176 	result->i_data.a_ops = &hpfs_aops;
177 	hpfs_i(result)->mmu_private = 0;
178 
179 	r = hpfs_add_dirent(dir, name, len, &dee);
180 	if (r == 1)
181 		goto bail2;
182 	if (r == -1) {
183 		err = -EEXIST;
184 		goto bail2;
185 	}
186 	fnode->len = len;
187 	memcpy(fnode->name, name, len > 15 ? 15 : len);
188 	fnode->up = cpu_to_le32(dir->i_ino);
189 	mark_buffer_dirty(bh);
190 	brelse(bh);
191 
192 	insert_inode_hash(result);
193 
194 	if (!uid_eq(result->i_uid, current_fsuid()) ||
195 	    !gid_eq(result->i_gid, current_fsgid()) ||
196 	    result->i_mode != (mode | S_IFREG)) {
197 		result->i_uid = current_fsuid();
198 		result->i_gid = current_fsgid();
199 		result->i_mode = mode | S_IFREG;
200 		hpfs_write_inode_nolock(result);
201 	}
202 	hpfs_update_directory_times(dir);
203 	d_instantiate(dentry, result);
204 	hpfs_unlock(dir->i_sb);
205 	return 0;
206 
207 bail2:
208 	iput(result);
209 bail1:
210 	brelse(bh);
211 	hpfs_free_sectors(dir->i_sb, fno, 1);
212 bail:
213 	hpfs_unlock(dir->i_sb);
214 	return err;
215 }
216 
217 static int hpfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
218 {
219 	const unsigned char *name = dentry->d_name.name;
220 	unsigned len = dentry->d_name.len;
221 	struct buffer_head *bh;
222 	struct fnode *fnode;
223 	fnode_secno fno;
224 	int r;
225 	struct hpfs_dirent dee;
226 	struct inode *result = NULL;
227 	int err;
228 	if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
229 	if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
230 	hpfs_lock(dir->i_sb);
231 	err = -ENOSPC;
232 	fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
233 	if (!fnode)
234 		goto bail;
235 	memset(&dee, 0, sizeof dee);
236 	if (!(mode & 0222)) dee.read_only = 1;
237 	dee.archive = 1;
238 	dee.hidden = name[0] == '.';
239 	dee.fnode = cpu_to_le32(fno);
240 	dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
241 
242 	result = new_inode(dir->i_sb);
243 	if (!result)
244 		goto bail1;
245 
246 	hpfs_init_inode(result);
247 	result->i_ino = fno;
248 	hpfs_i(result)->i_parent_dir = dir->i_ino;
249 	result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
250 	result->i_ctime.tv_nsec = 0;
251 	result->i_mtime.tv_nsec = 0;
252 	result->i_atime.tv_nsec = 0;
253 	hpfs_i(result)->i_ea_size = 0;
254 	result->i_uid = current_fsuid();
255 	result->i_gid = current_fsgid();
256 	set_nlink(result, 1);
257 	result->i_size = 0;
258 	result->i_blocks = 1;
259 	init_special_inode(result, mode, rdev);
260 
261 	r = hpfs_add_dirent(dir, name, len, &dee);
262 	if (r == 1)
263 		goto bail2;
264 	if (r == -1) {
265 		err = -EEXIST;
266 		goto bail2;
267 	}
268 	fnode->len = len;
269 	memcpy(fnode->name, name, len > 15 ? 15 : len);
270 	fnode->up = cpu_to_le32(dir->i_ino);
271 	mark_buffer_dirty(bh);
272 
273 	insert_inode_hash(result);
274 
275 	hpfs_write_inode_nolock(result);
276 	hpfs_update_directory_times(dir);
277 	d_instantiate(dentry, result);
278 	brelse(bh);
279 	hpfs_unlock(dir->i_sb);
280 	return 0;
281 bail2:
282 	iput(result);
283 bail1:
284 	brelse(bh);
285 	hpfs_free_sectors(dir->i_sb, fno, 1);
286 bail:
287 	hpfs_unlock(dir->i_sb);
288 	return err;
289 }
290 
291 static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
292 {
293 	const unsigned char *name = dentry->d_name.name;
294 	unsigned len = dentry->d_name.len;
295 	struct buffer_head *bh;
296 	struct fnode *fnode;
297 	fnode_secno fno;
298 	int r;
299 	struct hpfs_dirent dee;
300 	struct inode *result;
301 	int err;
302 	if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
303 	hpfs_lock(dir->i_sb);
304 	if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
305 		hpfs_unlock(dir->i_sb);
306 		return -EPERM;
307 	}
308 	err = -ENOSPC;
309 	fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
310 	if (!fnode)
311 		goto bail;
312 	memset(&dee, 0, sizeof dee);
313 	dee.archive = 1;
314 	dee.hidden = name[0] == '.';
315 	dee.fnode = cpu_to_le32(fno);
316 	dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
317 
318 	result = new_inode(dir->i_sb);
319 	if (!result)
320 		goto bail1;
321 	result->i_ino = fno;
322 	hpfs_init_inode(result);
323 	hpfs_i(result)->i_parent_dir = dir->i_ino;
324 	result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
325 	result->i_ctime.tv_nsec = 0;
326 	result->i_mtime.tv_nsec = 0;
327 	result->i_atime.tv_nsec = 0;
328 	hpfs_i(result)->i_ea_size = 0;
329 	result->i_mode = S_IFLNK | 0777;
330 	result->i_uid = current_fsuid();
331 	result->i_gid = current_fsgid();
332 	result->i_blocks = 1;
333 	set_nlink(result, 1);
334 	result->i_size = strlen(symlink);
335 	inode_nohighmem(result);
336 	result->i_op = &page_symlink_inode_operations;
337 	result->i_data.a_ops = &hpfs_symlink_aops;
338 
339 	r = hpfs_add_dirent(dir, name, len, &dee);
340 	if (r == 1)
341 		goto bail2;
342 	if (r == -1) {
343 		err = -EEXIST;
344 		goto bail2;
345 	}
346 	fnode->len = len;
347 	memcpy(fnode->name, name, len > 15 ? 15 : len);
348 	fnode->up = cpu_to_le32(dir->i_ino);
349 	hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
350 	mark_buffer_dirty(bh);
351 	brelse(bh);
352 
353 	insert_inode_hash(result);
354 
355 	hpfs_write_inode_nolock(result);
356 	hpfs_update_directory_times(dir);
357 	d_instantiate(dentry, result);
358 	hpfs_unlock(dir->i_sb);
359 	return 0;
360 bail2:
361 	iput(result);
362 bail1:
363 	brelse(bh);
364 	hpfs_free_sectors(dir->i_sb, fno, 1);
365 bail:
366 	hpfs_unlock(dir->i_sb);
367 	return err;
368 }
369 
370 static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
371 {
372 	const unsigned char *name = dentry->d_name.name;
373 	unsigned len = dentry->d_name.len;
374 	struct quad_buffer_head qbh;
375 	struct hpfs_dirent *de;
376 	struct inode *inode = d_inode(dentry);
377 	dnode_secno dno;
378 	int r;
379 	int rep = 0;
380 	int err;
381 
382 	hpfs_lock(dir->i_sb);
383 	hpfs_adjust_length(name, &len);
384 again:
385 	err = -ENOENT;
386 	de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
387 	if (!de)
388 		goto out;
389 
390 	err = -EPERM;
391 	if (de->first)
392 		goto out1;
393 
394 	err = -EISDIR;
395 	if (de->directory)
396 		goto out1;
397 
398 	r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
399 	switch (r) {
400 	case 1:
401 		hpfs_error(dir->i_sb, "there was error when removing dirent");
402 		err = -EFSERROR;
403 		break;
404 	case 2:		/* no space for deleting, try to truncate file */
405 
406 		err = -ENOSPC;
407 		if (rep++)
408 			break;
409 
410 		dentry_unhash(dentry);
411 		if (!d_unhashed(dentry)) {
412 			hpfs_unlock(dir->i_sb);
413 			return -ENOSPC;
414 		}
415 		if (generic_permission(inode, MAY_WRITE) ||
416 		    !S_ISREG(inode->i_mode) ||
417 		    get_write_access(inode)) {
418 			d_rehash(dentry);
419 		} else {
420 			struct iattr newattrs;
421 			/*pr_info("truncating file before delete.\n");*/
422 			newattrs.ia_size = 0;
423 			newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
424 			err = notify_change(dentry, &newattrs, NULL);
425 			put_write_access(inode);
426 			if (!err)
427 				goto again;
428 		}
429 		hpfs_unlock(dir->i_sb);
430 		return -ENOSPC;
431 	default:
432 		drop_nlink(inode);
433 		err = 0;
434 	}
435 	goto out;
436 
437 out1:
438 	hpfs_brelse4(&qbh);
439 out:
440 	if (!err)
441 		hpfs_update_directory_times(dir);
442 	hpfs_unlock(dir->i_sb);
443 	return err;
444 }
445 
446 static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
447 {
448 	const unsigned char *name = dentry->d_name.name;
449 	unsigned len = dentry->d_name.len;
450 	struct quad_buffer_head qbh;
451 	struct hpfs_dirent *de;
452 	struct inode *inode = d_inode(dentry);
453 	dnode_secno dno;
454 	int n_items = 0;
455 	int err;
456 	int r;
457 
458 	hpfs_adjust_length(name, &len);
459 	hpfs_lock(dir->i_sb);
460 	err = -ENOENT;
461 	de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
462 	if (!de)
463 		goto out;
464 
465 	err = -EPERM;
466 	if (de->first)
467 		goto out1;
468 
469 	err = -ENOTDIR;
470 	if (!de->directory)
471 		goto out1;
472 
473 	hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
474 	err = -ENOTEMPTY;
475 	if (n_items)
476 		goto out1;
477 
478 	r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
479 	switch (r) {
480 	case 1:
481 		hpfs_error(dir->i_sb, "there was error when removing dirent");
482 		err = -EFSERROR;
483 		break;
484 	case 2:
485 		err = -ENOSPC;
486 		break;
487 	default:
488 		drop_nlink(dir);
489 		clear_nlink(inode);
490 		err = 0;
491 	}
492 	goto out;
493 out1:
494 	hpfs_brelse4(&qbh);
495 out:
496 	if (!err)
497 		hpfs_update_directory_times(dir);
498 	hpfs_unlock(dir->i_sb);
499 	return err;
500 }
501 
502 static int hpfs_symlink_readpage(struct file *file, struct page *page)
503 {
504 	char *link = page_address(page);
505 	struct inode *i = page->mapping->host;
506 	struct fnode *fnode;
507 	struct buffer_head *bh;
508 	int err;
509 
510 	err = -EIO;
511 	hpfs_lock(i->i_sb);
512 	if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
513 		goto fail;
514 	err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
515 	brelse(bh);
516 	if (err)
517 		goto fail;
518 	hpfs_unlock(i->i_sb);
519 	SetPageUptodate(page);
520 	unlock_page(page);
521 	return 0;
522 
523 fail:
524 	hpfs_unlock(i->i_sb);
525 	SetPageError(page);
526 	unlock_page(page);
527 	return err;
528 }
529 
530 const struct address_space_operations hpfs_symlink_aops = {
531 	.readpage	= hpfs_symlink_readpage
532 };
533 
534 static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
535 		struct inode *new_dir, struct dentry *new_dentry)
536 {
537 	const unsigned char *old_name = old_dentry->d_name.name;
538 	unsigned old_len = old_dentry->d_name.len;
539 	const unsigned char *new_name = new_dentry->d_name.name;
540 	unsigned new_len = new_dentry->d_name.len;
541 	struct inode *i = d_inode(old_dentry);
542 	struct inode *new_inode = d_inode(new_dentry);
543 	struct quad_buffer_head qbh, qbh1;
544 	struct hpfs_dirent *dep, *nde;
545 	struct hpfs_dirent de;
546 	dnode_secno dno;
547 	int r;
548 	struct buffer_head *bh;
549 	struct fnode *fnode;
550 	int err;
551 
552 	if ((err = hpfs_chk_name(new_name, &new_len))) return err;
553 	err = 0;
554 	hpfs_adjust_length(old_name, &old_len);
555 
556 	hpfs_lock(i->i_sb);
557 	/* order doesn't matter, due to VFS exclusion */
558 
559 	/* Erm? Moving over the empty non-busy directory is perfectly legal */
560 	if (new_inode && S_ISDIR(new_inode->i_mode)) {
561 		err = -EINVAL;
562 		goto end1;
563 	}
564 
565 	if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
566 		hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
567 		err = -ENOENT;
568 		goto end1;
569 	}
570 	copy_de(&de, dep);
571 	de.hidden = new_name[0] == '.';
572 
573 	if (new_inode) {
574 		int r;
575 		if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
576 			if ((nde = map_dirent(new_dir, hpfs_i(new_dir)->i_dno, new_name, new_len, NULL, &qbh1))) {
577 				clear_nlink(new_inode);
578 				copy_de(nde, &de);
579 				memcpy(nde->name, new_name, new_len);
580 				hpfs_mark_4buffers_dirty(&qbh1);
581 				hpfs_brelse4(&qbh1);
582 				goto end;
583 			}
584 			hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
585 			err = -EFSERROR;
586 			goto end1;
587 		}
588 		err = r == 2 ? -ENOSPC : r == 1 ? -EFSERROR : 0;
589 		goto end1;
590 	}
591 
592 	if (new_dir == old_dir) hpfs_brelse4(&qbh);
593 
594 	if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
595 		if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
596 		err = r == 1 ? -ENOSPC : -EFSERROR;
597 		if (new_dir != old_dir) hpfs_brelse4(&qbh);
598 		goto end1;
599 	}
600 
601 	if (new_dir == old_dir)
602 		if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
603 			hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
604 			err = -ENOENT;
605 			goto end1;
606 		}
607 
608 	if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
609 		hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
610 		err = r == 2 ? -ENOSPC : -EFSERROR;
611 		goto end1;
612 	}
613 
614 end:
615 	hpfs_i(i)->i_parent_dir = new_dir->i_ino;
616 	if (S_ISDIR(i->i_mode)) {
617 		inc_nlink(new_dir);
618 		drop_nlink(old_dir);
619 	}
620 	if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
621 		fnode->up = cpu_to_le32(new_dir->i_ino);
622 		fnode->len = new_len;
623 		memcpy(fnode->name, new_name, new_len>15?15:new_len);
624 		if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
625 		mark_buffer_dirty(bh);
626 		brelse(bh);
627 	}
628 end1:
629 	if (!err) {
630 		hpfs_update_directory_times(old_dir);
631 		hpfs_update_directory_times(new_dir);
632 	}
633 	hpfs_unlock(i->i_sb);
634 	return err;
635 }
636 
637 const struct inode_operations hpfs_dir_iops =
638 {
639 	.create		= hpfs_create,
640 	.lookup		= hpfs_lookup,
641 	.unlink		= hpfs_unlink,
642 	.symlink	= hpfs_symlink,
643 	.mkdir		= hpfs_mkdir,
644 	.rmdir		= hpfs_rmdir,
645 	.mknod		= hpfs_mknod,
646 	.rename		= hpfs_rename,
647 	.setattr	= hpfs_setattr,
648 };
649