blob: ab16beaa830d3814250146d044f8d2c4a59c7dd4 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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>
Mingming Caodab291a2006-10-11 01:21:01 -070029#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#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>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040037#include "ext4.h"
38#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039
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
Dave Kleikamp8c55e202007-05-24 13:04:54 -040049#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
51
Mingming Cao617ba132006-10-11 01:20:53 -070052static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -050054 ext4_lblk_t *block, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055{
56 struct buffer_head *bh;
57
58 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
59
Akinobu Mitaa8716112008-04-17 10:38:59 -040060 bh = ext4_bread(handle, inode, *block, 1, err);
61 if (bh) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 inode->i_size += inode->i_sb->s_blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -070063 EXT4_I(inode)->i_disksize = inode->i_size;
Akinobu Mitaa8716112008-04-17 10:38:59 -040064 *err = ext4_journal_get_write_access(handle, bh);
65 if (*err) {
66 brelse(bh);
67 bh = NULL;
68 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069 }
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;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700148 u16 offs;
149 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150};
151
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500152static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
153static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154static 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);
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);
167static void dx_release (struct dx_frame *frames);
Mingming Cao617ba132006-10-11 01:20:53 -0700168static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
170static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Mingming Cao617ba132006-10-11 01:20:53 -0700171static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 struct dx_map_entry *offsets, int count);
Mingming Cao617ba132006-10-11 01:20:53 -0700173static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500174static void dx_insert_block(struct dx_frame *frame,
175 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700176static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 struct dx_frame *frame,
178 struct dx_frame *frames,
179 __u32 *start_hash);
Mingming Cao617ba132006-10-11 01:20:53 -0700180static 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,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 struct inode *inode);
184
185/*
186 * Future: use high four bits of block for coalesce-on-delete flags
187 * Mask them off for now.
188 */
189
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500190static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191{
192 return le32_to_cpu(entry->block) & 0x00ffffff;
193}
194
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500195static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196{
197 entry->block = cpu_to_le32(value);
198}
199
200static inline unsigned dx_get_hash (struct dx_entry *entry)
201{
202 return le32_to_cpu(entry->hash);
203}
204
205static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
206{
207 entry->hash = cpu_to_le32(value);
208}
209
210static inline unsigned dx_get_count (struct dx_entry *entries)
211{
212 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
213}
214
215static inline unsigned dx_get_limit (struct dx_entry *entries)
216{
217 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
218}
219
220static inline void dx_set_count (struct dx_entry *entries, unsigned value)
221{
222 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
223}
224
225static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
226{
227 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
228}
229
230static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
231{
Mingming Cao617ba132006-10-11 01:20:53 -0700232 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
233 EXT4_DIR_REC_LEN(2) - infosize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700234 return 0? 20: entry_space / sizeof(struct dx_entry);
235}
236
237static inline unsigned dx_node_limit (struct inode *dir)
238{
Mingming Cao617ba132006-10-11 01:20:53 -0700239 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 return 0? 22: entry_space / sizeof(struct dx_entry);
241}
242
243/*
244 * Debug
245 */
246#ifdef DX_DEBUG
247static void dx_show_index (char * label, struct dx_entry *entries)
248{
Andrew Morton63f57932006-10-11 01:21:24 -0700249 int i, n = dx_get_count (entries);
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400250 printk("%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700251 for (i = 0; i < n; i++) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500252 printk("%x->%lu ", i? dx_get_hash(entries + i) :
253 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700254 }
255 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256}
257
258struct stats
259{
260 unsigned names;
261 unsigned space;
262 unsigned bcount;
263};
264
Mingming Cao617ba132006-10-11 01:20:53 -0700265static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 int size, int show_names)
267{
268 unsigned names = 0, space = 0;
269 char *base = (char *) de;
270 struct dx_hash_info h = *hinfo;
271
272 printk("names: ");
273 while ((char *) de < base + size)
274 {
275 if (de->inode)
276 {
277 if (show_names)
278 {
279 int len = de->name_len;
280 char *name = de->name;
281 while (len--) printk("%c", *name++);
Mingming Cao617ba132006-10-11 01:20:53 -0700282 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 printk(":%x.%u ", h.hash,
284 ((char *) de - base));
285 }
Mingming Cao617ba132006-10-11 01:20:53 -0700286 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 names++;
288 }
Jan Karaa72d7f82008-01-28 23:58:27 -0500289 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 }
291 printk("(%i)\n", names);
292 return (struct stats) { names, space, 1 };
293}
294
295struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
296 struct dx_entry *entries, int levels)
297{
298 unsigned blocksize = dir->i_sb->s_blocksize;
299 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
300 unsigned bcount = 0;
301 struct buffer_head *bh;
302 int err;
303 printk("%i indexed blocks...\n", count);
304 for (i = 0; i < count; i++, entries++)
305 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500306 ext4_lblk_t block = dx_get_block(entries);
307 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
309 struct stats stats;
310 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Mingming Cao617ba132006-10-11 01:20:53 -0700311 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 stats = levels?
313 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Mingming Cao617ba132006-10-11 01:20:53 -0700314 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315 names += stats.names;
316 space += stats.space;
317 bcount += stats.bcount;
318 brelse (bh);
319 }
320 if (bcount)
321 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
322 names, space/bcount,(space/bcount)*100/blocksize);
323 return (struct stats) { names, space, bcount};
324}
325#endif /* DX_DEBUG */
326
327/*
328 * Probe for a directory leaf block to search.
329 *
330 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
331 * error in the directory index, and the caller should fall back to
332 * searching the directory normally. The callers of dx_probe **MUST**
333 * check for this error code, and make sure it never gets reflected
334 * back to userspace.
335 */
336static struct dx_frame *
337dx_probe(struct dentry *dentry, struct inode *dir,
338 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
339{
340 unsigned count, indirect;
341 struct dx_entry *at, *entries, *p, *q, *m;
342 struct dx_root *root;
343 struct buffer_head *bh;
344 struct dx_frame *frame = frame_in;
345 u32 hash;
346
347 frame->bh = NULL;
348 if (dentry)
349 dir = dentry->d_parent->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700350 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 goto fail;
352 root = (struct dx_root *) bh->b_data;
353 if (root->info.hash_version != DX_HASH_TEA &&
354 root->info.hash_version != DX_HASH_HALF_MD4 &&
355 root->info.hash_version != DX_HASH_LEGACY) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400356 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 "Unrecognised inode hash code %d",
358 root->info.hash_version);
359 brelse(bh);
360 *err = ERR_BAD_DX_DIR;
361 goto fail;
362 }
363 hinfo->hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -0700364 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 if (dentry)
Mingming Cao617ba132006-10-11 01:20:53 -0700366 ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 hash = hinfo->hash;
368
369 if (root->info.unused_flags & 1) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400370 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 "Unimplemented inode hash flags: %#06x",
372 root->info.unused_flags);
373 brelse(bh);
374 *err = ERR_BAD_DX_DIR;
375 goto fail;
376 }
377
378 if ((indirect = root->info.indirect_levels) > 1) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400379 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 "Unimplemented inode hash depth: %#06x",
381 root->info.indirect_levels);
382 brelse(bh);
383 *err = ERR_BAD_DX_DIR;
384 goto fail;
385 }
386
387 entries = (struct dx_entry *) (((char *)&root->info) +
388 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700389
390 if (dx_get_limit(entries) != dx_root_limit(dir,
391 root->info.info_length)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400392 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700393 "dx entry: limit != root limit");
394 brelse(bh);
395 *err = ERR_BAD_DX_DIR;
396 goto fail;
397 }
398
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399 dxtrace (printk("Look up %x", hash));
400 while (1)
401 {
402 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700403 if (!count || count > dx_get_limit(entries)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400404 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700405 "dx entry: no count or count > limit");
406 brelse(bh);
407 *err = ERR_BAD_DX_DIR;
408 goto fail2;
409 }
410
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411 p = entries + 1;
412 q = entries + count - 1;
413 while (p <= q)
414 {
415 m = p + (q - p)/2;
416 dxtrace(printk("."));
417 if (dx_get_hash(m) > hash)
418 q = m - 1;
419 else
420 p = m + 1;
421 }
422
423 if (0) // linear search cross check
424 {
425 unsigned n = count - 1;
426 at = entries;
427 while (n--)
428 {
429 dxtrace(printk(","));
430 if (dx_get_hash(++at) > hash)
431 {
432 at--;
433 break;
434 }
435 }
436 assert (at == p - 1);
437 }
438
439 at = p - 1;
440 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
441 frame->bh = bh;
442 frame->entries = entries;
443 frame->at = at;
444 if (!indirect--) return frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700445 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 goto fail2;
447 at = entries = ((struct dx_node *) bh->b_data)->entries;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700448 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400449 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700450 "dx entry: limit != node limit");
451 brelse(bh);
452 *err = ERR_BAD_DX_DIR;
453 goto fail2;
454 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700456 frame->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457 }
458fail2:
459 while (frame >= frame_in) {
460 brelse(frame->bh);
461 frame--;
462 }
463fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700464 if (*err == ERR_BAD_DX_DIR)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400465 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700466 "Corrupt dir inode %ld, running e2fsck is "
467 "recommended.", dir->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 return NULL;
469}
470
471static void dx_release (struct dx_frame *frames)
472{
473 if (frames[0].bh == NULL)
474 return;
475
476 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
477 brelse(frames[1].bh);
478 brelse(frames[0].bh);
479}
480
481/*
482 * This function increments the frame pointer to search the next leaf
483 * block, and reads in the necessary intervening nodes if the search
484 * should be necessary. Whether or not the search is necessary is
485 * controlled by the hash parameter. If the hash value is even, then
486 * the search is only continued if the next block starts with that
487 * hash value. This is used if we are searching for a specific file.
488 *
489 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
490 *
491 * This function returns 1 if the caller should continue to search,
492 * or 0 if it should not. If there is an error reading one of the
493 * index blocks, it will a negative error code.
494 *
495 * If start_hash is non-null, it will be filled in with the starting
496 * hash of the next page.
497 */
Mingming Cao617ba132006-10-11 01:20:53 -0700498static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 struct dx_frame *frame,
500 struct dx_frame *frames,
501 __u32 *start_hash)
502{
503 struct dx_frame *p;
504 struct buffer_head *bh;
505 int err, num_frames = 0;
506 __u32 bhash;
507
508 p = frame;
509 /*
510 * Find the next leaf page by incrementing the frame pointer.
511 * If we run out of entries in the interior node, loop around and
512 * increment pointer in the parent node. When we break out of
513 * this loop, num_frames indicates the number of interior
514 * nodes need to be read.
515 */
516 while (1) {
517 if (++(p->at) < p->entries + dx_get_count(p->entries))
518 break;
519 if (p == frames)
520 return 0;
521 num_frames++;
522 p--;
523 }
524
525 /*
526 * If the hash is 1, then continue only if the next page has a
527 * continuation hash of any value. This is used for readdir
528 * handling. Otherwise, check to see if the hash matches the
529 * desired contiuation hash. If it doesn't, return since
530 * there's no point to read in the successive index pages.
531 */
532 bhash = dx_get_hash(p->at);
533 if (start_hash)
534 *start_hash = bhash;
535 if ((hash & 1) == 0) {
536 if ((bhash & ~1) != hash)
537 return 0;
538 }
539 /*
540 * If the hash is HASH_NB_ALWAYS, we always go to the next
541 * block so no check is necessary
542 */
543 while (num_frames--) {
Mingming Cao617ba132006-10-11 01:20:53 -0700544 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 0, &err)))
546 return err; /* Failure */
547 p++;
548 brelse (p->bh);
549 p->bh = bh;
550 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
551 }
552 return 1;
553}
554
555
556/*
557 * p is at least 6 bytes before the end of page
558 */
Mingming Cao617ba132006-10-11 01:20:53 -0700559static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560{
Jan Karaa72d7f82008-01-28 23:58:27 -0500561 return (struct ext4_dir_entry_2 *)((char *)p +
562 ext4_rec_len_from_disk(p->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563}
564
565/*
566 * This function fills a red-black tree with information from a
567 * directory block. It returns the number directory entries loaded
568 * into the tree. If there is an error it is returned in err.
569 */
570static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500571 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 struct dx_hash_info *hinfo,
573 __u32 start_hash, __u32 start_minor_hash)
574{
575 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700576 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 int err, count = 0;
578
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500579 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
580 (unsigned long)block));
Mingming Cao617ba132006-10-11 01:20:53 -0700581 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 return err;
583
Mingming Cao617ba132006-10-11 01:20:53 -0700584 de = (struct ext4_dir_entry_2 *) bh->b_data;
585 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700587 EXT4_DIR_REC_LEN(0));
588 for (; de < top; de = ext4_next_entry(de)) {
Eric Sandeene6c40212006-12-06 20:36:28 -0800589 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
590 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
591 +((char *)de - bh->b_data))) {
592 /* On error, skip the f_pos to the next block. */
593 dir_file->f_pos = (dir_file->f_pos |
594 (dir->i_sb->s_blocksize - 1)) + 1;
595 brelse (bh);
596 return count;
597 }
Mingming Cao617ba132006-10-11 01:20:53 -0700598 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 if ((hinfo->hash < start_hash) ||
600 ((hinfo->hash == start_hash) &&
601 (hinfo->minor_hash < start_minor_hash)))
602 continue;
603 if (de->inode == 0)
604 continue;
Mingming Cao617ba132006-10-11 01:20:53 -0700605 if ((err = ext4_htree_store_dirent(dir_file,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 hinfo->hash, hinfo->minor_hash, de)) != 0) {
607 brelse(bh);
608 return err;
609 }
610 count++;
611 }
612 brelse(bh);
613 return count;
614}
615
616
617/*
618 * This function fills a red-black tree with information from a
619 * directory. We start scanning the directory in hash order, starting
620 * at start_hash and start_minor_hash.
621 *
622 * This function returns the number of entries inserted into the tree,
623 * or a negative error code.
624 */
Mingming Cao617ba132006-10-11 01:20:53 -0700625int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700626 __u32 start_minor_hash, __u32 *next_hash)
627{
628 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -0700629 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 struct dx_frame frames[2], *frame;
631 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500632 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500634 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 __u32 hashval;
636
637 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
638 start_minor_hash));
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800639 dir = dir_file->f_path.dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700640 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
641 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
642 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
644 start_hash, start_minor_hash);
645 *next_hash = ~0;
646 return count;
647 }
648 hinfo.hash = start_hash;
649 hinfo.minor_hash = 0;
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800650 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 if (!frame)
652 return err;
653
654 /* Add '.' and '..' from the htree header */
655 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -0700656 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
657 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 goto errout;
659 count++;
660 }
661 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700662 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
663 de = ext4_next_entry(de);
664 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 goto errout;
666 count++;
667 }
668
669 while (1) {
670 block = dx_get_block(frame->at);
671 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
672 start_hash, start_minor_hash);
673 if (ret < 0) {
674 err = ret;
675 goto errout;
676 }
677 count += ret;
678 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -0700679 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680 frame, frames, &hashval);
681 *next_hash = hashval;
682 if (ret < 0) {
683 err = ret;
684 goto errout;
685 }
686 /*
687 * Stop if: (a) there are no more entries, or
688 * (b) we have inserted at least one entry and the
689 * next hash value is not a continuation
690 */
691 if ((ret == 0) ||
692 (count && ((hashval & 1) == 0)))
693 break;
694 }
695 dx_release(frames);
696 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
697 count, *next_hash));
698 return count;
699errout:
700 dx_release(frames);
701 return (err);
702}
703
704
705/*
706 * Directory block splitting, compacting
707 */
708
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700709/*
710 * Create map of hash values, offsets, and sizes, stored at end of block.
711 * Returns number of entries mapped.
712 */
Mingming Cao617ba132006-10-11 01:20:53 -0700713static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
715{
716 int count = 0;
717 char *base = (char *) de;
718 struct dx_hash_info h = *hinfo;
719
720 while ((char *) de < base + size)
721 {
722 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -0700723 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 map_tail--;
725 map_tail->hash = h.hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700726 map_tail->offs = (u16) ((char *) de - base);
727 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 count++;
729 cond_resched();
730 }
731 /* XXX: do we need to check rec_len == 0 case? -Chris */
Jan Karaa72d7f82008-01-28 23:58:27 -0500732 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 }
734 return count;
735}
736
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700737/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738static void dx_sort_map (struct dx_map_entry *map, unsigned count)
739{
Andrew Morton63f57932006-10-11 01:21:24 -0700740 struct dx_map_entry *p, *q, *top = map + count - 1;
741 int more;
742 /* Combsort until bubble sort doesn't suck */
743 while (count > 2) {
744 count = count*10/13;
745 if (count - 9 < 2) /* 9, 10 -> 11 */
746 count = 11;
747 for (p = top, q = p - count; q >= map; p--, q--)
748 if (p->hash < q->hash)
749 swap(*p, *q);
750 }
751 /* Garden variety bubble sort */
752 do {
753 more = 0;
754 q = top;
755 while (q-- > map) {
756 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 continue;
Andrew Morton63f57932006-10-11 01:21:24 -0700758 swap(*(q+1), *q);
759 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 }
761 } while(more);
762}
763
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500764static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765{
766 struct dx_entry *entries = frame->entries;
767 struct dx_entry *old = frame->at, *new = old + 1;
768 int count = dx_get_count(entries);
769
770 assert(count < dx_get_limit(entries));
771 assert(old < entries + count);
772 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
773 dx_set_hash(new, hash);
774 dx_set_block(new, block);
775 dx_set_count(entries, count + 1);
776}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777
Mingming Cao617ba132006-10-11 01:20:53 -0700778static void ext4_update_dx_flag(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779{
Mingming Cao617ba132006-10-11 01:20:53 -0700780 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
781 EXT4_FEATURE_COMPAT_DIR_INDEX))
782 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783}
784
785/*
Mingming Cao617ba132006-10-11 01:20:53 -0700786 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 *
Mingming Cao617ba132006-10-11 01:20:53 -0700788 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 * `de != NULL' is guaranteed by caller.
790 */
Mingming Cao617ba132006-10-11 01:20:53 -0700791static inline int ext4_match (int len, const char * const name,
792 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793{
794 if (len != de->name_len)
795 return 0;
796 if (!de->inode)
797 return 0;
798 return !memcmp(name, de->name, len);
799}
800
801/*
802 * Returns 0 if not found, -1 on failure, and 1 on success
803 */
804static inline int search_dirblock(struct buffer_head * bh,
805 struct inode *dir,
806 struct dentry *dentry,
807 unsigned long offset,
Mingming Cao617ba132006-10-11 01:20:53 -0700808 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809{
Mingming Cao617ba132006-10-11 01:20:53 -0700810 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 char * dlimit;
812 int de_len;
813 const char *name = dentry->d_name.name;
814 int namelen = dentry->d_name.len;
815
Mingming Cao617ba132006-10-11 01:20:53 -0700816 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 dlimit = bh->b_data + dir->i_sb->s_blocksize;
818 while ((char *) de < dlimit) {
819 /* this code is executed quadratically often */
820 /* do minimal checking `by hand' */
821
822 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -0700823 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 /* found a match - just to be sure, do a full check */
Mingming Cao617ba132006-10-11 01:20:53 -0700825 if (!ext4_check_dir_entry("ext4_find_entry",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826 dir, de, bh, offset))
827 return -1;
828 *res_dir = de;
829 return 1;
830 }
831 /* prevent looping on a bad block */
Jan Karaa72d7f82008-01-28 23:58:27 -0500832 de_len = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 if (de_len <= 0)
834 return -1;
835 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -0700836 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 }
838 return 0;
839}
840
841
842/*
Mingming Cao617ba132006-10-11 01:20:53 -0700843 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 *
845 * finds an entry in the specified directory with the wanted name. It
846 * returns the cache buffer in which the entry was found, and the entry
847 * itself (as a parameter - res_dir). It does NOT read the inode of the
848 * entry - you'll have to do that yourself if you want to.
849 *
850 * The returned buffer_head has ->b_count elevated. The caller is expected
851 * to brelse() it when appropriate.
852 */
Mingming Cao617ba132006-10-11 01:20:53 -0700853static struct buffer_head * ext4_find_entry (struct dentry *dentry,
854 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855{
856 struct super_block * sb;
857 struct buffer_head * bh_use[NAMEI_RA_SIZE];
858 struct buffer_head * bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500859 ext4_lblk_t start, block, b;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 int ra_max = 0; /* Number of bh's in the readahead
861 buffer, bh_use[] */
862 int ra_ptr = 0; /* Current index into readahead
863 buffer */
864 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500865 ext4_lblk_t nblocks;
866 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 struct inode *dir = dentry->d_parent->d_inode;
868 int namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869
870 *res_dir = NULL;
871 sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 namelen = dentry->d_name.len;
Mingming Cao617ba132006-10-11 01:20:53 -0700873 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700876 bh = ext4_dx_find_entry(dentry, res_dir, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 /*
878 * On success, or if the error was file not found,
879 * return. Otherwise, fall back to doing a search the
880 * old fashioned way.
881 */
882 if (bh || (err != ERR_BAD_DX_DIR))
883 return bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700884 dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 }
Mingming Cao617ba132006-10-11 01:20:53 -0700886 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
887 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 if (start >= nblocks)
889 start = 0;
890 block = start;
891restart:
892 do {
893 /*
894 * We deal with the read-ahead logic here.
895 */
896 if (ra_ptr >= ra_max) {
897 /* Refill the readahead buffer */
898 ra_ptr = 0;
899 b = block;
900 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
901 /*
902 * Terminate if we reach the end of the
903 * directory and must wrap, or if our
904 * search has finished at this block.
905 */
906 if (b >= nblocks || (num && block == start)) {
907 bh_use[ra_max] = NULL;
908 break;
909 }
910 num++;
Mingming Cao617ba132006-10-11 01:20:53 -0700911 bh = ext4_getblk(NULL, dir, b++, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 bh_use[ra_max] = bh;
913 if (bh)
914 ll_rw_block(READ_META, 1, &bh);
915 }
916 }
917 if ((bh = bh_use[ra_ptr++]) == NULL)
918 goto next;
919 wait_on_buffer(bh);
920 if (!buffer_uptodate(bh)) {
921 /* read error, skip block & hope for the best */
Harvey Harrison46e665e2008-04-17 10:38:59 -0400922 ext4_error(sb, __func__, "reading directory #%lu "
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500923 "offset %lu", dir->i_ino,
924 (unsigned long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925 brelse(bh);
926 goto next;
927 }
928 i = search_dirblock(bh, dir, dentry,
Mingming Cao617ba132006-10-11 01:20:53 -0700929 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700931 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 ret = bh;
933 goto cleanup_and_exit;
934 } else {
935 brelse(bh);
936 if (i < 0)
937 goto cleanup_and_exit;
938 }
939 next:
940 if (++block >= nblocks)
941 block = 0;
942 } while (block != start);
943
944 /*
945 * If the directory has grown while we were searching, then
946 * search the last part of the directory before giving up.
947 */
948 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -0700949 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 if (block < nblocks) {
951 start = 0;
952 goto restart;
953 }
954
955cleanup_and_exit:
956 /* Clean up the read-ahead blocks */
957 for (; ra_ptr < ra_max; ra_ptr++)
958 brelse (bh_use[ra_ptr]);
959 return ret;
960}
961
Mingming Cao617ba132006-10-11 01:20:53 -0700962static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
963 struct ext4_dir_entry_2 **res_dir, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964{
965 struct super_block * sb;
966 struct dx_hash_info hinfo;
967 u32 hash;
968 struct dx_frame frames[2], *frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700969 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500971 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 int retval;
973 int namelen = dentry->d_name.len;
974 const u8 *name = dentry->d_name.name;
975 struct inode *dir = dentry->d_parent->d_inode;
976
977 sb = dir->i_sb;
978 /* NFS may look up ".." - look at dx_root directory block */
979 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
980 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
981 return NULL;
982 } else {
983 frame = frames;
984 frame->bh = NULL; /* for dx_release() */
985 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
986 dx_set_block(frame->at, 0); /* dx_root block is 0 */
987 }
988 hash = hinfo.hash;
989 do {
990 block = dx_get_block(frame->at);
Mingming Cao617ba132006-10-11 01:20:53 -0700991 if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 goto errout;
Mingming Cao617ba132006-10-11 01:20:53 -0700993 de = (struct ext4_dir_entry_2 *) bh->b_data;
994 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
995 EXT4_DIR_REC_LEN(0));
996 for (; de < top; de = ext4_next_entry(de))
997 if (ext4_match (namelen, name, de)) {
998 if (!ext4_check_dir_entry("ext4_find_entry",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 dir, de, bh,
Mingming Cao617ba132006-10-11 01:20:53 -07001000 (block<<EXT4_BLOCK_SIZE_BITS(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 +((char *)de - bh->b_data))) {
1002 brelse (bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001003 *err = ERR_BAD_DX_DIR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 goto errout;
1005 }
1006 *res_dir = de;
1007 dx_release (frames);
1008 return bh;
1009 }
1010 brelse (bh);
1011 /* Check to see if we should continue to search */
Mingming Cao617ba132006-10-11 01:20:53 -07001012 retval = ext4_htree_next_block(dir, hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013 frames, NULL);
1014 if (retval < 0) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001015 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 "error reading index page in directory #%lu",
1017 dir->i_ino);
1018 *err = retval;
1019 goto errout;
1020 }
1021 } while (retval == 1);
1022
1023 *err = -ENOENT;
1024errout:
1025 dxtrace(printk("%s not found\n", name));
1026 dx_release (frames);
1027 return NULL;
1028}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029
Mingming Cao617ba132006-10-11 01:20:53 -07001030static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031{
1032 struct inode * inode;
Mingming Cao617ba132006-10-11 01:20:53 -07001033 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 struct buffer_head * bh;
1035
Mingming Cao617ba132006-10-11 01:20:53 -07001036 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 return ERR_PTR(-ENAMETOOLONG);
1038
Mingming Cao617ba132006-10-11 01:20:53 -07001039 bh = ext4_find_entry(dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 inode = NULL;
1041 if (bh) {
1042 unsigned long ino = le32_to_cpu(de->inode);
1043 brelse (bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001044 if (!ext4_valid_inum(dir->i_sb, ino)) {
1045 ext4_error(dir->i_sb, "ext4_lookup",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 "bad inode number: %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001047 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001048 }
David Howells1d1fe1e2008-02-07 00:15:37 -08001049 inode = ext4_iget(dir->i_sb, ino);
1050 if (IS_ERR(inode))
1051 return ERR_CAST(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 }
1053 return d_splice_alias(inode, dentry);
1054}
1055
1056
Mingming Cao617ba132006-10-11 01:20:53 -07001057struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058{
1059 unsigned long ino;
1060 struct dentry *parent;
1061 struct inode *inode;
1062 struct dentry dotdot;
Mingming Cao617ba132006-10-11 01:20:53 -07001063 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 struct buffer_head *bh;
1065
1066 dotdot.d_name.name = "..";
1067 dotdot.d_name.len = 2;
1068 dotdot.d_parent = child; /* confusing, isn't it! */
1069
Mingming Cao617ba132006-10-11 01:20:53 -07001070 bh = ext4_find_entry(&dotdot, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 inode = NULL;
1072 if (!bh)
1073 return ERR_PTR(-ENOENT);
1074 ino = le32_to_cpu(de->inode);
1075 brelse(bh);
1076
Mingming Cao617ba132006-10-11 01:20:53 -07001077 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1078 ext4_error(child->d_inode->i_sb, "ext4_get_parent",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 "bad inode number: %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001080 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001081 }
1082
David Howells1d1fe1e2008-02-07 00:15:37 -08001083 inode = ext4_iget(child->d_inode->i_sb, ino);
1084 if (IS_ERR(inode))
1085 return ERR_CAST(inode);
1086
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087 parent = d_alloc_anon(inode);
1088 if (!parent) {
1089 iput(inode);
1090 parent = ERR_PTR(-ENOMEM);
1091 }
1092 return parent;
1093}
1094
1095#define S_SHIFT 12
Mingming Cao617ba132006-10-11 01:20:53 -07001096static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1097 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1098 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1099 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1100 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1101 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1102 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1103 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104};
1105
Mingming Cao617ba132006-10-11 01:20:53 -07001106static inline void ext4_set_de_type(struct super_block *sb,
1107 struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 umode_t mode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001109 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1110 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111}
1112
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001113/*
1114 * Move count entries from end of map between two memory locations.
1115 * Returns pointer to last entry moved.
1116 */
Mingming Cao617ba132006-10-11 01:20:53 -07001117static struct ext4_dir_entry_2 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1119{
1120 unsigned rec_len = 0;
1121
1122 while (count--) {
Mingming Cao617ba132006-10-11 01:20:53 -07001123 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
1124 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001126 ((struct ext4_dir_entry_2 *) to)->rec_len =
Jan Karaa72d7f82008-01-28 23:58:27 -05001127 ext4_rec_len_to_disk(rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128 de->inode = 0;
1129 map++;
1130 to += rec_len;
1131 }
Mingming Cao617ba132006-10-11 01:20:53 -07001132 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133}
1134
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001135/*
1136 * Compact each dir entry in the range to the minimal rec_len.
1137 * Returns pointer to last entry in range.
1138 */
Mingming Cao617ba132006-10-11 01:20:53 -07001139static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140{
Mingming Cao617ba132006-10-11 01:20:53 -07001141 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142 unsigned rec_len = 0;
1143
1144 prev = to = de;
1145 while ((char*)de < base + size) {
Jan Karaa72d7f82008-01-28 23:58:27 -05001146 next = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001148 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 if (de > to)
1150 memmove(to, de, rec_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001151 to->rec_len = ext4_rec_len_to_disk(rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001153 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001154 }
1155 de = next;
1156 }
1157 return prev;
1158}
1159
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001160/*
1161 * Split a full leaf block to make room for a new dir entry.
1162 * Allocate a new block, and move entries so that they are approx. equally full.
1163 * Returns pointer to de in block into which the new entry will be inserted.
1164 */
Mingming Cao617ba132006-10-11 01:20:53 -07001165static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 struct buffer_head **bh,struct dx_frame *frame,
1167 struct dx_hash_info *hinfo, int *error)
1168{
1169 unsigned blocksize = dir->i_sb->s_blocksize;
1170 unsigned count, continued;
1171 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001172 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 u32 hash2;
1174 struct dx_map_entry *map;
1175 char *data1 = (*bh)->b_data, *data2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001176 unsigned split, move, size, i;
Mingming Cao617ba132006-10-11 01:20:53 -07001177 struct ext4_dir_entry_2 *de = NULL, *de2;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001178 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001180 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 if (!(bh2)) {
1182 brelse(*bh);
1183 *bh = NULL;
1184 goto errout;
1185 }
1186
1187 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001188 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001189 if (err)
1190 goto journal_error;
1191
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001193 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 if (err)
1195 goto journal_error;
1196
1197 data2 = bh2->b_data;
1198
1199 /* create map in the end of data2 block */
1200 map = (struct dx_map_entry *) (data2 + blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -07001201 count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 blocksize, hinfo, map);
1203 map -= count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 dx_sort_map (map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001205 /* Split the existing block in the middle, size-wise */
1206 size = 0;
1207 move = 0;
1208 for (i = count-1; i >= 0; i--) {
1209 /* is more than half of this entry in 2nd half of the block? */
1210 if (size + map[i].size/2 > blocksize/2)
1211 break;
1212 size += map[i].size;
1213 move++;
1214 }
1215 /* map index at which we will split */
1216 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 hash2 = map[split].hash;
1218 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001219 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1220 (unsigned long)dx_get_block(frame->at),
1221 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222
1223 /* Fancy dance to stay within two buffers */
1224 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1225 de = dx_pack_dirents(data1,blocksize);
Jan Karaa72d7f82008-01-28 23:58:27 -05001226 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
1227 de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
Mingming Cao617ba132006-10-11 01:20:53 -07001228 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1229 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230
1231 /* Which block gets the new entry? */
1232 if (hinfo->hash >= hash2)
1233 {
1234 swap(*bh, bh2);
1235 de = de2;
1236 }
1237 dx_insert_block (frame, hash2 + continued, newblock);
Mingming Cao617ba132006-10-11 01:20:53 -07001238 err = ext4_journal_dirty_metadata (handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 if (err)
1240 goto journal_error;
Mingming Cao617ba132006-10-11 01:20:53 -07001241 err = ext4_journal_dirty_metadata (handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 if (err)
1243 goto journal_error;
1244 brelse (bh2);
1245 dxtrace(dx_show_index ("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001247
1248journal_error:
1249 brelse(*bh);
1250 brelse(bh2);
1251 *bh = NULL;
1252 ext4_std_error(dir->i_sb, err);
1253errout:
1254 *error = err;
1255 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257
1258/*
1259 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1260 * it points to a directory entry which is guaranteed to be large
1261 * enough for new directory entry. If de is NULL, then
1262 * add_dirent_to_buf will attempt search the directory block for
1263 * space. It will return -ENOSPC if no space is available, and -EIO
1264 * and -EEXIST if directory entry already exists.
1265 *
1266 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1267 * all other cases bh is released.
1268 */
1269static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001270 struct inode *inode, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 struct buffer_head * bh)
1272{
1273 struct inode *dir = dentry->d_parent->d_inode;
1274 const char *name = dentry->d_name.name;
1275 int namelen = dentry->d_name.len;
1276 unsigned long offset = 0;
1277 unsigned short reclen;
1278 int nlen, rlen, err;
1279 char *top;
1280
Mingming Cao617ba132006-10-11 01:20:53 -07001281 reclen = EXT4_DIR_REC_LEN(namelen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 if (!de) {
Mingming Cao617ba132006-10-11 01:20:53 -07001283 de = (struct ext4_dir_entry_2 *)bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1285 while ((char *) de <= top) {
Mingming Cao617ba132006-10-11 01:20:53 -07001286 if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 bh, offset)) {
1288 brelse (bh);
1289 return -EIO;
1290 }
Mingming Cao617ba132006-10-11 01:20:53 -07001291 if (ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 brelse (bh);
1293 return -EEXIST;
1294 }
Mingming Cao617ba132006-10-11 01:20:53 -07001295 nlen = EXT4_DIR_REC_LEN(de->name_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001296 rlen = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 if ((de->inode? rlen - nlen: rlen) >= reclen)
1298 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001299 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 offset += rlen;
1301 }
1302 if ((char *) de > top)
1303 return -ENOSPC;
1304 }
1305 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001306 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001308 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309 brelse(bh);
1310 return err;
1311 }
1312
1313 /* By now the buffer is marked for journaling */
Mingming Cao617ba132006-10-11 01:20:53 -07001314 nlen = EXT4_DIR_REC_LEN(de->name_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001315 rlen = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 if (de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001317 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
Jan Karaa72d7f82008-01-28 23:58:27 -05001318 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
1319 de->rec_len = ext4_rec_len_to_disk(nlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320 de = de1;
1321 }
Mingming Cao617ba132006-10-11 01:20:53 -07001322 de->file_type = EXT4_FT_UNKNOWN;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 if (inode) {
1324 de->inode = cpu_to_le32(inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001325 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326 } else
1327 de->inode = 0;
1328 de->name_len = namelen;
1329 memcpy (de->name, name, namelen);
1330 /*
1331 * XXX shouldn't update any times until successful
1332 * completion of syscall, but too many callers depend
1333 * on this.
1334 *
1335 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001336 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337 * recovery deletes the inode, so the worst that can
1338 * happen is that the times are slightly out of date
1339 * and/or different from the directory change time.
1340 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001341 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001342 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001343 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001344 ext4_mark_inode_dirty(handle, dir);
1345 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1346 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001348 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 brelse(bh);
1350 return 0;
1351}
1352
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001353/*
1354 * This converts a one block unindexed directory to a 3 block indexed
1355 * directory, and adds the dentry to the indexed directory.
1356 */
1357static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1358 struct inode *inode, struct buffer_head *bh)
1359{
1360 struct inode *dir = dentry->d_parent->d_inode;
1361 const char *name = dentry->d_name.name;
1362 int namelen = dentry->d_name.len;
1363 struct buffer_head *bh2;
1364 struct dx_root *root;
1365 struct dx_frame frames[2], *frame;
1366 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001367 struct ext4_dir_entry_2 *de, *de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 char *data1, *top;
1369 unsigned len;
1370 int retval;
1371 unsigned blocksize;
1372 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001373 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374 struct fake_dirent *fde;
1375
1376 blocksize = dir->i_sb->s_blocksize;
1377 dxtrace(printk("Creating index\n"));
Mingming Cao617ba132006-10-11 01:20:53 -07001378 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001380 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 brelse(bh);
1382 return retval;
1383 }
1384 root = (struct dx_root *) bh->b_data;
1385
Mingming Cao617ba132006-10-11 01:20:53 -07001386 bh2 = ext4_append (handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 if (!(bh2)) {
1388 brelse(bh);
1389 return retval;
1390 }
Mingming Cao617ba132006-10-11 01:20:53 -07001391 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001392 data1 = bh2->b_data;
1393
1394 /* The 0th block becomes the root, move the dirents out */
1395 fde = &root->dotdot;
Jan Karaa72d7f82008-01-28 23:58:27 -05001396 de = (struct ext4_dir_entry_2 *)((char *)fde +
1397 ext4_rec_len_from_disk(fde->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398 len = ((char *) root) + blocksize - (char *) de;
1399 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001400 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 top = data1 + len;
Jan Karaa72d7f82008-01-28 23:58:27 -05001402 while ((char *)(de2 = ext4_next_entry(de)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 de = de2;
Jan Karaa72d7f82008-01-28 23:58:27 -05001404 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001405 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001406 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Jan Karaa72d7f82008-01-28 23:58:27 -05001407 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 memset (&root->info, 0, sizeof(root->info));
1409 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001410 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 entries = root->entries;
1412 dx_set_block (entries, 1);
1413 dx_set_count (entries, 1);
1414 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1415
1416 /* Initialize as for dx_probe */
1417 hinfo.hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -07001418 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1419 ext4fs_dirhash(name, namelen, &hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420 frame = frames;
1421 frame->entries = entries;
1422 frame->at = entries;
1423 frame->bh = bh;
1424 bh = bh2;
1425 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1426 dx_release (frames);
1427 if (!(de))
1428 return retval;
1429
1430 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1431}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001432
1433/*
Mingming Cao617ba132006-10-11 01:20:53 -07001434 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001435 *
1436 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07001437 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 *
1439 * NOTE!! The inode part of 'de' is left at 0 - which means you
1440 * may not sleep between calling this and putting something into
1441 * the entry, as someone else might have used it while you slept.
1442 */
Mingming Cao617ba132006-10-11 01:20:53 -07001443static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444 struct inode *inode)
1445{
1446 struct inode *dir = dentry->d_parent->d_inode;
1447 unsigned long offset;
1448 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001449 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 struct super_block * sb;
1451 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001454 ext4_lblk_t block, blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455
1456 sb = dir->i_sb;
1457 blocksize = sb->s_blocksize;
1458 if (!dentry->d_name.len)
1459 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001460 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001461 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 if (!retval || (retval != ERR_BAD_DX_DIR))
1463 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001464 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07001466 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 blocks = dir->i_size >> sb->s_blocksize_bits;
1469 for (block = 0, offset = 0; block < blocks; block++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001470 bh = ext4_bread(handle, dir, block, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 if(!bh)
1472 return retval;
1473 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1474 if (retval != -ENOSPC)
1475 return retval;
1476
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001477 if (blocks == 1 && !dx_fallback &&
Mingming Cao617ba132006-10-11 01:20:53 -07001478 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001479 return make_indexed_dir(handle, dentry, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 brelse(bh);
1481 }
Mingming Cao617ba132006-10-11 01:20:53 -07001482 bh = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 if (!bh)
1484 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001485 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 de->inode = 0;
Jan Karaa72d7f82008-01-28 23:58:27 -05001487 de->rec_len = ext4_rec_len_to_disk(blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1489}
1490
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001491/*
1492 * Returns 0 for success, or a negative error value
1493 */
Mingming Cao617ba132006-10-11 01:20:53 -07001494static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 struct inode *inode)
1496{
1497 struct dx_frame frames[2], *frame;
1498 struct dx_entry *entries, *at;
1499 struct dx_hash_info hinfo;
1500 struct buffer_head * bh;
1501 struct inode *dir = dentry->d_parent->d_inode;
1502 struct super_block * sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001503 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 int err;
1505
1506 frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
1507 if (!frame)
1508 return err;
1509 entries = frame->entries;
1510 at = frame->at;
1511
Mingming Cao617ba132006-10-11 01:20:53 -07001512 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 goto cleanup;
1514
1515 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001516 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001517 if (err)
1518 goto journal_error;
1519
1520 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1521 if (err != -ENOSPC) {
1522 bh = NULL;
1523 goto cleanup;
1524 }
1525
1526 /* Block full, should compress but for now just split */
1527 dxtrace(printk("using %u of %u node entries\n",
1528 dx_get_count(entries), dx_get_limit(entries)));
1529 /* Need to split index? */
1530 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001531 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 unsigned icount = dx_get_count(entries);
1533 int levels = frame - frames;
1534 struct dx_entry *entries2;
1535 struct dx_node *node2;
1536 struct buffer_head *bh2;
1537
1538 if (levels && (dx_get_count(frames->entries) ==
1539 dx_get_limit(frames->entries))) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001540 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541 "Directory index full!");
1542 err = -ENOSPC;
1543 goto cleanup;
1544 }
Mingming Cao617ba132006-10-11 01:20:53 -07001545 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 if (!(bh2))
1547 goto cleanup;
1548 node2 = (struct dx_node *)(bh2->b_data);
1549 entries2 = node2->entries;
Jan Karaa72d7f82008-01-28 23:58:27 -05001550 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 node2->fake.inode = 0;
1552 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001553 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 if (err)
1555 goto journal_error;
1556 if (levels) {
1557 unsigned icount1 = icount/2, icount2 = icount - icount1;
1558 unsigned hash2 = dx_get_hash(entries + icount1);
1559 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1560
1561 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07001562 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 frames[0].bh);
1564 if (err)
1565 goto journal_error;
1566
1567 memcpy ((char *) entries2, (char *) (entries + icount1),
1568 icount2 * sizeof(struct dx_entry));
1569 dx_set_count (entries, icount1);
1570 dx_set_count (entries2, icount2);
1571 dx_set_limit (entries2, dx_node_limit(dir));
1572
1573 /* Which index block gets the new entry? */
1574 if (at - entries >= icount1) {
1575 frame->at = at = at - entries - icount1 + entries2;
1576 frame->entries = entries = entries2;
1577 swap(frame->bh, bh2);
1578 }
1579 dx_insert_block (frames + 0, hash2, newblock);
1580 dxtrace(dx_show_index ("node", frames[1].entries));
1581 dxtrace(dx_show_index ("node",
1582 ((struct dx_node *) bh2->b_data)->entries));
Mingming Cao617ba132006-10-11 01:20:53 -07001583 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 if (err)
1585 goto journal_error;
1586 brelse (bh2);
1587 } else {
1588 dxtrace(printk("Creating second level index...\n"));
1589 memcpy((char *) entries2, (char *) entries,
1590 icount * sizeof(struct dx_entry));
1591 dx_set_limit(entries2, dx_node_limit(dir));
1592
1593 /* Set up root */
1594 dx_set_count(entries, 1);
1595 dx_set_block(entries + 0, newblock);
1596 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1597
1598 /* Add new access path frame */
1599 frame = frames + 1;
1600 frame->at = at = at - entries + entries2;
1601 frame->entries = entries = entries2;
1602 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07001603 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604 frame->bh);
1605 if (err)
1606 goto journal_error;
1607 }
Mingming Cao617ba132006-10-11 01:20:53 -07001608 ext4_journal_dirty_metadata(handle, frames[0].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609 }
1610 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1611 if (!de)
1612 goto cleanup;
1613 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1614 bh = NULL;
1615 goto cleanup;
1616
1617journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07001618 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619cleanup:
1620 if (bh)
1621 brelse(bh);
1622 dx_release(frames);
1623 return err;
1624}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625
1626/*
Mingming Cao617ba132006-10-11 01:20:53 -07001627 * ext4_delete_entry deletes a directory entry by merging it with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001628 * previous entry
1629 */
Mingming Cao617ba132006-10-11 01:20:53 -07001630static int ext4_delete_entry (handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 struct inode * dir,
Mingming Cao617ba132006-10-11 01:20:53 -07001632 struct ext4_dir_entry_2 * de_del,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001633 struct buffer_head * bh)
1634{
Mingming Cao617ba132006-10-11 01:20:53 -07001635 struct ext4_dir_entry_2 * de, * pde;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001636 int i;
1637
1638 i = 0;
1639 pde = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001640 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641 while (i < bh->b_size) {
Mingming Cao617ba132006-10-11 01:20:53 -07001642 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 return -EIO;
1644 if (de == de_del) {
1645 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001646 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05001648 pde->rec_len = ext4_rec_len_to_disk(
1649 ext4_rec_len_from_disk(pde->rec_len) +
1650 ext4_rec_len_from_disk(de->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 else
1652 de->inode = 0;
1653 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001654 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1655 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 return 0;
1657 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001658 i += ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 pde = de;
Jan Karaa72d7f82008-01-28 23:58:27 -05001660 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001661 }
1662 return -ENOENT;
1663}
1664
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001665/*
1666 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
1667 * since this indicates that nlinks count was previously 1.
1668 */
1669static void ext4_inc_count(handle_t *handle, struct inode *inode)
1670{
1671 inc_nlink(inode);
1672 if (is_dx(inode) && inode->i_nlink > 1) {
1673 /* limit is 16-bit i_links_count */
1674 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
1675 inode->i_nlink = 1;
1676 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
1677 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
1678 }
1679 }
1680}
1681
1682/*
1683 * If a directory had nlink == 1, then we should let it be 1. This indicates
1684 * directory has >EXT4_LINK_MAX subdirs.
1685 */
1686static void ext4_dec_count(handle_t *handle, struct inode *inode)
1687{
1688 drop_nlink(inode);
1689 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
1690 inc_nlink(inode);
1691}
1692
1693
Mingming Cao617ba132006-10-11 01:20:53 -07001694static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695 struct dentry *dentry, struct inode *inode)
1696{
Mingming Cao617ba132006-10-11 01:20:53 -07001697 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001699 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 d_instantiate(dentry, inode);
1701 return 0;
1702 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08001703 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704 iput(inode);
1705 return err;
1706}
1707
1708/*
1709 * By the time this is called, we already have created
1710 * the directory cache entry for the new file, but it
1711 * is so far negative - it has no inode.
1712 *
1713 * If the create succeeds, we fill in the inode information
1714 * with d_instantiate().
1715 */
Mingming Cao617ba132006-10-11 01:20:53 -07001716static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 struct nameidata *nd)
1718{
1719 handle_t *handle;
1720 struct inode * inode;
1721 int err, retries = 0;
1722
1723retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001724 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1725 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1726 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 if (IS_ERR(handle))
1728 return PTR_ERR(handle);
1729
1730 if (IS_DIRSYNC(dir))
1731 handle->h_sync = 1;
1732
Mingming Cao617ba132006-10-11 01:20:53 -07001733 inode = ext4_new_inode (handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001734 err = PTR_ERR(inode);
1735 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001736 inode->i_op = &ext4_file_inode_operations;
1737 inode->i_fop = &ext4_file_operations;
1738 ext4_set_aops(inode);
1739 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001740 }
Mingming Cao617ba132006-10-11 01:20:53 -07001741 ext4_journal_stop(handle);
1742 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 goto retry;
1744 return err;
1745}
1746
Mingming Cao617ba132006-10-11 01:20:53 -07001747static int ext4_mknod (struct inode * dir, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 int mode, dev_t rdev)
1749{
1750 handle_t *handle;
1751 struct inode *inode;
1752 int err, retries = 0;
1753
1754 if (!new_valid_dev(rdev))
1755 return -EINVAL;
1756
1757retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001758 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1759 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1760 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001761 if (IS_ERR(handle))
1762 return PTR_ERR(handle);
1763
1764 if (IS_DIRSYNC(dir))
1765 handle->h_sync = 1;
1766
Mingming Cao617ba132006-10-11 01:20:53 -07001767 inode = ext4_new_inode (handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768 err = PTR_ERR(inode);
1769 if (!IS_ERR(inode)) {
1770 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07001771#ifdef CONFIG_EXT4DEV_FS_XATTR
1772 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001774 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775 }
Mingming Cao617ba132006-10-11 01:20:53 -07001776 ext4_journal_stop(handle);
1777 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001778 goto retry;
1779 return err;
1780}
1781
Mingming Cao617ba132006-10-11 01:20:53 -07001782static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783{
1784 handle_t *handle;
1785 struct inode * inode;
1786 struct buffer_head * dir_block;
Mingming Cao617ba132006-10-11 01:20:53 -07001787 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001788 int err, retries = 0;
1789
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001790 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791 return -EMLINK;
1792
1793retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001794 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1795 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1796 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001797 if (IS_ERR(handle))
1798 return PTR_ERR(handle);
1799
1800 if (IS_DIRSYNC(dir))
1801 handle->h_sync = 1;
1802
Mingming Cao617ba132006-10-11 01:20:53 -07001803 inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001804 err = PTR_ERR(inode);
1805 if (IS_ERR(inode))
1806 goto out_stop;
1807
Mingming Cao617ba132006-10-11 01:20:53 -07001808 inode->i_op = &ext4_dir_inode_operations;
1809 inode->i_fop = &ext4_dir_operations;
1810 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1811 dir_block = ext4_bread (handle, inode, 0, 1, &err);
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05001812 if (!dir_block)
1813 goto out_clear_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 BUFFER_TRACE(dir_block, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001815 ext4_journal_get_write_access(handle, dir_block);
1816 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 de->inode = cpu_to_le32(inode->i_ino);
1818 de->name_len = 1;
Jan Karaa72d7f82008-01-28 23:58:27 -05001819 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001820 strcpy (de->name, ".");
Mingming Cao617ba132006-10-11 01:20:53 -07001821 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
Jan Karaa72d7f82008-01-28 23:58:27 -05001822 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 de->inode = cpu_to_le32(dir->i_ino);
Jan Karaa72d7f82008-01-28 23:58:27 -05001824 de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
1825 EXT4_DIR_REC_LEN(1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 de->name_len = 2;
1827 strcpy (de->name, "..");
Mingming Cao617ba132006-10-11 01:20:53 -07001828 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001829 inode->i_nlink = 2;
Mingming Cao617ba132006-10-11 01:20:53 -07001830 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
1831 ext4_journal_dirty_metadata(handle, dir_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001832 brelse (dir_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001833 ext4_mark_inode_dirty(handle, inode);
1834 err = ext4_add_entry (handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001835 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05001836out_clear_inode:
1837 clear_nlink(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07001838 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001839 iput (inode);
1840 goto out_stop;
1841 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001842 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001843 ext4_update_dx_flag(dir);
1844 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 d_instantiate(dentry, inode);
1846out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07001847 ext4_journal_stop(handle);
1848 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849 goto retry;
1850 return err;
1851}
1852
1853/*
1854 * routine to check that the specified directory is empty (for rmdir)
1855 */
1856static int empty_dir (struct inode * inode)
1857{
1858 unsigned long offset;
1859 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001860 struct ext4_dir_entry_2 * de, * de1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 struct super_block * sb;
1862 int err = 0;
1863
1864 sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001865 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1866 !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001867 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -04001868 ext4_error(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 "error %d reading directory #%lu offset 0",
1870 err, inode->i_ino);
1871 else
Harvey Harrison46e665e2008-04-17 10:38:59 -04001872 ext4_warning(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001873 "bad directory (dir #%lu) - no data block",
1874 inode->i_ino);
1875 return 1;
1876 }
Mingming Cao617ba132006-10-11 01:20:53 -07001877 de = (struct ext4_dir_entry_2 *) bh->b_data;
Jan Karaa72d7f82008-01-28 23:58:27 -05001878 de1 = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 if (le32_to_cpu(de->inode) != inode->i_ino ||
1880 !le32_to_cpu(de1->inode) ||
1881 strcmp (".", de->name) ||
1882 strcmp ("..", de1->name)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001883 ext4_warning (inode->i_sb, "empty_dir",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884 "bad directory (dir #%lu) - no `.' or `..'",
1885 inode->i_ino);
1886 brelse (bh);
1887 return 1;
1888 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001889 offset = ext4_rec_len_from_disk(de->rec_len) +
1890 ext4_rec_len_from_disk(de1->rec_len);
1891 de = ext4_next_entry(de1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001892 while (offset < inode->i_size ) {
1893 if (!bh ||
1894 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1895 err = 0;
1896 brelse (bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001897 bh = ext4_bread (NULL, inode,
1898 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001899 if (!bh) {
1900 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -04001901 ext4_error(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001902 "error %d reading directory"
1903 " #%lu offset %lu",
1904 err, inode->i_ino, offset);
1905 offset += sb->s_blocksize;
1906 continue;
1907 }
Mingming Cao617ba132006-10-11 01:20:53 -07001908 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909 }
Mingming Cao617ba132006-10-11 01:20:53 -07001910 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1911 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001912 sb->s_blocksize);
1913 offset = (offset | (sb->s_blocksize - 1)) + 1;
1914 continue;
1915 }
1916 if (le32_to_cpu(de->inode)) {
1917 brelse (bh);
1918 return 0;
1919 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001920 offset += ext4_rec_len_from_disk(de->rec_len);
1921 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 }
1923 brelse (bh);
1924 return 1;
1925}
1926
Mingming Cao617ba132006-10-11 01:20:53 -07001927/* ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 * such inodes, starting at the superblock, in case we crash before the
1929 * file is closed/deleted, or in case the inode truncate spans multiple
1930 * transactions and the last transaction is not recovered after a crash.
1931 *
1932 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07001933 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001934 */
Mingming Cao617ba132006-10-11 01:20:53 -07001935int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936{
1937 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001938 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001939 int err = 0, rc;
1940
1941 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001942 if (!list_empty(&EXT4_I(inode)->i_orphan))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001943 goto out_unlock;
1944
1945 /* Orphan handling is only valid for files with data blocks
1946 * being truncated, or files being unlinked. */
1947
1948 /* @@@ FIXME: Observation from aviro:
Mingming Cao617ba132006-10-11 01:20:53 -07001949 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1950 * here (on lock_super()), so race with ext4_link() which might bump
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001951 * ->i_nlink. For, say it, character device. Not a regular file,
1952 * not a directory, not a symlink and ->i_nlink > 0.
1953 */
1954 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1955 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1956
Mingming Cao617ba132006-10-11 01:20:53 -07001957 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1958 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001959 if (err)
1960 goto out_unlock;
1961
Mingming Cao617ba132006-10-11 01:20:53 -07001962 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963 if (err)
1964 goto out_unlock;
1965
1966 /* Insert this inode at the head of the on-disk orphan list... */
Mingming Cao617ba132006-10-11 01:20:53 -07001967 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
1968 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1969 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1970 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 if (!err)
1972 err = rc;
1973
1974 /* Only add to the head of the in-memory list if all the
1975 * previous operations succeeded. If the orphan_add is going to
1976 * fail (possibly taking the journal offline), we can't risk
1977 * leaving the inode on the orphan list: stray orphan-list
1978 * entries can cause panics at unmount time.
1979 *
1980 * This is safe: on error we're going to ignore the orphan list
1981 * anyway on the next recovery. */
1982 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001983 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001984
1985 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
1986 jbd_debug(4, "orphan inode %lu will point to %d\n",
1987 inode->i_ino, NEXT_ORPHAN(inode));
1988out_unlock:
1989 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001990 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991 return err;
1992}
1993
1994/*
Mingming Cao617ba132006-10-11 01:20:53 -07001995 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001996 * of such inodes stored on disk, because it is finally being cleaned up.
1997 */
Mingming Cao617ba132006-10-11 01:20:53 -07001998int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999{
2000 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002001 struct ext4_inode_info *ei = EXT4_I(inode);
2002 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 unsigned long ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002004 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002005 int err = 0;
2006
2007 lock_super(inode->i_sb);
2008 if (list_empty(&ei->i_orphan)) {
2009 unlock_super(inode->i_sb);
2010 return 0;
2011 }
2012
2013 ino_next = NEXT_ORPHAN(inode);
2014 prev = ei->i_orphan.prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002015 sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016
2017 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2018
2019 list_del_init(&ei->i_orphan);
2020
2021 /* If we're on an error path, we may not have a valid
2022 * transaction handle with which to update the orphan list on
2023 * disk, but we still need to remove the inode from the linked
2024 * list in memory. */
2025 if (!handle)
2026 goto out;
2027
Mingming Cao617ba132006-10-11 01:20:53 -07002028 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 if (err)
2030 goto out_err;
2031
2032 if (prev == &sbi->s_orphan) {
2033 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2034 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002035 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036 if (err)
2037 goto out_brelse;
2038 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002039 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002040 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002041 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002043 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044
2045 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2046 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002047 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002048 if (err)
2049 goto out_brelse;
2050 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002051 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 }
2053 if (err)
2054 goto out_brelse;
2055 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002056 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057
2058out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002059 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060out:
2061 unlock_super(inode->i_sb);
2062 return err;
2063
2064out_brelse:
2065 brelse(iloc.bh);
2066 goto out_err;
2067}
2068
Mingming Cao617ba132006-10-11 01:20:53 -07002069static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070{
2071 int retval;
2072 struct inode * inode;
2073 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002074 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 handle_t *handle;
2076
2077 /* Initialize quotas before so that eventual writes go in
2078 * separate transaction */
2079 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002080 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 if (IS_ERR(handle))
2082 return PTR_ERR(handle);
2083
2084 retval = -ENOENT;
Mingming Cao617ba132006-10-11 01:20:53 -07002085 bh = ext4_find_entry (dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086 if (!bh)
2087 goto end_rmdir;
2088
2089 if (IS_DIRSYNC(dir))
2090 handle->h_sync = 1;
2091
2092 inode = dentry->d_inode;
2093
2094 retval = -EIO;
2095 if (le32_to_cpu(de->inode) != inode->i_ino)
2096 goto end_rmdir;
2097
2098 retval = -ENOTEMPTY;
2099 if (!empty_dir (inode))
2100 goto end_rmdir;
2101
Mingming Cao617ba132006-10-11 01:20:53 -07002102 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002103 if (retval)
2104 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002105 if (!EXT4_DIR_LINK_EMPTY(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07002106 ext4_warning (inode->i_sb, "ext4_rmdir",
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002107 "empty directory has too many links (%d)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 inode->i_nlink);
2109 inode->i_version++;
2110 clear_nlink(inode);
2111 /* There's no need to set i_disksize: the fact that i_nlink is
2112 * zero will ensure that the right thing happens during any
2113 * recovery. */
2114 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002115 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002116 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002117 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002118 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002119 ext4_update_dx_flag(dir);
2120 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121
2122end_rmdir:
Mingming Cao617ba132006-10-11 01:20:53 -07002123 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002124 brelse (bh);
2125 return retval;
2126}
2127
Mingming Cao617ba132006-10-11 01:20:53 -07002128static int ext4_unlink(struct inode * dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129{
2130 int retval;
2131 struct inode * inode;
2132 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002133 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 handle_t *handle;
2135
2136 /* Initialize quotas before so that eventual writes go
2137 * in separate transaction */
2138 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002139 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 if (IS_ERR(handle))
2141 return PTR_ERR(handle);
2142
2143 if (IS_DIRSYNC(dir))
2144 handle->h_sync = 1;
2145
2146 retval = -ENOENT;
Mingming Cao617ba132006-10-11 01:20:53 -07002147 bh = ext4_find_entry (dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 if (!bh)
2149 goto end_unlink;
2150
2151 inode = dentry->d_inode;
2152
2153 retval = -EIO;
2154 if (le32_to_cpu(de->inode) != inode->i_ino)
2155 goto end_unlink;
2156
2157 if (!inode->i_nlink) {
Mingming Cao617ba132006-10-11 01:20:53 -07002158 ext4_warning (inode->i_sb, "ext4_unlink",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 "Deleting nonexistent file (%lu), %d",
2160 inode->i_ino, inode->i_nlink);
2161 inode->i_nlink = 1;
2162 }
Mingming Cao617ba132006-10-11 01:20:53 -07002163 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 if (retval)
2165 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04002166 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002167 ext4_update_dx_flag(dir);
2168 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002169 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002170 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002171 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002172 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002173 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 retval = 0;
2175
2176end_unlink:
Mingming Cao617ba132006-10-11 01:20:53 -07002177 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002178 brelse (bh);
2179 return retval;
2180}
2181
Mingming Cao617ba132006-10-11 01:20:53 -07002182static int ext4_symlink (struct inode * dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183 struct dentry *dentry, const char * symname)
2184{
2185 handle_t *handle;
2186 struct inode * inode;
2187 int l, err, retries = 0;
2188
2189 l = strlen(symname)+1;
2190 if (l > dir->i_sb->s_blocksize)
2191 return -ENAMETOOLONG;
2192
2193retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002194 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2195 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2196 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 if (IS_ERR(handle))
2198 return PTR_ERR(handle);
2199
2200 if (IS_DIRSYNC(dir))
2201 handle->h_sync = 1;
2202
Mingming Cao617ba132006-10-11 01:20:53 -07002203 inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204 err = PTR_ERR(inode);
2205 if (IS_ERR(inode))
2206 goto out_stop;
2207
Mingming Cao617ba132006-10-11 01:20:53 -07002208 if (l > sizeof (EXT4_I(inode)->i_data)) {
2209 inode->i_op = &ext4_symlink_inode_operations;
2210 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002212 * page_symlink() calls into ext4_prepare/commit_write.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 * We have a transaction open. All is sweetness. It also sets
2214 * i_size in generic_commit_write().
2215 */
2216 err = __page_symlink(inode, symname, l,
2217 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
2218 if (err) {
Theodore Ts'o825f1482008-02-15 15:00:38 -05002219 clear_nlink(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002220 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221 iput (inode);
2222 goto out_stop;
2223 }
2224 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04002225 /* clear the extent format for fast symlink */
2226 EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
Mingming Cao617ba132006-10-11 01:20:53 -07002227 inode->i_op = &ext4_fast_symlink_inode_operations;
2228 memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229 inode->i_size = l-1;
2230 }
Mingming Cao617ba132006-10-11 01:20:53 -07002231 EXT4_I(inode)->i_disksize = inode->i_size;
2232 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07002234 ext4_journal_stop(handle);
2235 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236 goto retry;
2237 return err;
2238}
2239
Mingming Cao617ba132006-10-11 01:20:53 -07002240static int ext4_link (struct dentry * old_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 struct inode * dir, struct dentry *dentry)
2242{
2243 handle_t *handle;
2244 struct inode *inode = old_dentry->d_inode;
2245 int err, retries = 0;
2246
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002247 if (EXT4_DIR_LINK_MAX(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002248 return -EMLINK;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002249
Eric Sandeen2988a772007-02-10 01:46:16 -08002250 /*
2251 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2252 * otherwise has the potential to corrupt the orphan inode list.
2253 */
2254 if (inode->i_nlink == 0)
2255 return -ENOENT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256
2257retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002258 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2259 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 if (IS_ERR(handle))
2261 return PTR_ERR(handle);
2262
2263 if (IS_DIRSYNC(dir))
2264 handle->h_sync = 1;
2265
Kalpak Shahef7f3832007-07-18 09:15:20 -04002266 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002267 ext4_inc_count(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268 atomic_inc(&inode->i_count);
2269
Mingming Cao617ba132006-10-11 01:20:53 -07002270 err = ext4_add_nondir(handle, dentry, inode);
2271 ext4_journal_stop(handle);
2272 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002273 goto retry;
2274 return err;
2275}
2276
2277#define PARENT_INO(buffer) \
Jan Karaa72d7f82008-01-28 23:58:27 -05002278 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279
2280/*
2281 * Anybody can rename anything with this: the permission checks are left to the
2282 * higher-level routines.
2283 */
Mingming Cao617ba132006-10-11 01:20:53 -07002284static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 struct inode * new_dir,struct dentry *new_dentry)
2286{
2287 handle_t *handle;
2288 struct inode * old_inode, * new_inode;
2289 struct buffer_head * old_bh, * new_bh, * dir_bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002290 struct ext4_dir_entry_2 * old_de, * new_de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002291 int retval;
2292
2293 old_bh = new_bh = dir_bh = NULL;
2294
2295 /* Initialize quotas before so that eventual writes go
2296 * in separate transaction */
2297 if (new_dentry->d_inode)
2298 DQUOT_INIT(new_dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002299 handle = ext4_journal_start(old_dir, 2 *
2300 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2301 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002302 if (IS_ERR(handle))
2303 return PTR_ERR(handle);
2304
2305 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2306 handle->h_sync = 1;
2307
Mingming Cao617ba132006-10-11 01:20:53 -07002308 old_bh = ext4_find_entry (old_dentry, &old_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 /*
2310 * Check for inode number is _not_ due to possible IO errors.
2311 * We might rmdir the source, keep it as pwd of some process
2312 * and merrily kill the link to whatever was created under the
2313 * same name. Goodbye sticky bit ;-<
2314 */
2315 old_inode = old_dentry->d_inode;
2316 retval = -ENOENT;
2317 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2318 goto end_rename;
2319
2320 new_inode = new_dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07002321 new_bh = ext4_find_entry (new_dentry, &new_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002322 if (new_bh) {
2323 if (!new_inode) {
2324 brelse (new_bh);
2325 new_bh = NULL;
2326 }
2327 }
2328 if (S_ISDIR(old_inode->i_mode)) {
2329 if (new_inode) {
2330 retval = -ENOTEMPTY;
2331 if (!empty_dir (new_inode))
2332 goto end_rename;
2333 }
2334 retval = -EIO;
Mingming Cao617ba132006-10-11 01:20:53 -07002335 dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002336 if (!dir_bh)
2337 goto end_rename;
2338 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2339 goto end_rename;
2340 retval = -EMLINK;
2341 if (!new_inode && new_dir!=old_dir &&
Mingming Cao617ba132006-10-11 01:20:53 -07002342 new_dir->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343 goto end_rename;
2344 }
2345 if (!new_bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07002346 retval = ext4_add_entry (handle, new_dentry, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002347 if (retval)
2348 goto end_rename;
2349 } else {
2350 BUFFER_TRACE(new_bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07002351 ext4_journal_get_write_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 new_de->inode = cpu_to_le32(old_inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002353 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2354 EXT4_FEATURE_INCOMPAT_FILETYPE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002355 new_de->file_type = old_de->file_type;
2356 new_dir->i_version++;
Jan Kara53b7e9f2008-04-29 22:02:11 -04002357 new_dir->i_ctime = new_dir->i_mtime =
2358 ext4_current_time(new_dir);
2359 ext4_mark_inode_dirty(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002360 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
2361 ext4_journal_dirty_metadata(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002362 brelse(new_bh);
2363 new_bh = NULL;
2364 }
2365
2366 /*
2367 * Like most other Unix systems, set the ctime for inodes on a
2368 * rename.
2369 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04002370 old_inode->i_ctime = ext4_current_time(old_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002371 ext4_mark_inode_dirty(handle, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002372
2373 /*
2374 * ok, that's it
2375 */
2376 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2377 old_de->name_len != old_dentry->d_name.len ||
2378 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
Mingming Cao617ba132006-10-11 01:20:53 -07002379 (retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 old_de, old_bh)) == -ENOENT) {
2381 /* old_de could have moved from under us during htree split, so
2382 * make sure that we are deleting the right entry. We might
2383 * also be pointing to a stale entry in the unused part of
2384 * old_bh so just checking inum and the name isn't enough. */
2385 struct buffer_head *old_bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002386 struct ext4_dir_entry_2 *old_de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387
Mingming Cao617ba132006-10-11 01:20:53 -07002388 old_bh2 = ext4_find_entry(old_dentry, &old_de2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002389 if (old_bh2) {
Mingming Cao617ba132006-10-11 01:20:53 -07002390 retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391 old_de2, old_bh2);
2392 brelse(old_bh2);
2393 }
2394 }
2395 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002396 ext4_warning(old_dir->i_sb, "ext4_rename",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002397 "Deleting old file (%lu), %d, error=%d",
2398 old_dir->i_ino, old_dir->i_nlink, retval);
2399 }
2400
2401 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002402 ext4_dec_count(handle, new_inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002403 new_inode->i_ctime = ext4_current_time(new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002404 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002405 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002406 ext4_update_dx_flag(old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002407 if (dir_bh) {
2408 BUFFER_TRACE(dir_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002409 ext4_journal_get_write_access(handle, dir_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002411 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
2412 ext4_journal_dirty_metadata(handle, dir_bh);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002413 ext4_dec_count(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002415 /* checked empty_dir above, can't have another parent,
Theodore Ts'o825f1482008-02-15 15:00:38 -05002416 * ext4_dec_count() won't work for many-linked dirs */
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002417 new_inode->i_nlink = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 } else {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002419 ext4_inc_count(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002420 ext4_update_dx_flag(new_dir);
2421 ext4_mark_inode_dirty(handle, new_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422 }
2423 }
Mingming Cao617ba132006-10-11 01:20:53 -07002424 ext4_mark_inode_dirty(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425 if (new_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07002426 ext4_mark_inode_dirty(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002427 if (!new_inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002428 ext4_orphan_add(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 }
2430 retval = 0;
2431
2432end_rename:
2433 brelse (dir_bh);
2434 brelse (old_bh);
2435 brelse (new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002436 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002437 return retval;
2438}
2439
2440/*
2441 * directories can handle most operations...
2442 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08002443const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002444 .create = ext4_create,
2445 .lookup = ext4_lookup,
2446 .link = ext4_link,
2447 .unlink = ext4_unlink,
2448 .symlink = ext4_symlink,
2449 .mkdir = ext4_mkdir,
2450 .rmdir = ext4_rmdir,
2451 .mknod = ext4_mknod,
2452 .rename = ext4_rename,
2453 .setattr = ext4_setattr,
2454#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002455 .setxattr = generic_setxattr,
2456 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002457 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 .removexattr = generic_removexattr,
2459#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002460 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002461};
2462
Arjan van de Ven754661f2007-02-12 00:55:38 -08002463const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002464 .setattr = ext4_setattr,
2465#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002466 .setxattr = generic_setxattr,
2467 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002468 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 .removexattr = generic_removexattr,
2470#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002471 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002472};