blob: 88f64eb1b6faaaa12baba47ca8fd74ad371dd429 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/namei.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
Dave Kleikampe9ad5622006-09-27 01:49:35 -070018 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Hash Tree Directory indexing (c)
Dave Kleikampe9ad5622006-09-27 01:49:35 -070020 * Daniel Phillips, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Hash Tree Directory indexing porting
Dave Kleikampe9ad5622006-09-27 01:49:35 -070022 * Christopher Li, 2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Hash Tree Directory indexing cleanup
Dave Kleikampe9ad5622006-09-27 01:49:35 -070024 * Theodore Ts'o, 2002
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/quotaops.h>
Al Viro4613ad12012-03-29 22:30:07 -040028#include "ext3.h"
Ben Dooks381be2542005-10-30 15:02:56 -080029#include "namei.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xattr.h"
31#include "acl.h"
32
33/*
34 * define how far ahead to read directories while searching them.
35 */
36#define NAMEI_RA_CHUNKS 2
37#define NAMEI_RA_BLOCKS 4
38#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static struct buffer_head *ext3_append(handle_t *handle,
41 struct inode *inode,
42 u32 *block, int *err)
43{
44 struct buffer_head *bh;
45
46 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
47
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -030048 if ((bh = ext3_dir_bread(handle, inode, *block, 1, err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 inode->i_size += inode->i_sb->s_blocksize;
50 EXT3_I(inode)->i_disksize = inode->i_size;
Akinobu Mita33575f82008-04-28 02:16:08 -070051 *err = ext3_journal_get_write_access(handle, bh);
52 if (*err) {
53 brelse(bh);
54 bh = NULL;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57 return bh;
58}
59
60#ifndef assert
61#define assert(test) J_ASSERT(test)
62#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef DX_DEBUG
65#define dxtrace(command) command
66#else
Mingming Caoae6ddcc2006-09-27 01:49:27 -070067#define dxtrace(command)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
70struct fake_dirent
71{
72 __le32 inode;
73 __le16 rec_len;
74 u8 name_len;
75 u8 file_type;
76};
77
78struct dx_countlimit
79{
80 __le16 limit;
81 __le16 count;
82};
83
84struct dx_entry
85{
86 __le32 hash;
87 __le32 block;
88};
89
90/*
91 * dx_root_info is laid out so that if it should somehow get overlaid by a
92 * dirent the two low bits of the hash version will be zero. Therefore, the
93 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
94 */
95
96struct dx_root
97{
98 struct fake_dirent dot;
99 char dot_name[4];
100 struct fake_dirent dotdot;
101 char dotdot_name[4];
102 struct dx_root_info
103 {
104 __le32 reserved_zero;
105 u8 hash_version;
106 u8 info_length; /* 8 */
107 u8 indirect_levels;
108 u8 unused_flags;
109 }
110 info;
111 struct dx_entry entries[0];
112};
113
114struct dx_node
115{
116 struct fake_dirent fake;
117 struct dx_entry entries[0];
118};
119
120
121struct dx_frame
122{
123 struct buffer_head *bh;
124 struct dx_entry *entries;
125 struct dx_entry *at;
126};
127
128struct dx_map_entry
129{
130 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700131 u16 offs;
132 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static inline unsigned dx_get_block (struct dx_entry *entry);
136static void dx_set_block (struct dx_entry *entry, unsigned value);
137static inline unsigned dx_get_hash (struct dx_entry *entry);
138static void dx_set_hash (struct dx_entry *entry, unsigned value);
139static unsigned dx_get_count (struct dx_entry *entries);
140static unsigned dx_get_limit (struct dx_entry *entries);
141static void dx_set_count (struct dx_entry *entries, unsigned value);
142static void dx_set_limit (struct dx_entry *entries, unsigned value);
143static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
144static unsigned dx_node_limit (struct inode *dir);
Al Viro734711a2008-08-24 07:26:48 -0400145static struct dx_frame *dx_probe(struct qstr *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct inode *dir,
147 struct dx_hash_info *hinfo,
148 struct dx_frame *frame,
149 int *err);
150static void dx_release (struct dx_frame *frames);
Wei Yongjun45f90212009-04-02 16:57:14 -0700151static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
153static void dx_sort_map(struct dx_map_entry *map, unsigned count);
154static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
155 struct dx_map_entry *offsets, int count);
Wei Yongjun45f90212009-04-02 16:57:14 -0700156static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
158static int ext3_htree_next_block(struct inode *dir, __u32 hash,
159 struct dx_frame *frame,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700160 struct dx_frame *frames,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 __u32 *start_hash);
Al Viro734711a2008-08-24 07:26:48 -0400162static struct buffer_head * ext3_dx_find_entry(struct inode *dir,
163 struct qstr *entry, struct ext3_dir_entry_2 **res_dir,
164 int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
166 struct inode *inode);
167
168/*
Jan Kara7c06a8d2007-11-14 17:00:19 -0800169 * p is at least 6 bytes before the end of page
170 */
171static inline struct ext3_dir_entry_2 *
172ext3_next_entry(struct ext3_dir_entry_2 *p)
173{
174 return (struct ext3_dir_entry_2 *)((char *)p +
175 ext3_rec_len_from_disk(p->rec_len));
176}
177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Future: use high four bits of block for coalesce-on-delete flags
180 * Mask them off for now.
181 */
182
183static inline unsigned dx_get_block (struct dx_entry *entry)
184{
185 return le32_to_cpu(entry->block) & 0x00ffffff;
186}
187
188static inline void dx_set_block (struct dx_entry *entry, unsigned value)
189{
190 entry->block = cpu_to_le32(value);
191}
192
193static inline unsigned dx_get_hash (struct dx_entry *entry)
194{
195 return le32_to_cpu(entry->hash);
196}
197
198static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
199{
200 entry->hash = cpu_to_le32(value);
201}
202
203static inline unsigned dx_get_count (struct dx_entry *entries)
204{
205 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
206}
207
208static inline unsigned dx_get_limit (struct dx_entry *entries)
209{
210 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
211}
212
213static inline void dx_set_count (struct dx_entry *entries, unsigned value)
214{
215 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
216}
217
218static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
219{
220 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
221}
222
223static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
224{
225 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
226 EXT3_DIR_REC_LEN(2) - infosize;
Li Zefan8ef27202008-07-25 01:46:29 -0700227 return entry_space / sizeof(struct dx_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static inline unsigned dx_node_limit (struct inode *dir)
231{
232 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
Li Zefan8ef27202008-07-25 01:46:29 -0700233 return entry_space / sizeof(struct dx_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * Debug
238 */
239#ifdef DX_DEBUG
240static void dx_show_index (char * label, struct dx_entry *entries)
241{
242 int i, n = dx_get_count (entries);
243 printk("%s index ", label);
244 for (i = 0; i < n; i++)
245 {
246 printk("%x->%u ", i? dx_get_hash(entries + i): 0, dx_get_block(entries + i));
247 }
248 printk("\n");
249}
250
251struct stats
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned names;
254 unsigned space;
255 unsigned bcount;
256};
257
258static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
259 int size, int show_names)
260{
261 unsigned names = 0, space = 0;
262 char *base = (char *) de;
263 struct dx_hash_info h = *hinfo;
264
265 printk("names: ");
266 while ((char *) de < base + size)
267 {
268 if (de->inode)
269 {
270 if (show_names)
271 {
272 int len = de->name_len;
273 char *name = de->name;
274 while (len--) printk("%c", *name++);
275 ext3fs_dirhash(de->name, de->name_len, &h);
276 printk(":%x.%u ", h.hash,
Bernd Schubertc878c732011-07-20 23:16:33 +0200277 (unsigned) ((char *) de - base));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 space += EXT3_DIR_REC_LEN(de->name_len);
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700280 names++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Jan Kara7c06a8d2007-11-14 17:00:19 -0800282 de = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 printk("(%i)\n", names);
285 return (struct stats) { names, space, 1 };
286}
287
288struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
289 struct dx_entry *entries, int levels)
290{
291 unsigned blocksize = dir->i_sb->s_blocksize;
292 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
293 unsigned bcount = 0;
294 struct buffer_head *bh;
295 int err;
296 printk("%i indexed blocks...\n", count);
297 for (i = 0; i < count; i++, entries++)
298 {
299 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
300 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
301 struct stats stats;
302 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
303 if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
304 stats = levels?
305 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
306 dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
307 names += stats.names;
308 space += stats.space;
309 bcount += stats.bcount;
310 brelse (bh);
311 }
312 if (bcount)
313 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
314 names, space/bcount,(space/bcount)*100/blocksize);
315 return (struct stats) { names, space, bcount};
316}
317#endif /* DX_DEBUG */
318
319/*
320 * Probe for a directory leaf block to search.
321 *
322 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
323 * error in the directory index, and the caller should fall back to
324 * searching the directory normally. The callers of dx_probe **MUST**
325 * check for this error code, and make sure it never gets reflected
326 * back to userspace.
327 */
328static struct dx_frame *
Al Viro734711a2008-08-24 07:26:48 -0400329dx_probe(struct qstr *entry, struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
331{
332 unsigned count, indirect;
333 struct dx_entry *at, *entries, *p, *q, *m;
334 struct dx_root *root;
335 struct buffer_head *bh;
336 struct dx_frame *frame = frame_in;
337 u32 hash;
338
339 frame->bh = NULL;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300340 if (!(bh = ext3_dir_bread(NULL, dir, 0, 0, err))) {
341 *err = ERR_BAD_DX_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto fail;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 root = (struct dx_root *) bh->b_data;
345 if (root->info.hash_version != DX_HASH_TEA &&
346 root->info.hash_version != DX_HASH_HALF_MD4 &&
347 root->info.hash_version != DX_HASH_LEGACY) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700348 ext3_warning(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 "Unrecognised inode hash code %d",
350 root->info.hash_version);
351 brelse(bh);
352 *err = ERR_BAD_DX_DIR;
353 goto fail;
354 }
355 hinfo->hash_version = root->info.hash_version;
Theodore Ts'o5e1f8c92008-10-28 13:21:55 -0400356 if (hinfo->hash_version <= DX_HASH_TEA)
357 hinfo->hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
Al Viro734711a2008-08-24 07:26:48 -0400359 if (entry)
360 ext3fs_dirhash(entry->name, entry->len, hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 hash = hinfo->hash;
362
363 if (root->info.unused_flags & 1) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700364 ext3_warning(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -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) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700373 ext3_warning(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -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)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700386 ext3_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700387 "dx entry: limit != root limit");
388 brelse(bh);
389 *err = ERR_BAD_DX_DIR;
390 goto fail;
391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -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)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700398 ext3_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700399 "dx entry: no count or count > limit");
400 brelse(bh);
401 *err = ERR_BAD_DX_DIR;
402 goto fail2;
403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -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;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300439 if (!(bh = ext3_dir_bread(NULL, dir, dx_get_block(at), 0, err))) {
440 *err = ERR_BAD_DX_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto fail2;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 at = entries = ((struct dx_node *) bh->b_data)->entries;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700444 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700445 ext3_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700446 "dx entry: limit != node limit");
447 brelse(bh);
448 *err = ERR_BAD_DX_DIR;
449 goto fail2;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700452 frame->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454fail2:
455 while (frame >= frame_in) {
456 brelse(frame->bh);
457 frame--;
458 }
459fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700460 if (*err == ERR_BAD_DX_DIR)
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700461 ext3_warning(dir->i_sb, __func__,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700462 "Corrupt dir inode %ld, running e2fsck is "
463 "recommended.", dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return NULL;
465}
466
467static void dx_release (struct dx_frame *frames)
468{
469 if (frames[0].bh == NULL)
470 return;
471
472 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
473 brelse(frames[1].bh);
474 brelse(frames[0].bh);
475}
476
477/*
478 * This function increments the frame pointer to search the next leaf
479 * block, and reads in the necessary intervening nodes if the search
480 * should be necessary. Whether or not the search is necessary is
481 * controlled by the hash parameter. If the hash value is even, then
482 * the search is only continued if the next block starts with that
483 * hash value. This is used if we are searching for a specific file.
484 *
485 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
486 *
487 * This function returns 1 if the caller should continue to search,
488 * or 0 if it should not. If there is an error reading one of the
489 * index blocks, it will a negative error code.
490 *
491 * If start_hash is non-null, it will be filled in with the starting
492 * hash of the next page.
493 */
494static int ext3_htree_next_block(struct inode *dir, __u32 hash,
495 struct dx_frame *frame,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700496 struct dx_frame *frames,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 __u32 *start_hash)
498{
499 struct dx_frame *p;
500 struct buffer_head *bh;
501 int err, num_frames = 0;
502 __u32 bhash;
503
504 p = frame;
505 /*
506 * Find the next leaf page by incrementing the frame pointer.
507 * If we run out of entries in the interior node, loop around and
508 * increment pointer in the parent node. When we break out of
509 * this loop, num_frames indicates the number of interior
510 * nodes need to be read.
511 */
512 while (1) {
513 if (++(p->at) < p->entries + dx_get_count(p->entries))
514 break;
515 if (p == frames)
516 return 0;
517 num_frames++;
518 p--;
519 }
520
521 /*
522 * If the hash is 1, then continue only if the next page has a
523 * continuation hash of any value. This is used for readdir
524 * handling. Otherwise, check to see if the hash matches the
525 * desired contiuation hash. If it doesn't, return since
526 * there's no point to read in the successive index pages.
527 */
528 bhash = dx_get_hash(p->at);
529 if (start_hash)
530 *start_hash = bhash;
531 if ((hash & 1) == 0) {
532 if ((bhash & ~1) != hash)
533 return 0;
534 }
535 /*
536 * If the hash is HASH_NB_ALWAYS, we always go to the next
537 * block so no check is necessary
538 */
539 while (num_frames--) {
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300540 if (!(bh = ext3_dir_bread(NULL, dir, dx_get_block(p->at),
541 0, &err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return err; /* Failure */
543 p++;
544 brelse (p->bh);
545 p->bh = bh;
546 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
547 }
548 return 1;
549}
550
551
552/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * This function fills a red-black tree with information from a
554 * directory block. It returns the number directory entries loaded
555 * into the tree. If there is an error it is returned in err.
556 */
557static int htree_dirblock_to_tree(struct file *dir_file,
558 struct inode *dir, int block,
559 struct dx_hash_info *hinfo,
560 __u32 start_hash, __u32 start_minor_hash)
561{
562 struct buffer_head *bh;
563 struct ext3_dir_entry_2 *de, *top;
Carlos Maiolinoaa966012012-10-01 16:50:54 -0300564 int err = 0, count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300567
568 if (!(bh = ext3_dir_bread(NULL, dir, block, 0, &err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return err;
570
571 de = (struct ext3_dir_entry_2 *) bh->b_data;
572 top = (struct ext3_dir_entry_2 *) ((char *) de +
573 dir->i_sb->s_blocksize -
574 EXT3_DIR_REC_LEN(0));
575 for (; de < top; de = ext3_next_entry(de)) {
Eric Sandeen40b85132006-12-06 20:36:26 -0800576 if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
577 (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb))
578 +((char *)de - bh->b_data))) {
579 /* On error, skip the f_pos to the next block. */
580 dir_file->f_pos = (dir_file->f_pos |
581 (dir->i_sb->s_blocksize - 1)) + 1;
582 brelse (bh);
583 return count;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ext3fs_dirhash(de->name, de->name_len, hinfo);
586 if ((hinfo->hash < start_hash) ||
587 ((hinfo->hash == start_hash) &&
588 (hinfo->minor_hash < start_minor_hash)))
589 continue;
590 if (de->inode == 0)
591 continue;
592 if ((err = ext3_htree_store_dirent(dir_file,
593 hinfo->hash, hinfo->minor_hash, de)) != 0) {
594 brelse(bh);
595 return err;
596 }
597 count++;
598 }
599 brelse(bh);
600 return count;
601}
602
603
604/*
605 * This function fills a red-black tree with information from a
606 * directory. We start scanning the directory in hash order, starting
607 * at start_hash and start_minor_hash.
608 *
609 * This function returns the number of entries inserted into the tree,
610 * or a negative error code.
611 */
612int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
613 __u32 start_minor_hash, __u32 *next_hash)
614{
615 struct dx_hash_info hinfo;
616 struct ext3_dir_entry_2 *de;
617 struct dx_frame frames[2], *frame;
618 struct inode *dir;
619 int block, err;
620 int count = 0;
621 int ret;
622 __u32 hashval;
623
624 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
625 start_minor_hash));
Josef "Jeff" Sipekfe21a692006-12-08 02:36:38 -0800626 dir = dir_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!(EXT3_I(dir)->i_flags & EXT3_INDEX_FL)) {
628 hinfo.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'o5e1f8c92008-10-28 13:21:55 -0400629 if (hinfo.hash_version <= DX_HASH_TEA)
630 hinfo.hash_version +=
631 EXT3_SB(dir->i_sb)->s_hash_unsigned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
633 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
634 start_hash, start_minor_hash);
635 *next_hash = ~0;
636 return count;
637 }
638 hinfo.hash = start_hash;
639 hinfo.minor_hash = 0;
Josef "Jeff" Sipekfe21a692006-12-08 02:36:38 -0800640 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!frame)
642 return err;
643
644 /* Add '.' and '..' from the htree header */
645 if (!start_hash && !start_minor_hash) {
646 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
647 if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
648 goto errout;
649 count++;
650 }
651 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
652 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
653 de = ext3_next_entry(de);
654 if ((err = ext3_htree_store_dirent(dir_file, 2, 0, de)) != 0)
655 goto errout;
656 count++;
657 }
658
659 while (1) {
660 block = dx_get_block(frame->at);
661 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
662 start_hash, start_minor_hash);
663 if (ret < 0) {
664 err = ret;
665 goto errout;
666 }
667 count += ret;
668 hashval = ~0;
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700669 ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 frame, frames, &hashval);
671 *next_hash = hashval;
672 if (ret < 0) {
673 err = ret;
674 goto errout;
675 }
676 /*
677 * Stop if: (a) there are no more entries, or
678 * (b) we have inserted at least one entry and the
679 * next hash value is not a continuation
680 */
681 if ((ret == 0) ||
682 (count && ((hashval & 1) == 0)))
683 break;
684 }
685 dx_release(frames);
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700686 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 count, *next_hash));
688 return count;
689errout:
690 dx_release(frames);
691 return (err);
692}
693
694
695/*
696 * Directory block splitting, compacting
697 */
698
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700699/*
700 * Create map of hash values, offsets, and sizes, stored at end of block.
701 * Returns number of entries mapped.
702 */
Wei Yongjun45f90212009-04-02 16:57:14 -0700703static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize,
704 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 int count = 0;
707 char *base = (char *) de;
708 struct dx_hash_info h = *hinfo;
709
Wei Yongjun45f90212009-04-02 16:57:14 -0700710 while ((char *) de < base + blocksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 {
712 if (de->name_len && de->inode) {
713 ext3fs_dirhash(de->name, de->name_len, &h);
714 map_tail--;
715 map_tail->hash = h.hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700716 map_tail->offs = (u16) ((char *) de - base);
717 map_tail->size = le16_to_cpu(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 count++;
719 cond_resched();
720 }
721 /* XXX: do we need to check rec_len == 0 case? -Chris */
Jan Kara7c06a8d2007-11-14 17:00:19 -0800722 de = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 return count;
725}
726
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700727/* Sort map by hash value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static void dx_sort_map (struct dx_map_entry *map, unsigned count)
729{
730 struct dx_map_entry *p, *q, *top = map + count - 1;
731 int more;
732 /* Combsort until bubble sort doesn't suck */
733 while (count > 2)
734 {
735 count = count*10/13;
736 if (count - 9 < 2) /* 9, 10 -> 11 */
737 count = 11;
738 for (p = top, q = p - count; q >= map; p--, q--)
739 if (p->hash < q->hash)
740 swap(*p, *q);
741 }
742 /* Garden variety bubble sort */
743 do {
744 more = 0;
745 q = top;
746 while (q-- > map)
747 {
748 if (q[1].hash >= q[0].hash)
749 continue;
750 swap(*(q+1), *q);
751 more = 1;
752 }
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static void ext3_update_dx_flag(struct inode *inode)
771{
772 if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
773 EXT3_FEATURE_COMPAT_DIR_INDEX))
774 EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
775}
776
777/*
778 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
779 *
780 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
781 * `de != NULL' is guaranteed by caller.
782 */
783static inline int ext3_match (int len, const char * const name,
784 struct ext3_dir_entry_2 * de)
785{
786 if (len != de->name_len)
787 return 0;
788 if (!de->inode)
789 return 0;
790 return !memcmp(name, de->name, len);
791}
792
793/*
794 * Returns 0 if not found, -1 on failure, and 1 on success
795 */
796static inline int search_dirblock(struct buffer_head * bh,
797 struct inode *dir,
Al Viro734711a2008-08-24 07:26:48 -0400798 struct qstr *child,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 unsigned long offset,
800 struct ext3_dir_entry_2 ** res_dir)
801{
802 struct ext3_dir_entry_2 * de;
803 char * dlimit;
804 int de_len;
Al Viro734711a2008-08-24 07:26:48 -0400805 const char *name = child->name;
806 int namelen = child->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 de = (struct ext3_dir_entry_2 *) bh->b_data;
809 dlimit = bh->b_data + dir->i_sb->s_blocksize;
810 while ((char *) de < dlimit) {
811 /* this code is executed quadratically often */
812 /* do minimal checking `by hand' */
813
814 if ((char *) de + namelen <= dlimit &&
815 ext3_match (namelen, name, de)) {
816 /* found a match - just to be sure, do a full check */
817 if (!ext3_check_dir_entry("ext3_find_entry",
818 dir, de, bh, offset))
819 return -1;
820 *res_dir = de;
821 return 1;
822 }
823 /* prevent looping on a bad block */
Jan Kara7c06a8d2007-11-14 17:00:19 -0800824 de_len = ext3_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (de_len <= 0)
826 return -1;
827 offset += de_len;
828 de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
829 }
830 return 0;
831}
832
833
834/*
835 * ext3_find_entry()
836 *
837 * finds an entry in the specified directory with the wanted name. It
838 * returns the cache buffer in which the entry was found, and the entry
839 * itself (as a parameter - res_dir). It does NOT read the inode of the
840 * entry - you'll have to do that yourself if you want to.
841 *
842 * The returned buffer_head has ->b_count elevated. The caller is expected
843 * to brelse() it when appropriate.
844 */
Al Viro734711a2008-08-24 07:26:48 -0400845static struct buffer_head *ext3_find_entry(struct inode *dir,
846 struct qstr *entry,
847 struct ext3_dir_entry_2 **res_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct super_block * sb;
850 struct buffer_head * bh_use[NAMEI_RA_SIZE];
851 struct buffer_head * bh, *ret = NULL;
852 unsigned long start, block, b;
Theodore Ts'of0cad892010-10-16 19:36:59 -0400853 const u8 *name = entry->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 *res_dir = NULL;
863 sb = dir->i_sb;
Al Viro734711a2008-08-24 07:26:48 -0400864 namelen = entry->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (namelen > EXT3_NAME_LEN)
866 return NULL;
Theodore Ts'of0cad892010-10-16 19:36:59 -0400867 if ((namelen <= 2) && (name[0] == '.') &&
868 (name[1] == '.' || name[1] == 0)) {
869 /*
870 * "." or ".." will only be in the first block
871 * NFS may look up ".."; "." should be handled by the VFS
872 */
873 block = start = 0;
874 nblocks = 1;
875 goto restart;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (is_dx(dir)) {
Al Viro734711a2008-08-24 07:26:48 -0400878 bh = ext3_dx_find_entry(dir, entry, res_dir, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * On success, or if the error was file not found,
881 * return. Otherwise, fall back to doing a search the
882 * old fashioned way.
883 */
884 if (bh || (err != ERR_BAD_DX_DIR))
885 return bh;
886 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
889 start = EXT3_I(dir)->i_dir_start_lookup;
890 if (start >= nblocks)
891 start = 0;
892 block = start;
893restart:
894 do {
895 /*
896 * We deal with the read-ahead logic here.
897 */
898 if (ra_ptr >= ra_max) {
899 /* Refill the readahead buffer */
900 ra_ptr = 0;
901 b = block;
902 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
903 /*
904 * Terminate if we reach the end of the
905 * directory and must wrap, or if our
906 * search has finished at this block.
907 */
908 if (b >= nblocks || (num && block == start)) {
909 bh_use[ra_max] = NULL;
910 break;
911 }
912 num++;
913 bh = ext3_getblk(NULL, dir, b++, 0, &err);
914 bh_use[ra_max] = bh;
Zheng Liud03e1292011-12-05 15:55:11 +0800915 if (bh && !bh_uptodate_or_lock(bh)) {
916 get_bh(bh);
917 bh->b_end_io = end_buffer_read_sync;
918 submit_bh(READ | REQ_META | REQ_PRIO,
919 bh);
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 }
923 if ((bh = bh_use[ra_ptr++]) == NULL)
924 goto next;
925 wait_on_buffer(bh);
926 if (!buffer_uptodate(bh)) {
927 /* read error, skip block & hope for the best */
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700928 ext3_error(sb, __func__, "reading directory #%lu "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 "offset %lu", dir->i_ino, block);
930 brelse(bh);
931 goto next;
932 }
Al Viro734711a2008-08-24 07:26:48 -0400933 i = search_dirblock(bh, dir, entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
935 if (i == 1) {
936 EXT3_I(dir)->i_dir_start_lookup = block;
937 ret = bh;
938 goto cleanup_and_exit;
939 } else {
940 brelse(bh);
941 if (i < 0)
942 goto cleanup_and_exit;
943 }
944 next:
945 if (++block >= nblocks)
946 block = 0;
947 } while (block != start);
948
949 /*
950 * If the directory has grown while we were searching, then
951 * search the last part of the directory before giving up.
952 */
953 block = nblocks;
954 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
955 if (block < nblocks) {
956 start = 0;
957 goto restart;
958 }
959
960cleanup_and_exit:
961 /* Clean up the read-ahead blocks */
962 for (; ra_ptr < ra_max; ra_ptr++)
963 brelse (bh_use[ra_ptr]);
964 return ret;
965}
966
Al Viro734711a2008-08-24 07:26:48 -0400967static struct buffer_head * ext3_dx_find_entry(struct inode *dir,
968 struct qstr *entry, struct ext3_dir_entry_2 **res_dir,
969 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Theodore Ts'of0cad892010-10-16 19:36:59 -0400971 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct dx_hash_info hinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct dx_frame frames[2], *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct buffer_head *bh;
975 unsigned long block;
976 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Theodore Ts'of0cad892010-10-16 19:36:59 -0400978 if (!(frame = dx_probe(entry, dir, &hinfo, frames, err)))
979 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 do {
981 block = dx_get_block(frame->at);
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -0300982 if (!(bh = ext3_dir_bread (NULL, dir, block, 0, err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto errout;
Duane Griffin275c0a82008-07-25 01:46:31 -0700984
Theodore Ts'o5026e902010-10-16 19:37:00 -0400985 retval = search_dirblock(bh, dir, entry,
986 block << EXT3_BLOCK_SIZE_BITS(sb),
987 res_dir);
988 if (retval == 1) {
989 dx_release(frames);
990 return bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Theodore Ts'o5026e902010-10-16 19:37:00 -0400992 brelse(bh);
993 if (retval == -1) {
994 *err = ERR_BAD_DX_DIR;
995 goto errout;
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Check to see if we should continue to search */
Theodore Ts'of0cad892010-10-16 19:36:59 -0400999 retval = ext3_htree_next_block(dir, hinfo.hash, frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 frames, NULL);
1001 if (retval < 0) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001002 ext3_warning(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 "error reading index page in directory #%lu",
1004 dir->i_ino);
1005 *err = retval;
1006 goto errout;
1007 }
1008 } while (retval == 1);
1009
1010 *err = -ENOENT;
1011errout:
Bernd Schubertc878c732011-07-20 23:16:33 +02001012 dxtrace(printk("%s not found\n", entry->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 dx_release (frames);
1014 return NULL;
1015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Al Viro00cd8dd2012-06-10 17:13:09 -04001017static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct inode * inode;
1020 struct ext3_dir_entry_2 * de;
1021 struct buffer_head * bh;
1022
1023 if (dentry->d_name.len > EXT3_NAME_LEN)
1024 return ERR_PTR(-ENAMETOOLONG);
1025
Al Viro734711a2008-08-24 07:26:48 -04001026 bh = ext3_find_entry(dir, &dentry->d_name, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 inode = NULL;
1028 if (bh) {
1029 unsigned long ino = le32_to_cpu(de->inode);
1030 brelse (bh);
Neil Brown2ccb48e2006-07-30 03:03:01 -07001031 if (!ext3_valid_inum(dir->i_sb, ino)) {
1032 ext3_error(dir->i_sb, "ext3_lookup",
1033 "bad inode number: %lu", ino);
David Howells473043d2008-02-07 00:15:36 -08001034 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001035 }
David Howells473043d2008-02-07 00:15:36 -08001036 inode = ext3_iget(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001037 if (inode == ERR_PTR(-ESTALE)) {
1038 ext3_error(dir->i_sb, __func__,
1039 "deleted inode referenced: %lu",
1040 ino);
1041 return ERR_PTR(-EIO);
Bryan Donlande18f3b2009-04-02 16:57:15 -07001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Pekka Enbergba7fe362006-01-14 13:21:08 -08001044 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047
1048struct dentry *ext3_get_parent(struct dentry *child)
1049{
1050 unsigned long ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001051 struct qstr dotdot = QSTR_INIT("..", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct ext3_dir_entry_2 * de;
1053 struct buffer_head *bh;
1054
Al Viro734711a2008-08-24 07:26:48 -04001055 bh = ext3_find_entry(child->d_inode, &dotdot, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!bh)
1057 return ERR_PTR(-ENOENT);
1058 ino = le32_to_cpu(de->inode);
1059 brelse(bh);
Neil Brown2ccb48e2006-07-30 03:03:01 -07001060
1061 if (!ext3_valid_inum(child->d_inode->i_sb, ino)) {
1062 ext3_error(child->d_inode->i_sb, "ext3_get_parent",
1063 "bad inode number: %lu", ino);
David Howells473043d2008-02-07 00:15:36 -08001064 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001065 }
1066
Christoph Hellwig44003722008-08-11 15:49:04 +02001067 return d_obtain_alias(ext3_iget(child->d_inode->i_sb, ino));
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070#define S_SHIFT 12
1071static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
1072 [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE,
1073 [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR,
1074 [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV,
1075 [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV,
1076 [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO,
1077 [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK,
1078 [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK,
1079};
1080
1081static inline void ext3_set_de_type(struct super_block *sb,
1082 struct ext3_dir_entry_2 *de,
1083 umode_t mode) {
1084 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
1085 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1086}
1087
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001088/*
1089 * Move count entries from end of map between two memory locations.
1090 * Returns pointer to last entry moved.
1091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static struct ext3_dir_entry_2 *
1093dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1094{
1095 unsigned rec_len = 0;
1096
1097 while (count--) {
1098 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1099 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1100 memcpy (to, de, rec_len);
1101 ((struct ext3_dir_entry_2 *) to)->rec_len =
Jan Kara7c06a8d2007-11-14 17:00:19 -08001102 ext3_rec_len_to_disk(rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 de->inode = 0;
1104 map++;
1105 to += rec_len;
1106 }
1107 return (struct ext3_dir_entry_2 *) (to - rec_len);
1108}
1109
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001110/*
1111 * Compact each dir entry in the range to the minimal rec_len.
1112 * Returns pointer to last entry in range.
1113 */
Wei Yongjun45f90212009-04-02 16:57:14 -07001114static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Wei Yongjun45f90212009-04-02 16:57:14 -07001116 struct ext3_dir_entry_2 *next, *to, *prev;
1117 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *)base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned rec_len = 0;
1119
1120 prev = to = de;
Wei Yongjun45f90212009-04-02 16:57:14 -07001121 while ((char *)de < base + blocksize) {
Jan Kara7c06a8d2007-11-14 17:00:19 -08001122 next = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (de->inode && de->name_len) {
1124 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1125 if (de > to)
1126 memmove(to, de, rec_len);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001127 to->rec_len = ext3_rec_len_to_disk(rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 prev = to;
1129 to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
1130 }
1131 de = next;
1132 }
1133 return prev;
1134}
1135
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001136/*
1137 * Split a full leaf block to make room for a new dir entry.
1138 * Allocate a new block, and move entries so that they are approx. equally full.
1139 * Returns pointer to de in block into which the new entry will be inserted.
1140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1142 struct buffer_head **bh,struct dx_frame *frame,
1143 struct dx_hash_info *hinfo, int *error)
1144{
1145 unsigned blocksize = dir->i_sb->s_blocksize;
1146 unsigned count, continued;
1147 struct buffer_head *bh2;
1148 u32 newblock;
1149 u32 hash2;
1150 struct dx_map_entry *map;
1151 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001152 unsigned split, move, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct ext3_dir_entry_2 *de = NULL, *de2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001154 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001156 bh2 = ext3_append (handle, dir, &newblock, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!(bh2)) {
1158 brelse(*bh);
1159 *bh = NULL;
1160 goto errout;
1161 }
1162
1163 BUFFER_TRACE(*bh, "get_write_access");
1164 err = ext3_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001165 if (err)
1166 goto journal_error;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 BUFFER_TRACE(frame->bh, "get_write_access");
1169 err = ext3_journal_get_write_access(handle, frame->bh);
1170 if (err)
1171 goto journal_error;
1172
1173 data2 = bh2->b_data;
1174
1175 /* create map in the end of data2 block */
1176 map = (struct dx_map_entry *) (data2 + blocksize);
1177 count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1178 blocksize, hinfo, map);
1179 map -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 dx_sort_map (map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001181 /* Split the existing block in the middle, size-wise */
1182 size = 0;
1183 move = 0;
1184 for (i = count-1; i >= 0; i--) {
1185 /* is more than half of this entry in 2nd half of the block? */
1186 if (size + map[i].size/2 > blocksize/2)
1187 break;
1188 size += map[i].size;
1189 move++;
1190 }
1191 /* map index at which we will split */
1192 split = count - move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 hash2 = map[split].hash;
1194 continued = hash2 == map[split - 1].hash;
1195 dxtrace(printk("Split block %i at %x, %i/%i\n",
1196 dx_get_block(frame->at), hash2, split, count-split));
1197
1198 /* Fancy dance to stay within two buffers */
1199 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1200 de = dx_pack_dirents(data1,blocksize);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001201 de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de);
1202 de2->rec_len = ext3_rec_len_to_disk(data2 + blocksize - (char *) de2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1204 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1205
1206 /* Which block gets the new entry? */
1207 if (hinfo->hash >= hash2)
1208 {
1209 swap(*bh, bh2);
1210 de = de2;
1211 }
1212 dx_insert_block (frame, hash2 + continued, newblock);
1213 err = ext3_journal_dirty_metadata (handle, bh2);
1214 if (err)
1215 goto journal_error;
1216 err = ext3_journal_dirty_metadata (handle, frame->bh);
1217 if (err)
1218 goto journal_error;
1219 brelse (bh2);
1220 dxtrace(dx_show_index ("frame", frame->entries));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001222
1223journal_error:
1224 brelse(*bh);
1225 brelse(bh2);
1226 *bh = NULL;
1227 ext3_std_error(dir->i_sb, err);
1228errout:
1229 *error = err;
1230 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233
1234/*
1235 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1236 * it points to a directory entry which is guaranteed to be large
1237 * enough for new directory entry. If de is NULL, then
1238 * add_dirent_to_buf will attempt search the directory block for
1239 * space. It will return -ENOSPC if no space is available, and -EIO
1240 * and -EEXIST if directory entry already exists.
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1243 * all other cases bh is released.
1244 */
1245static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1246 struct inode *inode, struct ext3_dir_entry_2 *de,
1247 struct buffer_head * bh)
1248{
1249 struct inode *dir = dentry->d_parent->d_inode;
1250 const char *name = dentry->d_name.name;
1251 int namelen = dentry->d_name.len;
1252 unsigned long offset = 0;
1253 unsigned short reclen;
1254 int nlen, rlen, err;
1255 char *top;
1256
1257 reclen = EXT3_DIR_REC_LEN(namelen);
1258 if (!de) {
1259 de = (struct ext3_dir_entry_2 *)bh->b_data;
1260 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1261 while ((char *) de <= top) {
1262 if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1263 bh, offset)) {
1264 brelse (bh);
1265 return -EIO;
1266 }
1267 if (ext3_match (namelen, name, de)) {
1268 brelse (bh);
1269 return -EEXIST;
1270 }
1271 nlen = EXT3_DIR_REC_LEN(de->name_len);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001272 rlen = ext3_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if ((de->inode? rlen - nlen: rlen) >= reclen)
1274 break;
1275 de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1276 offset += rlen;
1277 }
1278 if ((char *) de > top)
1279 return -ENOSPC;
1280 }
1281 BUFFER_TRACE(bh, "get_write_access");
1282 err = ext3_journal_get_write_access(handle, bh);
1283 if (err) {
1284 ext3_std_error(dir->i_sb, err);
1285 brelse(bh);
1286 return err;
1287 }
1288
1289 /* By now the buffer is marked for journaling */
1290 nlen = EXT3_DIR_REC_LEN(de->name_len);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001291 rlen = ext3_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (de->inode) {
1293 struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001294 de1->rec_len = ext3_rec_len_to_disk(rlen - nlen);
1295 de->rec_len = ext3_rec_len_to_disk(nlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 de = de1;
1297 }
1298 de->file_type = EXT3_FT_UNKNOWN;
1299 if (inode) {
1300 de->inode = cpu_to_le32(inode->i_ino);
1301 ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1302 } else
1303 de->inode = 0;
1304 de->name_len = namelen;
1305 memcpy (de->name, name, namelen);
1306 /*
1307 * XXX shouldn't update any times until successful
1308 * completion of syscall, but too many callers depend
1309 * on this.
1310 *
1311 * XXX similarly, too many callers depend on
1312 * ext3_new_inode() setting the times, but error
1313 * recovery deletes the inode, so the worst that can
1314 * happen is that the times are slightly out of date
1315 * and/or different from the directory change time.
1316 */
1317 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
1318 ext3_update_dx_flag(dir);
1319 dir->i_version++;
1320 ext3_mark_inode_dirty(handle, dir);
1321 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1322 err = ext3_journal_dirty_metadata(handle, bh);
1323 if (err)
1324 ext3_std_error(dir->i_sb, err);
1325 brelse(bh);
1326 return 0;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/*
1330 * This converts a one block unindexed directory to a 3 block indexed
1331 * directory, and adds the dentry to the indexed directory.
1332 */
1333static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1334 struct inode *inode, struct buffer_head *bh)
1335{
1336 struct inode *dir = dentry->d_parent->d_inode;
1337 const char *name = dentry->d_name.name;
1338 int namelen = dentry->d_name.len;
1339 struct buffer_head *bh2;
1340 struct dx_root *root;
1341 struct dx_frame frames[2], *frame;
1342 struct dx_entry *entries;
1343 struct ext3_dir_entry_2 *de, *de2;
1344 char *data1, *top;
1345 unsigned len;
1346 int retval;
1347 unsigned blocksize;
1348 struct dx_hash_info hinfo;
1349 u32 block;
1350 struct fake_dirent *fde;
1351
1352 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oa21102b2009-01-16 11:13:47 -05001353 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 retval = ext3_journal_get_write_access(handle, bh);
1355 if (retval) {
1356 ext3_std_error(dir->i_sb, retval);
1357 brelse(bh);
1358 return retval;
1359 }
1360 root = (struct dx_root *) bh->b_data;
1361
Theodore Ts'oa21102b2009-01-16 11:13:47 -05001362 /* The 0th block becomes the root, move the dirents out */
1363 fde = &root->dotdot;
1364 de = (struct ext3_dir_entry_2 *)((char *)fde +
1365 ext3_rec_len_from_disk(fde->rec_len));
1366 if ((char *) de >= (((char *) root) + blocksize)) {
1367 ext3_error(dir->i_sb, __func__,
1368 "invalid rec_len for '..' in inode %lu",
1369 dir->i_ino);
1370 brelse(bh);
1371 return -EIO;
1372 }
1373 len = ((char *) root) + blocksize - (char *) de;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 bh2 = ext3_append (handle, dir, &block, &retval);
1376 if (!(bh2)) {
1377 brelse(bh);
1378 return retval;
1379 }
1380 EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1381 data1 = bh2->b_data;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 memcpy (data1, de, len);
1384 de = (struct ext3_dir_entry_2 *) data1;
1385 top = data1 + len;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001386 while ((char *)(de2 = ext3_next_entry(de)) < top)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 de = de2;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001388 de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* Initialize the root; the dot dirents already exist */
1390 de = (struct ext3_dir_entry_2 *) (&root->dotdot);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001391 de->rec_len = ext3_rec_len_to_disk(blocksize - EXT3_DIR_REC_LEN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 memset (&root->info, 0, sizeof(root->info));
1393 root->info.info_length = sizeof(root->info);
1394 root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
1395 entries = root->entries;
1396 dx_set_block (entries, 1);
1397 dx_set_count (entries, 1);
1398 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1399
1400 /* Initialize as for dx_probe */
1401 hinfo.hash_version = root->info.hash_version;
Theodore Ts'o5e1f8c92008-10-28 13:21:55 -04001402 if (hinfo.hash_version <= DX_HASH_TEA)
1403 hinfo.hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
1405 ext3fs_dirhash(name, namelen, &hinfo);
1406 frame = frames;
1407 frame->entries = entries;
1408 frame->at = entries;
1409 frame->bh = bh;
1410 bh = bh2;
Jan Kara86c4f6d82011-04-27 18:20:44 +02001411 /*
1412 * Mark buffers dirty here so that if do_split() fails we write a
1413 * consistent set of buffers to disk.
1414 */
1415 ext3_journal_dirty_metadata(handle, frame->bh);
1416 ext3_journal_dirty_metadata(handle, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
Jan Kara86c4f6d82011-04-27 18:20:44 +02001418 if (!de) {
1419 ext3_mark_inode_dirty(handle, dir);
1420 dx_release(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return retval;
Jan Kara86c4f6d82011-04-27 18:20:44 +02001422 }
1423 dx_release(frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428/*
1429 * ext3_add_entry()
1430 *
1431 * adds a file entry to the specified directory, using the same
1432 * semantics as ext3_find_entry(). It returns NULL if it failed.
1433 *
1434 * NOTE!! The inode part of 'de' is left at 0 - which means you
1435 * may not sleep between calling this and putting something into
1436 * the entry, as someone else might have used it while you slept.
1437 */
1438static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1439 struct inode *inode)
1440{
1441 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct buffer_head * bh;
1443 struct ext3_dir_entry_2 *de;
1444 struct super_block * sb;
1445 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int dx_fallback=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 unsigned blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 u32 block, blocks;
1449
1450 sb = dir->i_sb;
1451 blocksize = sb->s_blocksize;
1452 if (!dentry->d_name.len)
1453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (is_dx(dir)) {
1455 retval = ext3_dx_add_entry(handle, dentry, inode);
1456 if (!retval || (retval != ERR_BAD_DX_DIR))
1457 return retval;
1458 EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1459 dx_fallback++;
1460 ext3_mark_inode_dirty(handle, dir);
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 blocks = dir->i_size >> sb->s_blocksize_bits;
Andi Kleen0411ba72010-06-10 13:10:53 +02001463 for (block = 0; block < blocks; block++) {
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001464 if (!(bh = ext3_dir_bread(handle, dir, block, 0, &retval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return retval;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1468 if (retval != -ENOSPC)
1469 return retval;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (blocks == 1 && !dx_fallback &&
1472 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1473 return make_indexed_dir(handle, dentry, inode, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 brelse(bh);
1475 }
1476 bh = ext3_append(handle, dir, &block, &retval);
1477 if (!bh)
1478 return retval;
1479 de = (struct ext3_dir_entry_2 *) bh->b_data;
1480 de->inode = 0;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001481 de->rec_len = ext3_rec_len_to_disk(blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
1486 * Returns 0 for success, or a negative error value
1487 */
1488static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1489 struct inode *inode)
1490{
1491 struct dx_frame frames[2], *frame;
1492 struct dx_entry *entries, *at;
1493 struct dx_hash_info hinfo;
1494 struct buffer_head * bh;
1495 struct inode *dir = dentry->d_parent->d_inode;
1496 struct super_block * sb = dir->i_sb;
1497 struct ext3_dir_entry_2 *de;
1498 int err;
1499
Al Viro734711a2008-08-24 07:26:48 -04001500 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (!frame)
1502 return err;
1503 entries = frame->entries;
1504 at = frame->at;
1505
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001506 if (!(bh = ext3_dir_bread(handle, dir, dx_get_block(frame->at), 0, &err)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto cleanup;
1508
1509 BUFFER_TRACE(bh, "get_write_access");
1510 err = ext3_journal_get_write_access(handle, bh);
1511 if (err)
1512 goto journal_error;
1513
1514 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1515 if (err != -ENOSPC) {
1516 bh = NULL;
1517 goto cleanup;
1518 }
1519
1520 /* Block full, should compress but for now just split */
1521 dxtrace(printk("using %u of %u node entries\n",
1522 dx_get_count(entries), dx_get_limit(entries)));
1523 /* Need to split index? */
1524 if (dx_get_count(entries) == dx_get_limit(entries)) {
1525 u32 newblock;
1526 unsigned icount = dx_get_count(entries);
1527 int levels = frame - frames;
1528 struct dx_entry *entries2;
1529 struct dx_node *node2;
1530 struct buffer_head *bh2;
1531
1532 if (levels && (dx_get_count(frames->entries) ==
1533 dx_get_limit(frames->entries))) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001534 ext3_warning(sb, __func__,
Glauber de Oliveira Costa9f406682006-01-08 01:03:22 -08001535 "Directory index full!");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 err = -ENOSPC;
1537 goto cleanup;
1538 }
1539 bh2 = ext3_append (handle, dir, &newblock, &err);
1540 if (!(bh2))
1541 goto cleanup;
1542 node2 = (struct dx_node *)(bh2->b_data);
1543 entries2 = node2->entries;
Eric Sandeend7433142011-03-04 16:04:08 -06001544 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Jan Kara7c06a8d2007-11-14 17:00:19 -08001545 node2->fake.rec_len = ext3_rec_len_to_disk(sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 BUFFER_TRACE(frame->bh, "get_write_access");
1547 err = ext3_journal_get_write_access(handle, frame->bh);
1548 if (err)
1549 goto journal_error;
1550 if (levels) {
1551 unsigned icount1 = icount/2, icount2 = icount - icount1;
1552 unsigned hash2 = dx_get_hash(entries + icount1);
1553 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1554
1555 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1556 err = ext3_journal_get_write_access(handle,
1557 frames[0].bh);
1558 if (err)
1559 goto journal_error;
1560
1561 memcpy ((char *) entries2, (char *) (entries + icount1),
1562 icount2 * sizeof(struct dx_entry));
1563 dx_set_count (entries, icount1);
1564 dx_set_count (entries2, icount2);
1565 dx_set_limit (entries2, dx_node_limit(dir));
1566
1567 /* Which index block gets the new entry? */
1568 if (at - entries >= icount1) {
1569 frame->at = at = at - entries - icount1 + entries2;
1570 frame->entries = entries = entries2;
1571 swap(frame->bh, bh2);
1572 }
1573 dx_insert_block (frames + 0, hash2, newblock);
1574 dxtrace(dx_show_index ("node", frames[1].entries));
1575 dxtrace(dx_show_index ("node",
1576 ((struct dx_node *) bh2->b_data)->entries));
1577 err = ext3_journal_dirty_metadata(handle, bh2);
1578 if (err)
1579 goto journal_error;
1580 brelse (bh2);
1581 } else {
1582 dxtrace(printk("Creating second level index...\n"));
1583 memcpy((char *) entries2, (char *) entries,
1584 icount * sizeof(struct dx_entry));
1585 dx_set_limit(entries2, dx_node_limit(dir));
1586
1587 /* Set up root */
1588 dx_set_count(entries, 1);
1589 dx_set_block(entries + 0, newblock);
1590 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1591
1592 /* Add new access path frame */
1593 frame = frames + 1;
1594 frame->at = at = at - entries + entries2;
1595 frame->entries = entries = entries2;
1596 frame->bh = bh2;
1597 err = ext3_journal_get_write_access(handle,
1598 frame->bh);
1599 if (err)
1600 goto journal_error;
1601 }
Namhyung Kim156e7432010-11-25 01:53:13 +09001602 err = ext3_journal_dirty_metadata(handle, frames[0].bh);
1603 if (err)
1604 goto journal_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1607 if (!de)
1608 goto cleanup;
1609 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1610 bh = NULL;
1611 goto cleanup;
1612
1613journal_error:
1614 ext3_std_error(dir->i_sb, err);
1615cleanup:
1616 if (bh)
1617 brelse(bh);
1618 dx_release(frames);
1619 return err;
1620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622/*
1623 * ext3_delete_entry deletes a directory entry by merging it with the
1624 * previous entry
1625 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001626static int ext3_delete_entry (handle_t *handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct inode * dir,
1628 struct ext3_dir_entry_2 * de_del,
1629 struct buffer_head * bh)
1630{
1631 struct ext3_dir_entry_2 * de, * pde;
1632 int i;
1633
1634 i = 0;
1635 pde = NULL;
1636 de = (struct ext3_dir_entry_2 *) bh->b_data;
1637 while (i < bh->b_size) {
1638 if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
1639 return -EIO;
1640 if (de == de_del) {
Namhyung Kimfbcae8e2010-11-19 16:28:35 +09001641 int err;
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 BUFFER_TRACE(bh, "get_write_access");
Namhyung Kimfbcae8e2010-11-19 16:28:35 +09001644 err = ext3_journal_get_write_access(handle, bh);
1645 if (err)
1646 goto journal_error;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (pde)
Jan Kara7c06a8d2007-11-14 17:00:19 -08001649 pde->rec_len = ext3_rec_len_to_disk(
1650 ext3_rec_len_from_disk(pde->rec_len) +
1651 ext3_rec_len_from_disk(de->rec_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 else
1653 de->inode = 0;
1654 dir->i_version++;
1655 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
Namhyung Kimfbcae8e2010-11-19 16:28:35 +09001656 err = ext3_journal_dirty_metadata(handle, bh);
1657 if (err) {
1658journal_error:
1659 ext3_std_error(dir->i_sb, err);
1660 return err;
1661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
1663 }
Jan Kara7c06a8d2007-11-14 17:00:19 -08001664 i += ext3_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 pde = de;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001666 de = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 return -ENOENT;
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static int ext3_add_nondir(handle_t *handle,
1672 struct dentry *dentry, struct inode *inode)
1673{
1674 int err = ext3_add_entry(handle, dentry, inode);
1675 if (!err) {
1676 ext3_mark_inode_dirty(handle, inode);
Al Viroc38012d2008-12-30 02:02:50 -05001677 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04001678 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return 0;
1680 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08001681 drop_nlink(inode);
Al Viroc38012d2008-12-30 02:02:50 -05001682 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 iput(inode);
1684 return err;
1685}
1686
1687/*
1688 * By the time this is called, we already have created
1689 * the directory cache entry for the new file, but it
1690 * is so far negative - it has no inode.
1691 *
1692 * If the create succeeds, we fill in the inode information
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001693 * with d_instantiate().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 */
Al Viro4acdaf22011-07-26 01:42:34 -04001695static int ext3_create (struct inode * dir, struct dentry * dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04001696 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001698 handle_t *handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct inode * inode;
1700 int err, retries = 0;
1701
Christoph Hellwig871a2932010-03-03 09:05:07 -05001702 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704retry:
Jan Kara1f545872005-06-23 22:01:04 -07001705 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhovc4590012009-12-09 03:05:30 +03001707 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (IS_ERR(handle))
1709 return PTR_ERR(handle);
1710
1711 if (IS_DIRSYNC(dir))
1712 handle->h_sync = 1;
1713
Eric Paris2a7dba32011-02-01 11:05:39 -05001714 inode = ext3_new_inode (handle, dir, &dentry->d_name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 err = PTR_ERR(inode);
1716 if (!IS_ERR(inode)) {
1717 inode->i_op = &ext3_file_inode_operations;
1718 inode->i_fop = &ext3_file_operations;
1719 ext3_set_aops(inode);
1720 err = ext3_add_nondir(handle, dentry, inode);
1721 }
1722 ext3_journal_stop(handle);
1723 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1724 goto retry;
1725 return err;
1726}
1727
1728static int ext3_mknod (struct inode * dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001729 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 handle_t *handle;
1732 struct inode *inode;
1733 int err, retries = 0;
1734
1735 if (!new_valid_dev(rdev))
1736 return -EINVAL;
1737
Christoph Hellwig871a2932010-03-03 09:05:07 -05001738 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740retry:
Jan Kara1f545872005-06-23 22:01:04 -07001741 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
Dave Kleikampe9ad5622006-09-27 01:49:35 -07001742 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhovc4590012009-12-09 03:05:30 +03001743 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (IS_ERR(handle))
1745 return PTR_ERR(handle);
1746
1747 if (IS_DIRSYNC(dir))
1748 handle->h_sync = 1;
1749
Eric Paris2a7dba32011-02-01 11:05:39 -05001750 inode = ext3_new_inode (handle, dir, &dentry->d_name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 err = PTR_ERR(inode);
1752 if (!IS_ERR(inode)) {
1753 init_special_inode(inode, inode->i_mode, rdev);
1754#ifdef CONFIG_EXT3_FS_XATTR
1755 inode->i_op = &ext3_special_inode_operations;
1756#endif
1757 err = ext3_add_nondir(handle, dentry, inode);
1758 }
1759 ext3_journal_stop(handle);
1760 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1761 goto retry;
1762 return err;
1763}
1764
Al Viro18bb1db2011-07-26 01:41:39 -04001765static int ext3_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 handle_t *handle;
1768 struct inode * inode;
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001769 struct buffer_head * dir_block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct ext3_dir_entry_2 * de;
1771 int err, retries = 0;
1772
1773 if (dir->i_nlink >= EXT3_LINK_MAX)
1774 return -EMLINK;
1775
Christoph Hellwig871a2932010-03-03 09:05:07 -05001776 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778retry:
Jan Kara1f545872005-06-23 22:01:04 -07001779 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhovc4590012009-12-09 03:05:30 +03001781 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (IS_ERR(handle))
1783 return PTR_ERR(handle);
1784
1785 if (IS_DIRSYNC(dir))
1786 handle->h_sync = 1;
1787
Eric Paris2a7dba32011-02-01 11:05:39 -05001788 inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFDIR | mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 err = PTR_ERR(inode);
1790 if (IS_ERR(inode))
1791 goto out_stop;
1792
1793 inode->i_op = &ext3_dir_inode_operations;
1794 inode->i_fop = &ext3_dir_operations;
1795 inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001796 if (!(dir_block = ext3_dir_bread(handle, inode, 0, 1, &err)))
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001797 goto out_clear_inode;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 BUFFER_TRACE(dir_block, "get_write_access");
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001800 err = ext3_journal_get_write_access(handle, dir_block);
1801 if (err)
1802 goto out_clear_inode;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
1805 de->inode = cpu_to_le32(inode->i_ino);
1806 de->name_len = 1;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001807 de->rec_len = ext3_rec_len_to_disk(EXT3_DIR_REC_LEN(de->name_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 strcpy (de->name, ".");
1809 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001810 de = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 de->inode = cpu_to_le32(dir->i_ino);
Jan Kara7c06a8d2007-11-14 17:00:19 -08001812 de->rec_len = ext3_rec_len_to_disk(inode->i_sb->s_blocksize -
1813 EXT3_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 de->name_len = 2;
1815 strcpy (de->name, "..");
1816 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001817 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001819 err = ext3_journal_dirty_metadata(handle, dir_block);
1820 if (err)
1821 goto out_clear_inode;
1822
1823 err = ext3_mark_inode_dirty(handle, inode);
1824 if (!err)
1825 err = ext3_add_entry (handle, dentry, inode);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (err) {
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001828out_clear_inode:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001829 clear_nlink(inode);
Al Viroc38012d2008-12-30 02:02:50 -05001830 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 ext3_mark_inode_dirty(handle, inode);
1832 iput (inode);
1833 goto out_stop;
1834 }
Dave Hansend8c76e62006-09-30 23:29:04 -07001835 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 ext3_update_dx_flag(dir);
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001837 err = ext3_mark_inode_dirty(handle, dir);
1838 if (err)
1839 goto out_clear_inode;
1840
Al Viroc38012d2008-12-30 02:02:50 -05001841 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04001842 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843out_stop:
Namhyung Kim2b543ed2010-11-16 20:18:12 +09001844 brelse(dir_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ext3_journal_stop(handle);
1846 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
1847 goto retry;
1848 return err;
1849}
1850
1851/*
1852 * routine to check that the specified directory is empty (for rmdir)
1853 */
1854static int empty_dir (struct inode * inode)
1855{
1856 unsigned long offset;
1857 struct buffer_head * bh;
1858 struct ext3_dir_entry_2 * de, * de1;
1859 struct super_block * sb;
1860 int err = 0;
1861
1862 sb = inode->i_sb;
1863 if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001864 !(bh = ext3_dir_bread(NULL, inode, 0, 0, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (err)
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001866 ext3_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 "error %d reading directory #%lu offset 0",
1868 err, inode->i_ino);
1869 else
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001870 ext3_warning(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 "bad directory (dir #%lu) - no data block",
1872 inode->i_ino);
1873 return 1;
1874 }
1875 de = (struct ext3_dir_entry_2 *) bh->b_data;
Jan Kara7c06a8d2007-11-14 17:00:19 -08001876 de1 = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (le32_to_cpu(de->inode) != inode->i_ino ||
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001878 !le32_to_cpu(de1->inode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 strcmp (".", de->name) ||
1880 strcmp ("..", de1->name)) {
Dave Kleikampe9ad5622006-09-27 01:49:35 -07001881 ext3_warning (inode->i_sb, "empty_dir",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 "bad directory (dir #%lu) - no `.' or `..'",
1883 inode->i_ino);
1884 brelse (bh);
1885 return 1;
1886 }
Jan Kara7c06a8d2007-11-14 17:00:19 -08001887 offset = ext3_rec_len_from_disk(de->rec_len) +
1888 ext3_rec_len_from_disk(de1->rec_len);
1889 de = ext3_next_entry(de1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 while (offset < inode->i_size ) {
1891 if (!bh ||
1892 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1893 err = 0;
1894 brelse (bh);
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03001895 if (!(bh = ext3_dir_bread (NULL, inode,
1896 offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (err)
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001898 ext3_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 "error %d reading directory"
1900 " #%lu offset %lu",
1901 err, inode->i_ino, offset);
1902 offset += sb->s_blocksize;
1903 continue;
1904 }
1905 de = (struct ext3_dir_entry_2 *) bh->b_data;
1906 }
1907 if (!ext3_check_dir_entry("empty_dir", inode, de, bh, offset)) {
1908 de = (struct ext3_dir_entry_2 *)(bh->b_data +
1909 sb->s_blocksize);
1910 offset = (offset | (sb->s_blocksize - 1)) + 1;
1911 continue;
1912 }
1913 if (le32_to_cpu(de->inode)) {
1914 brelse (bh);
1915 return 0;
1916 }
Jan Kara7c06a8d2007-11-14 17:00:19 -08001917 offset += ext3_rec_len_from_disk(de->rec_len);
1918 de = ext3_next_entry(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 brelse (bh);
1921 return 1;
1922}
1923
1924/* ext3_orphan_add() links an unlinked or truncated inode into a list of
1925 * such inodes, starting at the superblock, in case we crash before the
1926 * file is closed/deleted, or in case the inode truncate spans multiple
1927 * transactions and the last transaction is not recovered after a crash.
1928 *
1929 * At filesystem recovery time, we walk this list deleting unlinked
1930 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1931 */
1932int ext3_orphan_add(handle_t *handle, struct inode *inode)
1933{
1934 struct super_block *sb = inode->i_sb;
1935 struct ext3_iloc iloc;
1936 int err = 0, rc;
1937
Eric Sandeenb8a052d2009-12-14 13:00:30 -06001938 mutex_lock(&EXT3_SB(sb)->s_orphan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!list_empty(&EXT3_I(inode)->i_orphan))
1940 goto out_unlock;
1941
1942 /* Orphan handling is only valid for files with data blocks
1943 * being truncated, or files being unlinked. */
1944
1945 /* @@@ FIXME: Observation from aviro:
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001946 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
Eric Sandeenb8a052d2009-12-14 13:00:30 -06001947 * here (on s_orphan_lock), so race with ext3_link() which might bump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * ->i_nlink. For, say it, character device. Not a regular file,
1949 * not a directory, not a symlink and ->i_nlink > 0.
Eric Sandeenb8a052d2009-12-14 13:00:30 -06001950 *
1951 * tytso, 4/25/2009: I'm not sure how that could happen;
1952 * shouldn't the fs core protect us from these sort of
1953 * unlink()/link() races?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
1955 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1956 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1957
1958 BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "get_write_access");
1959 err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
1960 if (err)
1961 goto out_unlock;
1962
1963 err = ext3_reserve_inode_write(handle, inode, &iloc);
1964 if (err)
1965 goto out_unlock;
1966
1967 /* Insert this inode at the head of the on-disk orphan list... */
1968 NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
1969 EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1970 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
1971 rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
1972 if (!err)
1973 err = rc;
1974
1975 /* Only add to the head of the in-memory list if all the
1976 * previous operations succeeded. If the orphan_add is going to
1977 * fail (possibly taking the journal offline), we can't risk
1978 * leaving the inode on the orphan list: stray orphan-list
1979 * entries can cause panics at unmount time.
1980 *
1981 * This is safe: on error we're going to ignore the orphan list
1982 * anyway on the next recovery. */
1983 if (!err)
1984 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
1985
Eric Sandeeneee194e2006-09-27 01:49:30 -07001986 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
1987 jbd_debug(4, "orphan inode %lu will point to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 inode->i_ino, NEXT_ORPHAN(inode));
1989out_unlock:
Eric Sandeenb8a052d2009-12-14 13:00:30 -06001990 mutex_unlock(&EXT3_SB(sb)->s_orphan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 ext3_std_error(inode->i_sb, err);
1992 return err;
1993}
1994
1995/*
1996 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1997 * of such inodes stored on disk, because it is finally being cleaned up.
1998 */
1999int ext3_orphan_del(handle_t *handle, struct inode *inode)
2000{
2001 struct list_head *prev;
2002 struct ext3_inode_info *ei = EXT3_I(inode);
2003 struct ext3_sb_info *sbi;
2004 unsigned long ino_next;
2005 struct ext3_iloc iloc;
2006 int err = 0;
2007
Eric Sandeenb8a052d2009-12-14 13:00:30 -06002008 mutex_lock(&EXT3_SB(inode->i_sb)->s_orphan_lock);
2009 if (list_empty(&ei->i_orphan))
2010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 ino_next = NEXT_ORPHAN(inode);
2013 prev = ei->i_orphan.prev;
2014 sbi = EXT3_SB(inode->i_sb);
2015
2016 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2017
2018 list_del_init(&ei->i_orphan);
2019
2020 /* If we're on an error path, we may not have a valid
2021 * transaction handle with which to update the orphan list on
2022 * disk, but we still need to remove the inode from the linked
2023 * list in memory. */
2024 if (!handle)
2025 goto out;
2026
2027 err = ext3_reserve_inode_write(handle, inode, &iloc);
2028 if (err)
2029 goto out_err;
2030
2031 if (prev == &sbi->s_orphan) {
2032 jbd_debug(4, "superblock will point to %lu\n", ino_next);
2033 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2034 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
2035 if (err)
2036 goto out_brelse;
2037 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2038 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
2039 } else {
2040 struct ext3_iloc iloc2;
2041 struct inode *i_prev =
2042 &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode;
2043
2044 jbd_debug(4, "orphan inode %lu will point to %lu\n",
2045 i_prev->i_ino, ino_next);
2046 err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
2047 if (err)
2048 goto out_brelse;
2049 NEXT_ORPHAN(i_prev) = ino_next;
2050 err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
2051 }
2052 if (err)
2053 goto out_brelse;
2054 NEXT_ORPHAN(inode) = 0;
2055 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2056
2057out_err:
2058 ext3_std_error(inode->i_sb, err);
2059out:
Eric Sandeenb8a052d2009-12-14 13:00:30 -06002060 mutex_unlock(&EXT3_SB(inode->i_sb)->s_orphan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return err;
2062
2063out_brelse:
2064 brelse(iloc.bh);
2065 goto out_err;
2066}
2067
2068static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
2069{
2070 int retval;
2071 struct inode * inode;
2072 struct buffer_head * bh;
2073 struct ext3_dir_entry_2 * de;
2074 handle_t *handle;
2075
2076 /* Initialize quotas before so that eventual writes go in
2077 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002078 dquot_initialize(dir);
2079 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002080
Jan Kara1f545872005-06-23 22:01:04 -07002081 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (IS_ERR(handle))
2083 return PTR_ERR(handle);
2084
2085 retval = -ENOENT;
Al Viro734711a2008-08-24 07:26:48 -04002086 bh = ext3_find_entry(dir, &dentry->d_name, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!bh)
2088 goto end_rmdir;
2089
2090 if (IS_DIRSYNC(dir))
2091 handle->h_sync = 1;
2092
2093 inode = dentry->d_inode;
2094
2095 retval = -EIO;
2096 if (le32_to_cpu(de->inode) != inode->i_ino)
2097 goto end_rmdir;
2098
2099 retval = -ENOTEMPTY;
2100 if (!empty_dir (inode))
2101 goto end_rmdir;
2102
2103 retval = ext3_delete_entry(handle, dir, de, bh);
2104 if (retval)
2105 goto end_rmdir;
2106 if (inode->i_nlink != 2)
2107 ext3_warning (inode->i_sb, "ext3_rmdir",
2108 "empty directory has nlink!=2 (%d)",
2109 inode->i_nlink);
2110 inode->i_version++;
Dave Hansence71ec32006-09-30 23:29:06 -07002111 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /* There's no need to set i_disksize: the fact that i_nlink is
2113 * zero will ensure that the right thing happens during any
2114 * recovery. */
2115 inode->i_size = 0;
2116 ext3_orphan_add(handle, inode);
2117 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2118 ext3_mark_inode_dirty(handle, inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002119 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 ext3_update_dx_flag(dir);
2121 ext3_mark_inode_dirty(handle, dir);
2122
2123end_rmdir:
2124 ext3_journal_stop(handle);
2125 brelse (bh);
2126 return retval;
2127}
2128
2129static int ext3_unlink(struct inode * dir, struct dentry *dentry)
2130{
2131 int retval;
2132 struct inode * inode;
2133 struct buffer_head * bh;
2134 struct ext3_dir_entry_2 * de;
2135 handle_t *handle;
2136
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002137 trace_ext3_unlink_enter(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Initialize quotas before so that eventual writes go
2139 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002140 dquot_initialize(dir);
2141 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002142
Jan Kara1f545872005-06-23 22:01:04 -07002143 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (IS_ERR(handle))
2145 return PTR_ERR(handle);
2146
2147 if (IS_DIRSYNC(dir))
2148 handle->h_sync = 1;
2149
2150 retval = -ENOENT;
Al Viro734711a2008-08-24 07:26:48 -04002151 bh = ext3_find_entry(dir, &dentry->d_name, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (!bh)
2153 goto end_unlink;
2154
2155 inode = dentry->d_inode;
2156
2157 retval = -EIO;
2158 if (le32_to_cpu(de->inode) != inode->i_ino)
2159 goto end_unlink;
2160
2161 if (!inode->i_nlink) {
2162 ext3_warning (inode->i_sb, "ext3_unlink",
2163 "Deleting nonexistent file (%lu), %d",
2164 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002165 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 retval = ext3_delete_entry(handle, dir, de, bh);
2168 if (retval)
2169 goto end_unlink;
2170 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
2171 ext3_update_dx_flag(dir);
2172 ext3_mark_inode_dirty(handle, dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002173 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!inode->i_nlink)
2175 ext3_orphan_add(handle, inode);
2176 inode->i_ctime = dir->i_ctime;
2177 ext3_mark_inode_dirty(handle, inode);
2178 retval = 0;
2179
2180end_unlink:
2181 ext3_journal_stop(handle);
2182 brelse (bh);
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002183 trace_ext3_unlink_exit(dentry, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return retval;
2185}
2186
2187static int ext3_symlink (struct inode * dir,
2188 struct dentry *dentry, const char * symname)
2189{
2190 handle_t *handle;
2191 struct inode * inode;
2192 int l, err, retries = 0;
Jan Karaae54870a2011-04-21 23:47:16 +02002193 int credits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 l = strlen(symname)+1;
2196 if (l > dir->i_sb->s_blocksize)
2197 return -ENAMETOOLONG;
2198
Christoph Hellwig871a2932010-03-03 09:05:07 -05002199 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002200
Jan Karaae54870a2011-04-21 23:47:16 +02002201 if (l > EXT3_N_BLOCKS * 4) {
2202 /*
2203 * For non-fast symlinks, we just allocate inode and put it on
2204 * orphan list in the first transaction => we need bitmap,
Eric Sandeend2db60d2011-08-11 09:51:46 -05002205 * group descriptor, sb, inode block, quota blocks, and
2206 * possibly selinux xattr blocks.
Jan Karaae54870a2011-04-21 23:47:16 +02002207 */
Eric Sandeend2db60d2011-08-11 09:51:46 -05002208 credits = 4 + EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2209 EXT3_XATTR_TRANS_BLOCKS;
Jan Karaae54870a2011-04-21 23:47:16 +02002210 } else {
2211 /*
2212 * Fast symlink. We have to add entry to directory
2213 * (EXT3_DATA_TRANS_BLOCKS + EXT3_INDEX_EXTRA_TRANS_BLOCKS),
2214 * allocate new inode (bitmap, group descriptor, inode block,
2215 * quota blocks, sb is already counted in previous macros).
2216 */
2217 credits = EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
2218 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2219 EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221retry:
Jan Karaae54870a2011-04-21 23:47:16 +02002222 handle = ext3_journal_start(dir, credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (IS_ERR(handle))
2224 return PTR_ERR(handle);
2225
2226 if (IS_DIRSYNC(dir))
2227 handle->h_sync = 1;
2228
Eric Paris2a7dba32011-02-01 11:05:39 -05002229 inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFLNK|S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 err = PTR_ERR(inode);
2231 if (IS_ERR(inode))
2232 goto out_stop;
2233
Jan Karaae54870a2011-04-21 23:47:16 +02002234 if (l > EXT3_N_BLOCKS * 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 inode->i_op = &ext3_symlink_inode_operations;
2236 ext3_set_aops(inode);
2237 /*
Jan Karaae54870a2011-04-21 23:47:16 +02002238 * We cannot call page_symlink() with transaction started
2239 * because it calls into ext3_write_begin() which acquires page
2240 * lock which ranks below transaction start (and it can also
2241 * wait for journal commit if we are running out of space). So
2242 * we have to stop transaction now and restart it when symlink
2243 * contents is written.
2244 *
2245 * To keep fs consistent in case of crash, we have to put inode
2246 * to orphan list in the mean time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 */
Jan Karaae54870a2011-04-21 23:47:16 +02002248 drop_nlink(inode);
2249 err = ext3_orphan_add(handle, inode);
2250 ext3_journal_stop(handle);
2251 if (err)
2252 goto err_drop_inode;
Nick Piggin54566b22009-01-04 12:00:53 -08002253 err = __page_symlink(inode, symname, l, 1);
Jan Karaae54870a2011-04-21 23:47:16 +02002254 if (err)
2255 goto err_drop_inode;
2256 /*
2257 * Now inode is being linked into dir (EXT3_DATA_TRANS_BLOCKS
2258 * + EXT3_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2259 */
2260 handle = ext3_journal_start(dir,
2261 EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
2262 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 1);
2263 if (IS_ERR(handle)) {
2264 err = PTR_ERR(handle);
2265 goto err_drop_inode;
2266 }
Al Viro0ce8c0102012-01-08 19:50:23 -05002267 set_nlink(inode, 1);
Jan Karaae54870a2011-04-21 23:47:16 +02002268 err = ext3_orphan_del(handle, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (err) {
Jan Karaae54870a2011-04-21 23:47:16 +02002270 ext3_journal_stop(handle);
Eric Sandeen731b9a52007-02-10 01:46:16 -08002271 drop_nlink(inode);
Jan Karaae54870a2011-04-21 23:47:16 +02002272 goto err_drop_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 } else {
2275 inode->i_op = &ext3_fast_symlink_inode_operations;
2276 memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2277 inode->i_size = l-1;
2278 }
2279 EXT3_I(inode)->i_disksize = inode->i_size;
2280 err = ext3_add_nondir(handle, dentry, inode);
2281out_stop:
2282 ext3_journal_stop(handle);
2283 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2284 goto retry;
2285 return err;
Jan Karaae54870a2011-04-21 23:47:16 +02002286err_drop_inode:
2287 unlock_new_inode(inode);
Jan Kara86c4f6d82011-04-27 18:20:44 +02002288 iput(inode);
Jan Karaae54870a2011-04-21 23:47:16 +02002289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292static int ext3_link (struct dentry * old_dentry,
2293 struct inode * dir, struct dentry *dentry)
2294{
2295 handle_t *handle;
2296 struct inode *inode = old_dentry->d_inode;
2297 int err, retries = 0;
2298
2299 if (inode->i_nlink >= EXT3_LINK_MAX)
2300 return -EMLINK;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002301
Christoph Hellwig871a2932010-03-03 09:05:07 -05002302 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304retry:
Jan Kara1f545872005-06-23 22:01:04 -07002305 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2307 if (IS_ERR(handle))
2308 return PTR_ERR(handle);
2309
2310 if (IS_DIRSYNC(dir))
2311 handle->h_sync = 1;
2312
2313 inode->i_ctime = CURRENT_TIME_SEC;
Eric Sandeen731b9a52007-02-10 01:46:16 -08002314 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002315 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Al Viroc38012d2008-12-30 02:02:50 -05002317 err = ext3_add_entry(handle, dentry, inode);
2318 if (!err) {
2319 ext3_mark_inode_dirty(handle, inode);
2320 d_instantiate(dentry, inode);
2321 } else {
2322 drop_nlink(inode);
2323 iput(inode);
2324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 ext3_journal_stop(handle);
2326 if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
2327 goto retry;
2328 return err;
2329}
2330
2331#define PARENT_INO(buffer) \
Jan Kara7c06a8d2007-11-14 17:00:19 -08002332 (ext3_next_entry((struct ext3_dir_entry_2 *)(buffer))->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334/*
2335 * Anybody can rename anything with this: the permission checks are left to the
2336 * higher-level routines.
2337 */
2338static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
2339 struct inode * new_dir,struct dentry *new_dentry)
2340{
2341 handle_t *handle;
2342 struct inode * old_inode, * new_inode;
2343 struct buffer_head * old_bh, * new_bh, * dir_bh;
2344 struct ext3_dir_entry_2 * old_de, * new_de;
Theodore Ts'oe7c8f502009-04-03 01:34:49 -04002345 int retval, flush_file = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Christoph Hellwig871a2932010-03-03 09:05:07 -05002347 dquot_initialize(old_dir);
2348 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 old_bh = new_bh = dir_bh = NULL;
2351
2352 /* Initialize quotas before so that eventual writes go
2353 * in separate transaction */
2354 if (new_dentry->d_inode)
Christoph Hellwig871a2932010-03-03 09:05:07 -05002355 dquot_initialize(new_dentry->d_inode);
Jan Kara1f545872005-06-23 22:01:04 -07002356 handle = ext3_journal_start(old_dir, 2 *
2357 EXT3_DATA_TRANS_BLOCKS(old_dir->i_sb) +
Dave Kleikampe9ad5622006-09-27 01:49:35 -07002358 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (IS_ERR(handle))
2360 return PTR_ERR(handle);
2361
2362 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2363 handle->h_sync = 1;
2364
Al Viro734711a2008-08-24 07:26:48 -04002365 old_bh = ext3_find_entry(old_dir, &old_dentry->d_name, &old_de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /*
2367 * Check for inode number is _not_ due to possible IO errors.
2368 * We might rmdir the source, keep it as pwd of some process
2369 * and merrily kill the link to whatever was created under the
2370 * same name. Goodbye sticky bit ;-<
2371 */
2372 old_inode = old_dentry->d_inode;
2373 retval = -ENOENT;
2374 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2375 goto end_rename;
2376
2377 new_inode = new_dentry->d_inode;
Al Viro734711a2008-08-24 07:26:48 -04002378 new_bh = ext3_find_entry(new_dir, &new_dentry->d_name, &new_de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (new_bh) {
2380 if (!new_inode) {
2381 brelse (new_bh);
2382 new_bh = NULL;
2383 }
2384 }
2385 if (S_ISDIR(old_inode->i_mode)) {
2386 if (new_inode) {
2387 retval = -ENOTEMPTY;
2388 if (!empty_dir (new_inode))
2389 goto end_rename;
2390 }
2391 retval = -EIO;
Carlos Maiolinoc3d59ad2012-10-02 23:59:23 -03002392 dir_bh = ext3_dir_bread(handle, old_inode, 0, 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!dir_bh)
2394 goto end_rename;
2395 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2396 goto end_rename;
2397 retval = -EMLINK;
2398 if (!new_inode && new_dir!=old_dir &&
2399 new_dir->i_nlink >= EXT3_LINK_MAX)
2400 goto end_rename;
2401 }
2402 if (!new_bh) {
2403 retval = ext3_add_entry (handle, new_dentry, old_inode);
2404 if (retval)
2405 goto end_rename;
2406 } else {
2407 BUFFER_TRACE(new_bh, "get write access");
Namhyung Kimad1857a2010-11-23 13:30:33 +09002408 retval = ext3_journal_get_write_access(handle, new_bh);
2409 if (retval)
2410 goto journal_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 new_de->inode = cpu_to_le32(old_inode->i_ino);
2412 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2413 EXT3_FEATURE_INCOMPAT_FILETYPE))
2414 new_de->file_type = old_de->file_type;
2415 new_dir->i_version++;
Jan Kara0b230762008-04-28 02:16:12 -07002416 new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME_SEC;
2417 ext3_mark_inode_dirty(handle, new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
Namhyung Kimad1857a2010-11-23 13:30:33 +09002419 retval = ext3_journal_dirty_metadata(handle, new_bh);
2420 if (retval)
2421 goto journal_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 brelse(new_bh);
2423 new_bh = NULL;
2424 }
2425
2426 /*
2427 * Like most other Unix systems, set the ctime for inodes on a
2428 * rename.
2429 */
2430 old_inode->i_ctime = CURRENT_TIME_SEC;
2431 ext3_mark_inode_dirty(handle, old_inode);
2432
2433 /*
2434 * ok, that's it
2435 */
2436 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2437 old_de->name_len != old_dentry->d_name.len ||
2438 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2439 (retval = ext3_delete_entry(handle, old_dir,
2440 old_de, old_bh)) == -ENOENT) {
2441 /* old_de could have moved from under us during htree split, so
2442 * make sure that we are deleting the right entry. We might
2443 * also be pointing to a stale entry in the unused part of
2444 * old_bh so just checking inum and the name isn't enough. */
2445 struct buffer_head *old_bh2;
2446 struct ext3_dir_entry_2 *old_de2;
2447
Al Viro734711a2008-08-24 07:26:48 -04002448 old_bh2 = ext3_find_entry(old_dir, &old_dentry->d_name,
2449 &old_de2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (old_bh2) {
2451 retval = ext3_delete_entry(handle, old_dir,
2452 old_de2, old_bh2);
2453 brelse(old_bh2);
2454 }
2455 }
2456 if (retval) {
2457 ext3_warning(old_dir->i_sb, "ext3_rename",
2458 "Deleting old file (%lu), %d, error=%d",
2459 old_dir->i_ino, old_dir->i_nlink, retval);
2460 }
2461
2462 if (new_inode) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002463 drop_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 new_inode->i_ctime = CURRENT_TIME_SEC;
2465 }
2466 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
2467 ext3_update_dx_flag(old_dir);
2468 if (dir_bh) {
2469 BUFFER_TRACE(dir_bh, "get_write_access");
Namhyung Kimad1857a2010-11-23 13:30:33 +09002470 retval = ext3_journal_get_write_access(handle, dir_bh);
2471 if (retval)
2472 goto journal_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
2474 BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
Namhyung Kimad1857a2010-11-23 13:30:33 +09002475 retval = ext3_journal_dirty_metadata(handle, dir_bh);
2476 if (retval) {
2477journal_error:
2478 ext3_std_error(new_dir->i_sb, retval);
2479 goto end_rename;
2480 }
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002481 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (new_inode) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002483 drop_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 } else {
Dave Hansend8c76e62006-09-30 23:29:04 -07002485 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 ext3_update_dx_flag(new_dir);
2487 ext3_mark_inode_dirty(handle, new_dir);
2488 }
2489 }
2490 ext3_mark_inode_dirty(handle, old_dir);
2491 if (new_inode) {
2492 ext3_mark_inode_dirty(handle, new_inode);
2493 if (!new_inode->i_nlink)
2494 ext3_orphan_add(handle, new_inode);
Theodore Ts'oe7c8f502009-04-03 01:34:49 -04002495 if (ext3_should_writeback_data(new_inode))
2496 flush_file = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 retval = 0;
2499
2500end_rename:
2501 brelse (dir_bh);
2502 brelse (old_bh);
2503 brelse (new_bh);
2504 ext3_journal_stop(handle);
Theodore Ts'oe7c8f502009-04-03 01:34:49 -04002505 if (retval == 0 && flush_file)
2506 filemap_flush(old_inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return retval;
2508}
2509
2510/*
2511 * directories can handle most operations...
2512 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08002513const struct inode_operations ext3_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .create = ext3_create,
2515 .lookup = ext3_lookup,
2516 .link = ext3_link,
2517 .unlink = ext3_unlink,
2518 .symlink = ext3_symlink,
2519 .mkdir = ext3_mkdir,
2520 .rmdir = ext3_rmdir,
2521 .mknod = ext3_mknod,
2522 .rename = ext3_rename,
2523 .setattr = ext3_setattr,
2524#ifdef CONFIG_EXT3_FS_XATTR
2525 .setxattr = generic_setxattr,
2526 .getxattr = generic_getxattr,
2527 .listxattr = ext3_listxattr,
2528 .removexattr = generic_removexattr,
2529#endif
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002530 .get_acl = ext3_get_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531};
2532
Arjan van de Ven754661f2007-02-12 00:55:38 -08002533const struct inode_operations ext3_special_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 .setattr = ext3_setattr,
2535#ifdef CONFIG_EXT3_FS_XATTR
2536 .setxattr = generic_setxattr,
2537 .getxattr = generic_getxattr,
2538 .listxattr = ext3_listxattr,
2539 .removexattr = generic_removexattr,
2540#endif
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002541 .get_acl = ext3_get_acl,
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002542};