blob: 7223b0b4bc38cd0a67a8b53eadcd616396d5dce5 [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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/fcntl.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/quotaops.h>
34#include <linux/buffer_head.h>
35#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040036#include "ext4.h"
37#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039#include "xattr.h"
40#include "acl.h"
41
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040042#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043/*
44 * define how far ahead to read directories while searching them.
45 */
46#define NAMEI_RA_CHUNKS 2
47#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040048#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Mingming Cao617ba132006-10-11 01:20:53 -070050static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050052 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053{
54 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040055 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056
Theodore Ts'odf981d02012-08-17 09:48:17 -040057 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
58 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050059 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
60 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040061
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
63
Theodore Ts'o1c215022014-08-29 20:52:15 -040064 bh = ext4_bread(handle, inode, *block, 1);
65 if (IS_ERR(bh))
66 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050067 inode->i_size += inode->i_sb->s_blocksize;
68 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040069 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050070 err = ext4_journal_get_write_access(handle, bh);
71 if (err) {
72 brelse(bh);
73 ext4_std_error(inode->i_sb, err);
74 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040075 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 return bh;
77}
78
Theodore Ts'odc6982f2013-02-14 23:59:26 -050079static int ext4_dx_csum_verify(struct inode *inode,
80 struct ext4_dir_entry *dirent);
81
82typedef enum {
83 EITHER, INDEX, DIRENT
84} dirblock_type_t;
85
86#define ext4_read_dirblock(inode, block, type) \
87 __ext4_read_dirblock((inode), (block), (type), __LINE__)
88
89static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
90 ext4_lblk_t block,
91 dirblock_type_t type,
92 unsigned int line)
93{
94 struct buffer_head *bh;
95 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -040096 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -050097
Theodore Ts'o1c215022014-08-29 20:52:15 -040098 bh = ext4_bread(NULL, inode, block, 0);
99 if (IS_ERR(bh)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500100 __ext4_warning(inode->i_sb, __func__, line,
Theodore Ts'o1c215022014-08-29 20:52:15 -0400101 "error %ld reading directory block "
102 "(ino %lu, block %lu)", PTR_ERR(bh), inode->i_ino,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500103 (unsigned long) block);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400104
105 return bh;
106 }
107 if (!bh) {
108 ext4_error_inode(inode, __func__, line, block, "Directory hole found");
109 return ERR_PTR(-EIO);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500110 }
111 dirent = (struct ext4_dir_entry *) bh->b_data;
112 /* Determine whether or not we have an index block */
113 if (is_dx(inode)) {
114 if (block == 0)
115 is_dx_block = 1;
116 else if (ext4_rec_len_from_disk(dirent->rec_len,
117 inode->i_sb->s_blocksize) ==
118 inode->i_sb->s_blocksize)
119 is_dx_block = 1;
120 }
121 if (!is_dx_block && type == INDEX) {
122 ext4_error_inode(inode, __func__, line, block,
123 "directory leaf block found instead of index block");
124 return ERR_PTR(-EIO);
125 }
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400126 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500127 buffer_verified(bh))
128 return bh;
129
130 /*
131 * An empty leaf block can get mistaken for a index block; for
132 * this reason, we can only check the index checksum when the
133 * caller is sure it should be an index block.
134 */
135 if (is_dx_block && type == INDEX) {
136 if (ext4_dx_csum_verify(inode, dirent))
137 set_buffer_verified(bh);
138 else {
139 ext4_error_inode(inode, __func__, line, block,
140 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 brelse(bh);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500142 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143 }
144 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500145 if (!is_dx_block) {
146 if (ext4_dirent_csum_verify(inode, dirent))
147 set_buffer_verified(bh);
148 else {
149 ext4_error_inode(inode, __func__, line, block,
150 "Directory block failed checksum");
151 brelse(bh);
152 return ERR_PTR(-EIO);
153 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154 }
155 return bh;
156}
157
158#ifndef assert
159#define assert(test) J_ASSERT(test)
160#endif
161
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162#ifdef DX_DEBUG
163#define dxtrace(command) command
164#else
165#define dxtrace(command)
166#endif
167
168struct fake_dirent
169{
170 __le32 inode;
171 __le16 rec_len;
172 u8 name_len;
173 u8 file_type;
174};
175
176struct dx_countlimit
177{
178 __le16 limit;
179 __le16 count;
180};
181
182struct dx_entry
183{
184 __le32 hash;
185 __le32 block;
186};
187
188/*
189 * dx_root_info is laid out so that if it should somehow get overlaid by a
190 * dirent the two low bits of the hash version will be zero. Therefore, the
191 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
192 */
193
194struct dx_root
195{
196 struct fake_dirent dot;
197 char dot_name[4];
198 struct fake_dirent dotdot;
199 char dotdot_name[4];
200 struct dx_root_info
201 {
202 __le32 reserved_zero;
203 u8 hash_version;
204 u8 info_length; /* 8 */
205 u8 indirect_levels;
206 u8 unused_flags;
207 }
208 info;
209 struct dx_entry entries[0];
210};
211
212struct dx_node
213{
214 struct fake_dirent fake;
215 struct dx_entry entries[0];
216};
217
218
219struct dx_frame
220{
221 struct buffer_head *bh;
222 struct dx_entry *entries;
223 struct dx_entry *at;
224};
225
226struct dx_map_entry
227{
228 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700229 u16 offs;
230 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231};
232
Darrick J. Wonge6153912012-04-29 18:23:10 -0400233/*
234 * This goes at the end of each htree block.
235 */
236struct dx_tail {
237 u32 dt_reserved;
238 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
239};
240
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500241static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
242static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400243static inline unsigned dx_get_hash(struct dx_entry *entry);
244static void dx_set_hash(struct dx_entry *entry, unsigned value);
245static unsigned dx_get_count(struct dx_entry *entries);
246static unsigned dx_get_limit(struct dx_entry *entries);
247static void dx_set_count(struct dx_entry *entries, unsigned value);
248static void dx_set_limit(struct dx_entry *entries, unsigned value);
249static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
250static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400251static struct dx_frame *dx_probe(const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 struct inode *dir,
253 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400254 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400255static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400256static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
257 unsigned blocksize, struct dx_hash_info *hinfo,
258 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400260static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500261 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500262static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500263static void dx_insert_block(struct dx_frame *frame,
264 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700265static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 struct dx_frame *frame,
267 struct dx_frame *frames,
268 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400269static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
270 const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400271 struct ext4_dir_entry_2 **res_dir);
Mingming Cao617ba132006-10-11 01:20:53 -0700272static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 struct inode *inode);
274
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400275/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500276void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
277 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400278{
279 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
280 t->det_rec_len = ext4_rec_len_to_disk(
281 sizeof(struct ext4_dir_entry_tail), blocksize);
282 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
283}
284
285/* Walk through a dirent block to find a checksum "dirent" at the tail */
286static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
287 struct ext4_dir_entry *de)
288{
289 struct ext4_dir_entry_tail *t;
290
291#ifdef PARANOID
292 struct ext4_dir_entry *d, *top;
293
294 d = de;
295 top = (struct ext4_dir_entry *)(((void *)de) +
296 (EXT4_BLOCK_SIZE(inode->i_sb) -
297 sizeof(struct ext4_dir_entry_tail)));
298 while (d < top && d->rec_len)
299 d = (struct ext4_dir_entry *)(((void *)d) +
300 le16_to_cpu(d->rec_len));
301
302 if (d != top)
303 return NULL;
304
305 t = (struct ext4_dir_entry_tail *)d;
306#else
307 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
308#endif
309
310 if (t->det_reserved_zero1 ||
311 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
312 t->det_reserved_zero2 ||
313 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
314 return NULL;
315
316 return t;
317}
318
319static __le32 ext4_dirent_csum(struct inode *inode,
320 struct ext4_dir_entry *dirent, int size)
321{
322 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
323 struct ext4_inode_info *ei = EXT4_I(inode);
324 __u32 csum;
325
326 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
327 return cpu_to_le32(csum);
328}
329
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500330static void warn_no_space_for_csum(struct inode *inode)
331{
332 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
333 "checksum. Please run e2fsck -D.", inode->i_ino);
334}
335
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400336int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
337{
338 struct ext4_dir_entry_tail *t;
339
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400340 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400341 return 1;
342
343 t = get_dirent_tail(inode, dirent);
344 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500345 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400346 return 0;
347 }
348
349 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
350 (void *)t - (void *)dirent))
351 return 0;
352
353 return 1;
354}
355
356static void ext4_dirent_csum_set(struct inode *inode,
357 struct ext4_dir_entry *dirent)
358{
359 struct ext4_dir_entry_tail *t;
360
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400361 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400362 return;
363
364 t = get_dirent_tail(inode, dirent);
365 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500366 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400367 return;
368 }
369
370 t->det_checksum = ext4_dirent_csum(inode, dirent,
371 (void *)t - (void *)dirent);
372}
373
Tao Ma3c47d542012-12-10 14:05:59 -0500374int ext4_handle_dirty_dirent_node(handle_t *handle,
375 struct inode *inode,
376 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400377{
378 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
379 return ext4_handle_dirty_metadata(handle, inode, bh);
380}
381
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400382static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
383 struct ext4_dir_entry *dirent,
384 int *offset)
385{
386 struct ext4_dir_entry *dp;
387 struct dx_root_info *root;
388 int count_offset;
389
390 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
391 count_offset = 8;
392 else if (le16_to_cpu(dirent->rec_len) == 12) {
393 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
394 if (le16_to_cpu(dp->rec_len) !=
395 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
396 return NULL;
397 root = (struct dx_root_info *)(((void *)dp + 12));
398 if (root->reserved_zero ||
399 root->info_length != sizeof(struct dx_root_info))
400 return NULL;
401 count_offset = 32;
402 } else
403 return NULL;
404
405 if (offset)
406 *offset = count_offset;
407 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
408}
409
410static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
411 int count_offset, int count, struct dx_tail *t)
412{
413 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
414 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400415 __u32 csum;
416 __le32 save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400417 int size;
418
419 size = count_offset + (count * sizeof(struct dx_entry));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400420 save_csum = t->dt_checksum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400421 t->dt_checksum = 0;
422 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
423 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400424 t->dt_checksum = save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400425
426 return cpu_to_le32(csum);
427}
428
429static int ext4_dx_csum_verify(struct inode *inode,
430 struct ext4_dir_entry *dirent)
431{
432 struct dx_countlimit *c;
433 struct dx_tail *t;
434 int count_offset, limit, count;
435
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400436 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400437 return 1;
438
439 c = get_dx_countlimit(inode, dirent, &count_offset);
440 if (!c) {
441 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
442 return 1;
443 }
444 limit = le16_to_cpu(c->limit);
445 count = le16_to_cpu(c->count);
446 if (count_offset + (limit * sizeof(struct dx_entry)) >
447 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500448 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400449 return 1;
450 }
451 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
452
453 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
454 count, t))
455 return 0;
456 return 1;
457}
458
459static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
460{
461 struct dx_countlimit *c;
462 struct dx_tail *t;
463 int count_offset, limit, count;
464
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400465 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400466 return;
467
468 c = get_dx_countlimit(inode, dirent, &count_offset);
469 if (!c) {
470 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
471 return;
472 }
473 limit = le16_to_cpu(c->limit);
474 count = le16_to_cpu(c->count);
475 if (count_offset + (limit * sizeof(struct dx_entry)) >
476 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500477 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400478 return;
479 }
480 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
481
482 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
483}
484
485static inline int ext4_handle_dirty_dx_node(handle_t *handle,
486 struct inode *inode,
487 struct buffer_head *bh)
488{
489 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
490 return ext4_handle_dirty_metadata(handle, inode, bh);
491}
492
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493/*
Li Zefanf795e142008-07-11 19:27:31 -0400494 * p is at least 6 bytes before the end of page
495 */
496static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500497ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400498{
499 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500500 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400501}
502
503/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 * Future: use high four bits of block for coalesce-on-delete flags
505 * Mask them off for now.
506 */
507
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500508static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509{
510 return le32_to_cpu(entry->block) & 0x00ffffff;
511}
512
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500513static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514{
515 entry->block = cpu_to_le32(value);
516}
517
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400518static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519{
520 return le32_to_cpu(entry->hash);
521}
522
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400523static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524{
525 entry->hash = cpu_to_le32(value);
526}
527
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400528static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529{
530 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
531}
532
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400533static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534{
535 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
536}
537
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400538static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539{
540 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
541}
542
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400543static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544{
545 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
546}
547
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400548static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
Mingming Cao617ba132006-10-11 01:20:53 -0700550 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
551 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400552
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400553 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400554 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400555 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556}
557
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400558static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
Mingming Cao617ba132006-10-11 01:20:53 -0700560 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400561
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400562 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400563 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400564 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565}
566
567/*
568 * Debug
569 */
570#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400571static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572{
Andrew Morton63f57932006-10-11 01:21:24 -0700573 int i, n = dx_get_count (entries);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400574 printk(KERN_DEBUG "%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700575 for (i = 0; i < n; i++) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400576 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500577 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700578 }
579 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580}
581
582struct stats
583{
584 unsigned names;
585 unsigned space;
586 unsigned bcount;
587};
588
Michael Halcrowb3098482015-04-12 01:07:01 -0400589static struct stats dx_show_leaf(struct inode *dir,
590 struct dx_hash_info *hinfo,
591 struct ext4_dir_entry_2 *de,
592 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
594 unsigned names = 0, space = 0;
595 char *base = (char *) de;
596 struct dx_hash_info h = *hinfo;
597
598 printk("names: ");
599 while ((char *) de < base + size)
600 {
601 if (de->inode)
602 {
603 if (show_names)
604 {
Michael Halcrowb3098482015-04-12 01:07:01 -0400605#ifdef CONFIG_EXT4_FS_ENCRYPTION
606 int len;
607 char *name;
608 struct ext4_str fname_crypto_str
609 = {.name = NULL, .len = 0};
610 struct ext4_fname_crypto_ctx *ctx = NULL;
611 int res;
612
613 name = de->name;
614 len = de->name_len;
615 ctx = ext4_get_fname_crypto_ctx(dir,
616 EXT4_NAME_LEN);
617 if (IS_ERR(ctx)) {
618 printk(KERN_WARNING "Error acquiring"
619 " crypto ctxt--skipping crypto\n");
620 ctx = NULL;
621 }
622 if (ctx == NULL) {
623 /* Directory is not encrypted */
624 ext4fs_dirhash(de->name,
625 de->name_len, &h);
626 printk("%*.s:(U)%x.%u ", len,
627 name, h.hash,
628 (unsigned) ((char *) de
629 - base));
630 } else {
631 /* Directory is encrypted */
632 res = ext4_fname_crypto_alloc_buffer(
633 ctx, de->name_len,
634 &fname_crypto_str);
635 if (res < 0) {
636 printk(KERN_WARNING "Error "
637 "allocating crypto "
638 "buffer--skipping "
639 "crypto\n");
640 ext4_put_fname_crypto_ctx(&ctx);
641 ctx = NULL;
642 }
643 res = ext4_fname_disk_to_usr(ctx, de,
644 &fname_crypto_str);
645 if (res < 0) {
646 printk(KERN_WARNING "Error "
647 "converting filename "
648 "from disk to usr"
649 "\n");
650 name = "??";
651 len = 2;
652 } else {
653 name = fname_crypto_str.name;
654 len = fname_crypto_str.len;
655 }
656 res = ext4_fname_disk_to_hash(ctx, de,
657 &h);
658 if (res < 0) {
659 printk(KERN_WARNING "Error "
660 "converting filename "
661 "from disk to htree"
662 "\n");
663 h.hash = 0xDEADBEEF;
664 }
665 printk("%*.s:(E)%x.%u ", len, name,
666 h.hash, (unsigned) ((char *) de
667 - base));
668 ext4_put_fname_crypto_ctx(&ctx);
669 ext4_fname_crypto_free_buffer(
670 &fname_crypto_str);
671 }
672#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 int len = de->name_len;
674 char *name = de->name;
Mingming Cao617ba132006-10-11 01:20:53 -0700675 ext4fs_dirhash(de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400676 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400677 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400678#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679 }
Mingming Cao617ba132006-10-11 01:20:53 -0700680 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 names++;
682 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500683 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684 }
685 printk("(%i)\n", names);
686 return (struct stats) { names, space, 1 };
687}
688
689struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
690 struct dx_entry *entries, int levels)
691{
692 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400693 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 unsigned bcount = 0;
695 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 printk("%i indexed blocks...\n", count);
697 for (i = 0; i < count; i++, entries++)
698 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500699 ext4_lblk_t block = dx_get_block(entries);
700 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
702 struct stats stats;
703 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400704 bh = ext4_bread(NULL,dir, block, 0);
705 if (!bh || IS_ERR(bh))
706 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 stats = levels?
708 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400709 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
710 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711 names += stats.names;
712 space += stats.space;
713 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400714 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 }
716 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400717 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400718 levels ? "" : " ", names, space/bcount,
719 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720 return (struct stats) { names, space, bcount};
721}
722#endif /* DX_DEBUG */
723
724/*
725 * Probe for a directory leaf block to search.
726 *
727 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
728 * error in the directory index, and the caller should fall back to
729 * searching the directory normally. The callers of dx_probe **MUST**
730 * check for this error code, and make sure it never gets reflected
731 * back to userspace.
732 */
733static struct dx_frame *
Theodore Ts'of702ba02008-09-22 15:21:01 -0400734dx_probe(const struct qstr *d_name, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400735 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736{
737 unsigned count, indirect;
738 struct dx_entry *at, *entries, *p, *q, *m;
739 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400741 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 u32 hash;
743
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400744 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
745 if (IS_ERR(frame->bh))
746 return (struct dx_frame *) frame->bh;
747
748 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 if (root->info.hash_version != DX_HASH_TEA &&
750 root->info.hash_version != DX_HASH_HALF_MD4 &&
751 root->info.hash_version != DX_HASH_LEGACY) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500752 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 goto fail;
755 }
756 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400757 if (hinfo->hash_version <= DX_HASH_TEA)
758 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700759 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrowb3098482015-04-12 01:07:01 -0400760#ifdef CONFIG_EXT4_FS_ENCRYPTION
761 if (d_name) {
762 struct ext4_fname_crypto_ctx *ctx = NULL;
763 int res;
764
765 /* Check if the directory is encrypted */
766 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
767 if (IS_ERR(ctx)) {
768 ret_err = ERR_PTR(PTR_ERR(ctx));
769 goto fail;
770 }
771 res = ext4_fname_usr_to_hash(ctx, d_name, hinfo);
772 if (res < 0) {
773 ret_err = ERR_PTR(res);
774 goto fail;
775 }
776 ext4_put_fname_crypto_ctx(&ctx);
777 }
778#else
Theodore Ts'of702ba02008-09-22 15:21:01 -0400779 if (d_name)
780 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Michael Halcrowb3098482015-04-12 01:07:01 -0400781#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 hash = hinfo->hash;
783
784 if (root->info.unused_flags & 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500785 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 goto fail;
788 }
789
790 if ((indirect = root->info.indirect_levels) > 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500791 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 root->info.indirect_levels);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 goto fail;
794 }
795
796 entries = (struct dx_entry *) (((char *)&root->info) +
797 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700798
799 if (dx_get_limit(entries) != dx_root_limit(dir,
800 root->info.info_length)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500801 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700802 goto fail;
803 }
804
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400805 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400806 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700808 if (!count || count > dx_get_limit(entries)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500809 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700810 "dx entry: no count or count > limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400811 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700812 }
813
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 p = entries + 1;
815 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400816 while (p <= q) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 m = p + (q - p)/2;
818 dxtrace(printk("."));
819 if (dx_get_hash(m) > hash)
820 q = m - 1;
821 else
822 p = m + 1;
823 }
824
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400825 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826 unsigned n = count - 1;
827 at = entries;
828 while (n--)
829 {
830 dxtrace(printk(","));
831 if (dx_get_hash(++at) > hash)
832 {
833 at--;
834 break;
835 }
836 }
837 assert (at == p - 1);
838 }
839
840 at = p - 1;
841 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 frame->entries = entries;
843 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400844 if (!indirect--)
845 return frame;
846 frame++;
847 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
848 if (IS_ERR(frame->bh)) {
849 ret_err = (struct dx_frame *) frame->bh;
850 frame->bh = NULL;
851 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400852 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400853 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400854
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700855 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500856 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700857 "dx entry: limit != node limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400858 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700859 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400861fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 while (frame >= frame_in) {
863 brelse(frame->bh);
864 frame--;
865 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400866
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400867 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500868 ext4_warning(dir->i_sb,
Zheng Liu9ee49302012-02-20 23:09:36 -0500869 "Corrupt dir inode %lu, running e2fsck is "
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700870 "recommended.", dir->i_ino);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400871 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872}
873
874static void dx_release (struct dx_frame *frames)
875{
876 if (frames[0].bh == NULL)
877 return;
878
879 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
880 brelse(frames[1].bh);
881 brelse(frames[0].bh);
882}
883
884/*
885 * This function increments the frame pointer to search the next leaf
886 * block, and reads in the necessary intervening nodes if the search
887 * should be necessary. Whether or not the search is necessary is
888 * controlled by the hash parameter. If the hash value is even, then
889 * the search is only continued if the next block starts with that
890 * hash value. This is used if we are searching for a specific file.
891 *
892 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
893 *
894 * This function returns 1 if the caller should continue to search,
895 * or 0 if it should not. If there is an error reading one of the
896 * index blocks, it will a negative error code.
897 *
898 * If start_hash is non-null, it will be filled in with the starting
899 * hash of the next page.
900 */
Mingming Cao617ba132006-10-11 01:20:53 -0700901static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 struct dx_frame *frame,
903 struct dx_frame *frames,
904 __u32 *start_hash)
905{
906 struct dx_frame *p;
907 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500908 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 __u32 bhash;
910
911 p = frame;
912 /*
913 * Find the next leaf page by incrementing the frame pointer.
914 * If we run out of entries in the interior node, loop around and
915 * increment pointer in the parent node. When we break out of
916 * this loop, num_frames indicates the number of interior
917 * nodes need to be read.
918 */
919 while (1) {
920 if (++(p->at) < p->entries + dx_get_count(p->entries))
921 break;
922 if (p == frames)
923 return 0;
924 num_frames++;
925 p--;
926 }
927
928 /*
929 * If the hash is 1, then continue only if the next page has a
930 * continuation hash of any value. This is used for readdir
931 * handling. Otherwise, check to see if the hash matches the
932 * desired contiuation hash. If it doesn't, return since
933 * there's no point to read in the successive index pages.
934 */
935 bhash = dx_get_hash(p->at);
936 if (start_hash)
937 *start_hash = bhash;
938 if ((hash & 1) == 0) {
939 if ((bhash & ~1) != hash)
940 return 0;
941 }
942 /*
943 * If the hash is HASH_NB_ALWAYS, we always go to the next
944 * block so no check is necessary
945 */
946 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500947 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
948 if (IS_ERR(bh))
949 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400951 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 p->bh = bh;
953 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
954 }
955 return 1;
956}
957
958
959/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 * This function fills a red-black tree with information from a
961 * directory block. It returns the number directory entries loaded
962 * into the tree. If there is an error it is returned in err.
963 */
964static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500965 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 struct dx_hash_info *hinfo,
967 __u32 start_hash, __u32 start_minor_hash)
968{
969 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700970 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400971 int err = 0, count = 0;
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400972 struct ext4_fname_crypto_ctx *ctx = NULL;
973 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500975 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
976 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500977 bh = ext4_read_dirblock(dir, block, DIRENT);
978 if (IS_ERR(bh))
979 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400980
Mingming Cao617ba132006-10-11 01:20:53 -0700981 de = (struct ext4_dir_entry_2 *) bh->b_data;
982 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700984 EXT4_DIR_REC_LEN(0));
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400985#ifdef CONFIG_EXT4_FS_ENCRYPTION
986 /* Check if the directory is encrypted */
987 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
988 if (IS_ERR(ctx)) {
989 err = PTR_ERR(ctx);
990 brelse(bh);
991 return err;
992 }
993 if (ctx != NULL) {
994 err = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
995 &fname_crypto_str);
996 if (err < 0) {
997 ext4_put_fname_crypto_ctx(&ctx);
998 brelse(bh);
999 return err;
1000 }
1001 }
1002#endif
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001003 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001004 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -05001005 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001006 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1007 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001008 /* silently ignore the rest of the block */
1009 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001010 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001011#ifdef CONFIG_EXT4_FS_ENCRYPTION
1012 err = ext4_fname_disk_to_hash(ctx, de, hinfo);
1013 if (err < 0) {
1014 count = err;
1015 goto errout;
1016 }
1017#else
Mingming Cao617ba132006-10-11 01:20:53 -07001018 ext4fs_dirhash(de->name, de->name_len, hinfo);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001019#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 if ((hinfo->hash < start_hash) ||
1021 ((hinfo->hash == start_hash) &&
1022 (hinfo->minor_hash < start_minor_hash)))
1023 continue;
1024 if (de->inode == 0)
1025 continue;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001026 if (ctx == NULL) {
1027 /* Directory is not encrypted */
1028 tmp_str.name = de->name;
1029 tmp_str.len = de->name_len;
1030 err = ext4_htree_store_dirent(dir_file,
1031 hinfo->hash, hinfo->minor_hash, de,
1032 &tmp_str);
1033 } else {
1034 /* Directory is encrypted */
1035 err = ext4_fname_disk_to_usr(ctx, de,
1036 &fname_crypto_str);
1037 if (err < 0) {
1038 count = err;
1039 goto errout;
1040 }
1041 err = ext4_htree_store_dirent(dir_file,
1042 hinfo->hash, hinfo->minor_hash, de,
1043 &fname_crypto_str);
1044 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001045 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001046 count = err;
1047 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048 }
1049 count++;
1050 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001051errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 brelse(bh);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001053#ifdef CONFIG_EXT4_FS_ENCRYPTION
1054 ext4_put_fname_crypto_ctx(&ctx);
1055 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1056#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 return count;
1058}
1059
1060
1061/*
1062 * This function fills a red-black tree with information from a
1063 * directory. We start scanning the directory in hash order, starting
1064 * at start_hash and start_minor_hash.
1065 *
1066 * This function returns the number of entries inserted into the tree,
1067 * or a negative error code.
1068 */
Mingming Cao617ba132006-10-11 01:20:53 -07001069int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 __u32 start_minor_hash, __u32 *next_hash)
1071{
1072 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001073 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 struct dx_frame frames[2], *frame;
1075 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001076 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001078 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 __u32 hashval;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001080 struct ext4_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081
Theodore Ts'o60e66792010-05-17 07:00:00 -04001082 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001083 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001084 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001085 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001086 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001087 if (hinfo.hash_version <= DX_HASH_TEA)
1088 hinfo.hash_version +=
1089 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001090 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001091 if (ext4_has_inline_data(dir)) {
1092 int has_inline_data = 1;
1093 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1094 &hinfo, start_hash,
1095 start_minor_hash,
1096 &has_inline_data);
1097 if (has_inline_data) {
1098 *next_hash = ~0;
1099 return count;
1100 }
1101 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1103 start_hash, start_minor_hash);
1104 *next_hash = ~0;
1105 return count;
1106 }
1107 hinfo.hash = start_hash;
1108 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001109 frame = dx_probe(NULL, dir, &hinfo, frames);
1110 if (IS_ERR(frame))
1111 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112
1113 /* Add '.' and '..' from the htree header */
1114 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001115 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001116 tmp_str.name = de->name;
1117 tmp_str.len = de->name_len;
1118 err = ext4_htree_store_dirent(dir_file, 0, 0,
1119 de, &tmp_str);
1120 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 goto errout;
1122 count++;
1123 }
1124 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001125 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001126 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001127 tmp_str.name = de->name;
1128 tmp_str.len = de->name_len;
1129 err = ext4_htree_store_dirent(dir_file, 2, 0,
1130 de, &tmp_str);
1131 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 goto errout;
1133 count++;
1134 }
1135
1136 while (1) {
1137 block = dx_get_block(frame->at);
1138 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1139 start_hash, start_minor_hash);
1140 if (ret < 0) {
1141 err = ret;
1142 goto errout;
1143 }
1144 count += ret;
1145 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001146 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147 frame, frames, &hashval);
1148 *next_hash = hashval;
1149 if (ret < 0) {
1150 err = ret;
1151 goto errout;
1152 }
1153 /*
1154 * Stop if: (a) there are no more entries, or
1155 * (b) we have inserted at least one entry and the
1156 * next hash value is not a continuation
1157 */
1158 if ((ret == 0) ||
1159 (count && ((hashval & 1) == 0)))
1160 break;
1161 }
1162 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001163 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1164 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 return count;
1166errout:
1167 dx_release(frames);
1168 return (err);
1169}
1170
Tao Ma7335cd32012-12-10 14:05:59 -05001171static inline int search_dirblock(struct buffer_head *bh,
1172 struct inode *dir,
1173 const struct qstr *d_name,
1174 unsigned int offset,
1175 struct ext4_dir_entry_2 **res_dir)
1176{
1177 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1178 d_name, offset, res_dir);
1179}
1180
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181/*
1182 * Directory block splitting, compacting
1183 */
1184
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001185/*
1186 * Create map of hash values, offsets, and sizes, stored at end of block.
1187 * Returns number of entries mapped.
1188 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001189static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1190 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001191 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192{
1193 int count = 0;
1194 char *base = (char *) de;
1195 struct dx_hash_info h = *hinfo;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001196#ifdef CONFIG_EXT4_FS_ENCRYPTION
1197 struct ext4_fname_crypto_ctx *ctx = NULL;
1198 int err;
1199
1200 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1201 if (IS_ERR(ctx))
1202 return PTR_ERR(ctx);
1203#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001205 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 if (de->name_len && de->inode) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001207#ifdef CONFIG_EXT4_FS_ENCRYPTION
1208 err = ext4_fname_disk_to_hash(ctx, de, &h);
1209 if (err < 0) {
1210 ext4_put_fname_crypto_ctx(&ctx);
1211 return err;
1212 }
1213#else
Mingming Cao617ba132006-10-11 01:20:53 -07001214 ext4fs_dirhash(de->name, de->name_len, &h);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001215#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216 map_tail--;
1217 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001218 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001219 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 count++;
1221 cond_resched();
1222 }
1223 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001224 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001226#ifdef CONFIG_EXT4_FS_ENCRYPTION
1227 ext4_put_fname_crypto_ctx(&ctx);
1228#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 return count;
1230}
1231
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001232/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1234{
Andrew Morton63f57932006-10-11 01:21:24 -07001235 struct dx_map_entry *p, *q, *top = map + count - 1;
1236 int more;
1237 /* Combsort until bubble sort doesn't suck */
1238 while (count > 2) {
1239 count = count*10/13;
1240 if (count - 9 < 2) /* 9, 10 -> 11 */
1241 count = 11;
1242 for (p = top, q = p - count; q >= map; p--, q--)
1243 if (p->hash < q->hash)
1244 swap(*p, *q);
1245 }
1246 /* Garden variety bubble sort */
1247 do {
1248 more = 0;
1249 q = top;
1250 while (q-- > map) {
1251 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001253 swap(*(q+1), *q);
1254 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 }
1256 } while(more);
1257}
1258
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001259static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260{
1261 struct dx_entry *entries = frame->entries;
1262 struct dx_entry *old = frame->at, *new = old + 1;
1263 int count = dx_get_count(entries);
1264
1265 assert(count < dx_get_limit(entries));
1266 assert(old < entries + count);
1267 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1268 dx_set_hash(new, hash);
1269 dx_set_block(new, block);
1270 dx_set_count(entries, count + 1);
1271}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001273/*
Mingming Cao617ba132006-10-11 01:20:53 -07001274 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001275 *
Mingming Cao617ba132006-10-11 01:20:53 -07001276 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 * `de != NULL' is guaranteed by caller.
1278 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001279static inline int ext4_match(struct ext4_fname_crypto_ctx *ctx,
1280 struct ext4_str *fname_crypto_str,
1281 int len, const char * const name,
1282 struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283{
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001284 int res;
1285
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 if (!de->inode)
1287 return 0;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001288
1289#ifdef CONFIG_EXT4_FS_ENCRYPTION
1290 if (ctx) {
1291 /* Directory is encrypted */
1292 res = ext4_fname_disk_to_usr(ctx, de, fname_crypto_str);
1293 if (res < 0)
1294 return res;
1295 if (len != res)
1296 return 0;
1297 res = memcmp(name, fname_crypto_str->name, len);
1298 return (res == 0) ? 1 : 0;
1299 }
1300#endif
1301 if (len != de->name_len)
1302 return 0;
1303 res = memcmp(name, de->name, len);
1304 return (res == 0) ? 1 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305}
1306
1307/*
1308 * Returns 0 if not found, -1 on failure, and 1 on success
1309 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001310int search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1311 struct inode *dir, const struct qstr *d_name,
1312 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313{
Mingming Cao617ba132006-10-11 01:20:53 -07001314 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001315 char * dlimit;
1316 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001317 const char *name = d_name->name;
1318 int namelen = d_name->len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001319 struct ext4_fname_crypto_ctx *ctx = NULL;
1320 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1321 int res;
1322
1323 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1324 if (IS_ERR(ctx))
1325 return -1;
1326
1327 if (ctx != NULL) {
1328 /* Allocate buffer to hold maximum name length */
1329 res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1330 &fname_crypto_str);
1331 if (res < 0) {
1332 ext4_put_fname_crypto_ctx(&ctx);
1333 return -1;
1334 }
1335 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001336
Tao Ma7335cd32012-12-10 14:05:59 -05001337 de = (struct ext4_dir_entry_2 *)search_buf;
1338 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339 while ((char *) de < dlimit) {
1340 /* this code is executed quadratically often */
1341 /* do minimal checking `by hand' */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001342 if ((char *) de + de->name_len <= dlimit) {
1343 res = ext4_match(ctx, &fname_crypto_str, namelen,
1344 name, de);
1345 if (res < 0) {
1346 res = -1;
1347 goto return_result;
1348 }
1349 if (res > 0) {
1350 /* found a match - just to be sure, do
1351 * a full check */
1352 if (ext4_check_dir_entry(dir, NULL, de, bh,
1353 bh->b_data,
1354 bh->b_size, offset)) {
1355 res = -1;
1356 goto return_result;
1357 }
1358 *res_dir = de;
1359 res = 1;
1360 goto return_result;
1361 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001363 }
1364 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001365 de_len = ext4_rec_len_from_disk(de->rec_len,
1366 dir->i_sb->s_blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001367 if (de_len <= 0) {
1368 res = -1;
1369 goto return_result;
1370 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001372 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001373 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001374
1375 res = 0;
1376return_result:
1377 ext4_put_fname_crypto_ctx(&ctx);
1378 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1379 return res;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001380}
1381
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001382static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1383 struct ext4_dir_entry *de)
1384{
1385 struct super_block *sb = dir->i_sb;
1386
1387 if (!is_dx(dir))
1388 return 0;
1389 if (block == 0)
1390 return 1;
1391 if (de->inode == 0 &&
1392 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1393 sb->s_blocksize)
1394 return 1;
1395 return 0;
1396}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001397
1398/*
Mingming Cao617ba132006-10-11 01:20:53 -07001399 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 *
1401 * finds an entry in the specified directory with the wanted name. It
1402 * returns the cache buffer in which the entry was found, and the entry
1403 * itself (as a parameter - res_dir). It does NOT read the inode of the
1404 * entry - you'll have to do that yourself if you want to.
1405 *
1406 * The returned buffer_head has ->b_count elevated. The caller is expected
1407 * to brelse() it when appropriate.
1408 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001409static struct buffer_head * ext4_find_entry (struct inode *dir,
1410 const struct qstr *d_name,
Tao Ma32f7f222012-12-10 14:06:01 -05001411 struct ext4_dir_entry_2 **res_dir,
1412 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001414 struct super_block *sb;
1415 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1416 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001417 ext4_lblk_t start, block, b;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001418 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 int ra_max = 0; /* Number of bh's in the readahead
1420 buffer, bh_use[] */
1421 int ra_ptr = 0; /* Current index into readahead
1422 buffer */
1423 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001424 ext4_lblk_t nblocks;
Theodore Ts'o10560082014-08-29 20:51:32 -04001425 int i, namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001426
1427 *res_dir = NULL;
1428 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001429 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001430 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001432
1433 if (ext4_has_inline_data(dir)) {
1434 int has_inline_data = 1;
1435 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1436 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001437 if (has_inline_data) {
1438 if (inlined)
1439 *inlined = 1;
Tao Mae8e948e2012-12-10 14:06:00 -05001440 return ret;
Tao Ma32f7f222012-12-10 14:06:01 -05001441 }
Tao Mae8e948e2012-12-10 14:06:00 -05001442 }
1443
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001444 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001445 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001446 /*
1447 * "." or ".." will only be in the first block
1448 * NFS may look up ".."; "." should be handled by the VFS
1449 */
1450 block = start = 0;
1451 nblocks = 1;
1452 goto restart;
1453 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 if (is_dx(dir)) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001455 bh = ext4_dx_find_entry(dir, d_name, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 /*
1457 * On success, or if the error was file not found,
1458 * return. Otherwise, fall back to doing a search the
1459 * old fashioned way.
1460 */
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001461 if (!IS_ERR(bh) || PTR_ERR(bh) != ERR_BAD_DX_DIR)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001463 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1464 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 }
Mingming Cao617ba132006-10-11 01:20:53 -07001466 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1467 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468 if (start >= nblocks)
1469 start = 0;
1470 block = start;
1471restart:
1472 do {
1473 /*
1474 * We deal with the read-ahead logic here.
1475 */
1476 if (ra_ptr >= ra_max) {
1477 /* Refill the readahead buffer */
1478 ra_ptr = 0;
1479 b = block;
1480 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1481 /*
1482 * Terminate if we reach the end of the
1483 * directory and must wrap, or if our
1484 * search has finished at this block.
1485 */
1486 if (b >= nblocks || (num && block == start)) {
1487 bh_use[ra_max] = NULL;
1488 break;
1489 }
1490 num++;
Theodore Ts'o10560082014-08-29 20:51:32 -04001491 bh = ext4_getblk(NULL, dir, b++, 0);
1492 if (unlikely(IS_ERR(bh))) {
Theodore Ts'o36de9282014-08-23 17:47:19 -04001493 if (ra_max == 0)
Theodore Ts'o10560082014-08-29 20:51:32 -04001494 return bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04001495 break;
1496 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 bh_use[ra_max] = bh;
1498 if (bh)
Christoph Hellwig65299a32011-08-23 14:50:29 +02001499 ll_rw_block(READ | REQ_META | REQ_PRIO,
1500 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 }
1502 }
1503 if ((bh = bh_use[ra_ptr++]) == NULL)
1504 goto next;
1505 wait_on_buffer(bh);
1506 if (!buffer_uptodate(bh)) {
1507 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001508 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1509 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 brelse(bh);
1511 goto next;
1512 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001513 if (!buffer_verified(bh) &&
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001514 !is_dx_internal_node(dir, block,
1515 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001516 !ext4_dirent_csum_verify(dir,
1517 (struct ext4_dir_entry *)bh->b_data)) {
1518 EXT4_ERROR_INODE(dir, "checksumming directory "
1519 "block %lu", (unsigned long)block);
1520 brelse(bh);
1521 goto next;
1522 }
1523 set_buffer_verified(bh);
Theodore Ts'of702ba02008-09-22 15:21:01 -04001524 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001525 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001527 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001528 ret = bh;
1529 goto cleanup_and_exit;
1530 } else {
1531 brelse(bh);
1532 if (i < 0)
1533 goto cleanup_and_exit;
1534 }
1535 next:
1536 if (++block >= nblocks)
1537 block = 0;
1538 } while (block != start);
1539
1540 /*
1541 * If the directory has grown while we were searching, then
1542 * search the last part of the directory before giving up.
1543 */
1544 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001545 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 if (block < nblocks) {
1547 start = 0;
1548 goto restart;
1549 }
1550
1551cleanup_and_exit:
1552 /* Clean up the read-ahead blocks */
1553 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001554 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 return ret;
1556}
1557
Theodore Ts'of702ba02008-09-22 15:21:01 -04001558static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001559 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001561 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 struct dx_hash_info hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001565 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001566 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001568#ifdef CONFIG_EXT4_FS_ENCRYPTION
1569 *res_dir = NULL;
1570#endif
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001571 frame = dx_probe(d_name, dir, &hinfo, frames);
1572 if (IS_ERR(frame))
1573 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 do {
1575 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001576 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001577 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001579
Theodore Ts'o7845c042010-10-27 21:30:08 -04001580 retval = search_dirblock(bh, dir, d_name,
1581 block << EXT4_BLOCK_SIZE_BITS(sb),
1582 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001583 if (retval == 1)
1584 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001585 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001586 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001587 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001588 goto errout;
1589 }
1590
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591 /* Check to see if we should continue to search */
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001592 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 frames, NULL);
1594 if (retval < 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001595 ext4_warning(sb,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001596 "error %d reading index page in directory #%lu",
1597 retval, dir->i_ino);
1598 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 goto errout;
1600 }
1601 } while (retval == 1);
1602
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001603 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604errout:
Bernd Schubert265c6a02011-07-16 19:41:23 -04001605 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001606success:
1607 dx_release(frames);
1608 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610
Al Viro00cd8dd2012-06-10 17:13:09 -04001611static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001613 struct inode *inode;
1614 struct ext4_dir_entry_2 *de;
1615 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616
Mingming Cao617ba132006-10-11 01:20:53 -07001617 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 return ERR_PTR(-ENAMETOOLONG);
1619
Tao Ma32f7f222012-12-10 14:06:01 -05001620 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001621 if (IS_ERR(bh))
1622 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 inode = NULL;
1624 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001625 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001626 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001627 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001628 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001629 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001630 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001631 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001632 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1633 dentry);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001634 return ERR_PTR(-EIO);
1635 }
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001636 inode = ext4_iget_normal(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001637 if (inode == ERR_PTR(-ESTALE)) {
1638 EXT4_ERROR_INODE(dir,
1639 "deleted inode referenced: %u",
1640 ino);
1641 return ERR_PTR(-EIO);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001642 }
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001643 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
1644 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1645 S_ISLNK(inode->i_mode)) &&
1646 !ext4_is_child_context_consistent_with_parent(dir,
1647 inode)) {
1648 iput(inode);
1649 ext4_warning(inode->i_sb,
1650 "Inconsistent encryption contexts: %lu/%lu\n",
1651 (unsigned long) dir->i_ino,
1652 (unsigned long) inode->i_ino);
1653 return ERR_PTR(-EPERM);
1654 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001655 }
1656 return d_splice_alias(inode, dentry);
1657}
1658
1659
Mingming Cao617ba132006-10-11 01:20:53 -07001660struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001661{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001662 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001663 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001664 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001665 struct buffer_head *bh;
1666
David Howells2b0143b2015-03-17 22:25:59 +00001667 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001668 if (IS_ERR(bh))
1669 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670 if (!bh)
1671 return ERR_PTR(-ENOENT);
1672 ino = le32_to_cpu(de->inode);
1673 brelse(bh);
1674
David Howells2b0143b2015-03-17 22:25:59 +00001675 if (!ext4_valid_inum(d_inode(child)->i_sb, ino)) {
1676 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001677 "bad parent inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001678 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001679 }
1680
David Howells2b0143b2015-03-17 22:25:59 +00001681 return d_obtain_alias(ext4_iget_normal(d_inode(child)->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001682}
1683
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001684/*
1685 * Move count entries from end of map between two memory locations.
1686 * Returns pointer to last entry moved.
1687 */
Mingming Cao617ba132006-10-11 01:20:53 -07001688static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001689dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1690 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691{
1692 unsigned rec_len = 0;
1693
1694 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001695 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001696 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001697 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001699 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001700 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701 de->inode = 0;
1702 map++;
1703 to += rec_len;
1704 }
Mingming Cao617ba132006-10-11 01:20:53 -07001705 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706}
1707
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001708/*
1709 * Compact each dir entry in the range to the minimal rec_len.
1710 * Returns pointer to last entry in range.
1711 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001712static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713{
Mingming Cao617ba132006-10-11 01:20:53 -07001714 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715 unsigned rec_len = 0;
1716
1717 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001718 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001719 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001721 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 if (de > to)
1723 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001724 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001725 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001726 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 }
1728 de = next;
1729 }
1730 return prev;
1731}
1732
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001733/*
1734 * Split a full leaf block to make room for a new dir entry.
1735 * Allocate a new block, and move entries so that they are approx. equally full.
1736 * Returns pointer to de in block into which the new entry will be inserted.
1737 */
Mingming Cao617ba132006-10-11 01:20:53 -07001738static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001739 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001740 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741{
1742 unsigned blocksize = dir->i_sb->s_blocksize;
1743 unsigned count, continued;
1744 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001745 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746 u32 hash2;
1747 struct dx_map_entry *map;
1748 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001749 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001750 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001751 struct ext4_dir_entry_tail *t;
1752 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001753 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001754
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001755 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001756 csum_size = sizeof(struct ext4_dir_entry_tail);
1757
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001758 bh2 = ext4_append(handle, dir, &newblock);
1759 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001760 brelse(*bh);
1761 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001762 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 }
1764
1765 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001766 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001767 if (err)
1768 goto journal_error;
1769
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001771 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772 if (err)
1773 goto journal_error;
1774
1775 data2 = bh2->b_data;
1776
1777 /* create map in the end of data2 block */
1778 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001779 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780 blocksize, hinfo, map);
1781 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001782 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001783 /* Split the existing block in the middle, size-wise */
1784 size = 0;
1785 move = 0;
1786 for (i = count-1; i >= 0; i--) {
1787 /* is more than half of this entry in 2nd half of the block? */
1788 if (size + map[i].size/2 > blocksize/2)
1789 break;
1790 size += map[i].size;
1791 move++;
1792 }
1793 /* map index at which we will split */
1794 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795 hash2 = map[split].hash;
1796 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001797 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1798 (unsigned long)dx_get_block(frame->at),
1799 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800
1801 /* Fancy dance to stay within two buffers */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001802 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1803 blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001804 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001805 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1806 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001807 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001808 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1809 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001810 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001811 if (csum_size) {
1812 t = EXT4_DIRENT_TAIL(data2, blocksize);
1813 initialize_dirent_tail(t, blocksize);
1814
1815 t = EXT4_DIRENT_TAIL(data1, blocksize);
1816 initialize_dirent_tail(t, blocksize);
1817 }
1818
Michael Halcrowb3098482015-04-12 01:07:01 -04001819 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1820 blocksize, 1));
1821 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1822 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823
1824 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001825 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 swap(*bh, bh2);
1827 de = de2;
1828 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001829 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001830 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831 if (err)
1832 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001833 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 if (err)
1835 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001836 brelse(bh2);
1837 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001839
1840journal_error:
1841 brelse(*bh);
1842 brelse(bh2);
1843 *bh = NULL;
1844 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001845 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847
Tao Ma978fef92012-12-10 14:05:58 -05001848int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1849 struct buffer_head *bh,
1850 void *buf, int buf_size,
1851 const char *name, int namelen,
1852 struct ext4_dir_entry_2 **dest_de)
1853{
1854 struct ext4_dir_entry_2 *de;
1855 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1856 int nlen, rlen;
1857 unsigned int offset = 0;
1858 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001859 struct ext4_fname_crypto_ctx *ctx = NULL;
1860 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1861 int res;
1862
1863 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1864 if (IS_ERR(ctx))
1865 return -1;
1866
1867 if (ctx != NULL) {
1868 /* Calculate record length needed to store the entry */
1869 res = ext4_fname_crypto_namelen_on_disk(ctx, namelen);
1870 if (res < 0) {
1871 ext4_put_fname_crypto_ctx(&ctx);
1872 return res;
1873 }
1874 reclen = EXT4_DIR_REC_LEN(res);
1875
1876 /* Allocate buffer to hold maximum name length */
1877 res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1878 &fname_crypto_str);
1879 if (res < 0) {
1880 ext4_put_fname_crypto_ctx(&ctx);
1881 return -1;
1882 }
1883 }
Tao Ma978fef92012-12-10 14:05:58 -05001884
1885 de = (struct ext4_dir_entry_2 *)buf;
1886 top = buf + buf_size - reclen;
1887 while ((char *) de <= top) {
1888 if (ext4_check_dir_entry(dir, NULL, de, bh,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001889 buf, buf_size, offset)) {
1890 res = -EIO;
1891 goto return_result;
1892 }
1893 /* Provide crypto context and crypto buffer to ext4 match */
1894 res = ext4_match(ctx, &fname_crypto_str, namelen, name, de);
1895 if (res < 0)
1896 goto return_result;
1897 if (res > 0) {
1898 res = -EEXIST;
1899 goto return_result;
1900 }
Tao Ma978fef92012-12-10 14:05:58 -05001901 nlen = EXT4_DIR_REC_LEN(de->name_len);
1902 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1903 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1904 break;
1905 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1906 offset += rlen;
1907 }
Tao Ma978fef92012-12-10 14:05:58 -05001908
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001909 if ((char *) de > top)
1910 res = -ENOSPC;
1911 else {
1912 *dest_de = de;
1913 res = 0;
1914 }
1915return_result:
1916 ext4_put_fname_crypto_ctx(&ctx);
1917 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1918 return res;
Tao Ma978fef92012-12-10 14:05:58 -05001919}
1920
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001921int ext4_insert_dentry(struct inode *dir,
1922 struct inode *inode,
1923 struct ext4_dir_entry_2 *de,
1924 int buf_size,
1925 const struct qstr *iname,
1926 const char *name, int namelen)
Tao Ma978fef92012-12-10 14:05:58 -05001927{
1928
1929 int nlen, rlen;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001930 struct ext4_fname_crypto_ctx *ctx = NULL;
1931 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1932 struct ext4_str tmp_str;
1933 int res;
1934
1935 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1936 if (IS_ERR(ctx))
1937 return -EIO;
1938 /* By default, the input name would be written to the disk */
1939 tmp_str.name = (unsigned char *)name;
1940 tmp_str.len = namelen;
1941 if (ctx != NULL) {
1942 /* Directory is encrypted */
1943 res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1944 &fname_crypto_str);
1945 if (res < 0) {
1946 ext4_put_fname_crypto_ctx(&ctx);
1947 return -ENOMEM;
1948 }
1949 res = ext4_fname_usr_to_disk(ctx, iname, &fname_crypto_str);
1950 if (res < 0) {
1951 ext4_put_fname_crypto_ctx(&ctx);
1952 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1953 return res;
1954 }
1955 tmp_str.name = fname_crypto_str.name;
1956 tmp_str.len = fname_crypto_str.len;
1957 }
Tao Ma978fef92012-12-10 14:05:58 -05001958
1959 nlen = EXT4_DIR_REC_LEN(de->name_len);
1960 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1961 if (de->inode) {
1962 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001963 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001964 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1965 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1966 de = de1;
1967 }
1968 de->file_type = EXT4_FT_UNKNOWN;
1969 de->inode = cpu_to_le32(inode->i_ino);
1970 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001971 de->name_len = tmp_str.len;
1972
1973 memcpy(de->name, tmp_str.name, tmp_str.len);
1974 ext4_put_fname_crypto_ctx(&ctx);
1975 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1976 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001977}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001978
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979/*
1980 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1981 * it points to a directory entry which is guaranteed to be large
1982 * enough for new directory entry. If de is NULL, then
1983 * add_dirent_to_buf will attempt search the directory block for
1984 * space. It will return -ENOSPC if no space is available, and -EIO
1985 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001986 */
1987static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001988 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001989 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990{
David Howells2b0143b2015-03-17 22:25:59 +00001991 struct inode *dir = d_inode(dentry->d_parent);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001992 const char *name = dentry->d_name.name;
1993 int namelen = dentry->d_name.len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001994 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001995 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001996 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001997
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001998 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001999 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002001 if (!de) {
Tao Ma978fef92012-12-10 14:05:58 -05002002 err = ext4_find_dest_de(dir, inode,
2003 bh, bh->b_data, blocksize - csum_size,
2004 name, namelen, &de);
2005 if (err)
2006 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 }
2008 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002009 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002010 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002011 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 return err;
2013 }
2014
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002015 /* By now the buffer is marked for journaling. Due to crypto operations,
2016 * the following function call may fail */
2017 err = ext4_insert_dentry(dir, inode, de, blocksize, &dentry->d_name,
2018 name, namelen);
2019 if (err < 0)
2020 return err;
Tao Ma978fef92012-12-10 14:05:58 -05002021
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 /*
2023 * XXX shouldn't update any times until successful
2024 * completion of syscall, but too many callers depend
2025 * on this.
2026 *
2027 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07002028 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 * recovery deletes the inode, so the worst that can
2030 * happen is that the times are slightly out of date
2031 * and/or different from the directory change time.
2032 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04002033 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002034 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002035 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07002036 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05002037 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002038 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002039 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07002040 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 return 0;
2042}
2043
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044/*
2045 * This converts a one block unindexed directory to a 3 block indexed
2046 * directory, and adds the dentry to the indexed directory.
2047 */
2048static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
2049 struct inode *inode, struct buffer_head *bh)
2050{
David Howells2b0143b2015-03-17 22:25:59 +00002051 struct inode *dir = d_inode(dentry->d_parent);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002052#ifdef CONFIG_EXT4_FS_ENCRYPTION
2053 struct ext4_fname_crypto_ctx *ctx = NULL;
2054 int res;
2055#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002056 const char *name = dentry->d_name.name;
2057 int namelen = dentry->d_name.len;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002058#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059 struct buffer_head *bh2;
2060 struct dx_root *root;
2061 struct dx_frame frames[2], *frame;
2062 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07002063 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002064 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065 char *data1, *top;
2066 unsigned len;
2067 int retval;
2068 unsigned blocksize;
2069 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002070 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002072 int csum_size = 0;
2073
2074#ifdef CONFIG_EXT4_FS_ENCRYPTION
2075 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
2076 if (IS_ERR(ctx))
2077 return PTR_ERR(ctx);
2078#endif
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002079
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002080 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002081 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082
2083 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002084 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04002085 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002086 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002088 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 brelse(bh);
2090 return retval;
2091 }
2092 root = (struct dx_root *) bh->b_data;
2093
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002094 /* The 0th block becomes the root, move the dirents out */
2095 fde = &root->dotdot;
2096 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002097 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002098 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002099 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002100 brelse(bh);
2101 return -EIO;
2102 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002103 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002104
2105 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002106 bh2 = ext4_append(handle, dir, &block);
2107 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002109 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002111 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112 data1 = bh2->b_data;
2113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002114 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07002115 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002116 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002117 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002119 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
2120 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002121 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002122
2123 if (csum_size) {
2124 t = EXT4_DIRENT_TAIL(data1, blocksize);
2125 initialize_dirent_tail(t, blocksize);
2126 }
2127
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002128 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002129 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002130 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2131 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 memset (&root->info, 0, sizeof(root->info));
2133 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07002134 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002136 dx_set_block(entries, 1);
2137 dx_set_count(entries, 1);
2138 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139
2140 /* Initialize as for dx_probe */
2141 hinfo.hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002142 if (hinfo.hash_version <= DX_HASH_TEA)
2143 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07002144 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002145#ifdef CONFIG_EXT4_FS_ENCRYPTION
2146 res = ext4_fname_usr_to_hash(ctx, &dentry->d_name, &hinfo);
2147 if (res < 0) {
2148 ext4_put_fname_crypto_ctx(&ctx);
2149 ext4_mark_inode_dirty(handle, dir);
2150 brelse(bh);
2151 return res;
2152 }
2153 ext4_put_fname_crypto_ctx(&ctx);
2154#else
Mingming Cao617ba132006-10-11 01:20:53 -07002155 ext4fs_dirhash(name, namelen, &hinfo);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002156#endif
Jan Kara6050d472014-10-30 10:53:17 -04002157 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 frame = frames;
2159 frame->entries = entries;
2160 frame->at = entries;
2161 frame->bh = bh;
2162 bh = bh2;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002163
Jan Kara6050d472014-10-30 10:53:17 -04002164 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2165 if (retval)
2166 goto out_frames;
2167 retval = ext4_handle_dirty_dirent_node(handle, dir, bh);
2168 if (retval)
2169 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002170
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002171 de = do_split(handle,dir, &bh, frame, &hinfo);
2172 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002173 retval = PTR_ERR(de);
2174 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002175 }
2176 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002178 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
2179 brelse(bh);
2180 return retval;
Jan Kara6050d472014-10-30 10:53:17 -04002181out_frames:
2182 /*
2183 * Even if the block split failed, we have to properly write
2184 * out all the changes we did so far. Otherwise we can end up
2185 * with corrupted filesystem.
2186 */
2187 ext4_mark_inode_dirty(handle, dir);
2188 dx_release(frames);
2189 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002190}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191
2192/*
Mingming Cao617ba132006-10-11 01:20:53 -07002193 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002194 *
2195 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002196 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 *
2198 * NOTE!! The inode part of 'de' is left at 0 - which means you
2199 * may not sleep between calling this and putting something into
2200 * the entry, as someone else might have used it while you slept.
2201 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002202static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2203 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204{
David Howells2b0143b2015-03-17 22:25:59 +00002205 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002206 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002207 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002208 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002209 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002210 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002212 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002213 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002214 int csum_size = 0;
2215
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002216 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002217 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218
2219 sb = dir->i_sb;
2220 blocksize = sb->s_blocksize;
2221 if (!dentry->d_name.len)
2222 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002223
2224 if (ext4_has_inline_data(dir)) {
2225 retval = ext4_try_add_inline_entry(handle, dentry, inode);
2226 if (retval < 0)
2227 return retval;
2228 if (retval == 1) {
2229 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002230 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002231 }
2232 }
2233
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002235 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002237 goto out;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002238 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002240 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002243 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002244 bh = ext4_read_dirblock(dir, block, DIRENT);
2245 if (IS_ERR(bh))
2246 return PTR_ERR(bh);
2247
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002248 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002249 if (retval != -ENOSPC)
2250 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002251
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002252 if (blocks == 1 && !dx_fallback &&
Lukas Czernere12fb972015-04-03 10:46:58 -04002253 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
2254 retval = make_indexed_dir(handle, dentry, inode, bh);
2255 bh = NULL; /* make_indexed_dir releases bh */
2256 goto out;
2257 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258 brelse(bh);
2259 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002260 bh = ext4_append(handle, dir, &block);
2261 if (IS_ERR(bh))
2262 return PTR_ERR(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002263 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002265 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2266
2267 if (csum_size) {
2268 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2269 initialize_dirent_tail(t, blocksize);
2270 }
2271
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002272 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002273out:
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002274 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002275 if (retval == 0)
2276 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002277 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002278}
2279
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280/*
2281 * Returns 0 for success, or a negative error value
2282 */
Mingming Cao617ba132006-10-11 01:20:53 -07002283static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284 struct inode *inode)
2285{
2286 struct dx_frame frames[2], *frame;
2287 struct dx_entry *entries, *at;
2288 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002289 struct buffer_head *bh;
David Howells2b0143b2015-03-17 22:25:59 +00002290 struct inode *dir = d_inode(dentry->d_parent);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002291 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002292 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 int err;
2294
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002295 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames);
2296 if (IS_ERR(frame))
2297 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298 entries = frame->entries;
2299 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002300 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2301 if (IS_ERR(bh)) {
2302 err = PTR_ERR(bh);
2303 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002305 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002306
2307 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002308 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 if (err)
2310 goto journal_error;
2311
2312 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002313 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002315
2316 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002317 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002318 dx_get_count(entries), dx_get_limit(entries)));
2319 /* Need to split index? */
2320 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002321 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002322 unsigned icount = dx_get_count(entries);
2323 int levels = frame - frames;
2324 struct dx_entry *entries2;
2325 struct dx_node *node2;
2326 struct buffer_head *bh2;
2327
2328 if (levels && (dx_get_count(frames->entries) ==
2329 dx_get_limit(frames->entries))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002330 ext4_warning(sb, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002331 err = -ENOSPC;
2332 goto cleanup;
2333 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002334 bh2 = ext4_append(handle, dir, &newblock);
2335 if (IS_ERR(bh2)) {
2336 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002337 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002338 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 node2 = (struct dx_node *)(bh2->b_data);
2340 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002341 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002342 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2343 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002344 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002345 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002346 if (err)
2347 goto journal_error;
2348 if (levels) {
2349 unsigned icount1 = icount/2, icount2 = icount - icount1;
2350 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002351 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2352 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353
2354 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002355 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356 frames[0].bh);
2357 if (err)
2358 goto journal_error;
2359
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002360 memcpy((char *) entries2, (char *) (entries + icount1),
2361 icount2 * sizeof(struct dx_entry));
2362 dx_set_count(entries, icount1);
2363 dx_set_count(entries2, icount2);
2364 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002365
2366 /* Which index block gets the new entry? */
2367 if (at - entries >= icount1) {
2368 frame->at = at = at - entries - icount1 + entries2;
2369 frame->entries = entries = entries2;
2370 swap(frame->bh, bh2);
2371 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002372 dx_insert_block(frames + 0, hash2, newblock);
2373 dxtrace(dx_show_index("node", frames[1].entries));
2374 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002376 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002377 if (err)
2378 goto journal_error;
2379 brelse (bh2);
2380 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002381 dxtrace(printk(KERN_DEBUG
2382 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 memcpy((char *) entries2, (char *) entries,
2384 icount * sizeof(struct dx_entry));
2385 dx_set_limit(entries2, dx_node_limit(dir));
2386
2387 /* Set up root */
2388 dx_set_count(entries, 1);
2389 dx_set_block(entries + 0, newblock);
2390 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2391
2392 /* Add new access path frame */
2393 frame = frames + 1;
2394 frame->at = at = at - entries + entries2;
2395 frame->entries = entries = entries2;
2396 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002397 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002398 frame->bh);
2399 if (err)
2400 goto journal_error;
2401 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002402 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002403 if (err) {
2404 ext4_std_error(inode->i_sb, err);
2405 goto cleanup;
2406 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002407 }
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002408 de = do_split(handle, dir, &bh, frame, &hinfo);
2409 if (IS_ERR(de)) {
2410 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002412 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002413 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 goto cleanup;
2415
2416journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002417 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002419 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 dx_release(frames);
2421 return err;
2422}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423
2424/*
Tao Ma05019a92012-12-10 14:06:00 -05002425 * ext4_generic_delete_entry deletes a directory entry by merging it
2426 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002427 */
Tao Ma05019a92012-12-10 14:06:00 -05002428int ext4_generic_delete_entry(handle_t *handle,
2429 struct inode *dir,
2430 struct ext4_dir_entry_2 *de_del,
2431 struct buffer_head *bh,
2432 void *entry_buf,
2433 int buf_size,
2434 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002435{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002436 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002437 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002438 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002439
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440 i = 0;
2441 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002442 de = (struct ext4_dir_entry_2 *)entry_buf;
2443 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002444 if (ext4_check_dir_entry(dir, NULL, de, bh,
2445 bh->b_data, bh->b_size, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 return -EIO;
2447 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002448 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002449 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002450 ext4_rec_len_from_disk(pde->rec_len,
2451 blocksize) +
2452 ext4_rec_len_from_disk(de->rec_len,
2453 blocksize),
2454 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002455 else
2456 de->inode = 0;
2457 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458 return 0;
2459 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002460 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002461 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002462 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002463 }
2464 return -ENOENT;
2465}
2466
Tao Ma05019a92012-12-10 14:06:00 -05002467static int ext4_delete_entry(handle_t *handle,
2468 struct inode *dir,
2469 struct ext4_dir_entry_2 *de_del,
2470 struct buffer_head *bh)
2471{
2472 int err, csum_size = 0;
2473
Tao Ma9f40fe52012-12-10 14:06:00 -05002474 if (ext4_has_inline_data(dir)) {
2475 int has_inline_data = 1;
2476 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2477 &has_inline_data);
2478 if (has_inline_data)
2479 return err;
2480 }
2481
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002482 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002483 csum_size = sizeof(struct ext4_dir_entry_tail);
2484
2485 BUFFER_TRACE(bh, "get_write_access");
2486 err = ext4_journal_get_write_access(handle, bh);
2487 if (unlikely(err))
2488 goto out;
2489
2490 err = ext4_generic_delete_entry(handle, dir, de_del,
2491 bh, bh->b_data,
2492 dir->i_sb->s_blocksize, csum_size);
2493 if (err)
2494 goto out;
2495
2496 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2497 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2498 if (unlikely(err))
2499 goto out;
2500
2501 return 0;
2502out:
2503 if (err != -ENOENT)
2504 ext4_std_error(dir->i_sb, err);
2505 return err;
2506}
2507
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002508/*
2509 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2510 * since this indicates that nlinks count was previously 1.
2511 */
2512static void ext4_inc_count(handle_t *handle, struct inode *inode)
2513{
2514 inc_nlink(inode);
2515 if (is_dx(inode) && inode->i_nlink > 1) {
2516 /* limit is 16-bit i_links_count */
2517 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002518 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002519 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2520 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2521 }
2522 }
2523}
2524
2525/*
2526 * If a directory had nlink == 1, then we should let it be 1. This indicates
2527 * directory has >EXT4_LINK_MAX subdirs.
2528 */
2529static void ext4_dec_count(handle_t *handle, struct inode *inode)
2530{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002531 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2532 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002533}
2534
2535
Mingming Cao617ba132006-10-11 01:20:53 -07002536static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002537 struct dentry *dentry, struct inode *inode)
2538{
Mingming Cao617ba132006-10-11 01:20:53 -07002539 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002540 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002541 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002542 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002543 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002544 return 0;
2545 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002546 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002547 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 iput(inode);
2549 return err;
2550}
2551
2552/*
2553 * By the time this is called, we already have created
2554 * the directory cache entry for the new file, but it
2555 * is so far negative - it has no inode.
2556 *
2557 * If the create succeeds, we fill in the inode information
2558 * with d_instantiate().
2559 */
Al Viro4acdaf22011-07-26 01:42:34 -04002560static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002561 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002562{
2563 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002564 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002565 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002566
Christoph Hellwig871a2932010-03-03 09:05:07 -05002567 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002568
Theodore Ts'o11395752013-02-09 16:27:09 -05002569 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002570 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002571retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002572 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2573 NULL, EXT4_HT_DIR, credits);
2574 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002575 err = PTR_ERR(inode);
2576 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002577 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002578 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002579 ext4_set_aops(inode);
Michael Halcrowdde680c2015-04-12 00:55:09 -04002580 err = 0;
2581#ifdef CONFIG_EXT4_FS_ENCRYPTION
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002582 if (!err && (ext4_encrypted_inode(dir) ||
2583 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)))) {
Michael Halcrowdde680c2015-04-12 00:55:09 -04002584 err = ext4_inherit_context(dir, inode);
2585 if (err) {
2586 clear_nlink(inode);
2587 unlock_new_inode(inode);
2588 iput(inode);
2589 }
2590 }
2591#endif
2592 if (!err)
2593 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002594 if (!err && IS_DIRSYNC(dir))
2595 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002596 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002597 if (handle)
2598 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002599 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002600 goto retry;
2601 return err;
2602}
2603
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002604static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002605 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002606{
2607 handle_t *handle;
2608 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002609 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610
2611 if (!new_valid_dev(rdev))
2612 return -EINVAL;
2613
Christoph Hellwig871a2932010-03-03 09:05:07 -05002614 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002615
Theodore Ts'o11395752013-02-09 16:27:09 -05002616 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002617 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002618retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002619 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2620 NULL, EXT4_HT_DIR, credits);
2621 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 err = PTR_ERR(inode);
2623 if (!IS_ERR(inode)) {
2624 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002625 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002626 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002627 if (!err && IS_DIRSYNC(dir))
2628 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002629 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002630 if (handle)
2631 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002632 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002633 goto retry;
2634 return err;
2635}
2636
Al Viroaf51a2a2013-06-29 13:23:08 +04002637static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2638{
2639 handle_t *handle;
2640 struct inode *inode;
2641 int err, retries = 0;
2642
2643 dquot_initialize(dir);
2644
2645retry:
2646 inode = ext4_new_inode_start_handle(dir, mode,
2647 NULL, 0, NULL,
2648 EXT4_HT_DIR,
2649 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2650 4 + EXT4_XATTR_TRANS_BLOCKS);
2651 handle = ext4_journal_current_handle();
2652 err = PTR_ERR(inode);
2653 if (!IS_ERR(inode)) {
2654 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002655 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002656 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002657 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002658 err = ext4_orphan_add(handle, inode);
2659 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002660 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002661 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002662 unlock_new_inode(inode);
2663 }
2664 if (handle)
2665 ext4_journal_stop(handle);
2666 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2667 goto retry;
2668 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002669err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002670 ext4_journal_stop(handle);
2671 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002672 return err;
2673}
2674
Tao Maa774f9c2012-12-10 14:05:57 -05002675struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2676 struct ext4_dir_entry_2 *de,
2677 int blocksize, int csum_size,
2678 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679{
Tao Maa774f9c2012-12-10 14:05:57 -05002680 de->inode = cpu_to_le32(inode->i_ino);
2681 de->name_len = 1;
2682 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2683 blocksize);
2684 strcpy(de->name, ".");
2685 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2686
2687 de = ext4_next_entry(de, blocksize);
2688 de->inode = cpu_to_le32(parent_ino);
2689 de->name_len = 2;
2690 if (!dotdot_real_len)
2691 de->rec_len = ext4_rec_len_to_disk(blocksize -
2692 (csum_size + EXT4_DIR_REC_LEN(1)),
2693 blocksize);
2694 else
2695 de->rec_len = ext4_rec_len_to_disk(
2696 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2697 strcpy(de->name, "..");
2698 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2699
2700 return ext4_next_entry(de, blocksize);
2701}
2702
2703static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2704 struct inode *inode)
2705{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002706 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002707 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002708 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002709 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002710 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002711 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002712 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002713
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002714 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002715 csum_size = sizeof(struct ext4_dir_entry_tail);
2716
Tao Ma3c47d542012-12-10 14:05:59 -05002717 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2718 err = ext4_try_create_inline_dir(handle, dir, inode);
2719 if (err < 0 && err != -ENOSPC)
2720 goto out;
2721 if (!err)
2722 goto out;
2723 }
2724
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002725 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002726 dir_block = ext4_append(handle, inode, &block);
2727 if (IS_ERR(dir_block))
2728 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002729 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2730 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2731 set_nlink(inode, 2);
2732 if (csum_size) {
2733 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2734 initialize_dirent_tail(t, blocksize);
2735 }
2736
2737 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2738 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2739 if (err)
2740 goto out;
2741 set_buffer_verified(dir_block);
2742out:
2743 brelse(dir_block);
2744 return err;
2745}
2746
2747static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2748{
2749 handle_t *handle;
2750 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002751 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002752
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002753 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002754 return -EMLINK;
2755
Christoph Hellwig871a2932010-03-03 09:05:07 -05002756 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002757
Theodore Ts'o11395752013-02-09 16:27:09 -05002758 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002759 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002760retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002761 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2762 &dentry->d_name,
2763 0, NULL, EXT4_HT_DIR, credits);
2764 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765 err = PTR_ERR(inode);
2766 if (IS_ERR(inode))
2767 goto out_stop;
2768
Mingming Cao617ba132006-10-11 01:20:53 -07002769 inode->i_op = &ext4_dir_inode_operations;
2770 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002771 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002772 if (err)
2773 goto out_clear_inode;
Michael Halcrowdde680c2015-04-12 00:55:09 -04002774#ifdef CONFIG_EXT4_FS_ENCRYPTION
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002775 if (ext4_encrypted_inode(dir) ||
2776 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) {
Michael Halcrowdde680c2015-04-12 00:55:09 -04002777 err = ext4_inherit_context(dir, inode);
2778 if (err)
2779 goto out_clear_inode;
2780 }
2781#endif
Namhyung Kimdabd9912011-01-10 12:11:16 -05002782 err = ext4_mark_inode_dirty(handle, inode);
2783 if (!err)
2784 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002785 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002786out_clear_inode:
2787 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002788 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002789 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002790 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002791 goto out_stop;
2792 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002793 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002794 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002795 err = ext4_mark_inode_dirty(handle, dir);
2796 if (err)
2797 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002798 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002799 d_instantiate(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002800 if (IS_DIRSYNC(dir))
2801 ext4_handle_sync(handle);
2802
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002803out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002804 if (handle)
2805 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002806 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002807 goto retry;
2808 return err;
2809}
2810
2811/*
2812 * routine to check that the specified directory is empty (for rmdir)
2813 */
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002814int ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002816 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002817 struct buffer_head *bh;
2818 struct ext4_dir_entry_2 *de, *de1;
2819 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820 int err = 0;
2821
Tao Ma61f86632012-12-10 14:06:01 -05002822 if (ext4_has_inline_data(inode)) {
2823 int has_inline_data = 1;
2824
2825 err = empty_inline_dir(inode, &has_inline_data);
2826 if (has_inline_data)
2827 return err;
2828 }
2829
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002831 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2832 EXT4_ERROR_INODE(inode, "invalid size");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002833 return 1;
2834 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002835 bh = ext4_read_dirblock(inode, 0, EITHER);
2836 if (IS_ERR(bh))
2837 return 1;
2838
Mingming Cao617ba132006-10-11 01:20:53 -07002839 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002840 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002841 if (le32_to_cpu(de->inode) != inode->i_ino ||
2842 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002843 strcmp(".", de->name) ||
2844 strcmp("..", de1->name)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002845 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002846 "bad directory (dir #%lu) - no `.' or `..'",
2847 inode->i_ino);
2848 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002849 return 1;
2850 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002851 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2852 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2853 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002854 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002855 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002856 unsigned int lblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002858 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002859 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002860 bh = ext4_read_dirblock(inode, lblock, EITHER);
2861 if (IS_ERR(bh))
2862 return 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002863 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864 }
Tao Ma226ba972012-12-10 14:05:58 -05002865 if (ext4_check_dir_entry(inode, NULL, de, bh,
2866 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002867 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002868 sb->s_blocksize);
2869 offset = (offset | (sb->s_blocksize - 1)) + 1;
2870 continue;
2871 }
2872 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002873 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874 return 0;
2875 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002876 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2877 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002878 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002879 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002880 return 1;
2881}
2882
Jan Karad745a8c2014-05-26 11:56:53 -04002883/*
2884 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002885 * such inodes, starting at the superblock, in case we crash before the
2886 * file is closed/deleted, or in case the inode truncate spans multiple
2887 * transactions and the last transaction is not recovered after a crash.
2888 *
2889 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002890 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002891 *
2892 * Orphan list manipulation functions must be called under i_mutex unless
2893 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 */
Mingming Cao617ba132006-10-11 01:20:53 -07002895int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896{
2897 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002898 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002899 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002901 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002902
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002903 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002904 return 0;
2905
Jan Karad745a8c2014-05-26 11:56:53 -04002906 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2907 !mutex_is_locked(&inode->i_mutex));
2908 /*
2909 * Exit early if inode already is on orphan list. This is a big speedup
2910 * since we don't have to contend on the global s_orphan_lock.
2911 */
Mingming Cao617ba132006-10-11 01:20:53 -07002912 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002913 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002914
Lukas Czernerafb86172011-07-11 18:47:04 -04002915 /*
2916 * Orphan handling is only valid for files with data blocks
2917 * being truncated, or files being unlinked. Note that we either
2918 * hold i_mutex, or the inode can not be referenced from outside,
2919 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002920 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002921 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2922 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002923
Jan Karacd2c0802014-05-26 11:39:17 -04002924 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2925 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002927 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002928
Mingming Cao617ba132006-10-11 01:20:53 -07002929 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002930 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002931 goto out;
2932
2933 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002934 /*
2935 * Due to previous errors inode may be already a part of on-disk
2936 * orphan list. If so skip on-disk list modification.
2937 */
Jan Karad745a8c2014-05-26 11:56:53 -04002938 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2939 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2940 /* Insert this inode at the head of the on-disk orphan list */
2941 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2942 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2943 dirty = true;
2944 }
2945 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2946 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947
Jan Karad745a8c2014-05-26 11:56:53 -04002948 if (dirty) {
2949 err = ext4_handle_dirty_super(handle, sb);
2950 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2951 if (!err)
2952 err = rc;
2953 if (err) {
2954 /*
2955 * We have to remove inode from in-memory list if
2956 * addition to on disk orphan list failed. Stray orphan
2957 * list entries can cause panics at unmount time.
2958 */
2959 mutex_lock(&sbi->s_orphan_lock);
2960 list_del(&EXT4_I(inode)->i_orphan);
2961 mutex_unlock(&sbi->s_orphan_lock);
2962 }
2963 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2965 jbd_debug(4, "orphan inode %lu will point to %d\n",
2966 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002967out:
Jan Karacd2c0802014-05-26 11:39:17 -04002968 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 return err;
2970}
2971
2972/*
Mingming Cao617ba132006-10-11 01:20:53 -07002973 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974 * of such inodes stored on disk, because it is finally being cleaned up.
2975 */
Mingming Cao617ba132006-10-11 01:20:53 -07002976int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002977{
2978 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002979 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002980 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002981 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002982 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983 int err = 0;
2984
Jan Karacd2c0802014-05-26 11:39:17 -04002985 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002986 return 0;
2987
Jan Karad745a8c2014-05-26 11:56:53 -04002988 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2989 !mutex_is_locked(&inode->i_mutex));
2990 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002991 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002992 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002993
Jan Karad745a8c2014-05-26 11:56:53 -04002994 if (handle) {
2995 /* Grab inode buffer early before taking global s_orphan_lock */
2996 err = ext4_reserve_inode_write(handle, inode, &iloc);
2997 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998
Jan Karad745a8c2014-05-26 11:56:53 -04002999 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
3001
Jan Karad745a8c2014-05-26 11:56:53 -04003002 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003 list_del_init(&ei->i_orphan);
3004
3005 /* If we're on an error path, we may not have a valid
3006 * transaction handle with which to update the orphan list on
3007 * disk, but we still need to remove the inode from the linked
3008 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04003009 if (!handle || err) {
3010 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003011 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04003012 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003013
Jan Karad745a8c2014-05-26 11:56:53 -04003014 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003015 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003016 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003018 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003019 if (err) {
3020 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003022 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04003024 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04003025 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003027 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07003029 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003031 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003032 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07003033 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003034 if (err) {
3035 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003037 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003038 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003039 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003040 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041 }
3042 if (err)
3043 goto out_brelse;
3044 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003045 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003046out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07003047 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003048 return err;
3049
3050out_brelse:
3051 brelse(iloc.bh);
3052 goto out_err;
3053}
3054
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003055static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056{
3057 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003058 struct inode *inode;
3059 struct buffer_head *bh;
3060 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003061 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062
3063 /* Initialize quotas before so that eventual writes go in
3064 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05003065 dquot_initialize(dir);
David Howells2b0143b2015-03-17 22:25:59 +00003066 dquot_initialize(d_inode(dentry));
Christoph Hellwig907f4552010-03-03 09:05:06 -05003067
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003069 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003070 if (IS_ERR(bh))
3071 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003072 if (!bh)
3073 goto end_rmdir;
3074
David Howells2b0143b2015-03-17 22:25:59 +00003075 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076
3077 retval = -EIO;
3078 if (le32_to_cpu(de->inode) != inode->i_ino)
3079 goto end_rmdir;
3080
3081 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003082 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003083 goto end_rmdir;
3084
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003085 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003086 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003087 if (IS_ERR(handle)) {
3088 retval = PTR_ERR(handle);
3089 handle = NULL;
3090 goto end_rmdir;
3091 }
3092
3093 if (IS_DIRSYNC(dir))
3094 ext4_handle_sync(handle);
3095
Mingming Cao617ba132006-10-11 01:20:53 -07003096 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003097 if (retval)
3098 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003099 if (!EXT4_DIR_LINK_EMPTY(inode))
Eric Sandeen12062dd2010-02-15 14:19:27 -05003100 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003101 "empty directory has too many links (%d)",
3102 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 inode->i_version++;
3104 clear_nlink(inode);
3105 /* There's no need to set i_disksize: the fact that i_nlink is
3106 * zero will ensure that the right thing happens during any
3107 * recovery. */
3108 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003109 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003110 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003111 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003112 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003113 ext4_update_dx_flag(dir);
3114 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003115
3116end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003117 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003118 if (handle)
3119 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 return retval;
3121}
3122
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003123static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124{
3125 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003126 struct inode *inode;
3127 struct buffer_head *bh;
3128 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003129 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003131 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 /* Initialize quotas before so that eventual writes go
3133 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05003134 dquot_initialize(dir);
David Howells2b0143b2015-03-17 22:25:59 +00003135 dquot_initialize(d_inode(dentry));
Christoph Hellwig907f4552010-03-03 09:05:06 -05003136
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003138 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003139 if (IS_ERR(bh))
3140 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003141 if (!bh)
3142 goto end_unlink;
3143
David Howells2b0143b2015-03-17 22:25:59 +00003144 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003145
3146 retval = -EIO;
3147 if (le32_to_cpu(de->inode) != inode->i_ino)
3148 goto end_unlink;
3149
Theodore Ts'o931b6862013-02-09 09:43:39 -05003150 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003151 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05003152 if (IS_ERR(handle)) {
3153 retval = PTR_ERR(handle);
3154 handle = NULL;
3155 goto end_unlink;
3156 }
3157
3158 if (IS_DIRSYNC(dir))
3159 ext4_handle_sync(handle);
3160
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 if (!inode->i_nlink) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003162 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003163 "Deleting nonexistent file (%lu), %d",
3164 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003165 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166 }
Mingming Cao617ba132006-10-11 01:20:53 -07003167 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003168 if (retval)
3169 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04003170 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003171 ext4_update_dx_flag(dir);
3172 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003173 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003175 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003176 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003177 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003178
3179end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003180 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003181 if (handle)
3182 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003183 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184 return retval;
3185}
3186
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003187static int ext4_symlink(struct inode *dir,
3188 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189{
3190 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003191 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003192 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003193 int credits;
Theodore Ts'of348c252015-04-16 01:55:00 -04003194 bool encryption_required;
3195 struct ext4_str disk_link;
3196 struct ext4_encrypted_symlink_data *sd = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003197
Theodore Ts'of348c252015-04-16 01:55:00 -04003198 disk_link.len = len + 1;
3199 disk_link.name = (char *) symname;
3200
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04003201 encryption_required = (ext4_encrypted_inode(dir) ||
3202 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
Theodore Ts'of348c252015-04-16 01:55:00 -04003203 if (encryption_required)
3204 disk_link.len = encrypted_symlink_data_len(len) + 1;
3205 if (disk_link.len > dir->i_sb->s_blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003206 return -ENAMETOOLONG;
3207
Christoph Hellwig871a2932010-03-03 09:05:07 -05003208 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003209
Theodore Ts'of348c252015-04-16 01:55:00 -04003210 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003211 /*
3212 * For non-fast symlinks, we just allocate inode and put it on
3213 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003214 * group descriptor, sb, inode block, quota blocks, and
3215 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003216 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003217 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3218 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003219 } else {
3220 /*
3221 * Fast symlink. We have to add entry to directory
3222 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3223 * allocate new inode (bitmap, group descriptor, inode block,
3224 * quota blocks, sb is already counted in previous macros).
3225 */
3226 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003227 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003228 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003229
Theodore Ts'o11395752013-02-09 16:27:09 -05003230 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3231 &dentry->d_name, 0, NULL,
3232 EXT4_HT_DIR, credits);
3233 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003234 if (IS_ERR(inode)) {
3235 if (handle)
3236 ext4_journal_stop(handle);
3237 return PTR_ERR(inode);
3238 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239
Theodore Ts'of348c252015-04-16 01:55:00 -04003240 if (encryption_required) {
3241 struct ext4_fname_crypto_ctx *ctx = NULL;
3242 struct qstr istr;
3243 struct ext4_str ostr;
3244
3245 sd = kzalloc(disk_link.len, GFP_NOFS);
3246 if (!sd) {
3247 err = -ENOMEM;
3248 goto err_drop_inode;
3249 }
3250 err = ext4_inherit_context(dir, inode);
3251 if (err)
3252 goto err_drop_inode;
3253 ctx = ext4_get_fname_crypto_ctx(inode,
3254 inode->i_sb->s_blocksize);
3255 if (IS_ERR_OR_NULL(ctx)) {
3256 /* We just set the policy, so ctx should not be NULL */
3257 err = (ctx == NULL) ? -EIO : PTR_ERR(ctx);
3258 goto err_drop_inode;
3259 }
3260 istr.name = (const unsigned char *) symname;
3261 istr.len = len;
3262 ostr.name = sd->encrypted_path;
3263 err = ext4_fname_usr_to_disk(ctx, &istr, &ostr);
3264 ext4_put_fname_crypto_ctx(&ctx);
3265 if (err < 0)
3266 goto err_drop_inode;
3267 sd->len = cpu_to_le16(ostr.len);
3268 disk_link.name = (char *) sd;
3269 }
3270
3271 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003272 inode->i_op = &ext4_symlink_inode_operations;
3273 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003275 * We cannot call page_symlink() with transaction started
3276 * because it calls into ext4_write_begin() which can wait
3277 * for transaction commit if we are running out of space
3278 * and thus we deadlock. So we have to stop transaction now
3279 * and restart it when symlink contents is written.
3280 *
3281 * To keep fs consistent in case of crash, we have to put inode
3282 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003283 */
Jan Karadf5e6222011-05-03 11:12:58 -04003284 drop_nlink(inode);
3285 err = ext4_orphan_add(handle, inode);
3286 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003287 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003288 if (err)
3289 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003290 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003291 if (err)
3292 goto err_drop_inode;
3293 /*
3294 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3295 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3296 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003297 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003298 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3299 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3300 if (IS_ERR(handle)) {
3301 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003302 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003303 goto err_drop_inode;
3304 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003305 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003306 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003307 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003308 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003310 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003311 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Theodore Ts'of348c252015-04-16 01:55:00 -04003312 inode->i_op = encryption_required ?
3313 &ext4_symlink_inode_operations :
3314 &ext4_fast_symlink_inode_operations;
3315 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3316 disk_link.len);
3317 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318 }
Mingming Cao617ba132006-10-11 01:20:53 -07003319 EXT4_I(inode)->i_disksize = inode->i_size;
3320 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003321 if (!err && IS_DIRSYNC(dir))
3322 ext4_handle_sync(handle);
3323
Theodore Ts'o11395752013-02-09 16:27:09 -05003324 if (handle)
3325 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003326 kfree(sd);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003327 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04003328err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003329 if (handle)
3330 ext4_journal_stop(handle);
3331 kfree(sd);
3332 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003333 unlock_new_inode(inode);
3334 iput(inode);
3335 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336}
3337
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003338static int ext4_link(struct dentry *old_dentry,
3339 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003340{
3341 handle_t *handle;
David Howells2b0143b2015-03-17 22:25:59 +00003342 struct inode *inode = d_inode(old_dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 int err, retries = 0;
3344
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003345 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 return -EMLINK;
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003347 if (ext4_encrypted_inode(dir) &&
3348 !ext4_is_child_context_consistent_with_parent(dir, inode))
3349 return -EPERM;
Christoph Hellwig871a2932010-03-03 09:05:07 -05003350 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003351
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003352retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003353 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3354 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003355 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003356 if (IS_ERR(handle))
3357 return PTR_ERR(handle);
3358
3359 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003360 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361
Kalpak Shahef7f3832007-07-18 09:15:20 -04003362 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003363 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003364 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365
Al Viro6b38e842008-12-30 02:03:31 -05003366 err = ext4_add_entry(handle, dentry, inode);
3367 if (!err) {
3368 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003369 /* this can happen only for tmpfile being
3370 * linked the first time
3371 */
3372 if (inode->i_nlink == 1)
3373 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003374 d_instantiate(dentry, inode);
3375 } else {
3376 drop_nlink(inode);
3377 iput(inode);
3378 }
Mingming Cao617ba132006-10-11 01:20:53 -07003379 ext4_journal_stop(handle);
3380 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003381 goto retry;
3382 return err;
3383}
3384
Tao Ma32f7f222012-12-10 14:06:01 -05003385
3386/*
3387 * Try to find buffer head where contains the parent block.
3388 * It should be the inode block if it is inlined or the 1st block
3389 * if it is a normal dir.
3390 */
3391static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3392 struct inode *inode,
3393 int *retval,
3394 struct ext4_dir_entry_2 **parent_de,
3395 int *inlined)
3396{
3397 struct buffer_head *bh;
3398
3399 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003400 bh = ext4_read_dirblock(inode, 0, EITHER);
3401 if (IS_ERR(bh)) {
3402 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003403 return NULL;
3404 }
3405 *parent_de = ext4_next_entry(
3406 (struct ext4_dir_entry_2 *)bh->b_data,
3407 inode->i_sb->s_blocksize);
3408 return bh;
3409 }
3410
3411 *inlined = 1;
3412 return ext4_get_first_inline_block(inode, parent_de, retval);
3413}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003414
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003415struct ext4_renament {
3416 struct inode *dir;
3417 struct dentry *dentry;
3418 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003419 bool is_dir;
3420 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003421
3422 /* entry for "dentry" */
3423 struct buffer_head *bh;
3424 struct ext4_dir_entry_2 *de;
3425 int inlined;
3426
3427 /* entry for ".." in inode if it's a directory */
3428 struct buffer_head *dir_bh;
3429 struct ext4_dir_entry_2 *parent_de;
3430 int dir_inlined;
3431};
3432
Miklos Szeredibd1af142014-04-01 17:08:44 +02003433static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3434{
3435 int retval;
3436
3437 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3438 &retval, &ent->parent_de,
3439 &ent->dir_inlined);
3440 if (!ent->dir_bh)
3441 return retval;
3442 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3443 return -EIO;
3444 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3445 return ext4_journal_get_write_access(handle, ent->dir_bh);
3446}
3447
3448static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3449 unsigned dir_ino)
3450{
3451 int retval;
3452
3453 ent->parent_de->inode = cpu_to_le32(dir_ino);
3454 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3455 if (!ent->dir_inlined) {
3456 if (is_dx(ent->inode)) {
3457 retval = ext4_handle_dirty_dx_node(handle,
3458 ent->inode,
3459 ent->dir_bh);
3460 } else {
3461 retval = ext4_handle_dirty_dirent_node(handle,
3462 ent->inode,
3463 ent->dir_bh);
3464 }
3465 } else {
3466 retval = ext4_mark_inode_dirty(handle, ent->inode);
3467 }
3468 if (retval) {
3469 ext4_std_error(ent->dir->i_sb, retval);
3470 return retval;
3471 }
3472 return 0;
3473}
3474
3475static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3476 unsigned ino, unsigned file_type)
3477{
3478 int retval;
3479
3480 BUFFER_TRACE(ent->bh, "get write access");
3481 retval = ext4_journal_get_write_access(handle, ent->bh);
3482 if (retval)
3483 return retval;
3484 ent->de->inode = cpu_to_le32(ino);
3485 if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
3486 EXT4_FEATURE_INCOMPAT_FILETYPE))
3487 ent->de->file_type = file_type;
3488 ent->dir->i_version++;
3489 ent->dir->i_ctime = ent->dir->i_mtime =
3490 ext4_current_time(ent->dir);
3491 ext4_mark_inode_dirty(handle, ent->dir);
3492 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3493 if (!ent->inlined) {
3494 retval = ext4_handle_dirty_dirent_node(handle,
3495 ent->dir, ent->bh);
3496 if (unlikely(retval)) {
3497 ext4_std_error(ent->dir->i_sb, retval);
3498 return retval;
3499 }
3500 }
3501 brelse(ent->bh);
3502 ent->bh = NULL;
3503
3504 return 0;
3505}
3506
3507static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3508 const struct qstr *d_name)
3509{
3510 int retval = -ENOENT;
3511 struct buffer_head *bh;
3512 struct ext4_dir_entry_2 *de;
3513
3514 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003515 if (IS_ERR(bh))
3516 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003517 if (bh) {
3518 retval = ext4_delete_entry(handle, dir, de, bh);
3519 brelse(bh);
3520 }
3521 return retval;
3522}
3523
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003524static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3525 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003526{
3527 int retval;
3528 /*
3529 * ent->de could have moved from under us during htree split, so make
3530 * sure that we are deleting the right entry. We might also be pointing
3531 * to a stale entry in the unused part of ent->bh so just checking inum
3532 * and the name isn't enough.
3533 */
3534 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3535 ent->de->name_len != ent->dentry->d_name.len ||
3536 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003537 ent->de->name_len) ||
3538 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003539 retval = ext4_find_delete_entry(handle, ent->dir,
3540 &ent->dentry->d_name);
3541 } else {
3542 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3543 if (retval == -ENOENT) {
3544 retval = ext4_find_delete_entry(handle, ent->dir,
3545 &ent->dentry->d_name);
3546 }
3547 }
3548
3549 if (retval) {
3550 ext4_warning(ent->dir->i_sb,
3551 "Deleting old file (%lu), %d, error=%d",
3552 ent->dir->i_ino, ent->dir->i_nlink, retval);
3553 }
3554}
3555
Miklos Szeredibd429982014-04-01 17:08:44 +02003556static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3557{
3558 if (ent->dir_nlink_delta) {
3559 if (ent->dir_nlink_delta == -1)
3560 ext4_dec_count(handle, ent->dir);
3561 else
3562 ext4_inc_count(handle, ent->dir);
3563 ext4_mark_inode_dirty(handle, ent->dir);
3564 }
3565}
3566
Miklos Szeredicd808de2014-10-24 00:14:37 +02003567static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3568 int credits, handle_t **h)
3569{
3570 struct inode *wh;
3571 handle_t *handle;
3572 int retries = 0;
3573
3574 /*
3575 * for inode block, sb block, group summaries,
3576 * and inode bitmap
3577 */
3578 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3579 EXT4_XATTR_TRANS_BLOCKS + 4);
3580retry:
3581 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3582 &ent->dentry->d_name, 0, NULL,
3583 EXT4_HT_DIR, credits);
3584
3585 handle = ext4_journal_current_handle();
3586 if (IS_ERR(wh)) {
3587 if (handle)
3588 ext4_journal_stop(handle);
3589 if (PTR_ERR(wh) == -ENOSPC &&
3590 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3591 goto retry;
3592 } else {
3593 *h = handle;
3594 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3595 wh->i_op = &ext4_special_inode_operations;
3596 }
3597 return wh;
3598}
3599
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003600/*
3601 * Anybody can rename anything with this: the permission checks are left to the
3602 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003603 *
3604 * n.b. old_{dentry,inode) refers to the source dentry/inode
3605 * while new_{dentry,inode) refers to the destination dentry/inode
3606 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003607 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003608static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003609 struct inode *new_dir, struct dentry *new_dentry,
3610 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003611{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003612 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003613 struct ext4_renament old = {
3614 .dir = old_dir,
3615 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003616 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003617 };
3618 struct ext4_renament new = {
3619 .dir = new_dir,
3620 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003621 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003622 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003623 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003624 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003625 struct inode *whiteout = NULL;
3626 int credits;
3627 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003628
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003629 dquot_initialize(old.dir);
3630 dquot_initialize(new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003631
3632 /* Initialize quotas before so that eventual writes go
3633 * in separate transaction */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003634 if (new.inode)
3635 dquot_initialize(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003636
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003637 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003638 if (IS_ERR(old.bh))
3639 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003640 /*
3641 * Check for inode number is _not_ due to possible IO errors.
3642 * We might rmdir the source, keep it as pwd of some process
3643 * and merrily kill the link to whatever was created under the
3644 * same name. Goodbye sticky bit ;-<
3645 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003646 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003647 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003648 goto end_rename;
3649
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003650 if ((old.dir != new.dir) &&
3651 ext4_encrypted_inode(new.dir) &&
3652 !ext4_is_child_context_consistent_with_parent(new.dir,
3653 old.inode)) {
3654 retval = -EPERM;
3655 goto end_rename;
3656 }
3657
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003658 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3659 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003660 if (IS_ERR(new.bh)) {
3661 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003662 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003663 goto end_rename;
3664 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003665 if (new.bh) {
3666 if (!new.inode) {
3667 brelse(new.bh);
3668 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003669 }
3670 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003671 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3672 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003673
Miklos Szeredicd808de2014-10-24 00:14:37 +02003674 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3675 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3676 if (!(flags & RENAME_WHITEOUT)) {
3677 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003678 if (IS_ERR(handle)) {
3679 retval = PTR_ERR(handle);
3680 handle = NULL;
3681 goto end_rename;
3682 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003683 } else {
3684 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003685 if (IS_ERR(whiteout)) {
3686 retval = PTR_ERR(whiteout);
3687 whiteout = NULL;
3688 goto end_rename;
3689 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003690 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003691
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003692 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003693 ext4_handle_sync(handle);
3694
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003695 if (S_ISDIR(old.inode->i_mode)) {
3696 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003697 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003698 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003699 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003700 } else {
3701 retval = -EMLINK;
3702 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3703 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003704 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003705 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003706 if (retval)
3707 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003709 /*
3710 * If we're renaming a file within an inline_data dir and adding or
3711 * setting the new dirent causes a conversion from inline_data to
3712 * extents/blockmap, we need to force the dirent delete code to
3713 * re-read the directory, or else we end up trying to delete a dirent
3714 * from what is now the extent tree root (or a block map).
3715 */
3716 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3717 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003718
3719 old_file_type = old.de->file_type;
3720 if (whiteout) {
3721 /*
3722 * Do this before adding a new entry, so the old entry is sure
3723 * to be still pointing to the valid old entry.
3724 */
3725 retval = ext4_setent(handle, &old, whiteout->i_ino,
3726 EXT4_FT_CHRDEV);
3727 if (retval)
3728 goto end_rename;
3729 ext4_mark_inode_dirty(handle, whiteout);
3730 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003731 if (!new.bh) {
3732 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003733 if (retval)
3734 goto end_rename;
3735 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003736 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003737 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003738 if (retval)
3739 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003740 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003741 if (force_reread)
3742 force_reread = !ext4_test_inode_flag(new.dir,
3743 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003744
3745 /*
3746 * Like most other Unix systems, set the ctime for inodes on a
3747 * rename.
3748 */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003749 old.inode->i_ctime = ext4_current_time(old.inode);
3750 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003751
Miklos Szeredicd808de2014-10-24 00:14:37 +02003752 if (!whiteout) {
3753 /*
3754 * ok, that's it
3755 */
3756 ext4_rename_delete(handle, &old, force_reread);
3757 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003759 if (new.inode) {
3760 ext4_dec_count(handle, new.inode);
3761 new.inode->i_ctime = ext4_current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003762 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003763 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3764 ext4_update_dx_flag(old.dir);
3765 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003766 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3767 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003768 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003769
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003770 ext4_dec_count(handle, old.dir);
3771 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003772 /* checked ext4_empty_dir above, can't have another
3773 * parent, ext4_dec_count() won't work for many-linked
3774 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003775 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003776 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003777 ext4_inc_count(handle, new.dir);
3778 ext4_update_dx_flag(new.dir);
3779 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003780 }
3781 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003782 ext4_mark_inode_dirty(handle, old.dir);
3783 if (new.inode) {
3784 ext4_mark_inode_dirty(handle, new.inode);
3785 if (!new.inode->i_nlink)
3786 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003787 }
3788 retval = 0;
3789
3790end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003791 brelse(old.dir_bh);
3792 brelse(old.bh);
3793 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003794 if (whiteout) {
3795 if (retval)
3796 drop_nlink(whiteout);
3797 unlock_new_inode(whiteout);
3798 iput(whiteout);
3799 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003800 if (handle)
3801 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003802 return retval;
3803}
3804
Miklos Szeredibd429982014-04-01 17:08:44 +02003805static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3806 struct inode *new_dir, struct dentry *new_dentry)
3807{
3808 handle_t *handle = NULL;
3809 struct ext4_renament old = {
3810 .dir = old_dir,
3811 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003812 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003813 };
3814 struct ext4_renament new = {
3815 .dir = new_dir,
3816 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003817 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003818 };
3819 u8 new_file_type;
3820 int retval;
3821
3822 dquot_initialize(old.dir);
3823 dquot_initialize(new.dir);
3824
3825 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3826 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003827 if (IS_ERR(old.bh))
3828 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003829 /*
3830 * Check for inode number is _not_ due to possible IO errors.
3831 * We might rmdir the source, keep it as pwd of some process
3832 * and merrily kill the link to whatever was created under the
3833 * same name. Goodbye sticky bit ;-<
3834 */
3835 retval = -ENOENT;
3836 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3837 goto end_rename;
3838
3839 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3840 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003841 if (IS_ERR(new.bh)) {
3842 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003843 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003844 goto end_rename;
3845 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003846
3847 /* RENAME_EXCHANGE case: old *and* new must both exist */
3848 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3849 goto end_rename;
3850
3851 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3852 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3853 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003854 if (IS_ERR(handle)) {
3855 retval = PTR_ERR(handle);
3856 handle = NULL;
3857 goto end_rename;
3858 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003859
3860 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3861 ext4_handle_sync(handle);
3862
3863 if (S_ISDIR(old.inode->i_mode)) {
3864 old.is_dir = true;
3865 retval = ext4_rename_dir_prepare(handle, &old);
3866 if (retval)
3867 goto end_rename;
3868 }
3869 if (S_ISDIR(new.inode->i_mode)) {
3870 new.is_dir = true;
3871 retval = ext4_rename_dir_prepare(handle, &new);
3872 if (retval)
3873 goto end_rename;
3874 }
3875
3876 /*
3877 * Other than the special case of overwriting a directory, parents'
3878 * nlink only needs to be modified if this is a cross directory rename.
3879 */
3880 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3881 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3882 new.dir_nlink_delta = -old.dir_nlink_delta;
3883 retval = -EMLINK;
3884 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3885 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3886 goto end_rename;
3887 }
3888
3889 new_file_type = new.de->file_type;
3890 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3891 if (retval)
3892 goto end_rename;
3893
3894 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3895 if (retval)
3896 goto end_rename;
3897
3898 /*
3899 * Like most other Unix systems, set the ctime for inodes on a
3900 * rename.
3901 */
3902 old.inode->i_ctime = ext4_current_time(old.inode);
3903 new.inode->i_ctime = ext4_current_time(new.inode);
3904 ext4_mark_inode_dirty(handle, old.inode);
3905 ext4_mark_inode_dirty(handle, new.inode);
3906
3907 if (old.dir_bh) {
3908 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3909 if (retval)
3910 goto end_rename;
3911 }
3912 if (new.dir_bh) {
3913 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3914 if (retval)
3915 goto end_rename;
3916 }
3917 ext4_update_dir_count(handle, &old);
3918 ext4_update_dir_count(handle, &new);
3919 retval = 0;
3920
3921end_rename:
3922 brelse(old.dir_bh);
3923 brelse(new.dir_bh);
3924 brelse(old.bh);
3925 brelse(new.bh);
3926 if (handle)
3927 ext4_journal_stop(handle);
3928 return retval;
3929}
3930
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003931static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3932 struct inode *new_dir, struct dentry *new_dentry,
3933 unsigned int flags)
3934{
Miklos Szeredicd808de2014-10-24 00:14:37 +02003935 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003936 return -EINVAL;
3937
Miklos Szeredibd429982014-04-01 17:08:44 +02003938 if (flags & RENAME_EXCHANGE) {
3939 return ext4_cross_rename(old_dir, old_dentry,
3940 new_dir, new_dentry);
3941 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003942
3943 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003944}
3945
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946/*
3947 * directories can handle most operations...
3948 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003949const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003950 .create = ext4_create,
3951 .lookup = ext4_lookup,
3952 .link = ext4_link,
3953 .unlink = ext4_unlink,
3954 .symlink = ext4_symlink,
3955 .mkdir = ext4_mkdir,
3956 .rmdir = ext4_rmdir,
3957 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003958 .tmpfile = ext4_tmpfile,
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003959 .rename2 = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003960 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003961 .setxattr = generic_setxattr,
3962 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003963 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003965 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003966 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003967 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003968};
3969
Arjan van de Ven754661f2007-02-12 00:55:38 -08003970const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003971 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003972 .setxattr = generic_setxattr,
3973 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003974 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003975 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003976 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003977 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003978};