]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ext3/namei.c
[PATCH] setuid core dump
[net-next-2.6.git] / fs / ext3 / namei.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ext3/namei.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
29#include <linux/jbd.h>
30#include <linux/time.h>
31#include <linux/ext3_fs.h>
32#include <linux/ext3_jbd.h>
33#include <linux/fcntl.h>
34#include <linux/stat.h>
35#include <linux/string.h>
36#include <linux/quotaops.h>
37#include <linux/buffer_head.h>
38#include <linux/smp_lock.h>
39#include "xattr.h"
40#include "acl.h"
41
42/*
43 * define how far ahead to read directories while searching them.
44 */
45#define NAMEI_RA_CHUNKS 2
46#define NAMEI_RA_BLOCKS 4
47#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
48#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
49
50static struct buffer_head *ext3_append(handle_t *handle,
51 struct inode *inode,
52 u32 *block, int *err)
53{
54 struct buffer_head *bh;
55
56 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
57
58 if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
59 inode->i_size += inode->i_sb->s_blocksize;
60 EXT3_I(inode)->i_disksize = inode->i_size;
61 ext3_journal_get_write_access(handle,bh);
62 }
63 return bh;
64}
65
66#ifndef assert
67#define assert(test) J_ASSERT(test)
68#endif
69
70#ifndef swap
71#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
72#endif
73
74#ifdef DX_DEBUG
75#define dxtrace(command) command
76#else
77#define dxtrace(command)
78#endif
79
80struct fake_dirent
81{
82 __le32 inode;
83 __le16 rec_len;
84 u8 name_len;
85 u8 file_type;
86};
87
88struct dx_countlimit
89{
90 __le16 limit;
91 __le16 count;
92};
93
94struct dx_entry
95{
96 __le32 hash;
97 __le32 block;
98};
99
100/*
101 * dx_root_info is laid out so that if it should somehow get overlaid by a
102 * dirent the two low bits of the hash version will be zero. Therefore, the
103 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
104 */
105
106struct dx_root
107{
108 struct fake_dirent dot;
109 char dot_name[4];
110 struct fake_dirent dotdot;
111 char dotdot_name[4];
112 struct dx_root_info
113 {
114 __le32 reserved_zero;
115 u8 hash_version;
116 u8 info_length; /* 8 */
117 u8 indirect_levels;
118 u8 unused_flags;
119 }
120 info;
121 struct dx_entry entries[0];
122};
123
124struct dx_node
125{
126 struct fake_dirent fake;
127 struct dx_entry entries[0];
128};
129
130
131struct dx_frame
132{
133 struct buffer_head *bh;
134 struct dx_entry *entries;
135 struct dx_entry *at;
136};
137
138struct dx_map_entry
139{
140 u32 hash;
141 u32 offs;
142};
143
144#ifdef CONFIG_EXT3_INDEX
145static inline unsigned dx_get_block (struct dx_entry *entry);
146static void dx_set_block (struct dx_entry *entry, unsigned value);
147static inline unsigned dx_get_hash (struct dx_entry *entry);
148static void dx_set_hash (struct dx_entry *entry, unsigned value);
149static unsigned dx_get_count (struct dx_entry *entries);
150static unsigned dx_get_limit (struct dx_entry *entries);
151static void dx_set_count (struct dx_entry *entries, unsigned value);
152static void dx_set_limit (struct dx_entry *entries, unsigned value);
153static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
154static unsigned dx_node_limit (struct inode *dir);
155static struct dx_frame *dx_probe(struct dentry *dentry,
156 struct inode *dir,
157 struct dx_hash_info *hinfo,
158 struct dx_frame *frame,
159 int *err);
160static void dx_release (struct dx_frame *frames);
161static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
162 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
163static void dx_sort_map(struct dx_map_entry *map, unsigned count);
164static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
165 struct dx_map_entry *offsets, int count);
166static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
167static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
168static int ext3_htree_next_block(struct inode *dir, __u32 hash,
169 struct dx_frame *frame,
170 struct dx_frame *frames,
171 __u32 *start_hash);
172static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
173 struct ext3_dir_entry_2 **res_dir, int *err);
174static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
175 struct inode *inode);
176
177/*
178 * Future: use high four bits of block for coalesce-on-delete flags
179 * Mask them off for now.
180 */
181
182static inline unsigned dx_get_block (struct dx_entry *entry)
183{
184 return le32_to_cpu(entry->block) & 0x00ffffff;
185}
186
187static inline void dx_set_block (struct dx_entry *entry, unsigned value)
188{
189 entry->block = cpu_to_le32(value);
190}
191
192static inline unsigned dx_get_hash (struct dx_entry *entry)
193{
194 return le32_to_cpu(entry->hash);
195}
196
197static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
198{
199 entry->hash = cpu_to_le32(value);
200}
201
202static inline unsigned dx_get_count (struct dx_entry *entries)
203{
204 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
205}
206
207static inline unsigned dx_get_limit (struct dx_entry *entries)
208{
209 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
210}
211
212static inline void dx_set_count (struct dx_entry *entries, unsigned value)
213{
214 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
215}
216
217static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
218{
219 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
220}
221
222static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
223{
224 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
225 EXT3_DIR_REC_LEN(2) - infosize;
226 return 0? 20: entry_space / sizeof(struct dx_entry);
227}
228
229static inline unsigned dx_node_limit (struct inode *dir)
230{
231 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
232 return 0? 22: entry_space / sizeof(struct dx_entry);
233}
234
235/*
236 * Debug
237 */
238#ifdef DX_DEBUG
239static void dx_show_index (char * label, struct dx_entry *entries)
240{
241 int i, n = dx_get_count (entries);
242 printk("%s index ", label);
243 for (i = 0; i < n; i++)
244 {
245 printk("%x->%u ", i? dx_get_hash(entries + i): 0, dx_get_block(entries + i));
246 }
247 printk("\n");
248}
249
250struct stats
251{
252 unsigned names;
253 unsigned space;
254 unsigned bcount;
255};
256
257static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
258 int size, int show_names)
259{
260 unsigned names = 0, space = 0;
261 char *base = (char *) de;
262 struct dx_hash_info h = *hinfo;
263
264 printk("names: ");
265 while ((char *) de < base + size)
266 {
267 if (de->inode)
268 {
269 if (show_names)
270 {
271 int len = de->name_len;
272 char *name = de->name;
273 while (len--) printk("%c", *name++);
274 ext3fs_dirhash(de->name, de->name_len, &h);
275 printk(":%x.%u ", h.hash,
276 ((char *) de - base));
277 }
278 space += EXT3_DIR_REC_LEN(de->name_len);
279 names++;
280 }
281 de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
282 }
283 printk("(%i)\n", names);
284 return (struct stats) { names, space, 1 };
285}
286
287struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
288 struct dx_entry *entries, int levels)
289{
290 unsigned blocksize = dir->i_sb->s_blocksize;
291 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
292 unsigned bcount = 0;
293 struct buffer_head *bh;
294 int err;
295 printk("%i indexed blocks...\n", count);
296 for (i = 0; i < count; i++, entries++)
297 {
298 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
299 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
300 struct stats stats;
301 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
302 if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
303 stats = levels?
304 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
305 dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
306 names += stats.names;
307 space += stats.space;
308 bcount += stats.bcount;
309 brelse (bh);
310 }
311 if (bcount)
312 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
313 names, space/bcount,(space/bcount)*100/blocksize);
314 return (struct stats) { names, space, bcount};
315}
316#endif /* DX_DEBUG */
317
318/*
319 * Probe for a directory leaf block to search.
320 *
321 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
322 * error in the directory index, and the caller should fall back to
323 * searching the directory normally. The callers of dx_probe **MUST**
324 * check for this error code, and make sure it never gets reflected
325 * back to userspace.
326 */
327static struct dx_frame *
328dx_probe(struct dentry *dentry, struct inode *dir,
329 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
330{
331 unsigned count, indirect;
332 struct dx_entry *at, *entries, *p, *q, *m;
333 struct dx_root *root;
334 struct buffer_head *bh;
335 struct dx_frame *frame = frame_in;
336 u32 hash;
337
338 frame->bh = NULL;
339 if (dentry)
340 dir = dentry->d_parent->d_inode;
341 if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
342 goto fail;
343 root = (struct dx_root *) bh->b_data;
344 if (root->info.hash_version != DX_HASH_TEA &&
345 root->info.hash_version != DX_HASH_HALF_MD4 &&
346 root->info.hash_version != DX_HASH_LEGACY) {
347 ext3_warning(dir->i_sb, __FUNCTION__,
348 "Unrecognised inode hash code %d",
349 root->info.hash_version);
350 brelse(bh);
351 *err = ERR_BAD_DX_DIR;
352 goto fail;
353 }
354 hinfo->hash_version = root->info.hash_version;
355 hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
356 if (dentry)
357 ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
358 hash = hinfo->hash;
359
360 if (root->info.unused_flags & 1) {
361 ext3_warning(dir->i_sb, __FUNCTION__,
362 "Unimplemented inode hash flags: %#06x",
363 root->info.unused_flags);
364 brelse(bh);
365 *err = ERR_BAD_DX_DIR;
366 goto fail;
367 }
368
369 if ((indirect = root->info.indirect_levels) > 1) {
370 ext3_warning(dir->i_sb, __FUNCTION__,
371 "Unimplemented inode hash depth: %#06x",
372 root->info.indirect_levels);
373 brelse(bh);
374 *err = ERR_BAD_DX_DIR;
375 goto fail;
376 }
377
378 entries = (struct dx_entry *) (((char *)&root->info) +
379 root->info.info_length);
380 assert(dx_get_limit(entries) == dx_root_limit(dir,
381 root->info.info_length));
382 dxtrace (printk("Look up %x", hash));
383 while (1)
384 {
385 count = dx_get_count(entries);
386 assert (count && count <= dx_get_limit(entries));
387 p = entries + 1;
388 q = entries + count - 1;
389 while (p <= q)
390 {
391 m = p + (q - p)/2;
392 dxtrace(printk("."));
393 if (dx_get_hash(m) > hash)
394 q = m - 1;
395 else
396 p = m + 1;
397 }
398
399 if (0) // linear search cross check
400 {
401 unsigned n = count - 1;
402 at = entries;
403 while (n--)
404 {
405 dxtrace(printk(","));
406 if (dx_get_hash(++at) > hash)
407 {
408 at--;
409 break;
410 }
411 }
412 assert (at == p - 1);
413 }
414
415 at = p - 1;
416 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
417 frame->bh = bh;
418 frame->entries = entries;
419 frame->at = at;
420 if (!indirect--) return frame;
421 if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
422 goto fail2;
423 at = entries = ((struct dx_node *) bh->b_data)->entries;
424 assert (dx_get_limit(entries) == dx_node_limit (dir));
425 frame++;
426 }
427fail2:
428 while (frame >= frame_in) {
429 brelse(frame->bh);
430 frame--;
431 }
432fail:
433 return NULL;
434}
435
436static void dx_release (struct dx_frame *frames)
437{
438 if (frames[0].bh == NULL)
439 return;
440
441 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
442 brelse(frames[1].bh);
443 brelse(frames[0].bh);
444}
445
446/*
447 * This function increments the frame pointer to search the next leaf
448 * block, and reads in the necessary intervening nodes if the search
449 * should be necessary. Whether or not the search is necessary is
450 * controlled by the hash parameter. If the hash value is even, then
451 * the search is only continued if the next block starts with that
452 * hash value. This is used if we are searching for a specific file.
453 *
454 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
455 *
456 * This function returns 1 if the caller should continue to search,
457 * or 0 if it should not. If there is an error reading one of the
458 * index blocks, it will a negative error code.
459 *
460 * If start_hash is non-null, it will be filled in with the starting
461 * hash of the next page.
462 */
463static int ext3_htree_next_block(struct inode *dir, __u32 hash,
464 struct dx_frame *frame,
465 struct dx_frame *frames,
466 __u32 *start_hash)
467{
468 struct dx_frame *p;
469 struct buffer_head *bh;
470 int err, num_frames = 0;
471 __u32 bhash;
472
473 p = frame;
474 /*
475 * Find the next leaf page by incrementing the frame pointer.
476 * If we run out of entries in the interior node, loop around and
477 * increment pointer in the parent node. When we break out of
478 * this loop, num_frames indicates the number of interior
479 * nodes need to be read.
480 */
481 while (1) {
482 if (++(p->at) < p->entries + dx_get_count(p->entries))
483 break;
484 if (p == frames)
485 return 0;
486 num_frames++;
487 p--;
488 }
489
490 /*
491 * If the hash is 1, then continue only if the next page has a
492 * continuation hash of any value. This is used for readdir
493 * handling. Otherwise, check to see if the hash matches the
494 * desired contiuation hash. If it doesn't, return since
495 * there's no point to read in the successive index pages.
496 */
497 bhash = dx_get_hash(p->at);
498 if (start_hash)
499 *start_hash = bhash;
500 if ((hash & 1) == 0) {
501 if ((bhash & ~1) != hash)
502 return 0;
503 }
504 /*
505 * If the hash is HASH_NB_ALWAYS, we always go to the next
506 * block so no check is necessary
507 */
508 while (num_frames--) {
509 if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
510 0, &err)))
511 return err; /* Failure */
512 p++;
513 brelse (p->bh);
514 p->bh = bh;
515 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
516 }
517 return 1;
518}
519
520
521/*
522 * p is at least 6 bytes before the end of page
523 */
524static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
525{
526 return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
527}
528
529/*
530 * This function fills a red-black tree with information from a
531 * directory block. It returns the number directory entries loaded
532 * into the tree. If there is an error it is returned in err.
533 */
534static int htree_dirblock_to_tree(struct file *dir_file,
535 struct inode *dir, int block,
536 struct dx_hash_info *hinfo,
537 __u32 start_hash, __u32 start_minor_hash)
538{
539 struct buffer_head *bh;
540 struct ext3_dir_entry_2 *de, *top;
541 int err, count = 0;
542
543 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
544 if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
545 return err;
546
547 de = (struct ext3_dir_entry_2 *) bh->b_data;
548 top = (struct ext3_dir_entry_2 *) ((char *) de +
549 dir->i_sb->s_blocksize -
550 EXT3_DIR_REC_LEN(0));
551 for (; de < top; de = ext3_next_entry(de)) {
552 ext3fs_dirhash(de->name, de->name_len, hinfo);
553 if ((hinfo->hash < start_hash) ||
554 ((hinfo->hash == start_hash) &&
555 (hinfo->minor_hash < start_minor_hash)))
556 continue;
557 if (de->inode == 0)
558 continue;
559 if ((err = ext3_htree_store_dirent(dir_file,
560 hinfo->hash, hinfo->minor_hash, de)) != 0) {
561 brelse(bh);
562 return err;
563 }
564 count++;
565 }
566 brelse(bh);
567 return count;
568}
569
570
571/*
572 * This function fills a red-black tree with information from a
573 * directory. We start scanning the directory in hash order, starting
574 * at start_hash and start_minor_hash.
575 *
576 * This function returns the number of entries inserted into the tree,
577 * or a negative error code.
578 */
579int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
580 __u32 start_minor_hash, __u32 *next_hash)
581{
582 struct dx_hash_info hinfo;
583 struct ext3_dir_entry_2 *de;
584 struct dx_frame frames[2], *frame;
585 struct inode *dir;
586 int block, err;
587 int count = 0;
588 int ret;
589 __u32 hashval;
590
591 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
592 start_minor_hash));
593 dir = dir_file->f_dentry->d_inode;
594 if (!(EXT3_I(dir)->i_flags & EXT3_INDEX_FL)) {
595 hinfo.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
596 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
597 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
598 start_hash, start_minor_hash);
599 *next_hash = ~0;
600 return count;
601 }
602 hinfo.hash = start_hash;
603 hinfo.minor_hash = 0;
604 frame = dx_probe(NULL, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
605 if (!frame)
606 return err;
607
608 /* Add '.' and '..' from the htree header */
609 if (!start_hash && !start_minor_hash) {
610 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
611 if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
612 goto errout;
613 count++;
614 }
615 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
616 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
617 de = ext3_next_entry(de);
618 if ((err = ext3_htree_store_dirent(dir_file, 2, 0, de)) != 0)
619 goto errout;
620 count++;
621 }
622
623 while (1) {
624 block = dx_get_block(frame->at);
625 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
626 start_hash, start_minor_hash);
627 if (ret < 0) {
628 err = ret;
629 goto errout;
630 }
631 count += ret;
632 hashval = ~0;
633 ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
634 frame, frames, &hashval);
635 *next_hash = hashval;
636 if (ret < 0) {
637 err = ret;
638 goto errout;
639 }
640 /*
641 * Stop if: (a) there are no more entries, or
642 * (b) we have inserted at least one entry and the
643 * next hash value is not a continuation
644 */
645 if ((ret == 0) ||
646 (count && ((hashval & 1) == 0)))
647 break;
648 }
649 dx_release(frames);
650 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
651 count, *next_hash));
652 return count;
653errout:
654 dx_release(frames);
655 return (err);
656}
657
658
659/*
660 * Directory block splitting, compacting
661 */
662
663static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
664 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
665{
666 int count = 0;
667 char *base = (char *) de;
668 struct dx_hash_info h = *hinfo;
669
670 while ((char *) de < base + size)
671 {
672 if (de->name_len && de->inode) {
673 ext3fs_dirhash(de->name, de->name_len, &h);
674 map_tail--;
675 map_tail->hash = h.hash;
676 map_tail->offs = (u32) ((char *) de - base);
677 count++;
678 cond_resched();
679 }
680 /* XXX: do we need to check rec_len == 0 case? -Chris */
681 de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
682 }
683 return count;
684}
685
686static void dx_sort_map (struct dx_map_entry *map, unsigned count)
687{
688 struct dx_map_entry *p, *q, *top = map + count - 1;
689 int more;
690 /* Combsort until bubble sort doesn't suck */
691 while (count > 2)
692 {
693 count = count*10/13;
694 if (count - 9 < 2) /* 9, 10 -> 11 */
695 count = 11;
696 for (p = top, q = p - count; q >= map; p--, q--)
697 if (p->hash < q->hash)
698 swap(*p, *q);
699 }
700 /* Garden variety bubble sort */
701 do {
702 more = 0;
703 q = top;
704 while (q-- > map)
705 {
706 if (q[1].hash >= q[0].hash)
707 continue;
708 swap(*(q+1), *q);
709 more = 1;
710 }
711 } while(more);
712}
713
714static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
715{
716 struct dx_entry *entries = frame->entries;
717 struct dx_entry *old = frame->at, *new = old + 1;
718 int count = dx_get_count(entries);
719
720 assert(count < dx_get_limit(entries));
721 assert(old < entries + count);
722 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
723 dx_set_hash(new, hash);
724 dx_set_block(new, block);
725 dx_set_count(entries, count + 1);
726}
727#endif
728
729
730static void ext3_update_dx_flag(struct inode *inode)
731{
732 if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
733 EXT3_FEATURE_COMPAT_DIR_INDEX))
734 EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
735}
736
737/*
738 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
739 *
740 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
741 * `de != NULL' is guaranteed by caller.
742 */
743static inline int ext3_match (int len, const char * const name,
744 struct ext3_dir_entry_2 * de)
745{
746 if (len != de->name_len)
747 return 0;
748 if (!de->inode)
749 return 0;
750 return !memcmp(name, de->name, len);
751}
752
753/*
754 * Returns 0 if not found, -1 on failure, and 1 on success
755 */
756static inline int search_dirblock(struct buffer_head * bh,
757 struct inode *dir,
758 struct dentry *dentry,
759 unsigned long offset,
760 struct ext3_dir_entry_2 ** res_dir)
761{
762 struct ext3_dir_entry_2 * de;
763 char * dlimit;
764 int de_len;
765 const char *name = dentry->d_name.name;
766 int namelen = dentry->d_name.len;
767
768 de = (struct ext3_dir_entry_2 *) bh->b_data;
769 dlimit = bh->b_data + dir->i_sb->s_blocksize;
770 while ((char *) de < dlimit) {
771 /* this code is executed quadratically often */
772 /* do minimal checking `by hand' */
773
774 if ((char *) de + namelen <= dlimit &&
775 ext3_match (namelen, name, de)) {
776 /* found a match - just to be sure, do a full check */
777 if (!ext3_check_dir_entry("ext3_find_entry",
778 dir, de, bh, offset))
779 return -1;
780 *res_dir = de;
781 return 1;
782 }
783 /* prevent looping on a bad block */
784 de_len = le16_to_cpu(de->rec_len);
785 if (de_len <= 0)
786 return -1;
787 offset += de_len;
788 de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
789 }
790 return 0;
791}
792
793
794/*
795 * ext3_find_entry()
796 *
797 * finds an entry in the specified directory with the wanted name. It
798 * returns the cache buffer in which the entry was found, and the entry
799 * itself (as a parameter - res_dir). It does NOT read the inode of the
800 * entry - you'll have to do that yourself if you want to.
801 *
802 * The returned buffer_head has ->b_count elevated. The caller is expected
803 * to brelse() it when appropriate.
804 */
805static struct buffer_head * ext3_find_entry (struct dentry *dentry,
806 struct ext3_dir_entry_2 ** res_dir)
807{
808 struct super_block * sb;
809 struct buffer_head * bh_use[NAMEI_RA_SIZE];
810 struct buffer_head * bh, *ret = NULL;
811 unsigned long start, block, b;
812 int ra_max = 0; /* Number of bh's in the readahead
813 buffer, bh_use[] */
814 int ra_ptr = 0; /* Current index into readahead
815 buffer */
816 int num = 0;
817 int nblocks, i, err;
818 struct inode *dir = dentry->d_parent->d_inode;
819 int namelen;
820 const u8 *name;
821 unsigned blocksize;
822
823 *res_dir = NULL;
824 sb = dir->i_sb;
825 blocksize = sb->s_blocksize;
826 namelen = dentry->d_name.len;
827 name = dentry->d_name.name;
828 if (namelen > EXT3_NAME_LEN)
829 return NULL;
830#ifdef CONFIG_EXT3_INDEX
831 if (is_dx(dir)) {
832 bh = ext3_dx_find_entry(dentry, res_dir, &err);
833 /*
834 * On success, or if the error was file not found,
835 * return. Otherwise, fall back to doing a search the
836 * old fashioned way.
837 */
838 if (bh || (err != ERR_BAD_DX_DIR))
839 return bh;
840 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
841 }
842#endif
843 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
844 start = EXT3_I(dir)->i_dir_start_lookup;
845 if (start >= nblocks)
846 start = 0;
847 block = start;
848restart:
849 do {
850 /*
851 * We deal with the read-ahead logic here.
852 */
853 if (ra_ptr >= ra_max) {
854 /* Refill the readahead buffer */
855 ra_ptr = 0;
856 b = block;
857 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
858 /*
859 * Terminate if we reach the end of the
860 * directory and must wrap, or if our
861 * search has finished at this block.
862 */
863 if (b >= nblocks || (num && block == start)) {
864 bh_use[ra_max] = NULL;
865 break;
866 }
867 num++;
868 bh = ext3_getblk(NULL, dir, b++, 0, &err);
869 bh_use[ra_max] = bh;
870 if (bh)
871 ll_rw_block(READ, 1, &bh);
872 }
873 }
874 if ((bh = bh_use[ra_ptr++]) == NULL)
875 goto next;
876 wait_on_buffer(bh);
877 if (!buffer_uptodate(bh)) {
878 /* read error, skip block & hope for the best */
879 ext3_error(sb, __FUNCTION__, "reading directory #%lu "
880 "offset %lu", dir->i_ino, block);
881 brelse(bh);
882 goto next;
883 }
884 i = search_dirblock(bh, dir, dentry,
885 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
886 if (i == 1) {
887 EXT3_I(dir)->i_dir_start_lookup = block;
888 ret = bh;
889 goto cleanup_and_exit;
890 } else {
891 brelse(bh);
892 if (i < 0)
893 goto cleanup_and_exit;
894 }
895 next:
896 if (++block >= nblocks)
897 block = 0;
898 } while (block != start);
899
900 /*
901 * If the directory has grown while we were searching, then
902 * search the last part of the directory before giving up.
903 */
904 block = nblocks;
905 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
906 if (block < nblocks) {
907 start = 0;
908 goto restart;
909 }
910
911cleanup_and_exit:
912 /* Clean up the read-ahead blocks */
913 for (; ra_ptr < ra_max; ra_ptr++)
914 brelse (bh_use[ra_ptr]);
915 return ret;
916}
917
918#ifdef CONFIG_EXT3_INDEX
919static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
920 struct ext3_dir_entry_2 **res_dir, int *err)
921{
922 struct super_block * sb;
923 struct dx_hash_info hinfo;
924 u32 hash;
925 struct dx_frame frames[2], *frame;
926 struct ext3_dir_entry_2 *de, *top;
927 struct buffer_head *bh;
928 unsigned long block;
929 int retval;
930 int namelen = dentry->d_name.len;
931 const u8 *name = dentry->d_name.name;
932 struct inode *dir = dentry->d_parent->d_inode;
933
934 sb = dir->i_sb;
935 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
936 return NULL;
937 hash = hinfo.hash;
938 do {
939 block = dx_get_block(frame->at);
940 if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
941 goto errout;
942 de = (struct ext3_dir_entry_2 *) bh->b_data;
943 top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
944 EXT3_DIR_REC_LEN(0));
945 for (; de < top; de = ext3_next_entry(de))
946 if (ext3_match (namelen, name, de)) {
947 if (!ext3_check_dir_entry("ext3_find_entry",
948 dir, de, bh,
949 (block<<EXT3_BLOCK_SIZE_BITS(sb))
950 +((char *)de - bh->b_data))) {
951 brelse (bh);
952 goto errout;
953 }
954 *res_dir = de;
955 dx_release (frames);
956 return bh;
957 }
958 brelse (bh);
959 /* Check to see if we should continue to search */
960 retval = ext3_htree_next_block(dir, hash, frame,
961 frames, NULL);
962 if (retval < 0) {
963 ext3_warning(sb, __FUNCTION__,
964 "error reading index page in directory #%lu",
965 dir->i_ino);
966 *err = retval;
967 goto errout;
968 }
969 } while (retval == 1);
970
971 *err = -ENOENT;
972errout:
973 dxtrace(printk("%s not found\n", name));
974 dx_release (frames);
975 return NULL;
976}
977#endif
978
979static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
980{
981 struct inode * inode;
982 struct ext3_dir_entry_2 * de;
983 struct buffer_head * bh;
984
985 if (dentry->d_name.len > EXT3_NAME_LEN)
986 return ERR_PTR(-ENAMETOOLONG);
987
988 bh = ext3_find_entry(dentry, &de);
989 inode = NULL;
990 if (bh) {
991 unsigned long ino = le32_to_cpu(de->inode);
992 brelse (bh);
993 inode = iget(dir->i_sb, ino);
994
995 if (!inode)
996 return ERR_PTR(-EACCES);
997 }
998 if (inode)
999 return d_splice_alias(inode, dentry);
1000 d_add(dentry, inode);
1001 return NULL;
1002}
1003
1004
1005struct dentry *ext3_get_parent(struct dentry *child)
1006{
1007 unsigned long ino;
1008 struct dentry *parent;
1009 struct inode *inode;
1010 struct dentry dotdot;
1011 struct ext3_dir_entry_2 * de;
1012 struct buffer_head *bh;
1013
1014 dotdot.d_name.name = "..";
1015 dotdot.d_name.len = 2;
1016 dotdot.d_parent = child; /* confusing, isn't it! */
1017
1018 bh = ext3_find_entry(&dotdot, &de);
1019 inode = NULL;
1020 if (!bh)
1021 return ERR_PTR(-ENOENT);
1022 ino = le32_to_cpu(de->inode);
1023 brelse(bh);
1024 inode = iget(child->d_inode->i_sb, ino);
1025
1026 if (!inode)
1027 return ERR_PTR(-EACCES);
1028
1029 parent = d_alloc_anon(inode);
1030 if (!parent) {
1031 iput(inode);
1032 parent = ERR_PTR(-ENOMEM);
1033 }
1034 return parent;
1035}
1036
1037#define S_SHIFT 12
1038static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
1039 [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE,
1040 [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR,
1041 [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV,
1042 [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV,
1043 [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO,
1044 [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK,
1045 [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK,
1046};
1047
1048static inline void ext3_set_de_type(struct super_block *sb,
1049 struct ext3_dir_entry_2 *de,
1050 umode_t mode) {
1051 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
1052 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1053}
1054
1055#ifdef CONFIG_EXT3_INDEX
1056static struct ext3_dir_entry_2 *
1057dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1058{
1059 unsigned rec_len = 0;
1060
1061 while (count--) {
1062 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1063 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1064 memcpy (to, de, rec_len);
1065 ((struct ext3_dir_entry_2 *) to)->rec_len =
1066 cpu_to_le16(rec_len);
1067 de->inode = 0;
1068 map++;
1069 to += rec_len;
1070 }
1071 return (struct ext3_dir_entry_2 *) (to - rec_len);
1072}
1073
1074static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1075{
1076 struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1077 unsigned rec_len = 0;
1078
1079 prev = to = de;
1080 while ((char*)de < base + size) {
1081 next = (struct ext3_dir_entry_2 *) ((char *) de +
1082 le16_to_cpu(de->rec_len));
1083 if (de->inode && de->name_len) {
1084 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1085 if (de > to)
1086 memmove(to, de, rec_len);
1087 to->rec_len = cpu_to_le16(rec_len);
1088 prev = to;
1089 to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
1090 }
1091 de = next;
1092 }
1093 return prev;
1094}
1095
1096static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1097 struct buffer_head **bh,struct dx_frame *frame,
1098 struct dx_hash_info *hinfo, int *error)
1099{
1100 unsigned blocksize = dir->i_sb->s_blocksize;
1101 unsigned count, continued;
1102 struct buffer_head *bh2;
1103 u32 newblock;
1104 u32 hash2;
1105 struct dx_map_entry *map;
1106 char *data1 = (*bh)->b_data, *data2;
1107 unsigned split;
1108 struct ext3_dir_entry_2 *de = NULL, *de2;
1109 int err;
1110
1111 bh2 = ext3_append (handle, dir, &newblock, error);
1112 if (!(bh2)) {
1113 brelse(*bh);
1114 *bh = NULL;
1115 goto errout;
1116 }
1117
1118 BUFFER_TRACE(*bh, "get_write_access");
1119 err = ext3_journal_get_write_access(handle, *bh);
1120 if (err) {
1121 journal_error:
1122 brelse(*bh);
1123 brelse(bh2);
1124 *bh = NULL;
1125 ext3_std_error(dir->i_sb, err);
1126 goto errout;
1127 }
1128 BUFFER_TRACE(frame->bh, "get_write_access");
1129 err = ext3_journal_get_write_access(handle, frame->bh);
1130 if (err)
1131 goto journal_error;
1132
1133 data2 = bh2->b_data;
1134
1135 /* create map in the end of data2 block */
1136 map = (struct dx_map_entry *) (data2 + blocksize);
1137 count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1138 blocksize, hinfo, map);
1139 map -= count;
1140 split = count/2; // need to adjust to actual middle
1141 dx_sort_map (map, count);
1142 hash2 = map[split].hash;
1143 continued = hash2 == map[split - 1].hash;
1144 dxtrace(printk("Split block %i at %x, %i/%i\n",
1145 dx_get_block(frame->at), hash2, split, count-split));
1146
1147 /* Fancy dance to stay within two buffers */
1148 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1149 de = dx_pack_dirents(data1,blocksize);
1150 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1151 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1152 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1153 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1154
1155 /* Which block gets the new entry? */
1156 if (hinfo->hash >= hash2)
1157 {
1158 swap(*bh, bh2);
1159 de = de2;
1160 }
1161 dx_insert_block (frame, hash2 + continued, newblock);
1162 err = ext3_journal_dirty_metadata (handle, bh2);
1163 if (err)
1164 goto journal_error;
1165 err = ext3_journal_dirty_metadata (handle, frame->bh);
1166 if (err)
1167 goto journal_error;
1168 brelse (bh2);
1169 dxtrace(dx_show_index ("frame", frame->entries));
1170errout:
1171 return de;
1172}
1173#endif
1174
1175
1176/*
1177 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1178 * it points to a directory entry which is guaranteed to be large
1179 * enough for new directory entry. If de is NULL, then
1180 * add_dirent_to_buf will attempt search the directory block for
1181 * space. It will return -ENOSPC if no space is available, and -EIO
1182 * and -EEXIST if directory entry already exists.
1183 *
1184 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1185 * all other cases bh is released.
1186 */
1187static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1188 struct inode *inode, struct ext3_dir_entry_2 *de,
1189 struct buffer_head * bh)
1190{
1191 struct inode *dir = dentry->d_parent->d_inode;
1192 const char *name = dentry->d_name.name;
1193 int namelen = dentry->d_name.len;
1194 unsigned long offset = 0;
1195 unsigned short reclen;
1196 int nlen, rlen, err;
1197 char *top;
1198
1199 reclen = EXT3_DIR_REC_LEN(namelen);
1200 if (!de) {
1201 de = (struct ext3_dir_entry_2 *)bh->b_data;
1202 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1203 while ((char *) de <= top) {
1204 if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1205 bh, offset)) {
1206 brelse (bh);
1207 return -EIO;
1208 }
1209 if (ext3_match (namelen, name, de)) {
1210 brelse (bh);
1211 return -EEXIST;
1212 }
1213 nlen = EXT3_DIR_REC_LEN(de->name_len);
1214 rlen = le16_to_cpu(de->rec_len);
1215 if ((de->inode? rlen - nlen: rlen) >= reclen)
1216 break;
1217 de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1218 offset += rlen;
1219 }
1220 if ((char *) de > top)
1221 return -ENOSPC;
1222 }
1223 BUFFER_TRACE(bh, "get_write_access");
1224 err = ext3_journal_get_write_access(handle, bh);
1225 if (err) {
1226 ext3_std_error(dir->i_sb, err);
1227 brelse(bh);
1228 return err;
1229 }
1230
1231 /* By now the buffer is marked for journaling */
1232 nlen = EXT3_DIR_REC_LEN(de->name_len);
1233 rlen = le16_to_cpu(de->rec_len);
1234 if (de->inode) {
1235 struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1236 de1->rec_len = cpu_to_le16(rlen - nlen);
1237 de->rec_len = cpu_to_le16(nlen);
1238 de = de1;
1239 }
1240 de->file_type = EXT3_FT_UNKNOWN;
1241 if (inode) {
1242 de->inode = cpu_to_le32(inode->i_ino);
1243 ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1244 } else
1245 de->inode = 0;
1246 de->name_len = namelen;
1247 memcpy (de->name, name, namelen);
1248 /*
1249 * XXX shouldn't update any times until successful
1250 * completion of syscall, but too many callers depend
1251 * on this.
1252 *
1253 * XXX similarly, too many callers depend on
1254 * ext3_new_inode() setting the times, but error
1255 * recovery deletes the inode, so the worst that can
1256 * happen is that the times are slightly out of date
1257 * and/or different from the directory change time.
1258 */
1259 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
1260 ext3_update_dx_flag(dir);
1261 dir->i_version++;
1262 ext3_mark_inode_dirty(handle, dir);
1263 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1264 err = ext3_journal_dirty_metadata(handle, bh);
1265 if (err)
1266 ext3_std_error(dir->i_sb, err);
1267 brelse(bh);
1268 return 0;
1269}
1270
1271#ifdef CONFIG_EXT3_INDEX
1272/*
1273 * This converts a one block unindexed directory to a 3 block indexed
1274 * directory, and adds the dentry to the indexed directory.
1275 */
1276static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1277 struct inode *inode, struct buffer_head *bh)
1278{
1279 struct inode *dir = dentry->d_parent->d_inode;
1280 const char *name = dentry->d_name.name;
1281 int namelen = dentry->d_name.len;
1282 struct buffer_head *bh2;
1283 struct dx_root *root;
1284 struct dx_frame frames[2], *frame;
1285 struct dx_entry *entries;
1286 struct ext3_dir_entry_2 *de, *de2;
1287 char *data1, *top;
1288 unsigned len;
1289 int retval;
1290 unsigned blocksize;
1291 struct dx_hash_info hinfo;
1292 u32 block;
1293 struct fake_dirent *fde;
1294
1295 blocksize = dir->i_sb->s_blocksize;
1296 dxtrace(printk("Creating index\n"));
1297 retval = ext3_journal_get_write_access(handle, bh);
1298 if (retval) {
1299 ext3_std_error(dir->i_sb, retval);
1300 brelse(bh);
1301 return retval;
1302 }
1303 root = (struct dx_root *) bh->b_data;
1304
1305 bh2 = ext3_append (handle, dir, &block, &retval);
1306 if (!(bh2)) {
1307 brelse(bh);
1308 return retval;
1309 }
1310 EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1311 data1 = bh2->b_data;
1312
1313 /* The 0th block becomes the root, move the dirents out */
1314 fde = &root->dotdot;
1315 de = (struct ext3_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
1316 len = ((char *) root) + blocksize - (char *) de;
1317 memcpy (data1, de, len);
1318 de = (struct ext3_dir_entry_2 *) data1;
1319 top = data1 + len;
1320 while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top)
1321 de = de2;
1322 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1323 /* Initialize the root; the dot dirents already exist */
1324 de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1325 de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1326 memset (&root->info, 0, sizeof(root->info));
1327 root->info.info_length = sizeof(root->info);
1328 root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
1329 entries = root->entries;
1330 dx_set_block (entries, 1);
1331 dx_set_count (entries, 1);
1332 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1333
1334 /* Initialize as for dx_probe */
1335 hinfo.hash_version = root->info.hash_version;
1336 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
1337 ext3fs_dirhash(name, namelen, &hinfo);
1338 frame = frames;
1339 frame->entries = entries;
1340 frame->at = entries;
1341 frame->bh = bh;
1342 bh = bh2;
1343 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1344 dx_release (frames);
1345 if (!(de))
1346 return retval;
1347
1348 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1349}
1350#endif
1351
1352/*
1353 * ext3_add_entry()
1354 *
1355 * adds a file entry to the specified directory, using the same
1356 * semantics as ext3_find_entry(). It returns NULL if it failed.
1357 *
1358 * NOTE!! The inode part of 'de' is left at 0 - which means you
1359 * may not sleep between calling this and putting something into
1360 * the entry, as someone else might have used it while you slept.
1361 */
1362static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1363 struct inode *inode)
1364{
1365 struct inode *dir = dentry->d_parent->d_inode;
1366 unsigned long offset;
1367 struct buffer_head * bh;
1368 struct ext3_dir_entry_2 *de;
1369 struct super_block * sb;
1370 int retval;
1371#ifdef CONFIG_EXT3_INDEX
1372 int dx_fallback=0;
1373#endif
1374 unsigned blocksize;
1375 unsigned nlen, rlen;
1376 u32 block, blocks;
1377
1378 sb = dir->i_sb;
1379 blocksize = sb->s_blocksize;
1380 if (!dentry->d_name.len)
1381 return -EINVAL;
1382#ifdef CONFIG_EXT3_INDEX
1383 if (is_dx(dir)) {
1384 retval = ext3_dx_add_entry(handle, dentry, inode);
1385 if (!retval || (retval != ERR_BAD_DX_DIR))
1386 return retval;
1387 EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1388 dx_fallback++;
1389 ext3_mark_inode_dirty(handle, dir);
1390 }
1391#endif
1392 blocks = dir->i_size >> sb->s_blocksize_bits;
1393 for (block = 0, offset = 0; block < blocks; block++) {
1394 bh = ext3_bread(handle, dir, block, 0, &retval);
1395 if(!bh)
1396 return retval;
1397 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1398 if (retval != -ENOSPC)
1399 return retval;
1400
1401#ifdef CONFIG_EXT3_INDEX
1402 if (blocks == 1 && !dx_fallback &&
1403 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1404 return make_indexed_dir(handle, dentry, inode, bh);
1405#endif
1406 brelse(bh);
1407 }
1408 bh = ext3_append(handle, dir, &block, &retval);
1409 if (!bh)
1410 return retval;
1411 de = (struct ext3_dir_entry_2 *) bh->b_data;
1412 de->inode = 0;
1413 de->rec_len = cpu_to_le16(rlen = blocksize);
1414 nlen = 0;
1415 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1416}
1417
1418#ifdef CONFIG_EXT3_INDEX
1419/*
1420 * Returns 0 for success, or a negative error value
1421 */
1422static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1423 struct inode *inode)
1424{
1425 struct dx_frame frames[2], *frame;
1426 struct dx_entry *entries, *at;
1427 struct dx_hash_info hinfo;
1428 struct buffer_head * bh;
1429 struct inode *dir = dentry->d_parent->d_inode;
1430 struct super_block * sb = dir->i_sb;
1431 struct ext3_dir_entry_2 *de;
1432 int err;
1433
1434 frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
1435 if (!frame)
1436 return err;
1437 entries = frame->entries;
1438 at = frame->at;
1439
1440 if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1441 goto cleanup;
1442
1443 BUFFER_TRACE(bh, "get_write_access");
1444 err = ext3_journal_get_write_access(handle, bh);
1445 if (err)
1446 goto journal_error;
1447
1448 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1449 if (err != -ENOSPC) {
1450 bh = NULL;
1451 goto cleanup;
1452 }
1453
1454 /* Block full, should compress but for now just split */
1455 dxtrace(printk("using %u of %u node entries\n",
1456 dx_get_count(entries), dx_get_limit(entries)));
1457 /* Need to split index? */
1458 if (dx_get_count(entries) == dx_get_limit(entries)) {
1459 u32 newblock;
1460 unsigned icount = dx_get_count(entries);
1461 int levels = frame - frames;
1462 struct dx_entry *entries2;
1463 struct dx_node *node2;
1464 struct buffer_head *bh2;
1465
1466 if (levels && (dx_get_count(frames->entries) ==
1467 dx_get_limit(frames->entries))) {
1468 ext3_warning(sb, __FUNCTION__,
1469 "Directory index full!\n");
1470 err = -ENOSPC;
1471 goto cleanup;
1472 }
1473 bh2 = ext3_append (handle, dir, &newblock, &err);
1474 if (!(bh2))
1475 goto cleanup;
1476 node2 = (struct dx_node *)(bh2->b_data);
1477 entries2 = node2->entries;
1478 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1479 node2->fake.inode = 0;
1480 BUFFER_TRACE(frame->bh, "get_write_access");
1481 err = ext3_journal_get_write_access(handle, frame->bh);
1482 if (err)
1483 goto journal_error;
1484 if (levels) {
1485 unsigned icount1 = icount/2, icount2 = icount - icount1;
1486 unsigned hash2 = dx_get_hash(entries + icount1);
1487 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1488
1489 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1490 err = ext3_journal_get_write_access(handle,
1491 frames[0].bh);
1492 if (err)
1493 goto journal_error;
1494
1495 memcpy ((char *) entries2, (char *) (entries + icount1),
1496 icount2 * sizeof(struct dx_entry));
1497 dx_set_count (entries, icount1);
1498 dx_set_count (entries2, icount2);
1499 dx_set_limit (entries2, dx_node_limit(dir));
1500
1501 /* Which index block gets the new entry? */
1502 if (at - entries >= icount1) {
1503 frame->at = at = at - entries - icount1 + entries2;
1504 frame->entries = entries = entries2;
1505 swap(frame->bh, bh2);
1506 }
1507 dx_insert_block (frames + 0, hash2, newblock);
1508 dxtrace(dx_show_index ("node", frames[1].entries));
1509 dxtrace(dx_show_index ("node",
1510 ((struct dx_node *) bh2->b_data)->entries));
1511 err = ext3_journal_dirty_metadata(handle, bh2);
1512 if (err)
1513 goto journal_error;
1514 brelse (bh2);
1515 } else {
1516 dxtrace(printk("Creating second level index...\n"));
1517 memcpy((char *) entries2, (char *) entries,
1518 icount * sizeof(struct dx_entry));
1519 dx_set_limit(entries2, dx_node_limit(dir));
1520
1521 /* Set up root */
1522 dx_set_count(entries, 1);
1523 dx_set_block(entries + 0, newblock);
1524 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1525
1526 /* Add new access path frame */
1527 frame = frames + 1;
1528 frame->at = at = at - entries + entries2;
1529 frame->entries = entries = entries2;
1530 frame->bh = bh2;
1531 err = ext3_journal_get_write_access(handle,
1532 frame->bh);
1533 if (err)
1534 goto journal_error;
1535 }
1536 ext3_journal_dirty_metadata(handle, frames[0].bh);
1537 }
1538 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1539 if (!de)
1540 goto cleanup;
1541 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1542 bh = NULL;
1543 goto cleanup;
1544
1545journal_error:
1546 ext3_std_error(dir->i_sb, err);
1547cleanup:
1548 if (bh)
1549 brelse(bh);
1550 dx_release(frames);
1551 return err;
1552}
1553#endif
1554
1555/*
1556 * ext3_delete_entry deletes a directory entry by merging it with the
1557 * previous entry
1558 */
1559static int ext3_delete_entry (handle_t *handle,
1560 struct inode * dir,
1561 struct ext3_dir_entry_2 * de_del,
1562 struct buffer_head * bh)
1563{
1564 struct ext3_dir_entry_2 * de, * pde;
1565 int i;
1566
1567 i = 0;
1568 pde = NULL;
1569 de = (struct ext3_dir_entry_2 *) bh->b_data;
1570 while (i < bh->b_size) {
1571 if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
1572 return -EIO;
1573 if (de == de_del) {
1574 BUFFER_TRACE(bh, "get_write_access");
1575 ext3_journal_get_write_access(handle, bh);
1576 if (pde)
1577 pde->rec_len =
1578 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1579 le16_to_cpu(de->rec_len));
1580 else
1581 de->inode = 0;
1582 dir->i_version++;
1583 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1584 ext3_journal_dirty_metadata(handle, bh);
1585 return 0;
1586 }
1587 i += le16_to_cpu(de->rec_len);
1588 pde = de;
1589 de = (struct ext3_dir_entry_2 *)
1590 ((char *) de + le16_to_cpu(de->rec_len));
1591 }
1592 return -ENOENT;
1593}
1594
1595/*
1596 * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we
1597 * do not perform it in these functions. We perform it at the call site,
1598 * if it is needed.
1599 */
1600static inline void ext3_inc_count(handle_t *handle, struct inode *inode)
1601{
1602 inode->i_nlink++;
1603}
1604
1605static inline void ext3_dec_count(handle_t *handle, struct inode *inode)
1606{
1607 inode->i_nlink--;
1608}
1609
1610static int ext3_add_nondir(handle_t *handle,
1611 struct dentry *dentry, struct inode *inode)
1612{
1613 int err = ext3_add_entry(handle, dentry, inode);
1614 if (!err) {
1615 ext3_mark_inode_dirty(handle, inode);
1616 d_instantiate(dentry, inode);
1617 return 0;
1618 }
1619 ext3_dec_count(handle, inode);
1620 iput(inode);
1621 return err;
1622}
1623
1624/*
1625 * By the time this is called, we already have created
1626 * the directory cache entry for the new file, but it
1627 * is so far negative - it has no inode.
1628 *
1629 * If the create succeeds, we fill in the inode information
1630 * with d_instantiate().
1631 */
1632static int ext3_create (struct inode * dir, struct dentry * dentry, int mode,
1633 struct nameidata *nd)
1634{
1635 handle_t *handle;
1636 struct inode * inode;
1637 int err, retries = 0;
1638
1639retry:
1640 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1641 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1642 2*EXT3_QUOTA_INIT_BLOCKS);
1643 if (IS_ERR(handle))
1644 return PTR_ERR(handle);
1645
1646 if (IS_DIRSYNC(dir))
1647 handle->h_sync = 1;
1648
1649 inode = ext3_new_inode (handle, dir, mode);
1650 err = PTR_ERR(inode);
1651 if (!IS_ERR(inode)) {
1652 inode->i_op = &ext3_file_inode_operations;
1653 inode->i_fop = &ext3_file_operations;
1654 ext3_set_aops(inode);
1655 err = ext3_add_nondir(handle, dentry, inode);
1656 }
1657 ext3_journal_stop(handle);
1658 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1659 goto retry;
1660 return err;
1661}
1662
1663static int ext3_mknod (struct inode * dir, struct dentry *dentry,
1664 int mode, dev_t rdev)
1665{
1666 handle_t *handle;
1667 struct inode *inode;
1668 int err, retries = 0;
1669
1670 if (!new_valid_dev(rdev))
1671 return -EINVAL;
1672
1673retry:
1674 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1675 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1676 2*EXT3_QUOTA_INIT_BLOCKS);
1677 if (IS_ERR(handle))
1678 return PTR_ERR(handle);
1679
1680 if (IS_DIRSYNC(dir))
1681 handle->h_sync = 1;
1682
1683 inode = ext3_new_inode (handle, dir, mode);
1684 err = PTR_ERR(inode);
1685 if (!IS_ERR(inode)) {
1686 init_special_inode(inode, inode->i_mode, rdev);
1687#ifdef CONFIG_EXT3_FS_XATTR
1688 inode->i_op = &ext3_special_inode_operations;
1689#endif
1690 err = ext3_add_nondir(handle, dentry, inode);
1691 }
1692 ext3_journal_stop(handle);
1693 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1694 goto retry;
1695 return err;
1696}
1697
1698static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
1699{
1700 handle_t *handle;
1701 struct inode * inode;
1702 struct buffer_head * dir_block;
1703 struct ext3_dir_entry_2 * de;
1704 int err, retries = 0;
1705
1706 if (dir->i_nlink >= EXT3_LINK_MAX)
1707 return -EMLINK;
1708
1709retry:
1710 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1711 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1712 2*EXT3_QUOTA_INIT_BLOCKS);
1713 if (IS_ERR(handle))
1714 return PTR_ERR(handle);
1715
1716 if (IS_DIRSYNC(dir))
1717 handle->h_sync = 1;
1718
1719 inode = ext3_new_inode (handle, dir, S_IFDIR | mode);
1720 err = PTR_ERR(inode);
1721 if (IS_ERR(inode))
1722 goto out_stop;
1723
1724 inode->i_op = &ext3_dir_inode_operations;
1725 inode->i_fop = &ext3_dir_operations;
1726 inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1727 dir_block = ext3_bread (handle, inode, 0, 1, &err);
1728 if (!dir_block) {
1729 inode->i_nlink--; /* is this nlink == 0? */
1730 ext3_mark_inode_dirty(handle, inode);
1731 iput (inode);
1732 goto out_stop;
1733 }
1734 BUFFER_TRACE(dir_block, "get_write_access");
1735 ext3_journal_get_write_access(handle, dir_block);
1736 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
1737 de->inode = cpu_to_le32(inode->i_ino);
1738 de->name_len = 1;
1739 de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));
1740 strcpy (de->name, ".");
1741 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1742 de = (struct ext3_dir_entry_2 *)
1743 ((char *) de + le16_to_cpu(de->rec_len));
1744 de->inode = cpu_to_le32(dir->i_ino);
1745 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));
1746 de->name_len = 2;
1747 strcpy (de->name, "..");
1748 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1749 inode->i_nlink = 2;
1750 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
1751 ext3_journal_dirty_metadata(handle, dir_block);
1752 brelse (dir_block);
1753 ext3_mark_inode_dirty(handle, inode);
1754 err = ext3_add_entry (handle, dentry, inode);
1755 if (err) {
1756 inode->i_nlink = 0;
1757 ext3_mark_inode_dirty(handle, inode);
1758 iput (inode);
1759 goto out_stop;
1760 }
1761 dir->i_nlink++;
1762 ext3_update_dx_flag(dir);
1763 ext3_mark_inode_dirty(handle, dir);
1764 d_instantiate(dentry, inode);
1765out_stop:
1766 ext3_journal_stop(handle);
1767 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1768 goto retry;
1769 return err;
1770}
1771
1772/*
1773 * routine to check that the specified directory is empty (for rmdir)
1774 */
1775static int empty_dir (struct inode * inode)
1776{
1777 unsigned long offset;
1778 struct buffer_head * bh;
1779 struct ext3_dir_entry_2 * de, * de1;
1780 struct super_block * sb;
1781 int err = 0;
1782
1783 sb = inode->i_sb;
1784 if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1785 !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
1786 if (err)
1787 ext3_error(inode->i_sb, __FUNCTION__,
1788 "error %d reading directory #%lu offset 0",
1789 err, inode->i_ino);
1790 else
1791 ext3_warning(inode->i_sb, __FUNCTION__,
1792 "bad directory (dir #%lu) - no data block",
1793 inode->i_ino);
1794 return 1;
1795 }
1796 de = (struct ext3_dir_entry_2 *) bh->b_data;
1797 de1 = (struct ext3_dir_entry_2 *)
1798 ((char *) de + le16_to_cpu(de->rec_len));
1799 if (le32_to_cpu(de->inode) != inode->i_ino ||
1800 !le32_to_cpu(de1->inode) ||
1801 strcmp (".", de->name) ||
1802 strcmp ("..", de1->name)) {
1803 ext3_warning (inode->i_sb, "empty_dir",
1804 "bad directory (dir #%lu) - no `.' or `..'",
1805 inode->i_ino);
1806 brelse (bh);
1807 return 1;
1808 }
1809 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
1810 de = (struct ext3_dir_entry_2 *)
1811 ((char *) de1 + le16_to_cpu(de1->rec_len));
1812 while (offset < inode->i_size ) {
1813 if (!bh ||
1814 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1815 err = 0;
1816 brelse (bh);
1817 bh = ext3_bread (NULL, inode,
1818 offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err);
1819 if (!bh) {
1820 if (err)
1821 ext3_error(sb, __FUNCTION__,
1822 "error %d reading directory"
1823 " #%lu offset %lu",
1824 err, inode->i_ino, offset);
1825 offset += sb->s_blocksize;
1826 continue;
1827 }
1828 de = (struct ext3_dir_entry_2 *) bh->b_data;
1829 }
1830 if (!ext3_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1831 de = (struct ext3_dir_entry_2 *)(bh->b_data +
1832 sb->s_blocksize);
1833 offset = (offset | (sb->s_blocksize - 1)) + 1;
1834 continue;
1835 }
1836 if (le32_to_cpu(de->inode)) {
1837 brelse (bh);
1838 return 0;
1839 }
1840 offset += le16_to_cpu(de->rec_len);
1841 de = (struct ext3_dir_entry_2 *)
1842 ((char *) de + le16_to_cpu(de->rec_len));
1843 }
1844 brelse (bh);
1845 return 1;
1846}
1847
1848/* ext3_orphan_add() links an unlinked or truncated inode into a list of
1849 * such inodes, starting at the superblock, in case we crash before the
1850 * file is closed/deleted, or in case the inode truncate spans multiple
1851 * transactions and the last transaction is not recovered after a crash.
1852 *
1853 * At filesystem recovery time, we walk this list deleting unlinked
1854 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1855 */
1856int ext3_orphan_add(handle_t *handle, struct inode *inode)
1857{
1858 struct super_block *sb = inode->i_sb;
1859 struct ext3_iloc iloc;
1860 int err = 0, rc;
1861
1862 lock_super(sb);
1863 if (!list_empty(&EXT3_I(inode)->i_orphan))
1864 goto out_unlock;
1865
1866 /* Orphan handling is only valid for files with data blocks
1867 * being truncated, or files being unlinked. */
1868
1869 /* @@@ FIXME: Observation from aviro:
1870 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1871 * here (on lock_super()), so race with ext3_link() which might bump
1872 * ->i_nlink. For, say it, character device. Not a regular file,
1873 * not a directory, not a symlink and ->i_nlink > 0.
1874 */
1875 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1876 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1877
1878 BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "get_write_access");
1879 err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
1880 if (err)
1881 goto out_unlock;
1882
1883 err = ext3_reserve_inode_write(handle, inode, &iloc);
1884 if (err)
1885 goto out_unlock;
1886
1887 /* Insert this inode at the head of the on-disk orphan list... */
1888 NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
1889 EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1890 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
1891 rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
1892 if (!err)
1893 err = rc;
1894
1895 /* Only add to the head of the in-memory list if all the
1896 * previous operations succeeded. If the orphan_add is going to
1897 * fail (possibly taking the journal offline), we can't risk
1898 * leaving the inode on the orphan list: stray orphan-list
1899 * entries can cause panics at unmount time.
1900 *
1901 * This is safe: on error we're going to ignore the orphan list
1902 * anyway on the next recovery. */
1903 if (!err)
1904 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
1905
1906 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
1907 jbd_debug(4, "orphan inode %ld will point to %d\n",
1908 inode->i_ino, NEXT_ORPHAN(inode));
1909out_unlock:
1910 unlock_super(sb);
1911 ext3_std_error(inode->i_sb, err);
1912 return err;
1913}
1914
1915/*
1916 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1917 * of such inodes stored on disk, because it is finally being cleaned up.
1918 */
1919int ext3_orphan_del(handle_t *handle, struct inode *inode)
1920{
1921 struct list_head *prev;
1922 struct ext3_inode_info *ei = EXT3_I(inode);
1923 struct ext3_sb_info *sbi;
1924 unsigned long ino_next;
1925 struct ext3_iloc iloc;
1926 int err = 0;
1927
1928 lock_super(inode->i_sb);
1929 if (list_empty(&ei->i_orphan)) {
1930 unlock_super(inode->i_sb);
1931 return 0;
1932 }
1933
1934 ino_next = NEXT_ORPHAN(inode);
1935 prev = ei->i_orphan.prev;
1936 sbi = EXT3_SB(inode->i_sb);
1937
1938 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
1939
1940 list_del_init(&ei->i_orphan);
1941
1942 /* If we're on an error path, we may not have a valid
1943 * transaction handle with which to update the orphan list on
1944 * disk, but we still need to remove the inode from the linked
1945 * list in memory. */
1946 if (!handle)
1947 goto out;
1948
1949 err = ext3_reserve_inode_write(handle, inode, &iloc);
1950 if (err)
1951 goto out_err;
1952
1953 if (prev == &sbi->s_orphan) {
1954 jbd_debug(4, "superblock will point to %lu\n", ino_next);
1955 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1956 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1957 if (err)
1958 goto out_brelse;
1959 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
1960 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1961 } else {
1962 struct ext3_iloc iloc2;
1963 struct inode *i_prev =
1964 &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode;
1965
1966 jbd_debug(4, "orphan inode %lu will point to %lu\n",
1967 i_prev->i_ino, ino_next);
1968 err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
1969 if (err)
1970 goto out_brelse;
1971 NEXT_ORPHAN(i_prev) = ino_next;
1972 err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
1973 }
1974 if (err)
1975 goto out_brelse;
1976 NEXT_ORPHAN(inode) = 0;
1977 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
1978
1979out_err:
1980 ext3_std_error(inode->i_sb, err);
1981out:
1982 unlock_super(inode->i_sb);
1983 return err;
1984
1985out_brelse:
1986 brelse(iloc.bh);
1987 goto out_err;
1988}
1989
1990static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
1991{
1992 int retval;
1993 struct inode * inode;
1994 struct buffer_head * bh;
1995 struct ext3_dir_entry_2 * de;
1996 handle_t *handle;
1997
1998 /* Initialize quotas before so that eventual writes go in
1999 * separate transaction */
2000 DQUOT_INIT(dentry->d_inode);
2001 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2002 if (IS_ERR(handle))
2003 return PTR_ERR(handle);
2004
2005 retval = -ENOENT;
2006 bh = ext3_find_entry (dentry, &de);
2007 if (!bh)
2008 goto end_rmdir;
2009
2010 if (IS_DIRSYNC(dir))
2011 handle->h_sync = 1;
2012
2013 inode = dentry->d_inode;
2014
2015 retval = -EIO;
2016 if (le32_to_cpu(de->inode) != inode->i_ino)
2017 goto end_rmdir;
2018
2019 retval = -ENOTEMPTY;
2020 if (!empty_dir (inode))
2021 goto end_rmdir;
2022
2023 retval = ext3_delete_entry(handle, dir, de, bh);
2024 if (retval)
2025 goto end_rmdir;
2026 if (inode->i_nlink != 2)
2027 ext3_warning (inode->i_sb, "ext3_rmdir",
2028 "empty directory has nlink!=2 (%d)",
2029 inode->i_nlink);
2030 inode->i_version++;
2031 inode->i_nlink = 0;
2032 /* There's no need to set i_disksize: the fact that i_nlink is
2033 * zero will ensure that the right thing happens during any
2034 * recovery. */
2035 inode->i_size = 0;
2036 ext3_orphan_add(handle, inode);
2037 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2038 ext3_mark_inode_dirty(handle, inode);
2039 dir->i_nlink--;
2040 ext3_update_dx_flag(dir);
2041 ext3_mark_inode_dirty(handle, dir);
2042
2043end_rmdir:
2044 ext3_journal_stop(handle);
2045 brelse (bh);
2046 return retval;
2047}
2048
2049static int ext3_unlink(struct inode * dir, struct dentry *dentry)
2050{
2051 int retval;
2052 struct inode * inode;
2053 struct buffer_head * bh;
2054 struct ext3_dir_entry_2 * de;
2055 handle_t *handle;
2056
2057 /* Initialize quotas before so that eventual writes go
2058 * in separate transaction */
2059 DQUOT_INIT(dentry->d_inode);
2060 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2061 if (IS_ERR(handle))
2062 return PTR_ERR(handle);
2063
2064 if (IS_DIRSYNC(dir))
2065 handle->h_sync = 1;
2066
2067 retval = -ENOENT;
2068 bh = ext3_find_entry (dentry, &de);
2069 if (!bh)
2070 goto end_unlink;
2071
2072 inode = dentry->d_inode;
2073
2074 retval = -EIO;
2075 if (le32_to_cpu(de->inode) != inode->i_ino)
2076 goto end_unlink;
2077
2078 if (!inode->i_nlink) {
2079 ext3_warning (inode->i_sb, "ext3_unlink",
2080 "Deleting nonexistent file (%lu), %d",
2081 inode->i_ino, inode->i_nlink);
2082 inode->i_nlink = 1;
2083 }
2084 retval = ext3_delete_entry(handle, dir, de, bh);
2085 if (retval)
2086 goto end_unlink;
2087 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2088 ext3_update_dx_flag(dir);
2089 ext3_mark_inode_dirty(handle, dir);
2090 inode->i_nlink--;
2091 if (!inode->i_nlink)
2092 ext3_orphan_add(handle, inode);
2093 inode->i_ctime = dir->i_ctime;
2094 ext3_mark_inode_dirty(handle, inode);
2095 retval = 0;
2096
2097end_unlink:
2098 ext3_journal_stop(handle);
2099 brelse (bh);
2100 return retval;
2101}
2102
2103static int ext3_symlink (struct inode * dir,
2104 struct dentry *dentry, const char * symname)
2105{
2106 handle_t *handle;
2107 struct inode * inode;
2108 int l, err, retries = 0;
2109
2110 l = strlen(symname)+1;
2111 if (l > dir->i_sb->s_blocksize)
2112 return -ENAMETOOLONG;
2113
2114retry:
2115 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2116 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2117 2*EXT3_QUOTA_INIT_BLOCKS);
2118 if (IS_ERR(handle))
2119 return PTR_ERR(handle);
2120
2121 if (IS_DIRSYNC(dir))
2122 handle->h_sync = 1;
2123
2124 inode = ext3_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
2125 err = PTR_ERR(inode);
2126 if (IS_ERR(inode))
2127 goto out_stop;
2128
2129 if (l > sizeof (EXT3_I(inode)->i_data)) {
2130 inode->i_op = &ext3_symlink_inode_operations;
2131 ext3_set_aops(inode);
2132 /*
2133 * page_symlink() calls into ext3_prepare/commit_write.
2134 * We have a transaction open. All is sweetness. It also sets
2135 * i_size in generic_commit_write().
2136 */
2137 err = page_symlink(inode, symname, l);
2138 if (err) {
2139 ext3_dec_count(handle, inode);
2140 ext3_mark_inode_dirty(handle, inode);
2141 iput (inode);
2142 goto out_stop;
2143 }
2144 } else {
2145 inode->i_op = &ext3_fast_symlink_inode_operations;
2146 memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2147 inode->i_size = l-1;
2148 }
2149 EXT3_I(inode)->i_disksize = inode->i_size;
2150 err = ext3_add_nondir(handle, dentry, inode);
2151out_stop:
2152 ext3_journal_stop(handle);
2153 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2154 goto retry;
2155 return err;
2156}
2157
2158static int ext3_link (struct dentry * old_dentry,
2159 struct inode * dir, struct dentry *dentry)
2160{
2161 handle_t *handle;
2162 struct inode *inode = old_dentry->d_inode;
2163 int err, retries = 0;
2164
2165 if (inode->i_nlink >= EXT3_LINK_MAX)
2166 return -EMLINK;
2167
2168retry:
2169 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2170 EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2171 if (IS_ERR(handle))
2172 return PTR_ERR(handle);
2173
2174 if (IS_DIRSYNC(dir))
2175 handle->h_sync = 1;
2176
2177 inode->i_ctime = CURRENT_TIME_SEC;
2178 ext3_inc_count(handle, inode);
2179 atomic_inc(&inode->i_count);
2180
2181 err = ext3_add_nondir(handle, dentry, inode);
2182 ext3_journal_stop(handle);
2183 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2184 goto retry;
2185 return err;
2186}
2187
2188#define PARENT_INO(buffer) \
2189 ((struct ext3_dir_entry_2 *) ((char *) buffer + \
2190 le16_to_cpu(((struct ext3_dir_entry_2 *) buffer)->rec_len)))->inode
2191
2192/*
2193 * Anybody can rename anything with this: the permission checks are left to the
2194 * higher-level routines.
2195 */
2196static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
2197 struct inode * new_dir,struct dentry *new_dentry)
2198{
2199 handle_t *handle;
2200 struct inode * old_inode, * new_inode;
2201 struct buffer_head * old_bh, * new_bh, * dir_bh;
2202 struct ext3_dir_entry_2 * old_de, * new_de;
2203 int retval;
2204
2205 old_bh = new_bh = dir_bh = NULL;
2206
2207 /* Initialize quotas before so that eventual writes go
2208 * in separate transaction */
2209 if (new_dentry->d_inode)
2210 DQUOT_INIT(new_dentry->d_inode);
2211 handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2212 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2213 if (IS_ERR(handle))
2214 return PTR_ERR(handle);
2215
2216 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2217 handle->h_sync = 1;
2218
2219 old_bh = ext3_find_entry (old_dentry, &old_de);
2220 /*
2221 * Check for inode number is _not_ due to possible IO errors.
2222 * We might rmdir the source, keep it as pwd of some process
2223 * and merrily kill the link to whatever was created under the
2224 * same name. Goodbye sticky bit ;-<
2225 */
2226 old_inode = old_dentry->d_inode;
2227 retval = -ENOENT;
2228 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2229 goto end_rename;
2230
2231 new_inode = new_dentry->d_inode;
2232 new_bh = ext3_find_entry (new_dentry, &new_de);
2233 if (new_bh) {
2234 if (!new_inode) {
2235 brelse (new_bh);
2236 new_bh = NULL;
2237 }
2238 }
2239 if (S_ISDIR(old_inode->i_mode)) {
2240 if (new_inode) {
2241 retval = -ENOTEMPTY;
2242 if (!empty_dir (new_inode))
2243 goto end_rename;
2244 }
2245 retval = -EIO;
2246 dir_bh = ext3_bread (handle, old_inode, 0, 0, &retval);
2247 if (!dir_bh)
2248 goto end_rename;
2249 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2250 goto end_rename;
2251 retval = -EMLINK;
2252 if (!new_inode && new_dir!=old_dir &&
2253 new_dir->i_nlink >= EXT3_LINK_MAX)
2254 goto end_rename;
2255 }
2256 if (!new_bh) {
2257 retval = ext3_add_entry (handle, new_dentry, old_inode);
2258 if (retval)
2259 goto end_rename;
2260 } else {
2261 BUFFER_TRACE(new_bh, "get write access");
2262 ext3_journal_get_write_access(handle, new_bh);
2263 new_de->inode = cpu_to_le32(old_inode->i_ino);
2264 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2265 EXT3_FEATURE_INCOMPAT_FILETYPE))
2266 new_de->file_type = old_de->file_type;
2267 new_dir->i_version++;
2268 BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
2269 ext3_journal_dirty_metadata(handle, new_bh);
2270 brelse(new_bh);
2271 new_bh = NULL;
2272 }
2273
2274 /*
2275 * Like most other Unix systems, set the ctime for inodes on a
2276 * rename.
2277 */
2278 old_inode->i_ctime = CURRENT_TIME_SEC;
2279 ext3_mark_inode_dirty(handle, old_inode);
2280
2281 /*
2282 * ok, that's it
2283 */
2284 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2285 old_de->name_len != old_dentry->d_name.len ||
2286 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2287 (retval = ext3_delete_entry(handle, old_dir,
2288 old_de, old_bh)) == -ENOENT) {
2289 /* old_de could have moved from under us during htree split, so
2290 * make sure that we are deleting the right entry. We might
2291 * also be pointing to a stale entry in the unused part of
2292 * old_bh so just checking inum and the name isn't enough. */
2293 struct buffer_head *old_bh2;
2294 struct ext3_dir_entry_2 *old_de2;
2295
2296 old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2297 if (old_bh2) {
2298 retval = ext3_delete_entry(handle, old_dir,
2299 old_de2, old_bh2);
2300 brelse(old_bh2);
2301 }
2302 }
2303 if (retval) {
2304 ext3_warning(old_dir->i_sb, "ext3_rename",
2305 "Deleting old file (%lu), %d, error=%d",
2306 old_dir->i_ino, old_dir->i_nlink, retval);
2307 }
2308
2309 if (new_inode) {
2310 new_inode->i_nlink--;
2311 new_inode->i_ctime = CURRENT_TIME_SEC;
2312 }
2313 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
2314 ext3_update_dx_flag(old_dir);
2315 if (dir_bh) {
2316 BUFFER_TRACE(dir_bh, "get_write_access");
2317 ext3_journal_get_write_access(handle, dir_bh);
2318 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
2319 BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
2320 ext3_journal_dirty_metadata(handle, dir_bh);
2321 old_dir->i_nlink--;
2322 if (new_inode) {
2323 new_inode->i_nlink--;
2324 } else {
2325 new_dir->i_nlink++;
2326 ext3_update_dx_flag(new_dir);
2327 ext3_mark_inode_dirty(handle, new_dir);
2328 }
2329 }
2330 ext3_mark_inode_dirty(handle, old_dir);
2331 if (new_inode) {
2332 ext3_mark_inode_dirty(handle, new_inode);
2333 if (!new_inode->i_nlink)
2334 ext3_orphan_add(handle, new_inode);
2335 }
2336 retval = 0;
2337
2338end_rename:
2339 brelse (dir_bh);
2340 brelse (old_bh);
2341 brelse (new_bh);
2342 ext3_journal_stop(handle);
2343 return retval;
2344}
2345
2346/*
2347 * directories can handle most operations...
2348 */
2349struct inode_operations ext3_dir_inode_operations = {
2350 .create = ext3_create,
2351 .lookup = ext3_lookup,
2352 .link = ext3_link,
2353 .unlink = ext3_unlink,
2354 .symlink = ext3_symlink,
2355 .mkdir = ext3_mkdir,
2356 .rmdir = ext3_rmdir,
2357 .mknod = ext3_mknod,
2358 .rename = ext3_rename,
2359 .setattr = ext3_setattr,
2360#ifdef CONFIG_EXT3_FS_XATTR
2361 .setxattr = generic_setxattr,
2362 .getxattr = generic_getxattr,
2363 .listxattr = ext3_listxattr,
2364 .removexattr = generic_removexattr,
2365#endif
2366 .permission = ext3_permission,
2367};
2368
2369struct inode_operations ext3_special_inode_operations = {
2370 .setattr = ext3_setattr,
2371#ifdef CONFIG_EXT3_FS_XATTR
2372 .setxattr = generic_setxattr,
2373 .getxattr = generic_getxattr,
2374 .listxattr = ext3_listxattr,
2375 .removexattr = generic_removexattr,
2376#endif
2377 .permission = ext3_permission,
2378};