blob: 5ea737114716c65f150d74aff29ba9110d4cbd39 [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 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400643 res = ext4_fname_disk_to_usr(ctx, NULL, de,
Michael Halcrowb3098482015-04-12 01:07:01 -0400644 &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 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400656 ext4fs_dirhash(de->name, de->name_len,
657 &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400658 printk("%*.s:(E)%x.%u ", len, name,
659 h.hash, (unsigned) ((char *) de
660 - base));
661 ext4_put_fname_crypto_ctx(&ctx);
662 ext4_fname_crypto_free_buffer(
663 &fname_crypto_str);
664 }
665#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666 int len = de->name_len;
667 char *name = de->name;
Mingming Cao617ba132006-10-11 01:20:53 -0700668 ext4fs_dirhash(de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400669 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400670 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400671#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 }
Mingming Cao617ba132006-10-11 01:20:53 -0700673 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 names++;
675 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500676 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677 }
678 printk("(%i)\n", names);
679 return (struct stats) { names, space, 1 };
680}
681
682struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
683 struct dx_entry *entries, int levels)
684{
685 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400686 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687 unsigned bcount = 0;
688 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 printk("%i indexed blocks...\n", count);
690 for (i = 0; i < count; i++, entries++)
691 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500692 ext4_lblk_t block = dx_get_block(entries);
693 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
695 struct stats stats;
696 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400697 bh = ext4_bread(NULL,dir, block, 0);
698 if (!bh || IS_ERR(bh))
699 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 stats = levels?
701 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400702 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
703 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 names += stats.names;
705 space += stats.space;
706 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400707 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 }
709 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400710 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400711 levels ? "" : " ", names, space/bcount,
712 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 return (struct stats) { names, space, bcount};
714}
715#endif /* DX_DEBUG */
716
717/*
718 * Probe for a directory leaf block to search.
719 *
720 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
721 * error in the directory index, and the caller should fall back to
722 * searching the directory normally. The callers of dx_probe **MUST**
723 * check for this error code, and make sure it never gets reflected
724 * back to userspace.
725 */
726static struct dx_frame *
Theodore Ts'of702ba02008-09-22 15:21:01 -0400727dx_probe(const struct qstr *d_name, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400728 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729{
730 unsigned count, indirect;
731 struct dx_entry *at, *entries, *p, *q, *m;
732 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400734 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735 u32 hash;
736
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400737 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
738 if (IS_ERR(frame->bh))
739 return (struct dx_frame *) frame->bh;
740
741 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 if (root->info.hash_version != DX_HASH_TEA &&
743 root->info.hash_version != DX_HASH_HALF_MD4 &&
744 root->info.hash_version != DX_HASH_LEGACY) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500745 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 goto fail;
748 }
749 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400750 if (hinfo->hash_version <= DX_HASH_TEA)
751 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700752 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrowb3098482015-04-12 01:07:01 -0400753#ifdef CONFIG_EXT4_FS_ENCRYPTION
754 if (d_name) {
755 struct ext4_fname_crypto_ctx *ctx = NULL;
756 int res;
757
758 /* Check if the directory is encrypted */
759 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
760 if (IS_ERR(ctx)) {
761 ret_err = ERR_PTR(PTR_ERR(ctx));
762 goto fail;
763 }
764 res = ext4_fname_usr_to_hash(ctx, d_name, hinfo);
765 if (res < 0) {
766 ret_err = ERR_PTR(res);
767 goto fail;
768 }
769 ext4_put_fname_crypto_ctx(&ctx);
770 }
771#else
Theodore Ts'of702ba02008-09-22 15:21:01 -0400772 if (d_name)
773 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Michael Halcrowb3098482015-04-12 01:07:01 -0400774#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 hash = hinfo->hash;
776
777 if (root->info.unused_flags & 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500778 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 goto fail;
781 }
782
783 if ((indirect = root->info.indirect_levels) > 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500784 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 root->info.indirect_levels);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 goto fail;
787 }
788
789 entries = (struct dx_entry *) (((char *)&root->info) +
790 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700791
792 if (dx_get_limit(entries) != dx_root_limit(dir,
793 root->info.info_length)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500794 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700795 goto fail;
796 }
797
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400798 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400799 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700801 if (!count || count > dx_get_limit(entries)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500802 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700803 "dx entry: no count or count > limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400804 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700805 }
806
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807 p = entries + 1;
808 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400809 while (p <= q) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 m = p + (q - p)/2;
811 dxtrace(printk("."));
812 if (dx_get_hash(m) > hash)
813 q = m - 1;
814 else
815 p = m + 1;
816 }
817
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400818 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 unsigned n = count - 1;
820 at = entries;
821 while (n--)
822 {
823 dxtrace(printk(","));
824 if (dx_get_hash(++at) > hash)
825 {
826 at--;
827 break;
828 }
829 }
830 assert (at == p - 1);
831 }
832
833 at = p - 1;
834 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700835 frame->entries = entries;
836 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400837 if (!indirect--)
838 return frame;
839 frame++;
840 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
841 if (IS_ERR(frame->bh)) {
842 ret_err = (struct dx_frame *) frame->bh;
843 frame->bh = NULL;
844 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400845 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400846 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400847
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700848 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500849 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700850 "dx entry: limit != node limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400851 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700852 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400854fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 while (frame >= frame_in) {
856 brelse(frame->bh);
857 frame--;
858 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400859
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400860 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500861 ext4_warning(dir->i_sb,
Zheng Liu9ee49302012-02-20 23:09:36 -0500862 "Corrupt dir inode %lu, running e2fsck is "
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700863 "recommended.", dir->i_ino);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400864 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865}
866
867static void dx_release (struct dx_frame *frames)
868{
869 if (frames[0].bh == NULL)
870 return;
871
872 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
873 brelse(frames[1].bh);
874 brelse(frames[0].bh);
875}
876
877/*
878 * This function increments the frame pointer to search the next leaf
879 * block, and reads in the necessary intervening nodes if the search
880 * should be necessary. Whether or not the search is necessary is
881 * controlled by the hash parameter. If the hash value is even, then
882 * the search is only continued if the next block starts with that
883 * hash value. This is used if we are searching for a specific file.
884 *
885 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
886 *
887 * This function returns 1 if the caller should continue to search,
888 * or 0 if it should not. If there is an error reading one of the
889 * index blocks, it will a negative error code.
890 *
891 * If start_hash is non-null, it will be filled in with the starting
892 * hash of the next page.
893 */
Mingming Cao617ba132006-10-11 01:20:53 -0700894static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 struct dx_frame *frame,
896 struct dx_frame *frames,
897 __u32 *start_hash)
898{
899 struct dx_frame *p;
900 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500901 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 __u32 bhash;
903
904 p = frame;
905 /*
906 * Find the next leaf page by incrementing the frame pointer.
907 * If we run out of entries in the interior node, loop around and
908 * increment pointer in the parent node. When we break out of
909 * this loop, num_frames indicates the number of interior
910 * nodes need to be read.
911 */
912 while (1) {
913 if (++(p->at) < p->entries + dx_get_count(p->entries))
914 break;
915 if (p == frames)
916 return 0;
917 num_frames++;
918 p--;
919 }
920
921 /*
922 * If the hash is 1, then continue only if the next page has a
923 * continuation hash of any value. This is used for readdir
924 * handling. Otherwise, check to see if the hash matches the
925 * desired contiuation hash. If it doesn't, return since
926 * there's no point to read in the successive index pages.
927 */
928 bhash = dx_get_hash(p->at);
929 if (start_hash)
930 *start_hash = bhash;
931 if ((hash & 1) == 0) {
932 if ((bhash & ~1) != hash)
933 return 0;
934 }
935 /*
936 * If the hash is HASH_NB_ALWAYS, we always go to the next
937 * block so no check is necessary
938 */
939 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500940 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
941 if (IS_ERR(bh))
942 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400944 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945 p->bh = bh;
946 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
947 }
948 return 1;
949}
950
951
952/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 * This function fills a red-black tree with information from a
954 * directory block. It returns the number directory entries loaded
955 * into the tree. If there is an error it is returned in err.
956 */
957static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500958 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 struct dx_hash_info *hinfo,
960 __u32 start_hash, __u32 start_minor_hash)
961{
962 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700963 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400964 int err = 0, count = 0;
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400965 struct ext4_fname_crypto_ctx *ctx = NULL;
966 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500968 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
969 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500970 bh = ext4_read_dirblock(dir, block, DIRENT);
971 if (IS_ERR(bh))
972 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400973
Mingming Cao617ba132006-10-11 01:20:53 -0700974 de = (struct ext4_dir_entry_2 *) bh->b_data;
975 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700977 EXT4_DIR_REC_LEN(0));
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400978#ifdef CONFIG_EXT4_FS_ENCRYPTION
979 /* Check if the directory is encrypted */
980 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
981 if (IS_ERR(ctx)) {
982 err = PTR_ERR(ctx);
983 brelse(bh);
984 return err;
985 }
986 if (ctx != NULL) {
987 err = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
988 &fname_crypto_str);
989 if (err < 0) {
990 ext4_put_fname_crypto_ctx(&ctx);
991 brelse(bh);
992 return err;
993 }
994 }
995#endif
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500996 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -0500997 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -0500998 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -0500999 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1000 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001001 /* silently ignore the rest of the block */
1002 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001003 }
Mingming Cao617ba132006-10-11 01:20:53 -07001004 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 if ((hinfo->hash < start_hash) ||
1006 ((hinfo->hash == start_hash) &&
1007 (hinfo->minor_hash < start_minor_hash)))
1008 continue;
1009 if (de->inode == 0)
1010 continue;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001011 if (ctx == NULL) {
1012 /* Directory is not encrypted */
1013 tmp_str.name = de->name;
1014 tmp_str.len = de->name_len;
1015 err = ext4_htree_store_dirent(dir_file,
1016 hinfo->hash, hinfo->minor_hash, de,
1017 &tmp_str);
1018 } else {
1019 /* Directory is encrypted */
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -04001020 err = ext4_fname_disk_to_usr(ctx, hinfo, de,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001021 &fname_crypto_str);
1022 if (err < 0) {
1023 count = err;
1024 goto errout;
1025 }
1026 err = ext4_htree_store_dirent(dir_file,
1027 hinfo->hash, hinfo->minor_hash, de,
1028 &fname_crypto_str);
1029 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001030 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001031 count = err;
1032 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033 }
1034 count++;
1035 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001036errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 brelse(bh);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001038#ifdef CONFIG_EXT4_FS_ENCRYPTION
1039 ext4_put_fname_crypto_ctx(&ctx);
1040 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1041#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042 return count;
1043}
1044
1045
1046/*
1047 * This function fills a red-black tree with information from a
1048 * directory. We start scanning the directory in hash order, starting
1049 * at start_hash and start_minor_hash.
1050 *
1051 * This function returns the number of entries inserted into the tree,
1052 * or a negative error code.
1053 */
Mingming Cao617ba132006-10-11 01:20:53 -07001054int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 __u32 start_minor_hash, __u32 *next_hash)
1056{
1057 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001058 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 struct dx_frame frames[2], *frame;
1060 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001061 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001063 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 __u32 hashval;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001065 struct ext4_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066
Theodore Ts'o60e66792010-05-17 07:00:00 -04001067 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001068 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001069 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001070 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001071 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001072 if (hinfo.hash_version <= DX_HASH_TEA)
1073 hinfo.hash_version +=
1074 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001075 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001076 if (ext4_has_inline_data(dir)) {
1077 int has_inline_data = 1;
1078 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1079 &hinfo, start_hash,
1080 start_minor_hash,
1081 &has_inline_data);
1082 if (has_inline_data) {
1083 *next_hash = ~0;
1084 return count;
1085 }
1086 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1088 start_hash, start_minor_hash);
1089 *next_hash = ~0;
1090 return count;
1091 }
1092 hinfo.hash = start_hash;
1093 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001094 frame = dx_probe(NULL, dir, &hinfo, frames);
1095 if (IS_ERR(frame))
1096 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097
1098 /* Add '.' and '..' from the htree header */
1099 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001100 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001101 tmp_str.name = de->name;
1102 tmp_str.len = de->name_len;
1103 err = ext4_htree_store_dirent(dir_file, 0, 0,
1104 de, &tmp_str);
1105 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 goto errout;
1107 count++;
1108 }
1109 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001110 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001111 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001112 tmp_str.name = de->name;
1113 tmp_str.len = de->name_len;
1114 err = ext4_htree_store_dirent(dir_file, 2, 0,
1115 de, &tmp_str);
1116 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 goto errout;
1118 count++;
1119 }
1120
1121 while (1) {
1122 block = dx_get_block(frame->at);
1123 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1124 start_hash, start_minor_hash);
1125 if (ret < 0) {
1126 err = ret;
1127 goto errout;
1128 }
1129 count += ret;
1130 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001131 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 frame, frames, &hashval);
1133 *next_hash = hashval;
1134 if (ret < 0) {
1135 err = ret;
1136 goto errout;
1137 }
1138 /*
1139 * Stop if: (a) there are no more entries, or
1140 * (b) we have inserted at least one entry and the
1141 * next hash value is not a continuation
1142 */
1143 if ((ret == 0) ||
1144 (count && ((hashval & 1) == 0)))
1145 break;
1146 }
1147 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001148 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1149 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 return count;
1151errout:
1152 dx_release(frames);
1153 return (err);
1154}
1155
Tao Ma7335cd32012-12-10 14:05:59 -05001156static inline int search_dirblock(struct buffer_head *bh,
1157 struct inode *dir,
1158 const struct qstr *d_name,
1159 unsigned int offset,
1160 struct ext4_dir_entry_2 **res_dir)
1161{
1162 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1163 d_name, offset, res_dir);
1164}
1165
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166/*
1167 * Directory block splitting, compacting
1168 */
1169
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001170/*
1171 * Create map of hash values, offsets, and sizes, stored at end of block.
1172 * Returns number of entries mapped.
1173 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001174static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1175 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001176 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177{
1178 int count = 0;
1179 char *base = (char *) de;
1180 struct dx_hash_info h = *hinfo;
1181
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001182 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001184 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 map_tail--;
1186 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001187 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001188 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 count++;
1190 cond_resched();
1191 }
1192 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001193 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 }
1195 return count;
1196}
1197
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001198/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1200{
Andrew Morton63f57932006-10-11 01:21:24 -07001201 struct dx_map_entry *p, *q, *top = map + count - 1;
1202 int more;
1203 /* Combsort until bubble sort doesn't suck */
1204 while (count > 2) {
1205 count = count*10/13;
1206 if (count - 9 < 2) /* 9, 10 -> 11 */
1207 count = 11;
1208 for (p = top, q = p - count; q >= map; p--, q--)
1209 if (p->hash < q->hash)
1210 swap(*p, *q);
1211 }
1212 /* Garden variety bubble sort */
1213 do {
1214 more = 0;
1215 q = top;
1216 while (q-- > map) {
1217 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001219 swap(*(q+1), *q);
1220 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 }
1222 } while(more);
1223}
1224
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001225static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226{
1227 struct dx_entry *entries = frame->entries;
1228 struct dx_entry *old = frame->at, *new = old + 1;
1229 int count = dx_get_count(entries);
1230
1231 assert(count < dx_get_limit(entries));
1232 assert(old < entries + count);
1233 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1234 dx_set_hash(new, hash);
1235 dx_set_block(new, block);
1236 dx_set_count(entries, count + 1);
1237}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239/*
Mingming Cao617ba132006-10-11 01:20:53 -07001240 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 *
Mingming Cao617ba132006-10-11 01:20:53 -07001242 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243 * `de != NULL' is guaranteed by caller.
1244 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001245static inline int ext4_match(struct ext4_fname_crypto_ctx *ctx,
1246 struct ext4_str *fname_crypto_str,
1247 int len, const char * const name,
1248 struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249{
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001250 int res;
1251
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 if (!de->inode)
1253 return 0;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001254
1255#ifdef CONFIG_EXT4_FS_ENCRYPTION
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -04001256 if (ctx)
1257 return ext4_fname_match(ctx, fname_crypto_str, len, name, de);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001258#endif
1259 if (len != de->name_len)
1260 return 0;
1261 res = memcmp(name, de->name, len);
1262 return (res == 0) ? 1 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263}
1264
1265/*
1266 * Returns 0 if not found, -1 on failure, and 1 on success
1267 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001268int search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1269 struct inode *dir, const struct qstr *d_name,
1270 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271{
Mingming Cao617ba132006-10-11 01:20:53 -07001272 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001273 char * dlimit;
1274 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001275 const char *name = d_name->name;
1276 int namelen = d_name->len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001277 struct ext4_fname_crypto_ctx *ctx = NULL;
1278 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1279 int res;
1280
1281 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1282 if (IS_ERR(ctx))
1283 return -1;
1284
Tao Ma7335cd32012-12-10 14:05:59 -05001285 de = (struct ext4_dir_entry_2 *)search_buf;
1286 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 while ((char *) de < dlimit) {
1288 /* this code is executed quadratically often */
1289 /* do minimal checking `by hand' */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001290 if ((char *) de + de->name_len <= dlimit) {
1291 res = ext4_match(ctx, &fname_crypto_str, namelen,
1292 name, de);
1293 if (res < 0) {
1294 res = -1;
1295 goto return_result;
1296 }
1297 if (res > 0) {
1298 /* found a match - just to be sure, do
1299 * a full check */
1300 if (ext4_check_dir_entry(dir, NULL, de, bh,
1301 bh->b_data,
1302 bh->b_size, offset)) {
1303 res = -1;
1304 goto return_result;
1305 }
1306 *res_dir = de;
1307 res = 1;
1308 goto return_result;
1309 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311 }
1312 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001313 de_len = ext4_rec_len_from_disk(de->rec_len,
1314 dir->i_sb->s_blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001315 if (de_len <= 0) {
1316 res = -1;
1317 goto return_result;
1318 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001320 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001322
1323 res = 0;
1324return_result:
1325 ext4_put_fname_crypto_ctx(&ctx);
1326 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1327 return res;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001328}
1329
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001330static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1331 struct ext4_dir_entry *de)
1332{
1333 struct super_block *sb = dir->i_sb;
1334
1335 if (!is_dx(dir))
1336 return 0;
1337 if (block == 0)
1338 return 1;
1339 if (de->inode == 0 &&
1340 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1341 sb->s_blocksize)
1342 return 1;
1343 return 0;
1344}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345
1346/*
Mingming Cao617ba132006-10-11 01:20:53 -07001347 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 *
1349 * finds an entry in the specified directory with the wanted name. It
1350 * returns the cache buffer in which the entry was found, and the entry
1351 * itself (as a parameter - res_dir). It does NOT read the inode of the
1352 * entry - you'll have to do that yourself if you want to.
1353 *
1354 * The returned buffer_head has ->b_count elevated. The caller is expected
1355 * to brelse() it when appropriate.
1356 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001357static struct buffer_head * ext4_find_entry (struct inode *dir,
1358 const struct qstr *d_name,
Tao Ma32f7f222012-12-10 14:06:01 -05001359 struct ext4_dir_entry_2 **res_dir,
1360 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001362 struct super_block *sb;
1363 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1364 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001365 ext4_lblk_t start, block, b;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001366 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001367 int ra_max = 0; /* Number of bh's in the readahead
1368 buffer, bh_use[] */
1369 int ra_ptr = 0; /* Current index into readahead
1370 buffer */
1371 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001372 ext4_lblk_t nblocks;
Theodore Ts'o10560082014-08-29 20:51:32 -04001373 int i, namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374
1375 *res_dir = NULL;
1376 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001377 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001378 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001380
1381 if (ext4_has_inline_data(dir)) {
1382 int has_inline_data = 1;
1383 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1384 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001385 if (has_inline_data) {
1386 if (inlined)
1387 *inlined = 1;
Tao Mae8e948e2012-12-10 14:06:00 -05001388 return ret;
Tao Ma32f7f222012-12-10 14:06:01 -05001389 }
Tao Mae8e948e2012-12-10 14:06:00 -05001390 }
1391
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001392 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001393 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001394 /*
1395 * "." or ".." will only be in the first block
1396 * NFS may look up ".."; "." should be handled by the VFS
1397 */
1398 block = start = 0;
1399 nblocks = 1;
1400 goto restart;
1401 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001402 if (is_dx(dir)) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001403 bh = ext4_dx_find_entry(dir, d_name, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001404 /*
1405 * On success, or if the error was file not found,
1406 * return. Otherwise, fall back to doing a search the
1407 * old fashioned way.
1408 */
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001409 if (!IS_ERR(bh) || PTR_ERR(bh) != ERR_BAD_DX_DIR)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001410 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001411 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1412 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 }
Mingming Cao617ba132006-10-11 01:20:53 -07001414 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1415 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001416 if (start >= nblocks)
1417 start = 0;
1418 block = start;
1419restart:
1420 do {
1421 /*
1422 * We deal with the read-ahead logic here.
1423 */
1424 if (ra_ptr >= ra_max) {
1425 /* Refill the readahead buffer */
1426 ra_ptr = 0;
1427 b = block;
1428 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1429 /*
1430 * Terminate if we reach the end of the
1431 * directory and must wrap, or if our
1432 * search has finished at this block.
1433 */
1434 if (b >= nblocks || (num && block == start)) {
1435 bh_use[ra_max] = NULL;
1436 break;
1437 }
1438 num++;
Theodore Ts'o10560082014-08-29 20:51:32 -04001439 bh = ext4_getblk(NULL, dir, b++, 0);
1440 if (unlikely(IS_ERR(bh))) {
Theodore Ts'o36de9282014-08-23 17:47:19 -04001441 if (ra_max == 0)
Theodore Ts'o10560082014-08-29 20:51:32 -04001442 return bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04001443 break;
1444 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445 bh_use[ra_max] = bh;
1446 if (bh)
Christoph Hellwig65299a32011-08-23 14:50:29 +02001447 ll_rw_block(READ | REQ_META | REQ_PRIO,
1448 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449 }
1450 }
1451 if ((bh = bh_use[ra_ptr++]) == NULL)
1452 goto next;
1453 wait_on_buffer(bh);
1454 if (!buffer_uptodate(bh)) {
1455 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001456 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1457 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 brelse(bh);
1459 goto next;
1460 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001461 if (!buffer_verified(bh) &&
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001462 !is_dx_internal_node(dir, block,
1463 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001464 !ext4_dirent_csum_verify(dir,
1465 (struct ext4_dir_entry *)bh->b_data)) {
1466 EXT4_ERROR_INODE(dir, "checksumming directory "
1467 "block %lu", (unsigned long)block);
1468 brelse(bh);
1469 goto next;
1470 }
1471 set_buffer_verified(bh);
Theodore Ts'of702ba02008-09-22 15:21:01 -04001472 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001473 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001475 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 ret = bh;
1477 goto cleanup_and_exit;
1478 } else {
1479 brelse(bh);
1480 if (i < 0)
1481 goto cleanup_and_exit;
1482 }
1483 next:
1484 if (++block >= nblocks)
1485 block = 0;
1486 } while (block != start);
1487
1488 /*
1489 * If the directory has grown while we were searching, then
1490 * search the last part of the directory before giving up.
1491 */
1492 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001493 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001494 if (block < nblocks) {
1495 start = 0;
1496 goto restart;
1497 }
1498
1499cleanup_and_exit:
1500 /* Clean up the read-ahead blocks */
1501 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001502 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503 return ret;
1504}
1505
Theodore Ts'of702ba02008-09-22 15:21:01 -04001506static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001507 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001509 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 struct dx_hash_info hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001513 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001514 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001515
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001516#ifdef CONFIG_EXT4_FS_ENCRYPTION
1517 *res_dir = NULL;
1518#endif
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001519 frame = dx_probe(d_name, dir, &hinfo, frames);
1520 if (IS_ERR(frame))
1521 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 do {
1523 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001524 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001525 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001527
Theodore Ts'o7845c042010-10-27 21:30:08 -04001528 retval = search_dirblock(bh, dir, d_name,
1529 block << EXT4_BLOCK_SIZE_BITS(sb),
1530 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001531 if (retval == 1)
1532 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001533 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001534 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001535 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001536 goto errout;
1537 }
1538
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539 /* Check to see if we should continue to search */
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001540 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541 frames, NULL);
1542 if (retval < 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001543 ext4_warning(sb,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001544 "error %d reading index page in directory #%lu",
1545 retval, dir->i_ino);
1546 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 goto errout;
1548 }
1549 } while (retval == 1);
1550
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001551 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552errout:
Bernd Schubert265c6a02011-07-16 19:41:23 -04001553 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001554success:
1555 dx_release(frames);
1556 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001557}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558
Al Viro00cd8dd2012-06-10 17:13:09 -04001559static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001561 struct inode *inode;
1562 struct ext4_dir_entry_2 *de;
1563 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564
Mingming Cao617ba132006-10-11 01:20:53 -07001565 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001566 return ERR_PTR(-ENAMETOOLONG);
1567
Tao Ma32f7f222012-12-10 14:06:01 -05001568 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001569 if (IS_ERR(bh))
1570 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 inode = NULL;
1572 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001573 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001574 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001575 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001576 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001577 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001578 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001579 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001580 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1581 dentry);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001582 return ERR_PTR(-EIO);
1583 }
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001584 inode = ext4_iget_normal(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001585 if (inode == ERR_PTR(-ESTALE)) {
1586 EXT4_ERROR_INODE(dir,
1587 "deleted inode referenced: %u",
1588 ino);
1589 return ERR_PTR(-EIO);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001590 }
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001591 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
1592 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1593 S_ISLNK(inode->i_mode)) &&
1594 !ext4_is_child_context_consistent_with_parent(dir,
1595 inode)) {
1596 iput(inode);
1597 ext4_warning(inode->i_sb,
1598 "Inconsistent encryption contexts: %lu/%lu\n",
1599 (unsigned long) dir->i_ino,
1600 (unsigned long) inode->i_ino);
1601 return ERR_PTR(-EPERM);
1602 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 }
1604 return d_splice_alias(inode, dentry);
1605}
1606
1607
Mingming Cao617ba132006-10-11 01:20:53 -07001608struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001610 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001611 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001612 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 struct buffer_head *bh;
1614
Tao Ma32f7f222012-12-10 14:06:01 -05001615 bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001616 if (IS_ERR(bh))
1617 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 if (!bh)
1619 return ERR_PTR(-ENOENT);
1620 ino = le32_to_cpu(de->inode);
1621 brelse(bh);
1622
Mingming Cao617ba132006-10-11 01:20:53 -07001623 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001624 EXT4_ERROR_INODE(child->d_inode,
1625 "bad parent inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001626 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001627 }
1628
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001629 return d_obtain_alias(ext4_iget_normal(child->d_inode->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630}
1631
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001632/*
1633 * Move count entries from end of map between two memory locations.
1634 * Returns pointer to last entry moved.
1635 */
Mingming Cao617ba132006-10-11 01:20:53 -07001636static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001637dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1638 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639{
1640 unsigned rec_len = 0;
1641
1642 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001643 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001644 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001645 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001647 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001648 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 de->inode = 0;
1650 map++;
1651 to += rec_len;
1652 }
Mingming Cao617ba132006-10-11 01:20:53 -07001653 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654}
1655
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001656/*
1657 * Compact each dir entry in the range to the minimal rec_len.
1658 * Returns pointer to last entry in range.
1659 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001660static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001661{
Mingming Cao617ba132006-10-11 01:20:53 -07001662 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001663 unsigned rec_len = 0;
1664
1665 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001666 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001667 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001668 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001669 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001670 if (de > to)
1671 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001672 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001674 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675 }
1676 de = next;
1677 }
1678 return prev;
1679}
1680
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001681/*
1682 * Split a full leaf block to make room for a new dir entry.
1683 * Allocate a new block, and move entries so that they are approx. equally full.
1684 * Returns pointer to de in block into which the new entry will be inserted.
1685 */
Mingming Cao617ba132006-10-11 01:20:53 -07001686static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001688 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689{
1690 unsigned blocksize = dir->i_sb->s_blocksize;
1691 unsigned count, continued;
1692 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001693 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 u32 hash2;
1695 struct dx_map_entry *map;
1696 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001697 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001698 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001699 struct ext4_dir_entry_tail *t;
1700 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001701 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001703 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001704 csum_size = sizeof(struct ext4_dir_entry_tail);
1705
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001706 bh2 = ext4_append(handle, dir, &newblock);
1707 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708 brelse(*bh);
1709 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001710 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711 }
1712
1713 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001714 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001715 if (err)
1716 goto journal_error;
1717
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001719 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 if (err)
1721 goto journal_error;
1722
1723 data2 = bh2->b_data;
1724
1725 /* create map in the end of data2 block */
1726 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001727 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 blocksize, hinfo, map);
1729 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001730 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001731 /* Split the existing block in the middle, size-wise */
1732 size = 0;
1733 move = 0;
1734 for (i = count-1; i >= 0; i--) {
1735 /* is more than half of this entry in 2nd half of the block? */
1736 if (size + map[i].size/2 > blocksize/2)
1737 break;
1738 size += map[i].size;
1739 move++;
1740 }
1741 /* map index at which we will split */
1742 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 hash2 = map[split].hash;
1744 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001745 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1746 (unsigned long)dx_get_block(frame->at),
1747 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748
1749 /* Fancy dance to stay within two buffers */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001750 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1751 blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001752 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001753 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1754 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001755 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001756 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1757 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001758 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001759 if (csum_size) {
1760 t = EXT4_DIRENT_TAIL(data2, blocksize);
1761 initialize_dirent_tail(t, blocksize);
1762
1763 t = EXT4_DIRENT_TAIL(data1, blocksize);
1764 initialize_dirent_tail(t, blocksize);
1765 }
1766
Michael Halcrowb3098482015-04-12 01:07:01 -04001767 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1768 blocksize, 1));
1769 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1770 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771
1772 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001773 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 swap(*bh, bh2);
1775 de = de2;
1776 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001777 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001778 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 if (err)
1780 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001781 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001782 if (err)
1783 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001784 brelse(bh2);
1785 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001786 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001787
1788journal_error:
1789 brelse(*bh);
1790 brelse(bh2);
1791 *bh = NULL;
1792 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001793 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001794}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795
Tao Ma978fef92012-12-10 14:05:58 -05001796int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1797 struct buffer_head *bh,
1798 void *buf, int buf_size,
1799 const char *name, int namelen,
1800 struct ext4_dir_entry_2 **dest_de)
1801{
1802 struct ext4_dir_entry_2 *de;
1803 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1804 int nlen, rlen;
1805 unsigned int offset = 0;
1806 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001807 struct ext4_fname_crypto_ctx *ctx = NULL;
1808 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1809 int res;
1810
1811 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1812 if (IS_ERR(ctx))
1813 return -1;
1814
1815 if (ctx != NULL) {
1816 /* Calculate record length needed to store the entry */
1817 res = ext4_fname_crypto_namelen_on_disk(ctx, namelen);
1818 if (res < 0) {
1819 ext4_put_fname_crypto_ctx(&ctx);
1820 return res;
1821 }
1822 reclen = EXT4_DIR_REC_LEN(res);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001823 }
Tao Ma978fef92012-12-10 14:05:58 -05001824
1825 de = (struct ext4_dir_entry_2 *)buf;
1826 top = buf + buf_size - reclen;
1827 while ((char *) de <= top) {
1828 if (ext4_check_dir_entry(dir, NULL, de, bh,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001829 buf, buf_size, offset)) {
1830 res = -EIO;
1831 goto return_result;
1832 }
1833 /* Provide crypto context and crypto buffer to ext4 match */
1834 res = ext4_match(ctx, &fname_crypto_str, namelen, name, de);
1835 if (res < 0)
1836 goto return_result;
1837 if (res > 0) {
1838 res = -EEXIST;
1839 goto return_result;
1840 }
Tao Ma978fef92012-12-10 14:05:58 -05001841 nlen = EXT4_DIR_REC_LEN(de->name_len);
1842 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1843 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1844 break;
1845 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1846 offset += rlen;
1847 }
Tao Ma978fef92012-12-10 14:05:58 -05001848
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001849 if ((char *) de > top)
1850 res = -ENOSPC;
1851 else {
1852 *dest_de = de;
1853 res = 0;
1854 }
1855return_result:
1856 ext4_put_fname_crypto_ctx(&ctx);
1857 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1858 return res;
Tao Ma978fef92012-12-10 14:05:58 -05001859}
1860
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001861int ext4_insert_dentry(struct inode *dir,
1862 struct inode *inode,
1863 struct ext4_dir_entry_2 *de,
1864 int buf_size,
1865 const struct qstr *iname,
1866 const char *name, int namelen)
Tao Ma978fef92012-12-10 14:05:58 -05001867{
1868
1869 int nlen, rlen;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001870 struct ext4_fname_crypto_ctx *ctx = NULL;
1871 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1872 struct ext4_str tmp_str;
1873 int res;
1874
1875 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1876 if (IS_ERR(ctx))
1877 return -EIO;
1878 /* By default, the input name would be written to the disk */
1879 tmp_str.name = (unsigned char *)name;
1880 tmp_str.len = namelen;
1881 if (ctx != NULL) {
1882 /* Directory is encrypted */
1883 res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1884 &fname_crypto_str);
1885 if (res < 0) {
1886 ext4_put_fname_crypto_ctx(&ctx);
1887 return -ENOMEM;
1888 }
1889 res = ext4_fname_usr_to_disk(ctx, iname, &fname_crypto_str);
1890 if (res < 0) {
1891 ext4_put_fname_crypto_ctx(&ctx);
1892 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1893 return res;
1894 }
1895 tmp_str.name = fname_crypto_str.name;
1896 tmp_str.len = fname_crypto_str.len;
1897 }
Tao Ma978fef92012-12-10 14:05:58 -05001898
1899 nlen = EXT4_DIR_REC_LEN(de->name_len);
1900 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1901 if (de->inode) {
1902 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001903 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001904 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1905 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1906 de = de1;
1907 }
1908 de->file_type = EXT4_FT_UNKNOWN;
1909 de->inode = cpu_to_le32(inode->i_ino);
1910 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001911 de->name_len = tmp_str.len;
1912
1913 memcpy(de->name, tmp_str.name, tmp_str.len);
1914 ext4_put_fname_crypto_ctx(&ctx);
1915 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1916 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001917}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001918
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001919/*
1920 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1921 * it points to a directory entry which is guaranteed to be large
1922 * enough for new directory entry. If de is NULL, then
1923 * add_dirent_to_buf will attempt search the directory block for
1924 * space. It will return -ENOSPC if no space is available, and -EIO
1925 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926 */
1927static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001928 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001929 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930{
1931 struct inode *dir = dentry->d_parent->d_inode;
1932 const char *name = dentry->d_name.name;
1933 int namelen = dentry->d_name.len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001934 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001935 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001936 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001937
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001938 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001939 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941 if (!de) {
Tao Ma978fef92012-12-10 14:05:58 -05001942 err = ext4_find_dest_de(dir, inode,
1943 bh, bh->b_data, blocksize - csum_size,
1944 name, namelen, &de);
1945 if (err)
1946 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001947 }
1948 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001949 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001951 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952 return err;
1953 }
1954
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001955 /* By now the buffer is marked for journaling. Due to crypto operations,
1956 * the following function call may fail */
1957 err = ext4_insert_dentry(dir, inode, de, blocksize, &dentry->d_name,
1958 name, namelen);
1959 if (err < 0)
1960 return err;
Tao Ma978fef92012-12-10 14:05:58 -05001961
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 /*
1963 * XXX shouldn't update any times until successful
1964 * completion of syscall, but too many callers depend
1965 * on this.
1966 *
1967 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001968 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 * recovery deletes the inode, so the worst that can
1970 * happen is that the times are slightly out of date
1971 * and/or different from the directory change time.
1972 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001973 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001974 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001976 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001977 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001978 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001980 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 return 0;
1982}
1983
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001984/*
1985 * This converts a one block unindexed directory to a 3 block indexed
1986 * directory, and adds the dentry to the indexed directory.
1987 */
1988static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1989 struct inode *inode, struct buffer_head *bh)
1990{
1991 struct inode *dir = dentry->d_parent->d_inode;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001992#ifdef CONFIG_EXT4_FS_ENCRYPTION
1993 struct ext4_fname_crypto_ctx *ctx = NULL;
1994 int res;
1995#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001996 const char *name = dentry->d_name.name;
1997 int namelen = dentry->d_name.len;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001998#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999 struct buffer_head *bh2;
2000 struct dx_root *root;
2001 struct dx_frame frames[2], *frame;
2002 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07002003 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002004 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002005 char *data1, *top;
2006 unsigned len;
2007 int retval;
2008 unsigned blocksize;
2009 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002010 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002012 int csum_size = 0;
2013
2014#ifdef CONFIG_EXT4_FS_ENCRYPTION
2015 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
2016 if (IS_ERR(ctx))
2017 return PTR_ERR(ctx);
2018#endif
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002019
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002020 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002021 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022
2023 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002024 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04002025 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002026 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002028 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 brelse(bh);
2030 return retval;
2031 }
2032 root = (struct dx_root *) bh->b_data;
2033
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002034 /* The 0th block becomes the root, move the dirents out */
2035 fde = &root->dotdot;
2036 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002037 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002038 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002039 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002040 brelse(bh);
2041 return -EIO;
2042 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002043 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002044
2045 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002046 bh2 = ext4_append(handle, dir, &block);
2047 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002048 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002049 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002051 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 data1 = bh2->b_data;
2053
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07002055 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002056 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002057 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002059 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
2060 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002061 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002062
2063 if (csum_size) {
2064 t = EXT4_DIRENT_TAIL(data1, blocksize);
2065 initialize_dirent_tail(t, blocksize);
2066 }
2067
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002068 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002069 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002070 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2071 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002072 memset (&root->info, 0, sizeof(root->info));
2073 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07002074 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002076 dx_set_block(entries, 1);
2077 dx_set_count(entries, 1);
2078 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079
2080 /* Initialize as for dx_probe */
2081 hinfo.hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002082 if (hinfo.hash_version <= DX_HASH_TEA)
2083 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07002084 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002085#ifdef CONFIG_EXT4_FS_ENCRYPTION
2086 res = ext4_fname_usr_to_hash(ctx, &dentry->d_name, &hinfo);
2087 if (res < 0) {
2088 ext4_put_fname_crypto_ctx(&ctx);
2089 ext4_mark_inode_dirty(handle, dir);
2090 brelse(bh);
2091 return res;
2092 }
2093 ext4_put_fname_crypto_ctx(&ctx);
2094#else
Mingming Cao617ba132006-10-11 01:20:53 -07002095 ext4fs_dirhash(name, namelen, &hinfo);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002096#endif
Jan Kara6050d472014-10-30 10:53:17 -04002097 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098 frame = frames;
2099 frame->entries = entries;
2100 frame->at = entries;
2101 frame->bh = bh;
2102 bh = bh2;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002103
Jan Kara6050d472014-10-30 10:53:17 -04002104 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2105 if (retval)
2106 goto out_frames;
2107 retval = ext4_handle_dirty_dirent_node(handle, dir, bh);
2108 if (retval)
2109 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002110
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002111 de = do_split(handle,dir, &bh, frame, &hinfo);
2112 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002113 retval = PTR_ERR(de);
2114 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002115 }
2116 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002118 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
2119 brelse(bh);
2120 return retval;
Jan Kara6050d472014-10-30 10:53:17 -04002121out_frames:
2122 /*
2123 * Even if the block split failed, we have to properly write
2124 * out all the changes we did so far. Otherwise we can end up
2125 * with corrupted filesystem.
2126 */
2127 ext4_mark_inode_dirty(handle, dir);
2128 dx_release(frames);
2129 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002131
2132/*
Mingming Cao617ba132006-10-11 01:20:53 -07002133 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 *
2135 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002136 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002137 *
2138 * NOTE!! The inode part of 'de' is left at 0 - which means you
2139 * may not sleep between calling this and putting something into
2140 * the entry, as someone else might have used it while you slept.
2141 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002142static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2143 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144{
2145 struct inode *dir = dentry->d_parent->d_inode;
Lukas Czernere12fb972015-04-03 10:46:58 -04002146 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002147 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002148 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002149 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002152 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002153 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002154 int csum_size = 0;
2155
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002156 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002157 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158
2159 sb = dir->i_sb;
2160 blocksize = sb->s_blocksize;
2161 if (!dentry->d_name.len)
2162 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002163
2164 if (ext4_has_inline_data(dir)) {
2165 retval = ext4_try_add_inline_entry(handle, dentry, inode);
2166 if (retval < 0)
2167 return retval;
2168 if (retval == 1) {
2169 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002170 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002171 }
2172 }
2173
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002175 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002176 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002177 goto out;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002178 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002180 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002181 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002182 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002183 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002184 bh = ext4_read_dirblock(dir, block, DIRENT);
2185 if (IS_ERR(bh))
2186 return PTR_ERR(bh);
2187
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002188 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002189 if (retval != -ENOSPC)
2190 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192 if (blocks == 1 && !dx_fallback &&
Lukas Czernere12fb972015-04-03 10:46:58 -04002193 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
2194 retval = make_indexed_dir(handle, dentry, inode, bh);
2195 bh = NULL; /* make_indexed_dir releases bh */
2196 goto out;
2197 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002198 brelse(bh);
2199 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002200 bh = ext4_append(handle, dir, &block);
2201 if (IS_ERR(bh))
2202 return PTR_ERR(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002203 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002204 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002205 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2206
2207 if (csum_size) {
2208 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2209 initialize_dirent_tail(t, blocksize);
2210 }
2211
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002212 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002213out:
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002214 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002215 if (retval == 0)
2216 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002217 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218}
2219
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002220/*
2221 * Returns 0 for success, or a negative error value
2222 */
Mingming Cao617ba132006-10-11 01:20:53 -07002223static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 struct inode *inode)
2225{
2226 struct dx_frame frames[2], *frame;
2227 struct dx_entry *entries, *at;
2228 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002229 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002231 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002232 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 int err;
2234
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002235 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames);
2236 if (IS_ERR(frame))
2237 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238 entries = frame->entries;
2239 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002240 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2241 if (IS_ERR(bh)) {
2242 err = PTR_ERR(bh);
2243 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002244 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002245 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246
2247 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002248 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 if (err)
2250 goto journal_error;
2251
2252 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002253 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255
2256 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002257 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258 dx_get_count(entries), dx_get_limit(entries)));
2259 /* Need to split index? */
2260 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002261 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002262 unsigned icount = dx_get_count(entries);
2263 int levels = frame - frames;
2264 struct dx_entry *entries2;
2265 struct dx_node *node2;
2266 struct buffer_head *bh2;
2267
2268 if (levels && (dx_get_count(frames->entries) ==
2269 dx_get_limit(frames->entries))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002270 ext4_warning(sb, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 err = -ENOSPC;
2272 goto cleanup;
2273 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002274 bh2 = ext4_append(handle, dir, &newblock);
2275 if (IS_ERR(bh2)) {
2276 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002278 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279 node2 = (struct dx_node *)(bh2->b_data);
2280 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002281 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002282 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2283 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002285 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 if (err)
2287 goto journal_error;
2288 if (levels) {
2289 unsigned icount1 = icount/2, icount2 = icount - icount1;
2290 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002291 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2292 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293
2294 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002295 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002296 frames[0].bh);
2297 if (err)
2298 goto journal_error;
2299
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002300 memcpy((char *) entries2, (char *) (entries + icount1),
2301 icount2 * sizeof(struct dx_entry));
2302 dx_set_count(entries, icount1);
2303 dx_set_count(entries2, icount2);
2304 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305
2306 /* Which index block gets the new entry? */
2307 if (at - entries >= icount1) {
2308 frame->at = at = at - entries - icount1 + entries2;
2309 frame->entries = entries = entries2;
2310 swap(frame->bh, bh2);
2311 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002312 dx_insert_block(frames + 0, hash2, newblock);
2313 dxtrace(dx_show_index("node", frames[1].entries));
2314 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002315 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002316 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317 if (err)
2318 goto journal_error;
2319 brelse (bh2);
2320 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002321 dxtrace(printk(KERN_DEBUG
2322 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002323 memcpy((char *) entries2, (char *) entries,
2324 icount * sizeof(struct dx_entry));
2325 dx_set_limit(entries2, dx_node_limit(dir));
2326
2327 /* Set up root */
2328 dx_set_count(entries, 1);
2329 dx_set_block(entries + 0, newblock);
2330 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2331
2332 /* Add new access path frame */
2333 frame = frames + 1;
2334 frame->at = at = at - entries + entries2;
2335 frame->entries = entries = entries2;
2336 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002337 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338 frame->bh);
2339 if (err)
2340 goto journal_error;
2341 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002342 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002343 if (err) {
2344 ext4_std_error(inode->i_sb, err);
2345 goto cleanup;
2346 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002347 }
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002348 de = do_split(handle, dir, &bh, frame, &hinfo);
2349 if (IS_ERR(de)) {
2350 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002352 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002354 goto cleanup;
2355
2356journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002357 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002358cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002359 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002360 dx_release(frames);
2361 return err;
2362}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002363
2364/*
Tao Ma05019a92012-12-10 14:06:00 -05002365 * ext4_generic_delete_entry deletes a directory entry by merging it
2366 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002367 */
Tao Ma05019a92012-12-10 14:06:00 -05002368int ext4_generic_delete_entry(handle_t *handle,
2369 struct inode *dir,
2370 struct ext4_dir_entry_2 *de_del,
2371 struct buffer_head *bh,
2372 void *entry_buf,
2373 int buf_size,
2374 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002376 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002377 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002378 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002379
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 i = 0;
2381 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002382 de = (struct ext4_dir_entry_2 *)entry_buf;
2383 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002384 if (ext4_check_dir_entry(dir, NULL, de, bh,
2385 bh->b_data, bh->b_size, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002386 return -EIO;
2387 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002388 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002389 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002390 ext4_rec_len_from_disk(pde->rec_len,
2391 blocksize) +
2392 ext4_rec_len_from_disk(de->rec_len,
2393 blocksize),
2394 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002395 else
2396 de->inode = 0;
2397 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002398 return 0;
2399 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002400 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002401 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002402 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403 }
2404 return -ENOENT;
2405}
2406
Tao Ma05019a92012-12-10 14:06:00 -05002407static int ext4_delete_entry(handle_t *handle,
2408 struct inode *dir,
2409 struct ext4_dir_entry_2 *de_del,
2410 struct buffer_head *bh)
2411{
2412 int err, csum_size = 0;
2413
Tao Ma9f40fe52012-12-10 14:06:00 -05002414 if (ext4_has_inline_data(dir)) {
2415 int has_inline_data = 1;
2416 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2417 &has_inline_data);
2418 if (has_inline_data)
2419 return err;
2420 }
2421
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002422 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002423 csum_size = sizeof(struct ext4_dir_entry_tail);
2424
2425 BUFFER_TRACE(bh, "get_write_access");
2426 err = ext4_journal_get_write_access(handle, bh);
2427 if (unlikely(err))
2428 goto out;
2429
2430 err = ext4_generic_delete_entry(handle, dir, de_del,
2431 bh, bh->b_data,
2432 dir->i_sb->s_blocksize, csum_size);
2433 if (err)
2434 goto out;
2435
2436 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2437 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2438 if (unlikely(err))
2439 goto out;
2440
2441 return 0;
2442out:
2443 if (err != -ENOENT)
2444 ext4_std_error(dir->i_sb, err);
2445 return err;
2446}
2447
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002448/*
2449 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2450 * since this indicates that nlinks count was previously 1.
2451 */
2452static void ext4_inc_count(handle_t *handle, struct inode *inode)
2453{
2454 inc_nlink(inode);
2455 if (is_dx(inode) && inode->i_nlink > 1) {
2456 /* limit is 16-bit i_links_count */
2457 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002458 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002459 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2460 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2461 }
2462 }
2463}
2464
2465/*
2466 * If a directory had nlink == 1, then we should let it be 1. This indicates
2467 * directory has >EXT4_LINK_MAX subdirs.
2468 */
2469static void ext4_dec_count(handle_t *handle, struct inode *inode)
2470{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002471 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2472 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002473}
2474
2475
Mingming Cao617ba132006-10-11 01:20:53 -07002476static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477 struct dentry *dentry, struct inode *inode)
2478{
Mingming Cao617ba132006-10-11 01:20:53 -07002479 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002481 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002482 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002483 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 return 0;
2485 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002486 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002487 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 iput(inode);
2489 return err;
2490}
2491
2492/*
2493 * By the time this is called, we already have created
2494 * the directory cache entry for the new file, but it
2495 * is so far negative - it has no inode.
2496 *
2497 * If the create succeeds, we fill in the inode information
2498 * with d_instantiate().
2499 */
Al Viro4acdaf22011-07-26 01:42:34 -04002500static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002501 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002502{
2503 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002504 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002505 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002506
Christoph Hellwig871a2932010-03-03 09:05:07 -05002507 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002508
Theodore Ts'o11395752013-02-09 16:27:09 -05002509 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002510 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002511retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002512 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2513 NULL, EXT4_HT_DIR, credits);
2514 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 err = PTR_ERR(inode);
2516 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002517 inode->i_op = &ext4_file_inode_operations;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002518 if (test_opt(inode->i_sb, DAX))
2519 inode->i_fop = &ext4_dax_file_operations;
2520 else
2521 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002522 ext4_set_aops(inode);
Michael Halcrowdde680c2015-04-12 00:55:09 -04002523 err = 0;
2524#ifdef CONFIG_EXT4_FS_ENCRYPTION
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002525 if (!err && (ext4_encrypted_inode(dir) ||
2526 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)))) {
Michael Halcrowdde680c2015-04-12 00:55:09 -04002527 err = ext4_inherit_context(dir, inode);
2528 if (err) {
2529 clear_nlink(inode);
2530 unlock_new_inode(inode);
2531 iput(inode);
2532 }
2533 }
2534#endif
2535 if (!err)
2536 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002537 if (!err && IS_DIRSYNC(dir))
2538 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002539 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002540 if (handle)
2541 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002542 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002543 goto retry;
2544 return err;
2545}
2546
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002547static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002548 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549{
2550 handle_t *handle;
2551 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002552 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553
2554 if (!new_valid_dev(rdev))
2555 return -EINVAL;
2556
Christoph Hellwig871a2932010-03-03 09:05:07 -05002557 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002558
Theodore Ts'o11395752013-02-09 16:27:09 -05002559 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002560 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002561retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002562 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2563 NULL, EXT4_HT_DIR, credits);
2564 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002565 err = PTR_ERR(inode);
2566 if (!IS_ERR(inode)) {
2567 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002568 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002569 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002570 if (!err && IS_DIRSYNC(dir))
2571 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002572 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002573 if (handle)
2574 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002575 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002576 goto retry;
2577 return err;
2578}
2579
Al Viroaf51a2a2013-06-29 13:23:08 +04002580static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2581{
2582 handle_t *handle;
2583 struct inode *inode;
2584 int err, retries = 0;
2585
2586 dquot_initialize(dir);
2587
2588retry:
2589 inode = ext4_new_inode_start_handle(dir, mode,
2590 NULL, 0, NULL,
2591 EXT4_HT_DIR,
2592 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2593 4 + EXT4_XATTR_TRANS_BLOCKS);
2594 handle = ext4_journal_current_handle();
2595 err = PTR_ERR(inode);
2596 if (!IS_ERR(inode)) {
2597 inode->i_op = &ext4_file_inode_operations;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002598 if (test_opt(inode->i_sb, DAX))
2599 inode->i_fop = &ext4_dax_file_operations;
2600 else
2601 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002602 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002603 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002604 err = ext4_orphan_add(handle, inode);
2605 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002606 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002607 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002608 unlock_new_inode(inode);
2609 }
2610 if (handle)
2611 ext4_journal_stop(handle);
2612 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2613 goto retry;
2614 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002615err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002616 ext4_journal_stop(handle);
2617 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002618 return err;
2619}
2620
Tao Maa774f9c2012-12-10 14:05:57 -05002621struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2622 struct ext4_dir_entry_2 *de,
2623 int blocksize, int csum_size,
2624 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002625{
Tao Maa774f9c2012-12-10 14:05:57 -05002626 de->inode = cpu_to_le32(inode->i_ino);
2627 de->name_len = 1;
2628 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2629 blocksize);
2630 strcpy(de->name, ".");
2631 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2632
2633 de = ext4_next_entry(de, blocksize);
2634 de->inode = cpu_to_le32(parent_ino);
2635 de->name_len = 2;
2636 if (!dotdot_real_len)
2637 de->rec_len = ext4_rec_len_to_disk(blocksize -
2638 (csum_size + EXT4_DIR_REC_LEN(1)),
2639 blocksize);
2640 else
2641 de->rec_len = ext4_rec_len_to_disk(
2642 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2643 strcpy(de->name, "..");
2644 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2645
2646 return ext4_next_entry(de, blocksize);
2647}
2648
2649static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2650 struct inode *inode)
2651{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002652 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002653 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002654 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002655 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002656 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002657 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002658 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002659
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002660 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002661 csum_size = sizeof(struct ext4_dir_entry_tail);
2662
Tao Ma3c47d542012-12-10 14:05:59 -05002663 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2664 err = ext4_try_create_inline_dir(handle, dir, inode);
2665 if (err < 0 && err != -ENOSPC)
2666 goto out;
2667 if (!err)
2668 goto out;
2669 }
2670
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002671 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002672 dir_block = ext4_append(handle, inode, &block);
2673 if (IS_ERR(dir_block))
2674 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002675 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2676 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2677 set_nlink(inode, 2);
2678 if (csum_size) {
2679 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2680 initialize_dirent_tail(t, blocksize);
2681 }
2682
2683 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2684 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2685 if (err)
2686 goto out;
2687 set_buffer_verified(dir_block);
2688out:
2689 brelse(dir_block);
2690 return err;
2691}
2692
2693static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2694{
2695 handle_t *handle;
2696 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002697 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002698
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002699 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002700 return -EMLINK;
2701
Christoph Hellwig871a2932010-03-03 09:05:07 -05002702 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002703
Theodore Ts'o11395752013-02-09 16:27:09 -05002704 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002705 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002707 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2708 &dentry->d_name,
2709 0, NULL, EXT4_HT_DIR, credits);
2710 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002711 err = PTR_ERR(inode);
2712 if (IS_ERR(inode))
2713 goto out_stop;
2714
Mingming Cao617ba132006-10-11 01:20:53 -07002715 inode->i_op = &ext4_dir_inode_operations;
2716 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002717 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002718 if (err)
2719 goto out_clear_inode;
Michael Halcrowdde680c2015-04-12 00:55:09 -04002720#ifdef CONFIG_EXT4_FS_ENCRYPTION
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002721 if (ext4_encrypted_inode(dir) ||
2722 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) {
Michael Halcrowdde680c2015-04-12 00:55:09 -04002723 err = ext4_inherit_context(dir, inode);
2724 if (err)
2725 goto out_clear_inode;
2726 }
2727#endif
Namhyung Kimdabd9912011-01-10 12:11:16 -05002728 err = ext4_mark_inode_dirty(handle, inode);
2729 if (!err)
2730 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002732out_clear_inode:
2733 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002734 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002735 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002736 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002737 goto out_stop;
2738 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002739 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002740 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002741 err = ext4_mark_inode_dirty(handle, dir);
2742 if (err)
2743 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002744 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002745 d_instantiate(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002746 if (IS_DIRSYNC(dir))
2747 ext4_handle_sync(handle);
2748
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002749out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002750 if (handle)
2751 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002752 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002753 goto retry;
2754 return err;
2755}
2756
2757/*
2758 * routine to check that the specified directory is empty (for rmdir)
2759 */
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002760int ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002762 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002763 struct buffer_head *bh;
2764 struct ext4_dir_entry_2 *de, *de1;
2765 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002766 int err = 0;
2767
Tao Ma61f86632012-12-10 14:06:01 -05002768 if (ext4_has_inline_data(inode)) {
2769 int has_inline_data = 1;
2770
2771 err = empty_inline_dir(inode, &has_inline_data);
2772 if (has_inline_data)
2773 return err;
2774 }
2775
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002776 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002777 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2778 EXT4_ERROR_INODE(inode, "invalid size");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002779 return 1;
2780 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002781 bh = ext4_read_dirblock(inode, 0, EITHER);
2782 if (IS_ERR(bh))
2783 return 1;
2784
Mingming Cao617ba132006-10-11 01:20:53 -07002785 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002786 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002787 if (le32_to_cpu(de->inode) != inode->i_ino ||
2788 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002789 strcmp(".", de->name) ||
2790 strcmp("..", de1->name)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002791 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002792 "bad directory (dir #%lu) - no `.' or `..'",
2793 inode->i_ino);
2794 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795 return 1;
2796 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002797 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2798 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2799 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002800 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002801 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002802 unsigned int lblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002803 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002804 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002805 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002806 bh = ext4_read_dirblock(inode, lblock, EITHER);
2807 if (IS_ERR(bh))
2808 return 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002809 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002810 }
Tao Ma226ba972012-12-10 14:05:58 -05002811 if (ext4_check_dir_entry(inode, NULL, de, bh,
2812 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002813 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002814 sb->s_blocksize);
2815 offset = (offset | (sb->s_blocksize - 1)) + 1;
2816 continue;
2817 }
2818 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002819 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820 return 0;
2821 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002822 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2823 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002825 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002826 return 1;
2827}
2828
Jan Karad745a8c2014-05-26 11:56:53 -04002829/*
2830 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831 * such inodes, starting at the superblock, in case we crash before the
2832 * file is closed/deleted, or in case the inode truncate spans multiple
2833 * transactions and the last transaction is not recovered after a crash.
2834 *
2835 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002836 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002837 *
2838 * Orphan list manipulation functions must be called under i_mutex unless
2839 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840 */
Mingming Cao617ba132006-10-11 01:20:53 -07002841int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002842{
2843 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002844 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002845 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002846 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002847 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002848
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002849 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002850 return 0;
2851
Jan Karad745a8c2014-05-26 11:56:53 -04002852 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2853 !mutex_is_locked(&inode->i_mutex));
2854 /*
2855 * Exit early if inode already is on orphan list. This is a big speedup
2856 * since we don't have to contend on the global s_orphan_lock.
2857 */
Mingming Cao617ba132006-10-11 01:20:53 -07002858 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002859 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860
Lukas Czernerafb86172011-07-11 18:47:04 -04002861 /*
2862 * Orphan handling is only valid for files with data blocks
2863 * being truncated, or files being unlinked. Note that we either
2864 * hold i_mutex, or the inode can not be referenced from outside,
2865 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002867 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2868 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002869
Jan Karacd2c0802014-05-26 11:39:17 -04002870 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2871 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002872 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002873 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874
Mingming Cao617ba132006-10-11 01:20:53 -07002875 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002877 goto out;
2878
2879 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002880 /*
2881 * Due to previous errors inode may be already a part of on-disk
2882 * orphan list. If so skip on-disk list modification.
2883 */
Jan Karad745a8c2014-05-26 11:56:53 -04002884 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2885 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2886 /* Insert this inode at the head of the on-disk orphan list */
2887 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2888 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2889 dirty = true;
2890 }
2891 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2892 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893
Jan Karad745a8c2014-05-26 11:56:53 -04002894 if (dirty) {
2895 err = ext4_handle_dirty_super(handle, sb);
2896 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2897 if (!err)
2898 err = rc;
2899 if (err) {
2900 /*
2901 * We have to remove inode from in-memory list if
2902 * addition to on disk orphan list failed. Stray orphan
2903 * list entries can cause panics at unmount time.
2904 */
2905 mutex_lock(&sbi->s_orphan_lock);
2906 list_del(&EXT4_I(inode)->i_orphan);
2907 mutex_unlock(&sbi->s_orphan_lock);
2908 }
2909 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002910 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2911 jbd_debug(4, "orphan inode %lu will point to %d\n",
2912 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002913out:
Jan Karacd2c0802014-05-26 11:39:17 -04002914 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 return err;
2916}
2917
2918/*
Mingming Cao617ba132006-10-11 01:20:53 -07002919 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002920 * of such inodes stored on disk, because it is finally being cleaned up.
2921 */
Mingming Cao617ba132006-10-11 01:20:53 -07002922int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002923{
2924 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002925 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002926 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002927 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002928 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002929 int err = 0;
2930
Jan Karacd2c0802014-05-26 11:39:17 -04002931 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002932 return 0;
2933
Jan Karad745a8c2014-05-26 11:56:53 -04002934 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2935 !mutex_is_locked(&inode->i_mutex));
2936 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002937 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002938 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939
Jan Karad745a8c2014-05-26 11:56:53 -04002940 if (handle) {
2941 /* Grab inode buffer early before taking global s_orphan_lock */
2942 err = ext4_reserve_inode_write(handle, inode, &iloc);
2943 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944
Jan Karad745a8c2014-05-26 11:56:53 -04002945 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2947
Jan Karad745a8c2014-05-26 11:56:53 -04002948 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949 list_del_init(&ei->i_orphan);
2950
2951 /* If we're on an error path, we may not have a valid
2952 * transaction handle with which to update the orphan list on
2953 * disk, but we still need to remove the inode from the linked
2954 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002955 if (!handle || err) {
2956 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002957 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002958 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959
Jan Karad745a8c2014-05-26 11:56:53 -04002960 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002961 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002962 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002963 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002964 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04002965 if (err) {
2966 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002967 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002968 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04002970 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002971 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002972 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002973 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002975 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002976
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002977 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002979 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002980 if (err) {
2981 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002982 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002983 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002985 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002986 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002987 }
2988 if (err)
2989 goto out_brelse;
2990 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002991 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002992out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002993 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994 return err;
2995
2996out_brelse:
2997 brelse(iloc.bh);
2998 goto out_err;
2999}
3000
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003001static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002{
3003 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003004 struct inode *inode;
3005 struct buffer_head *bh;
3006 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003007 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008
3009 /* Initialize quotas before so that eventual writes go in
3010 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05003011 dquot_initialize(dir);
3012 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003013
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003015 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003016 if (IS_ERR(bh))
3017 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003018 if (!bh)
3019 goto end_rmdir;
3020
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 inode = dentry->d_inode;
3022
3023 retval = -EIO;
3024 if (le32_to_cpu(de->inode) != inode->i_ino)
3025 goto end_rmdir;
3026
3027 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003028 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029 goto end_rmdir;
3030
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003031 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003032 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003033 if (IS_ERR(handle)) {
3034 retval = PTR_ERR(handle);
3035 handle = NULL;
3036 goto end_rmdir;
3037 }
3038
3039 if (IS_DIRSYNC(dir))
3040 ext4_handle_sync(handle);
3041
Mingming Cao617ba132006-10-11 01:20:53 -07003042 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043 if (retval)
3044 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003045 if (!EXT4_DIR_LINK_EMPTY(inode))
Eric Sandeen12062dd2010-02-15 14:19:27 -05003046 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003047 "empty directory has too many links (%d)",
3048 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003049 inode->i_version++;
3050 clear_nlink(inode);
3051 /* There's no need to set i_disksize: the fact that i_nlink is
3052 * zero will ensure that the right thing happens during any
3053 * recovery. */
3054 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003055 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003056 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003057 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003058 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003059 ext4_update_dx_flag(dir);
3060 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061
3062end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003063 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003064 if (handle)
3065 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066 return retval;
3067}
3068
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003069static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070{
3071 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003072 struct inode *inode;
3073 struct buffer_head *bh;
3074 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003075 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003077 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078 /* Initialize quotas before so that eventual writes go
3079 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05003080 dquot_initialize(dir);
3081 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003082
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003083 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003084 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003085 if (IS_ERR(bh))
3086 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003087 if (!bh)
3088 goto end_unlink;
3089
3090 inode = dentry->d_inode;
3091
3092 retval = -EIO;
3093 if (le32_to_cpu(de->inode) != inode->i_ino)
3094 goto end_unlink;
3095
Theodore Ts'o931b6862013-02-09 09:43:39 -05003096 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003097 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05003098 if (IS_ERR(handle)) {
3099 retval = PTR_ERR(handle);
3100 handle = NULL;
3101 goto end_unlink;
3102 }
3103
3104 if (IS_DIRSYNC(dir))
3105 ext4_handle_sync(handle);
3106
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003107 if (!inode->i_nlink) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003108 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003109 "Deleting nonexistent file (%lu), %d",
3110 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003111 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 }
Mingming Cao617ba132006-10-11 01:20:53 -07003113 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003114 if (retval)
3115 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04003116 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003117 ext4_update_dx_flag(dir);
3118 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003119 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003121 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003122 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003123 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124
3125end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003126 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003127 if (handle)
3128 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003129 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 return retval;
3131}
3132
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003133static int ext4_symlink(struct inode *dir,
3134 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135{
3136 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003137 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003138 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003139 int credits;
Theodore Ts'of348c252015-04-16 01:55:00 -04003140 bool encryption_required;
3141 struct ext4_str disk_link;
3142 struct ext4_encrypted_symlink_data *sd = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143
Theodore Ts'of348c252015-04-16 01:55:00 -04003144 disk_link.len = len + 1;
3145 disk_link.name = (char *) symname;
3146
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04003147 encryption_required = (ext4_encrypted_inode(dir) ||
3148 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
Theodore Ts'of348c252015-04-16 01:55:00 -04003149 if (encryption_required)
3150 disk_link.len = encrypted_symlink_data_len(len) + 1;
3151 if (disk_link.len > dir->i_sb->s_blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 return -ENAMETOOLONG;
3153
Christoph Hellwig871a2932010-03-03 09:05:07 -05003154 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003155
Theodore Ts'of348c252015-04-16 01:55:00 -04003156 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003157 /*
3158 * For non-fast symlinks, we just allocate inode and put it on
3159 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003160 * group descriptor, sb, inode block, quota blocks, and
3161 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003162 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003163 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3164 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003165 } else {
3166 /*
3167 * Fast symlink. We have to add entry to directory
3168 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3169 * allocate new inode (bitmap, group descriptor, inode block,
3170 * quota blocks, sb is already counted in previous macros).
3171 */
3172 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003173 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003174 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003175
Theodore Ts'o11395752013-02-09 16:27:09 -05003176 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3177 &dentry->d_name, 0, NULL,
3178 EXT4_HT_DIR, credits);
3179 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003180 if (IS_ERR(inode)) {
3181 if (handle)
3182 ext4_journal_stop(handle);
3183 return PTR_ERR(inode);
3184 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185
Theodore Ts'of348c252015-04-16 01:55:00 -04003186 if (encryption_required) {
3187 struct ext4_fname_crypto_ctx *ctx = NULL;
3188 struct qstr istr;
3189 struct ext4_str ostr;
3190
3191 sd = kzalloc(disk_link.len, GFP_NOFS);
3192 if (!sd) {
3193 err = -ENOMEM;
3194 goto err_drop_inode;
3195 }
3196 err = ext4_inherit_context(dir, inode);
3197 if (err)
3198 goto err_drop_inode;
3199 ctx = ext4_get_fname_crypto_ctx(inode,
3200 inode->i_sb->s_blocksize);
3201 if (IS_ERR_OR_NULL(ctx)) {
3202 /* We just set the policy, so ctx should not be NULL */
3203 err = (ctx == NULL) ? -EIO : PTR_ERR(ctx);
3204 goto err_drop_inode;
3205 }
3206 istr.name = (const unsigned char *) symname;
3207 istr.len = len;
3208 ostr.name = sd->encrypted_path;
3209 err = ext4_fname_usr_to_disk(ctx, &istr, &ostr);
3210 ext4_put_fname_crypto_ctx(&ctx);
3211 if (err < 0)
3212 goto err_drop_inode;
3213 sd->len = cpu_to_le16(ostr.len);
3214 disk_link.name = (char *) sd;
3215 }
3216
3217 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003218 inode->i_op = &ext4_symlink_inode_operations;
3219 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003221 * We cannot call page_symlink() with transaction started
3222 * because it calls into ext4_write_begin() which can wait
3223 * for transaction commit if we are running out of space
3224 * and thus we deadlock. So we have to stop transaction now
3225 * and restart it when symlink contents is written.
3226 *
3227 * To keep fs consistent in case of crash, we have to put inode
3228 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 */
Jan Karadf5e6222011-05-03 11:12:58 -04003230 drop_nlink(inode);
3231 err = ext4_orphan_add(handle, inode);
3232 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003233 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003234 if (err)
3235 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003236 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003237 if (err)
3238 goto err_drop_inode;
3239 /*
3240 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3241 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3242 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003243 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003244 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3245 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3246 if (IS_ERR(handle)) {
3247 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003248 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003249 goto err_drop_inode;
3250 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003251 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003252 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003253 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003254 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003255 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003256 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003257 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Theodore Ts'of348c252015-04-16 01:55:00 -04003258 inode->i_op = encryption_required ?
3259 &ext4_symlink_inode_operations :
3260 &ext4_fast_symlink_inode_operations;
3261 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3262 disk_link.len);
3263 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003264 }
Mingming Cao617ba132006-10-11 01:20:53 -07003265 EXT4_I(inode)->i_disksize = inode->i_size;
3266 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003267 if (!err && IS_DIRSYNC(dir))
3268 ext4_handle_sync(handle);
3269
Theodore Ts'o11395752013-02-09 16:27:09 -05003270 if (handle)
3271 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003272 kfree(sd);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003273 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04003274err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003275 if (handle)
3276 ext4_journal_stop(handle);
3277 kfree(sd);
3278 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003279 unlock_new_inode(inode);
3280 iput(inode);
3281 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003282}
3283
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003284static int ext4_link(struct dentry *old_dentry,
3285 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286{
3287 handle_t *handle;
3288 struct inode *inode = old_dentry->d_inode;
3289 int err, retries = 0;
3290
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003291 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 return -EMLINK;
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003293 if (ext4_encrypted_inode(dir) &&
3294 !ext4_is_child_context_consistent_with_parent(dir, inode))
3295 return -EPERM;
Christoph Hellwig871a2932010-03-03 09:05:07 -05003296 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003297
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003299 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3300 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003301 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003302 if (IS_ERR(handle))
3303 return PTR_ERR(handle);
3304
3305 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003306 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307
Kalpak Shahef7f3832007-07-18 09:15:20 -04003308 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003309 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003310 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003311
Al Viro6b38e842008-12-30 02:03:31 -05003312 err = ext4_add_entry(handle, dentry, inode);
3313 if (!err) {
3314 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003315 /* this can happen only for tmpfile being
3316 * linked the first time
3317 */
3318 if (inode->i_nlink == 1)
3319 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003320 d_instantiate(dentry, inode);
3321 } else {
3322 drop_nlink(inode);
3323 iput(inode);
3324 }
Mingming Cao617ba132006-10-11 01:20:53 -07003325 ext4_journal_stop(handle);
3326 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003327 goto retry;
3328 return err;
3329}
3330
Tao Ma32f7f222012-12-10 14:06:01 -05003331
3332/*
3333 * Try to find buffer head where contains the parent block.
3334 * It should be the inode block if it is inlined or the 1st block
3335 * if it is a normal dir.
3336 */
3337static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3338 struct inode *inode,
3339 int *retval,
3340 struct ext4_dir_entry_2 **parent_de,
3341 int *inlined)
3342{
3343 struct buffer_head *bh;
3344
3345 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003346 bh = ext4_read_dirblock(inode, 0, EITHER);
3347 if (IS_ERR(bh)) {
3348 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003349 return NULL;
3350 }
3351 *parent_de = ext4_next_entry(
3352 (struct ext4_dir_entry_2 *)bh->b_data,
3353 inode->i_sb->s_blocksize);
3354 return bh;
3355 }
3356
3357 *inlined = 1;
3358 return ext4_get_first_inline_block(inode, parent_de, retval);
3359}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003360
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003361struct ext4_renament {
3362 struct inode *dir;
3363 struct dentry *dentry;
3364 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003365 bool is_dir;
3366 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003367
3368 /* entry for "dentry" */
3369 struct buffer_head *bh;
3370 struct ext4_dir_entry_2 *de;
3371 int inlined;
3372
3373 /* entry for ".." in inode if it's a directory */
3374 struct buffer_head *dir_bh;
3375 struct ext4_dir_entry_2 *parent_de;
3376 int dir_inlined;
3377};
3378
Miklos Szeredibd1af142014-04-01 17:08:44 +02003379static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3380{
3381 int retval;
3382
3383 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3384 &retval, &ent->parent_de,
3385 &ent->dir_inlined);
3386 if (!ent->dir_bh)
3387 return retval;
3388 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3389 return -EIO;
3390 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3391 return ext4_journal_get_write_access(handle, ent->dir_bh);
3392}
3393
3394static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3395 unsigned dir_ino)
3396{
3397 int retval;
3398
3399 ent->parent_de->inode = cpu_to_le32(dir_ino);
3400 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3401 if (!ent->dir_inlined) {
3402 if (is_dx(ent->inode)) {
3403 retval = ext4_handle_dirty_dx_node(handle,
3404 ent->inode,
3405 ent->dir_bh);
3406 } else {
3407 retval = ext4_handle_dirty_dirent_node(handle,
3408 ent->inode,
3409 ent->dir_bh);
3410 }
3411 } else {
3412 retval = ext4_mark_inode_dirty(handle, ent->inode);
3413 }
3414 if (retval) {
3415 ext4_std_error(ent->dir->i_sb, retval);
3416 return retval;
3417 }
3418 return 0;
3419}
3420
3421static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3422 unsigned ino, unsigned file_type)
3423{
3424 int retval;
3425
3426 BUFFER_TRACE(ent->bh, "get write access");
3427 retval = ext4_journal_get_write_access(handle, ent->bh);
3428 if (retval)
3429 return retval;
3430 ent->de->inode = cpu_to_le32(ino);
3431 if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
3432 EXT4_FEATURE_INCOMPAT_FILETYPE))
3433 ent->de->file_type = file_type;
3434 ent->dir->i_version++;
3435 ent->dir->i_ctime = ent->dir->i_mtime =
3436 ext4_current_time(ent->dir);
3437 ext4_mark_inode_dirty(handle, ent->dir);
3438 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3439 if (!ent->inlined) {
3440 retval = ext4_handle_dirty_dirent_node(handle,
3441 ent->dir, ent->bh);
3442 if (unlikely(retval)) {
3443 ext4_std_error(ent->dir->i_sb, retval);
3444 return retval;
3445 }
3446 }
3447 brelse(ent->bh);
3448 ent->bh = NULL;
3449
3450 return 0;
3451}
3452
3453static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3454 const struct qstr *d_name)
3455{
3456 int retval = -ENOENT;
3457 struct buffer_head *bh;
3458 struct ext4_dir_entry_2 *de;
3459
3460 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003461 if (IS_ERR(bh))
3462 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003463 if (bh) {
3464 retval = ext4_delete_entry(handle, dir, de, bh);
3465 brelse(bh);
3466 }
3467 return retval;
3468}
3469
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003470static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3471 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003472{
3473 int retval;
3474 /*
3475 * ent->de could have moved from under us during htree split, so make
3476 * sure that we are deleting the right entry. We might also be pointing
3477 * to a stale entry in the unused part of ent->bh so just checking inum
3478 * and the name isn't enough.
3479 */
3480 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3481 ent->de->name_len != ent->dentry->d_name.len ||
3482 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003483 ent->de->name_len) ||
3484 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003485 retval = ext4_find_delete_entry(handle, ent->dir,
3486 &ent->dentry->d_name);
3487 } else {
3488 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3489 if (retval == -ENOENT) {
3490 retval = ext4_find_delete_entry(handle, ent->dir,
3491 &ent->dentry->d_name);
3492 }
3493 }
3494
3495 if (retval) {
3496 ext4_warning(ent->dir->i_sb,
3497 "Deleting old file (%lu), %d, error=%d",
3498 ent->dir->i_ino, ent->dir->i_nlink, retval);
3499 }
3500}
3501
Miklos Szeredibd429982014-04-01 17:08:44 +02003502static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3503{
3504 if (ent->dir_nlink_delta) {
3505 if (ent->dir_nlink_delta == -1)
3506 ext4_dec_count(handle, ent->dir);
3507 else
3508 ext4_inc_count(handle, ent->dir);
3509 ext4_mark_inode_dirty(handle, ent->dir);
3510 }
3511}
3512
Miklos Szeredicd808de2014-10-24 00:14:37 +02003513static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3514 int credits, handle_t **h)
3515{
3516 struct inode *wh;
3517 handle_t *handle;
3518 int retries = 0;
3519
3520 /*
3521 * for inode block, sb block, group summaries,
3522 * and inode bitmap
3523 */
3524 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3525 EXT4_XATTR_TRANS_BLOCKS + 4);
3526retry:
3527 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3528 &ent->dentry->d_name, 0, NULL,
3529 EXT4_HT_DIR, credits);
3530
3531 handle = ext4_journal_current_handle();
3532 if (IS_ERR(wh)) {
3533 if (handle)
3534 ext4_journal_stop(handle);
3535 if (PTR_ERR(wh) == -ENOSPC &&
3536 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3537 goto retry;
3538 } else {
3539 *h = handle;
3540 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3541 wh->i_op = &ext4_special_inode_operations;
3542 }
3543 return wh;
3544}
3545
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546/*
3547 * Anybody can rename anything with this: the permission checks are left to the
3548 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003549 *
3550 * n.b. old_{dentry,inode) refers to the source dentry/inode
3551 * while new_{dentry,inode) refers to the destination dentry/inode
3552 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003553 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003554static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003555 struct inode *new_dir, struct dentry *new_dentry,
3556 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003557{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003558 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003559 struct ext4_renament old = {
3560 .dir = old_dir,
3561 .dentry = old_dentry,
3562 .inode = old_dentry->d_inode,
3563 };
3564 struct ext4_renament new = {
3565 .dir = new_dir,
3566 .dentry = new_dentry,
3567 .inode = new_dentry->d_inode,
3568 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003569 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003570 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003571 struct inode *whiteout = NULL;
3572 int credits;
3573 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003574
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003575 dquot_initialize(old.dir);
3576 dquot_initialize(new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003577
3578 /* Initialize quotas before so that eventual writes go
3579 * in separate transaction */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003580 if (new.inode)
3581 dquot_initialize(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003582
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003583 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003584 if (IS_ERR(old.bh))
3585 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 /*
3587 * Check for inode number is _not_ due to possible IO errors.
3588 * We might rmdir the source, keep it as pwd of some process
3589 * and merrily kill the link to whatever was created under the
3590 * same name. Goodbye sticky bit ;-<
3591 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003592 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003593 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003594 goto end_rename;
3595
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003596 if ((old.dir != new.dir) &&
3597 ext4_encrypted_inode(new.dir) &&
3598 !ext4_is_child_context_consistent_with_parent(new.dir,
3599 old.inode)) {
3600 retval = -EPERM;
3601 goto end_rename;
3602 }
3603
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003604 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3605 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003606 if (IS_ERR(new.bh)) {
3607 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003608 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003609 goto end_rename;
3610 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003611 if (new.bh) {
3612 if (!new.inode) {
3613 brelse(new.bh);
3614 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003615 }
3616 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003617 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3618 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003619
Miklos Szeredicd808de2014-10-24 00:14:37 +02003620 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3621 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3622 if (!(flags & RENAME_WHITEOUT)) {
3623 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003624 if (IS_ERR(handle)) {
3625 retval = PTR_ERR(handle);
3626 handle = NULL;
3627 goto end_rename;
3628 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003629 } else {
3630 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003631 if (IS_ERR(whiteout)) {
3632 retval = PTR_ERR(whiteout);
3633 whiteout = NULL;
3634 goto end_rename;
3635 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003636 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003637
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003638 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003639 ext4_handle_sync(handle);
3640
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003641 if (S_ISDIR(old.inode->i_mode)) {
3642 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003643 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003644 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003645 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003646 } else {
3647 retval = -EMLINK;
3648 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3649 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003650 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003651 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003652 if (retval)
3653 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003654 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003655 /*
3656 * If we're renaming a file within an inline_data dir and adding or
3657 * setting the new dirent causes a conversion from inline_data to
3658 * extents/blockmap, we need to force the dirent delete code to
3659 * re-read the directory, or else we end up trying to delete a dirent
3660 * from what is now the extent tree root (or a block map).
3661 */
3662 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3663 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003664
3665 old_file_type = old.de->file_type;
3666 if (whiteout) {
3667 /*
3668 * Do this before adding a new entry, so the old entry is sure
3669 * to be still pointing to the valid old entry.
3670 */
3671 retval = ext4_setent(handle, &old, whiteout->i_ino,
3672 EXT4_FT_CHRDEV);
3673 if (retval)
3674 goto end_rename;
3675 ext4_mark_inode_dirty(handle, whiteout);
3676 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003677 if (!new.bh) {
3678 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003679 if (retval)
3680 goto end_rename;
3681 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003682 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003683 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003684 if (retval)
3685 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003686 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003687 if (force_reread)
3688 force_reread = !ext4_test_inode_flag(new.dir,
3689 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003690
3691 /*
3692 * Like most other Unix systems, set the ctime for inodes on a
3693 * rename.
3694 */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003695 old.inode->i_ctime = ext4_current_time(old.inode);
3696 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003697
Miklos Szeredicd808de2014-10-24 00:14:37 +02003698 if (!whiteout) {
3699 /*
3700 * ok, that's it
3701 */
3702 ext4_rename_delete(handle, &old, force_reread);
3703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003704
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003705 if (new.inode) {
3706 ext4_dec_count(handle, new.inode);
3707 new.inode->i_ctime = ext4_current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003709 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3710 ext4_update_dx_flag(old.dir);
3711 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003712 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3713 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003714 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003715
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003716 ext4_dec_count(handle, old.dir);
3717 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003718 /* checked ext4_empty_dir above, can't have another
3719 * parent, ext4_dec_count() won't work for many-linked
3720 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003721 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003723 ext4_inc_count(handle, new.dir);
3724 ext4_update_dx_flag(new.dir);
3725 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726 }
3727 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003728 ext4_mark_inode_dirty(handle, old.dir);
3729 if (new.inode) {
3730 ext4_mark_inode_dirty(handle, new.inode);
3731 if (!new.inode->i_nlink)
3732 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003733 }
3734 retval = 0;
3735
3736end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003737 brelse(old.dir_bh);
3738 brelse(old.bh);
3739 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003740 if (whiteout) {
3741 if (retval)
3742 drop_nlink(whiteout);
3743 unlock_new_inode(whiteout);
3744 iput(whiteout);
3745 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003746 if (handle)
3747 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003748 return retval;
3749}
3750
Miklos Szeredibd429982014-04-01 17:08:44 +02003751static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3752 struct inode *new_dir, struct dentry *new_dentry)
3753{
3754 handle_t *handle = NULL;
3755 struct ext4_renament old = {
3756 .dir = old_dir,
3757 .dentry = old_dentry,
3758 .inode = old_dentry->d_inode,
3759 };
3760 struct ext4_renament new = {
3761 .dir = new_dir,
3762 .dentry = new_dentry,
3763 .inode = new_dentry->d_inode,
3764 };
3765 u8 new_file_type;
3766 int retval;
3767
3768 dquot_initialize(old.dir);
3769 dquot_initialize(new.dir);
3770
3771 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3772 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003773 if (IS_ERR(old.bh))
3774 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003775 /*
3776 * Check for inode number is _not_ due to possible IO errors.
3777 * We might rmdir the source, keep it as pwd of some process
3778 * and merrily kill the link to whatever was created under the
3779 * same name. Goodbye sticky bit ;-<
3780 */
3781 retval = -ENOENT;
3782 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3783 goto end_rename;
3784
3785 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3786 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003787 if (IS_ERR(new.bh)) {
3788 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003789 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003790 goto end_rename;
3791 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003792
3793 /* RENAME_EXCHANGE case: old *and* new must both exist */
3794 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3795 goto end_rename;
3796
3797 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3798 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3799 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003800 if (IS_ERR(handle)) {
3801 retval = PTR_ERR(handle);
3802 handle = NULL;
3803 goto end_rename;
3804 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003805
3806 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3807 ext4_handle_sync(handle);
3808
3809 if (S_ISDIR(old.inode->i_mode)) {
3810 old.is_dir = true;
3811 retval = ext4_rename_dir_prepare(handle, &old);
3812 if (retval)
3813 goto end_rename;
3814 }
3815 if (S_ISDIR(new.inode->i_mode)) {
3816 new.is_dir = true;
3817 retval = ext4_rename_dir_prepare(handle, &new);
3818 if (retval)
3819 goto end_rename;
3820 }
3821
3822 /*
3823 * Other than the special case of overwriting a directory, parents'
3824 * nlink only needs to be modified if this is a cross directory rename.
3825 */
3826 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3827 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3828 new.dir_nlink_delta = -old.dir_nlink_delta;
3829 retval = -EMLINK;
3830 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3831 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3832 goto end_rename;
3833 }
3834
3835 new_file_type = new.de->file_type;
3836 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3837 if (retval)
3838 goto end_rename;
3839
3840 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3841 if (retval)
3842 goto end_rename;
3843
3844 /*
3845 * Like most other Unix systems, set the ctime for inodes on a
3846 * rename.
3847 */
3848 old.inode->i_ctime = ext4_current_time(old.inode);
3849 new.inode->i_ctime = ext4_current_time(new.inode);
3850 ext4_mark_inode_dirty(handle, old.inode);
3851 ext4_mark_inode_dirty(handle, new.inode);
3852
3853 if (old.dir_bh) {
3854 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3855 if (retval)
3856 goto end_rename;
3857 }
3858 if (new.dir_bh) {
3859 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3860 if (retval)
3861 goto end_rename;
3862 }
3863 ext4_update_dir_count(handle, &old);
3864 ext4_update_dir_count(handle, &new);
3865 retval = 0;
3866
3867end_rename:
3868 brelse(old.dir_bh);
3869 brelse(new.dir_bh);
3870 brelse(old.bh);
3871 brelse(new.bh);
3872 if (handle)
3873 ext4_journal_stop(handle);
3874 return retval;
3875}
3876
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003877static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3878 struct inode *new_dir, struct dentry *new_dentry,
3879 unsigned int flags)
3880{
Miklos Szeredicd808de2014-10-24 00:14:37 +02003881 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003882 return -EINVAL;
3883
Miklos Szeredibd429982014-04-01 17:08:44 +02003884 if (flags & RENAME_EXCHANGE) {
3885 return ext4_cross_rename(old_dir, old_dentry,
3886 new_dir, new_dentry);
3887 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003888
3889 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003890}
3891
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003892/*
3893 * directories can handle most operations...
3894 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003895const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003896 .create = ext4_create,
3897 .lookup = ext4_lookup,
3898 .link = ext4_link,
3899 .unlink = ext4_unlink,
3900 .symlink = ext4_symlink,
3901 .mkdir = ext4_mkdir,
3902 .rmdir = ext4_rmdir,
3903 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003904 .tmpfile = ext4_tmpfile,
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003905 .rename2 = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003906 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003907 .setxattr = generic_setxattr,
3908 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003909 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003910 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003911 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003912 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003913 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914};
3915
Arjan van de Ven754661f2007-02-12 00:55:38 -08003916const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003917 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003918 .setxattr = generic_setxattr,
3919 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003920 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003922 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003923 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003924};