1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
29#include <linux/jbd2.h>
30#include <linux/time.h>
31#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/quotaops.h>
35#include <linux/buffer_head.h>
36#include <linux/bio.h>
37#include "ext4.h"
38#include "ext4_jbd2.h"
39
40#include "xattr.h"
41#include "acl.h"
42
43#include <trace/events/ext4.h>
44
45
46
47#define NAMEI_RA_CHUNKS 2
48#define NAMEI_RA_BLOCKS 4
49#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
50
51static struct buffer_head *ext4_append(handle_t *handle,
52 struct inode *inode,
53 ext4_lblk_t *block)
54{
55 struct buffer_head *bh;
56 int err = 0;
57
58 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
60 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
61 return ERR_PTR(-ENOSPC);
62
63 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
64
65 bh = ext4_bread(handle, inode, *block, 1, &err);
66 if (!bh)
67 return ERR_PTR(err);
68 inode->i_size += inode->i_sb->s_blocksize;
69 EXT4_I(inode)->i_disksize = inode->i_size;
70 BUFFER_TRACE(bh, "get_write_access");
71 err = ext4_journal_get_write_access(handle, bh);
72 if (err) {
73 brelse(bh);
74 ext4_std_error(inode->i_sb, err);
75 return ERR_PTR(err);
76 }
77 return bh;
78}
79
80static int ext4_dx_csum_verify(struct inode *inode,
81 struct ext4_dir_entry *dirent);
82
83typedef enum {
84 EITHER, INDEX, DIRENT
85} dirblock_type_t;
86
87#define ext4_read_dirblock(inode, block, type) \
88 __ext4_read_dirblock((inode), (block), (type), __LINE__)
89
90static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
91 ext4_lblk_t block,
92 dirblock_type_t type,
93 unsigned int line)
94{
95 struct buffer_head *bh;
96 struct ext4_dir_entry *dirent;
97 int err = 0, is_dx_block = 0;
98
99 bh = ext4_bread(NULL, inode, block, 0, &err);
100 if (!bh) {
101 if (err == 0) {
102 ext4_error_inode(inode, __func__, line, block,
103 "Directory hole found");
104 return ERR_PTR(-EIO);
105 }
106 __ext4_warning(inode->i_sb, __func__, line,
107 "error reading directory block "
108 "(ino %lu, block %lu)", inode->i_ino,
109 (unsigned long) block);
110 return ERR_PTR(err);
111 }
112 dirent = (struct ext4_dir_entry *) bh->b_data;
113
114 if (is_dx(inode)) {
115 if (block == 0)
116 is_dx_block = 1;
117 else if (ext4_rec_len_from_disk(dirent->rec_len,
118 inode->i_sb->s_blocksize) ==
119 inode->i_sb->s_blocksize)
120 is_dx_block = 1;
121 }
122 if (!is_dx_block && type == INDEX) {
123 ext4_error_inode(inode, __func__, line, block,
124 "directory leaf block found instead of index block");
125 return ERR_PTR(-EIO);
126 }
127 if (!ext4_has_metadata_csum(inode->i_sb) ||
128 buffer_verified(bh))
129 return bh;
130
131
132
133
134
135
136 if (is_dx_block && type == INDEX) {
137 if (ext4_dx_csum_verify(inode, dirent))
138 set_buffer_verified(bh);
139 else {
140 ext4_error_inode(inode, __func__, line, block,
141 "Directory index failed checksum");
142 brelse(bh);
143 return ERR_PTR(-EIO);
144 }
145 }
146 if (!is_dx_block) {
147 if (ext4_dirent_csum_verify(inode, dirent))
148 set_buffer_verified(bh);
149 else {
150 ext4_error_inode(inode, __func__, line, block,
151 "Directory block failed checksum");
152 brelse(bh);
153 return ERR_PTR(-EIO);
154 }
155 }
156 return bh;
157}
158
159#ifndef assert
160#define assert(test) J_ASSERT(test)
161#endif
162
163#ifdef DX_DEBUG
164#define dxtrace(command) command
165#else
166#define dxtrace(command)
167#endif
168
169struct fake_dirent
170{
171 __le32 inode;
172 __le16 rec_len;
173 u8 name_len;
174 u8 file_type;
175};
176
177struct dx_countlimit
178{
179 __le16 limit;
180 __le16 count;
181};
182
183struct dx_entry
184{
185 __le32 hash;
186 __le32 block;
187};
188
189
190
191
192
193
194
195struct dx_root
196{
197 struct fake_dirent dot;
198 char dot_name[4];
199 struct fake_dirent dotdot;
200 char dotdot_name[4];
201 struct dx_root_info
202 {
203 __le32 reserved_zero;
204 u8 hash_version;
205 u8 info_length;
206 u8 indirect_levels;
207 u8 unused_flags;
208 }
209 info;
210 struct dx_entry entries[0];
211};
212
213struct dx_node
214{
215 struct fake_dirent fake;
216 struct dx_entry entries[0];
217};
218
219
220struct dx_frame
221{
222 struct buffer_head *bh;
223 struct dx_entry *entries;
224 struct dx_entry *at;
225};
226
227struct dx_map_entry
228{
229 u32 hash;
230 u16 offs;
231 u16 size;
232};
233
234
235
236
237struct dx_tail {
238 u32 dt_reserved;
239 __le32 dt_checksum;
240};
241
242static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
243static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
244static inline unsigned dx_get_hash(struct dx_entry *entry);
245static void dx_set_hash(struct dx_entry *entry, unsigned value);
246static unsigned dx_get_count(struct dx_entry *entries);
247static unsigned dx_get_limit(struct dx_entry *entries);
248static void dx_set_count(struct dx_entry *entries, unsigned value);
249static void dx_set_limit(struct dx_entry *entries, unsigned value);
250static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
251static unsigned dx_node_limit(struct inode *dir);
252static struct dx_frame *dx_probe(const struct qstr *d_name,
253 struct inode *dir,
254 struct dx_hash_info *hinfo,
255 struct dx_frame *frame,
256 int *err);
257static void dx_release(struct dx_frame *frames);
258static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
259 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
260static void dx_sort_map(struct dx_map_entry *map, unsigned count);
261static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
262 struct dx_map_entry *offsets, int count, unsigned blocksize);
263static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
264static void dx_insert_block(struct dx_frame *frame,
265 u32 hash, ext4_lblk_t block);
266static int ext4_htree_next_block(struct inode *dir, __u32 hash,
267 struct dx_frame *frame,
268 struct dx_frame *frames,
269 __u32 *start_hash);
270static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
271 const struct qstr *d_name,
272 struct ext4_dir_entry_2 **res_dir,
273 int *err);
274static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
275 struct inode *inode);
276
277
278void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
279 unsigned int blocksize)
280{
281 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
282 t->det_rec_len = ext4_rec_len_to_disk(
283 sizeof(struct ext4_dir_entry_tail), blocksize);
284 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
285}
286
287
288static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
289 struct ext4_dir_entry *de)
290{
291 struct ext4_dir_entry_tail *t;
292
293#ifdef PARANOID
294 struct ext4_dir_entry *d, *top;
295
296 d = de;
297 top = (struct ext4_dir_entry *)(((void *)de) +
298 (EXT4_BLOCK_SIZE(inode->i_sb) -
299 sizeof(struct ext4_dir_entry_tail)));
300 while (d < top && d->rec_len)
301 d = (struct ext4_dir_entry *)(((void *)d) +
302 le16_to_cpu(d->rec_len));
303
304 if (d != top)
305 return NULL;
306
307 t = (struct ext4_dir_entry_tail *)d;
308#else
309 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
310#endif
311
312 if (t->det_reserved_zero1 ||
313 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
314 t->det_reserved_zero2 ||
315 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
316 return NULL;
317
318 return t;
319}
320
321static __le32 ext4_dirent_csum(struct inode *inode,
322 struct ext4_dir_entry *dirent, int size)
323{
324 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
325 struct ext4_inode_info *ei = EXT4_I(inode);
326 __u32 csum;
327
328 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
329 return cpu_to_le32(csum);
330}
331
332static void warn_no_space_for_csum(struct inode *inode)
333{
334 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
335 "checksum. Please run e2fsck -D.", inode->i_ino);
336}
337
338int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
339{
340 struct ext4_dir_entry_tail *t;
341
342 if (!ext4_has_metadata_csum(inode->i_sb))
343 return 1;
344
345 t = get_dirent_tail(inode, dirent);
346 if (!t) {
347 warn_no_space_for_csum(inode);
348 return 0;
349 }
350
351 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
352 (void *)t - (void *)dirent))
353 return 0;
354
355 return 1;
356}
357
358static void ext4_dirent_csum_set(struct inode *inode,
359 struct ext4_dir_entry *dirent)
360{
361 struct ext4_dir_entry_tail *t;
362
363 if (!ext4_has_metadata_csum(inode->i_sb))
364 return;
365
366 t = get_dirent_tail(inode, dirent);
367 if (!t) {
368 warn_no_space_for_csum(inode);
369 return;
370 }
371
372 t->det_checksum = ext4_dirent_csum(inode, dirent,
373 (void *)t - (void *)dirent);
374}
375
376int ext4_handle_dirty_dirent_node(handle_t *handle,
377 struct inode *inode,
378 struct buffer_head *bh)
379{
380 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
381 return ext4_handle_dirty_metadata(handle, inode, bh);
382}
383
384static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
385 struct ext4_dir_entry *dirent,
386 int *offset)
387{
388 struct ext4_dir_entry *dp;
389 struct dx_root_info *root;
390 int count_offset;
391
392 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
393 count_offset = 8;
394 else if (le16_to_cpu(dirent->rec_len) == 12) {
395 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
396 if (le16_to_cpu(dp->rec_len) !=
397 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
398 return NULL;
399 root = (struct dx_root_info *)(((void *)dp + 12));
400 if (root->reserved_zero ||
401 root->info_length != sizeof(struct dx_root_info))
402 return NULL;
403 count_offset = 32;
404 } else
405 return NULL;
406
407 if (offset)
408 *offset = count_offset;
409 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
410}
411
412static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
413 int count_offset, int count, struct dx_tail *t)
414{
415 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
416 struct ext4_inode_info *ei = EXT4_I(inode);
417 __u32 csum;
418 __le32 save_csum;
419 int size;
420
421 size = count_offset + (count * sizeof(struct dx_entry));
422 save_csum = t->dt_checksum;
423 t->dt_checksum = 0;
424 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
425 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
426 t->dt_checksum = save_csum;
427
428 return cpu_to_le32(csum);
429}
430
431static int ext4_dx_csum_verify(struct inode *inode,
432 struct ext4_dir_entry *dirent)
433{
434 struct dx_countlimit *c;
435 struct dx_tail *t;
436 int count_offset, limit, count;
437
438 if (!ext4_has_metadata_csum(inode->i_sb))
439 return 1;
440
441 c = get_dx_countlimit(inode, dirent, &count_offset);
442 if (!c) {
443 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
444 return 1;
445 }
446 limit = le16_to_cpu(c->limit);
447 count = le16_to_cpu(c->count);
448 if (count_offset + (limit * sizeof(struct dx_entry)) >
449 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
450 warn_no_space_for_csum(inode);
451 return 1;
452 }
453 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
454
455 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
456 count, t))
457 return 0;
458 return 1;
459}
460
461static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
462{
463 struct dx_countlimit *c;
464 struct dx_tail *t;
465 int count_offset, limit, count;
466
467 if (!ext4_has_metadata_csum(inode->i_sb))
468 return;
469
470 c = get_dx_countlimit(inode, dirent, &count_offset);
471 if (!c) {
472 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
473 return;
474 }
475 limit = le16_to_cpu(c->limit);
476 count = le16_to_cpu(c->count);
477 if (count_offset + (limit * sizeof(struct dx_entry)) >
478 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
479 warn_no_space_for_csum(inode);
480 return;
481 }
482 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
483
484 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
485}
486
487static inline int ext4_handle_dirty_dx_node(handle_t *handle,
488 struct inode *inode,
489 struct buffer_head *bh)
490{
491 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
492 return ext4_handle_dirty_metadata(handle, inode, bh);
493}
494
495
496
497
498static inline struct ext4_dir_entry_2 *
499ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
500{
501 return (struct ext4_dir_entry_2 *)((char *)p +
502 ext4_rec_len_from_disk(p->rec_len, blocksize));
503}
504
505
506
507
508
509
510static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
511{
512 return le32_to_cpu(entry->block) & 0x00ffffff;
513}
514
515static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
516{
517 entry->block = cpu_to_le32(value);
518}
519
520static inline unsigned dx_get_hash(struct dx_entry *entry)
521{
522 return le32_to_cpu(entry->hash);
523}
524
525static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
526{
527 entry->hash = cpu_to_le32(value);
528}
529
530static inline unsigned dx_get_count(struct dx_entry *entries)
531{
532 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
533}
534
535static inline unsigned dx_get_limit(struct dx_entry *entries)
536{
537 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
538}
539
540static inline void dx_set_count(struct dx_entry *entries, unsigned value)
541{
542 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
543}
544
545static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
546{
547 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
548}
549
550static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
551{
552 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
553 EXT4_DIR_REC_LEN(2) - infosize;
554
555 if (ext4_has_metadata_csum(dir->i_sb))
556 entry_space -= sizeof(struct dx_tail);
557 return entry_space / sizeof(struct dx_entry);
558}
559
560static inline unsigned dx_node_limit(struct inode *dir)
561{
562 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
563
564 if (ext4_has_metadata_csum(dir->i_sb))
565 entry_space -= sizeof(struct dx_tail);
566 return entry_space / sizeof(struct dx_entry);
567}
568
569
570
571
572#ifdef DX_DEBUG
573static void dx_show_index(char * label, struct dx_entry *entries)
574{
575 int i, n = dx_get_count (entries);
576 printk(KERN_DEBUG "%s index ", label);
577 for (i = 0; i < n; i++) {
578 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
579 0, (unsigned long)dx_get_block(entries + i));
580 }
581 printk("\n");
582}
583
584struct stats
585{
586 unsigned names;
587 unsigned space;
588 unsigned bcount;
589};
590
591static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
592 int size, int show_names)
593{
594 unsigned names = 0, space = 0;
595 char *base = (char *) de;
596 struct dx_hash_info h = *hinfo;
597
598 printk("names: ");
599 while ((char *) de < base + size)
600 {
601 if (de->inode)
602 {
603 if (show_names)
604 {
605 int len = de->name_len;
606 char *name = de->name;
607 while (len--) printk("%c", *name++);
608 ext4fs_dirhash(de->name, de->name_len, &h);
609 printk(":%x.%u ", h.hash,
610 (unsigned) ((char *) de - base));
611 }
612 space += EXT4_DIR_REC_LEN(de->name_len);
613 names++;
614 }
615 de = ext4_next_entry(de, size);
616 }
617 printk("(%i)\n", names);
618 return (struct stats) { names, space, 1 };
619}
620
621struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
622 struct dx_entry *entries, int levels)
623{
624 unsigned blocksize = dir->i_sb->s_blocksize;
625 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
626 unsigned bcount = 0;
627 struct buffer_head *bh;
628 int err;
629 printk("%i indexed blocks...\n", count);
630 for (i = 0; i < count; i++, entries++)
631 {
632 ext4_lblk_t block = dx_get_block(entries);
633 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
634 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
635 struct stats stats;
636 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
637 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
638 stats = levels?
639 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
640 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
641 names += stats.names;
642 space += stats.space;
643 bcount += stats.bcount;
644 brelse(bh);
645 }
646 if (bcount)
647 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
648 levels ? "" : " ", names, space/bcount,
649 (space/bcount)*100/blocksize);
650 return (struct stats) { names, space, bcount};
651}
652#endif
653
654
655
656
657
658
659
660
661
662
663static struct dx_frame *
664dx_probe(const struct qstr *d_name, struct inode *dir,
665 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
666{
667 unsigned count, indirect;
668 struct dx_entry *at, *entries, *p, *q, *m;
669 struct dx_root *root;
670 struct buffer_head *bh;
671 struct dx_frame *frame = frame_in;
672 u32 hash;
673
674 frame->bh = NULL;
675 bh = ext4_read_dirblock(dir, 0, INDEX);
676 if (IS_ERR(bh)) {
677 *err = PTR_ERR(bh);
678 goto fail;
679 }
680 root = (struct dx_root *) bh->b_data;
681 if (root->info.hash_version != DX_HASH_TEA &&
682 root->info.hash_version != DX_HASH_HALF_MD4 &&
683 root->info.hash_version != DX_HASH_LEGACY) {
684 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
685 root->info.hash_version);
686 brelse(bh);
687 *err = ERR_BAD_DX_DIR;
688 goto fail;
689 }
690 hinfo->hash_version = root->info.hash_version;
691 if (hinfo->hash_version <= DX_HASH_TEA)
692 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
693 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
694 if (d_name)
695 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
696 hash = hinfo->hash;
697
698 if (root->info.unused_flags & 1) {
699 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
700 root->info.unused_flags);
701 brelse(bh);
702 *err = ERR_BAD_DX_DIR;
703 goto fail;
704 }
705
706 if ((indirect = root->info.indirect_levels) > 1) {
707 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
708 root->info.indirect_levels);
709 brelse(bh);
710 *err = ERR_BAD_DX_DIR;
711 goto fail;
712 }
713
714 entries = (struct dx_entry *) (((char *)&root->info) +
715 root->info.info_length);
716
717 if (dx_get_limit(entries) != dx_root_limit(dir,
718 root->info.info_length)) {
719 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
720 brelse(bh);
721 *err = ERR_BAD_DX_DIR;
722 goto fail;
723 }
724
725 dxtrace(printk("Look up %x", hash));
726 while (1)
727 {
728 count = dx_get_count(entries);
729 if (!count || count > dx_get_limit(entries)) {
730 ext4_warning(dir->i_sb,
731 "dx entry: no count or count > limit");
732 brelse(bh);
733 *err = ERR_BAD_DX_DIR;
734 goto fail2;
735 }
736
737 p = entries + 1;
738 q = entries + count - 1;
739 while (p <= q)
740 {
741 m = p + (q - p)/2;
742 dxtrace(printk("."));
743 if (dx_get_hash(m) > hash)
744 q = m - 1;
745 else
746 p = m + 1;
747 }
748
749 if (0)
750 {
751 unsigned n = count - 1;
752 at = entries;
753 while (n--)
754 {
755 dxtrace(printk(","));
756 if (dx_get_hash(++at) > hash)
757 {
758 at--;
759 break;
760 }
761 }
762 assert (at == p - 1);
763 }
764
765 at = p - 1;
766 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
767 frame->bh = bh;
768 frame->entries = entries;
769 frame->at = at;
770 if (!indirect--) return frame;
771 bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
772 if (IS_ERR(bh)) {
773 *err = PTR_ERR(bh);
774 goto fail2;
775 }
776 entries = ((struct dx_node *) bh->b_data)->entries;
777
778 if (dx_get_limit(entries) != dx_node_limit (dir)) {
779 ext4_warning(dir->i_sb,
780 "dx entry: limit != node limit");
781 brelse(bh);
782 *err = ERR_BAD_DX_DIR;
783 goto fail2;
784 }
785 frame++;
786 frame->bh = NULL;
787 }
788fail2:
789 while (frame >= frame_in) {
790 brelse(frame->bh);
791 frame--;
792 }
793fail:
794 if (*err == ERR_BAD_DX_DIR)
795 ext4_warning(dir->i_sb,
796 "Corrupt dir inode %lu, running e2fsck is "
797 "recommended.", dir->i_ino);
798 return NULL;
799}
800
801static void dx_release (struct dx_frame *frames)
802{
803 if (frames[0].bh == NULL)
804 return;
805
806 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
807 brelse(frames[1].bh);
808 brelse(frames[0].bh);
809}
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828static int ext4_htree_next_block(struct inode *dir, __u32 hash,
829 struct dx_frame *frame,
830 struct dx_frame *frames,
831 __u32 *start_hash)
832{
833 struct dx_frame *p;
834 struct buffer_head *bh;
835 int num_frames = 0;
836 __u32 bhash;
837
838 p = frame;
839
840
841
842
843
844
845
846 while (1) {
847 if (++(p->at) < p->entries + dx_get_count(p->entries))
848 break;
849 if (p == frames)
850 return 0;
851 num_frames++;
852 p--;
853 }
854
855
856
857
858
859
860
861
862 bhash = dx_get_hash(p->at);
863 if (start_hash)
864 *start_hash = bhash;
865 if ((hash & 1) == 0) {
866 if ((bhash & ~1) != hash)
867 return 0;
868 }
869
870
871
872
873 while (num_frames--) {
874 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
875 if (IS_ERR(bh))
876 return PTR_ERR(bh);
877 p++;
878 brelse(p->bh);
879 p->bh = bh;
880 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
881 }
882 return 1;
883}
884
885
886
887
888
889
890
891static int htree_dirblock_to_tree(struct file *dir_file,
892 struct inode *dir, ext4_lblk_t block,
893 struct dx_hash_info *hinfo,
894 __u32 start_hash, __u32 start_minor_hash)
895{
896 struct buffer_head *bh;
897 struct ext4_dir_entry_2 *de, *top;
898 int err = 0, count = 0;
899
900 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
901 (unsigned long)block));
902 bh = ext4_read_dirblock(dir, block, DIRENT);
903 if (IS_ERR(bh))
904 return PTR_ERR(bh);
905
906 de = (struct ext4_dir_entry_2 *) bh->b_data;
907 top = (struct ext4_dir_entry_2 *) ((char *) de +
908 dir->i_sb->s_blocksize -
909 EXT4_DIR_REC_LEN(0));
910 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
911 if (ext4_check_dir_entry(dir, NULL, de, bh,
912 bh->b_data, bh->b_size,
913 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
914 + ((char *)de - bh->b_data))) {
915
916 break;
917 }
918 ext4fs_dirhash(de->name, de->name_len, hinfo);
919 if ((hinfo->hash < start_hash) ||
920 ((hinfo->hash == start_hash) &&
921 (hinfo->minor_hash < start_minor_hash)))
922 continue;
923 if (de->inode == 0)
924 continue;
925 if ((err = ext4_htree_store_dirent(dir_file,
926 hinfo->hash, hinfo->minor_hash, de)) != 0) {
927 brelse(bh);
928 return err;
929 }
930 count++;
931 }
932 brelse(bh);
933 return count;
934}
935
936
937
938
939
940
941
942
943
944
945int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
946 __u32 start_minor_hash, __u32 *next_hash)
947{
948 struct dx_hash_info hinfo;
949 struct ext4_dir_entry_2 *de;
950 struct dx_frame frames[2], *frame;
951 struct inode *dir;
952 ext4_lblk_t block;
953 int count = 0;
954 int ret, err;
955 __u32 hashval;
956
957 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
958 start_hash, start_minor_hash));
959 dir = file_inode(dir_file);
960 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
961 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
962 if (hinfo.hash_version <= DX_HASH_TEA)
963 hinfo.hash_version +=
964 EXT4_SB(dir->i_sb)->s_hash_unsigned;
965 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
966 if (ext4_has_inline_data(dir)) {
967 int has_inline_data = 1;
968 count = htree_inlinedir_to_tree(dir_file, dir, 0,
969 &hinfo, start_hash,
970 start_minor_hash,
971 &has_inline_data);
972 if (has_inline_data) {
973 *next_hash = ~0;
974 return count;
975 }
976 }
977 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
978 start_hash, start_minor_hash);
979 *next_hash = ~0;
980 return count;
981 }
982 hinfo.hash = start_hash;
983 hinfo.minor_hash = 0;
984 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
985 if (!frame)
986 return err;
987
988
989 if (!start_hash && !start_minor_hash) {
990 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
991 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
992 goto errout;
993 count++;
994 }
995 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
996 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
997 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
998 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
999 goto errout;
1000 count++;
1001 }
1002
1003 while (1) {
1004 block = dx_get_block(frame->at);
1005 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1006 start_hash, start_minor_hash);
1007 if (ret < 0) {
1008 err = ret;
1009 goto errout;
1010 }
1011 count += ret;
1012 hashval = ~0;
1013 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1014 frame, frames, &hashval);
1015 *next_hash = hashval;
1016 if (ret < 0) {
1017 err = ret;
1018 goto errout;
1019 }
1020
1021
1022
1023
1024
1025 if ((ret == 0) ||
1026 (count && ((hashval & 1) == 0)))
1027 break;
1028 }
1029 dx_release(frames);
1030 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1031 "next hash: %x\n", count, *next_hash));
1032 return count;
1033errout:
1034 dx_release(frames);
1035 return (err);
1036}
1037
1038static inline int search_dirblock(struct buffer_head *bh,
1039 struct inode *dir,
1040 const struct qstr *d_name,
1041 unsigned int offset,
1042 struct ext4_dir_entry_2 **res_dir)
1043{
1044 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1045 d_name, offset, res_dir);
1046}
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1057 struct dx_hash_info *hinfo,
1058 struct dx_map_entry *map_tail)
1059{
1060 int count = 0;
1061 char *base = (char *) de;
1062 struct dx_hash_info h = *hinfo;
1063
1064 while ((char *) de < base + blocksize) {
1065 if (de->name_len && de->inode) {
1066 ext4fs_dirhash(de->name, de->name_len, &h);
1067 map_tail--;
1068 map_tail->hash = h.hash;
1069 map_tail->offs = ((char *) de - base)>>2;
1070 map_tail->size = le16_to_cpu(de->rec_len);
1071 count++;
1072 cond_resched();
1073 }
1074
1075 de = ext4_next_entry(de, blocksize);
1076 }
1077 return count;
1078}
1079
1080
1081static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1082{
1083 struct dx_map_entry *p, *q, *top = map + count - 1;
1084 int more;
1085
1086 while (count > 2) {
1087 count = count*10/13;
1088 if (count - 9 < 2)
1089 count = 11;
1090 for (p = top, q = p - count; q >= map; p--, q--)
1091 if (p->hash < q->hash)
1092 swap(*p, *q);
1093 }
1094
1095 do {
1096 more = 0;
1097 q = top;
1098 while (q-- > map) {
1099 if (q[1].hash >= q[0].hash)
1100 continue;
1101 swap(*(q+1), *q);
1102 more = 1;
1103 }
1104 } while(more);
1105}
1106
1107static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1108{
1109 struct dx_entry *entries = frame->entries;
1110 struct dx_entry *old = frame->at, *new = old + 1;
1111 int count = dx_get_count(entries);
1112
1113 assert(count < dx_get_limit(entries));
1114 assert(old < entries + count);
1115 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1116 dx_set_hash(new, hash);
1117 dx_set_block(new, block);
1118 dx_set_count(entries, count + 1);
1119}
1120
1121
1122
1123
1124
1125
1126
1127static inline int ext4_match (int len, const char * const name,
1128 struct ext4_dir_entry_2 * de)
1129{
1130 if (len != de->name_len)
1131 return 0;
1132 if (!de->inode)
1133 return 0;
1134 return !memcmp(name, de->name, len);
1135}
1136
1137
1138
1139
1140int search_dir(struct buffer_head *bh,
1141 char *search_buf,
1142 int buf_size,
1143 struct inode *dir,
1144 const struct qstr *d_name,
1145 unsigned int offset,
1146 struct ext4_dir_entry_2 **res_dir)
1147{
1148 struct ext4_dir_entry_2 * de;
1149 char * dlimit;
1150 int de_len;
1151 const char *name = d_name->name;
1152 int namelen = d_name->len;
1153
1154 de = (struct ext4_dir_entry_2 *)search_buf;
1155 dlimit = search_buf + buf_size;
1156 while ((char *) de < dlimit) {
1157
1158
1159
1160 if ((char *) de + namelen <= dlimit &&
1161 ext4_match (namelen, name, de)) {
1162
1163 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1164 bh->b_size, offset))
1165 return -1;
1166 *res_dir = de;
1167 return 1;
1168 }
1169
1170 de_len = ext4_rec_len_from_disk(de->rec_len,
1171 dir->i_sb->s_blocksize);
1172 if (de_len <= 0)
1173 return -1;
1174 offset += de_len;
1175 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1176 }
1177 return 0;
1178}
1179
1180static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1181 struct ext4_dir_entry *de)
1182{
1183 struct super_block *sb = dir->i_sb;
1184
1185 if (!is_dx(dir))
1186 return 0;
1187 if (block == 0)
1188 return 1;
1189 if (de->inode == 0 &&
1190 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1191 sb->s_blocksize)
1192 return 1;
1193 return 0;
1194}
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207static struct buffer_head * ext4_find_entry (struct inode *dir,
1208 const struct qstr *d_name,
1209 struct ext4_dir_entry_2 **res_dir,
1210 int *inlined)
1211{
1212 struct super_block *sb;
1213 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1214 struct buffer_head *bh, *ret = NULL;
1215 ext4_lblk_t start, block, b;
1216 const u8 *name = d_name->name;
1217 int ra_max = 0;
1218
1219 int ra_ptr = 0;
1220
1221 int num = 0;
1222 ext4_lblk_t nblocks;
1223 int i, err = 0;
1224 int namelen;
1225
1226 *res_dir = NULL;
1227 sb = dir->i_sb;
1228 namelen = d_name->len;
1229 if (namelen > EXT4_NAME_LEN)
1230 return NULL;
1231
1232 if (ext4_has_inline_data(dir)) {
1233 int has_inline_data = 1;
1234 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1235 &has_inline_data);
1236 if (has_inline_data) {
1237 if (inlined)
1238 *inlined = 1;
1239 return ret;
1240 }
1241 }
1242
1243 if ((namelen <= 2) && (name[0] == '.') &&
1244 (name[1] == '.' || name[1] == '\0')) {
1245
1246
1247
1248
1249 block = start = 0;
1250 nblocks = 1;
1251 goto restart;
1252 }
1253 if (is_dx(dir)) {
1254 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1255
1256
1257
1258
1259
1260 if (err == -ENOENT)
1261 return NULL;
1262 if (err && err != ERR_BAD_DX_DIR)
1263 return ERR_PTR(err);
1264 if (bh)
1265 return bh;
1266 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1267 "falling back\n"));
1268 }
1269 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1270 start = EXT4_I(dir)->i_dir_start_lookup;
1271 if (start >= nblocks)
1272 start = 0;
1273 block = start;
1274restart:
1275 do {
1276
1277
1278
1279 if (ra_ptr >= ra_max) {
1280
1281 ra_ptr = 0;
1282 b = block;
1283 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1284
1285
1286
1287
1288
1289 if (b >= nblocks || (num && block == start)) {
1290 bh_use[ra_max] = NULL;
1291 break;
1292 }
1293 num++;
1294 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1295 if (unlikely(err)) {
1296 if (ra_max == 0)
1297 return ERR_PTR(err);
1298 break;
1299 }
1300 bh_use[ra_max] = bh;
1301 if (bh)
1302 ll_rw_block(READ | REQ_META | REQ_PRIO,
1303 1, &bh);
1304 }
1305 }
1306 if ((bh = bh_use[ra_ptr++]) == NULL)
1307 goto next;
1308 wait_on_buffer(bh);
1309 if (!buffer_uptodate(bh)) {
1310
1311 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1312 (unsigned long) block);
1313 brelse(bh);
1314 goto next;
1315 }
1316 if (!buffer_verified(bh) &&
1317 !is_dx_internal_node(dir, block,
1318 (struct ext4_dir_entry *)bh->b_data) &&
1319 !ext4_dirent_csum_verify(dir,
1320 (struct ext4_dir_entry *)bh->b_data)) {
1321 EXT4_ERROR_INODE(dir, "checksumming directory "
1322 "block %lu", (unsigned long)block);
1323 brelse(bh);
1324 goto next;
1325 }
1326 set_buffer_verified(bh);
1327 i = search_dirblock(bh, dir, d_name,
1328 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1329 if (i == 1) {
1330 EXT4_I(dir)->i_dir_start_lookup = block;
1331 ret = bh;
1332 goto cleanup_and_exit;
1333 } else {
1334 brelse(bh);
1335 if (i < 0)
1336 goto cleanup_and_exit;
1337 }
1338 next:
1339 if (++block >= nblocks)
1340 block = 0;
1341 } while (block != start);
1342
1343
1344
1345
1346
1347 block = nblocks;
1348 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1349 if (block < nblocks) {
1350 start = 0;
1351 goto restart;
1352 }
1353
1354cleanup_and_exit:
1355
1356 for (; ra_ptr < ra_max; ra_ptr++)
1357 brelse(bh_use[ra_ptr]);
1358 return ret;
1359}
1360
1361static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1362 struct ext4_dir_entry_2 **res_dir, int *err)
1363{
1364 struct super_block * sb = dir->i_sb;
1365 struct dx_hash_info hinfo;
1366 struct dx_frame frames[2], *frame;
1367 struct buffer_head *bh;
1368 ext4_lblk_t block;
1369 int retval;
1370
1371 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1372 return NULL;
1373 do {
1374 block = dx_get_block(frame->at);
1375 bh = ext4_read_dirblock(dir, block, DIRENT);
1376 if (IS_ERR(bh)) {
1377 *err = PTR_ERR(bh);
1378 goto errout;
1379 }
1380 retval = search_dirblock(bh, dir, d_name,
1381 block << EXT4_BLOCK_SIZE_BITS(sb),
1382 res_dir);
1383 if (retval == 1) {
1384 dx_release(frames);
1385 return bh;
1386 }
1387 brelse(bh);
1388 if (retval == -1) {
1389 *err = ERR_BAD_DX_DIR;
1390 goto errout;
1391 }
1392
1393
1394 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1395 frames, NULL);
1396 if (retval < 0) {
1397 ext4_warning(sb,
1398 "error reading index page in directory #%lu",
1399 dir->i_ino);
1400 *err = retval;
1401 goto errout;
1402 }
1403 } while (retval == 1);
1404
1405 *err = -ENOENT;
1406errout:
1407 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1408 dx_release (frames);
1409 return NULL;
1410}
1411
1412static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1413{
1414 struct inode *inode;
1415 struct ext4_dir_entry_2 *de;
1416 struct buffer_head *bh;
1417
1418 if (dentry->d_name.len > EXT4_NAME_LEN)
1419 return ERR_PTR(-ENAMETOOLONG);
1420
1421 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
1422 if (IS_ERR(bh))
1423 return (struct dentry *) bh;
1424 inode = NULL;
1425 if (bh) {
1426 __u32 ino = le32_to_cpu(de->inode);
1427 brelse(bh);
1428 if (!ext4_valid_inum(dir->i_sb, ino)) {
1429 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1430 return ERR_PTR(-EIO);
1431 }
1432 if (unlikely(ino == dir->i_ino)) {
1433 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1434 dentry);
1435 return ERR_PTR(-EIO);
1436 }
1437 inode = ext4_iget_normal(dir->i_sb, ino);
1438 if (inode == ERR_PTR(-ESTALE)) {
1439 EXT4_ERROR_INODE(dir,
1440 "deleted inode referenced: %u",
1441 ino);
1442 return ERR_PTR(-EIO);
1443 }
1444 }
1445 return d_splice_alias(inode, dentry);
1446}
1447
1448
1449struct dentry *ext4_get_parent(struct dentry *child)
1450{
1451 __u32 ino;
1452 static const struct qstr dotdot = QSTR_INIT("..", 2);
1453 struct ext4_dir_entry_2 * de;
1454 struct buffer_head *bh;
1455
1456 bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
1457 if (IS_ERR(bh))
1458 return (struct dentry *) bh;
1459 if (!bh)
1460 return ERR_PTR(-ENOENT);
1461 ino = le32_to_cpu(de->inode);
1462 brelse(bh);
1463
1464 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1465 EXT4_ERROR_INODE(child->d_inode,
1466 "bad parent inode number: %u", ino);
1467 return ERR_PTR(-EIO);
1468 }
1469
1470 return d_obtain_alias(ext4_iget_normal(child->d_inode->i_sb, ino));
1471}
1472
1473
1474
1475
1476
1477static struct ext4_dir_entry_2 *
1478dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1479 unsigned blocksize)
1480{
1481 unsigned rec_len = 0;
1482
1483 while (count--) {
1484 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1485 (from + (map->offs<<2));
1486 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1487 memcpy (to, de, rec_len);
1488 ((struct ext4_dir_entry_2 *) to)->rec_len =
1489 ext4_rec_len_to_disk(rec_len, blocksize);
1490 de->inode = 0;
1491 map++;
1492 to += rec_len;
1493 }
1494 return (struct ext4_dir_entry_2 *) (to - rec_len);
1495}
1496
1497
1498
1499
1500
1501static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1502{
1503 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1504 unsigned rec_len = 0;
1505
1506 prev = to = de;
1507 while ((char*)de < base + blocksize) {
1508 next = ext4_next_entry(de, blocksize);
1509 if (de->inode && de->name_len) {
1510 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1511 if (de > to)
1512 memmove(to, de, rec_len);
1513 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1514 prev = to;
1515 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1516 }
1517 de = next;
1518 }
1519 return prev;
1520}
1521
1522
1523
1524
1525
1526
1527static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1528 struct buffer_head **bh,struct dx_frame *frame,
1529 struct dx_hash_info *hinfo, int *error)
1530{
1531 unsigned blocksize = dir->i_sb->s_blocksize;
1532 unsigned count, continued;
1533 struct buffer_head *bh2;
1534 ext4_lblk_t newblock;
1535 u32 hash2;
1536 struct dx_map_entry *map;
1537 char *data1 = (*bh)->b_data, *data2;
1538 unsigned split, move, size;
1539 struct ext4_dir_entry_2 *de = NULL, *de2;
1540 struct ext4_dir_entry_tail *t;
1541 int csum_size = 0;
1542 int err = 0, i;
1543
1544 if (ext4_has_metadata_csum(dir->i_sb))
1545 csum_size = sizeof(struct ext4_dir_entry_tail);
1546
1547 bh2 = ext4_append(handle, dir, &newblock);
1548 if (IS_ERR(bh2)) {
1549 brelse(*bh);
1550 *bh = NULL;
1551 *error = PTR_ERR(bh2);
1552 return NULL;
1553 }
1554
1555 BUFFER_TRACE(*bh, "get_write_access");
1556 err = ext4_journal_get_write_access(handle, *bh);
1557 if (err)
1558 goto journal_error;
1559
1560 BUFFER_TRACE(frame->bh, "get_write_access");
1561 err = ext4_journal_get_write_access(handle, frame->bh);
1562 if (err)
1563 goto journal_error;
1564
1565 data2 = bh2->b_data;
1566
1567
1568 map = (struct dx_map_entry *) (data2 + blocksize);
1569 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1570 blocksize, hinfo, map);
1571 map -= count;
1572 dx_sort_map(map, count);
1573
1574 size = 0;
1575 move = 0;
1576 for (i = count-1; i >= 0; i--) {
1577
1578 if (size + map[i].size/2 > blocksize/2)
1579 break;
1580 size += map[i].size;
1581 move++;
1582 }
1583
1584 split = count - move;
1585 hash2 = map[split].hash;
1586 continued = hash2 == map[split - 1].hash;
1587 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1588 (unsigned long)dx_get_block(frame->at),
1589 hash2, split, count-split));
1590
1591
1592 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1593 de = dx_pack_dirents(data1, blocksize);
1594 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1595 (char *) de,
1596 blocksize);
1597 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1598 (char *) de2,
1599 blocksize);
1600 if (csum_size) {
1601 t = EXT4_DIRENT_TAIL(data2, blocksize);
1602 initialize_dirent_tail(t, blocksize);
1603
1604 t = EXT4_DIRENT_TAIL(data1, blocksize);
1605 initialize_dirent_tail(t, blocksize);
1606 }
1607
1608 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1609 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1610
1611
1612 if (hinfo->hash >= hash2)
1613 {
1614 swap(*bh, bh2);
1615 de = de2;
1616 }
1617 dx_insert_block(frame, hash2 + continued, newblock);
1618 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1619 if (err)
1620 goto journal_error;
1621 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1622 if (err)
1623 goto journal_error;
1624 brelse(bh2);
1625 dxtrace(dx_show_index("frame", frame->entries));
1626 return de;
1627
1628journal_error:
1629 brelse(*bh);
1630 brelse(bh2);
1631 *bh = NULL;
1632 ext4_std_error(dir->i_sb, err);
1633 *error = err;
1634 return NULL;
1635}
1636
1637int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1638 struct buffer_head *bh,
1639 void *buf, int buf_size,
1640 const char *name, int namelen,
1641 struct ext4_dir_entry_2 **dest_de)
1642{
1643 struct ext4_dir_entry_2 *de;
1644 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1645 int nlen, rlen;
1646 unsigned int offset = 0;
1647 char *top;
1648
1649 de = (struct ext4_dir_entry_2 *)buf;
1650 top = buf + buf_size - reclen;
1651 while ((char *) de <= top) {
1652 if (ext4_check_dir_entry(dir, NULL, de, bh,
1653 buf, buf_size, offset))
1654 return -EIO;
1655 if (ext4_match(namelen, name, de))
1656 return -EEXIST;
1657 nlen = EXT4_DIR_REC_LEN(de->name_len);
1658 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1659 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1660 break;
1661 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1662 offset += rlen;
1663 }
1664 if ((char *) de > top)
1665 return -ENOSPC;
1666
1667 *dest_de = de;
1668 return 0;
1669}
1670
1671void ext4_insert_dentry(struct inode *inode,
1672 struct ext4_dir_entry_2 *de,
1673 int buf_size,
1674 const char *name, int namelen)
1675{
1676
1677 int nlen, rlen;
1678
1679 nlen = EXT4_DIR_REC_LEN(de->name_len);
1680 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1681 if (de->inode) {
1682 struct ext4_dir_entry_2 *de1 =
1683 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1684 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1685 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1686 de = de1;
1687 }
1688 de->file_type = EXT4_FT_UNKNOWN;
1689 de->inode = cpu_to_le32(inode->i_ino);
1690 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1691 de->name_len = namelen;
1692 memcpy(de->name, name, namelen);
1693}
1694
1695
1696
1697
1698
1699
1700
1701
1702static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1703 struct inode *inode, struct ext4_dir_entry_2 *de,
1704 struct buffer_head *bh)
1705{
1706 struct inode *dir = dentry->d_parent->d_inode;
1707 const char *name = dentry->d_name.name;
1708 int namelen = dentry->d_name.len;
1709 unsigned int blocksize = dir->i_sb->s_blocksize;
1710 int csum_size = 0;
1711 int err;
1712
1713 if (ext4_has_metadata_csum(inode->i_sb))
1714 csum_size = sizeof(struct ext4_dir_entry_tail);
1715
1716 if (!de) {
1717 err = ext4_find_dest_de(dir, inode,
1718 bh, bh->b_data, blocksize - csum_size,
1719 name, namelen, &de);
1720 if (err)
1721 return err;
1722 }
1723 BUFFER_TRACE(bh, "get_write_access");
1724 err = ext4_journal_get_write_access(handle, bh);
1725 if (err) {
1726 ext4_std_error(dir->i_sb, err);
1727 return err;
1728 }
1729
1730
1731 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1745 ext4_update_dx_flag(dir);
1746 dir->i_version++;
1747 ext4_mark_inode_dirty(handle, dir);
1748 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1749 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1750 if (err)
1751 ext4_std_error(dir->i_sb, err);
1752 return 0;
1753}
1754
1755
1756
1757
1758
1759static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1760 struct inode *inode, struct buffer_head *bh)
1761{
1762 struct inode *dir = dentry->d_parent->d_inode;
1763 const char *name = dentry->d_name.name;
1764 int namelen = dentry->d_name.len;
1765 struct buffer_head *bh2;
1766 struct dx_root *root;
1767 struct dx_frame frames[2], *frame;
1768 struct dx_entry *entries;
1769 struct ext4_dir_entry_2 *de, *de2;
1770 struct ext4_dir_entry_tail *t;
1771 char *data1, *top;
1772 unsigned len;
1773 int retval;
1774 unsigned blocksize;
1775 struct dx_hash_info hinfo;
1776 ext4_lblk_t block;
1777 struct fake_dirent *fde;
1778 int csum_size = 0;
1779
1780 if (ext4_has_metadata_csum(inode->i_sb))
1781 csum_size = sizeof(struct ext4_dir_entry_tail);
1782
1783 blocksize = dir->i_sb->s_blocksize;
1784 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1785 BUFFER_TRACE(bh, "get_write_access");
1786 retval = ext4_journal_get_write_access(handle, bh);
1787 if (retval) {
1788 ext4_std_error(dir->i_sb, retval);
1789 brelse(bh);
1790 return retval;
1791 }
1792 root = (struct dx_root *) bh->b_data;
1793
1794
1795 fde = &root->dotdot;
1796 de = (struct ext4_dir_entry_2 *)((char *)fde +
1797 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1798 if ((char *) de >= (((char *) root) + blocksize)) {
1799 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1800 brelse(bh);
1801 return -EIO;
1802 }
1803 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1804
1805
1806 bh2 = ext4_append(handle, dir, &block);
1807 if (IS_ERR(bh2)) {
1808 brelse(bh);
1809 return PTR_ERR(bh2);
1810 }
1811 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1812 data1 = bh2->b_data;
1813
1814 memcpy (data1, de, len);
1815 de = (struct ext4_dir_entry_2 *) data1;
1816 top = data1 + len;
1817 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1818 de = de2;
1819 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1820 (char *) de,
1821 blocksize);
1822
1823 if (csum_size) {
1824 t = EXT4_DIRENT_TAIL(data1, blocksize);
1825 initialize_dirent_tail(t, blocksize);
1826 }
1827
1828
1829 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1830 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1831 blocksize);
1832 memset (&root->info, 0, sizeof(root->info));
1833 root->info.info_length = sizeof(root->info);
1834 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1835 entries = root->entries;
1836 dx_set_block(entries, 1);
1837 dx_set_count(entries, 1);
1838 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1839
1840
1841 hinfo.hash_version = root->info.hash_version;
1842 if (hinfo.hash_version <= DX_HASH_TEA)
1843 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1844 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1845 ext4fs_dirhash(name, namelen, &hinfo);
1846 memset(frames, 0, sizeof(frames));
1847 frame = frames;
1848 frame->entries = entries;
1849 frame->at = entries;
1850 frame->bh = bh;
1851
1852 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1853 if (retval)
1854 goto out_frames;
1855 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1856 if (retval)
1857 goto out_frames;
1858
1859 de = do_split(handle,dir, &bh2, frame, &hinfo, &retval);
1860 if (!de) {
1861 goto out_frames;
1862 }
1863
1864 retval = add_dirent_to_buf(handle, dentry, inode, de, bh2);
1865out_frames:
1866
1867
1868
1869
1870
1871 if (retval)
1872 ext4_mark_inode_dirty(handle, dir);
1873 dx_release(frames);
1874 brelse(bh2);
1875 return retval;
1876}
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1889 struct inode *inode)
1890{
1891 struct inode *dir = dentry->d_parent->d_inode;
1892 struct buffer_head *bh = NULL;
1893 struct ext4_dir_entry_2 *de;
1894 struct ext4_dir_entry_tail *t;
1895 struct super_block *sb;
1896 int retval;
1897 int dx_fallback=0;
1898 unsigned blocksize;
1899 ext4_lblk_t block, blocks;
1900 int csum_size = 0;
1901
1902 if (ext4_has_metadata_csum(inode->i_sb))
1903 csum_size = sizeof(struct ext4_dir_entry_tail);
1904
1905 sb = dir->i_sb;
1906 blocksize = sb->s_blocksize;
1907 if (!dentry->d_name.len)
1908 return -EINVAL;
1909
1910 if (ext4_has_inline_data(dir)) {
1911 retval = ext4_try_add_inline_entry(handle, dentry, inode);
1912 if (retval < 0)
1913 return retval;
1914 if (retval == 1) {
1915 retval = 0;
1916 goto out;
1917 }
1918 }
1919
1920 if (is_dx(dir)) {
1921 retval = ext4_dx_add_entry(handle, dentry, inode);
1922 if (!retval || (retval != ERR_BAD_DX_DIR))
1923 goto out;
1924 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1925 dx_fallback++;
1926 ext4_mark_inode_dirty(handle, dir);
1927 }
1928 blocks = dir->i_size >> sb->s_blocksize_bits;
1929 for (block = 0; block < blocks; block++) {
1930 bh = ext4_read_dirblock(dir, block, DIRENT);
1931 if (IS_ERR(bh))
1932 return PTR_ERR(bh);
1933
1934 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1935 if (retval != -ENOSPC)
1936 goto out;
1937
1938 if (blocks == 1 && !dx_fallback &&
1939 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
1940 retval = make_indexed_dir(handle, dentry, inode, bh);
1941 bh = NULL;
1942 goto out;
1943 }
1944 brelse(bh);
1945 }
1946 bh = ext4_append(handle, dir, &block);
1947 if (IS_ERR(bh))
1948 return PTR_ERR(bh);
1949 de = (struct ext4_dir_entry_2 *) bh->b_data;
1950 de->inode = 0;
1951 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1952
1953 if (csum_size) {
1954 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1955 initialize_dirent_tail(t, blocksize);
1956 }
1957
1958 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1959out:
1960 brelse(bh);
1961 if (retval == 0)
1962 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1963 return retval;
1964}
1965
1966
1967
1968
1969static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1970 struct inode *inode)
1971{
1972 struct dx_frame frames[2], *frame;
1973 struct dx_entry *entries, *at;
1974 struct dx_hash_info hinfo;
1975 struct buffer_head *bh;
1976 struct inode *dir = dentry->d_parent->d_inode;
1977 struct super_block *sb = dir->i_sb;
1978 struct ext4_dir_entry_2 *de;
1979 int err;
1980
1981 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1982 if (!frame)
1983 return err;
1984 entries = frame->entries;
1985 at = frame->at;
1986 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
1987 if (IS_ERR(bh)) {
1988 err = PTR_ERR(bh);
1989 bh = NULL;
1990 goto cleanup;
1991 }
1992
1993 BUFFER_TRACE(bh, "get_write_access");
1994 err = ext4_journal_get_write_access(handle, bh);
1995 if (err)
1996 goto journal_error;
1997
1998 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1999 if (err != -ENOSPC)
2000 goto cleanup;
2001
2002
2003 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2004 dx_get_count(entries), dx_get_limit(entries)));
2005
2006 if (dx_get_count(entries) == dx_get_limit(entries)) {
2007 ext4_lblk_t newblock;
2008 unsigned icount = dx_get_count(entries);
2009 int levels = frame - frames;
2010 struct dx_entry *entries2;
2011 struct dx_node *node2;
2012 struct buffer_head *bh2;
2013
2014 if (levels && (dx_get_count(frames->entries) ==
2015 dx_get_limit(frames->entries))) {
2016 ext4_warning(sb, "Directory index full!");
2017 err = -ENOSPC;
2018 goto cleanup;
2019 }
2020 bh2 = ext4_append(handle, dir, &newblock);
2021 if (IS_ERR(bh2)) {
2022 err = PTR_ERR(bh2);
2023 goto cleanup;
2024 }
2025 node2 = (struct dx_node *)(bh2->b_data);
2026 entries2 = node2->entries;
2027 memset(&node2->fake, 0, sizeof(struct fake_dirent));
2028 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2029 sb->s_blocksize);
2030 BUFFER_TRACE(frame->bh, "get_write_access");
2031 err = ext4_journal_get_write_access(handle, frame->bh);
2032 if (err)
2033 goto journal_error;
2034 if (levels) {
2035 unsigned icount1 = icount/2, icount2 = icount - icount1;
2036 unsigned hash2 = dx_get_hash(entries + icount1);
2037 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2038 icount1, icount2));
2039
2040 BUFFER_TRACE(frame->bh, "get_write_access");
2041 err = ext4_journal_get_write_access(handle,
2042 frames[0].bh);
2043 if (err)
2044 goto journal_error;
2045
2046 memcpy((char *) entries2, (char *) (entries + icount1),
2047 icount2 * sizeof(struct dx_entry));
2048 dx_set_count(entries, icount1);
2049 dx_set_count(entries2, icount2);
2050 dx_set_limit(entries2, dx_node_limit(dir));
2051
2052
2053 if (at - entries >= icount1) {
2054 frame->at = at = at - entries - icount1 + entries2;
2055 frame->entries = entries = entries2;
2056 swap(frame->bh, bh2);
2057 }
2058 dx_insert_block(frames + 0, hash2, newblock);
2059 dxtrace(dx_show_index("node", frames[1].entries));
2060 dxtrace(dx_show_index("node",
2061 ((struct dx_node *) bh2->b_data)->entries));
2062 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2063 if (err)
2064 goto journal_error;
2065 brelse (bh2);
2066 } else {
2067 dxtrace(printk(KERN_DEBUG
2068 "Creating second level index...\n"));
2069 memcpy((char *) entries2, (char *) entries,
2070 icount * sizeof(struct dx_entry));
2071 dx_set_limit(entries2, dx_node_limit(dir));
2072
2073
2074 dx_set_count(entries, 1);
2075 dx_set_block(entries + 0, newblock);
2076 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2077
2078
2079 frame = frames + 1;
2080 frame->at = at = at - entries + entries2;
2081 frame->entries = entries = entries2;
2082 frame->bh = bh2;
2083 err = ext4_journal_get_write_access(handle,
2084 frame->bh);
2085 if (err)
2086 goto journal_error;
2087 }
2088 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2089 if (err) {
2090 ext4_std_error(inode->i_sb, err);
2091 goto cleanup;
2092 }
2093 }
2094 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2095 if (!de)
2096 goto cleanup;
2097 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2098 goto cleanup;
2099
2100journal_error:
2101 ext4_std_error(dir->i_sb, err);
2102cleanup:
2103 brelse(bh);
2104 dx_release(frames);
2105 return err;
2106}
2107
2108
2109
2110
2111
2112int ext4_generic_delete_entry(handle_t *handle,
2113 struct inode *dir,
2114 struct ext4_dir_entry_2 *de_del,
2115 struct buffer_head *bh,
2116 void *entry_buf,
2117 int buf_size,
2118 int csum_size)
2119{
2120 struct ext4_dir_entry_2 *de, *pde;
2121 unsigned int blocksize = dir->i_sb->s_blocksize;
2122 int i;
2123
2124 i = 0;
2125 pde = NULL;
2126 de = (struct ext4_dir_entry_2 *)entry_buf;
2127 while (i < buf_size - csum_size) {
2128 if (ext4_check_dir_entry(dir, NULL, de, bh,
2129 bh->b_data, bh->b_size, i))
2130 return -EIO;
2131 if (de == de_del) {
2132 if (pde)
2133 pde->rec_len = ext4_rec_len_to_disk(
2134 ext4_rec_len_from_disk(pde->rec_len,
2135 blocksize) +
2136 ext4_rec_len_from_disk(de->rec_len,
2137 blocksize),
2138 blocksize);
2139 else
2140 de->inode = 0;
2141 dir->i_version++;
2142 return 0;
2143 }
2144 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2145 pde = de;
2146 de = ext4_next_entry(de, blocksize);
2147 }
2148 return -ENOENT;
2149}
2150
2151static int ext4_delete_entry(handle_t *handle,
2152 struct inode *dir,
2153 struct ext4_dir_entry_2 *de_del,
2154 struct buffer_head *bh)
2155{
2156 int err, csum_size = 0;
2157
2158 if (ext4_has_inline_data(dir)) {
2159 int has_inline_data = 1;
2160 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2161 &has_inline_data);
2162 if (has_inline_data)
2163 return err;
2164 }
2165
2166 if (ext4_has_metadata_csum(dir->i_sb))
2167 csum_size = sizeof(struct ext4_dir_entry_tail);
2168
2169 BUFFER_TRACE(bh, "get_write_access");
2170 err = ext4_journal_get_write_access(handle, bh);
2171 if (unlikely(err))
2172 goto out;
2173
2174 err = ext4_generic_delete_entry(handle, dir, de_del,
2175 bh, bh->b_data,
2176 dir->i_sb->s_blocksize, csum_size);
2177 if (err)
2178 goto out;
2179
2180 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2181 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2182 if (unlikely(err))
2183 goto out;
2184
2185 return 0;
2186out:
2187 if (err != -ENOENT)
2188 ext4_std_error(dir->i_sb, err);
2189 return err;
2190}
2191
2192
2193
2194
2195
2196static void ext4_inc_count(handle_t *handle, struct inode *inode)
2197{
2198 inc_nlink(inode);
2199 if (is_dx(inode) && inode->i_nlink > 1) {
2200
2201 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2202 set_nlink(inode, 1);
2203 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2204 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2205 }
2206 }
2207}
2208
2209
2210
2211
2212
2213static void ext4_dec_count(handle_t *handle, struct inode *inode)
2214{
2215 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2216 drop_nlink(inode);
2217}
2218
2219
2220static int ext4_add_nondir(handle_t *handle,
2221 struct dentry *dentry, struct inode *inode)
2222{
2223 int err = ext4_add_entry(handle, dentry, inode);
2224 if (!err) {
2225 ext4_mark_inode_dirty(handle, inode);
2226 unlock_new_inode(inode);
2227 d_instantiate(dentry, inode);
2228 return 0;
2229 }
2230 drop_nlink(inode);
2231 unlock_new_inode(inode);
2232 iput(inode);
2233 return err;
2234}
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2245 bool excl)
2246{
2247 handle_t *handle;
2248 struct inode *inode;
2249 int err, credits, retries = 0;
2250
2251 dquot_initialize(dir);
2252
2253 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2254 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2255retry:
2256 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2257 NULL, EXT4_HT_DIR, credits);
2258 handle = ext4_journal_current_handle();
2259 err = PTR_ERR(inode);
2260 if (!IS_ERR(inode)) {
2261 inode->i_op = &ext4_file_inode_operations;
2262 inode->i_fop = &ext4_file_operations;
2263 ext4_set_aops(inode);
2264 err = ext4_add_nondir(handle, dentry, inode);
2265 if (!err && IS_DIRSYNC(dir))
2266 ext4_handle_sync(handle);
2267 }
2268 if (handle)
2269 ext4_journal_stop(handle);
2270 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2271 goto retry;
2272 return err;
2273}
2274
2275static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2276 umode_t mode, dev_t rdev)
2277{
2278 handle_t *handle;
2279 struct inode *inode;
2280 int err, credits, retries = 0;
2281
2282 if (!new_valid_dev(rdev))
2283 return -EINVAL;
2284
2285 dquot_initialize(dir);
2286
2287 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2288 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2289retry:
2290 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2291 NULL, EXT4_HT_DIR, credits);
2292 handle = ext4_journal_current_handle();
2293 err = PTR_ERR(inode);
2294 if (!IS_ERR(inode)) {
2295 init_special_inode(inode, inode->i_mode, rdev);
2296 inode->i_op = &ext4_special_inode_operations;
2297 err = ext4_add_nondir(handle, dentry, inode);
2298 if (!err && IS_DIRSYNC(dir))
2299 ext4_handle_sync(handle);
2300 }
2301 if (handle)
2302 ext4_journal_stop(handle);
2303 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2304 goto retry;
2305 return err;
2306}
2307
2308struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2309 struct ext4_dir_entry_2 *de,
2310 int blocksize, int csum_size,
2311 unsigned int parent_ino, int dotdot_real_len)
2312{
2313 de->inode = cpu_to_le32(inode->i_ino);
2314 de->name_len = 1;
2315 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2316 blocksize);
2317 strcpy(de->name, ".");
2318 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2319
2320 de = ext4_next_entry(de, blocksize);
2321 de->inode = cpu_to_le32(parent_ino);
2322 de->name_len = 2;
2323 if (!dotdot_real_len)
2324 de->rec_len = ext4_rec_len_to_disk(blocksize -
2325 (csum_size + EXT4_DIR_REC_LEN(1)),
2326 blocksize);
2327 else
2328 de->rec_len = ext4_rec_len_to_disk(
2329 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2330 strcpy(de->name, "..");
2331 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2332
2333 return ext4_next_entry(de, blocksize);
2334}
2335
2336static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2337 struct inode *inode)
2338{
2339 struct buffer_head *dir_block = NULL;
2340 struct ext4_dir_entry_2 *de;
2341 struct ext4_dir_entry_tail *t;
2342 ext4_lblk_t block = 0;
2343 unsigned int blocksize = dir->i_sb->s_blocksize;
2344 int csum_size = 0;
2345 int err;
2346
2347 if (ext4_has_metadata_csum(dir->i_sb))
2348 csum_size = sizeof(struct ext4_dir_entry_tail);
2349
2350 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2351 err = ext4_try_create_inline_dir(handle, dir, inode);
2352 if (err < 0 && err != -ENOSPC)
2353 goto out;
2354 if (!err)
2355 goto out;
2356 }
2357
2358 inode->i_size = 0;
2359 dir_block = ext4_append(handle, inode, &block);
2360 if (IS_ERR(dir_block))
2361 return PTR_ERR(dir_block);
2362 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2363 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2364 set_nlink(inode, 2);
2365 if (csum_size) {
2366 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2367 initialize_dirent_tail(t, blocksize);
2368 }
2369
2370 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2371 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2372 if (err)
2373 goto out;
2374 set_buffer_verified(dir_block);
2375out:
2376 brelse(dir_block);
2377 return err;
2378}
2379
2380static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2381{
2382 handle_t *handle;
2383 struct inode *inode;
2384 int err, credits, retries = 0;
2385
2386 if (EXT4_DIR_LINK_MAX(dir))
2387 return -EMLINK;
2388
2389 dquot_initialize(dir);
2390
2391 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2392 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2393retry:
2394 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2395 &dentry->d_name,
2396 0, NULL, EXT4_HT_DIR, credits);
2397 handle = ext4_journal_current_handle();
2398 err = PTR_ERR(inode);
2399 if (IS_ERR(inode))
2400 goto out_stop;
2401
2402 inode->i_op = &ext4_dir_inode_operations.ops;
2403 inode->i_fop = &ext4_dir_operations;
2404 inode->i_flags |= S_IOPS_WRAPPER;
2405 err = ext4_init_new_dir(handle, dir, inode);
2406 if (err)
2407 goto out_clear_inode;
2408 err = ext4_mark_inode_dirty(handle, inode);
2409 if (!err)
2410 err = ext4_add_entry(handle, dentry, inode);
2411 if (err) {
2412out_clear_inode:
2413 clear_nlink(inode);
2414 unlock_new_inode(inode);
2415 ext4_mark_inode_dirty(handle, inode);
2416 iput(inode);
2417 goto out_stop;
2418 }
2419 ext4_inc_count(handle, dir);
2420 ext4_update_dx_flag(dir);
2421 err = ext4_mark_inode_dirty(handle, dir);
2422 if (err)
2423 goto out_clear_inode;
2424 unlock_new_inode(inode);
2425 d_instantiate(dentry, inode);
2426 if (IS_DIRSYNC(dir))
2427 ext4_handle_sync(handle);
2428
2429out_stop:
2430 if (handle)
2431 ext4_journal_stop(handle);
2432 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2433 goto retry;
2434 return err;
2435}
2436
2437
2438
2439
2440static int empty_dir(struct inode *inode)
2441{
2442 unsigned int offset;
2443 struct buffer_head *bh;
2444 struct ext4_dir_entry_2 *de, *de1;
2445 struct super_block *sb;
2446 int err = 0;
2447
2448 if (ext4_has_inline_data(inode)) {
2449 int has_inline_data = 1;
2450
2451 err = empty_inline_dir(inode, &has_inline_data);
2452 if (has_inline_data)
2453 return err;
2454 }
2455
2456 sb = inode->i_sb;
2457 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2458 EXT4_ERROR_INODE(inode, "invalid size");
2459 return 1;
2460 }
2461 bh = ext4_read_dirblock(inode, 0, EITHER);
2462 if (IS_ERR(bh))
2463 return 1;
2464
2465 de = (struct ext4_dir_entry_2 *) bh->b_data;
2466 de1 = ext4_next_entry(de, sb->s_blocksize);
2467 if (le32_to_cpu(de->inode) != inode->i_ino ||
2468 !le32_to_cpu(de1->inode) ||
2469 strcmp(".", de->name) ||
2470 strcmp("..", de1->name)) {
2471 ext4_warning(inode->i_sb,
2472 "bad directory (dir #%lu) - no `.' or `..'",
2473 inode->i_ino);
2474 brelse(bh);
2475 return 1;
2476 }
2477 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2478 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2479 de = ext4_next_entry(de1, sb->s_blocksize);
2480 while (offset < inode->i_size) {
2481 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2482 unsigned int lblock;
2483 err = 0;
2484 brelse(bh);
2485 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2486 bh = ext4_read_dirblock(inode, lblock, EITHER);
2487 if (IS_ERR(bh))
2488 return 1;
2489 de = (struct ext4_dir_entry_2 *) bh->b_data;
2490 }
2491 if (ext4_check_dir_entry(inode, NULL, de, bh,
2492 bh->b_data, bh->b_size, offset)) {
2493 de = (struct ext4_dir_entry_2 *)(bh->b_data +
2494 sb->s_blocksize);
2495 offset = (offset | (sb->s_blocksize - 1)) + 1;
2496 continue;
2497 }
2498 if (le32_to_cpu(de->inode)) {
2499 brelse(bh);
2500 return 0;
2501 }
2502 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2503 de = ext4_next_entry(de, sb->s_blocksize);
2504 }
2505 brelse(bh);
2506 return 1;
2507}
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521int ext4_orphan_add(handle_t *handle, struct inode *inode)
2522{
2523 struct super_block *sb = inode->i_sb;
2524 struct ext4_sb_info *sbi = EXT4_SB(sb);
2525 struct ext4_iloc iloc;
2526 int err = 0, rc;
2527 bool dirty = false;
2528
2529 if (!sbi->s_journal || is_bad_inode(inode))
2530 return 0;
2531
2532 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2533 !mutex_is_locked(&inode->i_mutex));
2534
2535
2536
2537
2538 if (!list_empty(&EXT4_I(inode)->i_orphan))
2539 return 0;
2540
2541
2542
2543
2544
2545
2546
2547 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2548 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2549
2550 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2551 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2552 if (err)
2553 goto out;
2554
2555 err = ext4_reserve_inode_write(handle, inode, &iloc);
2556 if (err)
2557 goto out;
2558
2559 mutex_lock(&sbi->s_orphan_lock);
2560
2561
2562
2563
2564 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2565 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2566
2567 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2568 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2569 dirty = true;
2570 }
2571 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2572 mutex_unlock(&sbi->s_orphan_lock);
2573
2574 if (dirty) {
2575 err = ext4_handle_dirty_super(handle, sb);
2576 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2577 if (!err)
2578 err = rc;
2579 if (err) {
2580
2581
2582
2583
2584
2585 mutex_lock(&sbi->s_orphan_lock);
2586 list_del_init(&EXT4_I(inode)->i_orphan);
2587 mutex_unlock(&sbi->s_orphan_lock);
2588 }
2589 }
2590 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2591 jbd_debug(4, "orphan inode %lu will point to %d\n",
2592 inode->i_ino, NEXT_ORPHAN(inode));
2593out:
2594 ext4_std_error(sb, err);
2595 return err;
2596}
2597
2598
2599
2600
2601
2602int ext4_orphan_del(handle_t *handle, struct inode *inode)
2603{
2604 struct list_head *prev;
2605 struct ext4_inode_info *ei = EXT4_I(inode);
2606 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2607 __u32 ino_next;
2608 struct ext4_iloc iloc;
2609 int err = 0;
2610
2611 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
2612 return 0;
2613
2614 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2615 !mutex_is_locked(&inode->i_mutex));
2616
2617 if (list_empty(&ei->i_orphan))
2618 return 0;
2619
2620 if (handle) {
2621
2622 err = ext4_reserve_inode_write(handle, inode, &iloc);
2623 }
2624
2625 mutex_lock(&sbi->s_orphan_lock);
2626 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2627
2628 prev = ei->i_orphan.prev;
2629 list_del_init(&ei->i_orphan);
2630
2631
2632
2633
2634
2635 if (!handle || err) {
2636 mutex_unlock(&sbi->s_orphan_lock);
2637 goto out_err;
2638 }
2639
2640 ino_next = NEXT_ORPHAN(inode);
2641 if (prev == &sbi->s_orphan) {
2642 jbd_debug(4, "superblock will point to %u\n", ino_next);
2643 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2644 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2645 if (err) {
2646 mutex_unlock(&sbi->s_orphan_lock);
2647 goto out_brelse;
2648 }
2649 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2650 mutex_unlock(&sbi->s_orphan_lock);
2651 err = ext4_handle_dirty_super(handle, inode->i_sb);
2652 } else {
2653 struct ext4_iloc iloc2;
2654 struct inode *i_prev =
2655 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2656
2657 jbd_debug(4, "orphan inode %lu will point to %u\n",
2658 i_prev->i_ino, ino_next);
2659 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2660 if (err) {
2661 mutex_unlock(&sbi->s_orphan_lock);
2662 goto out_brelse;
2663 }
2664 NEXT_ORPHAN(i_prev) = ino_next;
2665 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2666 mutex_unlock(&sbi->s_orphan_lock);
2667 }
2668 if (err)
2669 goto out_brelse;
2670 NEXT_ORPHAN(inode) = 0;
2671 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2672out_err:
2673 ext4_std_error(inode->i_sb, err);
2674 return err;
2675
2676out_brelse:
2677 brelse(iloc.bh);
2678 goto out_err;
2679}
2680
2681static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2682{
2683 int retval;
2684 struct inode *inode;
2685 struct buffer_head *bh;
2686 struct ext4_dir_entry_2 *de;
2687 handle_t *handle = NULL;
2688
2689
2690
2691 dquot_initialize(dir);
2692 dquot_initialize(dentry->d_inode);
2693
2694 retval = -ENOENT;
2695 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2696 if (IS_ERR(bh))
2697 return PTR_ERR(bh);
2698 if (!bh)
2699 goto end_rmdir;
2700
2701 inode = dentry->d_inode;
2702
2703 retval = -EIO;
2704 if (le32_to_cpu(de->inode) != inode->i_ino)
2705 goto end_rmdir;
2706
2707 retval = -ENOTEMPTY;
2708 if (!empty_dir(inode))
2709 goto end_rmdir;
2710
2711 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2712 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
2713 if (IS_ERR(handle)) {
2714 retval = PTR_ERR(handle);
2715 handle = NULL;
2716 goto end_rmdir;
2717 }
2718
2719 if (IS_DIRSYNC(dir))
2720 ext4_handle_sync(handle);
2721
2722 retval = ext4_delete_entry(handle, dir, de, bh);
2723 if (retval)
2724 goto end_rmdir;
2725 if (!EXT4_DIR_LINK_EMPTY(inode))
2726 ext4_warning(inode->i_sb,
2727 "empty directory has too many links (%d)",
2728 inode->i_nlink);
2729 inode->i_version++;
2730 clear_nlink(inode);
2731
2732
2733
2734 inode->i_size = 0;
2735 ext4_orphan_add(handle, inode);
2736 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2737 ext4_mark_inode_dirty(handle, inode);
2738 ext4_dec_count(handle, dir);
2739 ext4_update_dx_flag(dir);
2740 ext4_mark_inode_dirty(handle, dir);
2741
2742end_rmdir:
2743 brelse(bh);
2744 if (handle)
2745 ext4_journal_stop(handle);
2746 return retval;
2747}
2748
2749static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2750{
2751 int retval;
2752 struct inode *inode;
2753 struct buffer_head *bh;
2754 struct ext4_dir_entry_2 *de;
2755 handle_t *handle = NULL;
2756
2757 trace_ext4_unlink_enter(dir, dentry);
2758
2759
2760 dquot_initialize(dir);
2761 dquot_initialize(dentry->d_inode);
2762
2763 retval = -ENOENT;
2764 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2765 if (IS_ERR(bh))
2766 return PTR_ERR(bh);
2767 if (!bh)
2768 goto end_unlink;
2769
2770 inode = dentry->d_inode;
2771
2772 retval = -EIO;
2773 if (le32_to_cpu(de->inode) != inode->i_ino)
2774 goto end_unlink;
2775
2776 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2777 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
2778 if (IS_ERR(handle)) {
2779 retval = PTR_ERR(handle);
2780 handle = NULL;
2781 goto end_unlink;
2782 }
2783
2784 if (IS_DIRSYNC(dir))
2785 ext4_handle_sync(handle);
2786
2787 if (!inode->i_nlink) {
2788 ext4_warning(inode->i_sb,
2789 "Deleting nonexistent file (%lu), %d",
2790 inode->i_ino, inode->i_nlink);
2791 set_nlink(inode, 1);
2792 }
2793 retval = ext4_delete_entry(handle, dir, de, bh);
2794 if (retval)
2795 goto end_unlink;
2796 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2797 ext4_update_dx_flag(dir);
2798 ext4_mark_inode_dirty(handle, dir);
2799 drop_nlink(inode);
2800 if (!inode->i_nlink)
2801 ext4_orphan_add(handle, inode);
2802 inode->i_ctime = ext4_current_time(inode);
2803 ext4_mark_inode_dirty(handle, inode);
2804
2805end_unlink:
2806 brelse(bh);
2807 if (handle)
2808 ext4_journal_stop(handle);
2809 trace_ext4_unlink_exit(dentry, retval);
2810 return retval;
2811}
2812
2813static int ext4_symlink(struct inode *dir,
2814 struct dentry *dentry, const char *symname)
2815{
2816 handle_t *handle;
2817 struct inode *inode;
2818 int l, err, retries = 0;
2819 int credits;
2820
2821 l = strlen(symname)+1;
2822 if (l > dir->i_sb->s_blocksize)
2823 return -ENAMETOOLONG;
2824
2825 dquot_initialize(dir);
2826
2827 if (l > EXT4_N_BLOCKS * 4) {
2828
2829
2830
2831
2832
2833
2834 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2835 EXT4_XATTR_TRANS_BLOCKS;
2836 } else {
2837
2838
2839
2840
2841
2842
2843 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2844 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
2845 }
2846retry:
2847 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
2848 &dentry->d_name, 0, NULL,
2849 EXT4_HT_DIR, credits);
2850 handle = ext4_journal_current_handle();
2851 err = PTR_ERR(inode);
2852 if (IS_ERR(inode))
2853 goto out_stop;
2854
2855 if (l > EXT4_N_BLOCKS * 4) {
2856 inode->i_op = &ext4_symlink_inode_operations;
2857 ext4_set_aops(inode);
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868 drop_nlink(inode);
2869 err = ext4_orphan_add(handle, inode);
2870 ext4_journal_stop(handle);
2871 if (err)
2872 goto err_drop_inode;
2873 err = __page_symlink(inode, symname, l, 1);
2874 if (err)
2875 goto err_drop_inode;
2876
2877
2878
2879
2880 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2881 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2882 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2883 if (IS_ERR(handle)) {
2884 err = PTR_ERR(handle);
2885 goto err_drop_inode;
2886 }
2887 set_nlink(inode, 1);
2888 err = ext4_orphan_del(handle, inode);
2889 if (err) {
2890 ext4_journal_stop(handle);
2891 clear_nlink(inode);
2892 goto err_drop_inode;
2893 }
2894 } else {
2895
2896 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2897 inode->i_op = &ext4_fast_symlink_inode_operations;
2898 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2899 inode->i_size = l-1;
2900 }
2901 EXT4_I(inode)->i_disksize = inode->i_size;
2902 err = ext4_add_nondir(handle, dentry, inode);
2903 if (!err && IS_DIRSYNC(dir))
2904 ext4_handle_sync(handle);
2905
2906out_stop:
2907 if (handle)
2908 ext4_journal_stop(handle);
2909 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2910 goto retry;
2911 return err;
2912err_drop_inode:
2913 unlock_new_inode(inode);
2914 iput(inode);
2915 return err;
2916}
2917
2918static int ext4_link(struct dentry *old_dentry,
2919 struct inode *dir, struct dentry *dentry)
2920{
2921 handle_t *handle;
2922 struct inode *inode = old_dentry->d_inode;
2923 int err, retries = 0;
2924
2925 if (inode->i_nlink >= EXT4_LINK_MAX)
2926 return -EMLINK;
2927
2928 dquot_initialize(dir);
2929
2930retry:
2931 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2932 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2933 EXT4_INDEX_EXTRA_TRANS_BLOCKS));
2934 if (IS_ERR(handle))
2935 return PTR_ERR(handle);
2936
2937 if (IS_DIRSYNC(dir))
2938 ext4_handle_sync(handle);
2939
2940 inode->i_ctime = ext4_current_time(inode);
2941 ext4_inc_count(handle, inode);
2942 ihold(inode);
2943
2944 err = ext4_add_entry(handle, dentry, inode);
2945 if (!err) {
2946 ext4_mark_inode_dirty(handle, inode);
2947 d_instantiate(dentry, inode);
2948 } else {
2949 drop_nlink(inode);
2950 iput(inode);
2951 }
2952 ext4_journal_stop(handle);
2953 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2954 goto retry;
2955 return err;
2956}
2957
2958
2959
2960
2961
2962
2963
2964static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
2965 struct inode *inode,
2966 int *retval,
2967 struct ext4_dir_entry_2 **parent_de,
2968 int *inlined)
2969{
2970 struct buffer_head *bh;
2971
2972 if (!ext4_has_inline_data(inode)) {
2973 bh = ext4_read_dirblock(inode, 0, EITHER);
2974 if (IS_ERR(bh)) {
2975 *retval = PTR_ERR(bh);
2976 return NULL;
2977 }
2978 *parent_de = ext4_next_entry(
2979 (struct ext4_dir_entry_2 *)bh->b_data,
2980 inode->i_sb->s_blocksize);
2981 return bh;
2982 }
2983
2984 *inlined = 1;
2985 return ext4_get_first_inline_block(inode, parent_de, retval);
2986}
2987
2988struct ext4_renament {
2989 struct inode *dir;
2990 struct dentry *dentry;
2991 struct inode *inode;
2992 bool is_dir;
2993 int dir_nlink_delta;
2994
2995
2996 struct buffer_head *bh;
2997 struct ext4_dir_entry_2 *de;
2998 int inlined;
2999
3000
3001 struct buffer_head *dir_bh;
3002 struct ext4_dir_entry_2 *parent_de;
3003 int dir_inlined;
3004};
3005
3006static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3007{
3008 int retval;
3009
3010 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3011 &retval, &ent->parent_de,
3012 &ent->dir_inlined);
3013 if (!ent->dir_bh)
3014 return retval;
3015 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3016 return -EIO;
3017 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3018 return ext4_journal_get_write_access(handle, ent->dir_bh);
3019}
3020
3021static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3022 unsigned dir_ino)
3023{
3024 int retval;
3025
3026 ent->parent_de->inode = cpu_to_le32(dir_ino);
3027 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3028 if (!ent->dir_inlined) {
3029 if (is_dx(ent->inode)) {
3030 retval = ext4_handle_dirty_dx_node(handle,
3031 ent->inode,
3032 ent->dir_bh);
3033 } else {
3034 retval = ext4_handle_dirty_dirent_node(handle,
3035 ent->inode,
3036 ent->dir_bh);
3037 }
3038 } else {
3039 retval = ext4_mark_inode_dirty(handle, ent->inode);
3040 }
3041 if (retval) {
3042 ext4_std_error(ent->dir->i_sb, retval);
3043 return retval;
3044 }
3045 return 0;
3046}
3047
3048static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3049 unsigned ino, unsigned file_type)
3050{
3051 int retval;
3052
3053 BUFFER_TRACE(ent->bh, "get write access");
3054 retval = ext4_journal_get_write_access(handle, ent->bh);
3055 if (retval)
3056 return retval;
3057 ent->de->inode = cpu_to_le32(ino);
3058 if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
3059 EXT4_FEATURE_INCOMPAT_FILETYPE))
3060 ent->de->file_type = file_type;
3061 ent->dir->i_version++;
3062 ent->dir->i_ctime = ent->dir->i_mtime =
3063 ext4_current_time(ent->dir);
3064 ext4_mark_inode_dirty(handle, ent->dir);
3065 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3066 if (!ent->inlined) {
3067 retval = ext4_handle_dirty_dirent_node(handle,
3068 ent->dir, ent->bh);
3069 if (unlikely(retval)) {
3070 ext4_std_error(ent->dir->i_sb, retval);
3071 return retval;
3072 }
3073 }
3074 brelse(ent->bh);
3075 ent->bh = NULL;
3076
3077 return 0;
3078}
3079
3080static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3081 const struct qstr *d_name)
3082{
3083 int retval = -ENOENT;
3084 struct buffer_head *bh;
3085 struct ext4_dir_entry_2 *de;
3086
3087 bh = ext4_find_entry(dir, d_name, &de, NULL);
3088 if (IS_ERR(bh))
3089 return PTR_ERR(bh);
3090 if (bh) {
3091 retval = ext4_delete_entry(handle, dir, de, bh);
3092 brelse(bh);
3093 }
3094 return retval;
3095}
3096
3097static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3098 int force_reread)
3099{
3100 int retval;
3101
3102
3103
3104
3105
3106
3107 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3108 ent->de->name_len != ent->dentry->d_name.len ||
3109 strncmp(ent->de->name, ent->dentry->d_name.name,
3110 ent->de->name_len) ||
3111 force_reread) {
3112 retval = ext4_find_delete_entry(handle, ent->dir,
3113 &ent->dentry->d_name);
3114 } else {
3115 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3116 if (retval == -ENOENT) {
3117 retval = ext4_find_delete_entry(handle, ent->dir,
3118 &ent->dentry->d_name);
3119 }
3120 }
3121
3122 if (retval) {
3123 ext4_warning(ent->dir->i_sb,
3124 "Deleting old file (%lu), %d, error=%d",
3125 ent->dir->i_ino, ent->dir->i_nlink, retval);
3126 }
3127}
3128
3129static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3130{
3131 if (ent->dir_nlink_delta) {
3132 if (ent->dir_nlink_delta == -1)
3133 ext4_dec_count(handle, ent->dir);
3134 else
3135 ext4_inc_count(handle, ent->dir);
3136 ext4_mark_inode_dirty(handle, ent->dir);
3137 }
3138}
3139
3140static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3141 int credits, handle_t **h)
3142{
3143 struct inode *wh;
3144 handle_t *handle;
3145 int retries = 0;
3146
3147
3148
3149
3150
3151 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3152 EXT4_XATTR_TRANS_BLOCKS + 4);
3153retry:
3154 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3155 &ent->dentry->d_name, 0, NULL,
3156 EXT4_HT_DIR, credits);
3157
3158 handle = ext4_journal_current_handle();
3159 if (IS_ERR(wh)) {
3160 if (handle)
3161 ext4_journal_stop(handle);
3162 if (PTR_ERR(wh) == -ENOSPC &&
3163 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3164 goto retry;
3165 } else {
3166 *h = handle;
3167 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3168 wh->i_op = &ext4_special_inode_operations;
3169 }
3170 return wh;
3171}
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3182 struct inode *new_dir, struct dentry *new_dentry,
3183 unsigned int flags)
3184{
3185 handle_t *handle = NULL;
3186 struct ext4_renament old = {
3187 .dir = old_dir,
3188 .dentry = old_dentry,
3189 .inode = old_dentry->d_inode,
3190 };
3191 struct ext4_renament new = {
3192 .dir = new_dir,
3193 .dentry = new_dentry,
3194 .inode = new_dentry->d_inode,
3195 };
3196 int force_reread;
3197 int retval;
3198 struct inode *whiteout = NULL;
3199 int credits;
3200 u8 old_file_type;
3201
3202 dquot_initialize(old.dir);
3203 dquot_initialize(new.dir);
3204
3205
3206
3207 if (new.inode)
3208 dquot_initialize(new.inode);
3209
3210 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
3211 if (IS_ERR(old.bh))
3212 return PTR_ERR(old.bh);
3213
3214
3215
3216
3217
3218
3219 retval = -ENOENT;
3220 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3221 goto end_rename;
3222
3223 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3224 &new.de, &new.inlined);
3225 if (IS_ERR(new.bh)) {
3226 retval = PTR_ERR(new.bh);
3227 new.bh = NULL;
3228 goto end_rename;
3229 }
3230 if (new.bh) {
3231 if (!new.inode) {
3232 brelse(new.bh);
3233 new.bh = NULL;
3234 }
3235 }
3236 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3237 ext4_alloc_da_blocks(old.inode);
3238
3239 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3240 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3241 if (!(flags & RENAME_WHITEOUT)) {
3242 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
3243 if (IS_ERR(handle)) {
3244 retval = PTR_ERR(handle);
3245 handle = NULL;
3246 goto end_rename;
3247 }
3248 } else {
3249 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
3250 if (IS_ERR(whiteout)) {
3251 retval = PTR_ERR(whiteout);
3252 whiteout = NULL;
3253 goto end_rename;
3254 }
3255 }
3256
3257 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3258 ext4_handle_sync(handle);
3259
3260 if (S_ISDIR(old.inode->i_mode)) {
3261 if (new.inode) {
3262 retval = -ENOTEMPTY;
3263 if (!empty_dir(new.inode))
3264 goto end_rename;
3265 } else {
3266 retval = -EMLINK;
3267 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3268 goto end_rename;
3269 }
3270 retval = ext4_rename_dir_prepare(handle, &old);
3271 if (retval)
3272 goto end_rename;
3273 }
3274
3275
3276
3277
3278
3279
3280
3281 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3282 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
3283
3284 old_file_type = old.de->file_type;
3285 if (whiteout) {
3286
3287
3288
3289
3290 retval = ext4_setent(handle, &old, whiteout->i_ino,
3291 EXT4_FT_CHRDEV);
3292 if (retval)
3293 goto end_rename;
3294 ext4_mark_inode_dirty(handle, whiteout);
3295 }
3296 if (!new.bh) {
3297 retval = ext4_add_entry(handle, new.dentry, old.inode);
3298 if (retval)
3299 goto end_rename;
3300 } else {
3301 retval = ext4_setent(handle, &new,
3302 old.inode->i_ino, old_file_type);
3303 if (retval)
3304 goto end_rename;
3305 }
3306 if (force_reread)
3307 force_reread = !ext4_test_inode_flag(new.dir,
3308 EXT4_INODE_INLINE_DATA);
3309
3310
3311
3312
3313
3314 old.inode->i_ctime = ext4_current_time(old.inode);
3315 ext4_mark_inode_dirty(handle, old.inode);
3316
3317 if (!whiteout) {
3318
3319
3320
3321 ext4_rename_delete(handle, &old, force_reread);
3322 }
3323
3324 if (new.inode) {
3325 ext4_dec_count(handle, new.inode);
3326 new.inode->i_ctime = ext4_current_time(new.inode);
3327 }
3328 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3329 ext4_update_dx_flag(old.dir);
3330 if (old.dir_bh) {
3331 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3332 if (retval)
3333 goto end_rename;
3334
3335 ext4_dec_count(handle, old.dir);
3336 if (new.inode) {
3337
3338
3339 clear_nlink(new.inode);
3340 } else {
3341 ext4_inc_count(handle, new.dir);
3342 ext4_update_dx_flag(new.dir);
3343 ext4_mark_inode_dirty(handle, new.dir);
3344 }
3345 }
3346 ext4_mark_inode_dirty(handle, old.dir);
3347 if (new.inode) {
3348 ext4_mark_inode_dirty(handle, new.inode);
3349 if (!new.inode->i_nlink)
3350 ext4_orphan_add(handle, new.inode);
3351 }
3352 retval = 0;
3353
3354end_rename:
3355 brelse(old.dir_bh);
3356 brelse(old.bh);
3357 brelse(new.bh);
3358 if (whiteout) {
3359 if (retval)
3360 drop_nlink(whiteout);
3361 unlock_new_inode(whiteout);
3362 iput(whiteout);
3363 }
3364 if (handle)
3365 ext4_journal_stop(handle);
3366 return retval;
3367}
3368
3369static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3370 struct inode *new_dir, struct dentry *new_dentry)
3371{
3372 handle_t *handle = NULL;
3373 struct ext4_renament old = {
3374 .dir = old_dir,
3375 .dentry = old_dentry,
3376 .inode = old_dentry->d_inode,
3377 };
3378 struct ext4_renament new = {
3379 .dir = new_dir,
3380 .dentry = new_dentry,
3381 .inode = new_dentry->d_inode,
3382 };
3383 u8 new_file_type;
3384 int retval;
3385
3386 dquot_initialize(old.dir);
3387 dquot_initialize(new.dir);
3388
3389 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3390 &old.de, &old.inlined);
3391
3392
3393
3394
3395
3396
3397 retval = -ENOENT;
3398 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3399 goto end_rename;
3400
3401 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3402 &new.de, &new.inlined);
3403
3404
3405 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3406 goto end_rename;
3407
3408 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3409 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3410 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3411 if (IS_ERR(handle)) {
3412 retval = PTR_ERR(handle);
3413 handle = NULL;
3414 goto end_rename;
3415 }
3416
3417 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3418 ext4_handle_sync(handle);
3419
3420 if (S_ISDIR(old.inode->i_mode)) {
3421 old.is_dir = true;
3422 retval = ext4_rename_dir_prepare(handle, &old);
3423 if (retval)
3424 goto end_rename;
3425 }
3426 if (S_ISDIR(new.inode->i_mode)) {
3427 new.is_dir = true;
3428 retval = ext4_rename_dir_prepare(handle, &new);
3429 if (retval)
3430 goto end_rename;
3431 }
3432
3433
3434
3435
3436
3437 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3438 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3439 new.dir_nlink_delta = -old.dir_nlink_delta;
3440 retval = -EMLINK;
3441 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3442 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3443 goto end_rename;
3444 }
3445
3446 new_file_type = new.de->file_type;
3447 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3448 if (retval)
3449 goto end_rename;
3450
3451 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3452 if (retval)
3453 goto end_rename;
3454
3455
3456
3457
3458
3459 old.inode->i_ctime = ext4_current_time(old.inode);
3460 new.inode->i_ctime = ext4_current_time(new.inode);
3461 ext4_mark_inode_dirty(handle, old.inode);
3462 ext4_mark_inode_dirty(handle, new.inode);
3463
3464 if (old.dir_bh) {
3465 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3466 if (retval)
3467 goto end_rename;
3468 }
3469 if (new.dir_bh) {
3470 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3471 if (retval)
3472 goto end_rename;
3473 }
3474 ext4_update_dir_count(handle, &old);
3475 ext4_update_dir_count(handle, &new);
3476 retval = 0;
3477
3478end_rename:
3479 brelse(old.dir_bh);
3480 brelse(new.dir_bh);
3481 brelse(old.bh);
3482 brelse(new.bh);
3483 if (handle)
3484 ext4_journal_stop(handle);
3485 return retval;
3486}
3487
3488static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3489 struct inode *new_dir, struct dentry *new_dentry,
3490 unsigned int flags)
3491{
3492 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
3493 return -EINVAL;
3494
3495 if (flags & RENAME_EXCHANGE) {
3496 return ext4_cross_rename(old_dir, old_dentry,
3497 new_dir, new_dentry);
3498 }
3499
3500 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
3501}
3502
3503static int ext4_rename_old(struct inode *old_dir, struct dentry *old_dentry,
3504 struct inode *new_dir, struct dentry *new_dentry)
3505{
3506 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, 0);
3507}
3508
3509
3510
3511
3512const struct inode_operations_wrapper ext4_dir_inode_operations = {
3513 .ops = {
3514 .create = ext4_create,
3515 .lookup = ext4_lookup,
3516 .link = ext4_link,
3517 .unlink = ext4_unlink,
3518 .symlink = ext4_symlink,
3519 .mkdir = ext4_mkdir,
3520 .rmdir = ext4_rmdir,
3521 .mknod = ext4_mknod,
3522 .rename = ext4_rename_old,
3523 .setattr = ext4_setattr,
3524 .setxattr = generic_setxattr,
3525 .getxattr = generic_getxattr,
3526 .listxattr = ext4_listxattr,
3527 .removexattr = generic_removexattr,
3528 .get_acl = ext4_get_acl,
3529 .fiemap = ext4_fiemap,
3530 },
3531 .rename2 = ext4_rename2,
3532};
3533
3534const struct inode_operations ext4_special_inode_operations = {
3535 .setattr = ext4_setattr,
3536 .setxattr = generic_setxattr,
3537 .getxattr = generic_getxattr,
3538 .listxattr = ext4_listxattr,
3539 .removexattr = generic_removexattr,
3540 .get_acl = ext4_get_acl,
3541};
3542