blob: af13c908f617c45dc2e5263c4aa29d5a91522f2b [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
Mingming Caodab291a2006-10-11 01:21:01 -070029#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/quotaops.h>
35#include <linux/buffer_head.h>
36#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040037#include "ext4.h"
38#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039
Dave Kleikampac27a0e2006-10-11 01:20:50 -070040#include "xattr.h"
41#include "acl.h"
42
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040043#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044/*
45 * define how far ahead to read directories while searching them.
46 */
47#define NAMEI_RA_CHUNKS 2
48#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040049#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050
Mingming Cao617ba132006-10-11 01:20:53 -070051static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050053 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054{
55 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040056 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057
Theodore Ts'odf981d02012-08-17 09:48:17 -040058 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050060 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
61 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040062
Dave Kleikampac27a0e2006-10-11 01:20:50 -070063 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
64
Theodore Ts'o1c215022014-08-29 20:52:15 -040065 bh = ext4_bread(handle, inode, *block, 1);
66 if (IS_ERR(bh))
67 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050068 inode->i_size += inode->i_sb->s_blocksize;
69 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040070 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050071 err = ext4_journal_get_write_access(handle, bh);
72 if (err) {
73 brelse(bh);
74 ext4_std_error(inode->i_sb, err);
75 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040076 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077 return bh;
78}
79
Theodore Ts'odc6982f2013-02-14 23:59:26 -050080static int ext4_dx_csum_verify(struct inode *inode,
81 struct ext4_dir_entry *dirent);
82
83typedef enum {
84 EITHER, INDEX, DIRENT
85} dirblock_type_t;
86
87#define ext4_read_dirblock(inode, block, type) \
88 __ext4_read_dirblock((inode), (block), (type), __LINE__)
89
90static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
91 ext4_lblk_t block,
92 dirblock_type_t type,
93 unsigned int line)
94{
95 struct buffer_head *bh;
96 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -040097 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -050098
Theodore Ts'o1c215022014-08-29 20:52:15 -040099 bh = ext4_bread(NULL, inode, block, 0);
100 if (IS_ERR(bh)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500101 __ext4_warning(inode->i_sb, __func__, line,
Theodore Ts'o1c215022014-08-29 20:52:15 -0400102 "error %ld reading directory block "
103 "(ino %lu, block %lu)", PTR_ERR(bh), inode->i_ino,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500104 (unsigned long) block);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400105
106 return bh;
107 }
108 if (!bh) {
109 ext4_error_inode(inode, __func__, line, block, "Directory hole found");
110 return ERR_PTR(-EIO);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500111 }
112 dirent = (struct ext4_dir_entry *) bh->b_data;
113 /* Determine whether or not we have an index block */
114 if (is_dx(inode)) {
115 if (block == 0)
116 is_dx_block = 1;
117 else if (ext4_rec_len_from_disk(dirent->rec_len,
118 inode->i_sb->s_blocksize) ==
119 inode->i_sb->s_blocksize)
120 is_dx_block = 1;
121 }
122 if (!is_dx_block && type == INDEX) {
123 ext4_error_inode(inode, __func__, line, block,
124 "directory leaf block found instead of index block");
125 return ERR_PTR(-EIO);
126 }
127 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
128 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) ||
129 buffer_verified(bh))
130 return bh;
131
132 /*
133 * An empty leaf block can get mistaken for a index block; for
134 * this reason, we can only check the index checksum when the
135 * caller is sure it should be an index block.
136 */
137 if (is_dx_block && type == INDEX) {
138 if (ext4_dx_csum_verify(inode, dirent))
139 set_buffer_verified(bh);
140 else {
141 ext4_error_inode(inode, __func__, line, block,
142 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143 brelse(bh);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500144 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145 }
146 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500147 if (!is_dx_block) {
148 if (ext4_dirent_csum_verify(inode, dirent))
149 set_buffer_verified(bh);
150 else {
151 ext4_error_inode(inode, __func__, line, block,
152 "Directory block failed checksum");
153 brelse(bh);
154 return ERR_PTR(-EIO);
155 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700156 }
157 return bh;
158}
159
160#ifndef assert
161#define assert(test) J_ASSERT(test)
162#endif
163
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164#ifdef DX_DEBUG
165#define dxtrace(command) command
166#else
167#define dxtrace(command)
168#endif
169
170struct fake_dirent
171{
172 __le32 inode;
173 __le16 rec_len;
174 u8 name_len;
175 u8 file_type;
176};
177
178struct dx_countlimit
179{
180 __le16 limit;
181 __le16 count;
182};
183
184struct dx_entry
185{
186 __le32 hash;
187 __le32 block;
188};
189
190/*
191 * dx_root_info is laid out so that if it should somehow get overlaid by a
192 * dirent the two low bits of the hash version will be zero. Therefore, the
193 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
194 */
195
196struct dx_root
197{
198 struct fake_dirent dot;
199 char dot_name[4];
200 struct fake_dirent dotdot;
201 char dotdot_name[4];
202 struct dx_root_info
203 {
204 __le32 reserved_zero;
205 u8 hash_version;
206 u8 info_length; /* 8 */
207 u8 indirect_levels;
208 u8 unused_flags;
209 }
210 info;
211 struct dx_entry entries[0];
212};
213
214struct dx_node
215{
216 struct fake_dirent fake;
217 struct dx_entry entries[0];
218};
219
220
221struct dx_frame
222{
223 struct buffer_head *bh;
224 struct dx_entry *entries;
225 struct dx_entry *at;
226};
227
228struct dx_map_entry
229{
230 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700231 u16 offs;
232 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233};
234
Darrick J. Wonge6153912012-04-29 18:23:10 -0400235/*
236 * This goes at the end of each htree block.
237 */
238struct dx_tail {
239 u32 dt_reserved;
240 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
241};
242
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500243static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
244static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400245static inline unsigned dx_get_hash(struct dx_entry *entry);
246static void dx_set_hash(struct dx_entry *entry, unsigned value);
247static unsigned dx_get_count(struct dx_entry *entries);
248static unsigned dx_get_limit(struct dx_entry *entries);
249static void dx_set_count(struct dx_entry *entries, unsigned value);
250static void dx_set_limit(struct dx_entry *entries, unsigned value);
251static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
252static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400253static struct dx_frame *dx_probe(const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 struct inode *dir,
255 struct dx_hash_info *hinfo,
256 struct dx_frame *frame,
257 int *err);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400258static void dx_release(struct dx_frame *frames);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500259static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400260 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400262static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500263 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500264static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500265static void dx_insert_block(struct dx_frame *frame,
266 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700267static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 struct dx_frame *frame,
269 struct dx_frame *frames,
270 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400271static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
272 const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400273 struct ext4_dir_entry_2 **res_dir);
Mingming Cao617ba132006-10-11 01:20:53 -0700274static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 struct inode *inode);
276
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400277/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500278void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
279 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400280{
281 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
282 t->det_rec_len = ext4_rec_len_to_disk(
283 sizeof(struct ext4_dir_entry_tail), blocksize);
284 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
285}
286
287/* Walk through a dirent block to find a checksum "dirent" at the tail */
288static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
289 struct ext4_dir_entry *de)
290{
291 struct ext4_dir_entry_tail *t;
292
293#ifdef PARANOID
294 struct ext4_dir_entry *d, *top;
295
296 d = de;
297 top = (struct ext4_dir_entry *)(((void *)de) +
298 (EXT4_BLOCK_SIZE(inode->i_sb) -
299 sizeof(struct ext4_dir_entry_tail)));
300 while (d < top && d->rec_len)
301 d = (struct ext4_dir_entry *)(((void *)d) +
302 le16_to_cpu(d->rec_len));
303
304 if (d != top)
305 return NULL;
306
307 t = (struct ext4_dir_entry_tail *)d;
308#else
309 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
310#endif
311
312 if (t->det_reserved_zero1 ||
313 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
314 t->det_reserved_zero2 ||
315 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
316 return NULL;
317
318 return t;
319}
320
321static __le32 ext4_dirent_csum(struct inode *inode,
322 struct ext4_dir_entry *dirent, int size)
323{
324 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
325 struct ext4_inode_info *ei = EXT4_I(inode);
326 __u32 csum;
327
328 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
329 return cpu_to_le32(csum);
330}
331
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500332static void warn_no_space_for_csum(struct inode *inode)
333{
334 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
335 "checksum. Please run e2fsck -D.", inode->i_ino);
336}
337
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400338int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
339{
340 struct ext4_dir_entry_tail *t;
341
342 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
343 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
344 return 1;
345
346 t = get_dirent_tail(inode, dirent);
347 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500348 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400349 return 0;
350 }
351
352 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
353 (void *)t - (void *)dirent))
354 return 0;
355
356 return 1;
357}
358
359static void ext4_dirent_csum_set(struct inode *inode,
360 struct ext4_dir_entry *dirent)
361{
362 struct ext4_dir_entry_tail *t;
363
364 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
365 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
366 return;
367
368 t = get_dirent_tail(inode, dirent);
369 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500370 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400371 return;
372 }
373
374 t->det_checksum = ext4_dirent_csum(inode, dirent,
375 (void *)t - (void *)dirent);
376}
377
Tao Ma3c47d542012-12-10 14:05:59 -0500378int ext4_handle_dirty_dirent_node(handle_t *handle,
379 struct inode *inode,
380 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400381{
382 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
383 return ext4_handle_dirty_metadata(handle, inode, bh);
384}
385
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400386static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
387 struct ext4_dir_entry *dirent,
388 int *offset)
389{
390 struct ext4_dir_entry *dp;
391 struct dx_root_info *root;
392 int count_offset;
393
394 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
395 count_offset = 8;
396 else if (le16_to_cpu(dirent->rec_len) == 12) {
397 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
398 if (le16_to_cpu(dp->rec_len) !=
399 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
400 return NULL;
401 root = (struct dx_root_info *)(((void *)dp + 12));
402 if (root->reserved_zero ||
403 root->info_length != sizeof(struct dx_root_info))
404 return NULL;
405 count_offset = 32;
406 } else
407 return NULL;
408
409 if (offset)
410 *offset = count_offset;
411 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
412}
413
414static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
415 int count_offset, int count, struct dx_tail *t)
416{
417 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
418 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400419 __u32 csum;
420 __le32 save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400421 int size;
422
423 size = count_offset + (count * sizeof(struct dx_entry));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400424 save_csum = t->dt_checksum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400425 t->dt_checksum = 0;
426 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
427 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400428 t->dt_checksum = save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400429
430 return cpu_to_le32(csum);
431}
432
433static int ext4_dx_csum_verify(struct inode *inode,
434 struct ext4_dir_entry *dirent)
435{
436 struct dx_countlimit *c;
437 struct dx_tail *t;
438 int count_offset, limit, count;
439
440 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
441 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
442 return 1;
443
444 c = get_dx_countlimit(inode, dirent, &count_offset);
445 if (!c) {
446 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
447 return 1;
448 }
449 limit = le16_to_cpu(c->limit);
450 count = le16_to_cpu(c->count);
451 if (count_offset + (limit * sizeof(struct dx_entry)) >
452 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500453 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400454 return 1;
455 }
456 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
457
458 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
459 count, t))
460 return 0;
461 return 1;
462}
463
464static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
465{
466 struct dx_countlimit *c;
467 struct dx_tail *t;
468 int count_offset, limit, count;
469
470 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
471 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
472 return;
473
474 c = get_dx_countlimit(inode, dirent, &count_offset);
475 if (!c) {
476 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
477 return;
478 }
479 limit = le16_to_cpu(c->limit);
480 count = le16_to_cpu(c->count);
481 if (count_offset + (limit * sizeof(struct dx_entry)) >
482 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500483 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400484 return;
485 }
486 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
487
488 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
489}
490
491static inline int ext4_handle_dirty_dx_node(handle_t *handle,
492 struct inode *inode,
493 struct buffer_head *bh)
494{
495 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
496 return ext4_handle_dirty_metadata(handle, inode, bh);
497}
498
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499/*
Li Zefanf795e142008-07-11 19:27:31 -0400500 * p is at least 6 bytes before the end of page
501 */
502static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500503ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400504{
505 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500506 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400507}
508
509/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 * Future: use high four bits of block for coalesce-on-delete flags
511 * Mask them off for now.
512 */
513
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500514static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515{
516 return le32_to_cpu(entry->block) & 0x00ffffff;
517}
518
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500519static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520{
521 entry->block = cpu_to_le32(value);
522}
523
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400524static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525{
526 return le32_to_cpu(entry->hash);
527}
528
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400529static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530{
531 entry->hash = cpu_to_le32(value);
532}
533
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400534static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535{
536 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
537}
538
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400539static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540{
541 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
542}
543
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400544static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545{
546 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
547}
548
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400549static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550{
551 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
552}
553
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400554static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555{
Mingming Cao617ba132006-10-11 01:20:53 -0700556 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
557 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400558
559 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
560 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
561 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400562 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563}
564
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400565static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566{
Mingming Cao617ba132006-10-11 01:20:53 -0700567 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400568
569 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
570 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
571 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400572 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573}
574
575/*
576 * Debug
577 */
578#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400579static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580{
Andrew Morton63f57932006-10-11 01:21:24 -0700581 int i, n = dx_get_count (entries);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400582 printk(KERN_DEBUG "%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700583 for (i = 0; i < n; i++) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400584 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500585 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700586 }
587 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588}
589
590struct stats
591{
592 unsigned names;
593 unsigned space;
594 unsigned bcount;
595};
596
Mingming Cao617ba132006-10-11 01:20:53 -0700597static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598 int size, int show_names)
599{
600 unsigned names = 0, space = 0;
601 char *base = (char *) de;
602 struct dx_hash_info h = *hinfo;
603
604 printk("names: ");
605 while ((char *) de < base + size)
606 {
607 if (de->inode)
608 {
609 if (show_names)
610 {
611 int len = de->name_len;
612 char *name = de->name;
613 while (len--) printk("%c", *name++);
Mingming Cao617ba132006-10-11 01:20:53 -0700614 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615 printk(":%x.%u ", h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400616 (unsigned) ((char *) de - base));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 }
Mingming Cao617ba132006-10-11 01:20:53 -0700618 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 names++;
620 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500621 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700622 }
623 printk("(%i)\n", names);
624 return (struct stats) { names, space, 1 };
625}
626
627struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
628 struct dx_entry *entries, int levels)
629{
630 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400631 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 unsigned bcount = 0;
633 struct buffer_head *bh;
634 int err;
635 printk("%i indexed blocks...\n", count);
636 for (i = 0; i < count; i++, entries++)
637 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500638 ext4_lblk_t block = dx_get_block(entries);
639 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
641 struct stats stats;
642 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400643 bh = ext4_bread(NULL,dir, block, 0);
644 if (!bh || IS_ERR(bh))
645 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 stats = levels?
647 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Mingming Cao617ba132006-10-11 01:20:53 -0700648 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 names += stats.names;
650 space += stats.space;
651 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400652 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 }
654 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400655 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400656 levels ? "" : " ", names, space/bcount,
657 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 return (struct stats) { names, space, bcount};
659}
660#endif /* DX_DEBUG */
661
662/*
663 * Probe for a directory leaf block to search.
664 *
665 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
666 * error in the directory index, and the caller should fall back to
667 * searching the directory normally. The callers of dx_probe **MUST**
668 * check for this error code, and make sure it never gets reflected
669 * back to userspace.
670 */
671static struct dx_frame *
Theodore Ts'of702ba02008-09-22 15:21:01 -0400672dx_probe(const struct qstr *d_name, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
674{
675 unsigned count, indirect;
676 struct dx_entry *at, *entries, *p, *q, *m;
677 struct dx_root *root;
678 struct buffer_head *bh;
679 struct dx_frame *frame = frame_in;
680 u32 hash;
681
682 frame->bh = NULL;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500683 bh = ext4_read_dirblock(dir, 0, INDEX);
684 if (IS_ERR(bh)) {
685 *err = PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400687 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700688 root = (struct dx_root *) bh->b_data;
689 if (root->info.hash_version != DX_HASH_TEA &&
690 root->info.hash_version != DX_HASH_HALF_MD4 &&
691 root->info.hash_version != DX_HASH_LEGACY) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500692 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 root->info.hash_version);
694 brelse(bh);
695 *err = ERR_BAD_DX_DIR;
696 goto fail;
697 }
698 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400699 if (hinfo->hash_version <= DX_HASH_TEA)
700 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700701 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400702 if (d_name)
703 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 hash = hinfo->hash;
705
706 if (root->info.unused_flags & 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500707 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 root->info.unused_flags);
709 brelse(bh);
710 *err = ERR_BAD_DX_DIR;
711 goto fail;
712 }
713
714 if ((indirect = root->info.indirect_levels) > 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500715 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 root->info.indirect_levels);
717 brelse(bh);
718 *err = ERR_BAD_DX_DIR;
719 goto fail;
720 }
721
722 entries = (struct dx_entry *) (((char *)&root->info) +
723 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700724
725 if (dx_get_limit(entries) != dx_root_limit(dir,
726 root->info.info_length)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500727 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700728 brelse(bh);
729 *err = ERR_BAD_DX_DIR;
730 goto fail;
731 }
732
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400733 dxtrace(printk("Look up %x", hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 while (1)
735 {
736 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700737 if (!count || count > dx_get_limit(entries)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500738 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700739 "dx entry: no count or count > limit");
740 brelse(bh);
741 *err = ERR_BAD_DX_DIR;
742 goto fail2;
743 }
744
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 p = entries + 1;
746 q = entries + count - 1;
747 while (p <= q)
748 {
749 m = p + (q - p)/2;
750 dxtrace(printk("."));
751 if (dx_get_hash(m) > hash)
752 q = m - 1;
753 else
754 p = m + 1;
755 }
756
757 if (0) // linear search cross check
758 {
759 unsigned n = count - 1;
760 at = entries;
761 while (n--)
762 {
763 dxtrace(printk(","));
764 if (dx_get_hash(++at) > hash)
765 {
766 at--;
767 break;
768 }
769 }
770 assert (at == p - 1);
771 }
772
773 at = p - 1;
774 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
775 frame->bh = bh;
776 frame->entries = entries;
777 frame->at = at;
778 if (!indirect--) return frame;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500779 bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
780 if (IS_ERR(bh)) {
781 *err = PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 goto fail2;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400783 }
Guo Chao41be8712013-01-28 21:33:28 -0500784 entries = ((struct dx_node *) bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400785
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700786 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500787 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700788 "dx entry: limit != node limit");
789 brelse(bh);
790 *err = ERR_BAD_DX_DIR;
791 goto fail2;
792 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700794 frame->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 }
796fail2:
797 while (frame >= frame_in) {
798 brelse(frame->bh);
799 frame--;
800 }
801fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700802 if (*err == ERR_BAD_DX_DIR)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500803 ext4_warning(dir->i_sb,
Zheng Liu9ee49302012-02-20 23:09:36 -0500804 "Corrupt dir inode %lu, running e2fsck is "
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700805 "recommended.", dir->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 return NULL;
807}
808
809static void dx_release (struct dx_frame *frames)
810{
811 if (frames[0].bh == NULL)
812 return;
813
814 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
815 brelse(frames[1].bh);
816 brelse(frames[0].bh);
817}
818
819/*
820 * This function increments the frame pointer to search the next leaf
821 * block, and reads in the necessary intervening nodes if the search
822 * should be necessary. Whether or not the search is necessary is
823 * controlled by the hash parameter. If the hash value is even, then
824 * the search is only continued if the next block starts with that
825 * hash value. This is used if we are searching for a specific file.
826 *
827 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
828 *
829 * This function returns 1 if the caller should continue to search,
830 * or 0 if it should not. If there is an error reading one of the
831 * index blocks, it will a negative error code.
832 *
833 * If start_hash is non-null, it will be filled in with the starting
834 * hash of the next page.
835 */
Mingming Cao617ba132006-10-11 01:20:53 -0700836static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 struct dx_frame *frame,
838 struct dx_frame *frames,
839 __u32 *start_hash)
840{
841 struct dx_frame *p;
842 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500843 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 __u32 bhash;
845
846 p = frame;
847 /*
848 * Find the next leaf page by incrementing the frame pointer.
849 * If we run out of entries in the interior node, loop around and
850 * increment pointer in the parent node. When we break out of
851 * this loop, num_frames indicates the number of interior
852 * nodes need to be read.
853 */
854 while (1) {
855 if (++(p->at) < p->entries + dx_get_count(p->entries))
856 break;
857 if (p == frames)
858 return 0;
859 num_frames++;
860 p--;
861 }
862
863 /*
864 * If the hash is 1, then continue only if the next page has a
865 * continuation hash of any value. This is used for readdir
866 * handling. Otherwise, check to see if the hash matches the
867 * desired contiuation hash. If it doesn't, return since
868 * there's no point to read in the successive index pages.
869 */
870 bhash = dx_get_hash(p->at);
871 if (start_hash)
872 *start_hash = bhash;
873 if ((hash & 1) == 0) {
874 if ((bhash & ~1) != hash)
875 return 0;
876 }
877 /*
878 * If the hash is HASH_NB_ALWAYS, we always go to the next
879 * block so no check is necessary
880 */
881 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500882 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
883 if (IS_ERR(bh))
884 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400886 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 p->bh = bh;
888 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
889 }
890 return 1;
891}
892
893
894/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 * This function fills a red-black tree with information from a
896 * directory block. It returns the number directory entries loaded
897 * into the tree. If there is an error it is returned in err.
898 */
899static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500900 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 struct dx_hash_info *hinfo,
902 __u32 start_hash, __u32 start_minor_hash)
903{
904 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700905 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400906 int err = 0, count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500908 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
909 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500910 bh = ext4_read_dirblock(dir, block, DIRENT);
911 if (IS_ERR(bh))
912 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400913
Mingming Cao617ba132006-10-11 01:20:53 -0700914 de = (struct ext4_dir_entry_2 *) bh->b_data;
915 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700917 EXT4_DIR_REC_LEN(0));
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500918 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -0500919 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -0500920 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -0500921 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
922 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -0400923 /* silently ignore the rest of the block */
924 break;
Eric Sandeene6c40212006-12-06 20:36:28 -0800925 }
Mingming Cao617ba132006-10-11 01:20:53 -0700926 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 if ((hinfo->hash < start_hash) ||
928 ((hinfo->hash == start_hash) &&
929 (hinfo->minor_hash < start_minor_hash)))
930 continue;
931 if (de->inode == 0)
932 continue;
Mingming Cao617ba132006-10-11 01:20:53 -0700933 if ((err = ext4_htree_store_dirent(dir_file,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 hinfo->hash, hinfo->minor_hash, de)) != 0) {
935 brelse(bh);
936 return err;
937 }
938 count++;
939 }
940 brelse(bh);
941 return count;
942}
943
944
945/*
946 * This function fills a red-black tree with information from a
947 * directory. We start scanning the directory in hash order, starting
948 * at start_hash and start_minor_hash.
949 *
950 * This function returns the number of entries inserted into the tree,
951 * or a negative error code.
952 */
Mingming Cao617ba132006-10-11 01:20:53 -0700953int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 __u32 start_minor_hash, __u32 *next_hash)
955{
956 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -0700957 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 struct dx_frame frames[2], *frame;
959 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500960 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500962 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 __u32 hashval;
964
Theodore Ts'o60e66792010-05-17 07:00:00 -0400965 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400966 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -0500967 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400968 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -0700969 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400970 if (hinfo.hash_version <= DX_HASH_TEA)
971 hinfo.hash_version +=
972 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700973 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -0400974 if (ext4_has_inline_data(dir)) {
975 int has_inline_data = 1;
976 count = htree_inlinedir_to_tree(dir_file, dir, 0,
977 &hinfo, start_hash,
978 start_minor_hash,
979 &has_inline_data);
980 if (has_inline_data) {
981 *next_hash = ~0;
982 return count;
983 }
984 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
986 start_hash, start_minor_hash);
987 *next_hash = ~0;
988 return count;
989 }
990 hinfo.hash = start_hash;
991 hinfo.minor_hash = 0;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400992 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 if (!frame)
994 return err;
995
996 /* Add '.' and '..' from the htree header */
997 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -0700998 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
999 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 goto errout;
1001 count++;
1002 }
1003 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001004 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001005 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -07001006 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007 goto errout;
1008 count++;
1009 }
1010
1011 while (1) {
1012 block = dx_get_block(frame->at);
1013 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1014 start_hash, start_minor_hash);
1015 if (ret < 0) {
1016 err = ret;
1017 goto errout;
1018 }
1019 count += ret;
1020 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001021 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001022 frame, frames, &hashval);
1023 *next_hash = hashval;
1024 if (ret < 0) {
1025 err = ret;
1026 goto errout;
1027 }
1028 /*
1029 * Stop if: (a) there are no more entries, or
1030 * (b) we have inserted at least one entry and the
1031 * next hash value is not a continuation
1032 */
1033 if ((ret == 0) ||
1034 (count && ((hashval & 1) == 0)))
1035 break;
1036 }
1037 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001038 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1039 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 return count;
1041errout:
1042 dx_release(frames);
1043 return (err);
1044}
1045
Tao Ma7335cd32012-12-10 14:05:59 -05001046static inline int search_dirblock(struct buffer_head *bh,
1047 struct inode *dir,
1048 const struct qstr *d_name,
1049 unsigned int offset,
1050 struct ext4_dir_entry_2 **res_dir)
1051{
1052 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1053 d_name, offset, res_dir);
1054}
1055
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056/*
1057 * Directory block splitting, compacting
1058 */
1059
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001060/*
1061 * Create map of hash values, offsets, and sizes, stored at end of block.
1062 * Returns number of entries mapped.
1063 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001064static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1065 struct dx_hash_info *hinfo,
1066 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067{
1068 int count = 0;
1069 char *base = (char *) de;
1070 struct dx_hash_info h = *hinfo;
1071
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001072 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001074 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 map_tail--;
1076 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001077 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001078 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 count++;
1080 cond_resched();
1081 }
1082 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001083 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 }
1085 return count;
1086}
1087
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001088/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1090{
Andrew Morton63f57932006-10-11 01:21:24 -07001091 struct dx_map_entry *p, *q, *top = map + count - 1;
1092 int more;
1093 /* Combsort until bubble sort doesn't suck */
1094 while (count > 2) {
1095 count = count*10/13;
1096 if (count - 9 < 2) /* 9, 10 -> 11 */
1097 count = 11;
1098 for (p = top, q = p - count; q >= map; p--, q--)
1099 if (p->hash < q->hash)
1100 swap(*p, *q);
1101 }
1102 /* Garden variety bubble sort */
1103 do {
1104 more = 0;
1105 q = top;
1106 while (q-- > map) {
1107 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001109 swap(*(q+1), *q);
1110 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111 }
1112 } while(more);
1113}
1114
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001115static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116{
1117 struct dx_entry *entries = frame->entries;
1118 struct dx_entry *old = frame->at, *new = old + 1;
1119 int count = dx_get_count(entries);
1120
1121 assert(count < dx_get_limit(entries));
1122 assert(old < entries + count);
1123 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1124 dx_set_hash(new, hash);
1125 dx_set_block(new, block);
1126 dx_set_count(entries, count + 1);
1127}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001128
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129/*
Mingming Cao617ba132006-10-11 01:20:53 -07001130 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 *
Mingming Cao617ba132006-10-11 01:20:53 -07001132 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 * `de != NULL' is guaranteed by caller.
1134 */
Mingming Cao617ba132006-10-11 01:20:53 -07001135static inline int ext4_match (int len, const char * const name,
1136 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137{
1138 if (len != de->name_len)
1139 return 0;
1140 if (!de->inode)
1141 return 0;
1142 return !memcmp(name, de->name, len);
1143}
1144
1145/*
1146 * Returns 0 if not found, -1 on failure, and 1 on success
1147 */
Tao Ma7335cd32012-12-10 14:05:59 -05001148int search_dir(struct buffer_head *bh,
1149 char *search_buf,
1150 int buf_size,
1151 struct inode *dir,
1152 const struct qstr *d_name,
1153 unsigned int offset,
1154 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155{
Mingming Cao617ba132006-10-11 01:20:53 -07001156 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157 char * dlimit;
1158 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001159 const char *name = d_name->name;
1160 int namelen = d_name->len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161
Tao Ma7335cd32012-12-10 14:05:59 -05001162 de = (struct ext4_dir_entry_2 *)search_buf;
1163 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 while ((char *) de < dlimit) {
1165 /* this code is executed quadratically often */
1166 /* do minimal checking `by hand' */
1167
1168 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -07001169 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 /* found a match - just to be sure, do a full check */
Tao Ma226ba972012-12-10 14:05:58 -05001171 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1172 bh->b_size, offset))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 return -1;
1174 *res_dir = de;
1175 return 1;
1176 }
1177 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001178 de_len = ext4_rec_len_from_disk(de->rec_len,
1179 dir->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 if (de_len <= 0)
1181 return -1;
1182 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001183 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001184 }
1185 return 0;
1186}
1187
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001188static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1189 struct ext4_dir_entry *de)
1190{
1191 struct super_block *sb = dir->i_sb;
1192
1193 if (!is_dx(dir))
1194 return 0;
1195 if (block == 0)
1196 return 1;
1197 if (de->inode == 0 &&
1198 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1199 sb->s_blocksize)
1200 return 1;
1201 return 0;
1202}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203
1204/*
Mingming Cao617ba132006-10-11 01:20:53 -07001205 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 *
1207 * finds an entry in the specified directory with the wanted name. It
1208 * returns the cache buffer in which the entry was found, and the entry
1209 * itself (as a parameter - res_dir). It does NOT read the inode of the
1210 * entry - you'll have to do that yourself if you want to.
1211 *
1212 * The returned buffer_head has ->b_count elevated. The caller is expected
1213 * to brelse() it when appropriate.
1214 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001215static struct buffer_head * ext4_find_entry (struct inode *dir,
1216 const struct qstr *d_name,
Tao Ma32f7f222012-12-10 14:06:01 -05001217 struct ext4_dir_entry_2 **res_dir,
1218 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001220 struct super_block *sb;
1221 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1222 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001223 ext4_lblk_t start, block, b;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001224 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 int ra_max = 0; /* Number of bh's in the readahead
1226 buffer, bh_use[] */
1227 int ra_ptr = 0; /* Current index into readahead
1228 buffer */
1229 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001230 ext4_lblk_t nblocks;
Theodore Ts'o10560082014-08-29 20:51:32 -04001231 int i, namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232
1233 *res_dir = NULL;
1234 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001235 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001236 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001238
1239 if (ext4_has_inline_data(dir)) {
1240 int has_inline_data = 1;
1241 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1242 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001243 if (has_inline_data) {
1244 if (inlined)
1245 *inlined = 1;
Tao Mae8e948e2012-12-10 14:06:00 -05001246 return ret;
Tao Ma32f7f222012-12-10 14:06:01 -05001247 }
Tao Mae8e948e2012-12-10 14:06:00 -05001248 }
1249
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001250 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001251 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001252 /*
1253 * "." or ".." will only be in the first block
1254 * NFS may look up ".."; "." should be handled by the VFS
1255 */
1256 block = start = 0;
1257 nblocks = 1;
1258 goto restart;
1259 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260 if (is_dx(dir)) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001261 bh = ext4_dx_find_entry(dir, d_name, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 /*
1263 * On success, or if the error was file not found,
1264 * return. Otherwise, fall back to doing a search the
1265 * old fashioned way.
1266 */
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001267 if (!IS_ERR(bh) || PTR_ERR(bh) != ERR_BAD_DX_DIR)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001269 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1270 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 }
Mingming Cao617ba132006-10-11 01:20:53 -07001272 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1273 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274 if (start >= nblocks)
1275 start = 0;
1276 block = start;
1277restart:
1278 do {
1279 /*
1280 * We deal with the read-ahead logic here.
1281 */
1282 if (ra_ptr >= ra_max) {
1283 /* Refill the readahead buffer */
1284 ra_ptr = 0;
1285 b = block;
1286 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1287 /*
1288 * Terminate if we reach the end of the
1289 * directory and must wrap, or if our
1290 * search has finished at this block.
1291 */
1292 if (b >= nblocks || (num && block == start)) {
1293 bh_use[ra_max] = NULL;
1294 break;
1295 }
1296 num++;
Theodore Ts'o10560082014-08-29 20:51:32 -04001297 bh = ext4_getblk(NULL, dir, b++, 0);
1298 if (unlikely(IS_ERR(bh))) {
Theodore Ts'o36de9282014-08-23 17:47:19 -04001299 if (ra_max == 0)
Theodore Ts'o10560082014-08-29 20:51:32 -04001300 return bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04001301 break;
1302 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303 bh_use[ra_max] = bh;
1304 if (bh)
Christoph Hellwig65299a32011-08-23 14:50:29 +02001305 ll_rw_block(READ | REQ_META | REQ_PRIO,
1306 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 }
1308 }
1309 if ((bh = bh_use[ra_ptr++]) == NULL)
1310 goto next;
1311 wait_on_buffer(bh);
1312 if (!buffer_uptodate(bh)) {
1313 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001314 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1315 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 brelse(bh);
1317 goto next;
1318 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001319 if (!buffer_verified(bh) &&
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001320 !is_dx_internal_node(dir, block,
1321 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001322 !ext4_dirent_csum_verify(dir,
1323 (struct ext4_dir_entry *)bh->b_data)) {
1324 EXT4_ERROR_INODE(dir, "checksumming directory "
1325 "block %lu", (unsigned long)block);
1326 brelse(bh);
1327 goto next;
1328 }
1329 set_buffer_verified(bh);
Theodore Ts'of702ba02008-09-22 15:21:01 -04001330 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001331 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001333 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334 ret = bh;
1335 goto cleanup_and_exit;
1336 } else {
1337 brelse(bh);
1338 if (i < 0)
1339 goto cleanup_and_exit;
1340 }
1341 next:
1342 if (++block >= nblocks)
1343 block = 0;
1344 } while (block != start);
1345
1346 /*
1347 * If the directory has grown while we were searching, then
1348 * search the last part of the directory before giving up.
1349 */
1350 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001351 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001352 if (block < nblocks) {
1353 start = 0;
1354 goto restart;
1355 }
1356
1357cleanup_and_exit:
1358 /* Clean up the read-ahead blocks */
1359 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001360 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 return ret;
1362}
1363
Theodore Ts'of702ba02008-09-22 15:21:01 -04001364static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001365 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001366{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001367 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 struct dx_hash_info hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001370 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001371 ext4_lblk_t block;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001372 int err = 0, retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001373
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001374 frame = dx_probe(d_name, dir, &hinfo, frames, &err);
1375 if (err)
1376 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001377 do {
1378 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001379 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001380 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001382
Theodore Ts'o7845c042010-10-27 21:30:08 -04001383 retval = search_dirblock(bh, dir, d_name,
1384 block << EXT4_BLOCK_SIZE_BITS(sb),
1385 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001386 if (retval == 1)
1387 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001388 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001389 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001390 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001391 goto errout;
1392 }
1393
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 /* Check to see if we should continue to search */
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001395 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396 frames, NULL);
1397 if (retval < 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001398 ext4_warning(sb,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001399 "error %d reading index page in directory #%lu",
1400 retval, dir->i_ino);
1401 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001402 goto errout;
1403 }
1404 } while (retval == 1);
1405
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001406 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001407errout:
Bernd Schubert265c6a02011-07-16 19:41:23 -04001408 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001409success:
1410 dx_release(frames);
1411 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001412}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413
Al Viro00cd8dd2012-06-10 17:13:09 -04001414static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001416 struct inode *inode;
1417 struct ext4_dir_entry_2 *de;
1418 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419
Mingming Cao617ba132006-10-11 01:20:53 -07001420 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 return ERR_PTR(-ENAMETOOLONG);
1422
Tao Ma32f7f222012-12-10 14:06:01 -05001423 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001424 if (IS_ERR(bh))
1425 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426 inode = NULL;
1427 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001428 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001429 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001430 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001431 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001432 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001433 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001434 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001435 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1436 dentry);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001437 return ERR_PTR(-EIO);
1438 }
David Howells1d1fe1e2008-02-07 00:15:37 -08001439 inode = ext4_iget(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001440 if (inode == ERR_PTR(-ESTALE)) {
1441 EXT4_ERROR_INODE(dir,
1442 "deleted inode referenced: %u",
1443 ino);
1444 return ERR_PTR(-EIO);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001445 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 }
1447 return d_splice_alias(inode, dentry);
1448}
1449
1450
Mingming Cao617ba132006-10-11 01:20:53 -07001451struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001453 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001454 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001455 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 struct buffer_head *bh;
1457
Tao Ma32f7f222012-12-10 14:06:01 -05001458 bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001459 if (IS_ERR(bh))
1460 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 if (!bh)
1462 return ERR_PTR(-ENOENT);
1463 ino = le32_to_cpu(de->inode);
1464 brelse(bh);
1465
Mingming Cao617ba132006-10-11 01:20:53 -07001466 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001467 EXT4_ERROR_INODE(child->d_inode,
1468 "bad parent inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001469 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001470 }
1471
Christoph Hellwig44003722008-08-11 15:49:04 +02001472 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473}
1474
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001475/*
1476 * Move count entries from end of map between two memory locations.
1477 * Returns pointer to last entry moved.
1478 */
Mingming Cao617ba132006-10-11 01:20:53 -07001479static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001480dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1481 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482{
1483 unsigned rec_len = 0;
1484
1485 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001486 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001487 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001488 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001490 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001491 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 de->inode = 0;
1493 map++;
1494 to += rec_len;
1495 }
Mingming Cao617ba132006-10-11 01:20:53 -07001496 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497}
1498
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001499/*
1500 * Compact each dir entry in the range to the minimal rec_len.
1501 * Returns pointer to last entry in range.
1502 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001503static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504{
Mingming Cao617ba132006-10-11 01:20:53 -07001505 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001506 unsigned rec_len = 0;
1507
1508 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001509 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001510 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001512 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 if (de > to)
1514 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001515 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001517 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 }
1519 de = next;
1520 }
1521 return prev;
1522}
1523
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001524/*
1525 * Split a full leaf block to make room for a new dir entry.
1526 * Allocate a new block, and move entries so that they are approx. equally full.
1527 * Returns pointer to de in block into which the new entry will be inserted.
1528 */
Mingming Cao617ba132006-10-11 01:20:53 -07001529static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 struct buffer_head **bh,struct dx_frame *frame,
1531 struct dx_hash_info *hinfo, int *error)
1532{
1533 unsigned blocksize = dir->i_sb->s_blocksize;
1534 unsigned count, continued;
1535 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001536 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 u32 hash2;
1538 struct dx_map_entry *map;
1539 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001540 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001541 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001542 struct ext4_dir_entry_tail *t;
1543 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001544 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001546 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1547 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1548 csum_size = sizeof(struct ext4_dir_entry_tail);
1549
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001550 bh2 = ext4_append(handle, dir, &newblock);
1551 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 brelse(*bh);
1553 *bh = NULL;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001554 *error = PTR_ERR(bh2);
1555 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 }
1557
1558 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001559 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001560 if (err)
1561 goto journal_error;
1562
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001564 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001565 if (err)
1566 goto journal_error;
1567
1568 data2 = bh2->b_data;
1569
1570 /* create map in the end of data2 block */
1571 map = (struct dx_map_entry *) (data2 + blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001572 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 blocksize, hinfo, map);
1574 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001575 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001576 /* Split the existing block in the middle, size-wise */
1577 size = 0;
1578 move = 0;
1579 for (i = count-1; i >= 0; i--) {
1580 /* is more than half of this entry in 2nd half of the block? */
1581 if (size + map[i].size/2 > blocksize/2)
1582 break;
1583 size += map[i].size;
1584 move++;
1585 }
1586 /* map index at which we will split */
1587 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 hash2 = map[split].hash;
1589 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001590 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1591 (unsigned long)dx_get_block(frame->at),
1592 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593
1594 /* Fancy dance to stay within two buffers */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001595 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001596 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001597 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1598 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001599 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001600 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1601 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001602 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001603 if (csum_size) {
1604 t = EXT4_DIRENT_TAIL(data2, blocksize);
1605 initialize_dirent_tail(t, blocksize);
1606
1607 t = EXT4_DIRENT_TAIL(data1, blocksize);
1608 initialize_dirent_tail(t, blocksize);
1609 }
1610
Mingming Cao617ba132006-10-11 01:20:53 -07001611 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1612 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613
1614 /* Which block gets the new entry? */
1615 if (hinfo->hash >= hash2)
1616 {
1617 swap(*bh, bh2);
1618 de = de2;
1619 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001620 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001621 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 if (err)
1623 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001624 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 if (err)
1626 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001627 brelse(bh2);
1628 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001630
1631journal_error:
1632 brelse(*bh);
1633 brelse(bh2);
1634 *bh = NULL;
1635 ext4_std_error(dir->i_sb, err);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001636 *error = err;
1637 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639
Tao Ma978fef92012-12-10 14:05:58 -05001640int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1641 struct buffer_head *bh,
1642 void *buf, int buf_size,
1643 const char *name, int namelen,
1644 struct ext4_dir_entry_2 **dest_de)
1645{
1646 struct ext4_dir_entry_2 *de;
1647 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1648 int nlen, rlen;
1649 unsigned int offset = 0;
1650 char *top;
1651
1652 de = (struct ext4_dir_entry_2 *)buf;
1653 top = buf + buf_size - reclen;
1654 while ((char *) de <= top) {
1655 if (ext4_check_dir_entry(dir, NULL, de, bh,
1656 buf, buf_size, offset))
1657 return -EIO;
1658 if (ext4_match(namelen, name, de))
1659 return -EEXIST;
1660 nlen = EXT4_DIR_REC_LEN(de->name_len);
1661 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1662 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1663 break;
1664 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1665 offset += rlen;
1666 }
1667 if ((char *) de > top)
1668 return -ENOSPC;
1669
1670 *dest_de = de;
1671 return 0;
1672}
1673
1674void ext4_insert_dentry(struct inode *inode,
1675 struct ext4_dir_entry_2 *de,
1676 int buf_size,
1677 const char *name, int namelen)
1678{
1679
1680 int nlen, rlen;
1681
1682 nlen = EXT4_DIR_REC_LEN(de->name_len);
1683 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1684 if (de->inode) {
1685 struct ext4_dir_entry_2 *de1 =
1686 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1687 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1688 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1689 de = de1;
1690 }
1691 de->file_type = EXT4_FT_UNKNOWN;
1692 de->inode = cpu_to_le32(inode->i_ino);
1693 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1694 de->name_len = namelen;
1695 memcpy(de->name, name, namelen);
1696}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697/*
1698 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1699 * it points to a directory entry which is guaranteed to be large
1700 * enough for new directory entry. If de is NULL, then
1701 * add_dirent_to_buf will attempt search the directory block for
1702 * space. It will return -ENOSPC if no space is available, and -EIO
1703 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704 */
1705static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001706 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001707 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708{
1709 struct inode *dir = dentry->d_parent->d_inode;
1710 const char *name = dentry->d_name.name;
1711 int namelen = dentry->d_name.len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001712 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001713 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001714 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001715
1716 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1717 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1718 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 if (!de) {
Tao Ma978fef92012-12-10 14:05:58 -05001721 err = ext4_find_dest_de(dir, inode,
1722 bh, bh->b_data, blocksize - csum_size,
1723 name, namelen, &de);
1724 if (err)
1725 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001726 }
1727 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001728 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001730 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001731 return err;
1732 }
1733
1734 /* By now the buffer is marked for journaling */
Tao Ma978fef92012-12-10 14:05:58 -05001735 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1736
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 /*
1738 * XXX shouldn't update any times until successful
1739 * completion of syscall, but too many callers depend
1740 * on this.
1741 *
1742 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001743 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 * recovery deletes the inode, so the worst that can
1745 * happen is that the times are slightly out of date
1746 * and/or different from the directory change time.
1747 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001748 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001749 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001751 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001752 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001753 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001754 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001755 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756 return 0;
1757}
1758
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759/*
1760 * This converts a one block unindexed directory to a 3 block indexed
1761 * directory, and adds the dentry to the indexed directory.
1762 */
1763static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1764 struct inode *inode, struct buffer_head *bh)
1765{
1766 struct inode *dir = dentry->d_parent->d_inode;
1767 const char *name = dentry->d_name.name;
1768 int namelen = dentry->d_name.len;
1769 struct buffer_head *bh2;
1770 struct dx_root *root;
1771 struct dx_frame frames[2], *frame;
1772 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001773 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001774 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775 char *data1, *top;
1776 unsigned len;
1777 int retval;
1778 unsigned blocksize;
1779 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001780 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781 struct fake_dirent *fde;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001782 int csum_size = 0;
1783
1784 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1785 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1786 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787
1788 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001789 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04001790 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001791 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001792 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001793 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794 brelse(bh);
1795 return retval;
1796 }
1797 root = (struct dx_root *) bh->b_data;
1798
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001799 /* The 0th block becomes the root, move the dirents out */
1800 fde = &root->dotdot;
1801 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001802 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001803 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001804 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001805 brelse(bh);
1806 return -EIO;
1807 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001808 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001809
1810 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001811 bh2 = ext4_append(handle, dir, &block);
1812 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001814 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001815 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001816 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 data1 = bh2->b_data;
1818
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001820 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001822 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001824 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1825 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001826 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001827
1828 if (csum_size) {
1829 t = EXT4_DIRENT_TAIL(data1, blocksize);
1830 initialize_dirent_tail(t, blocksize);
1831 }
1832
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001833 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001834 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001835 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1836 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837 memset (&root->info, 0, sizeof(root->info));
1838 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001839 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001841 dx_set_block(entries, 1);
1842 dx_set_count(entries, 1);
1843 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001844
1845 /* Initialize as for dx_probe */
1846 hinfo.hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001847 if (hinfo.hash_version <= DX_HASH_TEA)
1848 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001849 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1850 ext4fs_dirhash(name, namelen, &hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001851 frame = frames;
1852 frame->entries = entries;
1853 frame->at = entries;
1854 frame->bh = bh;
1855 bh = bh2;
Allison Henderson6976a6f2011-05-15 00:19:41 -04001856
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001857 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001858 ext4_handle_dirty_dirent_node(handle, dir, bh);
Allison Henderson6976a6f2011-05-15 00:19:41 -04001859
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001861 if (!de) {
1862 /*
1863 * Even if the block split failed, we have to properly write
1864 * out all the changes we did so far. Otherwise we can end up
1865 * with corrupted filesystem.
1866 */
1867 ext4_mark_inode_dirty(handle, dir);
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001868 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 return retval;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001870 }
1871 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001873 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1874 brelse(bh);
1875 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877
1878/*
Mingming Cao617ba132006-10-11 01:20:53 -07001879 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001880 *
1881 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07001882 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883 *
1884 * NOTE!! The inode part of 'de' is left at 0 - which means you
1885 * may not sleep between calling this and putting something into
1886 * the entry, as someone else might have used it while you slept.
1887 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001888static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1889 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001890{
1891 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001892 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001893 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001894 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001895 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001897 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001898 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001899 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001900 int csum_size = 0;
1901
1902 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1903 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1904 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905
1906 sb = dir->i_sb;
1907 blocksize = sb->s_blocksize;
1908 if (!dentry->d_name.len)
1909 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05001910
1911 if (ext4_has_inline_data(dir)) {
1912 retval = ext4_try_add_inline_entry(handle, dentry, inode);
1913 if (retval < 0)
1914 return retval;
1915 if (retval == 1) {
1916 retval = 0;
1917 return retval;
1918 }
1919 }
1920
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001921 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001922 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001923 if (!retval || (retval != ERR_BAD_DX_DIR))
1924 return retval;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001925 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07001927 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001929 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001930 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001931 bh = ext4_read_dirblock(dir, block, DIRENT);
1932 if (IS_ERR(bh))
1933 return PTR_ERR(bh);
1934
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001935 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001936 if (retval != -ENOSPC) {
1937 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001938 return retval;
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001939 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941 if (blocks == 1 && !dx_fallback &&
Theodore Ts'o1e424a32009-11-08 15:45:44 -05001942 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1943 return make_indexed_dir(handle, dentry, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 brelse(bh);
1945 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001946 bh = ext4_append(handle, dir, &block);
1947 if (IS_ERR(bh))
1948 return PTR_ERR(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001949 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001951 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1952
1953 if (csum_size) {
1954 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1955 initialize_dirent_tail(t, blocksize);
1956 }
1957
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001958 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1959 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04001960 if (retval == 0)
1961 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001962 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963}
1964
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965/*
1966 * Returns 0 for success, or a negative error value
1967 */
Mingming Cao617ba132006-10-11 01:20:53 -07001968static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 struct inode *inode)
1970{
1971 struct dx_frame frames[2], *frame;
1972 struct dx_entry *entries, *at;
1973 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001974 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001976 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001977 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001978 int err;
1979
Theodore Ts'of702ba02008-09-22 15:21:01 -04001980 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 if (!frame)
1982 return err;
1983 entries = frame->entries;
1984 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001985 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
1986 if (IS_ERR(bh)) {
1987 err = PTR_ERR(bh);
1988 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001990 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991
1992 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001993 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 if (err)
1995 goto journal_error;
1996
1997 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001998 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000
2001 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002002 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 dx_get_count(entries), dx_get_limit(entries)));
2004 /* Need to split index? */
2005 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002006 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 unsigned icount = dx_get_count(entries);
2008 int levels = frame - frames;
2009 struct dx_entry *entries2;
2010 struct dx_node *node2;
2011 struct buffer_head *bh2;
2012
2013 if (levels && (dx_get_count(frames->entries) ==
2014 dx_get_limit(frames->entries))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002015 ext4_warning(sb, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 err = -ENOSPC;
2017 goto cleanup;
2018 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002019 bh2 = ext4_append(handle, dir, &newblock);
2020 if (IS_ERR(bh2)) {
2021 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002023 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002024 node2 = (struct dx_node *)(bh2->b_data);
2025 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002026 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002027 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2028 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002030 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031 if (err)
2032 goto journal_error;
2033 if (levels) {
2034 unsigned icount1 = icount/2, icount2 = icount - icount1;
2035 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002036 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2037 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038
2039 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002040 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 frames[0].bh);
2042 if (err)
2043 goto journal_error;
2044
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002045 memcpy((char *) entries2, (char *) (entries + icount1),
2046 icount2 * sizeof(struct dx_entry));
2047 dx_set_count(entries, icount1);
2048 dx_set_count(entries2, icount2);
2049 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050
2051 /* Which index block gets the new entry? */
2052 if (at - entries >= icount1) {
2053 frame->at = at = at - entries - icount1 + entries2;
2054 frame->entries = entries = entries2;
2055 swap(frame->bh, bh2);
2056 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002057 dx_insert_block(frames + 0, hash2, newblock);
2058 dxtrace(dx_show_index("node", frames[1].entries));
2059 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002061 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062 if (err)
2063 goto journal_error;
2064 brelse (bh2);
2065 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002066 dxtrace(printk(KERN_DEBUG
2067 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002068 memcpy((char *) entries2, (char *) entries,
2069 icount * sizeof(struct dx_entry));
2070 dx_set_limit(entries2, dx_node_limit(dir));
2071
2072 /* Set up root */
2073 dx_set_count(entries, 1);
2074 dx_set_block(entries + 0, newblock);
2075 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2076
2077 /* Add new access path frame */
2078 frame = frames + 1;
2079 frame->at = at = at - entries + entries2;
2080 frame->entries = entries = entries2;
2081 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002082 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002083 frame->bh);
2084 if (err)
2085 goto journal_error;
2086 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002087 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002088 if (err) {
2089 ext4_std_error(inode->i_sb, err);
2090 goto cleanup;
2091 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092 }
2093 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2094 if (!de)
2095 goto cleanup;
2096 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002097 goto cleanup;
2098
2099journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002100 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002102 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002103 dx_release(frames);
2104 return err;
2105}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002106
2107/*
Tao Ma05019a92012-12-10 14:06:00 -05002108 * ext4_generic_delete_entry deletes a directory entry by merging it
2109 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 */
Tao Ma05019a92012-12-10 14:06:00 -05002111int ext4_generic_delete_entry(handle_t *handle,
2112 struct inode *dir,
2113 struct ext4_dir_entry_2 *de_del,
2114 struct buffer_head *bh,
2115 void *entry_buf,
2116 int buf_size,
2117 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002119 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002120 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002121 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002122
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002123 i = 0;
2124 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002125 de = (struct ext4_dir_entry_2 *)entry_buf;
2126 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002127 if (ext4_check_dir_entry(dir, NULL, de, bh,
2128 bh->b_data, bh->b_size, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 return -EIO;
2130 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002132 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002133 ext4_rec_len_from_disk(pde->rec_len,
2134 blocksize) +
2135 ext4_rec_len_from_disk(de->rec_len,
2136 blocksize),
2137 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138 else
2139 de->inode = 0;
2140 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 return 0;
2142 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002143 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002145 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 }
2147 return -ENOENT;
2148}
2149
Tao Ma05019a92012-12-10 14:06:00 -05002150static int ext4_delete_entry(handle_t *handle,
2151 struct inode *dir,
2152 struct ext4_dir_entry_2 *de_del,
2153 struct buffer_head *bh)
2154{
2155 int err, csum_size = 0;
2156
Tao Ma9f40fe52012-12-10 14:06:00 -05002157 if (ext4_has_inline_data(dir)) {
2158 int has_inline_data = 1;
2159 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2160 &has_inline_data);
2161 if (has_inline_data)
2162 return err;
2163 }
2164
Tao Ma05019a92012-12-10 14:06:00 -05002165 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2166 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2167 csum_size = sizeof(struct ext4_dir_entry_tail);
2168
2169 BUFFER_TRACE(bh, "get_write_access");
2170 err = ext4_journal_get_write_access(handle, bh);
2171 if (unlikely(err))
2172 goto out;
2173
2174 err = ext4_generic_delete_entry(handle, dir, de_del,
2175 bh, bh->b_data,
2176 dir->i_sb->s_blocksize, csum_size);
2177 if (err)
2178 goto out;
2179
2180 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2181 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2182 if (unlikely(err))
2183 goto out;
2184
2185 return 0;
2186out:
2187 if (err != -ENOENT)
2188 ext4_std_error(dir->i_sb, err);
2189 return err;
2190}
2191
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002192/*
2193 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2194 * since this indicates that nlinks count was previously 1.
2195 */
2196static void ext4_inc_count(handle_t *handle, struct inode *inode)
2197{
2198 inc_nlink(inode);
2199 if (is_dx(inode) && inode->i_nlink > 1) {
2200 /* limit is 16-bit i_links_count */
2201 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002202 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002203 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2204 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2205 }
2206 }
2207}
2208
2209/*
2210 * If a directory had nlink == 1, then we should let it be 1. This indicates
2211 * directory has >EXT4_LINK_MAX subdirs.
2212 */
2213static void ext4_dec_count(handle_t *handle, struct inode *inode)
2214{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002215 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2216 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002217}
2218
2219
Mingming Cao617ba132006-10-11 01:20:53 -07002220static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221 struct dentry *dentry, struct inode *inode)
2222{
Mingming Cao617ba132006-10-11 01:20:53 -07002223 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002225 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002226 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002227 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002228 return 0;
2229 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002230 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002231 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 iput(inode);
2233 return err;
2234}
2235
2236/*
2237 * By the time this is called, we already have created
2238 * the directory cache entry for the new file, but it
2239 * is so far negative - it has no inode.
2240 *
2241 * If the create succeeds, we fill in the inode information
2242 * with d_instantiate().
2243 */
Al Viro4acdaf22011-07-26 01:42:34 -04002244static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002245 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246{
2247 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002248 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002249 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002250
Christoph Hellwig871a2932010-03-03 09:05:07 -05002251 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002252
Theodore Ts'o11395752013-02-09 16:27:09 -05002253 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002254 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002256 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2257 NULL, EXT4_HT_DIR, credits);
2258 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002259 err = PTR_ERR(inode);
2260 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002261 inode->i_op = &ext4_file_inode_operations;
2262 inode->i_fop = &ext4_file_operations;
2263 ext4_set_aops(inode);
2264 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002265 if (!err && IS_DIRSYNC(dir))
2266 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002268 if (handle)
2269 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002270 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 goto retry;
2272 return err;
2273}
2274
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002275static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002276 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277{
2278 handle_t *handle;
2279 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002280 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281
2282 if (!new_valid_dev(rdev))
2283 return -EINVAL;
2284
Christoph Hellwig871a2932010-03-03 09:05:07 -05002285 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002286
Theodore Ts'o11395752013-02-09 16:27:09 -05002287 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002288 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002290 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2291 NULL, EXT4_HT_DIR, credits);
2292 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 err = PTR_ERR(inode);
2294 if (!IS_ERR(inode)) {
2295 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002296 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002297 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002298 if (!err && IS_DIRSYNC(dir))
2299 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002301 if (handle)
2302 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002303 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304 goto retry;
2305 return err;
2306}
2307
Al Viroaf51a2a2013-06-29 13:23:08 +04002308static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2309{
2310 handle_t *handle;
2311 struct inode *inode;
2312 int err, retries = 0;
2313
2314 dquot_initialize(dir);
2315
2316retry:
2317 inode = ext4_new_inode_start_handle(dir, mode,
2318 NULL, 0, NULL,
2319 EXT4_HT_DIR,
2320 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2321 4 + EXT4_XATTR_TRANS_BLOCKS);
2322 handle = ext4_journal_current_handle();
2323 err = PTR_ERR(inode);
2324 if (!IS_ERR(inode)) {
2325 inode->i_op = &ext4_file_inode_operations;
2326 inode->i_fop = &ext4_file_operations;
2327 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002328 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002329 err = ext4_orphan_add(handle, inode);
2330 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002331 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002332 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002333 unlock_new_inode(inode);
2334 }
2335 if (handle)
2336 ext4_journal_stop(handle);
2337 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2338 goto retry;
2339 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002340err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002341 ext4_journal_stop(handle);
2342 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002343 return err;
2344}
2345
Tao Maa774f9c2012-12-10 14:05:57 -05002346struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2347 struct ext4_dir_entry_2 *de,
2348 int blocksize, int csum_size,
2349 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002350{
Tao Maa774f9c2012-12-10 14:05:57 -05002351 de->inode = cpu_to_le32(inode->i_ino);
2352 de->name_len = 1;
2353 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2354 blocksize);
2355 strcpy(de->name, ".");
2356 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2357
2358 de = ext4_next_entry(de, blocksize);
2359 de->inode = cpu_to_le32(parent_ino);
2360 de->name_len = 2;
2361 if (!dotdot_real_len)
2362 de->rec_len = ext4_rec_len_to_disk(blocksize -
2363 (csum_size + EXT4_DIR_REC_LEN(1)),
2364 blocksize);
2365 else
2366 de->rec_len = ext4_rec_len_to_disk(
2367 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2368 strcpy(de->name, "..");
2369 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2370
2371 return ext4_next_entry(de, blocksize);
2372}
2373
2374static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2375 struct inode *inode)
2376{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002377 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002378 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002379 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002380 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002381 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002382 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002383 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002384
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002385 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2386 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2387 csum_size = sizeof(struct ext4_dir_entry_tail);
2388
Tao Ma3c47d542012-12-10 14:05:59 -05002389 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2390 err = ext4_try_create_inline_dir(handle, dir, inode);
2391 if (err < 0 && err != -ENOSPC)
2392 goto out;
2393 if (!err)
2394 goto out;
2395 }
2396
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002397 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002398 dir_block = ext4_append(handle, inode, &block);
2399 if (IS_ERR(dir_block))
2400 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002401 BUFFER_TRACE(dir_block, "get_write_access");
2402 err = ext4_journal_get_write_access(handle, dir_block);
2403 if (err)
2404 goto out;
2405 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2406 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2407 set_nlink(inode, 2);
2408 if (csum_size) {
2409 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2410 initialize_dirent_tail(t, blocksize);
2411 }
2412
2413 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2414 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2415 if (err)
2416 goto out;
2417 set_buffer_verified(dir_block);
2418out:
2419 brelse(dir_block);
2420 return err;
2421}
2422
2423static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2424{
2425 handle_t *handle;
2426 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002427 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002428
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002429 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002430 return -EMLINK;
2431
Christoph Hellwig871a2932010-03-03 09:05:07 -05002432 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002433
Theodore Ts'o11395752013-02-09 16:27:09 -05002434 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002435 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002436retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002437 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2438 &dentry->d_name,
2439 0, NULL, EXT4_HT_DIR, credits);
2440 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002441 err = PTR_ERR(inode);
2442 if (IS_ERR(inode))
2443 goto out_stop;
2444
Mingming Cao617ba132006-10-11 01:20:53 -07002445 inode->i_op = &ext4_dir_inode_operations;
2446 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002447 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002448 if (err)
2449 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002450 err = ext4_mark_inode_dirty(handle, inode);
2451 if (!err)
2452 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002454out_clear_inode:
2455 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002456 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002457 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002458 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002459 goto out_stop;
2460 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002461 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002462 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002463 err = ext4_mark_inode_dirty(handle, dir);
2464 if (err)
2465 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002466 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002467 d_instantiate(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002468 if (IS_DIRSYNC(dir))
2469 ext4_handle_sync(handle);
2470
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002472 if (handle)
2473 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002474 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475 goto retry;
2476 return err;
2477}
2478
2479/*
2480 * routine to check that the specified directory is empty (for rmdir)
2481 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002482static int empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002484 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002485 struct buffer_head *bh;
2486 struct ext4_dir_entry_2 *de, *de1;
2487 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 int err = 0;
2489
Tao Ma61f86632012-12-10 14:06:01 -05002490 if (ext4_has_inline_data(inode)) {
2491 int has_inline_data = 1;
2492
2493 err = empty_inline_dir(inode, &has_inline_data);
2494 if (has_inline_data)
2495 return err;
2496 }
2497
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002498 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002499 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2500 EXT4_ERROR_INODE(inode, "invalid size");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002501 return 1;
2502 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002503 bh = ext4_read_dirblock(inode, 0, EITHER);
2504 if (IS_ERR(bh))
2505 return 1;
2506
Mingming Cao617ba132006-10-11 01:20:53 -07002507 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002508 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002509 if (le32_to_cpu(de->inode) != inode->i_ino ||
2510 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002511 strcmp(".", de->name) ||
2512 strcmp("..", de1->name)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002513 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002514 "bad directory (dir #%lu) - no `.' or `..'",
2515 inode->i_ino);
2516 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002517 return 1;
2518 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002519 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2520 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2521 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002522 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002523 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002524 unsigned int lblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002525 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002526 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002527 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002528 bh = ext4_read_dirblock(inode, lblock, EITHER);
2529 if (IS_ERR(bh))
2530 return 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002531 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002532 }
Tao Ma226ba972012-12-10 14:05:58 -05002533 if (ext4_check_dir_entry(inode, NULL, de, bh,
2534 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002535 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002536 sb->s_blocksize);
2537 offset = (offset | (sb->s_blocksize - 1)) + 1;
2538 continue;
2539 }
2540 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002541 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002542 return 0;
2543 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002544 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2545 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002547 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 return 1;
2549}
2550
Jan Karad745a8c2014-05-26 11:56:53 -04002551/*
2552 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553 * such inodes, starting at the superblock, in case we crash before the
2554 * file is closed/deleted, or in case the inode truncate spans multiple
2555 * transactions and the last transaction is not recovered after a crash.
2556 *
2557 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002558 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002559 *
2560 * Orphan list manipulation functions must be called under i_mutex unless
2561 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002562 */
Mingming Cao617ba132006-10-11 01:20:53 -07002563int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002564{
2565 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002566 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002567 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002568 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002569 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002570
Jan Karacd2c0802014-05-26 11:39:17 -04002571 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05002572 return 0;
2573
Jan Karad745a8c2014-05-26 11:56:53 -04002574 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2575 !mutex_is_locked(&inode->i_mutex));
2576 /*
2577 * Exit early if inode already is on orphan list. This is a big speedup
2578 * since we don't have to contend on the global s_orphan_lock.
2579 */
Mingming Cao617ba132006-10-11 01:20:53 -07002580 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002581 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002582
Lukas Czernerafb86172011-07-11 18:47:04 -04002583 /*
2584 * Orphan handling is only valid for files with data blocks
2585 * being truncated, or files being unlinked. Note that we either
2586 * hold i_mutex, or the inode can not be referenced from outside,
2587 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002589 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2590 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002591
Jan Karacd2c0802014-05-26 11:39:17 -04002592 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2593 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002595 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002596
Mingming Cao617ba132006-10-11 01:20:53 -07002597 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002599 goto out;
2600
2601 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002602 /*
2603 * Due to previous errors inode may be already a part of on-disk
2604 * orphan list. If so skip on-disk list modification.
2605 */
Jan Karad745a8c2014-05-26 11:56:53 -04002606 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2607 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2608 /* Insert this inode at the head of the on-disk orphan list */
2609 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2610 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2611 dirty = true;
2612 }
2613 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2614 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002615
Jan Karad745a8c2014-05-26 11:56:53 -04002616 if (dirty) {
2617 err = ext4_handle_dirty_super(handle, sb);
2618 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2619 if (!err)
2620 err = rc;
2621 if (err) {
2622 /*
2623 * We have to remove inode from in-memory list if
2624 * addition to on disk orphan list failed. Stray orphan
2625 * list entries can cause panics at unmount time.
2626 */
2627 mutex_lock(&sbi->s_orphan_lock);
2628 list_del(&EXT4_I(inode)->i_orphan);
2629 mutex_unlock(&sbi->s_orphan_lock);
2630 }
2631 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002632 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2633 jbd_debug(4, "orphan inode %lu will point to %d\n",
2634 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002635out:
Jan Karacd2c0802014-05-26 11:39:17 -04002636 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002637 return err;
2638}
2639
2640/*
Mingming Cao617ba132006-10-11 01:20:53 -07002641 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 * of such inodes stored on disk, because it is finally being cleaned up.
2643 */
Mingming Cao617ba132006-10-11 01:20:53 -07002644int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002645{
2646 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002647 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002648 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002649 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002650 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002651 int err = 0;
2652
Jan Karacd2c0802014-05-26 11:39:17 -04002653 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002654 return 0;
2655
Jan Karad745a8c2014-05-26 11:56:53 -04002656 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2657 !mutex_is_locked(&inode->i_mutex));
2658 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002659 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002660 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661
Jan Karad745a8c2014-05-26 11:56:53 -04002662 if (handle) {
2663 /* Grab inode buffer early before taking global s_orphan_lock */
2664 err = ext4_reserve_inode_write(handle, inode, &iloc);
2665 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002666
Jan Karad745a8c2014-05-26 11:56:53 -04002667 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002668 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2669
Jan Karad745a8c2014-05-26 11:56:53 -04002670 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002671 list_del_init(&ei->i_orphan);
2672
2673 /* If we're on an error path, we may not have a valid
2674 * transaction handle with which to update the orphan list on
2675 * disk, but we still need to remove the inode from the linked
2676 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002677 if (!handle || err) {
2678 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002680 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681
Jan Karad745a8c2014-05-26 11:56:53 -04002682 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002683 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002684 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002686 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04002687 if (err) {
2688 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002690 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002691 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04002692 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002693 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002694 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002695 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002696 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002697 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002698
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002699 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002700 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002701 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002702 if (err) {
2703 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002704 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002705 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002707 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002708 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002709 }
2710 if (err)
2711 goto out_brelse;
2712 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002713 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002715 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002716 return err;
2717
2718out_brelse:
2719 brelse(iloc.bh);
2720 goto out_err;
2721}
2722
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002723static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002724{
2725 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002726 struct inode *inode;
2727 struct buffer_head *bh;
2728 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002729 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002730
2731 /* Initialize quotas before so that eventual writes go in
2732 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002733 dquot_initialize(dir);
2734 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002735
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002736 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002737 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002738 if (IS_ERR(bh))
2739 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740 if (!bh)
2741 goto end_rmdir;
2742
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743 inode = dentry->d_inode;
2744
2745 retval = -EIO;
2746 if (le32_to_cpu(de->inode) != inode->i_ino)
2747 goto end_rmdir;
2748
2749 retval = -ENOTEMPTY;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002750 if (!empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751 goto end_rmdir;
2752
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002753 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002754 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002755 if (IS_ERR(handle)) {
2756 retval = PTR_ERR(handle);
2757 handle = NULL;
2758 goto end_rmdir;
2759 }
2760
2761 if (IS_DIRSYNC(dir))
2762 ext4_handle_sync(handle);
2763
Mingming Cao617ba132006-10-11 01:20:53 -07002764 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765 if (retval)
2766 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002767 if (!EXT4_DIR_LINK_EMPTY(inode))
Eric Sandeen12062dd2010-02-15 14:19:27 -05002768 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002769 "empty directory has too many links (%d)",
2770 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002771 inode->i_version++;
2772 clear_nlink(inode);
2773 /* There's no need to set i_disksize: the fact that i_nlink is
2774 * zero will ensure that the right thing happens during any
2775 * recovery. */
2776 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002777 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002778 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002779 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002780 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002781 ext4_update_dx_flag(dir);
2782 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783
2784end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002785 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002786 if (handle)
2787 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 return retval;
2789}
2790
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002791static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002792{
2793 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002794 struct inode *inode;
2795 struct buffer_head *bh;
2796 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05002797 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002798
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002799 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002800 /* Initialize quotas before so that eventual writes go
2801 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002802 dquot_initialize(dir);
2803 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002804
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002805 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002806 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002807 if (IS_ERR(bh))
2808 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809 if (!bh)
2810 goto end_unlink;
2811
2812 inode = dentry->d_inode;
2813
2814 retval = -EIO;
2815 if (le32_to_cpu(de->inode) != inode->i_ino)
2816 goto end_unlink;
2817
Theodore Ts'o931b6862013-02-09 09:43:39 -05002818 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002819 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05002820 if (IS_ERR(handle)) {
2821 retval = PTR_ERR(handle);
2822 handle = NULL;
2823 goto end_unlink;
2824 }
2825
2826 if (IS_DIRSYNC(dir))
2827 ext4_handle_sync(handle);
2828
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002829 if (!inode->i_nlink) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002830 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002831 "Deleting nonexistent file (%lu), %d",
2832 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002833 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002834 }
Mingming Cao617ba132006-10-11 01:20:53 -07002835 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002836 if (retval)
2837 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04002838 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002839 ext4_update_dx_flag(dir);
2840 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002841 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002842 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002843 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002844 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002845 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002846 retval = 0;
2847
2848end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002849 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05002850 if (handle)
2851 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002852 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002853 return retval;
2854}
2855
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002856static int ext4_symlink(struct inode *dir,
2857 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858{
2859 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002860 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002861 int l, err, retries = 0;
Jan Karadf5e6222011-05-03 11:12:58 -04002862 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002863
2864 l = strlen(symname)+1;
2865 if (l > dir->i_sb->s_blocksize)
2866 return -ENAMETOOLONG;
2867
Christoph Hellwig871a2932010-03-03 09:05:07 -05002868 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002869
Jan Karadf5e6222011-05-03 11:12:58 -04002870 if (l > EXT4_N_BLOCKS * 4) {
2871 /*
2872 * For non-fast symlinks, we just allocate inode and put it on
2873 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05002874 * group descriptor, sb, inode block, quota blocks, and
2875 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04002876 */
Eric Sandeen8c208712011-08-11 09:54:31 -05002877 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2878 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04002879 } else {
2880 /*
2881 * Fast symlink. We have to add entry to directory
2882 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2883 * allocate new inode (bitmap, group descriptor, inode block,
2884 * quota blocks, sb is already counted in previous macros).
2885 */
2886 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002887 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04002888 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002890 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
2891 &dentry->d_name, 0, NULL,
2892 EXT4_HT_DIR, credits);
2893 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 err = PTR_ERR(inode);
2895 if (IS_ERR(inode))
2896 goto out_stop;
2897
Jan Karadf5e6222011-05-03 11:12:58 -04002898 if (l > EXT4_N_BLOCKS * 4) {
Mingming Cao617ba132006-10-11 01:20:53 -07002899 inode->i_op = &ext4_symlink_inode_operations;
2900 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002901 /*
Jan Karadf5e6222011-05-03 11:12:58 -04002902 * We cannot call page_symlink() with transaction started
2903 * because it calls into ext4_write_begin() which can wait
2904 * for transaction commit if we are running out of space
2905 * and thus we deadlock. So we have to stop transaction now
2906 * and restart it when symlink contents is written.
2907 *
2908 * To keep fs consistent in case of crash, we have to put inode
2909 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002910 */
Jan Karadf5e6222011-05-03 11:12:58 -04002911 drop_nlink(inode);
2912 err = ext4_orphan_add(handle, inode);
2913 ext4_journal_stop(handle);
2914 if (err)
2915 goto err_drop_inode;
Nick Piggin54566b22009-01-04 12:00:53 -08002916 err = __page_symlink(inode, symname, l, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04002917 if (err)
2918 goto err_drop_inode;
2919 /*
2920 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2921 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2922 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05002923 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04002924 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2925 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2926 if (IS_ERR(handle)) {
2927 err = PTR_ERR(handle);
2928 goto err_drop_inode;
2929 }
Al Viro0ce8c0102012-01-08 19:50:23 -05002930 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04002931 err = ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002932 if (err) {
Jan Karadf5e6222011-05-03 11:12:58 -04002933 ext4_journal_stop(handle);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002934 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04002935 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002936 }
2937 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04002938 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002939 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Mingming Cao617ba132006-10-11 01:20:53 -07002940 inode->i_op = &ext4_fast_symlink_inode_operations;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002941 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002942 inode->i_size = l-1;
2943 }
Mingming Cao617ba132006-10-11 01:20:53 -07002944 EXT4_I(inode)->i_disksize = inode->i_size;
2945 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002946 if (!err && IS_DIRSYNC(dir))
2947 ext4_handle_sync(handle);
2948
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002950 if (handle)
2951 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002952 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002953 goto retry;
2954 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04002955err_drop_inode:
2956 unlock_new_inode(inode);
2957 iput(inode);
2958 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959}
2960
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002961static int ext4_link(struct dentry *old_dentry,
2962 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002963{
2964 handle_t *handle;
2965 struct inode *inode = old_dentry->d_inode;
2966 int err, retries = 0;
2967
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04002968 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 return -EMLINK;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002970
Christoph Hellwig871a2932010-03-03 09:05:07 -05002971 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002972
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002973retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05002974 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2975 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04002976 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002977 if (IS_ERR(handle))
2978 return PTR_ERR(handle);
2979
2980 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002981 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002982
Kalpak Shahef7f3832007-07-18 09:15:20 -04002983 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002984 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002985 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986
Al Viro6b38e842008-12-30 02:03:31 -05002987 err = ext4_add_entry(handle, dentry, inode);
2988 if (!err) {
2989 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002990 /* this can happen only for tmpfile being
2991 * linked the first time
2992 */
2993 if (inode->i_nlink == 1)
2994 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002995 d_instantiate(dentry, inode);
2996 } else {
2997 drop_nlink(inode);
2998 iput(inode);
2999 }
Mingming Cao617ba132006-10-11 01:20:53 -07003000 ext4_journal_stop(handle);
3001 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002 goto retry;
3003 return err;
3004}
3005
Tao Ma32f7f222012-12-10 14:06:01 -05003006
3007/*
3008 * Try to find buffer head where contains the parent block.
3009 * It should be the inode block if it is inlined or the 1st block
3010 * if it is a normal dir.
3011 */
3012static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3013 struct inode *inode,
3014 int *retval,
3015 struct ext4_dir_entry_2 **parent_de,
3016 int *inlined)
3017{
3018 struct buffer_head *bh;
3019
3020 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003021 bh = ext4_read_dirblock(inode, 0, EITHER);
3022 if (IS_ERR(bh)) {
3023 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003024 return NULL;
3025 }
3026 *parent_de = ext4_next_entry(
3027 (struct ext4_dir_entry_2 *)bh->b_data,
3028 inode->i_sb->s_blocksize);
3029 return bh;
3030 }
3031
3032 *inlined = 1;
3033 return ext4_get_first_inline_block(inode, parent_de, retval);
3034}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003036struct ext4_renament {
3037 struct inode *dir;
3038 struct dentry *dentry;
3039 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003040 bool is_dir;
3041 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003042
3043 /* entry for "dentry" */
3044 struct buffer_head *bh;
3045 struct ext4_dir_entry_2 *de;
3046 int inlined;
3047
3048 /* entry for ".." in inode if it's a directory */
3049 struct buffer_head *dir_bh;
3050 struct ext4_dir_entry_2 *parent_de;
3051 int dir_inlined;
3052};
3053
Miklos Szeredibd1af142014-04-01 17:08:44 +02003054static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3055{
3056 int retval;
3057
3058 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3059 &retval, &ent->parent_de,
3060 &ent->dir_inlined);
3061 if (!ent->dir_bh)
3062 return retval;
3063 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3064 return -EIO;
3065 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3066 return ext4_journal_get_write_access(handle, ent->dir_bh);
3067}
3068
3069static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3070 unsigned dir_ino)
3071{
3072 int retval;
3073
3074 ent->parent_de->inode = cpu_to_le32(dir_ino);
3075 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3076 if (!ent->dir_inlined) {
3077 if (is_dx(ent->inode)) {
3078 retval = ext4_handle_dirty_dx_node(handle,
3079 ent->inode,
3080 ent->dir_bh);
3081 } else {
3082 retval = ext4_handle_dirty_dirent_node(handle,
3083 ent->inode,
3084 ent->dir_bh);
3085 }
3086 } else {
3087 retval = ext4_mark_inode_dirty(handle, ent->inode);
3088 }
3089 if (retval) {
3090 ext4_std_error(ent->dir->i_sb, retval);
3091 return retval;
3092 }
3093 return 0;
3094}
3095
3096static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3097 unsigned ino, unsigned file_type)
3098{
3099 int retval;
3100
3101 BUFFER_TRACE(ent->bh, "get write access");
3102 retval = ext4_journal_get_write_access(handle, ent->bh);
3103 if (retval)
3104 return retval;
3105 ent->de->inode = cpu_to_le32(ino);
3106 if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
3107 EXT4_FEATURE_INCOMPAT_FILETYPE))
3108 ent->de->file_type = file_type;
3109 ent->dir->i_version++;
3110 ent->dir->i_ctime = ent->dir->i_mtime =
3111 ext4_current_time(ent->dir);
3112 ext4_mark_inode_dirty(handle, ent->dir);
3113 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3114 if (!ent->inlined) {
3115 retval = ext4_handle_dirty_dirent_node(handle,
3116 ent->dir, ent->bh);
3117 if (unlikely(retval)) {
3118 ext4_std_error(ent->dir->i_sb, retval);
3119 return retval;
3120 }
3121 }
3122 brelse(ent->bh);
3123 ent->bh = NULL;
3124
3125 return 0;
3126}
3127
3128static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3129 const struct qstr *d_name)
3130{
3131 int retval = -ENOENT;
3132 struct buffer_head *bh;
3133 struct ext4_dir_entry_2 *de;
3134
3135 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003136 if (IS_ERR(bh))
3137 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003138 if (bh) {
3139 retval = ext4_delete_entry(handle, dir, de, bh);
3140 brelse(bh);
3141 }
3142 return retval;
3143}
3144
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003145static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3146 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003147{
3148 int retval;
3149 /*
3150 * ent->de could have moved from under us during htree split, so make
3151 * sure that we are deleting the right entry. We might also be pointing
3152 * to a stale entry in the unused part of ent->bh so just checking inum
3153 * and the name isn't enough.
3154 */
3155 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3156 ent->de->name_len != ent->dentry->d_name.len ||
3157 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003158 ent->de->name_len) ||
3159 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003160 retval = ext4_find_delete_entry(handle, ent->dir,
3161 &ent->dentry->d_name);
3162 } else {
3163 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3164 if (retval == -ENOENT) {
3165 retval = ext4_find_delete_entry(handle, ent->dir,
3166 &ent->dentry->d_name);
3167 }
3168 }
3169
3170 if (retval) {
3171 ext4_warning(ent->dir->i_sb,
3172 "Deleting old file (%lu), %d, error=%d",
3173 ent->dir->i_ino, ent->dir->i_nlink, retval);
3174 }
3175}
3176
Miklos Szeredibd429982014-04-01 17:08:44 +02003177static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3178{
3179 if (ent->dir_nlink_delta) {
3180 if (ent->dir_nlink_delta == -1)
3181 ext4_dec_count(handle, ent->dir);
3182 else
3183 ext4_inc_count(handle, ent->dir);
3184 ext4_mark_inode_dirty(handle, ent->dir);
3185 }
3186}
3187
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003188/*
3189 * Anybody can rename anything with this: the permission checks are left to the
3190 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003191 *
3192 * n.b. old_{dentry,inode) refers to the source dentry/inode
3193 * while new_{dentry,inode) refers to the destination dentry/inode
3194 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003196static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3197 struct inode *new_dir, struct dentry *new_dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003198{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003199 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003200 struct ext4_renament old = {
3201 .dir = old_dir,
3202 .dentry = old_dentry,
3203 .inode = old_dentry->d_inode,
3204 };
3205 struct ext4_renament new = {
3206 .dir = new_dir,
3207 .dentry = new_dentry,
3208 .inode = new_dentry->d_inode,
3209 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003210 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003211 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003213 dquot_initialize(old.dir);
3214 dquot_initialize(new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003215
3216 /* Initialize quotas before so that eventual writes go
3217 * in separate transaction */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003218 if (new.inode)
3219 dquot_initialize(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003221 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003222 if (IS_ERR(old.bh))
3223 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224 /*
3225 * Check for inode number is _not_ due to possible IO errors.
3226 * We might rmdir the source, keep it as pwd of some process
3227 * and merrily kill the link to whatever was created under the
3228 * same name. Goodbye sticky bit ;-<
3229 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003230 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003231 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232 goto end_rename;
3233
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003234 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3235 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003236 if (IS_ERR(new.bh)) {
3237 retval = PTR_ERR(new.bh);
3238 goto end_rename;
3239 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003240 if (new.bh) {
3241 if (!new.inode) {
3242 brelse(new.bh);
3243 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003244 }
3245 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003246 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3247 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003248
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003249 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3250 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003251 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3252 if (IS_ERR(handle))
3253 return PTR_ERR(handle);
3254
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003255 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003256 ext4_handle_sync(handle);
3257
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003258 if (S_ISDIR(old.inode->i_mode)) {
3259 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260 retval = -ENOTEMPTY;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003261 if (!empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003262 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003263 } else {
3264 retval = -EMLINK;
3265 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3266 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003268 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003269 if (retval)
3270 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003272 /*
3273 * If we're renaming a file within an inline_data dir and adding or
3274 * setting the new dirent causes a conversion from inline_data to
3275 * extents/blockmap, we need to force the dirent delete code to
3276 * re-read the directory, or else we end up trying to delete a dirent
3277 * from what is now the extent tree root (or a block map).
3278 */
3279 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3280 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003281 if (!new.bh) {
3282 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003283 if (retval)
3284 goto end_rename;
3285 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003286 retval = ext4_setent(handle, &new,
3287 old.inode->i_ino, old.de->file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003288 if (retval)
3289 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003291 if (force_reread)
3292 force_reread = !ext4_test_inode_flag(new.dir,
3293 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294
3295 /*
3296 * Like most other Unix systems, set the ctime for inodes on a
3297 * rename.
3298 */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003299 old.inode->i_ctime = ext4_current_time(old.inode);
3300 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301
3302 /*
3303 * ok, that's it
3304 */
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003305 ext4_rename_delete(handle, &old, force_reread);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003307 if (new.inode) {
3308 ext4_dec_count(handle, new.inode);
3309 new.inode->i_ctime = ext4_current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003310 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003311 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3312 ext4_update_dx_flag(old.dir);
3313 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003314 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3315 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003316 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003317
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003318 ext4_dec_count(handle, old.dir);
3319 if (new.inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003320 /* checked empty_dir above, can't have another parent,
Theodore Ts'o825f1482008-02-15 15:00:38 -05003321 * ext4_dec_count() won't work for many-linked dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003322 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003323 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003324 ext4_inc_count(handle, new.dir);
3325 ext4_update_dx_flag(new.dir);
3326 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003327 }
3328 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003329 ext4_mark_inode_dirty(handle, old.dir);
3330 if (new.inode) {
3331 ext4_mark_inode_dirty(handle, new.inode);
3332 if (!new.inode->i_nlink)
3333 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334 }
3335 retval = 0;
3336
3337end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003338 brelse(old.dir_bh);
3339 brelse(old.bh);
3340 brelse(new.bh);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003341 if (handle)
3342 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 return retval;
3344}
3345
Miklos Szeredibd429982014-04-01 17:08:44 +02003346static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3347 struct inode *new_dir, struct dentry *new_dentry)
3348{
3349 handle_t *handle = NULL;
3350 struct ext4_renament old = {
3351 .dir = old_dir,
3352 .dentry = old_dentry,
3353 .inode = old_dentry->d_inode,
3354 };
3355 struct ext4_renament new = {
3356 .dir = new_dir,
3357 .dentry = new_dentry,
3358 .inode = new_dentry->d_inode,
3359 };
3360 u8 new_file_type;
3361 int retval;
3362
3363 dquot_initialize(old.dir);
3364 dquot_initialize(new.dir);
3365
3366 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3367 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003368 if (IS_ERR(old.bh))
3369 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003370 /*
3371 * Check for inode number is _not_ due to possible IO errors.
3372 * We might rmdir the source, keep it as pwd of some process
3373 * and merrily kill the link to whatever was created under the
3374 * same name. Goodbye sticky bit ;-<
3375 */
3376 retval = -ENOENT;
3377 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3378 goto end_rename;
3379
3380 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3381 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003382 if (IS_ERR(new.bh)) {
3383 retval = PTR_ERR(new.bh);
3384 goto end_rename;
3385 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003386
3387 /* RENAME_EXCHANGE case: old *and* new must both exist */
3388 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3389 goto end_rename;
3390
3391 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3392 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3393 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3394 if (IS_ERR(handle))
3395 return PTR_ERR(handle);
3396
3397 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3398 ext4_handle_sync(handle);
3399
3400 if (S_ISDIR(old.inode->i_mode)) {
3401 old.is_dir = true;
3402 retval = ext4_rename_dir_prepare(handle, &old);
3403 if (retval)
3404 goto end_rename;
3405 }
3406 if (S_ISDIR(new.inode->i_mode)) {
3407 new.is_dir = true;
3408 retval = ext4_rename_dir_prepare(handle, &new);
3409 if (retval)
3410 goto end_rename;
3411 }
3412
3413 /*
3414 * Other than the special case of overwriting a directory, parents'
3415 * nlink only needs to be modified if this is a cross directory rename.
3416 */
3417 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3418 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3419 new.dir_nlink_delta = -old.dir_nlink_delta;
3420 retval = -EMLINK;
3421 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3422 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3423 goto end_rename;
3424 }
3425
3426 new_file_type = new.de->file_type;
3427 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3428 if (retval)
3429 goto end_rename;
3430
3431 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3432 if (retval)
3433 goto end_rename;
3434
3435 /*
3436 * Like most other Unix systems, set the ctime for inodes on a
3437 * rename.
3438 */
3439 old.inode->i_ctime = ext4_current_time(old.inode);
3440 new.inode->i_ctime = ext4_current_time(new.inode);
3441 ext4_mark_inode_dirty(handle, old.inode);
3442 ext4_mark_inode_dirty(handle, new.inode);
3443
3444 if (old.dir_bh) {
3445 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3446 if (retval)
3447 goto end_rename;
3448 }
3449 if (new.dir_bh) {
3450 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3451 if (retval)
3452 goto end_rename;
3453 }
3454 ext4_update_dir_count(handle, &old);
3455 ext4_update_dir_count(handle, &new);
3456 retval = 0;
3457
3458end_rename:
3459 brelse(old.dir_bh);
3460 brelse(new.dir_bh);
3461 brelse(old.bh);
3462 brelse(new.bh);
3463 if (handle)
3464 ext4_journal_stop(handle);
3465 return retval;
3466}
3467
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003468static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3469 struct inode *new_dir, struct dentry *new_dentry,
3470 unsigned int flags)
3471{
Miklos Szeredibd429982014-04-01 17:08:44 +02003472 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003473 return -EINVAL;
3474
Miklos Szeredibd429982014-04-01 17:08:44 +02003475 if (flags & RENAME_EXCHANGE) {
3476 return ext4_cross_rename(old_dir, old_dentry,
3477 new_dir, new_dentry);
3478 }
3479 /*
3480 * Existence checking was done by the VFS, otherwise "RENAME_NOREPLACE"
3481 * is equivalent to regular rename.
3482 */
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003483 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry);
3484}
3485
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003486/*
3487 * directories can handle most operations...
3488 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003489const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003490 .create = ext4_create,
3491 .lookup = ext4_lookup,
3492 .link = ext4_link,
3493 .unlink = ext4_unlink,
3494 .symlink = ext4_symlink,
3495 .mkdir = ext4_mkdir,
3496 .rmdir = ext4_rmdir,
3497 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003498 .tmpfile = ext4_tmpfile,
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003499 .rename2 = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003500 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003501 .setxattr = generic_setxattr,
3502 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003503 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003505 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003506 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003507 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003508};
3509
Arjan van de Ven754661f2007-02-12 00:55:38 -08003510const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003511 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003512 .setxattr = generic_setxattr,
3513 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003514 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003515 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003516 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003517 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003518};