This source file includes following definitions.
- ext2_match
- ext2_find_entry
- ext2_lookup
- ext2_add_entry
- ext2_delete_entry
- ext2_create
- ext2_mknod
- ext2_mkdir
- empty_dir
- ext2_rmdir
- ext2_unlink
- ext2_symlink
- ext2_link
- subdir
- do_ext2_rename
- ext2_rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <asm/segment.h>
16
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/ext2_fs.h>
20 #include <linux/fcntl.h>
21 #include <linux/sched.h>
22 #include <linux/stat.h>
23 #include <linux/string.h>
24 #include <linux/locks.h>
25
26
27
28
29
30
31
32
33
34
35 #define NAMEI_RA_CHUNKS 2
36 #define NAMEI_RA_BLOCKS 4
37 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
38 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
39
40
41
42
43 static int ext2_match (int len, const char * const name,
44 struct ext2_dir_entry * de)
45 {
46 if (!de || !de->inode || len > EXT2_NAME_LEN)
47 return 0;
48
49
50
51 if (!len && de->name_len == 1 && (de->name[0] == '.') &&
52 (de->name[1] == '\0'))
53 return 1;
54 if (len != de->name_len)
55 return 0;
56 return !memcmp(name, de->name, len);
57 }
58
59
60
61
62
63
64
65
66
67 static struct buffer_head * ext2_find_entry (struct inode * dir,
68 const char * const name, int namelen,
69 struct ext2_dir_entry ** res_dir)
70 {
71 struct super_block * sb;
72 struct buffer_head * bh_use[NAMEI_RA_SIZE];
73 struct buffer_head * bh_read[NAMEI_RA_SIZE];
74 unsigned long offset;
75 int block, toread, i, err;
76
77 *res_dir = NULL;
78 if (!dir)
79 return NULL;
80 sb = dir->i_sb;
81
82 #ifdef NO_TRUNCATE
83 if (namelen > EXT2_NAME_LEN)
84 return NULL;
85 #else
86 if (namelen > EXT2_NAME_LEN)
87 namelen = EXT2_NAME_LEN;
88 #endif
89
90 memset (bh_use, 0, sizeof (bh_use));
91 toread = 0;
92 for (block = 0; block < NAMEI_RA_SIZE; ++block) {
93 struct buffer_head * bh;
94
95 if ((block << EXT2_BLOCK_SIZE_BITS (sb)) >= dir->i_size)
96 break;
97 bh = ext2_getblk (dir, block, 0, &err);
98 bh_use[block] = bh;
99 if (bh && !bh->b_uptodate)
100 bh_read[toread++] = bh;
101 }
102
103 block = 0;
104 offset = 0;
105 while (offset < dir->i_size) {
106 struct buffer_head * bh;
107 struct ext2_dir_entry * de;
108 char * dlimit;
109
110 if ((block % NAMEI_RA_BLOCKS) == 0 && toread) {
111 ll_rw_block (READ, toread, bh_read);
112 toread = 0;
113 }
114 bh = bh_use[block % NAMEI_RA_SIZE];
115 if (!bh)
116 ext2_panic (sb, "ext2_find_entry",
117 "buffer head pointer is NULL");
118 wait_on_buffer (bh);
119 if (!bh->b_uptodate) {
120
121
122
123 break;
124 }
125
126 de = (struct ext2_dir_entry *) bh->b_data;
127 dlimit = bh->b_data + sb->s_blocksize;
128 while ((char *) de < dlimit) {
129 if (!ext2_check_dir_entry ("ext2_find_entry", dir,
130 de, bh, offset))
131 goto failure;
132 if (de->inode != 0 && ext2_match (namelen, name, de)) {
133 for (i = 0; i < NAMEI_RA_SIZE; ++i) {
134 if (bh_use[i] != bh)
135 brelse (bh_use[i]);
136 }
137 *res_dir = de;
138 return bh;
139 }
140 offset += de->rec_len;
141 de = (struct ext2_dir_entry *)
142 ((char *) de + de->rec_len);
143 }
144
145 brelse (bh);
146 if (((block + NAMEI_RA_SIZE) << EXT2_BLOCK_SIZE_BITS (sb)) >=
147 dir->i_size)
148 bh = NULL;
149 else
150 bh = ext2_getblk (dir, block + NAMEI_RA_SIZE, 0, &err);
151 bh_use[block++ % NAMEI_RA_SIZE] = bh;
152 if (bh && !bh->b_uptodate)
153 bh_read[toread++] = bh;
154 }
155
156 failure:
157 for (i = 0; i < NAMEI_RA_SIZE; ++i)
158 brelse (bh_use[i]);
159 return NULL;
160 }
161
162 int ext2_lookup (struct inode * dir, const char * name, int len,
163 struct inode ** result)
164 {
165 unsigned long ino;
166 struct ext2_dir_entry * de;
167 struct buffer_head * bh;
168
169 *result = NULL;
170 if (!dir)
171 return -ENOENT;
172 if (!S_ISDIR(dir->i_mode)) {
173 iput (dir);
174 return -ENOENT;
175 }
176 if (dcache_lookup(dir, name, len, &ino)) {
177 if (!ino) {
178 iput(dir);
179 return -ENOENT;
180 }
181 if (!(*result = iget (dir->i_sb, ino))) {
182 iput (dir);
183 return -EACCES;
184 }
185 iput (dir);
186 return 0;
187 }
188 ino = dir->i_version;
189 if (!(bh = ext2_find_entry (dir, name, len, &de))) {
190 if (ino == dir->i_version)
191 dcache_add(dir, name, len, 0);
192 iput (dir);
193 return -ENOENT;
194 }
195 ino = de->inode;
196 dcache_add(dir, name, len, ino);
197 brelse (bh);
198 if (!(*result = iget (dir->i_sb, ino))) {
199 iput (dir);
200 return -EACCES;
201 }
202 iput (dir);
203 return 0;
204 }
205
206
207
208
209
210
211
212
213
214
215
216 static struct buffer_head * ext2_add_entry (struct inode * dir,
217 const char * name, int namelen,
218 struct ext2_dir_entry ** res_dir,
219 int *err)
220 {
221 unsigned long offset;
222 unsigned short rec_len;
223 struct buffer_head * bh;
224 struct ext2_dir_entry * de, * de1;
225 struct super_block * sb;
226
227 *err = -EINVAL;
228 *res_dir = NULL;
229 if (!dir)
230 return NULL;
231 sb = dir->i_sb;
232 #ifdef NO_TRUNCATE
233 if (namelen > EXT2_NAME_LEN)
234 return NULL;
235 #else
236 if (namelen > EXT2_NAME_LEN)
237 namelen = EXT2_NAME_LEN;
238 #endif
239 if (!namelen)
240 return NULL;
241
242
243
244 if (dir->i_size == 0)
245 {
246 *err = -ENOENT;
247 return NULL;
248 }
249 bh = ext2_bread (dir, 0, 0, err);
250 if (!bh)
251 return NULL;
252 rec_len = EXT2_DIR_REC_LEN(namelen);
253 offset = 0;
254 de = (struct ext2_dir_entry *) bh->b_data;
255 *err = -ENOSPC;
256 while (1) {
257 if ((char *)de >= sb->s_blocksize + bh->b_data) {
258 brelse (bh);
259 bh = NULL;
260 bh = ext2_bread (dir, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, err);
261 if (!bh)
262 return NULL;
263 if (dir->i_size <= offset) {
264 if (dir->i_size == 0) {
265 *err = -ENOENT;
266 return NULL;
267 }
268
269 ext2_debug ("creating next block\n");
270
271 de = (struct ext2_dir_entry *) bh->b_data;
272 de->inode = 0;
273 de->rec_len = sb->s_blocksize;
274 dir->i_size = offset + sb->s_blocksize;
275 dir->i_dirt = 1;
276 } else {
277
278 ext2_debug ("skipping to next block\n");
279
280 de = (struct ext2_dir_entry *) bh->b_data;
281 }
282 }
283 if (!ext2_check_dir_entry ("ext2_add_entry", dir, de, bh,
284 offset)) {
285 *err = -ENOENT;
286 brelse (bh);
287 return NULL;
288 }
289 if (de->inode != 0 && ext2_match (namelen, name, de)) {
290 *err = -EEXIST;
291 brelse (bh);
292 return NULL;
293 }
294 if ((de->inode == 0 && de->rec_len >= rec_len) ||
295 (de->rec_len >= EXT2_DIR_REC_LEN(de->name_len) + rec_len)) {
296 offset += de->rec_len;
297 if (de->inode) {
298 de1 = (struct ext2_dir_entry *) ((char *) de +
299 EXT2_DIR_REC_LEN(de->name_len));
300 de1->rec_len = de->rec_len -
301 EXT2_DIR_REC_LEN(de->name_len);
302 de->rec_len = EXT2_DIR_REC_LEN(de->name_len);
303 de = de1;
304 }
305 de->inode = 0;
306 de->name_len = namelen;
307 memcpy (de->name, name, namelen);
308
309
310
311
312
313
314
315
316
317
318
319 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
320 dir->i_dirt = 1;
321 mark_buffer_dirty(bh, 1);
322 *res_dir = de;
323 *err = 0;
324 return bh;
325 }
326 offset += de->rec_len;
327 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
328 }
329 brelse (bh);
330 return NULL;
331 }
332
333
334
335
336
337 static int ext2_delete_entry (struct ext2_dir_entry * dir,
338 struct buffer_head * bh)
339 {
340 struct ext2_dir_entry * de, * pde;
341 int i;
342
343 i = 0;
344 pde = NULL;
345 de = (struct ext2_dir_entry *) bh->b_data;
346 while (i < bh->b_size) {
347 if (!ext2_check_dir_entry ("ext2_delete_entry", NULL,
348 de, bh, i))
349 return -EIO;
350 if (de == dir) {
351 if (pde)
352 pde->rec_len += dir->rec_len;
353 dir->inode = 0;
354 return 0;
355 }
356 i += de->rec_len;
357 pde = de;
358 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
359 }
360 return -ENOENT;
361 }
362
363 int ext2_create (struct inode * dir,const char * name, int len, int mode,
364 struct inode ** result)
365 {
366 struct inode * inode;
367 struct buffer_head * bh;
368 struct ext2_dir_entry * de;
369 int err;
370
371 *result = NULL;
372 if (!dir)
373 return -ENOENT;
374 inode = ext2_new_inode (dir, mode);
375 if (!inode) {
376 iput (dir);
377 return -ENOSPC;
378 }
379 inode->i_op = &ext2_file_inode_operations;
380 inode->i_mode = mode;
381 inode->i_dirt = 1;
382 bh = ext2_add_entry (dir, name, len, &de, &err);
383 if (!bh) {
384 inode->i_nlink--;
385 inode->i_dirt = 1;
386 iput (inode);
387 iput (dir);
388 return err;
389 }
390 de->inode = inode->i_ino;
391 dir->i_version = ++event;
392 dcache_add(dir, de->name, de->name_len, de->inode);
393 mark_buffer_dirty(bh, 1);
394 if (IS_SYNC(dir)) {
395 ll_rw_block (WRITE, 1, &bh);
396 wait_on_buffer (bh);
397 }
398 brelse (bh);
399 iput (dir);
400 *result = inode;
401 return 0;
402 }
403
404 int ext2_mknod (struct inode * dir, const char * name, int len, int mode,
405 int rdev)
406 {
407 struct inode * inode;
408 struct buffer_head * bh;
409 struct ext2_dir_entry * de;
410 int err;
411
412 if (!dir)
413 return -ENOENT;
414 bh = ext2_find_entry (dir, name, len, &de);
415 if (bh) {
416 brelse (bh);
417 iput (dir);
418 return -EEXIST;
419 }
420 inode = ext2_new_inode (dir, mode);
421 if (!inode) {
422 iput (dir);
423 return -ENOSPC;
424 }
425 inode->i_uid = current->fsuid;
426 inode->i_mode = mode;
427 inode->i_op = NULL;
428 if (S_ISREG(inode->i_mode))
429 inode->i_op = &ext2_file_inode_operations;
430 else if (S_ISDIR(inode->i_mode)) {
431 inode->i_op = &ext2_dir_inode_operations;
432 if (dir->i_mode & S_ISGID)
433 inode->i_mode |= S_ISGID;
434 }
435 else if (S_ISLNK(inode->i_mode))
436 inode->i_op = &ext2_symlink_inode_operations;
437 else if (S_ISCHR(inode->i_mode))
438 inode->i_op = &chrdev_inode_operations;
439 else if (S_ISBLK(inode->i_mode))
440 inode->i_op = &blkdev_inode_operations;
441 else if (S_ISFIFO(inode->i_mode))
442 init_fifo(inode);
443 if (S_ISBLK(mode) || S_ISCHR(mode))
444 inode->i_rdev = rdev;
445 inode->i_dirt = 1;
446 bh = ext2_add_entry (dir, name, len, &de, &err);
447 if (!bh) {
448 inode->i_nlink--;
449 inode->i_dirt = 1;
450 iput (inode);
451 iput (dir);
452 return err;
453 }
454 de->inode = inode->i_ino;
455 dir->i_version = ++event;
456 dcache_add(dir, de->name, de->name_len, de->inode);
457 mark_buffer_dirty(bh, 1);
458 if (IS_SYNC(dir)) {
459 ll_rw_block (WRITE, 1, &bh);
460 wait_on_buffer (bh);
461 }
462 brelse (bh);
463 iput (dir);
464 iput (inode);
465 return 0;
466 }
467
468 int ext2_mkdir (struct inode * dir, const char * name, int len, int mode)
469 {
470 struct inode * inode;
471 struct buffer_head * bh, * dir_block;
472 struct ext2_dir_entry * de;
473 int err;
474
475 if (!dir)
476 return -ENOENT;
477 bh = ext2_find_entry (dir, name, len, &de);
478 if (bh) {
479 brelse (bh);
480 iput (dir);
481 return -EEXIST;
482 }
483 if (dir->i_nlink >= EXT2_LINK_MAX) {
484 iput (dir);
485 return -EMLINK;
486 }
487 inode = ext2_new_inode (dir, S_IFDIR);
488 if (!inode) {
489 iput (dir);
490 return -ENOSPC;
491 }
492 inode->i_op = &ext2_dir_inode_operations;
493 inode->i_size = inode->i_sb->s_blocksize;
494 dir_block = ext2_bread (inode, 0, 1, &err);
495 if (!dir_block) {
496 iput (dir);
497 inode->i_nlink--;
498 inode->i_dirt = 1;
499 iput (inode);
500 return err;
501 }
502 inode->i_blocks = inode->i_sb->s_blocksize / 512;
503 de = (struct ext2_dir_entry *) dir_block->b_data;
504 de->inode = inode->i_ino;
505 de->name_len = 1;
506 de->rec_len = EXT2_DIR_REC_LEN(de->name_len);
507 strcpy (de->name, ".");
508 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
509 de->inode = dir->i_ino;
510 de->rec_len = inode->i_sb->s_blocksize - EXT2_DIR_REC_LEN(1);
511 de->name_len = 2;
512 strcpy (de->name, "..");
513 inode->i_nlink = 2;
514 mark_buffer_dirty(dir_block, 1);
515 brelse (dir_block);
516 inode->i_mode = S_IFDIR | (mode & S_IRWXUGO & ~current->fs->umask);
517 if (dir->i_mode & S_ISGID)
518 inode->i_mode |= S_ISGID;
519 inode->i_dirt = 1;
520 bh = ext2_add_entry (dir, name, len, &de, &err);
521 if (!bh) {
522 iput (dir);
523 inode->i_nlink = 0;
524 inode->i_dirt = 1;
525 iput (inode);
526 return err;
527 }
528 de->inode = inode->i_ino;
529 dir->i_version = ++event;
530 dcache_add(dir, de->name, de->name_len, de->inode);
531 mark_buffer_dirty(bh, 1);
532 if (IS_SYNC(dir)) {
533 ll_rw_block (WRITE, 1, &bh);
534 wait_on_buffer (bh);
535 }
536 dir->i_nlink++;
537 dir->i_dirt = 1;
538 iput (dir);
539 iput (inode);
540 brelse (bh);
541 return 0;
542 }
543
544
545
546
547 static int empty_dir (struct inode * inode)
548 {
549 unsigned long offset;
550 struct buffer_head * bh;
551 struct ext2_dir_entry * de, * de1;
552 struct super_block * sb;
553 int err;
554
555 sb = inode->i_sb;
556 if (inode->i_size < EXT2_DIR_REC_LEN(1) + EXT2_DIR_REC_LEN(2) ||
557 !(bh = ext2_bread (inode, 0, 0, &err))) {
558 ext2_warning (inode->i_sb, "empty_dir",
559 "bad directory (dir %lu)", inode->i_ino);
560 return 1;
561 }
562 de = (struct ext2_dir_entry *) bh->b_data;
563 de1 = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
564 if (de->inode != inode->i_ino || !de1->inode ||
565 strcmp (".", de->name) || strcmp ("..", de1->name)) {
566 ext2_warning (inode->i_sb, "empty_dir",
567 "bad directory (dir %lu)", inode->i_ino);
568 return 1;
569 }
570 offset = de->rec_len + de1->rec_len;
571 de = (struct ext2_dir_entry *) ((char *) de1 + de1->rec_len);
572 while (offset < inode->i_size ) {
573 if ((void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {
574 brelse (bh);
575 bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);
576 if (!bh) {
577 offset += sb->s_blocksize;
578 continue;
579 }
580 de = (struct ext2_dir_entry *) bh->b_data;
581 }
582 if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,
583 offset)) {
584 brelse (bh);
585 return 1;
586 }
587 if (de->inode) {
588 brelse (bh);
589 return 0;
590 }
591 offset += de->rec_len;
592 de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);
593 }
594 brelse (bh);
595 return 1;
596 }
597
598 int ext2_rmdir (struct inode * dir, const char * name, int len)
599 {
600 int retval;
601 struct inode * inode;
602 struct buffer_head * bh;
603 struct ext2_dir_entry * de;
604
605 repeat:
606 if (!dir)
607 return -ENOENT;
608 inode = NULL;
609 bh = ext2_find_entry (dir, name, len, &de);
610 retval = -ENOENT;
611 if (!bh)
612 goto end_rmdir;
613 retval = -EPERM;
614 if (!(inode = iget (dir->i_sb, de->inode)))
615 goto end_rmdir;
616 if (inode->i_dev != dir->i_dev)
617 goto end_rmdir;
618 if (de->inode != inode->i_ino) {
619 iput(inode);
620 brelse(bh);
621 current->counter = 0;
622 schedule();
623 goto repeat;
624 }
625 if ((dir->i_mode & S_ISVTX) && !fsuser() &&
626 current->fsuid != inode->i_uid &&
627 current->fsuid != dir->i_uid)
628 goto end_rmdir;
629 if (inode == dir)
630 goto end_rmdir;
631 if (!S_ISDIR(inode->i_mode)) {
632 retval = -ENOTDIR;
633 goto end_rmdir;
634 }
635 down(&inode->i_sem);
636 if (!empty_dir (inode))
637 retval = -ENOTEMPTY;
638 else if (de->inode != inode->i_ino)
639 retval = -ENOENT;
640 else {
641 if (inode->i_count > 1) {
642
643
644
645
646
647
648
649 inode->i_size = 0;
650 }
651 retval = ext2_delete_entry (de, bh);
652 dir->i_version = ++event;
653 }
654 up(&inode->i_sem);
655 if (retval)
656 goto end_rmdir;
657 mark_buffer_dirty(bh, 1);
658 if (IS_SYNC(dir)) {
659 ll_rw_block (WRITE, 1, &bh);
660 wait_on_buffer (bh);
661 }
662 if (inode->i_nlink != 2)
663 ext2_warning (inode->i_sb, "ext2_rmdir",
664 "empty directory has nlink!=2 (%d)",
665 inode->i_nlink);
666 inode->i_version = ++event;
667 inode->i_nlink = 0;
668 inode->i_dirt = 1;
669 dir->i_nlink--;
670 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
671 dir->i_dirt = 1;
672 end_rmdir:
673 iput (dir);
674 iput (inode);
675 brelse (bh);
676 return retval;
677 }
678
679 int ext2_unlink (struct inode * dir, const char * name, int len)
680 {
681 int retval;
682 struct inode * inode;
683 struct buffer_head * bh;
684 struct ext2_dir_entry * de;
685
686 repeat:
687 if (!dir)
688 return -ENOENT;
689 retval = -ENOENT;
690 inode = NULL;
691 bh = ext2_find_entry (dir, name, len, &de);
692 if (!bh)
693 goto end_unlink;
694 if (!(inode = iget (dir->i_sb, de->inode)))
695 goto end_unlink;
696 retval = -EPERM;
697 if (S_ISDIR(inode->i_mode))
698 goto end_unlink;
699 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
700 goto end_unlink;
701 if (de->inode != inode->i_ino) {
702 iput(inode);
703 brelse(bh);
704 current->counter = 0;
705 schedule();
706 goto repeat;
707 }
708 if ((dir->i_mode & S_ISVTX) && !fsuser() &&
709 current->fsuid != inode->i_uid &&
710 current->fsuid != dir->i_uid)
711 goto end_unlink;
712 if (!inode->i_nlink) {
713 ext2_warning (inode->i_sb, "ext2_unlink",
714 "Deleting nonexistent file (%lu), %d",
715 inode->i_ino, inode->i_nlink);
716 inode->i_nlink = 1;
717 }
718 retval = ext2_delete_entry (de, bh);
719 if (retval)
720 goto end_unlink;
721 dir->i_version = ++event;
722 mark_buffer_dirty(bh, 1);
723 if (IS_SYNC(dir)) {
724 ll_rw_block (WRITE, 1, &bh);
725 wait_on_buffer (bh);
726 }
727 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
728 dir->i_dirt = 1;
729 inode->i_nlink--;
730 inode->i_dirt = 1;
731 inode->i_ctime = dir->i_ctime;
732 retval = 0;
733 end_unlink:
734 brelse (bh);
735 iput (inode);
736 iput (dir);
737 return retval;
738 }
739
740 int ext2_symlink (struct inode * dir, const char * name, int len,
741 const char * symname)
742 {
743 struct ext2_dir_entry * de;
744 struct inode * inode = NULL;
745 struct buffer_head * bh = NULL, * name_block = NULL;
746 char * link;
747 int i, err;
748 int l;
749 char c;
750
751 if (!(inode = ext2_new_inode (dir, S_IFLNK))) {
752 iput (dir);
753 return -ENOSPC;
754 }
755 inode->i_mode = S_IFLNK | S_IRWXUGO;
756 inode->i_op = &ext2_symlink_inode_operations;
757 for (l = 0; l < inode->i_sb->s_blocksize - 1 &&
758 symname [l]; l++)
759 ;
760 if (l >= EXT2_N_BLOCKS * sizeof (unsigned long)) {
761
762 ext2_debug ("l=%d, normal symlink\n", l);
763
764 name_block = ext2_bread (inode, 0, 1, &err);
765 if (!name_block) {
766 iput (dir);
767 inode->i_nlink--;
768 inode->i_dirt = 1;
769 iput (inode);
770 return err;
771 }
772 link = name_block->b_data;
773 } else {
774 link = (char *) inode->u.ext2_i.i_data;
775
776 ext2_debug ("l=%d, fast symlink\n", l);
777
778 }
779 i = 0;
780 while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))
781 link[i++] = c;
782 link[i] = 0;
783 if (name_block) {
784 mark_buffer_dirty(name_block, 1);
785 brelse (name_block);
786 }
787 inode->i_size = i;
788 inode->i_dirt = 1;
789 bh = ext2_find_entry (dir, name, len, &de);
790 if (bh) {
791 inode->i_nlink--;
792 inode->i_dirt = 1;
793 iput (inode);
794 brelse (bh);
795 iput (dir);
796 return -EEXIST;
797 }
798 bh = ext2_add_entry (dir, name, len, &de, &err);
799 if (!bh) {
800 inode->i_nlink--;
801 inode->i_dirt = 1;
802 iput (inode);
803 iput (dir);
804 return err;
805 }
806 de->inode = inode->i_ino;
807 dir->i_version = ++event;
808 dcache_add(dir, de->name, de->name_len, de->inode);
809 mark_buffer_dirty(bh, 1);
810 if (IS_SYNC(dir)) {
811 ll_rw_block (WRITE, 1, &bh);
812 wait_on_buffer (bh);
813 }
814 brelse (bh);
815 iput (dir);
816 iput (inode);
817 return 0;
818 }
819
820 int ext2_link (struct inode * oldinode, struct inode * dir,
821 const char * name, int len)
822 {
823 struct ext2_dir_entry * de;
824 struct buffer_head * bh;
825 int err;
826
827 if (S_ISDIR(oldinode->i_mode)) {
828 iput (oldinode);
829 iput (dir);
830 return -EPERM;
831 }
832 if (IS_APPEND(oldinode) || IS_IMMUTABLE(oldinode)) {
833 iput (oldinode);
834 iput (dir);
835 return -EPERM;
836 }
837 if (oldinode->i_nlink >= EXT2_LINK_MAX) {
838 iput (oldinode);
839 iput (dir);
840 return -EMLINK;
841 }
842 bh = ext2_find_entry (dir, name, len, &de);
843 if (bh) {
844 brelse (bh);
845 iput (dir);
846 iput (oldinode);
847 return -EEXIST;
848 }
849 bh = ext2_add_entry (dir, name, len, &de, &err);
850 if (!bh) {
851 iput (dir);
852 iput (oldinode);
853 return err;
854 }
855 de->inode = oldinode->i_ino;
856 dir->i_version = ++event;
857 dcache_add(dir, de->name, de->name_len, de->inode);
858 mark_buffer_dirty(bh, 1);
859 if (IS_SYNC(dir)) {
860 ll_rw_block (WRITE, 1, &bh);
861 wait_on_buffer (bh);
862 }
863 brelse (bh);
864 iput (dir);
865 oldinode->i_nlink++;
866 oldinode->i_ctime = CURRENT_TIME;
867 oldinode->i_dirt = 1;
868 iput (oldinode);
869 return 0;
870 }
871
872 static int subdir (struct inode * new_inode, struct inode * old_inode)
873 {
874 int ino;
875 int result;
876
877 new_inode->i_count++;
878 result = 0;
879 for (;;) {
880 if (new_inode == old_inode) {
881 result = 1;
882 break;
883 }
884 if (new_inode->i_dev != old_inode->i_dev)
885 break;
886 ino = new_inode->i_ino;
887 if (ext2_lookup (new_inode, "..", 2, &new_inode))
888 break;
889 if (new_inode->i_ino == ino)
890 break;
891 }
892 iput (new_inode);
893 return result;
894 }
895
896 #define PARENT_INO(buffer) \
897 ((struct ext2_dir_entry *) ((char *) buffer + \
898 ((struct ext2_dir_entry *) buffer)->rec_len))->inode
899
900 #define PARENT_NAME(buffer) \
901 ((struct ext2_dir_entry *) ((char *) buffer + \
902 ((struct ext2_dir_entry *) buffer)->rec_len))->name
903
904
905
906
907
908
909
910
911
912
913
914
915 static int do_ext2_rename (struct inode * old_dir, const char * old_name,
916 int old_len, struct inode * new_dir,
917 const char * new_name, int new_len)
918 {
919 struct inode * old_inode, * new_inode;
920 struct buffer_head * old_bh, * new_bh, * dir_bh;
921 struct ext2_dir_entry * old_de, * new_de;
922 int retval;
923
924 goto start_up;
925 try_again:
926 if (new_bh && new_de)
927 ext2_delete_entry(new_de, new_bh);
928 brelse (old_bh);
929 brelse (new_bh);
930 brelse (dir_bh);
931 iput (old_inode);
932 iput (new_inode);
933 current->counter = 0;
934 schedule ();
935 start_up:
936 old_inode = new_inode = NULL;
937 old_bh = new_bh = dir_bh = NULL;
938 new_de = NULL;
939 old_bh = ext2_find_entry (old_dir, old_name, old_len, &old_de);
940 retval = -ENOENT;
941 if (!old_bh)
942 goto end_rename;
943 old_inode = __iget (old_dir->i_sb, old_de->inode, 0);
944 if (!old_inode)
945 goto end_rename;
946 retval = -EPERM;
947 if ((old_dir->i_mode & S_ISVTX) &&
948 current->fsuid != old_inode->i_uid &&
949 current->fsuid != old_dir->i_uid && !fsuser())
950 goto end_rename;
951 if (IS_APPEND(old_inode) || IS_IMMUTABLE(old_inode))
952 goto end_rename;
953 new_bh = ext2_find_entry (new_dir, new_name, new_len, &new_de);
954 if (new_bh) {
955 new_inode = __iget (new_dir->i_sb, new_de->inode, 0);
956 if (!new_inode) {
957 brelse (new_bh);
958 new_bh = NULL;
959 }
960 }
961 if (new_inode == old_inode) {
962 retval = 0;
963 goto end_rename;
964 }
965 if (new_inode && S_ISDIR(new_inode->i_mode)) {
966 retval = -EISDIR;
967 if (!S_ISDIR(old_inode->i_mode))
968 goto end_rename;
969 retval = -EINVAL;
970 if (subdir (new_dir, old_inode))
971 goto end_rename;
972 retval = -ENOTEMPTY;
973 if (!empty_dir (new_inode))
974 goto end_rename;
975 retval = -EBUSY;
976 if (new_inode->i_count > 1)
977 goto end_rename;
978 }
979 retval = -EPERM;
980 if (new_inode && (new_dir->i_mode & S_ISVTX) &&
981 current->fsuid != new_inode->i_uid &&
982 current->fsuid != new_dir->i_uid && !fsuser())
983 goto end_rename;
984 if (S_ISDIR(old_inode->i_mode)) {
985 retval = -ENOTDIR;
986 if (new_inode && !S_ISDIR(new_inode->i_mode))
987 goto end_rename;
988 retval = -EINVAL;
989 if (subdir (new_dir, old_inode))
990 goto end_rename;
991 dir_bh = ext2_bread (old_inode, 0, 0, &retval);
992 if (!dir_bh)
993 goto end_rename;
994 if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
995 goto end_rename;
996 retval = -EMLINK;
997 if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)
998 goto end_rename;
999 }
1000 if (!new_bh)
1001 new_bh = ext2_add_entry (new_dir, new_name, new_len, &new_de,
1002 &retval);
1003 if (!new_bh)
1004 goto end_rename;
1005
1006
1007
1008 if (new_inode && (new_de->inode != new_inode->i_ino))
1009 goto try_again;
1010 if (new_de->inode && !new_inode)
1011 goto try_again;
1012 if (old_de->inode != old_inode->i_ino)
1013 goto try_again;
1014
1015
1016
1017 new_de->inode = old_inode->i_ino;
1018 new_dir->i_version = ++event;
1019 dcache_add(new_dir, new_de->name, new_de->name_len, new_de->inode);
1020 retval = ext2_delete_entry (old_de, old_bh);
1021 if (retval == -ENOENT)
1022 goto try_again;
1023 if (retval)
1024 goto end_rename;
1025 old_dir->i_version = ++event;
1026 if (new_inode) {
1027 new_inode->i_nlink--;
1028 new_inode->i_ctime = CURRENT_TIME;
1029 new_inode->i_dirt = 1;
1030 }
1031 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1032 old_dir->i_dirt = 1;
1033 if (dir_bh) {
1034 PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
1035 dcache_add(old_inode, "..", 2, new_dir->i_ino);
1036 mark_buffer_dirty(dir_bh, 1);
1037 old_dir->i_nlink--;
1038 old_dir->i_dirt = 1;
1039 if (new_inode) {
1040 new_inode->i_nlink--;
1041 new_inode->i_dirt = 1;
1042 } else {
1043 new_dir->i_nlink++;
1044 new_dir->i_dirt = 1;
1045 }
1046 }
1047 mark_buffer_dirty(old_bh, 1);
1048 if (IS_SYNC(old_dir)) {
1049 ll_rw_block (WRITE, 1, &old_bh);
1050 wait_on_buffer (old_bh);
1051 }
1052 mark_buffer_dirty(new_bh, 1);
1053 if (IS_SYNC(new_dir)) {
1054 ll_rw_block (WRITE, 1, &new_bh);
1055 wait_on_buffer (new_bh);
1056 }
1057 retval = 0;
1058 end_rename:
1059 brelse (dir_bh);
1060 brelse (old_bh);
1061 brelse (new_bh);
1062 iput (old_inode);
1063 iput (new_inode);
1064 iput (old_dir);
1065 iput (new_dir);
1066 return retval;
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 int ext2_rename (struct inode * old_dir, const char * old_name, int old_len,
1083 struct inode * new_dir, const char * new_name, int new_len)
1084 {
1085 int result;
1086
1087 while (old_dir->i_sb->u.ext2_sb.s_rename_lock)
1088 sleep_on (&old_dir->i_sb->u.ext2_sb.s_rename_wait);
1089 old_dir->i_sb->u.ext2_sb.s_rename_lock = 1;
1090 result = do_ext2_rename (old_dir, old_name, old_len, new_dir,
1091 new_name, new_len);
1092 old_dir->i_sb->u.ext2_sb.s_rename_lock = 0;
1093 wake_up (&old_dir->i_sb->u.ext2_sb.s_rename_wait);
1094 return result;
1095 }