blob: 5fdb862e71c47fb8df885e87e93090141227154a [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>
Mingming Cao617ba132006-10-11 01:20:53 -070031#include <linux/ext4_fs.h>
Mingming Caodab291a2006-10-11 01:21:01 -070032#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033#include <linux/fcntl.h>
34#include <linux/stat.h>
35#include <linux/string.h>
36#include <linux/quotaops.h>
37#include <linux/buffer_head.h>
38#include <linux/bio.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,
54 u32 *block, int *err)
55{
56 struct buffer_head *bh;
57
58 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
59
Mingming Cao617ba132006-10-11 01:20:53 -070060 if ((bh = ext4_bread(handle, inode, *block, 1, err))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061 inode->i_size += inode->i_sb->s_blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -070062 EXT4_I(inode)->i_disksize = inode->i_size;
63 ext4_journal_get_write_access(handle,bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 }
65 return bh;
66}
67
68#ifndef assert
69#define assert(test) J_ASSERT(test)
70#endif
71
72#ifndef swap
73#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
74#endif
75
76#ifdef DX_DEBUG
77#define dxtrace(command) command
78#else
79#define dxtrace(command)
80#endif
81
82struct fake_dirent
83{
84 __le32 inode;
85 __le16 rec_len;
86 u8 name_len;
87 u8 file_type;
88};
89
90struct dx_countlimit
91{
92 __le16 limit;
93 __le16 count;
94};
95
96struct dx_entry
97{
98 __le32 hash;
99 __le32 block;
100};
101
102/*
103 * dx_root_info is laid out so that if it should somehow get overlaid by a
104 * dirent the two low bits of the hash version will be zero. Therefore, the
105 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
106 */
107
108struct dx_root
109{
110 struct fake_dirent dot;
111 char dot_name[4];
112 struct fake_dirent dotdot;
113 char dotdot_name[4];
114 struct dx_root_info
115 {
116 __le32 reserved_zero;
117 u8 hash_version;
118 u8 info_length; /* 8 */
119 u8 indirect_levels;
120 u8 unused_flags;
121 }
122 info;
123 struct dx_entry entries[0];
124};
125
126struct dx_node
127{
128 struct fake_dirent fake;
129 struct dx_entry entries[0];
130};
131
132
133struct dx_frame
134{
135 struct buffer_head *bh;
136 struct dx_entry *entries;
137 struct dx_entry *at;
138};
139
140struct dx_map_entry
141{
142 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700143 u16 offs;
144 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145};
146
Mingming Cao617ba132006-10-11 01:20:53 -0700147#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148static inline unsigned dx_get_block (struct dx_entry *entry);
149static void dx_set_block (struct dx_entry *entry, unsigned value);
150static inline unsigned dx_get_hash (struct dx_entry *entry);
151static void dx_set_hash (struct dx_entry *entry, unsigned value);
152static unsigned dx_get_count (struct dx_entry *entries);
153static unsigned dx_get_limit (struct dx_entry *entries);
154static void dx_set_count (struct dx_entry *entries, unsigned value);
155static void dx_set_limit (struct dx_entry *entries, unsigned value);
156static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
157static unsigned dx_node_limit (struct inode *dir);
158static struct dx_frame *dx_probe(struct dentry *dentry,
159 struct inode *dir,
160 struct dx_hash_info *hinfo,
161 struct dx_frame *frame,
162 int *err);
163static void dx_release (struct dx_frame *frames);
Mingming Cao617ba132006-10-11 01:20:53 -0700164static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
166static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Mingming Cao617ba132006-10-11 01:20:53 -0700167static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 struct dx_map_entry *offsets, int count);
Mingming Cao617ba132006-10-11 01:20:53 -0700169static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
Mingming Cao617ba132006-10-11 01:20:53 -0700171static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 struct dx_frame *frame,
173 struct dx_frame *frames,
174 __u32 *start_hash);
Mingming Cao617ba132006-10-11 01:20:53 -0700175static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
176 struct ext4_dir_entry_2 **res_dir, int *err);
177static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178 struct inode *inode);
179
180/*
181 * Future: use high four bits of block for coalesce-on-delete flags
182 * Mask them off for now.
183 */
184
185static inline unsigned dx_get_block (struct dx_entry *entry)
186{
187 return le32_to_cpu(entry->block) & 0x00ffffff;
188}
189
190static inline void dx_set_block (struct dx_entry *entry, unsigned value)
191{
192 entry->block = cpu_to_le32(value);
193}
194
195static inline unsigned dx_get_hash (struct dx_entry *entry)
196{
197 return le32_to_cpu(entry->hash);
198}
199
200static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
201{
202 entry->hash = cpu_to_le32(value);
203}
204
205static inline unsigned dx_get_count (struct dx_entry *entries)
206{
207 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
208}
209
210static inline unsigned dx_get_limit (struct dx_entry *entries)
211{
212 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
213}
214
215static inline void dx_set_count (struct dx_entry *entries, unsigned value)
216{
217 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
218}
219
220static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
221{
222 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
223}
224
225static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
226{
Mingming Cao617ba132006-10-11 01:20:53 -0700227 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
228 EXT4_DIR_REC_LEN(2) - infosize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 return 0? 20: entry_space / sizeof(struct dx_entry);
230}
231
232static inline unsigned dx_node_limit (struct inode *dir)
233{
Mingming Cao617ba132006-10-11 01:20:53 -0700234 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235 return 0? 22: entry_space / sizeof(struct dx_entry);
236}
237
238/*
239 * Debug
240 */
241#ifdef DX_DEBUG
242static void dx_show_index (char * label, struct dx_entry *entries)
243{
Andrew Morton63f57932006-10-11 01:21:24 -0700244 int i, n = dx_get_count (entries);
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400245 printk("%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700246 for (i = 0; i < n; i++) {
247 printk("%x->%u ", i? dx_get_hash(entries + i) :
248 0, dx_get_block(entries + i));
249 }
250 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251}
252
253struct stats
254{
255 unsigned names;
256 unsigned space;
257 unsigned bcount;
258};
259
Mingming Cao617ba132006-10-11 01:20:53 -0700260static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 int size, int show_names)
262{
263 unsigned names = 0, space = 0;
264 char *base = (char *) de;
265 struct dx_hash_info h = *hinfo;
266
267 printk("names: ");
268 while ((char *) de < base + size)
269 {
270 if (de->inode)
271 {
272 if (show_names)
273 {
274 int len = de->name_len;
275 char *name = de->name;
276 while (len--) printk("%c", *name++);
Mingming Cao617ba132006-10-11 01:20:53 -0700277 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 printk(":%x.%u ", h.hash,
279 ((char *) de - base));
280 }
Mingming Cao617ba132006-10-11 01:20:53 -0700281 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 names++;
283 }
Mingming Cao617ba132006-10-11 01:20:53 -0700284 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 }
286 printk("(%i)\n", names);
287 return (struct stats) { names, space, 1 };
288}
289
290struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
291 struct dx_entry *entries, int levels)
292{
293 unsigned blocksize = dir->i_sb->s_blocksize;
294 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
295 unsigned bcount = 0;
296 struct buffer_head *bh;
297 int err;
298 printk("%i indexed blocks...\n", count);
299 for (i = 0; i < count; i++, entries++)
300 {
301 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
302 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
303 struct stats stats;
304 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Mingming Cao617ba132006-10-11 01:20:53 -0700305 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 stats = levels?
307 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Mingming Cao617ba132006-10-11 01:20:53 -0700308 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 names += stats.names;
310 space += stats.space;
311 bcount += stats.bcount;
312 brelse (bh);
313 }
314 if (bcount)
315 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
316 names, space/bcount,(space/bcount)*100/blocksize);
317 return (struct stats) { names, space, bcount};
318}
319#endif /* DX_DEBUG */
320
321/*
322 * Probe for a directory leaf block to search.
323 *
324 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
325 * error in the directory index, and the caller should fall back to
326 * searching the directory normally. The callers of dx_probe **MUST**
327 * check for this error code, and make sure it never gets reflected
328 * back to userspace.
329 */
330static struct dx_frame *
331dx_probe(struct dentry *dentry, struct inode *dir,
332 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
333{
334 unsigned count, indirect;
335 struct dx_entry *at, *entries, *p, *q, *m;
336 struct dx_root *root;
337 struct buffer_head *bh;
338 struct dx_frame *frame = frame_in;
339 u32 hash;
340
341 frame->bh = NULL;
342 if (dentry)
343 dir = dentry->d_parent->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700344 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 goto fail;
346 root = (struct dx_root *) bh->b_data;
347 if (root->info.hash_version != DX_HASH_TEA &&
348 root->info.hash_version != DX_HASH_HALF_MD4 &&
349 root->info.hash_version != DX_HASH_LEGACY) {
Mingming Cao617ba132006-10-11 01:20:53 -0700350 ext4_warning(dir->i_sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 "Unrecognised inode hash code %d",
352 root->info.hash_version);
353 brelse(bh);
354 *err = ERR_BAD_DX_DIR;
355 goto fail;
356 }
357 hinfo->hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -0700358 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 if (dentry)
Mingming Cao617ba132006-10-11 01:20:53 -0700360 ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 hash = hinfo->hash;
362
363 if (root->info.unused_flags & 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700364 ext4_warning(dir->i_sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 "Unimplemented inode hash flags: %#06x",
366 root->info.unused_flags);
367 brelse(bh);
368 *err = ERR_BAD_DX_DIR;
369 goto fail;
370 }
371
372 if ((indirect = root->info.indirect_levels) > 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700373 ext4_warning(dir->i_sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700374 "Unimplemented inode hash depth: %#06x",
375 root->info.indirect_levels);
376 brelse(bh);
377 *err = ERR_BAD_DX_DIR;
378 goto fail;
379 }
380
381 entries = (struct dx_entry *) (((char *)&root->info) +
382 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700383
384 if (dx_get_limit(entries) != dx_root_limit(dir,
385 root->info.info_length)) {
386 ext4_warning(dir->i_sb, __FUNCTION__,
387 "dx entry: limit != root limit");
388 brelse(bh);
389 *err = ERR_BAD_DX_DIR;
390 goto fail;
391 }
392
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393 dxtrace (printk("Look up %x", hash));
394 while (1)
395 {
396 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700397 if (!count || count > dx_get_limit(entries)) {
398 ext4_warning(dir->i_sb, __FUNCTION__,
399 "dx entry: no count or count > limit");
400 brelse(bh);
401 *err = ERR_BAD_DX_DIR;
402 goto fail2;
403 }
404
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700405 p = entries + 1;
406 q = entries + count - 1;
407 while (p <= q)
408 {
409 m = p + (q - p)/2;
410 dxtrace(printk("."));
411 if (dx_get_hash(m) > hash)
412 q = m - 1;
413 else
414 p = m + 1;
415 }
416
417 if (0) // linear search cross check
418 {
419 unsigned n = count - 1;
420 at = entries;
421 while (n--)
422 {
423 dxtrace(printk(","));
424 if (dx_get_hash(++at) > hash)
425 {
426 at--;
427 break;
428 }
429 }
430 assert (at == p - 1);
431 }
432
433 at = p - 1;
434 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
435 frame->bh = bh;
436 frame->entries = entries;
437 frame->at = at;
438 if (!indirect--) return frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700439 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440 goto fail2;
441 at = entries = ((struct dx_node *) bh->b_data)->entries;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700442 if (dx_get_limit(entries) != dx_node_limit (dir)) {
443 ext4_warning(dir->i_sb, __FUNCTION__,
444 "dx entry: limit != node limit");
445 brelse(bh);
446 *err = ERR_BAD_DX_DIR;
447 goto fail2;
448 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700449 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700450 frame->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 }
452fail2:
453 while (frame >= frame_in) {
454 brelse(frame->bh);
455 frame--;
456 }
457fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700458 if (*err == ERR_BAD_DX_DIR)
459 ext4_warning(dir->i_sb, __FUNCTION__,
460 "Corrupt dir inode %ld, running e2fsck is "
461 "recommended.", dir->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 return NULL;
463}
464
465static void dx_release (struct dx_frame *frames)
466{
467 if (frames[0].bh == NULL)
468 return;
469
470 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
471 brelse(frames[1].bh);
472 brelse(frames[0].bh);
473}
474
475/*
476 * This function increments the frame pointer to search the next leaf
477 * block, and reads in the necessary intervening nodes if the search
478 * should be necessary. Whether or not the search is necessary is
479 * controlled by the hash parameter. If the hash value is even, then
480 * the search is only continued if the next block starts with that
481 * hash value. This is used if we are searching for a specific file.
482 *
483 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
484 *
485 * This function returns 1 if the caller should continue to search,
486 * or 0 if it should not. If there is an error reading one of the
487 * index blocks, it will a negative error code.
488 *
489 * If start_hash is non-null, it will be filled in with the starting
490 * hash of the next page.
491 */
Mingming Cao617ba132006-10-11 01:20:53 -0700492static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 struct dx_frame *frame,
494 struct dx_frame *frames,
495 __u32 *start_hash)
496{
497 struct dx_frame *p;
498 struct buffer_head *bh;
499 int err, num_frames = 0;
500 __u32 bhash;
501
502 p = frame;
503 /*
504 * Find the next leaf page by incrementing the frame pointer.
505 * If we run out of entries in the interior node, loop around and
506 * increment pointer in the parent node. When we break out of
507 * this loop, num_frames indicates the number of interior
508 * nodes need to be read.
509 */
510 while (1) {
511 if (++(p->at) < p->entries + dx_get_count(p->entries))
512 break;
513 if (p == frames)
514 return 0;
515 num_frames++;
516 p--;
517 }
518
519 /*
520 * If the hash is 1, then continue only if the next page has a
521 * continuation hash of any value. This is used for readdir
522 * handling. Otherwise, check to see if the hash matches the
523 * desired contiuation hash. If it doesn't, return since
524 * there's no point to read in the successive index pages.
525 */
526 bhash = dx_get_hash(p->at);
527 if (start_hash)
528 *start_hash = bhash;
529 if ((hash & 1) == 0) {
530 if ((bhash & ~1) != hash)
531 return 0;
532 }
533 /*
534 * If the hash is HASH_NB_ALWAYS, we always go to the next
535 * block so no check is necessary
536 */
537 while (num_frames--) {
Mingming Cao617ba132006-10-11 01:20:53 -0700538 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 0, &err)))
540 return err; /* Failure */
541 p++;
542 brelse (p->bh);
543 p->bh = bh;
544 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
545 }
546 return 1;
547}
548
549
550/*
551 * p is at least 6 bytes before the end of page
552 */
Mingming Cao617ba132006-10-11 01:20:53 -0700553static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554{
Mingming Cao617ba132006-10-11 01:20:53 -0700555 return (struct ext4_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556}
557
558/*
559 * This function fills a red-black tree with information from a
560 * directory block. It returns the number directory entries loaded
561 * into the tree. If there is an error it is returned in err.
562 */
563static int htree_dirblock_to_tree(struct file *dir_file,
564 struct inode *dir, int block,
565 struct dx_hash_info *hinfo,
566 __u32 start_hash, __u32 start_minor_hash)
567{
568 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700569 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 int err, count = 0;
571
572 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
Mingming Cao617ba132006-10-11 01:20:53 -0700573 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574 return err;
575
Mingming Cao617ba132006-10-11 01:20:53 -0700576 de = (struct ext4_dir_entry_2 *) bh->b_data;
577 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700579 EXT4_DIR_REC_LEN(0));
580 for (; de < top; de = ext4_next_entry(de)) {
Eric Sandeene6c40212006-12-06 20:36:28 -0800581 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
582 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
583 +((char *)de - bh->b_data))) {
584 /* On error, skip the f_pos to the next block. */
585 dir_file->f_pos = (dir_file->f_pos |
586 (dir->i_sb->s_blocksize - 1)) + 1;
587 brelse (bh);
588 return count;
589 }
Mingming Cao617ba132006-10-11 01:20:53 -0700590 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591 if ((hinfo->hash < start_hash) ||
592 ((hinfo->hash == start_hash) &&
593 (hinfo->minor_hash < start_minor_hash)))
594 continue;
595 if (de->inode == 0)
596 continue;
Mingming Cao617ba132006-10-11 01:20:53 -0700597 if ((err = ext4_htree_store_dirent(dir_file,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598 hinfo->hash, hinfo->minor_hash, de)) != 0) {
599 brelse(bh);
600 return err;
601 }
602 count++;
603 }
604 brelse(bh);
605 return count;
606}
607
608
609/*
610 * This function fills a red-black tree with information from a
611 * directory. We start scanning the directory in hash order, starting
612 * at start_hash and start_minor_hash.
613 *
614 * This function returns the number of entries inserted into the tree,
615 * or a negative error code.
616 */
Mingming Cao617ba132006-10-11 01:20:53 -0700617int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 __u32 start_minor_hash, __u32 *next_hash)
619{
620 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -0700621 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 struct dx_frame frames[2], *frame;
623 struct inode *dir;
624 int block, err;
625 int count = 0;
626 int ret;
627 __u32 hashval;
628
629 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
630 start_minor_hash));
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800631 dir = dir_file->f_path.dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700632 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
633 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
634 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
636 start_hash, start_minor_hash);
637 *next_hash = ~0;
638 return count;
639 }
640 hinfo.hash = start_hash;
641 hinfo.minor_hash = 0;
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800642 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643 if (!frame)
644 return err;
645
646 /* Add '.' and '..' from the htree header */
647 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -0700648 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
649 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 goto errout;
651 count++;
652 }
653 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700654 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
655 de = ext4_next_entry(de);
656 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657 goto errout;
658 count++;
659 }
660
661 while (1) {
662 block = dx_get_block(frame->at);
663 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
664 start_hash, start_minor_hash);
665 if (ret < 0) {
666 err = ret;
667 goto errout;
668 }
669 count += ret;
670 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -0700671 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 frame, frames, &hashval);
673 *next_hash = hashval;
674 if (ret < 0) {
675 err = ret;
676 goto errout;
677 }
678 /*
679 * Stop if: (a) there are no more entries, or
680 * (b) we have inserted at least one entry and the
681 * next hash value is not a continuation
682 */
683 if ((ret == 0) ||
684 (count && ((hashval & 1) == 0)))
685 break;
686 }
687 dx_release(frames);
688 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
689 count, *next_hash));
690 return count;
691errout:
692 dx_release(frames);
693 return (err);
694}
695
696
697/*
698 * Directory block splitting, compacting
699 */
700
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700701/*
702 * Create map of hash values, offsets, and sizes, stored at end of block.
703 * Returns number of entries mapped.
704 */
Mingming Cao617ba132006-10-11 01:20:53 -0700705static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
707{
708 int count = 0;
709 char *base = (char *) de;
710 struct dx_hash_info h = *hinfo;
711
712 while ((char *) de < base + size)
713 {
714 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -0700715 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 map_tail--;
717 map_tail->hash = h.hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700718 map_tail->offs = (u16) ((char *) de - base);
719 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720 count++;
721 cond_resched();
722 }
723 /* XXX: do we need to check rec_len == 0 case? -Chris */
Mingming Cao617ba132006-10-11 01:20:53 -0700724 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 }
726 return count;
727}
728
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700729/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730static void dx_sort_map (struct dx_map_entry *map, unsigned count)
731{
Andrew Morton63f57932006-10-11 01:21:24 -0700732 struct dx_map_entry *p, *q, *top = map + count - 1;
733 int more;
734 /* Combsort until bubble sort doesn't suck */
735 while (count > 2) {
736 count = count*10/13;
737 if (count - 9 < 2) /* 9, 10 -> 11 */
738 count = 11;
739 for (p = top, q = p - count; q >= map; p--, q--)
740 if (p->hash < q->hash)
741 swap(*p, *q);
742 }
743 /* Garden variety bubble sort */
744 do {
745 more = 0;
746 q = top;
747 while (q-- > map) {
748 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 continue;
Andrew Morton63f57932006-10-11 01:21:24 -0700750 swap(*(q+1), *q);
751 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 }
753 } while(more);
754}
755
756static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
757{
758 struct dx_entry *entries = frame->entries;
759 struct dx_entry *old = frame->at, *new = old + 1;
760 int count = dx_get_count(entries);
761
762 assert(count < dx_get_limit(entries));
763 assert(old < entries + count);
764 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
765 dx_set_hash(new, hash);
766 dx_set_block(new, block);
767 dx_set_count(entries, count + 1);
768}
769#endif
770
771
Mingming Cao617ba132006-10-11 01:20:53 -0700772static void ext4_update_dx_flag(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773{
Mingming Cao617ba132006-10-11 01:20:53 -0700774 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
775 EXT4_FEATURE_COMPAT_DIR_INDEX))
776 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777}
778
779/*
Mingming Cao617ba132006-10-11 01:20:53 -0700780 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 *
Mingming Cao617ba132006-10-11 01:20:53 -0700782 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 * `de != NULL' is guaranteed by caller.
784 */
Mingming Cao617ba132006-10-11 01:20:53 -0700785static inline int ext4_match (int len, const char * const name,
786 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787{
788 if (len != de->name_len)
789 return 0;
790 if (!de->inode)
791 return 0;
792 return !memcmp(name, de->name, len);
793}
794
795/*
796 * Returns 0 if not found, -1 on failure, and 1 on success
797 */
798static inline int search_dirblock(struct buffer_head * bh,
799 struct inode *dir,
800 struct dentry *dentry,
801 unsigned long offset,
Mingming Cao617ba132006-10-11 01:20:53 -0700802 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803{
Mingming Cao617ba132006-10-11 01:20:53 -0700804 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805 char * dlimit;
806 int de_len;
807 const char *name = dentry->d_name.name;
808 int namelen = dentry->d_name.len;
809
Mingming Cao617ba132006-10-11 01:20:53 -0700810 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 dlimit = bh->b_data + dir->i_sb->s_blocksize;
812 while ((char *) de < dlimit) {
813 /* this code is executed quadratically often */
814 /* do minimal checking `by hand' */
815
816 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -0700817 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818 /* found a match - just to be sure, do a full check */
Mingming Cao617ba132006-10-11 01:20:53 -0700819 if (!ext4_check_dir_entry("ext4_find_entry",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820 dir, de, bh, offset))
821 return -1;
822 *res_dir = de;
823 return 1;
824 }
825 /* prevent looping on a bad block */
826 de_len = le16_to_cpu(de->rec_len);
827 if (de_len <= 0)
828 return -1;
829 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -0700830 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 }
832 return 0;
833}
834
835
836/*
Mingming Cao617ba132006-10-11 01:20:53 -0700837 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 *
839 * finds an entry in the specified directory with the wanted name. It
840 * returns the cache buffer in which the entry was found, and the entry
841 * itself (as a parameter - res_dir). It does NOT read the inode of the
842 * entry - you'll have to do that yourself if you want to.
843 *
844 * The returned buffer_head has ->b_count elevated. The caller is expected
845 * to brelse() it when appropriate.
846 */
Mingming Cao617ba132006-10-11 01:20:53 -0700847static struct buffer_head * ext4_find_entry (struct dentry *dentry,
848 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849{
850 struct super_block * sb;
851 struct buffer_head * bh_use[NAMEI_RA_SIZE];
852 struct buffer_head * bh, *ret = NULL;
853 unsigned long start, block, b;
854 int ra_max = 0; /* Number of bh's in the readahead
855 buffer, bh_use[] */
856 int ra_ptr = 0; /* Current index into readahead
857 buffer */
858 int num = 0;
859 int nblocks, i, err;
860 struct inode *dir = dentry->d_parent->d_inode;
861 int namelen;
862 const u8 *name;
863 unsigned blocksize;
864
865 *res_dir = NULL;
866 sb = dir->i_sb;
867 blocksize = sb->s_blocksize;
868 namelen = dentry->d_name.len;
869 name = dentry->d_name.name;
Mingming Cao617ba132006-10-11 01:20:53 -0700870 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700872#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700874 bh = ext4_dx_find_entry(dentry, res_dir, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 /*
876 * On success, or if the error was file not found,
877 * return. Otherwise, fall back to doing a search the
878 * old fashioned way.
879 */
880 if (bh || (err != ERR_BAD_DX_DIR))
881 return bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700882 dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 }
884#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700885 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
886 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 if (start >= nblocks)
888 start = 0;
889 block = start;
890restart:
891 do {
892 /*
893 * We deal with the read-ahead logic here.
894 */
895 if (ra_ptr >= ra_max) {
896 /* Refill the readahead buffer */
897 ra_ptr = 0;
898 b = block;
899 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
900 /*
901 * Terminate if we reach the end of the
902 * directory and must wrap, or if our
903 * search has finished at this block.
904 */
905 if (b >= nblocks || (num && block == start)) {
906 bh_use[ra_max] = NULL;
907 break;
908 }
909 num++;
Mingming Cao617ba132006-10-11 01:20:53 -0700910 bh = ext4_getblk(NULL, dir, b++, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700911 bh_use[ra_max] = bh;
912 if (bh)
913 ll_rw_block(READ_META, 1, &bh);
914 }
915 }
916 if ((bh = bh_use[ra_ptr++]) == NULL)
917 goto next;
918 wait_on_buffer(bh);
919 if (!buffer_uptodate(bh)) {
920 /* read error, skip block & hope for the best */
Mingming Cao617ba132006-10-11 01:20:53 -0700921 ext4_error(sb, __FUNCTION__, "reading directory #%lu "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 "offset %lu", dir->i_ino, block);
923 brelse(bh);
924 goto next;
925 }
926 i = search_dirblock(bh, dir, dentry,
Mingming Cao617ba132006-10-11 01:20:53 -0700927 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700929 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 ret = bh;
931 goto cleanup_and_exit;
932 } else {
933 brelse(bh);
934 if (i < 0)
935 goto cleanup_and_exit;
936 }
937 next:
938 if (++block >= nblocks)
939 block = 0;
940 } while (block != start);
941
942 /*
943 * If the directory has grown while we were searching, then
944 * search the last part of the directory before giving up.
945 */
946 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -0700947 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 if (block < nblocks) {
949 start = 0;
950 goto restart;
951 }
952
953cleanup_and_exit:
954 /* Clean up the read-ahead blocks */
955 for (; ra_ptr < ra_max; ra_ptr++)
956 brelse (bh_use[ra_ptr]);
957 return ret;
958}
959
Mingming Cao617ba132006-10-11 01:20:53 -0700960#ifdef CONFIG_EXT4_INDEX
961static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
962 struct ext4_dir_entry_2 **res_dir, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963{
964 struct super_block * sb;
965 struct dx_hash_info hinfo;
966 u32 hash;
967 struct dx_frame frames[2], *frame;
Mingming Cao617ba132006-10-11 01:20:53 -0700968 struct ext4_dir_entry_2 *de, *top;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 struct buffer_head *bh;
970 unsigned long block;
971 int retval;
972 int namelen = dentry->d_name.len;
973 const u8 *name = dentry->d_name.name;
974 struct inode *dir = dentry->d_parent->d_inode;
975
976 sb = dir->i_sb;
977 /* NFS may look up ".." - look at dx_root directory block */
978 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
979 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
980 return NULL;
981 } else {
982 frame = frames;
983 frame->bh = NULL; /* for dx_release() */
984 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
985 dx_set_block(frame->at, 0); /* dx_root block is 0 */
986 }
987 hash = hinfo.hash;
988 do {
989 block = dx_get_block(frame->at);
Mingming Cao617ba132006-10-11 01:20:53 -0700990 if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 goto errout;
Mingming Cao617ba132006-10-11 01:20:53 -0700992 de = (struct ext4_dir_entry_2 *) bh->b_data;
993 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
994 EXT4_DIR_REC_LEN(0));
995 for (; de < top; de = ext4_next_entry(de))
996 if (ext4_match (namelen, name, de)) {
997 if (!ext4_check_dir_entry("ext4_find_entry",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 dir, de, bh,
Mingming Cao617ba132006-10-11 01:20:53 -0700999 (block<<EXT4_BLOCK_SIZE_BITS(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 +((char *)de - bh->b_data))) {
1001 brelse (bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001002 *err = ERR_BAD_DX_DIR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 goto errout;
1004 }
1005 *res_dir = de;
1006 dx_release (frames);
1007 return bh;
1008 }
1009 brelse (bh);
1010 /* Check to see if we should continue to search */
Mingming Cao617ba132006-10-11 01:20:53 -07001011 retval = ext4_htree_next_block(dir, hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012 frames, NULL);
1013 if (retval < 0) {
Mingming Cao617ba132006-10-11 01:20:53 -07001014 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 "error reading index page in directory #%lu",
1016 dir->i_ino);
1017 *err = retval;
1018 goto errout;
1019 }
1020 } while (retval == 1);
1021
1022 *err = -ENOENT;
1023errout:
1024 dxtrace(printk("%s not found\n", name));
1025 dx_release (frames);
1026 return NULL;
1027}
1028#endif
1029
Mingming Cao617ba132006-10-11 01:20:53 -07001030static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031{
1032 struct inode * inode;
Mingming Cao617ba132006-10-11 01:20:53 -07001033 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 struct buffer_head * bh;
1035
Mingming Cao617ba132006-10-11 01:20:53 -07001036 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 return ERR_PTR(-ENAMETOOLONG);
1038
Mingming Cao617ba132006-10-11 01:20:53 -07001039 bh = ext4_find_entry(dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 inode = NULL;
1041 if (bh) {
1042 unsigned long ino = le32_to_cpu(de->inode);
1043 brelse (bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001044 if (!ext4_valid_inum(dir->i_sb, ino)) {
1045 ext4_error(dir->i_sb, "ext4_lookup",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 "bad inode number: %lu", ino);
1047 inode = NULL;
1048 } else
1049 inode = iget(dir->i_sb, ino);
1050
1051 if (!inode)
1052 return ERR_PTR(-EACCES);
Vasily Averina6c15c22007-07-15 23:40:46 -07001053
1054 if (is_bad_inode(inode)) {
1055 iput(inode);
1056 return ERR_PTR(-ENOENT);
1057 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 }
1059 return d_splice_alias(inode, dentry);
1060}
1061
1062
Mingming Cao617ba132006-10-11 01:20:53 -07001063struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064{
1065 unsigned long ino;
1066 struct dentry *parent;
1067 struct inode *inode;
1068 struct dentry dotdot;
Mingming Cao617ba132006-10-11 01:20:53 -07001069 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 struct buffer_head *bh;
1071
1072 dotdot.d_name.name = "..";
1073 dotdot.d_name.len = 2;
1074 dotdot.d_parent = child; /* confusing, isn't it! */
1075
Mingming Cao617ba132006-10-11 01:20:53 -07001076 bh = ext4_find_entry(&dotdot, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 inode = NULL;
1078 if (!bh)
1079 return ERR_PTR(-ENOENT);
1080 ino = le32_to_cpu(de->inode);
1081 brelse(bh);
1082
Mingming Cao617ba132006-10-11 01:20:53 -07001083 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1084 ext4_error(child->d_inode->i_sb, "ext4_get_parent",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 "bad inode number: %lu", ino);
1086 inode = NULL;
1087 } else
1088 inode = iget(child->d_inode->i_sb, ino);
1089
1090 if (!inode)
1091 return ERR_PTR(-EACCES);
1092
Vasily Averina6c15c22007-07-15 23:40:46 -07001093 if (is_bad_inode(inode)) {
1094 iput(inode);
1095 return ERR_PTR(-ENOENT);
1096 }
1097
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 parent = d_alloc_anon(inode);
1099 if (!parent) {
1100 iput(inode);
1101 parent = ERR_PTR(-ENOMEM);
1102 }
1103 return parent;
1104}
1105
1106#define S_SHIFT 12
Mingming Cao617ba132006-10-11 01:20:53 -07001107static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1108 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1109 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1110 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1111 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1112 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1113 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1114 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115};
1116
Mingming Cao617ba132006-10-11 01:20:53 -07001117static inline void ext4_set_de_type(struct super_block *sb,
1118 struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 umode_t mode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001120 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1121 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001122}
1123
Mingming Cao617ba132006-10-11 01:20:53 -07001124#ifdef CONFIG_EXT4_INDEX
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001125/*
1126 * Move count entries from end of map between two memory locations.
1127 * Returns pointer to last entry moved.
1128 */
Mingming Cao617ba132006-10-11 01:20:53 -07001129static struct ext4_dir_entry_2 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1131{
1132 unsigned rec_len = 0;
1133
1134 while (count--) {
Mingming Cao617ba132006-10-11 01:20:53 -07001135 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
1136 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001138 ((struct ext4_dir_entry_2 *) to)->rec_len =
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 cpu_to_le16(rec_len);
1140 de->inode = 0;
1141 map++;
1142 to += rec_len;
1143 }
Mingming Cao617ba132006-10-11 01:20:53 -07001144 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145}
1146
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001147/*
1148 * Compact each dir entry in the range to the minimal rec_len.
1149 * Returns pointer to last entry in range.
1150 */
Mingming Cao617ba132006-10-11 01:20:53 -07001151static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152{
Mingming Cao617ba132006-10-11 01:20:53 -07001153 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001154 unsigned rec_len = 0;
1155
1156 prev = to = de;
1157 while ((char*)de < base + size) {
Mingming Cao617ba132006-10-11 01:20:53 -07001158 next = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 le16_to_cpu(de->rec_len));
1160 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001161 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 if (de > to)
1163 memmove(to, de, rec_len);
1164 to->rec_len = cpu_to_le16(rec_len);
1165 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001166 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 }
1168 de = next;
1169 }
1170 return prev;
1171}
1172
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001173/*
1174 * Split a full leaf block to make room for a new dir entry.
1175 * Allocate a new block, and move entries so that they are approx. equally full.
1176 * Returns pointer to de in block into which the new entry will be inserted.
1177 */
Mingming Cao617ba132006-10-11 01:20:53 -07001178static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 struct buffer_head **bh,struct dx_frame *frame,
1180 struct dx_hash_info *hinfo, int *error)
1181{
1182 unsigned blocksize = dir->i_sb->s_blocksize;
1183 unsigned count, continued;
1184 struct buffer_head *bh2;
1185 u32 newblock;
1186 u32 hash2;
1187 struct dx_map_entry *map;
1188 char *data1 = (*bh)->b_data, *data2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001189 unsigned split, move, size, i;
Mingming Cao617ba132006-10-11 01:20:53 -07001190 struct ext4_dir_entry_2 *de = NULL, *de2;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001191 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001193 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 if (!(bh2)) {
1195 brelse(*bh);
1196 *bh = NULL;
1197 goto errout;
1198 }
1199
1200 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001201 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001202 if (err)
1203 goto journal_error;
1204
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001206 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207 if (err)
1208 goto journal_error;
1209
1210 data2 = bh2->b_data;
1211
1212 /* create map in the end of data2 block */
1213 map = (struct dx_map_entry *) (data2 + blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -07001214 count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 blocksize, hinfo, map);
1216 map -= count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 dx_sort_map (map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001218 /* Split the existing block in the middle, size-wise */
1219 size = 0;
1220 move = 0;
1221 for (i = count-1; i >= 0; i--) {
1222 /* is more than half of this entry in 2nd half of the block? */
1223 if (size + map[i].size/2 > blocksize/2)
1224 break;
1225 size += map[i].size;
1226 move++;
1227 }
1228 /* map index at which we will split */
1229 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 hash2 = map[split].hash;
1231 continued = hash2 == map[split - 1].hash;
1232 dxtrace(printk("Split block %i at %x, %i/%i\n",
1233 dx_get_block(frame->at), hash2, split, count-split));
1234
1235 /* Fancy dance to stay within two buffers */
1236 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1237 de = dx_pack_dirents(data1,blocksize);
1238 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1239 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
Mingming Cao617ba132006-10-11 01:20:53 -07001240 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1241 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242
1243 /* Which block gets the new entry? */
1244 if (hinfo->hash >= hash2)
1245 {
1246 swap(*bh, bh2);
1247 de = de2;
1248 }
1249 dx_insert_block (frame, hash2 + continued, newblock);
Mingming Cao617ba132006-10-11 01:20:53 -07001250 err = ext4_journal_dirty_metadata (handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251 if (err)
1252 goto journal_error;
Mingming Cao617ba132006-10-11 01:20:53 -07001253 err = ext4_journal_dirty_metadata (handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 if (err)
1255 goto journal_error;
1256 brelse (bh2);
1257 dxtrace(dx_show_index ("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001259
1260journal_error:
1261 brelse(*bh);
1262 brelse(bh2);
1263 *bh = NULL;
1264 ext4_std_error(dir->i_sb, err);
1265errout:
1266 *error = err;
1267 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268}
1269#endif
1270
1271
1272/*
1273 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1274 * it points to a directory entry which is guaranteed to be large
1275 * enough for new directory entry. If de is NULL, then
1276 * add_dirent_to_buf will attempt search the directory block for
1277 * space. It will return -ENOSPC if no space is available, and -EIO
1278 * and -EEXIST if directory entry already exists.
1279 *
1280 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1281 * all other cases bh is released.
1282 */
1283static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001284 struct inode *inode, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285 struct buffer_head * bh)
1286{
1287 struct inode *dir = dentry->d_parent->d_inode;
1288 const char *name = dentry->d_name.name;
1289 int namelen = dentry->d_name.len;
1290 unsigned long offset = 0;
1291 unsigned short reclen;
1292 int nlen, rlen, err;
1293 char *top;
1294
Mingming Cao617ba132006-10-11 01:20:53 -07001295 reclen = EXT4_DIR_REC_LEN(namelen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296 if (!de) {
Mingming Cao617ba132006-10-11 01:20:53 -07001297 de = (struct ext4_dir_entry_2 *)bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1299 while ((char *) de <= top) {
Mingming Cao617ba132006-10-11 01:20:53 -07001300 if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001301 bh, offset)) {
1302 brelse (bh);
1303 return -EIO;
1304 }
Mingming Cao617ba132006-10-11 01:20:53 -07001305 if (ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306 brelse (bh);
1307 return -EEXIST;
1308 }
Mingming Cao617ba132006-10-11 01:20:53 -07001309 nlen = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 rlen = le16_to_cpu(de->rec_len);
1311 if ((de->inode? rlen - nlen: rlen) >= reclen)
1312 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001313 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001314 offset += rlen;
1315 }
1316 if ((char *) de > top)
1317 return -ENOSPC;
1318 }
1319 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001320 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001322 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 brelse(bh);
1324 return err;
1325 }
1326
1327 /* By now the buffer is marked for journaling */
Mingming Cao617ba132006-10-11 01:20:53 -07001328 nlen = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001329 rlen = le16_to_cpu(de->rec_len);
1330 if (de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001331 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332 de1->rec_len = cpu_to_le16(rlen - nlen);
1333 de->rec_len = cpu_to_le16(nlen);
1334 de = de1;
1335 }
Mingming Cao617ba132006-10-11 01:20:53 -07001336 de->file_type = EXT4_FT_UNKNOWN;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337 if (inode) {
1338 de->inode = cpu_to_le32(inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001339 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001340 } else
1341 de->inode = 0;
1342 de->name_len = namelen;
1343 memcpy (de->name, name, namelen);
1344 /*
1345 * XXX shouldn't update any times until successful
1346 * completion of syscall, but too many callers depend
1347 * on this.
1348 *
1349 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001350 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 * recovery deletes the inode, so the worst that can
1352 * happen is that the times are slightly out of date
1353 * and/or different from the directory change time.
1354 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001355 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001356 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001357 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001358 ext4_mark_inode_dirty(handle, dir);
1359 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1360 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001362 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001363 brelse(bh);
1364 return 0;
1365}
1366
Mingming Cao617ba132006-10-11 01:20:53 -07001367#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368/*
1369 * This converts a one block unindexed directory to a 3 block indexed
1370 * directory, and adds the dentry to the indexed directory.
1371 */
1372static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1373 struct inode *inode, struct buffer_head *bh)
1374{
1375 struct inode *dir = dentry->d_parent->d_inode;
1376 const char *name = dentry->d_name.name;
1377 int namelen = dentry->d_name.len;
1378 struct buffer_head *bh2;
1379 struct dx_root *root;
1380 struct dx_frame frames[2], *frame;
1381 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001382 struct ext4_dir_entry_2 *de, *de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 char *data1, *top;
1384 unsigned len;
1385 int retval;
1386 unsigned blocksize;
1387 struct dx_hash_info hinfo;
1388 u32 block;
1389 struct fake_dirent *fde;
1390
1391 blocksize = dir->i_sb->s_blocksize;
1392 dxtrace(printk("Creating index\n"));
Mingming Cao617ba132006-10-11 01:20:53 -07001393 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001395 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 brelse(bh);
1397 return retval;
1398 }
1399 root = (struct dx_root *) bh->b_data;
1400
Mingming Cao617ba132006-10-11 01:20:53 -07001401 bh2 = ext4_append (handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001402 if (!(bh2)) {
1403 brelse(bh);
1404 return retval;
1405 }
Mingming Cao617ba132006-10-11 01:20:53 -07001406 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001407 data1 = bh2->b_data;
1408
1409 /* The 0th block becomes the root, move the dirents out */
1410 fde = &root->dotdot;
Mingming Cao617ba132006-10-11 01:20:53 -07001411 de = (struct ext4_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412 len = ((char *) root) + blocksize - (char *) de;
1413 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001414 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 top = data1 + len;
1416 while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top)
1417 de = de2;
1418 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1419 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001420 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1421 de->rec_len = cpu_to_le16(blocksize - EXT4_DIR_REC_LEN(2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001422 memset (&root->info, 0, sizeof(root->info));
1423 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001424 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 entries = root->entries;
1426 dx_set_block (entries, 1);
1427 dx_set_count (entries, 1);
1428 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1429
1430 /* Initialize as for dx_probe */
1431 hinfo.hash_version = root->info.hash_version;
Mingming Cao617ba132006-10-11 01:20:53 -07001432 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1433 ext4fs_dirhash(name, namelen, &hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 frame = frames;
1435 frame->entries = entries;
1436 frame->at = entries;
1437 frame->bh = bh;
1438 bh = bh2;
1439 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1440 dx_release (frames);
1441 if (!(de))
1442 return retval;
1443
1444 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1445}
1446#endif
1447
1448/*
Mingming Cao617ba132006-10-11 01:20:53 -07001449 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 *
1451 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07001452 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 *
1454 * NOTE!! The inode part of 'de' is left at 0 - which means you
1455 * may not sleep between calling this and putting something into
1456 * the entry, as someone else might have used it while you slept.
1457 */
Mingming Cao617ba132006-10-11 01:20:53 -07001458static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 struct inode *inode)
1460{
1461 struct inode *dir = dentry->d_parent->d_inode;
1462 unsigned long offset;
1463 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001464 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 struct super_block * sb;
1466 int retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001467#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 int dx_fallback=0;
1469#endif
1470 unsigned blocksize;
1471 u32 block, blocks;
1472
1473 sb = dir->i_sb;
1474 blocksize = sb->s_blocksize;
1475 if (!dentry->d_name.len)
1476 return -EINVAL;
Mingming Cao617ba132006-10-11 01:20:53 -07001477#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001479 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 if (!retval || (retval != ERR_BAD_DX_DIR))
1481 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001482 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07001484 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 }
1486#endif
1487 blocks = dir->i_size >> sb->s_blocksize_bits;
1488 for (block = 0, offset = 0; block < blocks; block++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001489 bh = ext4_bread(handle, dir, block, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 if(!bh)
1491 return retval;
1492 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1493 if (retval != -ENOSPC)
1494 return retval;
1495
Mingming Cao617ba132006-10-11 01:20:53 -07001496#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 if (blocks == 1 && !dx_fallback &&
Mingming Cao617ba132006-10-11 01:20:53 -07001498 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001499 return make_indexed_dir(handle, dentry, inode, bh);
1500#endif
1501 brelse(bh);
1502 }
Mingming Cao617ba132006-10-11 01:20:53 -07001503 bh = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 if (!bh)
1505 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001506 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 de->inode = 0;
1508 de->rec_len = cpu_to_le16(blocksize);
1509 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1510}
1511
Mingming Cao617ba132006-10-11 01:20:53 -07001512#ifdef CONFIG_EXT4_INDEX
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513/*
1514 * Returns 0 for success, or a negative error value
1515 */
Mingming Cao617ba132006-10-11 01:20:53 -07001516static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001517 struct inode *inode)
1518{
1519 struct dx_frame frames[2], *frame;
1520 struct dx_entry *entries, *at;
1521 struct dx_hash_info hinfo;
1522 struct buffer_head * bh;
1523 struct inode *dir = dentry->d_parent->d_inode;
1524 struct super_block * sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001525 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 int err;
1527
1528 frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
1529 if (!frame)
1530 return err;
1531 entries = frame->entries;
1532 at = frame->at;
1533
Mingming Cao617ba132006-10-11 01:20:53 -07001534 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535 goto cleanup;
1536
1537 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001538 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539 if (err)
1540 goto journal_error;
1541
1542 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1543 if (err != -ENOSPC) {
1544 bh = NULL;
1545 goto cleanup;
1546 }
1547
1548 /* Block full, should compress but for now just split */
1549 dxtrace(printk("using %u of %u node entries\n",
1550 dx_get_count(entries), dx_get_limit(entries)));
1551 /* Need to split index? */
1552 if (dx_get_count(entries) == dx_get_limit(entries)) {
1553 u32 newblock;
1554 unsigned icount = dx_get_count(entries);
1555 int levels = frame - frames;
1556 struct dx_entry *entries2;
1557 struct dx_node *node2;
1558 struct buffer_head *bh2;
1559
1560 if (levels && (dx_get_count(frames->entries) ==
1561 dx_get_limit(frames->entries))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001562 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 "Directory index full!");
1564 err = -ENOSPC;
1565 goto cleanup;
1566 }
Mingming Cao617ba132006-10-11 01:20:53 -07001567 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001568 if (!(bh2))
1569 goto cleanup;
1570 node2 = (struct dx_node *)(bh2->b_data);
1571 entries2 = node2->entries;
1572 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1573 node2->fake.inode = 0;
1574 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001575 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001576 if (err)
1577 goto journal_error;
1578 if (levels) {
1579 unsigned icount1 = icount/2, icount2 = icount - icount1;
1580 unsigned hash2 = dx_get_hash(entries + icount1);
1581 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1582
1583 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07001584 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 frames[0].bh);
1586 if (err)
1587 goto journal_error;
1588
1589 memcpy ((char *) entries2, (char *) (entries + icount1),
1590 icount2 * sizeof(struct dx_entry));
1591 dx_set_count (entries, icount1);
1592 dx_set_count (entries2, icount2);
1593 dx_set_limit (entries2, dx_node_limit(dir));
1594
1595 /* Which index block gets the new entry? */
1596 if (at - entries >= icount1) {
1597 frame->at = at = at - entries - icount1 + entries2;
1598 frame->entries = entries = entries2;
1599 swap(frame->bh, bh2);
1600 }
1601 dx_insert_block (frames + 0, hash2, newblock);
1602 dxtrace(dx_show_index ("node", frames[1].entries));
1603 dxtrace(dx_show_index ("node",
1604 ((struct dx_node *) bh2->b_data)->entries));
Mingming Cao617ba132006-10-11 01:20:53 -07001605 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 if (err)
1607 goto journal_error;
1608 brelse (bh2);
1609 } else {
1610 dxtrace(printk("Creating second level index...\n"));
1611 memcpy((char *) entries2, (char *) entries,
1612 icount * sizeof(struct dx_entry));
1613 dx_set_limit(entries2, dx_node_limit(dir));
1614
1615 /* Set up root */
1616 dx_set_count(entries, 1);
1617 dx_set_block(entries + 0, newblock);
1618 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1619
1620 /* Add new access path frame */
1621 frame = frames + 1;
1622 frame->at = at = at - entries + entries2;
1623 frame->entries = entries = entries2;
1624 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07001625 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626 frame->bh);
1627 if (err)
1628 goto journal_error;
1629 }
Mingming Cao617ba132006-10-11 01:20:53 -07001630 ext4_journal_dirty_metadata(handle, frames[0].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 }
1632 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1633 if (!de)
1634 goto cleanup;
1635 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1636 bh = NULL;
1637 goto cleanup;
1638
1639journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07001640 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641cleanup:
1642 if (bh)
1643 brelse(bh);
1644 dx_release(frames);
1645 return err;
1646}
1647#endif
1648
1649/*
Mingming Cao617ba132006-10-11 01:20:53 -07001650 * ext4_delete_entry deletes a directory entry by merging it with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 * previous entry
1652 */
Mingming Cao617ba132006-10-11 01:20:53 -07001653static int ext4_delete_entry (handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654 struct inode * dir,
Mingming Cao617ba132006-10-11 01:20:53 -07001655 struct ext4_dir_entry_2 * de_del,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 struct buffer_head * bh)
1657{
Mingming Cao617ba132006-10-11 01:20:53 -07001658 struct ext4_dir_entry_2 * de, * pde;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659 int i;
1660
1661 i = 0;
1662 pde = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001663 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 while (i < bh->b_size) {
Mingming Cao617ba132006-10-11 01:20:53 -07001665 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666 return -EIO;
1667 if (de == de_del) {
1668 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001669 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670 if (pde)
1671 pde->rec_len =
1672 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1673 le16_to_cpu(de->rec_len));
1674 else
1675 de->inode = 0;
1676 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001677 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
1678 ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 return 0;
1680 }
1681 i += le16_to_cpu(de->rec_len);
1682 pde = de;
Mingming Cao617ba132006-10-11 01:20:53 -07001683 de = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 ((char *) de + le16_to_cpu(de->rec_len));
1685 }
1686 return -ENOENT;
1687}
1688
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001689/*
1690 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
1691 * since this indicates that nlinks count was previously 1.
1692 */
1693static void ext4_inc_count(handle_t *handle, struct inode *inode)
1694{
1695 inc_nlink(inode);
1696 if (is_dx(inode) && inode->i_nlink > 1) {
1697 /* limit is 16-bit i_links_count */
1698 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
1699 inode->i_nlink = 1;
1700 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
1701 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
1702 }
1703 }
1704}
1705
1706/*
1707 * If a directory had nlink == 1, then we should let it be 1. This indicates
1708 * directory has >EXT4_LINK_MAX subdirs.
1709 */
1710static void ext4_dec_count(handle_t *handle, struct inode *inode)
1711{
1712 drop_nlink(inode);
1713 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
1714 inc_nlink(inode);
1715}
1716
1717
Mingming Cao617ba132006-10-11 01:20:53 -07001718static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 struct dentry *dentry, struct inode *inode)
1720{
Mingming Cao617ba132006-10-11 01:20:53 -07001721 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001723 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 d_instantiate(dentry, inode);
1725 return 0;
1726 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08001727 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 iput(inode);
1729 return err;
1730}
1731
1732/*
1733 * By the time this is called, we already have created
1734 * the directory cache entry for the new file, but it
1735 * is so far negative - it has no inode.
1736 *
1737 * If the create succeeds, we fill in the inode information
1738 * with d_instantiate().
1739 */
Mingming Cao617ba132006-10-11 01:20:53 -07001740static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 struct nameidata *nd)
1742{
1743 handle_t *handle;
1744 struct inode * inode;
1745 int err, retries = 0;
1746
1747retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001748 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1749 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1750 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751 if (IS_ERR(handle))
1752 return PTR_ERR(handle);
1753
1754 if (IS_DIRSYNC(dir))
1755 handle->h_sync = 1;
1756
Mingming Cao617ba132006-10-11 01:20:53 -07001757 inode = ext4_new_inode (handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 err = PTR_ERR(inode);
1759 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001760 inode->i_op = &ext4_file_inode_operations;
1761 inode->i_fop = &ext4_file_operations;
1762 ext4_set_aops(inode);
1763 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764 }
Mingming Cao617ba132006-10-11 01:20:53 -07001765 ext4_journal_stop(handle);
1766 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767 goto retry;
1768 return err;
1769}
1770
Mingming Cao617ba132006-10-11 01:20:53 -07001771static int ext4_mknod (struct inode * dir, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772 int mode, dev_t rdev)
1773{
1774 handle_t *handle;
1775 struct inode *inode;
1776 int err, retries = 0;
1777
1778 if (!new_valid_dev(rdev))
1779 return -EINVAL;
1780
1781retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001782 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1783 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1784 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001785 if (IS_ERR(handle))
1786 return PTR_ERR(handle);
1787
1788 if (IS_DIRSYNC(dir))
1789 handle->h_sync = 1;
1790
Mingming Cao617ba132006-10-11 01:20:53 -07001791 inode = ext4_new_inode (handle, dir, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001792 err = PTR_ERR(inode);
1793 if (!IS_ERR(inode)) {
1794 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07001795#ifdef CONFIG_EXT4DEV_FS_XATTR
1796 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001797#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001798 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001799 }
Mingming Cao617ba132006-10-11 01:20:53 -07001800 ext4_journal_stop(handle);
1801 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001802 goto retry;
1803 return err;
1804}
1805
Mingming Cao617ba132006-10-11 01:20:53 -07001806static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807{
1808 handle_t *handle;
1809 struct inode * inode;
1810 struct buffer_head * dir_block;
Mingming Cao617ba132006-10-11 01:20:53 -07001811 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001812 int err, retries = 0;
1813
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001814 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815 return -EMLINK;
1816
1817retry:
Mingming Cao617ba132006-10-11 01:20:53 -07001818 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
1819 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
1820 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821 if (IS_ERR(handle))
1822 return PTR_ERR(handle);
1823
1824 if (IS_DIRSYNC(dir))
1825 handle->h_sync = 1;
1826
Mingming Cao617ba132006-10-11 01:20:53 -07001827 inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001828 err = PTR_ERR(inode);
1829 if (IS_ERR(inode))
1830 goto out_stop;
1831
Mingming Cao617ba132006-10-11 01:20:53 -07001832 inode->i_op = &ext4_dir_inode_operations;
1833 inode->i_fop = &ext4_dir_operations;
1834 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1835 dir_block = ext4_bread (handle, inode, 0, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 if (!dir_block) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001837 ext4_dec_count(handle, inode); /* is this nlink == 0? */
Mingming Cao617ba132006-10-11 01:20:53 -07001838 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001839 iput (inode);
1840 goto out_stop;
1841 }
1842 BUFFER_TRACE(dir_block, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001843 ext4_journal_get_write_access(handle, dir_block);
1844 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 de->inode = cpu_to_le32(inode->i_ino);
1846 de->name_len = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001847 de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de->name_len));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001848 strcpy (de->name, ".");
Mingming Cao617ba132006-10-11 01:20:53 -07001849 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
1850 de = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001851 ((char *) de + le16_to_cpu(de->rec_len));
1852 de->inode = cpu_to_le32(dir->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001853 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT4_DIR_REC_LEN(1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 de->name_len = 2;
1855 strcpy (de->name, "..");
Mingming Cao617ba132006-10-11 01:20:53 -07001856 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001857 inode->i_nlink = 2;
Mingming Cao617ba132006-10-11 01:20:53 -07001858 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
1859 ext4_journal_dirty_metadata(handle, dir_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860 brelse (dir_block);
Mingming Cao617ba132006-10-11 01:20:53 -07001861 ext4_mark_inode_dirty(handle, inode);
1862 err = ext4_add_entry (handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 if (err) {
1864 inode->i_nlink = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001865 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 iput (inode);
1867 goto out_stop;
1868 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04001869 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001870 ext4_update_dx_flag(dir);
1871 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 d_instantiate(dentry, inode);
1873out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07001874 ext4_journal_stop(handle);
1875 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 goto retry;
1877 return err;
1878}
1879
1880/*
1881 * routine to check that the specified directory is empty (for rmdir)
1882 */
1883static int empty_dir (struct inode * inode)
1884{
1885 unsigned long offset;
1886 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001887 struct ext4_dir_entry_2 * de, * de1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 struct super_block * sb;
1889 int err = 0;
1890
1891 sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001892 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
1893 !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001894 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001895 ext4_error(inode->i_sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 "error %d reading directory #%lu offset 0",
1897 err, inode->i_ino);
1898 else
Mingming Cao617ba132006-10-11 01:20:53 -07001899 ext4_warning(inode->i_sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 "bad directory (dir #%lu) - no data block",
1901 inode->i_ino);
1902 return 1;
1903 }
Mingming Cao617ba132006-10-11 01:20:53 -07001904 de = (struct ext4_dir_entry_2 *) bh->b_data;
1905 de1 = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906 ((char *) de + le16_to_cpu(de->rec_len));
1907 if (le32_to_cpu(de->inode) != inode->i_ino ||
1908 !le32_to_cpu(de1->inode) ||
1909 strcmp (".", de->name) ||
1910 strcmp ("..", de1->name)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001911 ext4_warning (inode->i_sb, "empty_dir",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001912 "bad directory (dir #%lu) - no `.' or `..'",
1913 inode->i_ino);
1914 brelse (bh);
1915 return 1;
1916 }
1917 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001918 de = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919 ((char *) de1 + le16_to_cpu(de1->rec_len));
1920 while (offset < inode->i_size ) {
1921 if (!bh ||
1922 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1923 err = 0;
1924 brelse (bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001925 bh = ext4_bread (NULL, inode,
1926 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001927 if (!bh) {
1928 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001929 ext4_error(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 "error %d reading directory"
1931 " #%lu offset %lu",
1932 err, inode->i_ino, offset);
1933 offset += sb->s_blocksize;
1934 continue;
1935 }
Mingming Cao617ba132006-10-11 01:20:53 -07001936 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001937 }
Mingming Cao617ba132006-10-11 01:20:53 -07001938 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1939 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940 sb->s_blocksize);
1941 offset = (offset | (sb->s_blocksize - 1)) + 1;
1942 continue;
1943 }
1944 if (le32_to_cpu(de->inode)) {
1945 brelse (bh);
1946 return 0;
1947 }
1948 offset += le16_to_cpu(de->rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001949 de = (struct ext4_dir_entry_2 *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 ((char *) de + le16_to_cpu(de->rec_len));
1951 }
1952 brelse (bh);
1953 return 1;
1954}
1955
Mingming Cao617ba132006-10-11 01:20:53 -07001956/* ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001957 * such inodes, starting at the superblock, in case we crash before the
1958 * file is closed/deleted, or in case the inode truncate spans multiple
1959 * transactions and the last transaction is not recovered after a crash.
1960 *
1961 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07001962 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963 */
Mingming Cao617ba132006-10-11 01:20:53 -07001964int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965{
1966 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001967 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968 int err = 0, rc;
1969
1970 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07001971 if (!list_empty(&EXT4_I(inode)->i_orphan))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 goto out_unlock;
1973
1974 /* Orphan handling is only valid for files with data blocks
1975 * being truncated, or files being unlinked. */
1976
1977 /* @@@ FIXME: Observation from aviro:
Mingming Cao617ba132006-10-11 01:20:53 -07001978 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
1979 * here (on lock_super()), so race with ext4_link() which might bump
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001980 * ->i_nlink. For, say it, character device. Not a regular file,
1981 * not a directory, not a symlink and ->i_nlink > 0.
1982 */
1983 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1984 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1985
Mingming Cao617ba132006-10-11 01:20:53 -07001986 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
1987 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 if (err)
1989 goto out_unlock;
1990
Mingming Cao617ba132006-10-11 01:20:53 -07001991 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 if (err)
1993 goto out_unlock;
1994
1995 /* Insert this inode at the head of the on-disk orphan list... */
Mingming Cao617ba132006-10-11 01:20:53 -07001996 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
1997 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1998 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1999 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000 if (!err)
2001 err = rc;
2002
2003 /* Only add to the head of the in-memory list if all the
2004 * previous operations succeeded. If the orphan_add is going to
2005 * fail (possibly taking the journal offline), we can't risk
2006 * leaving the inode on the orphan list: stray orphan-list
2007 * entries can cause panics at unmount time.
2008 *
2009 * This is safe: on error we're going to ignore the orphan list
2010 * anyway on the next recovery. */
2011 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07002012 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013
2014 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2015 jbd_debug(4, "orphan inode %lu will point to %d\n",
2016 inode->i_ino, NEXT_ORPHAN(inode));
2017out_unlock:
2018 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002019 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020 return err;
2021}
2022
2023/*
Mingming Cao617ba132006-10-11 01:20:53 -07002024 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 * of such inodes stored on disk, because it is finally being cleaned up.
2026 */
Mingming Cao617ba132006-10-11 01:20:53 -07002027int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002028{
2029 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002030 struct ext4_inode_info *ei = EXT4_I(inode);
2031 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 unsigned long ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002033 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 int err = 0;
2035
2036 lock_super(inode->i_sb);
2037 if (list_empty(&ei->i_orphan)) {
2038 unlock_super(inode->i_sb);
2039 return 0;
2040 }
2041
2042 ino_next = NEXT_ORPHAN(inode);
2043 prev = ei->i_orphan.prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002044 sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045
2046 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2047
2048 list_del_init(&ei->i_orphan);
2049
2050 /* If we're on an error path, we may not have a valid
2051 * transaction handle with which to update the orphan list on
2052 * disk, but we still need to remove the inode from the linked
2053 * list in memory. */
2054 if (!handle)
2055 goto out;
2056
Mingming Cao617ba132006-10-11 01:20:53 -07002057 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058 if (err)
2059 goto out_err;
2060
2061 if (prev == &sbi->s_orphan) {
2062 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2063 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002064 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065 if (err)
2066 goto out_brelse;
2067 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002068 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002069 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002070 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002072 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002073
2074 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2075 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002076 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 if (err)
2078 goto out_brelse;
2079 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002080 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 }
2082 if (err)
2083 goto out_brelse;
2084 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002085 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086
2087out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002088 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089out:
2090 unlock_super(inode->i_sb);
2091 return err;
2092
2093out_brelse:
2094 brelse(iloc.bh);
2095 goto out_err;
2096}
2097
Mingming Cao617ba132006-10-11 01:20:53 -07002098static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099{
2100 int retval;
2101 struct inode * inode;
2102 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002103 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 handle_t *handle;
2105
2106 /* Initialize quotas before so that eventual writes go in
2107 * separate transaction */
2108 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002109 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 if (IS_ERR(handle))
2111 return PTR_ERR(handle);
2112
2113 retval = -ENOENT;
Mingming Cao617ba132006-10-11 01:20:53 -07002114 bh = ext4_find_entry (dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115 if (!bh)
2116 goto end_rmdir;
2117
2118 if (IS_DIRSYNC(dir))
2119 handle->h_sync = 1;
2120
2121 inode = dentry->d_inode;
2122
2123 retval = -EIO;
2124 if (le32_to_cpu(de->inode) != inode->i_ino)
2125 goto end_rmdir;
2126
2127 retval = -ENOTEMPTY;
2128 if (!empty_dir (inode))
2129 goto end_rmdir;
2130
Mingming Cao617ba132006-10-11 01:20:53 -07002131 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 if (retval)
2133 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002134 if (!EXT4_DIR_LINK_EMPTY(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07002135 ext4_warning (inode->i_sb, "ext4_rmdir",
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002136 "empty directory has too many links (%d)",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002137 inode->i_nlink);
2138 inode->i_version++;
2139 clear_nlink(inode);
2140 /* There's no need to set i_disksize: the fact that i_nlink is
2141 * zero will ensure that the right thing happens during any
2142 * recovery. */
2143 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002144 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002145 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002146 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002147 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002148 ext4_update_dx_flag(dir);
2149 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150
2151end_rmdir:
Mingming Cao617ba132006-10-11 01:20:53 -07002152 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 brelse (bh);
2154 return retval;
2155}
2156
Mingming Cao617ba132006-10-11 01:20:53 -07002157static int ext4_unlink(struct inode * dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158{
2159 int retval;
2160 struct inode * inode;
2161 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002162 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 handle_t *handle;
2164
2165 /* Initialize quotas before so that eventual writes go
2166 * in separate transaction */
2167 DQUOT_INIT(dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002168 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 if (IS_ERR(handle))
2170 return PTR_ERR(handle);
2171
2172 if (IS_DIRSYNC(dir))
2173 handle->h_sync = 1;
2174
2175 retval = -ENOENT;
Mingming Cao617ba132006-10-11 01:20:53 -07002176 bh = ext4_find_entry (dentry, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177 if (!bh)
2178 goto end_unlink;
2179
2180 inode = dentry->d_inode;
2181
2182 retval = -EIO;
2183 if (le32_to_cpu(de->inode) != inode->i_ino)
2184 goto end_unlink;
2185
2186 if (!inode->i_nlink) {
Mingming Cao617ba132006-10-11 01:20:53 -07002187 ext4_warning (inode->i_sb, "ext4_unlink",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002188 "Deleting nonexistent file (%lu), %d",
2189 inode->i_ino, inode->i_nlink);
2190 inode->i_nlink = 1;
2191 }
Mingming Cao617ba132006-10-11 01:20:53 -07002192 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 if (retval)
2194 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04002195 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002196 ext4_update_dx_flag(dir);
2197 ext4_mark_inode_dirty(handle, dir);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002198 ext4_dec_count(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002200 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002201 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002202 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002203 retval = 0;
2204
2205end_unlink:
Mingming Cao617ba132006-10-11 01:20:53 -07002206 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002207 brelse (bh);
2208 return retval;
2209}
2210
Mingming Cao617ba132006-10-11 01:20:53 -07002211static int ext4_symlink (struct inode * dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002212 struct dentry *dentry, const char * symname)
2213{
2214 handle_t *handle;
2215 struct inode * inode;
2216 int l, err, retries = 0;
2217
2218 l = strlen(symname)+1;
2219 if (l > dir->i_sb->s_blocksize)
2220 return -ENAMETOOLONG;
2221
2222retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002223 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2224 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
2225 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002226 if (IS_ERR(handle))
2227 return PTR_ERR(handle);
2228
2229 if (IS_DIRSYNC(dir))
2230 handle->h_sync = 1;
2231
Mingming Cao617ba132006-10-11 01:20:53 -07002232 inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 err = PTR_ERR(inode);
2234 if (IS_ERR(inode))
2235 goto out_stop;
2236
Mingming Cao617ba132006-10-11 01:20:53 -07002237 if (l > sizeof (EXT4_I(inode)->i_data)) {
2238 inode->i_op = &ext4_symlink_inode_operations;
2239 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002240 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002241 * page_symlink() calls into ext4_prepare/commit_write.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 * We have a transaction open. All is sweetness. It also sets
2243 * i_size in generic_commit_write().
2244 */
2245 err = __page_symlink(inode, symname, l,
2246 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
2247 if (err) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002248 ext4_dec_count(handle, inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002249 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002250 iput (inode);
2251 goto out_stop;
2252 }
2253 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002254 inode->i_op = &ext4_fast_symlink_inode_operations;
2255 memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 inode->i_size = l-1;
2257 }
Mingming Cao617ba132006-10-11 01:20:53 -07002258 EXT4_I(inode)->i_disksize = inode->i_size;
2259 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07002261 ext4_journal_stop(handle);
2262 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263 goto retry;
2264 return err;
2265}
2266
Mingming Cao617ba132006-10-11 01:20:53 -07002267static int ext4_link (struct dentry * old_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268 struct inode * dir, struct dentry *dentry)
2269{
2270 handle_t *handle;
2271 struct inode *inode = old_dentry->d_inode;
2272 int err, retries = 0;
2273
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002274 if (EXT4_DIR_LINK_MAX(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002275 return -EMLINK;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002276
Eric Sandeen2988a772007-02-10 01:46:16 -08002277 /*
2278 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
2279 * otherwise has the potential to corrupt the orphan inode list.
2280 */
2281 if (inode->i_nlink == 0)
2282 return -ENOENT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283
2284retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002285 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2286 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 if (IS_ERR(handle))
2288 return PTR_ERR(handle);
2289
2290 if (IS_DIRSYNC(dir))
2291 handle->h_sync = 1;
2292
Kalpak Shahef7f3832007-07-18 09:15:20 -04002293 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002294 ext4_inc_count(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 atomic_inc(&inode->i_count);
2296
Mingming Cao617ba132006-10-11 01:20:53 -07002297 err = ext4_add_nondir(handle, dentry, inode);
2298 ext4_journal_stop(handle);
2299 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 goto retry;
2301 return err;
2302}
2303
2304#define PARENT_INO(buffer) \
Mingming Cao617ba132006-10-11 01:20:53 -07002305 ((struct ext4_dir_entry_2 *) ((char *) buffer + \
2306 le16_to_cpu(((struct ext4_dir_entry_2 *) buffer)->rec_len)))->inode
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307
2308/*
2309 * Anybody can rename anything with this: the permission checks are left to the
2310 * higher-level routines.
2311 */
Mingming Cao617ba132006-10-11 01:20:53 -07002312static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313 struct inode * new_dir,struct dentry *new_dentry)
2314{
2315 handle_t *handle;
2316 struct inode * old_inode, * new_inode;
2317 struct buffer_head * old_bh, * new_bh, * dir_bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002318 struct ext4_dir_entry_2 * old_de, * new_de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 int retval;
2320
2321 old_bh = new_bh = dir_bh = NULL;
2322
2323 /* Initialize quotas before so that eventual writes go
2324 * in separate transaction */
2325 if (new_dentry->d_inode)
2326 DQUOT_INIT(new_dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002327 handle = ext4_journal_start(old_dir, 2 *
2328 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2329 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002330 if (IS_ERR(handle))
2331 return PTR_ERR(handle);
2332
2333 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2334 handle->h_sync = 1;
2335
Mingming Cao617ba132006-10-11 01:20:53 -07002336 old_bh = ext4_find_entry (old_dentry, &old_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002337 /*
2338 * Check for inode number is _not_ due to possible IO errors.
2339 * We might rmdir the source, keep it as pwd of some process
2340 * and merrily kill the link to whatever was created under the
2341 * same name. Goodbye sticky bit ;-<
2342 */
2343 old_inode = old_dentry->d_inode;
2344 retval = -ENOENT;
2345 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2346 goto end_rename;
2347
2348 new_inode = new_dentry->d_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07002349 new_bh = ext4_find_entry (new_dentry, &new_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350 if (new_bh) {
2351 if (!new_inode) {
2352 brelse (new_bh);
2353 new_bh = NULL;
2354 }
2355 }
2356 if (S_ISDIR(old_inode->i_mode)) {
2357 if (new_inode) {
2358 retval = -ENOTEMPTY;
2359 if (!empty_dir (new_inode))
2360 goto end_rename;
2361 }
2362 retval = -EIO;
Mingming Cao617ba132006-10-11 01:20:53 -07002363 dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002364 if (!dir_bh)
2365 goto end_rename;
2366 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2367 goto end_rename;
2368 retval = -EMLINK;
2369 if (!new_inode && new_dir!=old_dir &&
Mingming Cao617ba132006-10-11 01:20:53 -07002370 new_dir->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002371 goto end_rename;
2372 }
2373 if (!new_bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07002374 retval = ext4_add_entry (handle, new_dentry, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375 if (retval)
2376 goto end_rename;
2377 } else {
2378 BUFFER_TRACE(new_bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07002379 ext4_journal_get_write_access(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 new_de->inode = cpu_to_le32(old_inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002381 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2382 EXT4_FEATURE_INCOMPAT_FILETYPE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 new_de->file_type = old_de->file_type;
2384 new_dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07002385 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
2386 ext4_journal_dirty_metadata(handle, new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002387 brelse(new_bh);
2388 new_bh = NULL;
2389 }
2390
2391 /*
2392 * Like most other Unix systems, set the ctime for inodes on a
2393 * rename.
2394 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04002395 old_inode->i_ctime = ext4_current_time(old_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002396 ext4_mark_inode_dirty(handle, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002397
2398 /*
2399 * ok, that's it
2400 */
2401 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2402 old_de->name_len != old_dentry->d_name.len ||
2403 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
Mingming Cao617ba132006-10-11 01:20:53 -07002404 (retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002405 old_de, old_bh)) == -ENOENT) {
2406 /* old_de could have moved from under us during htree split, so
2407 * make sure that we are deleting the right entry. We might
2408 * also be pointing to a stale entry in the unused part of
2409 * old_bh so just checking inum and the name isn't enough. */
2410 struct buffer_head *old_bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002411 struct ext4_dir_entry_2 *old_de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412
Mingming Cao617ba132006-10-11 01:20:53 -07002413 old_bh2 = ext4_find_entry(old_dentry, &old_de2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 if (old_bh2) {
Mingming Cao617ba132006-10-11 01:20:53 -07002415 retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002416 old_de2, old_bh2);
2417 brelse(old_bh2);
2418 }
2419 }
2420 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002421 ext4_warning(old_dir->i_sb, "ext4_rename",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422 "Deleting old file (%lu), %d, error=%d",
2423 old_dir->i_ino, old_dir->i_nlink, retval);
2424 }
2425
2426 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002427 ext4_dec_count(handle, new_inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002428 new_inode->i_ctime = ext4_current_time(new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002430 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002431 ext4_update_dx_flag(old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002432 if (dir_bh) {
2433 BUFFER_TRACE(dir_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002434 ext4_journal_get_write_access(handle, dir_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002435 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002436 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
2437 ext4_journal_dirty_metadata(handle, dir_bh);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002438 ext4_dec_count(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002440 /* checked empty_dir above, can't have another parent,
2441 * ext3_dec_count() won't work for many-linked dirs */
2442 new_inode->i_nlink = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002443 } else {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002444 ext4_inc_count(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002445 ext4_update_dx_flag(new_dir);
2446 ext4_mark_inode_dirty(handle, new_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002447 }
2448 }
Mingming Cao617ba132006-10-11 01:20:53 -07002449 ext4_mark_inode_dirty(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450 if (new_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07002451 ext4_mark_inode_dirty(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002452 if (!new_inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002453 ext4_orphan_add(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454 }
2455 retval = 0;
2456
2457end_rename:
2458 brelse (dir_bh);
2459 brelse (old_bh);
2460 brelse (new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002461 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 return retval;
2463}
2464
2465/*
2466 * directories can handle most operations...
2467 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08002468const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002469 .create = ext4_create,
2470 .lookup = ext4_lookup,
2471 .link = ext4_link,
2472 .unlink = ext4_unlink,
2473 .symlink = ext4_symlink,
2474 .mkdir = ext4_mkdir,
2475 .rmdir = ext4_rmdir,
2476 .mknod = ext4_mknod,
2477 .rename = ext4_rename,
2478 .setattr = ext4_setattr,
2479#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 .setxattr = generic_setxattr,
2481 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002482 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483 .removexattr = generic_removexattr,
2484#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002485 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002486};
2487
Arjan van de Ven754661f2007-02-12 00:55:38 -08002488const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07002489 .setattr = ext4_setattr,
2490#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002491 .setxattr = generic_setxattr,
2492 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07002493 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002494 .removexattr = generic_removexattr,
2495#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002496 .permission = ext4_permission,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497};