blob: 5b93a7d94d4256132fcb7a9ed13ec8c6213510c2 [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);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400154static 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);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400162static struct dx_frame *dx_probe(const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163 struct inode *dir,
164 struct dx_hash_info *hinfo,
165 struct dx_frame *frame,
166 int *err);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400167static void dx_release(struct dx_frame *frames);
168static int dx_make_map(struct ext4_dir_entry_2 *de, int size,
169 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400171static 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);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400173static 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);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400180static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
181 const struct qstr *d_name,
182 struct ext4_dir_entry_2 **res_dir,
183 int *err);
Mingming Cao617ba132006-10-11 01:20:53 -0700184static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 struct inode *inode);
186
187/*
Li Zefanf795e142008-07-11 19:27:31 -0400188 * p is at least 6 bytes before the end of page
189 */
190static inline struct ext4_dir_entry_2 *
191ext4_next_entry(struct ext4_dir_entry_2 *p)
192{
193 return (struct ext4_dir_entry_2 *)((char *)p +
194 ext4_rec_len_from_disk(p->rec_len));
195}
196
197/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 * Future: use high four bits of block for coalesce-on-delete flags
199 * Mask them off for now.
200 */
201
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500202static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203{
204 return le32_to_cpu(entry->block) & 0x00ffffff;
205}
206
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500207static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208{
209 entry->block = cpu_to_le32(value);
210}
211
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400212static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213{
214 return le32_to_cpu(entry->hash);
215}
216
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400217static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218{
219 entry->hash = cpu_to_le32(value);
220}
221
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400222static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223{
224 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
225}
226
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400227static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228{
229 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
230}
231
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400232static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233{
234 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
235}
236
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400237static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238{
239 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
240}
241
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400242static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243{
Mingming Cao617ba132006-10-11 01:20:53 -0700244 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
245 EXT4_DIR_REC_LEN(2) - infosize;
Li Zefand9c769b2008-07-11 19:27:31 -0400246 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247}
248
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400249static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250{
Mingming Cao617ba132006-10-11 01:20:53 -0700251 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Li Zefand9c769b2008-07-11 19:27:31 -0400252 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253}
254
255/*
256 * Debug
257 */
258#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400259static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260{
Andrew Morton63f57932006-10-11 01:21:24 -0700261 int i, n = dx_get_count (entries);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400262 printk(KERN_DEBUG "%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700263 for (i = 0; i < n; i++) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400264 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500265 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700266 }
267 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268}
269
270struct stats
271{
272 unsigned names;
273 unsigned space;
274 unsigned bcount;
275};
276
Mingming Cao617ba132006-10-11 01:20:53 -0700277static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 int size, int show_names)
279{
280 unsigned names = 0, space = 0;
281 char *base = (char *) de;
282 struct dx_hash_info h = *hinfo;
283
284 printk("names: ");
285 while ((char *) de < base + size)
286 {
287 if (de->inode)
288 {
289 if (show_names)
290 {
291 int len = de->name_len;
292 char *name = de->name;
293 while (len--) printk("%c", *name++);
Mingming Cao617ba132006-10-11 01:20:53 -0700294 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 printk(":%x.%u ", h.hash,
296 ((char *) de - base));
297 }
Mingming Cao617ba132006-10-11 01:20:53 -0700298 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 names++;
300 }
Jan Karaa72d7f82008-01-28 23:58:27 -0500301 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 }
303 printk("(%i)\n", names);
304 return (struct stats) { names, space, 1 };
305}
306
307struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
308 struct dx_entry *entries, int levels)
309{
310 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400311 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 unsigned bcount = 0;
313 struct buffer_head *bh;
314 int err;
315 printk("%i indexed blocks...\n", count);
316 for (i = 0; i < count; i++, entries++)
317 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500318 ext4_lblk_t block = dx_get_block(entries);
319 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
321 struct stats stats;
322 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Mingming Cao617ba132006-10-11 01:20:53 -0700323 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 stats = levels?
325 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Mingming Cao617ba132006-10-11 01:20:53 -0700326 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 names += stats.names;
328 space += stats.space;
329 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400330 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 }
332 if (bcount)
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400333 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
334 levels ? "" : " ", names, space/bcount,
335 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 return (struct stats) { names, space, bcount};
337}
338#endif /* DX_DEBUG */
339
340/*
341 * Probe for a directory leaf block to search.
342 *
343 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
344 * error in the directory index, and the caller should fall back to
345 * searching the directory normally. The callers of dx_probe **MUST**
346 * check for this error code, and make sure it never gets reflected
347 * back to userspace.
348 */
349static struct dx_frame *
Theodore Ts'of702ba02008-09-22 15:21:01 -0400350dx_probe(const struct qstr *d_name, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
352{
353 unsigned count, indirect;
354 struct dx_entry *at, *entries, *p, *q, *m;
355 struct dx_root *root;
356 struct buffer_head *bh;
357 struct dx_frame *frame = frame_in;
358 u32 hash;
359
360 frame->bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700361 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 goto fail;
363 root = (struct dx_root *) bh->b_data;
364 if (root->info.hash_version != DX_HASH_TEA &&
365 root->info.hash_version != DX_HASH_HALF_MD4 &&
366 root->info.hash_version != DX_HASH_LEGACY) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400367 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 "Unrecognised inode hash code %d",
369 root->info.hash_version);
370 brelse(bh);
371 *err = ERR_BAD_DX_DIR;
372 goto fail;
373 }
374 hinfo->hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -0700375 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400376 if (d_name)
377 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378 hash = hinfo->hash;
379
380 if (root->info.unused_flags & 1) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400381 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382 "Unimplemented inode hash flags: %#06x",
383 root->info.unused_flags);
384 brelse(bh);
385 *err = ERR_BAD_DX_DIR;
386 goto fail;
387 }
388
389 if ((indirect = root->info.indirect_levels) > 1) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400390 ext4_warning(dir->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700391 "Unimplemented inode hash depth: %#06x",
392 root->info.indirect_levels);
393 brelse(bh);
394 *err = ERR_BAD_DX_DIR;
395 goto fail;
396 }
397
398 entries = (struct dx_entry *) (((char *)&root->info) +
399 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700400
401 if (dx_get_limit(entries) != dx_root_limit(dir,
402 root->info.info_length)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400403 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700404 "dx entry: limit != root limit");
405 brelse(bh);
406 *err = ERR_BAD_DX_DIR;
407 goto fail;
408 }
409
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400410 dxtrace(printk("Look up %x", hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411 while (1)
412 {
413 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700414 if (!count || count > dx_get_limit(entries)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400415 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700416 "dx entry: no count or count > limit");
417 brelse(bh);
418 *err = ERR_BAD_DX_DIR;
419 goto fail2;
420 }
421
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422 p = entries + 1;
423 q = entries + count - 1;
424 while (p <= q)
425 {
426 m = p + (q - p)/2;
427 dxtrace(printk("."));
428 if (dx_get_hash(m) > hash)
429 q = m - 1;
430 else
431 p = m + 1;
432 }
433
434 if (0) // linear search cross check
435 {
436 unsigned n = count - 1;
437 at = entries;
438 while (n--)
439 {
440 dxtrace(printk(","));
441 if (dx_get_hash(++at) > hash)
442 {
443 at--;
444 break;
445 }
446 }
447 assert (at == p - 1);
448 }
449
450 at = p - 1;
451 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
452 frame->bh = bh;
453 frame->entries = entries;
454 frame->at = at;
455 if (!indirect--) return frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700456 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457 goto fail2;
458 at = entries = ((struct dx_node *) bh->b_data)->entries;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700459 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400460 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700461 "dx entry: limit != node limit");
462 brelse(bh);
463 *err = ERR_BAD_DX_DIR;
464 goto fail2;
465 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700467 frame->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 }
469fail2:
470 while (frame >= frame_in) {
471 brelse(frame->bh);
472 frame--;
473 }
474fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700475 if (*err == ERR_BAD_DX_DIR)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400476 ext4_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700477 "Corrupt dir inode %ld, running e2fsck is "
478 "recommended.", dir->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 return NULL;
480}
481
482static void dx_release (struct dx_frame *frames)
483{
484 if (frames[0].bh == NULL)
485 return;
486
487 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
488 brelse(frames[1].bh);
489 brelse(frames[0].bh);
490}
491
492/*
493 * This function increments the frame pointer to search the next leaf
494 * block, and reads in the necessary intervening nodes if the search
495 * should be necessary. Whether or not the search is necessary is
496 * controlled by the hash parameter. If the hash value is even, then
497 * the search is only continued if the next block starts with that
498 * hash value. This is used if we are searching for a specific file.
499 *
500 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
501 *
502 * This function returns 1 if the caller should continue to search,
503 * or 0 if it should not. If there is an error reading one of the
504 * index blocks, it will a negative error code.
505 *
506 * If start_hash is non-null, it will be filled in with the starting
507 * hash of the next page.
508 */
Mingming Cao617ba132006-10-11 01:20:53 -0700509static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 struct dx_frame *frame,
511 struct dx_frame *frames,
512 __u32 *start_hash)
513{
514 struct dx_frame *p;
515 struct buffer_head *bh;
516 int err, num_frames = 0;
517 __u32 bhash;
518
519 p = frame;
520 /*
521 * Find the next leaf page by incrementing the frame pointer.
522 * If we run out of entries in the interior node, loop around and
523 * increment pointer in the parent node. When we break out of
524 * this loop, num_frames indicates the number of interior
525 * nodes need to be read.
526 */
527 while (1) {
528 if (++(p->at) < p->entries + dx_get_count(p->entries))
529 break;
530 if (p == frames)
531 return 0;
532 num_frames++;
533 p--;
534 }
535
536 /*
537 * If the hash is 1, then continue only if the next page has a
538 * continuation hash of any value. This is used for readdir
539 * handling. Otherwise, check to see if the hash matches the
540 * desired contiuation hash. If it doesn't, return since
541 * there's no point to read in the successive index pages.
542 */
543 bhash = dx_get_hash(p->at);
544 if (start_hash)
545 *start_hash = bhash;
546 if ((hash & 1) == 0) {
547 if ((bhash & ~1) != hash)
548 return 0;
549 }
550 /*
551 * If the hash is HASH_NB_ALWAYS, we always go to the next
552 * block so no check is necessary
553 */
554 while (num_frames--) {
Mingming Cao617ba132006-10-11 01:20:53 -0700555 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 0, &err)))
557 return err; /* Failure */
558 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400559 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 p->bh = bh;
561 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
562 }
563 return 1;
564}
565
566
567/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 * This function fills a red-black tree with information from a
569 * directory block. It returns the number directory entries loaded
570 * into the tree. If there is an error it is returned in err.
571 */
572static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500573 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574 struct dx_hash_info *hinfo,
575 __u32 start_hash, __u32 start_minor_hash)
576{
577 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700578 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 int err, count = 0;
580
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500581 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
582 (unsigned long)block));
Mingming Cao617ba132006-10-11 01:20:53 -0700583 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584 return err;
585
Mingming Cao617ba132006-10-11 01:20:53 -0700586 de = (struct ext4_dir_entry_2 *) bh->b_data;
587 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700589 EXT4_DIR_REC_LEN(0));
590 for (; de < top; de = ext4_next_entry(de)) {
Eric Sandeene6c40212006-12-06 20:36:28 -0800591 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
592 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
593 +((char *)de - bh->b_data))) {
594 /* On error, skip the f_pos to the next block. */
595 dir_file->f_pos = (dir_file->f_pos |
596 (dir->i_sb->s_blocksize - 1)) + 1;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400597 brelse(bh);
Eric Sandeene6c40212006-12-06 20:36:28 -0800598 return count;
599 }
Mingming Cao617ba132006-10-11 01:20:53 -0700600 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601 if ((hinfo->hash < start_hash) ||
602 ((hinfo->hash == start_hash) &&
603 (hinfo->minor_hash < start_minor_hash)))
604 continue;
605 if (de->inode == 0)
606 continue;
Mingming Cao617ba132006-10-11 01:20:53 -0700607 if ((err = ext4_htree_store_dirent(dir_file,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608 hinfo->hash, hinfo->minor_hash, de)) != 0) {
609 brelse(bh);
610 return err;
611 }
612 count++;
613 }
614 brelse(bh);
615 return count;
616}
617
618
619/*
620 * This function fills a red-black tree with information from a
621 * directory. We start scanning the directory in hash order, starting
622 * at start_hash and start_minor_hash.
623 *
624 * This function returns the number of entries inserted into the tree,
625 * or a negative error code.
626 */
Mingming Cao617ba132006-10-11 01:20:53 -0700627int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 __u32 start_minor_hash, __u32 *next_hash)
629{
630 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -0700631 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 struct dx_frame frames[2], *frame;
633 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500634 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500636 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637 __u32 hashval;
638
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400639 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
640 start_hash, start_minor_hash));
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800641 dir = dir_file->f_path.dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700642 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
643 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
644 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
646 start_hash, start_minor_hash);
647 *next_hash = ~0;
648 return count;
649 }
650 hinfo.hash = start_hash;
651 hinfo.minor_hash = 0;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400652 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 if (!frame)
654 return err;
655
656 /* Add '.' and '..' from the htree header */
657 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -0700658 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
659 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 goto errout;
661 count++;
662 }
663 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700664 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
665 de = ext4_next_entry(de);
666 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 goto errout;
668 count++;
669 }
670
671 while (1) {
672 block = dx_get_block(frame->at);
673 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
674 start_hash, start_minor_hash);
675 if (ret < 0) {
676 err = ret;
677 goto errout;
678 }
679 count += ret;
680 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -0700681 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 frame, frames, &hashval);
683 *next_hash = hashval;
684 if (ret < 0) {
685 err = ret;
686 goto errout;
687 }
688 /*
689 * Stop if: (a) there are no more entries, or
690 * (b) we have inserted at least one entry and the
691 * next hash value is not a continuation
692 */
693 if ((ret == 0) ||
694 (count && ((hashval & 1) == 0)))
695 break;
696 }
697 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400698 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
699 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 return count;
701errout:
702 dx_release(frames);
703 return (err);
704}
705
706
707/*
708 * Directory block splitting, compacting
709 */
710
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700711/*
712 * Create map of hash values, offsets, and sizes, stored at end of block.
713 * Returns number of entries mapped.
714 */
Mingming Cao617ba132006-10-11 01:20:53 -0700715static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
717{
718 int count = 0;
719 char *base = (char *) de;
720 struct dx_hash_info h = *hinfo;
721
722 while ((char *) de < base + size)
723 {
724 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -0700725 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 map_tail--;
727 map_tail->hash = h.hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700728 map_tail->offs = (u16) ((char *) de - base);
729 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 count++;
731 cond_resched();
732 }
733 /* XXX: do we need to check rec_len == 0 case? -Chris */
Jan Karaa72d7f82008-01-28 23:58:27 -0500734 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735 }
736 return count;
737}
738
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700739/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740static void dx_sort_map (struct dx_map_entry *map, unsigned count)
741{
Andrew Morton63f57932006-10-11 01:21:24 -0700742 struct dx_map_entry *p, *q, *top = map + count - 1;
743 int more;
744 /* Combsort until bubble sort doesn't suck */
745 while (count > 2) {
746 count = count*10/13;
747 if (count - 9 < 2) /* 9, 10 -> 11 */
748 count = 11;
749 for (p = top, q = p - count; q >= map; p--, q--)
750 if (p->hash < q->hash)
751 swap(*p, *q);
752 }
753 /* Garden variety bubble sort */
754 do {
755 more = 0;
756 q = top;
757 while (q-- > map) {
758 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 continue;
Andrew Morton63f57932006-10-11 01:21:24 -0700760 swap(*(q+1), *q);
761 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 }
763 } while(more);
764}
765
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500766static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767{
768 struct dx_entry *entries = frame->entries;
769 struct dx_entry *old = frame->at, *new = old + 1;
770 int count = dx_get_count(entries);
771
772 assert(count < dx_get_limit(entries));
773 assert(old < entries + count);
774 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
775 dx_set_hash(new, hash);
776 dx_set_block(new, block);
777 dx_set_count(entries, count + 1);
778}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779
Mingming Cao617ba132006-10-11 01:20:53 -0700780static void ext4_update_dx_flag(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781{
Mingming Cao617ba132006-10-11 01:20:53 -0700782 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
783 EXT4_FEATURE_COMPAT_DIR_INDEX))
784 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785}
786
787/*
Mingming Cao617ba132006-10-11 01:20:53 -0700788 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 *
Mingming Cao617ba132006-10-11 01:20:53 -0700790 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 * `de != NULL' is guaranteed by caller.
792 */
Mingming Cao617ba132006-10-11 01:20:53 -0700793static inline int ext4_match (int len, const char * const name,
794 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795{
796 if (len != de->name_len)
797 return 0;
798 if (!de->inode)
799 return 0;
800 return !memcmp(name, de->name, len);
801}
802
803/*
804 * Returns 0 if not found, -1 on failure, and 1 on success
805 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400806static inline int search_dirblock(struct buffer_head *bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 struct inode *dir,
Theodore Ts'of702ba02008-09-22 15:21:01 -0400808 const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 unsigned long offset,
Mingming Cao617ba132006-10-11 01:20:53 -0700810 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811{
Mingming Cao617ba132006-10-11 01:20:53 -0700812 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 char * dlimit;
814 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400815 const char *name = d_name->name;
816 int namelen = d_name->len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817
Mingming Cao617ba132006-10-11 01:20:53 -0700818 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 dlimit = bh->b_data + dir->i_sb->s_blocksize;
820 while ((char *) de < dlimit) {
821 /* this code is executed quadratically often */
822 /* do minimal checking `by hand' */
823
824 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -0700825 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826 /* found a match - just to be sure, do a full check */
Mingming Cao617ba132006-10-11 01:20:53 -0700827 if (!ext4_check_dir_entry("ext4_find_entry",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828 dir, de, bh, offset))
829 return -1;
830 *res_dir = de;
831 return 1;
832 }
833 /* prevent looping on a bad block */
Jan Karaa72d7f82008-01-28 23:58:27 -0500834 de_len = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700835 if (de_len <= 0)
836 return -1;
837 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -0700838 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 }
840 return 0;
841}
842
843
844/*
Mingming Cao617ba132006-10-11 01:20:53 -0700845 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 *
847 * finds an entry in the specified directory with the wanted name. It
848 * returns the cache buffer in which the entry was found, and the entry
849 * itself (as a parameter - res_dir). It does NOT read the inode of the
850 * entry - you'll have to do that yourself if you want to.
851 *
852 * The returned buffer_head has ->b_count elevated. The caller is expected
853 * to brelse() it when appropriate.
854 */
Theodore Ts'of702ba02008-09-22 15:21:01 -0400855static struct buffer_head * ext4_find_entry (struct inode *dir,
856 const struct qstr *d_name,
Mingming Cao617ba132006-10-11 01:20:53 -0700857 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400859 struct super_block *sb;
860 struct buffer_head *bh_use[NAMEI_RA_SIZE];
861 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500862 ext4_lblk_t start, block, b;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 int ra_max = 0; /* Number of bh's in the readahead
864 buffer, bh_use[] */
865 int ra_ptr = 0; /* Current index into readahead
866 buffer */
867 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500868 ext4_lblk_t nblocks;
869 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 int namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871
872 *res_dir = NULL;
873 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400874 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -0700875 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 if (is_dx(dir)) {
Theodore Ts'of702ba02008-09-22 15:21:01 -0400878 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 /*
880 * On success, or if the error was file not found,
881 * return. Otherwise, fall back to doing a search the
882 * old fashioned way.
883 */
884 if (bh || (err != ERR_BAD_DX_DIR))
885 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400886 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
887 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 }
Mingming Cao617ba132006-10-11 01:20:53 -0700889 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
890 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 if (start >= nblocks)
892 start = 0;
893 block = start;
894restart:
895 do {
896 /*
897 * We deal with the read-ahead logic here.
898 */
899 if (ra_ptr >= ra_max) {
900 /* Refill the readahead buffer */
901 ra_ptr = 0;
902 b = block;
903 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
904 /*
905 * Terminate if we reach the end of the
906 * directory and must wrap, or if our
907 * search has finished at this block.
908 */
909 if (b >= nblocks || (num && block == start)) {
910 bh_use[ra_max] = NULL;
911 break;
912 }
913 num++;
Mingming Cao617ba132006-10-11 01:20:53 -0700914 bh = ext4_getblk(NULL, dir, b++, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 bh_use[ra_max] = bh;
916 if (bh)
917 ll_rw_block(READ_META, 1, &bh);
918 }
919 }
920 if ((bh = bh_use[ra_ptr++]) == NULL)
921 goto next;
922 wait_on_buffer(bh);
923 if (!buffer_uptodate(bh)) {
924 /* read error, skip block & hope for the best */
Harvey Harrison46e665e2008-04-17 10:38:59 -0400925 ext4_error(sb, __func__, "reading directory #%lu "
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500926 "offset %lu", dir->i_ino,
927 (unsigned long)block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 brelse(bh);
929 goto next;
930 }
Theodore Ts'of702ba02008-09-22 15:21:01 -0400931 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -0700932 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700934 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 ret = bh;
936 goto cleanup_and_exit;
937 } else {
938 brelse(bh);
939 if (i < 0)
940 goto cleanup_and_exit;
941 }
942 next:
943 if (++block >= nblocks)
944 block = 0;
945 } while (block != start);
946
947 /*
948 * If the directory has grown while we were searching, then
949 * search the last part of the directory before giving up.
950 */
951 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -0700952 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 if (block < nblocks) {
954 start = 0;
955 goto restart;
956 }
957
958cleanup_and_exit:
959 /* Clean up the read-ahead blocks */
960 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400961 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 return ret;
963}
964
Theodore Ts'of702ba02008-09-22 15:21:01 -0400965static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Mingming Cao617ba132006-10-11 01:20:53 -0700966 struct ext4_dir_entry_2 **res_dir, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967{
968 struct super_block * sb;
969 struct dx_hash_info hinfo;
970 u32 hash;
971 struct dx_frame frames[2], *frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700972 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500974 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700975 int retval;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400976 int namelen = d_name->len;
977 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978
979 sb = dir->i_sb;
980 /* NFS may look up ".." - look at dx_root directory block */
981 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
Theodore Ts'of702ba02008-09-22 15:21:01 -0400982 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 return NULL;
984 } else {
985 frame = frames;
986 frame->bh = NULL; /* for dx_release() */
987 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
988 dx_set_block(frame->at, 0); /* dx_root block is 0 */
989 }
990 hash = hinfo.hash;
991 do {
992 block = dx_get_block(frame->at);
Mingming Cao617ba132006-10-11 01:20:53 -0700993 if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994 goto errout;
Mingming Cao617ba132006-10-11 01:20:53 -0700995 de = (struct ext4_dir_entry_2 *) bh->b_data;
996 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
997 EXT4_DIR_REC_LEN(0));
Duane Griffinf3b35f02008-07-11 19:27:31 -0400998 for (; de < top; de = ext4_next_entry(de)) {
999 int off = (block << EXT4_BLOCK_SIZE_BITS(sb))
1000 + ((char *) de - bh->b_data);
1001
1002 if (!ext4_check_dir_entry(__func__, dir, de, bh, off)) {
1003 brelse(bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001004 *err = ERR_BAD_DX_DIR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 goto errout;
1006 }
Duane Griffinf3b35f02008-07-11 19:27:31 -04001007
1008 if (ext4_match(namelen, name, de)) {
1009 *res_dir = de;
1010 dx_release(frames);
1011 return bh;
1012 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001014 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 /* Check to see if we should continue to search */
Mingming Cao617ba132006-10-11 01:20:53 -07001016 retval = ext4_htree_next_block(dir, hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017 frames, NULL);
1018 if (retval < 0) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001019 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 "error reading index page in directory #%lu",
1021 dir->i_ino);
1022 *err = retval;
1023 goto errout;
1024 }
1025 } while (retval == 1);
1026
1027 *err = -ENOENT;
1028errout:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001029 dxtrace(printk(KERN_DEBUG "%s not found\n", name));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 dx_release (frames);
1031 return NULL;
1032}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001034static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001036 struct inode *inode;
1037 struct ext4_dir_entry_2 *de;
1038 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039
Mingming Cao617ba132006-10-11 01:20:53 -07001040 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 return ERR_PTR(-ENAMETOOLONG);
1042
Theodore Ts'of702ba02008-09-22 15:21:01 -04001043 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001044 inode = NULL;
1045 if (bh) {
1046 unsigned long ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001047 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001048 if (!ext4_valid_inum(dir->i_sb, ino)) {
1049 ext4_error(dir->i_sb, "ext4_lookup",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 "bad inode number: %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001051 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001052 }
David Howells1d1fe1e2008-02-07 00:15:37 -08001053 inode = ext4_iget(dir->i_sb, ino);
1054 if (IS_ERR(inode))
1055 return ERR_CAST(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 }
1057 return d_splice_alias(inode, dentry);
1058}
1059
1060
Mingming Cao617ba132006-10-11 01:20:53 -07001061struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062{
1063 unsigned long ino;
1064 struct dentry *parent;
1065 struct inode *inode;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001066 static const struct qstr dotdot = {
1067 .name = "..",
1068 .len = 2,
1069 };
Mingming Cao617ba132006-10-11 01:20:53 -07001070 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 struct buffer_head *bh;
1072
Theodore Ts'of702ba02008-09-22 15:21:01 -04001073 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 inode = NULL;
1075 if (!bh)
1076 return ERR_PTR(-ENOENT);
1077 ino = le32_to_cpu(de->inode);
1078 brelse(bh);
1079
Mingming Cao617ba132006-10-11 01:20:53 -07001080 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1081 ext4_error(child->d_inode->i_sb, "ext4_get_parent",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 "bad inode number: %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001083 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001084 }
1085
Christoph Hellwig44003722008-08-11 15:49:04 +02001086 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087}
1088
1089#define S_SHIFT 12
Mingming Cao617ba132006-10-11 01:20:53 -07001090static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1091 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1092 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1093 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1094 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1095 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1096 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1097 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098};
1099
Mingming Cao617ba132006-10-11 01:20:53 -07001100static inline void ext4_set_de_type(struct super_block *sb,
1101 struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 umode_t mode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001103 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1104 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105}
1106
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001107/*
1108 * Move count entries from end of map between two memory locations.
1109 * Returns pointer to last entry moved.
1110 */
Mingming Cao617ba132006-10-11 01:20:53 -07001111static struct ext4_dir_entry_2 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1113{
1114 unsigned rec_len = 0;
1115
1116 while (count--) {
Mingming Cao617ba132006-10-11 01:20:53 -07001117 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
1118 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001120 ((struct ext4_dir_entry_2 *) to)->rec_len =
Jan Karaa72d7f82008-01-28 23:58:27 -05001121 ext4_rec_len_to_disk(rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001122 de->inode = 0;
1123 map++;
1124 to += rec_len;
1125 }
Mingming Cao617ba132006-10-11 01:20:53 -07001126 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127}
1128
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001129/*
1130 * Compact each dir entry in the range to the minimal rec_len.
1131 * Returns pointer to last entry in range.
1132 */
Mingming Cao617ba132006-10-11 01:20:53 -07001133static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134{
Mingming Cao617ba132006-10-11 01:20:53 -07001135 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 unsigned rec_len = 0;
1137
1138 prev = to = de;
1139 while ((char*)de < base + size) {
Jan Karaa72d7f82008-01-28 23:58:27 -05001140 next = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001142 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 if (de > to)
1144 memmove(to, de, rec_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001145 to->rec_len = ext4_rec_len_to_disk(rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001147 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 }
1149 de = next;
1150 }
1151 return prev;
1152}
1153
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001154/*
1155 * Split a full leaf block to make room for a new dir entry.
1156 * Allocate a new block, and move entries so that they are approx. equally full.
1157 * Returns pointer to de in block into which the new entry will be inserted.
1158 */
Mingming Cao617ba132006-10-11 01:20:53 -07001159static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 struct buffer_head **bh,struct dx_frame *frame,
1161 struct dx_hash_info *hinfo, int *error)
1162{
1163 unsigned blocksize = dir->i_sb->s_blocksize;
1164 unsigned count, continued;
1165 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001166 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 u32 hash2;
1168 struct dx_map_entry *map;
1169 char *data1 = (*bh)->b_data, *data2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001170 unsigned split, move, size, i;
Mingming Cao617ba132006-10-11 01:20:53 -07001171 struct ext4_dir_entry_2 *de = NULL, *de2;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001172 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001174 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 if (!(bh2)) {
1176 brelse(*bh);
1177 *bh = NULL;
1178 goto errout;
1179 }
1180
1181 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001182 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001183 if (err)
1184 goto journal_error;
1185
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001187 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 if (err)
1189 goto journal_error;
1190
1191 data2 = bh2->b_data;
1192
1193 /* create map in the end of data2 block */
1194 map = (struct dx_map_entry *) (data2 + blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001195 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 blocksize, hinfo, map);
1197 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001198 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001199 /* Split the existing block in the middle, size-wise */
1200 size = 0;
1201 move = 0;
1202 for (i = count-1; i >= 0; i--) {
1203 /* is more than half of this entry in 2nd half of the block? */
1204 if (size + map[i].size/2 > blocksize/2)
1205 break;
1206 size += map[i].size;
1207 move++;
1208 }
1209 /* map index at which we will split */
1210 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 hash2 = map[split].hash;
1212 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001213 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1214 (unsigned long)dx_get_block(frame->at),
1215 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216
1217 /* Fancy dance to stay within two buffers */
1218 de2 = dx_move_dirents(data1, data2, map + split, count - split);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001219 de = dx_pack_dirents(data1, blocksize);
Jan Karaa72d7f82008-01-28 23:58:27 -05001220 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
1221 de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
Mingming Cao617ba132006-10-11 01:20:53 -07001222 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1223 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224
1225 /* Which block gets the new entry? */
1226 if (hinfo->hash >= hash2)
1227 {
1228 swap(*bh, bh2);
1229 de = de2;
1230 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001231 dx_insert_block(frame, hash2 + continued, newblock);
1232 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 if (err)
1234 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001235 err = ext4_journal_dirty_metadata(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 if (err)
1237 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001238 brelse(bh2);
1239 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001240 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001241
1242journal_error:
1243 brelse(*bh);
1244 brelse(bh2);
1245 *bh = NULL;
1246 ext4_std_error(dir->i_sb, err);
1247errout:
1248 *error = err;
1249 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001250}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251
1252/*
1253 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1254 * it points to a directory entry which is guaranteed to be large
1255 * enough for new directory entry. If de is NULL, then
1256 * add_dirent_to_buf will attempt search the directory block for
1257 * space. It will return -ENOSPC if no space is available, and -EIO
1258 * and -EEXIST if directory entry already exists.
1259 *
1260 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1261 * all other cases bh is released.
1262 */
1263static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001264 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001265 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266{
1267 struct inode *dir = dentry->d_parent->d_inode;
1268 const char *name = dentry->d_name.name;
1269 int namelen = dentry->d_name.len;
1270 unsigned long offset = 0;
1271 unsigned short reclen;
1272 int nlen, rlen, err;
1273 char *top;
1274
Mingming Cao617ba132006-10-11 01:20:53 -07001275 reclen = EXT4_DIR_REC_LEN(namelen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 if (!de) {
Mingming Cao617ba132006-10-11 01:20:53 -07001277 de = (struct ext4_dir_entry_2 *)bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1279 while ((char *) de <= top) {
Mingming Cao617ba132006-10-11 01:20:53 -07001280 if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 bh, offset)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001282 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 return -EIO;
1284 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001285 if (ext4_match(namelen, name, de)) {
1286 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 return -EEXIST;
1288 }
Mingming Cao617ba132006-10-11 01:20:53 -07001289 nlen = EXT4_DIR_REC_LEN(de->name_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001290 rlen = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 if ((de->inode? rlen - nlen: rlen) >= reclen)
1292 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001293 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 offset += rlen;
1295 }
1296 if ((char *) de > top)
1297 return -ENOSPC;
1298 }
1299 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001300 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001302 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303 brelse(bh);
1304 return err;
1305 }
1306
1307 /* By now the buffer is marked for journaling */
Mingming Cao617ba132006-10-11 01:20:53 -07001308 nlen = EXT4_DIR_REC_LEN(de->name_len);
Jan Karaa72d7f82008-01-28 23:58:27 -05001309 rlen = ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 if (de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001311 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
Jan Karaa72d7f82008-01-28 23:58:27 -05001312 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
1313 de->rec_len = ext4_rec_len_to_disk(nlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001314 de = de1;
1315 }
Mingming Cao617ba132006-10-11 01:20:53 -07001316 de->file_type = EXT4_FT_UNKNOWN;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001317 if (inode) {
1318 de->inode = cpu_to_le32(inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001319 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320 } else
1321 de->inode = 0;
1322 de->name_len = namelen;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001323 memcpy(de->name, name, namelen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 /*
1325 * XXX shouldn't update any times until successful
1326 * completion of syscall, but too many callers depend
1327 * on this.
1328 *
1329 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001330 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 * recovery deletes the inode, so the worst that can
1332 * happen is that the times are slightly out of date
1333 * and/or different from the directory change time.
1334 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001335 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001336 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001338 ext4_mark_inode_dirty(handle, dir);
1339 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1340 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001342 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001343 brelse(bh);
1344 return 0;
1345}
1346
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347/*
1348 * This converts a one block unindexed directory to a 3 block indexed
1349 * directory, and adds the dentry to the indexed directory.
1350 */
1351static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1352 struct inode *inode, struct buffer_head *bh)
1353{
1354 struct inode *dir = dentry->d_parent->d_inode;
1355 const char *name = dentry->d_name.name;
1356 int namelen = dentry->d_name.len;
1357 struct buffer_head *bh2;
1358 struct dx_root *root;
1359 struct dx_frame frames[2], *frame;
1360 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001361 struct ext4_dir_entry_2 *de, *de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362 char *data1, *top;
1363 unsigned len;
1364 int retval;
1365 unsigned blocksize;
1366 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001367 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 struct fake_dirent *fde;
1369
1370 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001371 dxtrace(printk(KERN_DEBUG "Creating index\n"));
Mingming Cao617ba132006-10-11 01:20:53 -07001372 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001373 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001374 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001375 brelse(bh);
1376 return retval;
1377 }
1378 root = (struct dx_root *) bh->b_data;
1379
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001380 bh2 = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 if (!(bh2)) {
1382 brelse(bh);
1383 return retval;
1384 }
Mingming Cao617ba132006-10-11 01:20:53 -07001385 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001386 data1 = bh2->b_data;
1387
1388 /* The 0th block becomes the root, move the dirents out */
1389 fde = &root->dotdot;
Jan Karaa72d7f82008-01-28 23:58:27 -05001390 de = (struct ext4_dir_entry_2 *)((char *)fde +
1391 ext4_rec_len_from_disk(fde->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001392 len = ((char *) root) + blocksize - (char *) de;
1393 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001394 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 top = data1 + len;
Jan Karaa72d7f82008-01-28 23:58:27 -05001396 while ((char *)(de2 = ext4_next_entry(de)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001397 de = de2;
Jan Karaa72d7f82008-01-28 23:58:27 -05001398 de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001399 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001400 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Jan Karaa72d7f82008-01-28 23:58:27 -05001401 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001402 memset (&root->info, 0, sizeof(root->info));
1403 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001404 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001405 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001406 dx_set_block(entries, 1);
1407 dx_set_count(entries, 1);
1408 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409
1410 /* Initialize as for dx_probe */
1411 hinfo.hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -07001412 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1413 ext4fs_dirhash(name, namelen, &hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 frame = frames;
1415 frame->entries = entries;
1416 frame->at = entries;
1417 frame->bh = bh;
1418 bh = bh2;
1419 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1420 dx_release (frames);
1421 if (!(de))
1422 return retval;
1423
1424 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1425}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426
1427/*
Mingming Cao617ba132006-10-11 01:20:53 -07001428 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 *
1430 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07001431 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001432 *
1433 * NOTE!! The inode part of 'de' is left at 0 - which means you
1434 * may not sleep between calling this and putting something into
1435 * the entry, as someone else might have used it while you slept.
1436 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001437static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1438 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439{
1440 struct inode *dir = dentry->d_parent->d_inode;
1441 unsigned long offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001442 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001443 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001444 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001448 ext4_lblk_t block, blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449
1450 sb = dir->i_sb;
1451 blocksize = sb->s_blocksize;
1452 if (!dentry->d_name.len)
1453 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001455 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 if (!retval || (retval != ERR_BAD_DX_DIR))
1457 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001458 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07001460 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 blocks = dir->i_size >> sb->s_blocksize_bits;
1463 for (block = 0, offset = 0; block < blocks; block++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001464 bh = ext4_bread(handle, dir, block, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 if(!bh)
1466 return retval;
1467 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1468 if (retval != -ENOSPC)
1469 return retval;
1470
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 if (blocks == 1 && !dx_fallback &&
Mingming Cao617ba132006-10-11 01:20:53 -07001472 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473 return make_indexed_dir(handle, dentry, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474 brelse(bh);
1475 }
Mingming Cao617ba132006-10-11 01:20:53 -07001476 bh = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001477 if (!bh)
1478 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001479 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 de->inode = 0;
Jan Karaa72d7f82008-01-28 23:58:27 -05001481 de->rec_len = ext4_rec_len_to_disk(blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1483}
1484
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485/*
1486 * Returns 0 for success, or a negative error value
1487 */
Mingming Cao617ba132006-10-11 01:20:53 -07001488static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 struct inode *inode)
1490{
1491 struct dx_frame frames[2], *frame;
1492 struct dx_entry *entries, *at;
1493 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001494 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001495 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001496 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001497 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 int err;
1499
Theodore Ts'of702ba02008-09-22 15:21:01 -04001500 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 if (!frame)
1502 return err;
1503 entries = frame->entries;
1504 at = frame->at;
1505
Mingming Cao617ba132006-10-11 01:20:53 -07001506 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 goto cleanup;
1508
1509 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001510 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 if (err)
1512 goto journal_error;
1513
1514 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1515 if (err != -ENOSPC) {
1516 bh = NULL;
1517 goto cleanup;
1518 }
1519
1520 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001521 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 dx_get_count(entries), dx_get_limit(entries)));
1523 /* Need to split index? */
1524 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001525 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 unsigned icount = dx_get_count(entries);
1527 int levels = frame - frames;
1528 struct dx_entry *entries2;
1529 struct dx_node *node2;
1530 struct buffer_head *bh2;
1531
1532 if (levels && (dx_get_count(frames->entries) ==
1533 dx_get_limit(frames->entries))) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001534 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535 "Directory index full!");
1536 err = -ENOSPC;
1537 goto cleanup;
1538 }
Mingming Cao617ba132006-10-11 01:20:53 -07001539 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 if (!(bh2))
1541 goto cleanup;
1542 node2 = (struct dx_node *)(bh2->b_data);
1543 entries2 = node2->entries;
Jan Karaa72d7f82008-01-28 23:58:27 -05001544 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 node2->fake.inode = 0;
1546 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001547 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001548 if (err)
1549 goto journal_error;
1550 if (levels) {
1551 unsigned icount1 = icount/2, icount2 = icount - icount1;
1552 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001553 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
1554 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555
1556 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07001557 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558 frames[0].bh);
1559 if (err)
1560 goto journal_error;
1561
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001562 memcpy((char *) entries2, (char *) (entries + icount1),
1563 icount2 * sizeof(struct dx_entry));
1564 dx_set_count(entries, icount1);
1565 dx_set_count(entries2, icount2);
1566 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567
1568 /* Which index block gets the new entry? */
1569 if (at - entries >= icount1) {
1570 frame->at = at = at - entries - icount1 + entries2;
1571 frame->entries = entries = entries2;
1572 swap(frame->bh, bh2);
1573 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001574 dx_insert_block(frames + 0, hash2, newblock);
1575 dxtrace(dx_show_index("node", frames[1].entries));
1576 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 ((struct dx_node *) bh2->b_data)->entries));
Mingming Cao617ba132006-10-11 01:20:53 -07001578 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001579 if (err)
1580 goto journal_error;
1581 brelse (bh2);
1582 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001583 dxtrace(printk(KERN_DEBUG
1584 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 memcpy((char *) entries2, (char *) entries,
1586 icount * sizeof(struct dx_entry));
1587 dx_set_limit(entries2, dx_node_limit(dir));
1588
1589 /* Set up root */
1590 dx_set_count(entries, 1);
1591 dx_set_block(entries + 0, newblock);
1592 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1593
1594 /* Add new access path frame */
1595 frame = frames + 1;
1596 frame->at = at = at - entries + entries2;
1597 frame->entries = entries = entries2;
1598 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07001599 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001600 frame->bh);
1601 if (err)
1602 goto journal_error;
1603 }
Mingming Cao617ba132006-10-11 01:20:53 -07001604 ext4_journal_dirty_metadata(handle, frames[0].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 }
1606 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1607 if (!de)
1608 goto cleanup;
1609 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1610 bh = NULL;
1611 goto cleanup;
1612
1613journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07001614 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615cleanup:
1616 if (bh)
1617 brelse(bh);
1618 dx_release(frames);
1619 return err;
1620}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621
1622/*
Mingming Cao617ba132006-10-11 01:20:53 -07001623 * ext4_delete_entry deletes a directory entry by merging it with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 * previous entry
1625 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001626static int ext4_delete_entry(handle_t *handle,
1627 struct inode *dir,
1628 struct ext4_dir_entry_2 *de_del,
1629 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001631 struct ext4_dir_entry_2 *de, *pde;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 int i;
1633
1634 i = 0;
1635 pde = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001636 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 while (i < bh->b_size) {
Mingming Cao617ba132006-10-11 01:20:53 -07001638 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 return -EIO;
1640 if (de == de_del) {
1641 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001642 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05001644 pde->rec_len = ext4_rec_len_to_disk(
1645 ext4_rec_len_from_disk(pde->rec_len) +
1646 ext4_rec_len_from_disk(de->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 else
1648 de->inode = 0;
1649 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001650 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1651 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 return 0;
1653 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001654 i += ext4_rec_len_from_disk(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001655 pde = de;
Jan Karaa72d7f82008-01-28 23:58:27 -05001656 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 }
1658 return -ENOENT;
1659}
1660
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001661/*
1662 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
1663 * since this indicates that nlinks count was previously 1.
1664 */
1665static void ext4_inc_count(handle_t *handle, struct inode *inode)
1666{
1667 inc_nlink(inode);
1668 if (is_dx(inode) && inode->i_nlink > 1) {
1669 /* limit is 16-bit i_links_count */
1670 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
1671 inode->i_nlink = 1;
1672 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
1673 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
1674 }
1675 }
1676}
1677
1678/*
1679 * If a directory had nlink == 1, then we should let it be 1. This indicates
1680 * directory has >EXT4_LINK_MAX subdirs.
1681 */
1682static void ext4_dec_count(handle_t *handle, struct inode *inode)
1683{
1684 drop_nlink(inode);
1685 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
1686 inc_nlink(inode);
1687}
1688
1689
Mingming Cao617ba132006-10-11 01:20:53 -07001690static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 struct dentry *dentry, struct inode *inode)
1692{
Mingming Cao617ba132006-10-11 01:20:53 -07001693 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001695 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696 d_instantiate(dentry, inode);
1697 return 0;
1698 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08001699 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 iput(inode);
1701 return err;
1702}
1703
1704/*
1705 * By the time this is called, we already have created
1706 * the directory cache entry for the new file, but it
1707 * is so far negative - it has no inode.
1708 *
1709 * If the create succeeds, we fill in the inode information
1710 * with d_instantiate().
1711 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001712static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
1713 struct nameidata *nd)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714{
1715 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001716 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 int err, retries = 0;
1718
1719retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001720 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1721 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1722 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 if (IS_ERR(handle))
1724 return PTR_ERR(handle);
1725
1726 if (IS_DIRSYNC(dir))
1727 handle->h_sync = 1;
1728
Mingming Cao617ba132006-10-11 01:20:53 -07001729 inode = ext4_new_inode (handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 err = PTR_ERR(inode);
1731 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001732 inode->i_op = &ext4_file_inode_operations;
1733 inode->i_fop = &ext4_file_operations;
1734 ext4_set_aops(inode);
1735 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736 }
Mingming Cao617ba132006-10-11 01:20:53 -07001737 ext4_journal_stop(handle);
1738 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 goto retry;
1740 return err;
1741}
1742
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001743static int ext4_mknod(struct inode *dir, struct dentry *dentry,
1744 int mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745{
1746 handle_t *handle;
1747 struct inode *inode;
1748 int err, retries = 0;
1749
1750 if (!new_valid_dev(rdev))
1751 return -EINVAL;
1752
1753retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001754 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1755 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1756 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001757 if (IS_ERR(handle))
1758 return PTR_ERR(handle);
1759
1760 if (IS_DIRSYNC(dir))
1761 handle->h_sync = 1;
1762
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001763 inode = ext4_new_inode(handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764 err = PTR_ERR(inode);
1765 if (!IS_ERR(inode)) {
1766 init_special_inode(inode, inode->i_mode, rdev);
Theodore Ts'o03010a32008-10-10 20:02:48 -04001767#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001768 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001770 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 }
Mingming Cao617ba132006-10-11 01:20:53 -07001772 ext4_journal_stop(handle);
1773 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 goto retry;
1775 return err;
1776}
1777
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001778static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779{
1780 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001781 struct inode *inode;
1782 struct buffer_head *dir_block;
1783 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001784 int err, retries = 0;
1785
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001786 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787 return -EMLINK;
1788
1789retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001790 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1791 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1792 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793 if (IS_ERR(handle))
1794 return PTR_ERR(handle);
1795
1796 if (IS_DIRSYNC(dir))
1797 handle->h_sync = 1;
1798
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001799 inode = ext4_new_inode(handle, dir, S_IFDIR | mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800 err = PTR_ERR(inode);
1801 if (IS_ERR(inode))
1802 goto out_stop;
1803
Mingming Cao617ba132006-10-11 01:20:53 -07001804 inode->i_op = &ext4_dir_inode_operations;
1805 inode->i_fop = &ext4_dir_operations;
1806 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001807 dir_block = ext4_bread(handle, inode, 0, 1, &err);
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05001808 if (!dir_block)
1809 goto out_clear_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001810 BUFFER_TRACE(dir_block, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001811 ext4_journal_get_write_access(handle, dir_block);
1812 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 de->inode = cpu_to_le32(inode->i_ino);
1814 de->name_len = 1;
Jan Karaa72d7f82008-01-28 23:58:27 -05001815 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001816 strcpy(de->name, ".");
Mingming Cao617ba132006-10-11 01:20:53 -07001817 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
Jan Karaa72d7f82008-01-28 23:58:27 -05001818 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 de->inode = cpu_to_le32(dir->i_ino);
Jan Karaa72d7f82008-01-28 23:58:27 -05001820 de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
1821 EXT4_DIR_REC_LEN(1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001822 de->name_len = 2;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001823 strcpy(de->name, "..");
Mingming Cao617ba132006-10-11 01:20:53 -07001824 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001825 inode->i_nlink = 2;
Mingming Cao617ba132006-10-11 01:20:53 -07001826 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
1827 ext4_journal_dirty_metadata(handle, dir_block);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001828 brelse(dir_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001829 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001830 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05001832out_clear_inode:
1833 clear_nlink(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07001834 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001835 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 goto out_stop;
1837 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001838 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001839 ext4_update_dx_flag(dir);
1840 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001841 d_instantiate(dentry, inode);
1842out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07001843 ext4_journal_stop(handle);
1844 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 goto retry;
1846 return err;
1847}
1848
1849/*
1850 * routine to check that the specified directory is empty (for rmdir)
1851 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001852static int empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001853{
1854 unsigned long offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001855 struct buffer_head *bh;
1856 struct ext4_dir_entry_2 *de, *de1;
1857 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001858 int err = 0;
1859
1860 sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001861 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001862 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -04001864 ext4_error(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 "error %d reading directory #%lu offset 0",
1866 err, inode->i_ino);
1867 else
Harvey Harrison46e665e2008-04-17 10:38:59 -04001868 ext4_warning(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 "bad directory (dir #%lu) - no data block",
1870 inode->i_ino);
1871 return 1;
1872 }
Mingming Cao617ba132006-10-11 01:20:53 -07001873 de = (struct ext4_dir_entry_2 *) bh->b_data;
Jan Karaa72d7f82008-01-28 23:58:27 -05001874 de1 = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 if (le32_to_cpu(de->inode) != inode->i_ino ||
1876 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001877 strcmp(".", de->name) ||
1878 strcmp("..", de1->name)) {
1879 ext4_warning(inode->i_sb, "empty_dir",
1880 "bad directory (dir #%lu) - no `.' or `..'",
1881 inode->i_ino);
1882 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883 return 1;
1884 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001885 offset = ext4_rec_len_from_disk(de->rec_len) +
1886 ext4_rec_len_from_disk(de1->rec_len);
1887 de = ext4_next_entry(de1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001888 while (offset < inode->i_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001889 if (!bh ||
1890 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1891 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001892 brelse(bh);
1893 bh = ext4_bread(NULL, inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001894 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001895 if (!bh) {
1896 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -04001897 ext4_error(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001898 "error %d reading directory"
1899 " #%lu offset %lu",
1900 err, inode->i_ino, offset);
1901 offset += sb->s_blocksize;
1902 continue;
1903 }
Mingming Cao617ba132006-10-11 01:20:53 -07001904 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905 }
Mingming Cao617ba132006-10-11 01:20:53 -07001906 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1907 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001908 sb->s_blocksize);
1909 offset = (offset | (sb->s_blocksize - 1)) + 1;
1910 continue;
1911 }
1912 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001913 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914 return 0;
1915 }
Jan Karaa72d7f82008-01-28 23:58:27 -05001916 offset += ext4_rec_len_from_disk(de->rec_len);
1917 de = ext4_next_entry(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001918 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001919 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 return 1;
1921}
1922
Mingming Cao617ba132006-10-11 01:20:53 -07001923/* ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001924 * such inodes, starting at the superblock, in case we crash before the
1925 * file is closed/deleted, or in case the inode truncate spans multiple
1926 * transactions and the last transaction is not recovered after a crash.
1927 *
1928 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07001929 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 */
Mingming Cao617ba132006-10-11 01:20:53 -07001931int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932{
1933 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001934 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001935 int err = 0, rc;
1936
1937 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001938 if (!list_empty(&EXT4_I(inode)->i_orphan))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001939 goto out_unlock;
1940
1941 /* Orphan handling is only valid for files with data blocks
1942 * being truncated, or files being unlinked. */
1943
1944 /* @@@ FIXME: Observation from aviro:
Mingming Cao617ba132006-10-11 01:20:53 -07001945 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1946 * here (on lock_super()), so race with ext4_link() which might bump
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001947 * ->i_nlink. For, say it, character device. Not a regular file,
1948 * not a directory, not a symlink and ->i_nlink > 0.
1949 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001950 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1951 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952
Mingming Cao617ba132006-10-11 01:20:53 -07001953 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1954 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001955 if (err)
1956 goto out_unlock;
1957
Mingming Cao617ba132006-10-11 01:20:53 -07001958 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001959 if (err)
1960 goto out_unlock;
1961
1962 /* Insert this inode at the head of the on-disk orphan list... */
Mingming Cao617ba132006-10-11 01:20:53 -07001963 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
1964 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1965 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1966 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001967 if (!err)
1968 err = rc;
1969
1970 /* Only add to the head of the in-memory list if all the
1971 * previous operations succeeded. If the orphan_add is going to
1972 * fail (possibly taking the journal offline), we can't risk
1973 * leaving the inode on the orphan list: stray orphan-list
1974 * entries can cause panics at unmount time.
1975 *
1976 * This is safe: on error we're going to ignore the orphan list
1977 * anyway on the next recovery. */
1978 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001979 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001980
1981 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
1982 jbd_debug(4, "orphan inode %lu will point to %d\n",
1983 inode->i_ino, NEXT_ORPHAN(inode));
1984out_unlock:
1985 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001986 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 return err;
1988}
1989
1990/*
Mingming Cao617ba132006-10-11 01:20:53 -07001991 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 * of such inodes stored on disk, because it is finally being cleaned up.
1993 */
Mingming Cao617ba132006-10-11 01:20:53 -07001994int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995{
1996 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07001997 struct ext4_inode_info *ei = EXT4_I(inode);
1998 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999 unsigned long ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002000 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001 int err = 0;
2002
2003 lock_super(inode->i_sb);
2004 if (list_empty(&ei->i_orphan)) {
2005 unlock_super(inode->i_sb);
2006 return 0;
2007 }
2008
2009 ino_next = NEXT_ORPHAN(inode);
2010 prev = ei->i_orphan.prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002011 sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012
2013 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2014
2015 list_del_init(&ei->i_orphan);
2016
2017 /* If we're on an error path, we may not have a valid
2018 * transaction handle with which to update the orphan list on
2019 * disk, but we still need to remove the inode from the linked
2020 * list in memory. */
2021 if (!handle)
2022 goto out;
2023
Mingming Cao617ba132006-10-11 01:20:53 -07002024 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 if (err)
2026 goto out_err;
2027
2028 if (prev == &sbi->s_orphan) {
2029 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2030 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002031 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 if (err)
2033 goto out_brelse;
2034 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002035 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002037 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002039 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002040
2041 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2042 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002043 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 if (err)
2045 goto out_brelse;
2046 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002047 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002048 }
2049 if (err)
2050 goto out_brelse;
2051 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002052 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002053
2054out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002055 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002056out:
2057 unlock_super(inode->i_sb);
2058 return err;
2059
2060out_brelse:
2061 brelse(iloc.bh);
2062 goto out_err;
2063}
2064
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002065static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066{
2067 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002068 struct inode *inode;
2069 struct buffer_head *bh;
2070 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071 handle_t *handle;
2072
2073 /* Initialize quotas before so that eventual writes go in
2074 * separate transaction */
2075 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002076 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 if (IS_ERR(handle))
2078 return PTR_ERR(handle);
2079
2080 retval = -ENOENT;
Theodore Ts'of702ba02008-09-22 15:21:01 -04002081 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 if (!bh)
2083 goto end_rmdir;
2084
2085 if (IS_DIRSYNC(dir))
2086 handle->h_sync = 1;
2087
2088 inode = dentry->d_inode;
2089
2090 retval = -EIO;
2091 if (le32_to_cpu(de->inode) != inode->i_ino)
2092 goto end_rmdir;
2093
2094 retval = -ENOTEMPTY;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002095 if (!empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002096 goto end_rmdir;
2097
Mingming Cao617ba132006-10-11 01:20:53 -07002098 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099 if (retval)
2100 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002101 if (!EXT4_DIR_LINK_EMPTY(inode))
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002102 ext4_warning(inode->i_sb, "ext4_rmdir",
2103 "empty directory has too many links (%d)",
2104 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105 inode->i_version++;
2106 clear_nlink(inode);
2107 /* There's no need to set i_disksize: the fact that i_nlink is
2108 * zero will ensure that the right thing happens during any
2109 * recovery. */
2110 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002111 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002112 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002113 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002114 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002115 ext4_update_dx_flag(dir);
2116 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117
2118end_rmdir:
Mingming Cao617ba132006-10-11 01:20:53 -07002119 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002120 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002121 return retval;
2122}
2123
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002124static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125{
2126 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002127 struct inode *inode;
2128 struct buffer_head *bh;
2129 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 handle_t *handle;
2131
2132 /* Initialize quotas before so that eventual writes go
2133 * in separate transaction */
2134 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002135 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002136 if (IS_ERR(handle))
2137 return PTR_ERR(handle);
2138
2139 if (IS_DIRSYNC(dir))
2140 handle->h_sync = 1;
2141
2142 retval = -ENOENT;
Theodore Ts'of702ba02008-09-22 15:21:01 -04002143 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144 if (!bh)
2145 goto end_unlink;
2146
2147 inode = dentry->d_inode;
2148
2149 retval = -EIO;
2150 if (le32_to_cpu(de->inode) != inode->i_ino)
2151 goto end_unlink;
2152
2153 if (!inode->i_nlink) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002154 ext4_warning(inode->i_sb, "ext4_unlink",
2155 "Deleting nonexistent file (%lu), %d",
2156 inode->i_ino, inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002157 inode->i_nlink = 1;
2158 }
Mingming Cao617ba132006-10-11 01:20:53 -07002159 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002160 if (retval)
2161 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04002162 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002163 ext4_update_dx_flag(dir);
2164 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002165 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002167 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002168 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002169 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002170 retval = 0;
2171
2172end_unlink:
Mingming Cao617ba132006-10-11 01:20:53 -07002173 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002174 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002175 return retval;
2176}
2177
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002178static int ext4_symlink(struct inode *dir,
2179 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180{
2181 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002182 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183 int l, err, retries = 0;
2184
2185 l = strlen(symname)+1;
2186 if (l > dir->i_sb->s_blocksize)
2187 return -ENAMETOOLONG;
2188
2189retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002190 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2191 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2192 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 if (IS_ERR(handle))
2194 return PTR_ERR(handle);
2195
2196 if (IS_DIRSYNC(dir))
2197 handle->h_sync = 1;
2198
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002199 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 err = PTR_ERR(inode);
2201 if (IS_ERR(inode))
2202 goto out_stop;
2203
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002204 if (l > sizeof(EXT4_I(inode)->i_data)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002205 inode->i_op = &ext4_symlink_inode_operations;
2206 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002207 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002208 * page_symlink() calls into ext4_prepare/commit_write.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002209 * We have a transaction open. All is sweetness. It also sets
2210 * i_size in generic_commit_write().
2211 */
2212 err = __page_symlink(inode, symname, l,
2213 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
2214 if (err) {
Theodore Ts'o825f1482008-02-15 15:00:38 -05002215 clear_nlink(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002216 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002217 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218 goto out_stop;
2219 }
2220 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04002221 /* clear the extent format for fast symlink */
2222 EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
Mingming Cao617ba132006-10-11 01:20:53 -07002223 inode->i_op = &ext4_fast_symlink_inode_operations;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002224 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 inode->i_size = l-1;
2226 }
Mingming Cao617ba132006-10-11 01:20:53 -07002227 EXT4_I(inode)->i_disksize = inode->i_size;
2228 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07002230 ext4_journal_stop(handle);
2231 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 goto retry;
2233 return err;
2234}
2235
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002236static int ext4_link(struct dentry *old_dentry,
2237 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238{
2239 handle_t *handle;
2240 struct inode *inode = old_dentry->d_inode;
2241 int err, retries = 0;
2242
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002243 if (EXT4_DIR_LINK_MAX(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002244 return -EMLINK;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002245
Eric Sandeen2988a772007-02-10 01:46:16 -08002246 /*
2247 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2248 * otherwise has the potential to corrupt the orphan inode list.
2249 */
2250 if (inode->i_nlink == 0)
2251 return -ENOENT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002252
2253retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002254 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2255 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 if (IS_ERR(handle))
2257 return PTR_ERR(handle);
2258
2259 if (IS_DIRSYNC(dir))
2260 handle->h_sync = 1;
2261
Kalpak Shahef7f3832007-07-18 09:15:20 -04002262 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002263 ext4_inc_count(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 atomic_inc(&inode->i_count);
2265
Mingming Cao617ba132006-10-11 01:20:53 -07002266 err = ext4_add_nondir(handle, dentry, inode);
2267 ext4_journal_stop(handle);
2268 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 goto retry;
2270 return err;
2271}
2272
2273#define PARENT_INO(buffer) \
Jan Karaa72d7f82008-01-28 23:58:27 -05002274 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002275
2276/*
2277 * Anybody can rename anything with this: the permission checks are left to the
2278 * higher-level routines.
2279 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002280static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2281 struct inode *new_dir, struct dentry *new_dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002282{
2283 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002284 struct inode *old_inode, *new_inode;
2285 struct buffer_head *old_bh, *new_bh, *dir_bh;
2286 struct ext4_dir_entry_2 *old_de, *new_de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 int retval;
2288
2289 old_bh = new_bh = dir_bh = NULL;
2290
2291 /* Initialize quotas before so that eventual writes go
2292 * in separate transaction */
2293 if (new_dentry->d_inode)
2294 DQUOT_INIT(new_dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002295 handle = ext4_journal_start(old_dir, 2 *
2296 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2297 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298 if (IS_ERR(handle))
2299 return PTR_ERR(handle);
2300
2301 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2302 handle->h_sync = 1;
2303
Theodore Ts'of702ba02008-09-22 15:21:01 -04002304 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 /*
2306 * Check for inode number is _not_ due to possible IO errors.
2307 * We might rmdir the source, keep it as pwd of some process
2308 * and merrily kill the link to whatever was created under the
2309 * same name. Goodbye sticky bit ;-<
2310 */
2311 old_inode = old_dentry->d_inode;
2312 retval = -ENOENT;
2313 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2314 goto end_rename;
2315
2316 new_inode = new_dentry->d_inode;
Theodore Ts'of702ba02008-09-22 15:21:01 -04002317 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002318 if (new_bh) {
2319 if (!new_inode) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002320 brelse(new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002321 new_bh = NULL;
2322 }
2323 }
2324 if (S_ISDIR(old_inode->i_mode)) {
2325 if (new_inode) {
2326 retval = -ENOTEMPTY;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002327 if (!empty_dir(new_inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328 goto end_rename;
2329 }
2330 retval = -EIO;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002331 dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002332 if (!dir_bh)
2333 goto end_rename;
2334 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2335 goto end_rename;
2336 retval = -EMLINK;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002337 if (!new_inode && new_dir != old_dir &&
Mingming Cao617ba132006-10-11 01:20:53 -07002338 new_dir->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 goto end_rename;
2340 }
2341 if (!new_bh) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002342 retval = ext4_add_entry(handle, new_dentry, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343 if (retval)
2344 goto end_rename;
2345 } else {
2346 BUFFER_TRACE(new_bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07002347 ext4_journal_get_write_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002348 new_de->inode = cpu_to_le32(old_inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002349 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2350 EXT4_FEATURE_INCOMPAT_FILETYPE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351 new_de->file_type = old_de->file_type;
2352 new_dir->i_version++;
Jan Kara53b7e9f2008-04-29 22:02:11 -04002353 new_dir->i_ctime = new_dir->i_mtime =
2354 ext4_current_time(new_dir);
2355 ext4_mark_inode_dirty(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002356 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
2357 ext4_journal_dirty_metadata(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002358 brelse(new_bh);
2359 new_bh = NULL;
2360 }
2361
2362 /*
2363 * Like most other Unix systems, set the ctime for inodes on a
2364 * rename.
2365 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04002366 old_inode->i_ctime = ext4_current_time(old_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002367 ext4_mark_inode_dirty(handle, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002368
2369 /*
2370 * ok, that's it
2371 */
2372 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2373 old_de->name_len != old_dentry->d_name.len ||
2374 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
Mingming Cao617ba132006-10-11 01:20:53 -07002375 (retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002376 old_de, old_bh)) == -ENOENT) {
2377 /* old_de could have moved from under us during htree split, so
2378 * make sure that we are deleting the right entry. We might
2379 * also be pointing to a stale entry in the unused part of
2380 * old_bh so just checking inum and the name isn't enough. */
2381 struct buffer_head *old_bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002382 struct ext4_dir_entry_2 *old_de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383
Theodore Ts'of702ba02008-09-22 15:21:01 -04002384 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002385 if (old_bh2) {
Mingming Cao617ba132006-10-11 01:20:53 -07002386 retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387 old_de2, old_bh2);
2388 brelse(old_bh2);
2389 }
2390 }
2391 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002392 ext4_warning(old_dir->i_sb, "ext4_rename",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002393 "Deleting old file (%lu), %d, error=%d",
2394 old_dir->i_ino, old_dir->i_nlink, retval);
2395 }
2396
2397 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002398 ext4_dec_count(handle, new_inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002399 new_inode->i_ctime = ext4_current_time(new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002400 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002401 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002402 ext4_update_dx_flag(old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403 if (dir_bh) {
2404 BUFFER_TRACE(dir_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002405 ext4_journal_get_write_access(handle, dir_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002407 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
2408 ext4_journal_dirty_metadata(handle, dir_bh);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002409 ext4_dec_count(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002411 /* checked empty_dir above, can't have another parent,
Theodore Ts'o825f1482008-02-15 15:00:38 -05002412 * ext4_dec_count() won't work for many-linked dirs */
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002413 new_inode->i_nlink = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 } else {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002415 ext4_inc_count(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002416 ext4_update_dx_flag(new_dir);
2417 ext4_mark_inode_dirty(handle, new_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418 }
2419 }
Mingming Cao617ba132006-10-11 01:20:53 -07002420 ext4_mark_inode_dirty(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002421 if (new_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07002422 ext4_mark_inode_dirty(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423 if (!new_inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002424 ext4_orphan_add(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425 }
2426 retval = 0;
2427
2428end_rename:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002429 brelse(dir_bh);
2430 brelse(old_bh);
2431 brelse(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002432 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002433 return retval;
2434}
2435
2436/*
2437 * directories can handle most operations...
2438 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08002439const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002440 .create = ext4_create,
2441 .lookup = ext4_lookup,
2442 .link = ext4_link,
2443 .unlink = ext4_unlink,
2444 .symlink = ext4_symlink,
2445 .mkdir = ext4_mkdir,
2446 .rmdir = ext4_rmdir,
2447 .mknod = ext4_mknod,
2448 .rename = ext4_rename,
2449 .setattr = ext4_setattr,
Theodore Ts'o03010a32008-10-10 20:02:48 -04002450#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 .setxattr = generic_setxattr,
2452 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002453 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454 .removexattr = generic_removexattr,
2455#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002456 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457};
2458
Arjan van de Ven754661f2007-02-12 00:55:38 -08002459const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002460 .setattr = ext4_setattr,
Theodore Ts'o03010a32008-10-10 20:02:48 -04002461#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 .setxattr = generic_setxattr,
2463 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002464 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002465 .removexattr = generic_removexattr,
2466#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002467 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468};