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