blob: 4909ced4e672a4c8a7f2b313671c5dabf523e640 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/namei.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
16 * Big-endian to little-endian byte-swapping/bitmaps by
17 * David S. Miller (davem@caip.rutgers.edu), 1995
18 * Directory entry file type support and forward compatibility hooks
19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Hash Tree Directory indexing (c)
21 * Daniel Phillips, 2001
22 * Hash Tree Directory indexing porting
23 * Christopher Li, 2002
24 * Hash Tree Directory indexing cleanup
25 * Theodore Ts'o, 2002
26 */
27
28#include <linux/fs.h>
29#include <linux/pagemap.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/quotaops.h>
35#include <linux/buffer_head.h>
36#include <linux/bio.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050037#include <linux/iversion.h>
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -040038#include <linux/unicode.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040039#include "ext4.h"
40#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
44
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040045#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046/*
47 * define how far ahead to read directories while searching them.
48 */
49#define NAMEI_RA_CHUNKS 2
50#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040051#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052
Mingming Cao617ba132006-10-11 01:20:53 -070053static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050055 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056{
57 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040058 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070059
Theodore Ts'odf981d02012-08-17 09:48:17 -040060 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
61 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050062 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
63 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040064
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
66
Theodore Ts'oc5e298a2015-06-21 01:25:29 -040067 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
Theodore Ts'o1c215022014-08-29 20:52:15 -040068 if (IS_ERR(bh))
69 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050070 inode->i_size += inode->i_sb->s_blocksize;
71 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040072 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050073 err = ext4_journal_get_write_access(handle, bh);
74 if (err) {
75 brelse(bh);
76 ext4_std_error(inode->i_sb, err);
77 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040078 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070079 return bh;
80}
81
Theodore Ts'odc6982f2013-02-14 23:59:26 -050082static int ext4_dx_csum_verify(struct inode *inode,
83 struct ext4_dir_entry *dirent);
84
85typedef enum {
86 EITHER, INDEX, DIRENT
87} dirblock_type_t;
88
89#define ext4_read_dirblock(inode, block, type) \
Andreas Dilgerb03a2f72015-06-15 14:50:26 -040090 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
Theodore Ts'odc6982f2013-02-14 23:59:26 -050091
92static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
Andreas Dilgerb03a2f72015-06-15 14:50:26 -040093 ext4_lblk_t block,
94 dirblock_type_t type,
95 const char *func,
96 unsigned int line)
Theodore Ts'odc6982f2013-02-14 23:59:26 -050097{
98 struct buffer_head *bh;
99 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400100 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500101
Theodore Ts'o1c215022014-08-29 20:52:15 -0400102 bh = ext4_bread(NULL, inode, block, 0);
103 if (IS_ERR(bh)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400104 __ext4_warning(inode->i_sb, func, line,
105 "inode #%lu: lblock %lu: comm %s: "
106 "error %ld reading directory block",
107 inode->i_ino, (unsigned long)block,
108 current->comm, PTR_ERR(bh));
Theodore Ts'o1c215022014-08-29 20:52:15 -0400109
110 return bh;
111 }
112 if (!bh) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400113 ext4_error_inode(inode, func, line, block,
114 "Directory hole found");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400115 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500116 }
117 dirent = (struct ext4_dir_entry *) bh->b_data;
118 /* Determine whether or not we have an index block */
119 if (is_dx(inode)) {
120 if (block == 0)
121 is_dx_block = 1;
122 else if (ext4_rec_len_from_disk(dirent->rec_len,
123 inode->i_sb->s_blocksize) ==
124 inode->i_sb->s_blocksize)
125 is_dx_block = 1;
126 }
127 if (!is_dx_block && type == INDEX) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400128 ext4_error_inode(inode, func, line, block,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500129 "directory leaf block found instead of index block");
Vasily Averinde59fae2018-11-07 22:36:23 -0500130 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400131 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500132 }
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400133 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500134 buffer_verified(bh))
135 return bh;
136
137 /*
138 * An empty leaf block can get mistaken for a index block; for
139 * this reason, we can only check the index checksum when the
140 * caller is sure it should be an index block.
141 */
142 if (is_dx_block && type == INDEX) {
143 if (ext4_dx_csum_verify(inode, dirent))
144 set_buffer_verified(bh);
145 else {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400146 ext4_error_inode(inode, func, line, block,
147 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400149 return ERR_PTR(-EFSBADCRC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 }
151 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500152 if (!is_dx_block) {
153 if (ext4_dirent_csum_verify(inode, dirent))
154 set_buffer_verified(bh);
155 else {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400156 ext4_error_inode(inode, func, line, block,
157 "Directory block failed checksum");
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500158 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400159 return ERR_PTR(-EFSBADCRC);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500160 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 }
162 return bh;
163}
164
165#ifndef assert
166#define assert(test) J_ASSERT(test)
167#endif
168
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169#ifdef DX_DEBUG
170#define dxtrace(command) command
171#else
172#define dxtrace(command)
173#endif
174
175struct fake_dirent
176{
177 __le32 inode;
178 __le16 rec_len;
179 u8 name_len;
180 u8 file_type;
181};
182
183struct dx_countlimit
184{
185 __le16 limit;
186 __le16 count;
187};
188
189struct dx_entry
190{
191 __le32 hash;
192 __le32 block;
193};
194
195/*
196 * dx_root_info is laid out so that if it should somehow get overlaid by a
197 * dirent the two low bits of the hash version will be zero. Therefore, the
198 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
199 */
200
201struct dx_root
202{
203 struct fake_dirent dot;
204 char dot_name[4];
205 struct fake_dirent dotdot;
206 char dotdot_name[4];
207 struct dx_root_info
208 {
209 __le32 reserved_zero;
210 u8 hash_version;
211 u8 info_length; /* 8 */
212 u8 indirect_levels;
213 u8 unused_flags;
214 }
215 info;
216 struct dx_entry entries[0];
217};
218
219struct dx_node
220{
221 struct fake_dirent fake;
222 struct dx_entry entries[0];
223};
224
225
226struct dx_frame
227{
228 struct buffer_head *bh;
229 struct dx_entry *entries;
230 struct dx_entry *at;
231};
232
233struct dx_map_entry
234{
235 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700236 u16 offs;
237 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238};
239
Darrick J. Wonge6153912012-04-29 18:23:10 -0400240/*
241 * This goes at the end of each htree block.
242 */
243struct dx_tail {
244 u32 dt_reserved;
245 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
246};
247
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500248static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
249static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400250static inline unsigned dx_get_hash(struct dx_entry *entry);
251static void dx_set_hash(struct dx_entry *entry, unsigned value);
252static unsigned dx_get_count(struct dx_entry *entries);
253static unsigned dx_get_limit(struct dx_entry *entries);
254static void dx_set_count(struct dx_entry *entries, unsigned value);
255static void dx_set_limit(struct dx_entry *entries, unsigned value);
256static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
257static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400258static struct dx_frame *dx_probe(struct ext4_filename *fname,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 struct inode *dir,
260 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400261 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400262static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400263static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
264 unsigned blocksize, struct dx_hash_info *hinfo,
265 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400267static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500268 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500269static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500270static void dx_insert_block(struct dx_frame *frame,
271 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700272static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 struct dx_frame *frame,
274 struct dx_frame *frames,
275 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400276static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400277 struct ext4_filename *fname,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400278 struct ext4_dir_entry_2 **res_dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400279static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -0500280 struct inode *dir, struct inode *inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400282/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500283void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
284 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400285{
286 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
287 t->det_rec_len = ext4_rec_len_to_disk(
288 sizeof(struct ext4_dir_entry_tail), blocksize);
289 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
290}
291
292/* Walk through a dirent block to find a checksum "dirent" at the tail */
293static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
294 struct ext4_dir_entry *de)
295{
296 struct ext4_dir_entry_tail *t;
297
298#ifdef PARANOID
299 struct ext4_dir_entry *d, *top;
300
301 d = de;
302 top = (struct ext4_dir_entry *)(((void *)de) +
303 (EXT4_BLOCK_SIZE(inode->i_sb) -
304 sizeof(struct ext4_dir_entry_tail)));
305 while (d < top && d->rec_len)
306 d = (struct ext4_dir_entry *)(((void *)d) +
307 le16_to_cpu(d->rec_len));
308
309 if (d != top)
310 return NULL;
311
312 t = (struct ext4_dir_entry_tail *)d;
313#else
314 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
315#endif
316
317 if (t->det_reserved_zero1 ||
318 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
319 t->det_reserved_zero2 ||
320 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
321 return NULL;
322
323 return t;
324}
325
326static __le32 ext4_dirent_csum(struct inode *inode,
327 struct ext4_dir_entry *dirent, int size)
328{
329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
330 struct ext4_inode_info *ei = EXT4_I(inode);
331 __u32 csum;
332
333 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
334 return cpu_to_le32(csum);
335}
336
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400337#define warn_no_space_for_csum(inode) \
338 __warn_no_space_for_csum((inode), __func__, __LINE__)
339
340static void __warn_no_space_for_csum(struct inode *inode, const char *func,
341 unsigned int line)
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500342{
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400343 __ext4_warning_inode(inode, func, line,
344 "No space for directory leaf checksum. Please run e2fsck -D.");
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500345}
346
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400347int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
348{
349 struct ext4_dir_entry_tail *t;
350
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400351 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400352 return 1;
353
354 t = get_dirent_tail(inode, dirent);
355 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500356 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400357 return 0;
358 }
359
360 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
361 (void *)t - (void *)dirent))
362 return 0;
363
364 return 1;
365}
366
367static void ext4_dirent_csum_set(struct inode *inode,
368 struct ext4_dir_entry *dirent)
369{
370 struct ext4_dir_entry_tail *t;
371
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400372 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400373 return;
374
375 t = get_dirent_tail(inode, dirent);
376 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500377 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400378 return;
379 }
380
381 t->det_checksum = ext4_dirent_csum(inode, dirent,
382 (void *)t - (void *)dirent);
383}
384
Tao Ma3c47d542012-12-10 14:05:59 -0500385int ext4_handle_dirty_dirent_node(handle_t *handle,
386 struct inode *inode,
387 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400388{
389 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
390 return ext4_handle_dirty_metadata(handle, inode, bh);
391}
392
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400393static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
394 struct ext4_dir_entry *dirent,
395 int *offset)
396{
397 struct ext4_dir_entry *dp;
398 struct dx_root_info *root;
399 int count_offset;
400
401 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
402 count_offset = 8;
403 else if (le16_to_cpu(dirent->rec_len) == 12) {
404 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
405 if (le16_to_cpu(dp->rec_len) !=
406 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
407 return NULL;
408 root = (struct dx_root_info *)(((void *)dp + 12));
409 if (root->reserved_zero ||
410 root->info_length != sizeof(struct dx_root_info))
411 return NULL;
412 count_offset = 32;
413 } else
414 return NULL;
415
416 if (offset)
417 *offset = count_offset;
418 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
419}
420
421static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
422 int count_offset, int count, struct dx_tail *t)
423{
424 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
425 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400426 __u32 csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400427 int size;
Daeho Jeongb47820e2016-07-03 17:51:39 -0400428 __u32 dummy_csum = 0;
429 int offset = offsetof(struct dx_tail, dt_checksum);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400430
431 size = count_offset + (count * sizeof(struct dx_entry));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400432 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400433 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
434 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400435
436 return cpu_to_le32(csum);
437}
438
439static int ext4_dx_csum_verify(struct inode *inode,
440 struct ext4_dir_entry *dirent)
441{
442 struct dx_countlimit *c;
443 struct dx_tail *t;
444 int count_offset, limit, count;
445
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400446 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400447 return 1;
448
449 c = get_dx_countlimit(inode, dirent, &count_offset);
450 if (!c) {
451 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
Daeho Jeongfa964542016-07-03 21:11:08 -0400452 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400453 }
454 limit = le16_to_cpu(c->limit);
455 count = le16_to_cpu(c->count);
456 if (count_offset + (limit * sizeof(struct dx_entry)) >
457 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500458 warn_no_space_for_csum(inode);
Daeho Jeongfa964542016-07-03 21:11:08 -0400459 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400460 }
461 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
462
463 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
464 count, t))
465 return 0;
466 return 1;
467}
468
469static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
470{
471 struct dx_countlimit *c;
472 struct dx_tail *t;
473 int count_offset, limit, count;
474
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400475 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400476 return;
477
478 c = get_dx_countlimit(inode, dirent, &count_offset);
479 if (!c) {
480 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
481 return;
482 }
483 limit = le16_to_cpu(c->limit);
484 count = le16_to_cpu(c->count);
485 if (count_offset + (limit * sizeof(struct dx_entry)) >
486 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500487 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400488 return;
489 }
490 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
491
492 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
493}
494
495static inline int ext4_handle_dirty_dx_node(handle_t *handle,
496 struct inode *inode,
497 struct buffer_head *bh)
498{
499 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
500 return ext4_handle_dirty_metadata(handle, inode, bh);
501}
502
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503/*
Li Zefanf795e142008-07-11 19:27:31 -0400504 * p is at least 6 bytes before the end of page
505 */
506static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500507ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400508{
509 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500510 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400511}
512
513/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 * Future: use high four bits of block for coalesce-on-delete flags
515 * Mask them off for now.
516 */
517
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500518static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400520 return le32_to_cpu(entry->block) & 0x0fffffff;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521}
522
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500523static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524{
525 entry->block = cpu_to_le32(value);
526}
527
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400528static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529{
530 return le32_to_cpu(entry->hash);
531}
532
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400533static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534{
535 entry->hash = cpu_to_le32(value);
536}
537
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400538static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539{
540 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
541}
542
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400543static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544{
545 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
546}
547
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400548static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
550 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
551}
552
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400553static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554{
555 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
556}
557
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400558static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
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(1) -
561 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400562
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400563 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400564 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400565 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566}
567
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400568static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569{
Mingming Cao617ba132006-10-11 01:20:53 -0700570 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400571
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400572 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400573 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400574 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575}
576
577/*
578 * Debug
579 */
580#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400581static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582{
Andrew Morton63f57932006-10-11 01:21:24 -0700583 int i, n = dx_get_count (entries);
Joe Perchesd74f3d22016-10-15 09:57:31 -0400584 printk(KERN_DEBUG "%s index", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700585 for (i = 0; i < n; i++) {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400586 printk(KERN_CONT " %x->%lu",
587 i ? dx_get_hash(entries + i) : 0,
588 (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700589 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400590 printk(KERN_CONT "\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700591}
592
593struct stats
594{
595 unsigned names;
596 unsigned space;
597 unsigned bcount;
598};
599
Michael Halcrowb3098482015-04-12 01:07:01 -0400600static struct stats dx_show_leaf(struct inode *dir,
601 struct dx_hash_info *hinfo,
602 struct ext4_dir_entry_2 *de,
603 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604{
605 unsigned names = 0, space = 0;
606 char *base = (char *) de;
607 struct dx_hash_info h = *hinfo;
608
609 printk("names: ");
610 while ((char *) de < base + size)
611 {
612 if (de->inode)
613 {
614 if (show_names)
615 {
Chandan Rajendra643fa962018-12-12 15:20:12 +0530616#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrowb3098482015-04-12 01:07:01 -0400617 int len;
618 char *name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400619 struct fscrypt_str fname_crypto_str =
620 FSTR_INIT(NULL, 0);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400621 int res = 0;
Michael Halcrowb3098482015-04-12 01:07:01 -0400622
623 name = de->name;
624 len = de->name_len;
Chandan Rajendra592ddec2018-12-12 15:20:10 +0530625 if (IS_ENCRYPTED(dir))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400626 res = fscrypt_get_encryption_info(dir);
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400627 if (res) {
628 printk(KERN_WARNING "Error setting up"
629 " fname crypto: %d\n", res);
Michael Halcrowb3098482015-04-12 01:07:01 -0400630 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400631 if (!fscrypt_has_encryption_key(dir)) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400632 /* Directory is not encrypted */
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400633 ext4fs_dirhash(dir, de->name,
Michael Halcrowb3098482015-04-12 01:07:01 -0400634 de->name_len, &h);
635 printk("%*.s:(U)%x.%u ", len,
636 name, h.hash,
637 (unsigned) ((char *) de
638 - base));
639 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400640 struct fscrypt_str de_name =
641 FSTR_INIT(name, len);
642
Michael Halcrowb3098482015-04-12 01:07:01 -0400643 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400644 res = fscrypt_fname_alloc_buffer(
645 dir, len,
Michael Halcrowb3098482015-04-12 01:07:01 -0400646 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400647 if (res)
Michael Halcrowb3098482015-04-12 01:07:01 -0400648 printk(KERN_WARNING "Error "
649 "allocating crypto "
650 "buffer--skipping "
651 "crypto\n");
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400652 res = fscrypt_fname_disk_to_usr(dir,
653 0, 0, &de_name,
654 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400655 if (res) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400656 printk(KERN_WARNING "Error "
657 "converting filename "
658 "from disk to usr"
659 "\n");
660 name = "??";
661 len = 2;
662 } else {
663 name = fname_crypto_str.name;
664 len = fname_crypto_str.len;
665 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400666 ext4fs_dirhash(dir, de->name,
667 de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400668 printk("%*.s:(E)%x.%u ", len, name,
669 h.hash, (unsigned) ((char *) de
670 - base));
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400671 fscrypt_fname_free_buffer(
672 &fname_crypto_str);
Michael Halcrowb3098482015-04-12 01:07:01 -0400673 }
674#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 int len = de->name_len;
676 char *name = de->name;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400677 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400678 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400679 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400680#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 }
Mingming Cao617ba132006-10-11 01:20:53 -0700682 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 names++;
684 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500685 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400687 printk(KERN_CONT "(%i)\n", names);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700688 return (struct stats) { names, space, 1 };
689}
690
691struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
692 struct dx_entry *entries, int levels)
693{
694 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400695 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 unsigned bcount = 0;
697 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 printk("%i indexed blocks...\n", count);
699 for (i = 0; i < count; i++, entries++)
700 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500701 ext4_lblk_t block = dx_get_block(entries);
702 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
704 struct stats stats;
705 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400706 bh = ext4_bread(NULL,dir, block, 0);
707 if (!bh || IS_ERR(bh))
708 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 stats = levels?
710 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400711 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
712 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 names += stats.names;
714 space += stats.space;
715 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400716 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 }
718 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400719 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400720 levels ? "" : " ", names, space/bcount,
721 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 return (struct stats) { names, space, bcount};
723}
724#endif /* DX_DEBUG */
725
726/*
727 * Probe for a directory leaf block to search.
728 *
729 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
730 * error in the directory index, and the caller should fall back to
731 * searching the directory normally. The callers of dx_probe **MUST**
732 * check for this error code, and make sure it never gets reflected
733 * back to userspace.
734 */
735static struct dx_frame *
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400736dx_probe(struct ext4_filename *fname, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400737 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738{
739 unsigned count, indirect;
740 struct dx_entry *at, *entries, *p, *q, *m;
741 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400743 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744 u32 hash;
745
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400746 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400747 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
748 if (IS_ERR(frame->bh))
749 return (struct dx_frame *) frame->bh;
750
751 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 if (root->info.hash_version != DX_HASH_TEA &&
753 root->info.hash_version != DX_HASH_HALF_MD4 &&
754 root->info.hash_version != DX_HASH_LEGACY) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400755 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
756 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 goto fail;
758 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400759 if (fname)
760 hinfo = &fname->hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400762 if (hinfo->hash_version <= DX_HASH_TEA)
763 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700764 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400765 if (fname && fname_name(fname))
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -0400766 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 hash = hinfo->hash;
768
769 if (root->info.unused_flags & 1) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400770 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
771 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 goto fail;
773 }
774
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400775 indirect = root->info.indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400776 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
777 ext4_warning(dir->i_sb,
778 "Directory (ino: %lu) htree depth %#06x exceed"
779 "supported value", dir->i_ino,
780 ext4_dir_htree_level(dir->i_sb));
781 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
782 ext4_warning(dir->i_sb, "Enable large directory "
783 "feature to access it");
784 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 goto fail;
786 }
787
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400788 entries = (struct dx_entry *)(((char *)&root->info) +
789 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700790
791 if (dx_get_limit(entries) != dx_root_limit(dir,
792 root->info.info_length)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400793 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
794 dx_get_limit(entries),
795 dx_root_limit(dir, root->info.info_length));
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700796 goto fail;
797 }
798
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400799 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400800 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700802 if (!count || count > dx_get_limit(entries)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400803 ext4_warning_inode(dir,
804 "dx entry: count %u beyond limit %u",
805 count, dx_get_limit(entries));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400806 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700807 }
808
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 p = entries + 1;
810 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400811 while (p <= q) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400812 m = p + (q - p) / 2;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400813 dxtrace(printk(KERN_CONT "."));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 if (dx_get_hash(m) > hash)
815 q = m - 1;
816 else
817 p = m + 1;
818 }
819
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400820 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700821 unsigned n = count - 1;
822 at = entries;
823 while (n--)
824 {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400825 dxtrace(printk(KERN_CONT ","));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826 if (dx_get_hash(++at) > hash)
827 {
828 at--;
829 break;
830 }
831 }
832 assert (at == p - 1);
833 }
834
835 at = p - 1;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400836 dxtrace(printk(KERN_CONT " %x->%u\n",
837 at == entries ? 0 : dx_get_hash(at),
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400838 dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 frame->entries = entries;
840 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400841 if (!indirect--)
842 return frame;
843 frame++;
844 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
845 if (IS_ERR(frame->bh)) {
846 ret_err = (struct dx_frame *) frame->bh;
847 frame->bh = NULL;
848 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400849 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400850 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400851
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400852 if (dx_get_limit(entries) != dx_node_limit(dir)) {
853 ext4_warning_inode(dir,
854 "dx entry: limit %u != node limit %u",
855 dx_get_limit(entries), dx_node_limit(dir));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400856 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700857 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400859fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 while (frame >= frame_in) {
861 brelse(frame->bh);
862 frame--;
863 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400864
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400865 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400866 ext4_warning_inode(dir,
867 "Corrupt directory, running e2fsck is recommended");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400868 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869}
870
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400871static void dx_release(struct dx_frame *frames)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400873 struct dx_root_info *info;
874 int i;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400875 unsigned int indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400876
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 if (frames[0].bh == NULL)
878 return;
879
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400880 info = &((struct dx_root *)frames[0].bh->b_data)->info;
Sahitya Tummala08fc98a2019-05-10 22:00:33 -0400881 /* save local copy, "info" may be freed after brelse() */
882 indirect_levels = info->indirect_levels;
883 for (i = 0; i <= indirect_levels; i++) {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400884 if (frames[i].bh == NULL)
885 break;
886 brelse(frames[i].bh);
887 frames[i].bh = NULL;
888 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889}
890
891/*
892 * This function increments the frame pointer to search the next leaf
893 * block, and reads in the necessary intervening nodes if the search
894 * should be necessary. Whether or not the search is necessary is
895 * controlled by the hash parameter. If the hash value is even, then
896 * the search is only continued if the next block starts with that
897 * hash value. This is used if we are searching for a specific file.
898 *
899 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
900 *
901 * This function returns 1 if the caller should continue to search,
902 * or 0 if it should not. If there is an error reading one of the
903 * index blocks, it will a negative error code.
904 *
905 * If start_hash is non-null, it will be filled in with the starting
906 * hash of the next page.
907 */
Mingming Cao617ba132006-10-11 01:20:53 -0700908static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 struct dx_frame *frame,
910 struct dx_frame *frames,
911 __u32 *start_hash)
912{
913 struct dx_frame *p;
914 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500915 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916 __u32 bhash;
917
918 p = frame;
919 /*
920 * Find the next leaf page by incrementing the frame pointer.
921 * If we run out of entries in the interior node, loop around and
922 * increment pointer in the parent node. When we break out of
923 * this loop, num_frames indicates the number of interior
924 * nodes need to be read.
925 */
926 while (1) {
927 if (++(p->at) < p->entries + dx_get_count(p->entries))
928 break;
929 if (p == frames)
930 return 0;
931 num_frames++;
932 p--;
933 }
934
935 /*
936 * If the hash is 1, then continue only if the next page has a
937 * continuation hash of any value. This is used for readdir
938 * handling. Otherwise, check to see if the hash matches the
939 * desired contiuation hash. If it doesn't, return since
940 * there's no point to read in the successive index pages.
941 */
942 bhash = dx_get_hash(p->at);
943 if (start_hash)
944 *start_hash = bhash;
945 if ((hash & 1) == 0) {
946 if ((bhash & ~1) != hash)
947 return 0;
948 }
949 /*
950 * If the hash is HASH_NB_ALWAYS, we always go to the next
951 * block so no check is necessary
952 */
953 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500954 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
955 if (IS_ERR(bh))
956 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400958 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 p->bh = bh;
960 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
961 }
962 return 1;
963}
964
965
966/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 * This function fills a red-black tree with information from a
968 * directory block. It returns the number directory entries loaded
969 * into the tree. If there is an error it is returned in err.
970 */
971static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500972 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 struct dx_hash_info *hinfo,
974 __u32 start_hash, __u32 start_minor_hash)
975{
976 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700977 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400978 int err = 0, count = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400979 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500981 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
982 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500983 bh = ext4_read_dirblock(dir, block, DIRENT);
984 if (IS_ERR(bh))
985 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400986
Mingming Cao617ba132006-10-11 01:20:53 -0700987 de = (struct ext4_dir_entry_2 *) bh->b_data;
988 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700990 EXT4_DIR_REC_LEN(0));
Chandan Rajendra643fa962018-12-12 15:20:12 +0530991#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400992 /* Check if the directory is encrypted */
Chandan Rajendra592ddec2018-12-12 15:20:10 +0530993 if (IS_ENCRYPTED(dir)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400994 err = fscrypt_get_encryption_info(dir);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400995 if (err < 0) {
996 brelse(bh);
997 return err;
998 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400999 err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001000 &fname_crypto_str);
1001 if (err < 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001002 brelse(bh);
1003 return err;
1004 }
1005 }
1006#endif
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001007 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001008 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -05001009 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001010 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1011 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001012 /* silently ignore the rest of the block */
1013 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001014 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001015 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 if ((hinfo->hash < start_hash) ||
1017 ((hinfo->hash == start_hash) &&
1018 (hinfo->minor_hash < start_minor_hash)))
1019 continue;
1020 if (de->inode == 0)
1021 continue;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301022 if (!IS_ENCRYPTED(dir)) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001023 tmp_str.name = de->name;
1024 tmp_str.len = de->name_len;
1025 err = ext4_htree_store_dirent(dir_file,
1026 hinfo->hash, hinfo->minor_hash, de,
1027 &tmp_str);
1028 } else {
Theodore Ts'od2299592015-05-18 13:15:47 -04001029 int save_len = fname_crypto_str.len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001030 struct fscrypt_str de_name = FSTR_INIT(de->name,
1031 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -04001032
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001033 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001034 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1035 hinfo->minor_hash, &de_name,
1036 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001037 if (err) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001038 count = err;
1039 goto errout;
1040 }
1041 err = ext4_htree_store_dirent(dir_file,
1042 hinfo->hash, hinfo->minor_hash, de,
1043 &fname_crypto_str);
Theodore Ts'od2299592015-05-18 13:15:47 -04001044 fname_crypto_str.len = save_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001045 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001046 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001047 count = err;
1048 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 }
1050 count++;
1051 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001052errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 brelse(bh);
Chandan Rajendra643fa962018-12-12 15:20:12 +05301054#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001055 fscrypt_fname_free_buffer(&fname_crypto_str);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001056#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 return count;
1058}
1059
1060
1061/*
1062 * This function fills a red-black tree with information from a
1063 * directory. We start scanning the directory in hash order, starting
1064 * at start_hash and start_minor_hash.
1065 *
1066 * This function returns the number of entries inserted into the tree,
1067 * or a negative error code.
1068 */
Mingming Cao617ba132006-10-11 01:20:53 -07001069int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 __u32 start_minor_hash, __u32 *next_hash)
1071{
1072 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001073 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001074 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001076 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001078 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 __u32 hashval;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001080 struct fscrypt_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081
Theodore Ts'o60e66792010-05-17 07:00:00 -04001082 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001083 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001084 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001085 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001086 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001087 if (hinfo.hash_version <= DX_HASH_TEA)
1088 hinfo.hash_version +=
1089 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001090 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001091 if (ext4_has_inline_data(dir)) {
1092 int has_inline_data = 1;
1093 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1094 &hinfo, start_hash,
1095 start_minor_hash,
1096 &has_inline_data);
1097 if (has_inline_data) {
1098 *next_hash = ~0;
1099 return count;
1100 }
1101 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1103 start_hash, start_minor_hash);
1104 *next_hash = ~0;
1105 return count;
1106 }
1107 hinfo.hash = start_hash;
1108 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001109 frame = dx_probe(NULL, dir, &hinfo, frames);
1110 if (IS_ERR(frame))
1111 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112
1113 /* Add '.' and '..' from the htree header */
1114 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001115 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001116 tmp_str.name = de->name;
1117 tmp_str.len = de->name_len;
1118 err = ext4_htree_store_dirent(dir_file, 0, 0,
1119 de, &tmp_str);
1120 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 goto errout;
1122 count++;
1123 }
1124 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001125 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001126 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001127 tmp_str.name = de->name;
1128 tmp_str.len = de->name_len;
1129 err = ext4_htree_store_dirent(dir_file, 2, 0,
1130 de, &tmp_str);
1131 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 goto errout;
1133 count++;
1134 }
1135
1136 while (1) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -04001137 if (fatal_signal_pending(current)) {
1138 err = -ERESTARTSYS;
1139 goto errout;
1140 }
1141 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142 block = dx_get_block(frame->at);
1143 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1144 start_hash, start_minor_hash);
1145 if (ret < 0) {
1146 err = ret;
1147 goto errout;
1148 }
1149 count += ret;
1150 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001151 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 frame, frames, &hashval);
1153 *next_hash = hashval;
1154 if (ret < 0) {
1155 err = ret;
1156 goto errout;
1157 }
1158 /*
1159 * Stop if: (a) there are no more entries, or
1160 * (b) we have inserted at least one entry and the
1161 * next hash value is not a continuation
1162 */
1163 if ((ret == 0) ||
1164 (count && ((hashval & 1) == 0)))
1165 break;
1166 }
1167 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001168 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1169 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 return count;
1171errout:
1172 dx_release(frames);
1173 return (err);
1174}
1175
Tao Ma7335cd32012-12-10 14:05:59 -05001176static inline int search_dirblock(struct buffer_head *bh,
1177 struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001178 struct ext4_filename *fname,
Tao Ma7335cd32012-12-10 14:05:59 -05001179 unsigned int offset,
1180 struct ext4_dir_entry_2 **res_dir)
1181{
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001182 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
Eric Biggersd6b97552017-05-24 18:10:49 -04001183 fname, offset, res_dir);
Tao Ma7335cd32012-12-10 14:05:59 -05001184}
1185
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186/*
1187 * Directory block splitting, compacting
1188 */
1189
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001190/*
1191 * Create map of hash values, offsets, and sizes, stored at end of block.
1192 * Returns number of entries mapped.
1193 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001194static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1195 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001196 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197{
1198 int count = 0;
1199 char *base = (char *) de;
1200 struct dx_hash_info h = *hinfo;
1201
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001202 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 if (de->name_len && de->inode) {
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001204 ext4fs_dirhash(dir, de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205 map_tail--;
1206 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001207 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001208 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 count++;
1210 cond_resched();
1211 }
1212 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001213 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001214 }
1215 return count;
1216}
1217
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001218/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1220{
Andrew Morton63f57932006-10-11 01:21:24 -07001221 struct dx_map_entry *p, *q, *top = map + count - 1;
1222 int more;
1223 /* Combsort until bubble sort doesn't suck */
1224 while (count > 2) {
1225 count = count*10/13;
1226 if (count - 9 < 2) /* 9, 10 -> 11 */
1227 count = 11;
1228 for (p = top, q = p - count; q >= map; p--, q--)
1229 if (p->hash < q->hash)
1230 swap(*p, *q);
1231 }
1232 /* Garden variety bubble sort */
1233 do {
1234 more = 0;
1235 q = top;
1236 while (q-- > map) {
1237 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001239 swap(*(q+1), *q);
1240 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 }
1242 } while(more);
1243}
1244
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001245static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246{
1247 struct dx_entry *entries = frame->entries;
1248 struct dx_entry *old = frame->at, *new = old + 1;
1249 int count = dx_get_count(entries);
1250
1251 assert(count < dx_get_limit(entries));
1252 assert(old < entries + count);
1253 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1254 dx_set_hash(new, hash);
1255 dx_set_block(new, block);
1256 dx_set_count(entries, count + 1);
1257}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001259#ifdef CONFIG_UNICODE
1260/*
1261 * Test whether a case-insensitive directory entry matches the filename
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001262 * being searched for. If quick is set, assume the name being looked up
1263 * is already in the casefolded form.
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001264 *
1265 * Returns: 0 if the directory entry matches, more than 0 if it
1266 * doesn't match or less than zero on error.
1267 */
1268int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001269 const struct qstr *entry, bool quick)
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001270{
1271 const struct ext4_sb_info *sbi = EXT4_SB(parent->i_sb);
1272 const struct unicode_map *um = sbi->s_encoding;
1273 int ret;
1274
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001275 if (quick)
1276 ret = utf8_strncasecmp_folded(um, name, entry);
1277 else
1278 ret = utf8_strncasecmp(um, name, entry);
1279
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001280 if (ret < 0) {
1281 /* Handle invalid character sequence as either an error
1282 * or as an opaque byte sequence.
1283 */
1284 if (ext4_has_strict_mode(sbi))
1285 return -EINVAL;
1286
1287 if (name->len != entry->len)
1288 return 1;
1289
1290 return !!memcmp(name->name, entry->name, name->len);
1291 }
1292
1293 return ret;
1294}
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001295
1296void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1297 struct fscrypt_str *cf_name)
1298{
1299 if (!IS_CASEFOLDED(dir)) {
1300 cf_name->name = NULL;
1301 return;
1302 }
1303
1304 cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1305 if (!cf_name->name)
1306 return;
1307
1308 cf_name->len = utf8_casefold(EXT4_SB(dir->i_sb)->s_encoding,
1309 iname, cf_name->name,
1310 EXT4_NAME_LEN);
1311 if (cf_name->len <= 0) {
1312 kfree(cf_name->name);
1313 cf_name->name = NULL;
1314 }
1315}
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001316#endif
1317
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318/*
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001319 * Test whether a directory entry matches the filename being searched for.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320 *
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001321 * Return: %true if the directory entry matches, otherwise %false.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 */
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001323static inline bool ext4_match(const struct inode *parent,
1324 const struct ext4_filename *fname,
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001325 const struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326{
Eric Biggers067d1022017-04-24 10:00:11 -07001327 struct fscrypt_name f;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001328#ifdef CONFIG_UNICODE
1329 const struct qstr entry = {.name = de->name, .len = de->name_len};
1330#endif
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001331
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332 if (!de->inode)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001333 return false;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001334
Eric Biggers067d1022017-04-24 10:00:11 -07001335 f.usr_fname = fname->usr_fname;
1336 f.disk_name = fname->disk_name;
Chandan Rajendra643fa962018-12-12 15:20:12 +05301337#ifdef CONFIG_FS_ENCRYPTION
Eric Biggers067d1022017-04-24 10:00:11 -07001338 f.crypto_buf = fname->crypto_buf;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001339#endif
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001340
1341#ifdef CONFIG_UNICODE
Gabriel Krisman Bertazi3ae72562019-06-19 23:45:09 -04001342 if (EXT4_SB(parent->i_sb)->s_encoding && IS_CASEFOLDED(parent)) {
1343 if (fname->cf_name.name) {
1344 struct qstr cf = {.name = fname->cf_name.name,
1345 .len = fname->cf_name.len};
1346 return !ext4_ci_compare(parent, &cf, &entry, true);
1347 }
1348 return !ext4_ci_compare(parent, fname->usr_fname, &entry,
1349 false);
1350 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001351#endif
1352
Eric Biggers067d1022017-04-24 10:00:11 -07001353 return fscrypt_match_name(&f, de->name, de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354}
1355
1356/*
1357 * Returns 0 if not found, -1 on failure, and 1 on success
1358 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001359int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1360 struct inode *dir, struct ext4_filename *fname,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001361 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362{
Mingming Cao617ba132006-10-11 01:20:53 -07001363 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 char * dlimit;
1365 int de_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001366
Tao Ma7335cd32012-12-10 14:05:59 -05001367 de = (struct ext4_dir_entry_2 *)search_buf;
1368 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 while ((char *) de < dlimit) {
1370 /* this code is executed quadratically often */
1371 /* do minimal checking `by hand' */
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001372 if ((char *) de + de->name_len <= dlimit &&
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001373 ext4_match(dir, fname, de)) {
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001374 /* found a match - just to be sure, do
1375 * a full check */
1376 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1377 bh->b_size, offset))
1378 return -1;
1379 *res_dir = de;
1380 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 }
1382 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001383 de_len = ext4_rec_len_from_disk(de->rec_len,
1384 dir->i_sb->s_blocksize);
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001385 if (de_len <= 0)
1386 return -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001388 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389 }
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001390 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391}
1392
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001393static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1394 struct ext4_dir_entry *de)
1395{
1396 struct super_block *sb = dir->i_sb;
1397
1398 if (!is_dx(dir))
1399 return 0;
1400 if (block == 0)
1401 return 1;
1402 if (de->inode == 0 &&
1403 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1404 sb->s_blocksize)
1405 return 1;
1406 return 0;
1407}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408
1409/*
Eric Biggersb01531d2019-03-20 11:39:13 -07001410 * __ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 *
1412 * finds an entry in the specified directory with the wanted name. It
1413 * returns the cache buffer in which the entry was found, and the entry
1414 * itself (as a parameter - res_dir). It does NOT read the inode of the
1415 * entry - you'll have to do that yourself if you want to.
1416 *
1417 * The returned buffer_head has ->b_count elevated. The caller is expected
1418 * to brelse() it when appropriate.
1419 */
Eric Biggersb01531d2019-03-20 11:39:13 -07001420static struct buffer_head *__ext4_find_entry(struct inode *dir,
1421 struct ext4_filename *fname,
1422 struct ext4_dir_entry_2 **res_dir,
1423 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001425 struct super_block *sb;
1426 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1427 struct buffer_head *bh, *ret = NULL;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001428 ext4_lblk_t start, block;
Eric Biggersb01531d2019-03-20 11:39:13 -07001429 const u8 *name = fname->usr_fname->name;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001430 size_t ra_max = 0; /* Number of bh's in the readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 buffer, bh_use[] */
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001432 size_t ra_ptr = 0; /* Current index into readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 buffer */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001434 ext4_lblk_t nblocks;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001435 int i, namelen, retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436
1437 *res_dir = NULL;
1438 sb = dir->i_sb;
Eric Biggersb01531d2019-03-20 11:39:13 -07001439 namelen = fname->usr_fname->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001440 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001442
1443 if (ext4_has_inline_data(dir)) {
1444 int has_inline_data = 1;
Eric Biggersb01531d2019-03-20 11:39:13 -07001445 ret = ext4_find_inline_entry(dir, fname, res_dir,
Tao Mae8e948e2012-12-10 14:06:00 -05001446 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001447 if (has_inline_data) {
1448 if (inlined)
1449 *inlined = 1;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001450 goto cleanup_and_exit;
Tao Ma32f7f222012-12-10 14:06:01 -05001451 }
Tao Mae8e948e2012-12-10 14:06:00 -05001452 }
1453
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001454 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001455 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001456 /*
1457 * "." or ".." will only be in the first block
1458 * NFS may look up ".."; "." should be handled by the VFS
1459 */
1460 block = start = 0;
1461 nblocks = 1;
1462 goto restart;
1463 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464 if (is_dx(dir)) {
Eric Biggersb01531d2019-03-20 11:39:13 -07001465 ret = ext4_dx_find_entry(dir, fname, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 /*
1467 * On success, or if the error was file not found,
1468 * return. Otherwise, fall back to doing a search the
1469 * old fashioned way.
1470 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001471 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1472 goto cleanup_and_exit;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001473 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1474 "falling back\n"));
Eric Sandeenf39b3f42018-07-29 17:13:42 -04001475 ret = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 }
Mingming Cao617ba132006-10-11 01:20:53 -07001477 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Chandan Rajendra9d5afec2017-12-11 15:00:57 -05001478 if (!nblocks) {
1479 ret = NULL;
1480 goto cleanup_and_exit;
1481 }
Mingming Cao617ba132006-10-11 01:20:53 -07001482 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 if (start >= nblocks)
1484 start = 0;
1485 block = start;
1486restart:
1487 do {
1488 /*
1489 * We deal with the read-ahead logic here.
1490 */
1491 if (ra_ptr >= ra_max) {
1492 /* Refill the readahead buffer */
1493 ra_ptr = 0;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001494 if (block < start)
1495 ra_max = start - block;
1496 else
1497 ra_max = nblocks - block;
1498 ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1499 retval = ext4_bread_batch(dir, block, ra_max,
1500 false /* wait */, bh_use);
1501 if (retval) {
1502 ret = ERR_PTR(retval);
1503 ra_max = 0;
1504 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 }
1506 }
1507 if ((bh = bh_use[ra_ptr++]) == NULL)
1508 goto next;
1509 wait_on_buffer(bh);
1510 if (!buffer_uptodate(bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001511 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1512 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 brelse(bh);
Khazhismel Kumykov6febe6f2017-06-23 00:29:05 -04001514 ret = ERR_PTR(-EIO);
1515 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001517 if (!buffer_verified(bh) &&
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001518 !is_dx_internal_node(dir, block,
1519 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001520 !ext4_dirent_csum_verify(dir,
1521 (struct ext4_dir_entry *)bh->b_data)) {
1522 EXT4_ERROR_INODE(dir, "checksumming directory "
1523 "block %lu", (unsigned long)block);
1524 brelse(bh);
Theodore Ts'obdddf342017-06-23 00:47:05 -04001525 ret = ERR_PTR(-EFSBADCRC);
1526 goto cleanup_and_exit;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001527 }
1528 set_buffer_verified(bh);
Eric Biggersb01531d2019-03-20 11:39:13 -07001529 i = search_dirblock(bh, dir, fname,
Mingming Cao617ba132006-10-11 01:20:53 -07001530 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001531 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001532 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533 ret = bh;
1534 goto cleanup_and_exit;
1535 } else {
1536 brelse(bh);
1537 if (i < 0)
1538 goto cleanup_and_exit;
1539 }
1540 next:
1541 if (++block >= nblocks)
1542 block = 0;
1543 } while (block != start);
1544
1545 /*
1546 * If the directory has grown while we were searching, then
1547 * search the last part of the directory before giving up.
1548 */
1549 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001550 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 if (block < nblocks) {
1552 start = 0;
1553 goto restart;
1554 }
1555
1556cleanup_and_exit:
1557 /* Clean up the read-ahead blocks */
1558 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001559 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 return ret;
1561}
1562
Eric Biggersb01531d2019-03-20 11:39:13 -07001563static struct buffer_head *ext4_find_entry(struct inode *dir,
1564 const struct qstr *d_name,
1565 struct ext4_dir_entry_2 **res_dir,
1566 int *inlined)
1567{
1568 int err;
1569 struct ext4_filename fname;
1570 struct buffer_head *bh;
1571
1572 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1573 if (err == -ENOENT)
1574 return NULL;
1575 if (err)
1576 return ERR_PTR(err);
1577
1578 bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1579
1580 ext4_fname_free_filename(&fname);
1581 return bh;
1582}
1583
1584static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1585 struct dentry *dentry,
1586 struct ext4_dir_entry_2 **res_dir)
1587{
1588 int err;
1589 struct ext4_filename fname;
1590 struct buffer_head *bh;
1591
1592 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1593 if (err == -ENOENT)
1594 return NULL;
1595 if (err)
1596 return ERR_PTR(err);
1597
1598 bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1599
1600 ext4_fname_free_filename(&fname);
1601 return bh;
1602}
1603
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001604static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1605 struct ext4_filename *fname,
1606 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001608 struct super_block * sb = dir->i_sb;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001609 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001611 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613
Chandan Rajendra643fa962018-12-12 15:20:12 +05301614#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001615 *res_dir = NULL;
1616#endif
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001617 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001618 if (IS_ERR(frame))
1619 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001620 do {
1621 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001622 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001623 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001625
Eric Biggersd6b97552017-05-24 18:10:49 -04001626 retval = search_dirblock(bh, dir, fname,
Theodore Ts'o7845c042010-10-27 21:30:08 -04001627 block << EXT4_BLOCK_SIZE_BITS(sb),
1628 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001629 if (retval == 1)
1630 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001631 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001632 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001633 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001634 goto errout;
1635 }
1636
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 /* Check to see if we should continue to search */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001638 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 frames, NULL);
1640 if (retval < 0) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04001641 ext4_warning_inode(dir,
1642 "error %d reading directory index block",
1643 retval);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001644 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645 goto errout;
1646 }
1647 } while (retval == 1);
1648
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001649 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650errout:
Eric Biggersd6b97552017-05-24 18:10:49 -04001651 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001652success:
1653 dx_release(frames);
1654 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001655}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656
Al Viro00cd8dd2012-06-10 17:13:09 -04001657static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001658{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001659 struct inode *inode;
1660 struct ext4_dir_entry_2 *de;
1661 struct buffer_head *bh;
Theodore Ts'o28b4c262016-02-07 19:35:05 -05001662
Eric Biggers89904272017-10-18 20:21:58 -04001663 if (dentry->d_name.len > EXT4_NAME_LEN)
1664 return ERR_PTR(-ENAMETOOLONG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001665
Eric Biggersb01531d2019-03-20 11:39:13 -07001666 bh = ext4_lookup_entry(dir, dentry, &de);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001667 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001668 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 inode = NULL;
1670 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001671 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001672 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001673 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001674 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001675 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001676 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001677 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001678 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1679 dentry);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001680 return ERR_PTR(-EFSCORRUPTED);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001681 }
Theodore Ts'o8a363972018-12-19 12:29:13 -05001682 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
Al Viroa9049372011-07-08 21:20:11 -04001683 if (inode == ERR_PTR(-ESTALE)) {
1684 EXT4_ERROR_INODE(dir,
1685 "deleted inode referenced: %u",
1686 ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001687 return ERR_PTR(-EFSCORRUPTED);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001688 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05301689 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
Theodore Ts'off978b02016-02-08 00:54:26 -05001690 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001691 !fscrypt_has_permitted_context(dir, inode)) {
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001692 ext4_warning(inode->i_sb,
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001693 "Inconsistent encryption contexts: %lu/%lu",
Eric Biggers8c680842017-04-07 10:58:38 -07001694 dir->i_ino, inode->i_ino);
Eric Biggersdd01b692017-02-01 21:07:11 -05001695 iput(inode);
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001696 return ERR_PTR(-EPERM);
1697 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001699
1700#ifdef CONFIG_UNICODE
1701 if (!inode && IS_CASEFOLDED(dir)) {
1702 /* Eventually we want to call d_add_ci(dentry, NULL)
1703 * for negative dentries in the encoding case as
1704 * well. For now, prevent the negative dentry
1705 * from being cached.
1706 */
1707 return NULL;
1708 }
1709#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 return d_splice_alias(inode, dentry);
1711}
1712
1713
Mingming Cao617ba132006-10-11 01:20:53 -07001714struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001715{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001716 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001717 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001718 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 struct buffer_head *bh;
1720
David Howells2b0143b2015-03-17 22:25:59 +00001721 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001722 if (IS_ERR(bh))
Al Viroe884bce2018-10-10 16:41:40 -04001723 return ERR_CAST(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 if (!bh)
1725 return ERR_PTR(-ENOENT);
1726 ino = le32_to_cpu(de->inode);
1727 brelse(bh);
1728
Al Virofc640052016-04-10 01:33:30 -04001729 if (!ext4_valid_inum(child->d_sb, ino)) {
David Howells2b0143b2015-03-17 22:25:59 +00001730 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001731 "bad parent inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001732 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001733 }
1734
Theodore Ts'o8a363972018-12-19 12:29:13 -05001735 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736}
1737
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001738/*
1739 * Move count entries from end of map between two memory locations.
1740 * Returns pointer to last entry moved.
1741 */
Mingming Cao617ba132006-10-11 01:20:53 -07001742static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001743dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1744 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745{
1746 unsigned rec_len = 0;
1747
1748 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001749 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001750 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001751 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001753 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001754 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 de->inode = 0;
1756 map++;
1757 to += rec_len;
1758 }
Mingming Cao617ba132006-10-11 01:20:53 -07001759 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001760}
1761
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001762/*
1763 * Compact each dir entry in the range to the minimal rec_len.
1764 * Returns pointer to last entry in range.
1765 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001766static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767{
Mingming Cao617ba132006-10-11 01:20:53 -07001768 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 unsigned rec_len = 0;
1770
1771 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001772 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001773 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001775 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001776 if (de > to)
1777 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001778 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001779 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001780 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781 }
1782 de = next;
1783 }
1784 return prev;
1785}
1786
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001787/*
1788 * Split a full leaf block to make room for a new dir entry.
1789 * Allocate a new block, and move entries so that they are approx. equally full.
1790 * Returns pointer to de in block into which the new entry will be inserted.
1791 */
Mingming Cao617ba132006-10-11 01:20:53 -07001792static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001794 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795{
1796 unsigned blocksize = dir->i_sb->s_blocksize;
1797 unsigned count, continued;
1798 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001799 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800 u32 hash2;
1801 struct dx_map_entry *map;
1802 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001803 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001804 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001805 struct ext4_dir_entry_tail *t;
1806 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001807 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001808
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001809 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001810 csum_size = sizeof(struct ext4_dir_entry_tail);
1811
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001812 bh2 = ext4_append(handle, dir, &newblock);
1813 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 brelse(*bh);
1815 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001816 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 }
1818
1819 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001820 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001821 if (err)
1822 goto journal_error;
1823
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001824 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001825 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 if (err)
1827 goto journal_error;
1828
1829 data2 = bh2->b_data;
1830
1831 /* create map in the end of data2 block */
1832 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001833 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 blocksize, hinfo, map);
1835 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001836 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001837 /* Split the existing block in the middle, size-wise */
1838 size = 0;
1839 move = 0;
1840 for (i = count-1; i >= 0; i--) {
1841 /* is more than half of this entry in 2nd half of the block? */
1842 if (size + map[i].size/2 > blocksize/2)
1843 break;
1844 size += map[i].size;
1845 move++;
1846 }
1847 /* map index at which we will split */
1848 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849 hash2 = map[split].hash;
1850 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001851 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1852 (unsigned long)dx_get_block(frame->at),
1853 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854
1855 /* Fancy dance to stay within two buffers */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001856 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1857 blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001858 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001859 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1860 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001861 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001862 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1863 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001864 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001865 if (csum_size) {
1866 t = EXT4_DIRENT_TAIL(data2, blocksize);
1867 initialize_dirent_tail(t, blocksize);
1868
1869 t = EXT4_DIRENT_TAIL(data1, blocksize);
1870 initialize_dirent_tail(t, blocksize);
1871 }
1872
Michael Halcrowb3098482015-04-12 01:07:01 -04001873 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1874 blocksize, 1));
1875 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1876 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877
1878 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001879 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001880 swap(*bh, bh2);
1881 de = de2;
1882 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001883 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001884 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001885 if (err)
1886 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001887 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 if (err)
1889 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001890 brelse(bh2);
1891 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001892 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001893
1894journal_error:
1895 brelse(*bh);
1896 brelse(bh2);
1897 *bh = NULL;
1898 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001899 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001901
Tao Ma978fef92012-12-10 14:05:58 -05001902int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1903 struct buffer_head *bh,
1904 void *buf, int buf_size,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001905 struct ext4_filename *fname,
Tao Ma978fef92012-12-10 14:05:58 -05001906 struct ext4_dir_entry_2 **dest_de)
1907{
1908 struct ext4_dir_entry_2 *de;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001909 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001910 int nlen, rlen;
1911 unsigned int offset = 0;
1912 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001913
Tao Ma978fef92012-12-10 14:05:58 -05001914 de = (struct ext4_dir_entry_2 *)buf;
1915 top = buf + buf_size - reclen;
1916 while ((char *) de <= top) {
1917 if (ext4_check_dir_entry(dir, NULL, de, bh,
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001918 buf, buf_size, offset))
1919 return -EFSCORRUPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04001920 if (ext4_match(dir, fname, de))
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001921 return -EEXIST;
Tao Ma978fef92012-12-10 14:05:58 -05001922 nlen = EXT4_DIR_REC_LEN(de->name_len);
1923 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1924 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1925 break;
1926 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1927 offset += rlen;
1928 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001929 if ((char *) de > top)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001930 return -ENOSPC;
1931
1932 *dest_de = de;
1933 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001934}
1935
Eric Biggers1bc0af62017-04-29 23:27:26 -04001936void ext4_insert_dentry(struct inode *inode,
1937 struct ext4_dir_entry_2 *de,
1938 int buf_size,
1939 struct ext4_filename *fname)
Tao Ma978fef92012-12-10 14:05:58 -05001940{
1941
1942 int nlen, rlen;
1943
1944 nlen = EXT4_DIR_REC_LEN(de->name_len);
1945 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1946 if (de->inode) {
1947 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001948 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001949 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1950 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1951 de = de1;
1952 }
1953 de->file_type = EXT4_FT_UNKNOWN;
1954 de->inode = cpu_to_le32(inode->i_ino);
1955 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001956 de->name_len = fname_len(fname);
1957 memcpy(de->name, fname_name(fname), fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001958}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001959
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001960/*
1961 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1962 * it points to a directory entry which is guaranteed to be large
1963 * enough for new directory entry. If de is NULL, then
1964 * add_dirent_to_buf will attempt search the directory block for
1965 * space. It will return -ENOSPC if no space is available, and -EIO
1966 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001967 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001968static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
1969 struct inode *dir,
Mingming Cao617ba132006-10-11 01:20:53 -07001970 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001971 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972{
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001973 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001974 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001975 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001976
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001977 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001978 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001980 if (!de) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001981 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
1982 blocksize - csum_size, fname, &de);
Tao Ma978fef92012-12-10 14:05:58 -05001983 if (err)
1984 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001985 }
1986 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001987 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001989 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990 return err;
1991 }
1992
Eric Biggers1bc0af62017-04-29 23:27:26 -04001993 /* By now the buffer is marked for journaling */
1994 ext4_insert_dentry(inode, de, blocksize, fname);
Tao Ma978fef92012-12-10 14:05:58 -05001995
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001996 /*
1997 * XXX shouldn't update any times until successful
1998 * completion of syscall, but too many callers depend
1999 * on this.
2000 *
2001 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07002002 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 * recovery deletes the inode, so the worst that can
2004 * happen is that the times are slightly out of date
2005 * and/or different from the directory change time.
2006 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002007 dir->i_mtime = dir->i_ctime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002008 ext4_update_dx_flag(dir);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002009 inode_inc_iversion(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002010 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05002011 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002012 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07002014 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002015 return 0;
2016}
2017
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002018/*
2019 * This converts a one block unindexed directory to a 3 block indexed
2020 * directory, and adds the dentry to the indexed directory.
2021 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002022static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002023 struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002024 struct inode *inode, struct buffer_head *bh)
2025{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026 struct buffer_head *bh2;
2027 struct dx_root *root;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002028 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07002030 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002031 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 char *data1, *top;
2033 unsigned len;
2034 int retval;
2035 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002036 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04002038 int csum_size = 0;
2039
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002040 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002041 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042
2043 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002044 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04002045 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002046 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07002048 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002049 brelse(bh);
2050 return retval;
2051 }
2052 root = (struct dx_root *) bh->b_data;
2053
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002054 /* The 0th block becomes the root, move the dirents out */
2055 fde = &root->dotdot;
2056 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002057 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002058 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002059 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002060 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002061 return -EFSCORRUPTED;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002062 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002063 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05002064
2065 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002066 bh2 = ext4_append(handle, dir, &block);
2067 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002068 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002069 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002071 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002072 data1 = bh2->b_data;
2073
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002074 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07002075 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002077 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002078 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002079 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
2080 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002081 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002082
2083 if (csum_size) {
2084 t = EXT4_DIRENT_TAIL(data1, blocksize);
2085 initialize_dirent_tail(t, blocksize);
2086 }
2087
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002089 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002090 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2091 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092 memset (&root->info, 0, sizeof(root->info));
2093 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07002094 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002096 dx_set_block(entries, 1);
2097 dx_set_count(entries, 1);
2098 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099
2100 /* Initialize as for dx_probe */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002101 fname->hinfo.hash_version = root->info.hash_version;
2102 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2103 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2104 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002105 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), &fname->hinfo);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002106
Jan Kara6050d472014-10-30 10:53:17 -04002107 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 frame = frames;
2109 frame->entries = entries;
2110 frame->at = entries;
2111 frame->bh = bh;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002112
Jan Kara6050d472014-10-30 10:53:17 -04002113 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2114 if (retval)
2115 goto out_frames;
gmaile81d4472016-09-30 01:33:37 -04002116 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002117 if (retval)
2118 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002119
gmaile81d4472016-09-30 01:33:37 -04002120 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002121 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002122 retval = PTR_ERR(de);
2123 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002124 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125
gmaile81d4472016-09-30 01:33:37 -04002126 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002127out_frames:
2128 /*
2129 * Even if the block split failed, we have to properly write
2130 * out all the changes we did so far. Otherwise we can end up
2131 * with corrupted filesystem.
2132 */
gmaile81d4472016-09-30 01:33:37 -04002133 if (retval)
2134 ext4_mark_inode_dirty(handle, dir);
Jan Kara6050d472014-10-30 10:53:17 -04002135 dx_release(frames);
gmaile81d4472016-09-30 01:33:37 -04002136 brelse(bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002137 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139
2140/*
Mingming Cao617ba132006-10-11 01:20:53 -07002141 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002142 *
2143 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002144 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002145 *
2146 * NOTE!! The inode part of 'de' is left at 0 - which means you
2147 * may not sleep between calling this and putting something into
2148 * the entry, as someone else might have used it while you slept.
2149 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002150static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2151 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002152{
David Howells2b0143b2015-03-17 22:25:59 +00002153 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002154 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002155 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002156 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002157 struct super_block *sb;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002158 struct ext4_sb_info *sbi;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002159 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002160 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002163 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002164 int csum_size = 0;
2165
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002166 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002167 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002168
2169 sb = dir->i_sb;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002170 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002171 blocksize = sb->s_blocksize;
2172 if (!dentry->d_name.len)
2173 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002174
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04002175#ifdef CONFIG_UNICODE
2176 if (ext4_has_strict_mode(sbi) && IS_CASEFOLDED(dir) &&
2177 utf8_validate(sbi->s_encoding, &dentry->d_name))
2178 return -EINVAL;
2179#endif
2180
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002181 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2182 if (retval)
2183 return retval;
2184
Tao Ma3c47d542012-12-10 14:05:59 -05002185 if (ext4_has_inline_data(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002186 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05002187 if (retval < 0)
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002188 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002189 if (retval == 1) {
2190 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002191 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002192 }
2193 }
2194
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195 if (is_dx(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002196 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002198 goto out;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002199 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002201 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002202 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002203 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002204 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002205 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002206 if (IS_ERR(bh)) {
2207 retval = PTR_ERR(bh);
2208 bh = NULL;
2209 goto out;
2210 }
2211 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2212 NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002213 if (retval != -ENOSPC)
2214 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002216 if (blocks == 1 && !dx_fallback &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002217 ext4_has_feature_dir_index(sb)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002218 retval = make_indexed_dir(handle, &fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002219 inode, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002220 bh = NULL; /* make_indexed_dir releases bh */
2221 goto out;
2222 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002223 brelse(bh);
2224 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002225 bh = ext4_append(handle, dir, &block);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002226 if (IS_ERR(bh)) {
2227 retval = PTR_ERR(bh);
2228 bh = NULL;
2229 goto out;
2230 }
Mingming Cao617ba132006-10-11 01:20:53 -07002231 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002233 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2234
2235 if (csum_size) {
2236 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2237 initialize_dirent_tail(t, blocksize);
2238 }
2239
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002240 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002241out:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002242 ext4_fname_free_filename(&fname);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002243 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002244 if (retval == 0)
2245 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002246 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002247}
2248
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249/*
2250 * Returns 0 for success, or a negative error value
2251 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002252static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002253 struct inode *dir, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002255 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256 struct dx_entry *entries, *at;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002257 struct buffer_head *bh;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002258 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002259 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002260 int restart;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261 int err;
2262
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002263again:
2264 restart = 0;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002265 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002266 if (IS_ERR(frame))
2267 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268 entries = frame->entries;
2269 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002270 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2271 if (IS_ERR(bh)) {
2272 err = PTR_ERR(bh);
2273 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002275 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276
2277 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002278 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279 if (err)
2280 goto journal_error;
2281
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002282 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002283 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002286 err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002287 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002288 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289 dx_get_count(entries), dx_get_limit(entries)));
2290 /* Need to split index? */
2291 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002292 ext4_lblk_t newblock;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002293 int levels = frame - frames + 1;
2294 unsigned int icount;
2295 int add_level = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002296 struct dx_entry *entries2;
2297 struct dx_node *node2;
2298 struct buffer_head *bh2;
2299
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002300 while (frame > frames) {
2301 if (dx_get_count((frame - 1)->entries) <
2302 dx_get_limit((frame - 1)->entries)) {
2303 add_level = 0;
2304 break;
2305 }
2306 frame--; /* split higher index block */
2307 at = frame->at;
2308 entries = frame->entries;
2309 restart = 1;
2310 }
2311 if (add_level && levels == ext4_dir_htree_level(sb)) {
2312 ext4_warning(sb, "Directory (ino: %lu) index full, "
2313 "reach max htree level :%d",
2314 dir->i_ino, levels);
2315 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2316 ext4_warning(sb, "Large directory feature is "
2317 "not enabled on this "
2318 "filesystem");
2319 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320 err = -ENOSPC;
2321 goto cleanup;
2322 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002323 icount = dx_get_count(entries);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002324 bh2 = ext4_append(handle, dir, &newblock);
2325 if (IS_ERR(bh2)) {
2326 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002327 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002328 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002329 node2 = (struct dx_node *)(bh2->b_data);
2330 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002331 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002332 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2333 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002334 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002335 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002336 if (err)
2337 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002338 if (!add_level) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 unsigned icount1 = icount/2, icount2 = icount - icount1;
2340 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002341 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2342 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343
2344 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002345 err = ext4_journal_get_write_access(handle,
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002346 (frame - 1)->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002347 if (err)
2348 goto journal_error;
2349
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002350 memcpy((char *) entries2, (char *) (entries + icount1),
2351 icount2 * sizeof(struct dx_entry));
2352 dx_set_count(entries, icount1);
2353 dx_set_count(entries2, icount2);
2354 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002355
2356 /* Which index block gets the new entry? */
2357 if (at - entries >= icount1) {
2358 frame->at = at = at - entries - icount1 + entries2;
2359 frame->entries = entries = entries2;
2360 swap(frame->bh, bh2);
2361 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002362 dx_insert_block((frame - 1), hash2, newblock);
2363 dxtrace(dx_show_index("node", frame->entries));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002364 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002365 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002366 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002367 if (err)
2368 goto journal_error;
2369 brelse (bh2);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002370 err = ext4_handle_dirty_dx_node(handle, dir,
2371 (frame - 1)->bh);
2372 if (err)
2373 goto journal_error;
2374 if (restart) {
2375 err = ext4_handle_dirty_dx_node(handle, dir,
2376 frame->bh);
2377 goto journal_error;
2378 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002379 } else {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002380 struct dx_root *dxroot;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002381 memcpy((char *) entries2, (char *) entries,
2382 icount * sizeof(struct dx_entry));
2383 dx_set_limit(entries2, dx_node_limit(dir));
2384
2385 /* Set up root */
2386 dx_set_count(entries, 1);
2387 dx_set_block(entries + 0, newblock);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002388 dxroot = (struct dx_root *)frames[0].bh->b_data;
2389 dxroot->info.indirect_levels += 1;
2390 dxtrace(printk(KERN_DEBUG
2391 "Creating %d level index...\n",
Gabriel Krisman Bertazi799578a2018-10-02 12:43:51 -04002392 dxroot->info.indirect_levels));
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002393 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394 if (err)
2395 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002396 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2397 brelse(bh2);
2398 restart = 1;
2399 goto journal_error;
Theodore Ts'ob4097142011-01-10 12:46:59 -05002400 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002401 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002402 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002403 if (IS_ERR(de)) {
2404 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002405 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002406 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002407 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002408 goto cleanup;
2409
2410journal_error:
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002411 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002413 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 dx_release(frames);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002415 /* @restart is true means htree-path has been changed, we need to
2416 * repeat dx_probe() to find out valid htree-path
2417 */
2418 if (restart && err == 0)
2419 goto again;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 return err;
2421}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422
2423/*
Tao Ma05019a92012-12-10 14:06:00 -05002424 * ext4_generic_delete_entry deletes a directory entry by merging it
2425 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 */
Tao Ma05019a92012-12-10 14:06:00 -05002427int ext4_generic_delete_entry(handle_t *handle,
2428 struct inode *dir,
2429 struct ext4_dir_entry_2 *de_del,
2430 struct buffer_head *bh,
2431 void *entry_buf,
2432 int buf_size,
2433 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002434{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002435 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002436 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002437 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002438
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 i = 0;
2440 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002441 de = (struct ext4_dir_entry_2 *)entry_buf;
2442 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002443 if (ext4_check_dir_entry(dir, NULL, de, bh,
2444 bh->b_data, bh->b_size, i))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002445 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002447 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002448 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002449 ext4_rec_len_from_disk(pde->rec_len,
2450 blocksize) +
2451 ext4_rec_len_from_disk(de->rec_len,
2452 blocksize),
2453 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454 else
2455 de->inode = 0;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002456 inode_inc_iversion(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457 return 0;
2458 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002459 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002460 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002461 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 }
2463 return -ENOENT;
2464}
2465
Tao Ma05019a92012-12-10 14:06:00 -05002466static int ext4_delete_entry(handle_t *handle,
2467 struct inode *dir,
2468 struct ext4_dir_entry_2 *de_del,
2469 struct buffer_head *bh)
2470{
2471 int err, csum_size = 0;
2472
Tao Ma9f40fe52012-12-10 14:06:00 -05002473 if (ext4_has_inline_data(dir)) {
2474 int has_inline_data = 1;
2475 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2476 &has_inline_data);
2477 if (has_inline_data)
2478 return err;
2479 }
2480
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002481 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002482 csum_size = sizeof(struct ext4_dir_entry_tail);
2483
2484 BUFFER_TRACE(bh, "get_write_access");
2485 err = ext4_journal_get_write_access(handle, bh);
2486 if (unlikely(err))
2487 goto out;
2488
2489 err = ext4_generic_delete_entry(handle, dir, de_del,
2490 bh, bh->b_data,
2491 dir->i_sb->s_blocksize, csum_size);
2492 if (err)
2493 goto out;
2494
2495 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2496 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2497 if (unlikely(err))
2498 goto out;
2499
2500 return 0;
2501out:
2502 if (err != -ENOENT)
2503 ext4_std_error(dir->i_sb, err);
2504 return err;
2505}
2506
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002507/*
Andreas Dilgerc7414892017-08-05 19:47:34 -04002508 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2509 * since this indicates that nlinks count was previously 1 to avoid overflowing
2510 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2511 * that subdirectory link counts are not being maintained accurately.
2512 *
2513 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2514 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2515 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2516 * on regular files) and to avoid creating huge/slow non-HTREE directories.
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002517 */
2518static void ext4_inc_count(handle_t *handle, struct inode *inode)
2519{
2520 inc_nlink(inode);
Andreas Dilgerc7414892017-08-05 19:47:34 -04002521 if (is_dx(inode) &&
2522 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2523 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002524}
2525
2526/*
2527 * If a directory had nlink == 1, then we should let it be 1. This indicates
2528 * directory has >EXT4_LINK_MAX subdirs.
2529 */
2530static void ext4_dec_count(handle_t *handle, struct inode *inode)
2531{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002532 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2533 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002534}
2535
2536
Mingming Cao617ba132006-10-11 01:20:53 -07002537static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002538 struct dentry *dentry, struct inode *inode)
2539{
Mingming Cao617ba132006-10-11 01:20:53 -07002540 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002541 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002542 ext4_mark_inode_dirty(handle, inode);
Al Viro1e2e5472018-05-04 08:23:01 -04002543 d_instantiate_new(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002544 return 0;
2545 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002546 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002547 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002548 iput(inode);
2549 return err;
2550}
2551
2552/*
2553 * By the time this is called, we already have created
2554 * the directory cache entry for the new file, but it
2555 * is so far negative - it has no inode.
2556 *
2557 * If the create succeeds, we fill in the inode information
2558 * with d_instantiate().
2559 */
Al Viro4acdaf22011-07-26 01:42:34 -04002560static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002561 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002562{
2563 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002564 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002565 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002566
Jan Karaa7cdade2015-06-29 16:22:54 +02002567 err = dquot_initialize(dir);
2568 if (err)
2569 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002570
Theodore Ts'o11395752013-02-09 16:27:09 -05002571 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002572 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002573retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002574 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2575 NULL, EXT4_HT_DIR, credits);
2576 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002577 err = PTR_ERR(inode);
2578 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002579 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002580 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002581 ext4_set_aops(inode);
Theodore Ts'oe709e9d2015-05-31 13:35:02 -04002582 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002583 if (!err && IS_DIRSYNC(dir))
2584 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002585 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002586 if (handle)
2587 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002588 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002589 goto retry;
2590 return err;
2591}
2592
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002593static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002594 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002595{
2596 handle_t *handle;
2597 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002598 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002599
Jan Karaa7cdade2015-06-29 16:22:54 +02002600 err = dquot_initialize(dir);
2601 if (err)
2602 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002603
Theodore Ts'o11395752013-02-09 16:27:09 -05002604 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002605 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002606retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002607 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2608 NULL, EXT4_HT_DIR, credits);
2609 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610 err = PTR_ERR(inode);
2611 if (!IS_ERR(inode)) {
2612 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002613 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002614 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002615 if (!err && IS_DIRSYNC(dir))
2616 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002618 if (handle)
2619 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002620 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002621 goto retry;
2622 return err;
2623}
2624
Al Viroaf51a2a2013-06-29 13:23:08 +04002625static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2626{
2627 handle_t *handle;
2628 struct inode *inode;
2629 int err, retries = 0;
2630
Jan Karaa7cdade2015-06-29 16:22:54 +02002631 err = dquot_initialize(dir);
2632 if (err)
2633 return err;
Al Viroaf51a2a2013-06-29 13:23:08 +04002634
2635retry:
2636 inode = ext4_new_inode_start_handle(dir, mode,
2637 NULL, 0, NULL,
2638 EXT4_HT_DIR,
2639 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2640 4 + EXT4_XATTR_TRANS_BLOCKS);
2641 handle = ext4_journal_current_handle();
2642 err = PTR_ERR(inode);
2643 if (!IS_ERR(inode)) {
2644 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002645 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002646 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002647 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002648 err = ext4_orphan_add(handle, inode);
2649 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002650 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002651 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002652 unlock_new_inode(inode);
2653 }
2654 if (handle)
2655 ext4_journal_stop(handle);
2656 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2657 goto retry;
2658 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002659err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002660 ext4_journal_stop(handle);
2661 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002662 return err;
2663}
2664
Tao Maa774f9c2012-12-10 14:05:57 -05002665struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2666 struct ext4_dir_entry_2 *de,
2667 int blocksize, int csum_size,
2668 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002669{
Tao Maa774f9c2012-12-10 14:05:57 -05002670 de->inode = cpu_to_le32(inode->i_ino);
2671 de->name_len = 1;
2672 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2673 blocksize);
2674 strcpy(de->name, ".");
2675 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2676
2677 de = ext4_next_entry(de, blocksize);
2678 de->inode = cpu_to_le32(parent_ino);
2679 de->name_len = 2;
2680 if (!dotdot_real_len)
2681 de->rec_len = ext4_rec_len_to_disk(blocksize -
2682 (csum_size + EXT4_DIR_REC_LEN(1)),
2683 blocksize);
2684 else
2685 de->rec_len = ext4_rec_len_to_disk(
2686 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2687 strcpy(de->name, "..");
2688 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2689
2690 return ext4_next_entry(de, blocksize);
2691}
2692
2693static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2694 struct inode *inode)
2695{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002696 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002697 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002698 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002699 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002700 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002701 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002702 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002703
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002704 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002705 csum_size = sizeof(struct ext4_dir_entry_tail);
2706
Tao Ma3c47d542012-12-10 14:05:59 -05002707 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2708 err = ext4_try_create_inline_dir(handle, dir, inode);
2709 if (err < 0 && err != -ENOSPC)
2710 goto out;
2711 if (!err)
2712 goto out;
2713 }
2714
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002715 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002716 dir_block = ext4_append(handle, inode, &block);
2717 if (IS_ERR(dir_block))
2718 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002719 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2720 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2721 set_nlink(inode, 2);
2722 if (csum_size) {
2723 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2724 initialize_dirent_tail(t, blocksize);
2725 }
2726
2727 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2728 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2729 if (err)
2730 goto out;
2731 set_buffer_verified(dir_block);
2732out:
2733 brelse(dir_block);
2734 return err;
2735}
2736
2737static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2738{
2739 handle_t *handle;
2740 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002741 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002742
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002743 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744 return -EMLINK;
2745
Jan Karaa7cdade2015-06-29 16:22:54 +02002746 err = dquot_initialize(dir);
2747 if (err)
2748 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002749
Theodore Ts'o11395752013-02-09 16:27:09 -05002750 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002751 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002753 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2754 &dentry->d_name,
2755 0, NULL, EXT4_HT_DIR, credits);
2756 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 err = PTR_ERR(inode);
2758 if (IS_ERR(inode))
2759 goto out_stop;
2760
Mingming Cao617ba132006-10-11 01:20:53 -07002761 inode->i_op = &ext4_dir_inode_operations;
2762 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002763 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002764 if (err)
2765 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002766 err = ext4_mark_inode_dirty(handle, inode);
2767 if (!err)
2768 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002769 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002770out_clear_inode:
2771 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002772 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002773 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002774 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775 goto out_stop;
2776 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002777 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002778 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002779 err = ext4_mark_inode_dirty(handle, dir);
2780 if (err)
2781 goto out_clear_inode;
Al Viro1e2e5472018-05-04 08:23:01 -04002782 d_instantiate_new(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002783 if (IS_DIRSYNC(dir))
2784 ext4_handle_sync(handle);
2785
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002787 if (handle)
2788 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002789 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002790 goto retry;
2791 return err;
2792}
2793
2794/*
2795 * routine to check that the specified directory is empty (for rmdir)
2796 */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002797bool ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002798{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002799 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002800 struct buffer_head *bh;
2801 struct ext4_dir_entry_2 *de, *de1;
2802 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002803
Tao Ma61f86632012-12-10 14:06:01 -05002804 if (ext4_has_inline_data(inode)) {
2805 int has_inline_data = 1;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002806 int ret;
Tao Ma61f86632012-12-10 14:06:01 -05002807
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002808 ret = empty_inline_dir(inode, &has_inline_data);
Tao Ma61f86632012-12-10 14:06:01 -05002809 if (has_inline_data)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002810 return ret;
Tao Ma61f86632012-12-10 14:06:01 -05002811 }
2812
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002813 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002814 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2815 EXT4_ERROR_INODE(inode, "invalid size");
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002816 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002818 bh = ext4_read_dirblock(inode, 0, EITHER);
2819 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002820 return true;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002821
Mingming Cao617ba132006-10-11 01:20:53 -07002822 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002823 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824 if (le32_to_cpu(de->inode) != inode->i_ino ||
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04002825 le32_to_cpu(de1->inode) == 0 ||
2826 strcmp(".", de->name) || strcmp("..", de1->name)) {
2827 ext4_warning_inode(inode, "directory missing '.' and/or '..'");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002828 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002829 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002831 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2832 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2833 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002834 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002835 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002836 unsigned int lblock;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002837 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002838 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002839 bh = ext4_read_dirblock(inode, lblock, EITHER);
2840 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002841 return true;
Mingming Cao617ba132006-10-11 01:20:53 -07002842 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002843 }
Tao Ma226ba972012-12-10 14:05:58 -05002844 if (ext4_check_dir_entry(inode, NULL, de, bh,
2845 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002846 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 sb->s_blocksize);
2848 offset = (offset | (sb->s_blocksize - 1)) + 1;
2849 continue;
2850 }
2851 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002852 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002853 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002855 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2856 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002858 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002859 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860}
2861
Jan Karad745a8c2014-05-26 11:56:53 -04002862/*
2863 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864 * such inodes, starting at the superblock, in case we crash before the
2865 * file is closed/deleted, or in case the inode truncate spans multiple
2866 * transactions and the last transaction is not recovered after a crash.
2867 *
2868 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002869 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002870 *
2871 * Orphan list manipulation functions must be called under i_mutex unless
2872 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 */
Mingming Cao617ba132006-10-11 01:20:53 -07002874int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002875{
2876 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002877 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002878 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002879 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002880 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002882 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002883 return 0;
2884
Jan Karad745a8c2014-05-26 11:56:53 -04002885 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002886 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002887 /*
2888 * Exit early if inode already is on orphan list. This is a big speedup
2889 * since we don't have to contend on the global s_orphan_lock.
2890 */
Mingming Cao617ba132006-10-11 01:20:53 -07002891 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002892 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893
Lukas Czernerafb86172011-07-11 18:47:04 -04002894 /*
2895 * Orphan handling is only valid for files with data blocks
2896 * being truncated, or files being unlinked. Note that we either
2897 * hold i_mutex, or the inode can not be referenced from outside,
2898 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002899 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002900 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2901 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002902
Jan Karacd2c0802014-05-26 11:39:17 -04002903 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2904 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002906 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907
Mingming Cao617ba132006-10-11 01:20:53 -07002908 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002910 goto out;
2911
2912 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002913 /*
2914 * Due to previous errors inode may be already a part of on-disk
2915 * orphan list. If so skip on-disk list modification.
2916 */
Jan Karad745a8c2014-05-26 11:56:53 -04002917 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2918 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2919 /* Insert this inode at the head of the on-disk orphan list */
2920 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2921 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2922 dirty = true;
2923 }
2924 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2925 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926
Jan Karad745a8c2014-05-26 11:56:53 -04002927 if (dirty) {
2928 err = ext4_handle_dirty_super(handle, sb);
2929 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2930 if (!err)
2931 err = rc;
2932 if (err) {
2933 /*
2934 * We have to remove inode from in-memory list if
2935 * addition to on disk orphan list failed. Stray orphan
2936 * list entries can cause panics at unmount time.
2937 */
2938 mutex_lock(&sbi->s_orphan_lock);
Jan Kara74177f552016-05-05 11:10:15 -04002939 list_del_init(&EXT4_I(inode)->i_orphan);
Jan Karad745a8c2014-05-26 11:56:53 -04002940 mutex_unlock(&sbi->s_orphan_lock);
2941 }
Vasily Averinfeaf2642018-11-06 17:01:36 -05002942 } else
2943 brelse(iloc.bh);
2944
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2946 jbd_debug(4, "orphan inode %lu will point to %d\n",
2947 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002948out:
Jan Karacd2c0802014-05-26 11:39:17 -04002949 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002950 return err;
2951}
2952
2953/*
Mingming Cao617ba132006-10-11 01:20:53 -07002954 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955 * of such inodes stored on disk, because it is finally being cleaned up.
2956 */
Mingming Cao617ba132006-10-11 01:20:53 -07002957int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958{
2959 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002960 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002961 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002962 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002963 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964 int err = 0;
2965
Jan Karacd2c0802014-05-26 11:39:17 -04002966 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002967 return 0;
2968
Jan Karad745a8c2014-05-26 11:56:53 -04002969 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002970 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002971 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002972 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002973 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974
Jan Karad745a8c2014-05-26 11:56:53 -04002975 if (handle) {
2976 /* Grab inode buffer early before taking global s_orphan_lock */
2977 err = ext4_reserve_inode_write(handle, inode, &iloc);
2978 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979
Jan Karad745a8c2014-05-26 11:56:53 -04002980 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2982
Jan Karad745a8c2014-05-26 11:56:53 -04002983 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984 list_del_init(&ei->i_orphan);
2985
2986 /* If we're on an error path, we may not have a valid
2987 * transaction handle with which to update the orphan list on
2988 * disk, but we still need to remove the inode from the linked
2989 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002990 if (!handle || err) {
2991 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002992 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002993 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994
Jan Karad745a8c2014-05-26 11:56:53 -04002995 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002997 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002999 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04003000 if (err) {
3001 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003003 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003004 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04003005 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04003006 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003008 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07003010 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003011
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003012 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003013 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07003014 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003015 if (err) {
3016 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04003018 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003019 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07003020 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04003021 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022 }
3023 if (err)
3024 goto out_brelse;
3025 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003026 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07003028 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029 return err;
3030
3031out_brelse:
3032 brelse(iloc.bh);
3033 goto out_err;
3034}
3035
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003036static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037{
3038 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003039 struct inode *inode;
3040 struct buffer_head *bh;
3041 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003042 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003044 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3045 return -EIO;
3046
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047 /* Initialize quotas before so that eventual writes go in
3048 * separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003049 retval = dquot_initialize(dir);
3050 if (retval)
3051 return retval;
3052 retval = dquot_initialize(d_inode(dentry));
3053 if (retval)
3054 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003055
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003057 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003058 if (IS_ERR(bh))
3059 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003060 if (!bh)
3061 goto end_rmdir;
3062
David Howells2b0143b2015-03-17 22:25:59 +00003063 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003064
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003065 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066 if (le32_to_cpu(de->inode) != inode->i_ino)
3067 goto end_rmdir;
3068
3069 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003070 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 goto end_rmdir;
3072
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003073 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003074 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003075 if (IS_ERR(handle)) {
3076 retval = PTR_ERR(handle);
3077 handle = NULL;
3078 goto end_rmdir;
3079 }
3080
3081 if (IS_DIRSYNC(dir))
3082 ext4_handle_sync(handle);
3083
Mingming Cao617ba132006-10-11 01:20:53 -07003084 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 if (retval)
3086 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003087 if (!EXT4_DIR_LINK_EMPTY(inode))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003088 ext4_warning_inode(inode,
3089 "empty directory '%.*s' has too many links (%u)",
3090 dentry->d_name.len, dentry->d_name.name,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003091 inode->i_nlink);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003092 inode_inc_iversion(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 clear_nlink(inode);
3094 /* There's no need to set i_disksize: the fact that i_nlink is
3095 * zero will ensure that the right thing happens during any
3096 * recovery. */
3097 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003098 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003099 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003100 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003101 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003102 ext4_update_dx_flag(dir);
3103 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003104
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003105#ifdef CONFIG_UNICODE
3106 /* VFS negative dentries are incompatible with Encoding and
3107 * Case-insensitiveness. Eventually we'll want avoid
3108 * invalidating the dentries here, alongside with returning the
3109 * negative dentries at ext4_lookup(), when it is better
3110 * supported by the VFS for the CI case.
3111 */
3112 if (IS_CASEFOLDED(dir))
3113 d_invalidate(dentry);
3114#endif
3115
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003117 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003118 if (handle)
3119 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 return retval;
3121}
3122
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003123static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124{
3125 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003126 struct inode *inode;
3127 struct buffer_head *bh;
3128 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003129 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003131 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3132 return -EIO;
3133
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003134 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135 /* Initialize quotas before so that eventual writes go
3136 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003137 retval = dquot_initialize(dir);
3138 if (retval)
3139 return retval;
3140 retval = dquot_initialize(d_inode(dentry));
3141 if (retval)
3142 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003143
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003145 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003146 if (IS_ERR(bh))
3147 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003148 if (!bh)
3149 goto end_unlink;
3150
David Howells2b0143b2015-03-17 22:25:59 +00003151 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003153 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003154 if (le32_to_cpu(de->inode) != inode->i_ino)
3155 goto end_unlink;
3156
Theodore Ts'o931b6862013-02-09 09:43:39 -05003157 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003158 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05003159 if (IS_ERR(handle)) {
3160 retval = PTR_ERR(handle);
3161 handle = NULL;
3162 goto end_unlink;
3163 }
3164
3165 if (IS_DIRSYNC(dir))
3166 ext4_handle_sync(handle);
3167
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003168 if (inode->i_nlink == 0) {
3169 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3170 dentry->d_name.len, dentry->d_name.name);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003171 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003172 }
Mingming Cao617ba132006-10-11 01:20:53 -07003173 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174 if (retval)
3175 goto end_unlink;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003176 dir->i_ctime = dir->i_mtime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003177 ext4_update_dx_flag(dir);
3178 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003179 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003180 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003181 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003182 inode->i_ctime = current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003183 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04003185#ifdef CONFIG_UNICODE
3186 /* VFS negative dentries are incompatible with Encoding and
3187 * Case-insensitiveness. Eventually we'll want avoid
3188 * invalidating the dentries here, alongside with returning the
3189 * negative dentries at ext4_lookup(), when it is better
3190 * supported by the VFS for the CI case.
3191 */
3192 if (IS_CASEFOLDED(dir))
3193 d_invalidate(dentry);
3194#endif
3195
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003196end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003197 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003198 if (handle)
3199 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003200 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 return retval;
3202}
3203
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003204static int ext4_symlink(struct inode *dir,
3205 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003206{
3207 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003208 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003209 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003210 int credits;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003211 struct fscrypt_str disk_link;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003213 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3214 return -EIO;
3215
Eric Biggers78e10602018-01-11 22:10:40 -05003216 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3217 &disk_link);
3218 if (err)
3219 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220
Jan Karaa7cdade2015-06-29 16:22:54 +02003221 err = dquot_initialize(dir);
3222 if (err)
Eric Biggers78e10602018-01-11 22:10:40 -05003223 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003224
Theodore Ts'of348c252015-04-16 01:55:00 -04003225 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003226 /*
3227 * For non-fast symlinks, we just allocate inode and put it on
3228 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003229 * group descriptor, sb, inode block, quota blocks, and
3230 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003231 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003232 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3233 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003234 } else {
3235 /*
3236 * Fast symlink. We have to add entry to directory
3237 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3238 * allocate new inode (bitmap, group descriptor, inode block,
3239 * quota blocks, sb is already counted in previous macros).
3240 */
3241 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003242 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003243 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003244
Theodore Ts'o11395752013-02-09 16:27:09 -05003245 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3246 &dentry->d_name, 0, NULL,
3247 EXT4_HT_DIR, credits);
3248 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003249 if (IS_ERR(inode)) {
3250 if (handle)
3251 ext4_journal_stop(handle);
Eric Biggers78e10602018-01-11 22:10:40 -05003252 return PTR_ERR(inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003253 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003254
Eric Biggers78e10602018-01-11 22:10:40 -05003255 if (IS_ENCRYPTED(inode)) {
3256 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04003257 if (err)
Theodore Ts'of348c252015-04-16 01:55:00 -04003258 goto err_drop_inode;
Al Viroa7a67e82015-04-27 17:51:30 -04003259 inode->i_op = &ext4_encrypted_symlink_inode_operations;
Theodore Ts'of348c252015-04-16 01:55:00 -04003260 }
3261
3262 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Eric Biggers78e10602018-01-11 22:10:40 -05003263 if (!IS_ENCRYPTED(inode))
Al Viroa7a67e82015-04-27 17:51:30 -04003264 inode->i_op = &ext4_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003265 inode_nohighmem(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003266 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003268 * We cannot call page_symlink() with transaction started
3269 * because it calls into ext4_write_begin() which can wait
3270 * for transaction commit if we are running out of space
3271 * and thus we deadlock. So we have to stop transaction now
3272 * and restart it when symlink contents is written.
3273 *
3274 * To keep fs consistent in case of crash, we have to put inode
3275 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276 */
Jan Karadf5e6222011-05-03 11:12:58 -04003277 drop_nlink(inode);
3278 err = ext4_orphan_add(handle, inode);
3279 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003280 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003281 if (err)
3282 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003283 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003284 if (err)
3285 goto err_drop_inode;
3286 /*
3287 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3288 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3289 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003290 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003291 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3292 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3293 if (IS_ERR(handle)) {
3294 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003295 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003296 goto err_drop_inode;
3297 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003298 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003299 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003300 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003301 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003302 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003303 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003304 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Eric Biggers78e10602018-01-11 22:10:40 -05003305 if (!IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04003306 inode->i_op = &ext4_fast_symlink_inode_operations;
Al Viro75e75662015-05-02 10:13:58 -04003307 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3308 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003309 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3310 disk_link.len);
3311 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312 }
Mingming Cao617ba132006-10-11 01:20:53 -07003313 EXT4_I(inode)->i_disksize = inode->i_size;
3314 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003315 if (!err && IS_DIRSYNC(dir))
3316 ext4_handle_sync(handle);
3317
Theodore Ts'o11395752013-02-09 16:27:09 -05003318 if (handle)
3319 ext4_journal_stop(handle);
Eric Biggers78e10602018-01-11 22:10:40 -05003320 goto out_free_encrypted_link;
3321
Jan Karadf5e6222011-05-03 11:12:58 -04003322err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003323 if (handle)
3324 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003325 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003326 unlock_new_inode(inode);
3327 iput(inode);
Eric Biggers78e10602018-01-11 22:10:40 -05003328out_free_encrypted_link:
3329 if (disk_link.name != (unsigned char *)symname)
3330 kfree(disk_link.name);
Jan Karadf5e6222011-05-03 11:12:58 -04003331 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003332}
3333
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003334static int ext4_link(struct dentry *old_dentry,
3335 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336{
3337 handle_t *handle;
David Howells2b0143b2015-03-17 22:25:59 +00003338 struct inode *inode = d_inode(old_dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003339 int err, retries = 0;
3340
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003341 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342 return -EMLINK;
Eric Biggers69725182017-10-18 20:21:57 -04003343
3344 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3345 if (err)
3346 return err;
Li Xi040cb372016-01-08 16:01:21 -05003347
Colin Ian Kinga794df02018-01-11 14:17:30 -05003348 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3349 (!projid_eq(EXT4_I(dir)->i_projid,
3350 EXT4_I(old_dentry->d_inode)->i_projid)))
Li Xi040cb372016-01-08 16:01:21 -05003351 return -EXDEV;
3352
Jan Karaa7cdade2015-06-29 16:22:54 +02003353 err = dquot_initialize(dir);
3354 if (err)
3355 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003356
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003357retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003358 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3359 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003360 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361 if (IS_ERR(handle))
3362 return PTR_ERR(handle);
3363
3364 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003365 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003366
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003367 inode->i_ctime = current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003368 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003369 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370
Al Viro6b38e842008-12-30 02:03:31 -05003371 err = ext4_add_entry(handle, dentry, inode);
3372 if (!err) {
3373 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003374 /* this can happen only for tmpfile being
3375 * linked the first time
3376 */
3377 if (inode->i_nlink == 1)
3378 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003379 d_instantiate(dentry, inode);
3380 } else {
3381 drop_nlink(inode);
3382 iput(inode);
3383 }
Mingming Cao617ba132006-10-11 01:20:53 -07003384 ext4_journal_stop(handle);
3385 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003386 goto retry;
3387 return err;
3388}
3389
Tao Ma32f7f222012-12-10 14:06:01 -05003390
3391/*
3392 * Try to find buffer head where contains the parent block.
3393 * It should be the inode block if it is inlined or the 1st block
3394 * if it is a normal dir.
3395 */
3396static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3397 struct inode *inode,
3398 int *retval,
3399 struct ext4_dir_entry_2 **parent_de,
3400 int *inlined)
3401{
3402 struct buffer_head *bh;
3403
3404 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003405 bh = ext4_read_dirblock(inode, 0, EITHER);
3406 if (IS_ERR(bh)) {
3407 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003408 return NULL;
3409 }
3410 *parent_de = ext4_next_entry(
3411 (struct ext4_dir_entry_2 *)bh->b_data,
3412 inode->i_sb->s_blocksize);
3413 return bh;
3414 }
3415
3416 *inlined = 1;
3417 return ext4_get_first_inline_block(inode, parent_de, retval);
3418}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003419
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003420struct ext4_renament {
3421 struct inode *dir;
3422 struct dentry *dentry;
3423 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003424 bool is_dir;
3425 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003426
3427 /* entry for "dentry" */
3428 struct buffer_head *bh;
3429 struct ext4_dir_entry_2 *de;
3430 int inlined;
3431
3432 /* entry for ".." in inode if it's a directory */
3433 struct buffer_head *dir_bh;
3434 struct ext4_dir_entry_2 *parent_de;
3435 int dir_inlined;
3436};
3437
Miklos Szeredibd1af142014-04-01 17:08:44 +02003438static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3439{
3440 int retval;
3441
3442 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3443 &retval, &ent->parent_de,
3444 &ent->dir_inlined);
3445 if (!ent->dir_bh)
3446 return retval;
3447 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003448 return -EFSCORRUPTED;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003449 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3450 return ext4_journal_get_write_access(handle, ent->dir_bh);
3451}
3452
3453static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3454 unsigned dir_ino)
3455{
3456 int retval;
3457
3458 ent->parent_de->inode = cpu_to_le32(dir_ino);
3459 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3460 if (!ent->dir_inlined) {
3461 if (is_dx(ent->inode)) {
3462 retval = ext4_handle_dirty_dx_node(handle,
3463 ent->inode,
3464 ent->dir_bh);
3465 } else {
3466 retval = ext4_handle_dirty_dirent_node(handle,
3467 ent->inode,
3468 ent->dir_bh);
3469 }
3470 } else {
3471 retval = ext4_mark_inode_dirty(handle, ent->inode);
3472 }
3473 if (retval) {
3474 ext4_std_error(ent->dir->i_sb, retval);
3475 return retval;
3476 }
3477 return 0;
3478}
3479
3480static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3481 unsigned ino, unsigned file_type)
3482{
3483 int retval;
3484
3485 BUFFER_TRACE(ent->bh, "get write access");
3486 retval = ext4_journal_get_write_access(handle, ent->bh);
3487 if (retval)
3488 return retval;
3489 ent->de->inode = cpu_to_le32(ino);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003490 if (ext4_has_feature_filetype(ent->dir->i_sb))
Miklos Szeredibd1af142014-04-01 17:08:44 +02003491 ent->de->file_type = file_type;
Jeff Laytonee73f9a2018-01-09 08:21:39 -05003492 inode_inc_iversion(ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003493 ent->dir->i_ctime = ent->dir->i_mtime =
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003494 current_time(ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003495 ext4_mark_inode_dirty(handle, ent->dir);
3496 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3497 if (!ent->inlined) {
3498 retval = ext4_handle_dirty_dirent_node(handle,
3499 ent->dir, ent->bh);
3500 if (unlikely(retval)) {
3501 ext4_std_error(ent->dir->i_sb, retval);
3502 return retval;
3503 }
3504 }
3505 brelse(ent->bh);
3506 ent->bh = NULL;
3507
3508 return 0;
3509}
3510
3511static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3512 const struct qstr *d_name)
3513{
3514 int retval = -ENOENT;
3515 struct buffer_head *bh;
3516 struct ext4_dir_entry_2 *de;
3517
3518 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003519 if (IS_ERR(bh))
3520 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003521 if (bh) {
3522 retval = ext4_delete_entry(handle, dir, de, bh);
3523 brelse(bh);
3524 }
3525 return retval;
3526}
3527
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003528static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3529 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003530{
3531 int retval;
3532 /*
3533 * ent->de could have moved from under us during htree split, so make
3534 * sure that we are deleting the right entry. We might also be pointing
3535 * to a stale entry in the unused part of ent->bh so just checking inum
3536 * and the name isn't enough.
3537 */
3538 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3539 ent->de->name_len != ent->dentry->d_name.len ||
3540 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003541 ent->de->name_len) ||
3542 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003543 retval = ext4_find_delete_entry(handle, ent->dir,
3544 &ent->dentry->d_name);
3545 } else {
3546 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3547 if (retval == -ENOENT) {
3548 retval = ext4_find_delete_entry(handle, ent->dir,
3549 &ent->dentry->d_name);
3550 }
3551 }
3552
3553 if (retval) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003554 ext4_warning_inode(ent->dir,
3555 "Deleting old file: nlink %d, error=%d",
3556 ent->dir->i_nlink, retval);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003557 }
3558}
3559
Miklos Szeredibd429982014-04-01 17:08:44 +02003560static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3561{
3562 if (ent->dir_nlink_delta) {
3563 if (ent->dir_nlink_delta == -1)
3564 ext4_dec_count(handle, ent->dir);
3565 else
3566 ext4_inc_count(handle, ent->dir);
3567 ext4_mark_inode_dirty(handle, ent->dir);
3568 }
3569}
3570
Miklos Szeredicd808de2014-10-24 00:14:37 +02003571static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3572 int credits, handle_t **h)
3573{
3574 struct inode *wh;
3575 handle_t *handle;
3576 int retries = 0;
3577
3578 /*
3579 * for inode block, sb block, group summaries,
3580 * and inode bitmap
3581 */
3582 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3583 EXT4_XATTR_TRANS_BLOCKS + 4);
3584retry:
3585 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3586 &ent->dentry->d_name, 0, NULL,
3587 EXT4_HT_DIR, credits);
3588
3589 handle = ext4_journal_current_handle();
3590 if (IS_ERR(wh)) {
3591 if (handle)
3592 ext4_journal_stop(handle);
3593 if (PTR_ERR(wh) == -ENOSPC &&
3594 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3595 goto retry;
3596 } else {
3597 *h = handle;
3598 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3599 wh->i_op = &ext4_special_inode_operations;
3600 }
3601 return wh;
3602}
3603
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003604/*
3605 * Anybody can rename anything with this: the permission checks are left to the
3606 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003607 *
3608 * n.b. old_{dentry,inode) refers to the source dentry/inode
3609 * while new_{dentry,inode) refers to the destination dentry/inode
3610 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003611 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003612static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003613 struct inode *new_dir, struct dentry *new_dentry,
3614 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003615{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003616 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003617 struct ext4_renament old = {
3618 .dir = old_dir,
3619 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003620 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003621 };
3622 struct ext4_renament new = {
3623 .dir = new_dir,
3624 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003625 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003626 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003627 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003628 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003629 struct inode *whiteout = NULL;
3630 int credits;
3631 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003632
Theodore Ts'ob50282f2018-08-27 01:47:09 -04003633 if (new.inode && new.inode->i_nlink == 0) {
3634 EXT4_ERROR_INODE(new.inode,
3635 "target of rename is already freed");
3636 return -EFSCORRUPTED;
3637 }
3638
Li Xi040cb372016-01-08 16:01:21 -05003639 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3640 (!projid_eq(EXT4_I(new_dir)->i_projid,
3641 EXT4_I(old_dentry->d_inode)->i_projid)))
3642 return -EXDEV;
3643
Jan Karaa7cdade2015-06-29 16:22:54 +02003644 retval = dquot_initialize(old.dir);
3645 if (retval)
3646 return retval;
3647 retval = dquot_initialize(new.dir);
3648 if (retval)
3649 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003650
3651 /* Initialize quotas before so that eventual writes go
3652 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003653 if (new.inode) {
3654 retval = dquot_initialize(new.inode);
3655 if (retval)
3656 return retval;
3657 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003658
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003659 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003660 if (IS_ERR(old.bh))
3661 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003662 /*
3663 * Check for inode number is _not_ due to possible IO errors.
3664 * We might rmdir the source, keep it as pwd of some process
3665 * and merrily kill the link to whatever was created under the
3666 * same name. Goodbye sticky bit ;-<
3667 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003668 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003669 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003670 goto end_rename;
3671
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003672 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3673 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003674 if (IS_ERR(new.bh)) {
3675 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003676 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003677 goto end_rename;
3678 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003679 if (new.bh) {
3680 if (!new.inode) {
3681 brelse(new.bh);
3682 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683 }
3684 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003685 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3686 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003687
Miklos Szeredicd808de2014-10-24 00:14:37 +02003688 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3689 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3690 if (!(flags & RENAME_WHITEOUT)) {
3691 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003692 if (IS_ERR(handle)) {
3693 retval = PTR_ERR(handle);
3694 handle = NULL;
3695 goto end_rename;
3696 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003697 } else {
3698 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003699 if (IS_ERR(whiteout)) {
3700 retval = PTR_ERR(whiteout);
3701 whiteout = NULL;
3702 goto end_rename;
3703 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003704 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003705
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003706 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003707 ext4_handle_sync(handle);
3708
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003709 if (S_ISDIR(old.inode->i_mode)) {
3710 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003711 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003712 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003713 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003714 } else {
3715 retval = -EMLINK;
3716 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3717 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003718 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003719 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003720 if (retval)
3721 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003723 /*
3724 * If we're renaming a file within an inline_data dir and adding or
3725 * setting the new dirent causes a conversion from inline_data to
3726 * extents/blockmap, we need to force the dirent delete code to
3727 * re-read the directory, or else we end up trying to delete a dirent
3728 * from what is now the extent tree root (or a block map).
3729 */
3730 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3731 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003732
3733 old_file_type = old.de->file_type;
3734 if (whiteout) {
3735 /*
3736 * Do this before adding a new entry, so the old entry is sure
3737 * to be still pointing to the valid old entry.
3738 */
3739 retval = ext4_setent(handle, &old, whiteout->i_ino,
3740 EXT4_FT_CHRDEV);
3741 if (retval)
3742 goto end_rename;
3743 ext4_mark_inode_dirty(handle, whiteout);
3744 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003745 if (!new.bh) {
3746 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003747 if (retval)
3748 goto end_rename;
3749 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003750 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003751 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003752 if (retval)
3753 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003754 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003755 if (force_reread)
3756 force_reread = !ext4_test_inode_flag(new.dir,
3757 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758
3759 /*
3760 * Like most other Unix systems, set the ctime for inodes on a
3761 * rename.
3762 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003763 old.inode->i_ctime = current_time(old.inode);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003764 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003765
Miklos Szeredicd808de2014-10-24 00:14:37 +02003766 if (!whiteout) {
3767 /*
3768 * ok, that's it
3769 */
3770 ext4_rename_delete(handle, &old, force_reread);
3771 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003772
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003773 if (new.inode) {
3774 ext4_dec_count(handle, new.inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003775 new.inode->i_ctime = current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003776 }
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003777 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003778 ext4_update_dx_flag(old.dir);
3779 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003780 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3781 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003782 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003783
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003784 ext4_dec_count(handle, old.dir);
3785 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003786 /* checked ext4_empty_dir above, can't have another
3787 * parent, ext4_dec_count() won't work for many-linked
3788 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003789 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003790 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003791 ext4_inc_count(handle, new.dir);
3792 ext4_update_dx_flag(new.dir);
3793 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003794 }
3795 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003796 ext4_mark_inode_dirty(handle, old.dir);
3797 if (new.inode) {
3798 ext4_mark_inode_dirty(handle, new.inode);
3799 if (!new.inode->i_nlink)
3800 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003801 }
3802 retval = 0;
3803
3804end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003805 brelse(old.dir_bh);
3806 brelse(old.bh);
3807 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003808 if (whiteout) {
3809 if (retval)
3810 drop_nlink(whiteout);
3811 unlock_new_inode(whiteout);
3812 iput(whiteout);
3813 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003814 if (handle)
3815 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003816 return retval;
3817}
3818
Miklos Szeredibd429982014-04-01 17:08:44 +02003819static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3820 struct inode *new_dir, struct dentry *new_dentry)
3821{
3822 handle_t *handle = NULL;
3823 struct ext4_renament old = {
3824 .dir = old_dir,
3825 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003826 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003827 };
3828 struct ext4_renament new = {
3829 .dir = new_dir,
3830 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003831 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003832 };
3833 u8 new_file_type;
3834 int retval;
Deepa Dinamani95582b02018-05-08 19:36:02 -07003835 struct timespec64 ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02003836
Li Xi040cb372016-01-08 16:01:21 -05003837 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
3838 !projid_eq(EXT4_I(new_dir)->i_projid,
3839 EXT4_I(old_dentry->d_inode)->i_projid)) ||
3840 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
3841 !projid_eq(EXT4_I(old_dir)->i_projid,
3842 EXT4_I(new_dentry->d_inode)->i_projid)))
3843 return -EXDEV;
3844
Jan Karaa7cdade2015-06-29 16:22:54 +02003845 retval = dquot_initialize(old.dir);
3846 if (retval)
3847 return retval;
3848 retval = dquot_initialize(new.dir);
3849 if (retval)
3850 return retval;
Miklos Szeredibd429982014-04-01 17:08:44 +02003851
3852 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3853 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003854 if (IS_ERR(old.bh))
3855 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003856 /*
3857 * Check for inode number is _not_ due to possible IO errors.
3858 * We might rmdir the source, keep it as pwd of some process
3859 * and merrily kill the link to whatever was created under the
3860 * same name. Goodbye sticky bit ;-<
3861 */
3862 retval = -ENOENT;
3863 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3864 goto end_rename;
3865
3866 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3867 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003868 if (IS_ERR(new.bh)) {
3869 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003870 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003871 goto end_rename;
3872 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003873
3874 /* RENAME_EXCHANGE case: old *and* new must both exist */
3875 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3876 goto end_rename;
3877
3878 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3879 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3880 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003881 if (IS_ERR(handle)) {
3882 retval = PTR_ERR(handle);
3883 handle = NULL;
3884 goto end_rename;
3885 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003886
3887 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3888 ext4_handle_sync(handle);
3889
3890 if (S_ISDIR(old.inode->i_mode)) {
3891 old.is_dir = true;
3892 retval = ext4_rename_dir_prepare(handle, &old);
3893 if (retval)
3894 goto end_rename;
3895 }
3896 if (S_ISDIR(new.inode->i_mode)) {
3897 new.is_dir = true;
3898 retval = ext4_rename_dir_prepare(handle, &new);
3899 if (retval)
3900 goto end_rename;
3901 }
3902
3903 /*
3904 * Other than the special case of overwriting a directory, parents'
3905 * nlink only needs to be modified if this is a cross directory rename.
3906 */
3907 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3908 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3909 new.dir_nlink_delta = -old.dir_nlink_delta;
3910 retval = -EMLINK;
3911 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3912 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3913 goto end_rename;
3914 }
3915
3916 new_file_type = new.de->file_type;
3917 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3918 if (retval)
3919 goto end_rename;
3920
3921 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3922 if (retval)
3923 goto end_rename;
3924
3925 /*
3926 * Like most other Unix systems, set the ctime for inodes on a
3927 * rename.
3928 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003929 ctime = current_time(old.inode);
3930 old.inode->i_ctime = ctime;
3931 new.inode->i_ctime = ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02003932 ext4_mark_inode_dirty(handle, old.inode);
3933 ext4_mark_inode_dirty(handle, new.inode);
3934
3935 if (old.dir_bh) {
3936 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3937 if (retval)
3938 goto end_rename;
3939 }
3940 if (new.dir_bh) {
3941 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3942 if (retval)
3943 goto end_rename;
3944 }
3945 ext4_update_dir_count(handle, &old);
3946 ext4_update_dir_count(handle, &new);
3947 retval = 0;
3948
3949end_rename:
3950 brelse(old.dir_bh);
3951 brelse(new.dir_bh);
3952 brelse(old.bh);
3953 brelse(new.bh);
3954 if (handle)
3955 ext4_journal_stop(handle);
3956 return retval;
3957}
3958
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003959static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3960 struct inode *new_dir, struct dentry *new_dentry,
3961 unsigned int flags)
3962{
Eric Biggers07543d12017-10-18 20:21:57 -04003963 int err;
3964
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003965 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
3966 return -EIO;
3967
Miklos Szeredicd808de2014-10-24 00:14:37 +02003968 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003969 return -EINVAL;
3970
Eric Biggers07543d12017-10-18 20:21:57 -04003971 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
3972 flags);
3973 if (err)
3974 return err;
3975
Miklos Szeredibd429982014-04-01 17:08:44 +02003976 if (flags & RENAME_EXCHANGE) {
3977 return ext4_cross_rename(old_dir, old_dentry,
3978 new_dir, new_dentry);
3979 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003980
3981 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003982}
3983
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984/*
3985 * directories can handle most operations...
3986 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003987const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003988 .create = ext4_create,
3989 .lookup = ext4_lookup,
3990 .link = ext4_link,
3991 .unlink = ext4_unlink,
3992 .symlink = ext4_symlink,
3993 .mkdir = ext4_mkdir,
3994 .rmdir = ext4_rmdir,
3995 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003996 .tmpfile = ext4_tmpfile,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003997 .rename = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003998 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01003999 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004000 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004001 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004002 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04004003 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004004};
4005
Arjan van de Ven754661f2007-02-12 00:55:38 -08004006const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07004007 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01004008 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07004009 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02004010 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004011 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012};