blob: 7f3015a509f88efcc207e9104874ad505b693ee0 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/fcntl.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/quotaops.h>
34#include <linux/buffer_head.h>
35#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040036#include "ext4.h"
37#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039#include "xattr.h"
40#include "acl.h"
41
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040042#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043/*
44 * define how far ahead to read directories while searching them.
45 */
46#define NAMEI_RA_CHUNKS 2
47#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040048#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Mingming Cao617ba132006-10-11 01:20:53 -070050static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050052 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053{
54 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040055 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056
Theodore Ts'odf981d02012-08-17 09:48:17 -040057 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
58 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050059 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
60 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040061
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
63
Theodore Ts'oc5e298a2015-06-21 01:25:29 -040064 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
Theodore Ts'o1c215022014-08-29 20:52:15 -040065 if (IS_ERR(bh))
66 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050067 inode->i_size += inode->i_sb->s_blocksize;
68 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040069 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050070 err = ext4_journal_get_write_access(handle, bh);
71 if (err) {
72 brelse(bh);
73 ext4_std_error(inode->i_sb, err);
74 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040075 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 return bh;
77}
78
Theodore Ts'odc6982f2013-02-14 23:59:26 -050079static int ext4_dx_csum_verify(struct inode *inode,
80 struct ext4_dir_entry *dirent);
81
Theodore Ts'o3f0307b2019-06-20 21:19:02 -040082/*
83 * Hints to ext4_read_dirblock regarding whether we expect a directory
84 * block being read to be an index block, or a block containing
85 * directory entries (and if the latter, whether it was found via a
86 * logical block in an htree index block). This is used to control
87 * what sort of sanity checkinig ext4_read_dirblock() will do on the
88 * directory block read from the storage device. EITHER will means
89 * the caller doesn't know what kind of directory block will be read,
90 * so no specific verification will be done.
91 */
Theodore Ts'odc6982f2013-02-14 23:59:26 -050092typedef enum {
Theodore Ts'o3f0307b2019-06-20 21:19:02 -040093 EITHER, INDEX, DIRENT, DIRENT_HTREE
Theodore Ts'odc6982f2013-02-14 23:59:26 -050094} dirblock_type_t;
95
96#define ext4_read_dirblock(inode, block, type) \
Andreas Dilgerb03a2f72015-06-15 14:50:26 -040097 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
Theodore Ts'odc6982f2013-02-14 23:59:26 -050098
99static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400100 ext4_lblk_t block,
101 dirblock_type_t type,
102 const char *func,
103 unsigned int line)
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500104{
105 struct buffer_head *bh;
106 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400107 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500108
Theodore Ts'o1c215022014-08-29 20:52:15 -0400109 bh = ext4_bread(NULL, inode, block, 0);
110 if (IS_ERR(bh)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400111 __ext4_warning(inode->i_sb, func, line,
112 "inode #%lu: lblock %lu: comm %s: "
113 "error %ld reading directory block",
114 inode->i_ino, (unsigned long)block,
115 current->comm, PTR_ERR(bh));
Theodore Ts'o1c215022014-08-29 20:52:15 -0400116
117 return bh;
118 }
Theodore Ts'o3f0307b2019-06-20 21:19:02 -0400119 if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400120 ext4_error_inode(inode, func, line, block,
Theodore Ts'o3f0307b2019-06-20 21:19:02 -0400121 "Directory hole found for htree %s block",
122 (type == INDEX) ? "index" : "leaf");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400123 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500124 }
Theodore Ts'o3f0307b2019-06-20 21:19:02 -0400125 if (!bh)
126 return NULL;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500127 dirent = (struct ext4_dir_entry *) bh->b_data;
128 /* Determine whether or not we have an index block */
129 if (is_dx(inode)) {
130 if (block == 0)
131 is_dx_block = 1;
132 else if (ext4_rec_len_from_disk(dirent->rec_len,
133 inode->i_sb->s_blocksize) ==
134 inode->i_sb->s_blocksize)
135 is_dx_block = 1;
136 }
137 if (!is_dx_block && type == INDEX) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400138 ext4_error_inode(inode, func, line, block,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500139 "directory leaf block found instead of index block");
Vasily Averind450fcd2018-11-07 22:36:23 -0500140 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400141 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500142 }
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400143 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500144 buffer_verified(bh))
145 return bh;
146
147 /*
148 * An empty leaf block can get mistaken for a index block; for
149 * this reason, we can only check the index checksum when the
150 * caller is sure it should be an index block.
151 */
152 if (is_dx_block && type == INDEX) {
153 if (ext4_dx_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 index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400159 return ERR_PTR(-EFSBADCRC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 }
161 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500162 if (!is_dx_block) {
163 if (ext4_dirent_csum_verify(inode, dirent))
164 set_buffer_verified(bh);
165 else {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400166 ext4_error_inode(inode, func, line, block,
167 "Directory block failed checksum");
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500168 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400169 return ERR_PTR(-EFSBADCRC);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500170 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171 }
172 return bh;
173}
174
175#ifndef assert
176#define assert(test) J_ASSERT(test)
177#endif
178
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179#ifdef DX_DEBUG
180#define dxtrace(command) command
181#else
182#define dxtrace(command)
183#endif
184
185struct fake_dirent
186{
187 __le32 inode;
188 __le16 rec_len;
189 u8 name_len;
190 u8 file_type;
191};
192
193struct dx_countlimit
194{
195 __le16 limit;
196 __le16 count;
197};
198
199struct dx_entry
200{
201 __le32 hash;
202 __le32 block;
203};
204
205/*
206 * dx_root_info is laid out so that if it should somehow get overlaid by a
207 * dirent the two low bits of the hash version will be zero. Therefore, the
208 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
209 */
210
211struct dx_root
212{
213 struct fake_dirent dot;
214 char dot_name[4];
215 struct fake_dirent dotdot;
216 char dotdot_name[4];
217 struct dx_root_info
218 {
219 __le32 reserved_zero;
220 u8 hash_version;
221 u8 info_length; /* 8 */
222 u8 indirect_levels;
223 u8 unused_flags;
224 }
225 info;
226 struct dx_entry entries[0];
227};
228
229struct dx_node
230{
231 struct fake_dirent fake;
232 struct dx_entry entries[0];
233};
234
235
236struct dx_frame
237{
238 struct buffer_head *bh;
239 struct dx_entry *entries;
240 struct dx_entry *at;
241};
242
243struct dx_map_entry
244{
245 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700246 u16 offs;
247 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248};
249
Darrick J. Wonge6153912012-04-29 18:23:10 -0400250/*
251 * This goes at the end of each htree block.
252 */
253struct dx_tail {
254 u32 dt_reserved;
255 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
256};
257
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500258static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
259static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400260static inline unsigned dx_get_hash(struct dx_entry *entry);
261static void dx_set_hash(struct dx_entry *entry, unsigned value);
262static unsigned dx_get_count(struct dx_entry *entries);
263static unsigned dx_get_limit(struct dx_entry *entries);
264static void dx_set_count(struct dx_entry *entries, unsigned value);
265static void dx_set_limit(struct dx_entry *entries, unsigned value);
266static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
267static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400268static struct dx_frame *dx_probe(struct ext4_filename *fname,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 struct inode *dir,
270 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400271 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400272static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400273static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
274 unsigned blocksize, struct dx_hash_info *hinfo,
275 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400277static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500278 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500279static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500280static void dx_insert_block(struct dx_frame *frame,
281 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700282static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 struct dx_frame *frame,
284 struct dx_frame *frames,
285 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400286static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400287 struct ext4_filename *fname,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400288 struct ext4_dir_entry_2 **res_dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400289static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -0500290 struct inode *dir, struct inode *inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400292/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500293void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
294 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400295{
296 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
297 t->det_rec_len = ext4_rec_len_to_disk(
298 sizeof(struct ext4_dir_entry_tail), blocksize);
299 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
300}
301
302/* Walk through a dirent block to find a checksum "dirent" at the tail */
303static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
304 struct ext4_dir_entry *de)
305{
306 struct ext4_dir_entry_tail *t;
307
308#ifdef PARANOID
309 struct ext4_dir_entry *d, *top;
310
311 d = de;
312 top = (struct ext4_dir_entry *)(((void *)de) +
313 (EXT4_BLOCK_SIZE(inode->i_sb) -
314 sizeof(struct ext4_dir_entry_tail)));
315 while (d < top && d->rec_len)
316 d = (struct ext4_dir_entry *)(((void *)d) +
317 le16_to_cpu(d->rec_len));
318
319 if (d != top)
320 return NULL;
321
322 t = (struct ext4_dir_entry_tail *)d;
323#else
324 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
325#endif
326
327 if (t->det_reserved_zero1 ||
328 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
329 t->det_reserved_zero2 ||
330 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
331 return NULL;
332
333 return t;
334}
335
336static __le32 ext4_dirent_csum(struct inode *inode,
337 struct ext4_dir_entry *dirent, int size)
338{
339 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
340 struct ext4_inode_info *ei = EXT4_I(inode);
341 __u32 csum;
342
343 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
344 return cpu_to_le32(csum);
345}
346
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400347#define warn_no_space_for_csum(inode) \
348 __warn_no_space_for_csum((inode), __func__, __LINE__)
349
350static void __warn_no_space_for_csum(struct inode *inode, const char *func,
351 unsigned int line)
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500352{
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400353 __ext4_warning_inode(inode, func, line,
354 "No space for directory leaf checksum. Please run e2fsck -D.");
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500355}
356
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400357int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
358{
359 struct ext4_dir_entry_tail *t;
360
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400361 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400362 return 1;
363
364 t = get_dirent_tail(inode, dirent);
365 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500366 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400367 return 0;
368 }
369
370 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
371 (void *)t - (void *)dirent))
372 return 0;
373
374 return 1;
375}
376
377static void ext4_dirent_csum_set(struct inode *inode,
378 struct ext4_dir_entry *dirent)
379{
380 struct ext4_dir_entry_tail *t;
381
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400382 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400383 return;
384
385 t = get_dirent_tail(inode, dirent);
386 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500387 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400388 return;
389 }
390
391 t->det_checksum = ext4_dirent_csum(inode, dirent,
392 (void *)t - (void *)dirent);
393}
394
Tao Ma3c47d542012-12-10 14:05:59 -0500395int ext4_handle_dirty_dirent_node(handle_t *handle,
396 struct inode *inode,
397 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400398{
399 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
400 return ext4_handle_dirty_metadata(handle, inode, bh);
401}
402
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400403static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
404 struct ext4_dir_entry *dirent,
405 int *offset)
406{
407 struct ext4_dir_entry *dp;
408 struct dx_root_info *root;
409 int count_offset;
410
411 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
412 count_offset = 8;
413 else if (le16_to_cpu(dirent->rec_len) == 12) {
414 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
415 if (le16_to_cpu(dp->rec_len) !=
416 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
417 return NULL;
418 root = (struct dx_root_info *)(((void *)dp + 12));
419 if (root->reserved_zero ||
420 root->info_length != sizeof(struct dx_root_info))
421 return NULL;
422 count_offset = 32;
423 } else
424 return NULL;
425
426 if (offset)
427 *offset = count_offset;
428 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
429}
430
431static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
432 int count_offset, int count, struct dx_tail *t)
433{
434 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
435 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400436 __u32 csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400437 int size;
Daeho Jeongb47820e2016-07-03 17:51:39 -0400438 __u32 dummy_csum = 0;
439 int offset = offsetof(struct dx_tail, dt_checksum);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400440
441 size = count_offset + (count * sizeof(struct dx_entry));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400442 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400443 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
444 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400445
446 return cpu_to_le32(csum);
447}
448
449static int ext4_dx_csum_verify(struct inode *inode,
450 struct ext4_dir_entry *dirent)
451{
452 struct dx_countlimit *c;
453 struct dx_tail *t;
454 int count_offset, limit, count;
455
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400456 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400457 return 1;
458
459 c = get_dx_countlimit(inode, dirent, &count_offset);
460 if (!c) {
461 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
Daeho Jeongfa964542016-07-03 21:11:08 -0400462 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400463 }
464 limit = le16_to_cpu(c->limit);
465 count = le16_to_cpu(c->count);
466 if (count_offset + (limit * sizeof(struct dx_entry)) >
467 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500468 warn_no_space_for_csum(inode);
Daeho Jeongfa964542016-07-03 21:11:08 -0400469 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400470 }
471 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
472
473 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
474 count, t))
475 return 0;
476 return 1;
477}
478
479static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
480{
481 struct dx_countlimit *c;
482 struct dx_tail *t;
483 int count_offset, limit, count;
484
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400485 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400486 return;
487
488 c = get_dx_countlimit(inode, dirent, &count_offset);
489 if (!c) {
490 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
491 return;
492 }
493 limit = le16_to_cpu(c->limit);
494 count = le16_to_cpu(c->count);
495 if (count_offset + (limit * sizeof(struct dx_entry)) >
496 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500497 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400498 return;
499 }
500 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
501
502 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
503}
504
505static inline int ext4_handle_dirty_dx_node(handle_t *handle,
506 struct inode *inode,
507 struct buffer_head *bh)
508{
509 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
510 return ext4_handle_dirty_metadata(handle, inode, bh);
511}
512
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513/*
Li Zefanf795e142008-07-11 19:27:31 -0400514 * p is at least 6 bytes before the end of page
515 */
516static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500517ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400518{
519 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500520 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400521}
522
523/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 * Future: use high four bits of block for coalesce-on-delete flags
525 * Mask them off for now.
526 */
527
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500528static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529{
530 return le32_to_cpu(entry->block) & 0x00ffffff;
531}
532
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500533static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534{
535 entry->block = cpu_to_le32(value);
536}
537
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400538static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539{
540 return le32_to_cpu(entry->hash);
541}
542
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400543static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544{
545 entry->hash = cpu_to_le32(value);
546}
547
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400548static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
550 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
551}
552
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400553static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554{
555 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
556}
557
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400558static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
560 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
561}
562
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400563static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564{
565 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
566}
567
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400568static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
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(1) -
571 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400572
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400573 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400574 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400575 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576}
577
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400578static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579{
Mingming Cao617ba132006-10-11 01:20:53 -0700580 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400581
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400582 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400583 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400584 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585}
586
587/*
588 * Debug
589 */
590#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400591static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592{
Andrew Morton63f57932006-10-11 01:21:24 -0700593 int i, n = dx_get_count (entries);
Joe Perchesd74f3d22016-10-15 09:57:31 -0400594 printk(KERN_DEBUG "%s index", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700595 for (i = 0; i < n; i++) {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400596 printk(KERN_CONT " %x->%lu",
597 i ? dx_get_hash(entries + i) : 0,
598 (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700599 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400600 printk(KERN_CONT "\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601}
602
603struct stats
604{
605 unsigned names;
606 unsigned space;
607 unsigned bcount;
608};
609
Michael Halcrowb3098482015-04-12 01:07:01 -0400610static struct stats dx_show_leaf(struct inode *dir,
611 struct dx_hash_info *hinfo,
612 struct ext4_dir_entry_2 *de,
613 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614{
615 unsigned names = 0, space = 0;
616 char *base = (char *) de;
617 struct dx_hash_info h = *hinfo;
618
619 printk("names: ");
620 while ((char *) de < base + size)
621 {
622 if (de->inode)
623 {
624 if (show_names)
625 {
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530626#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrowb3098482015-04-12 01:07:01 -0400627 int len;
628 char *name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400629 struct fscrypt_str fname_crypto_str =
630 FSTR_INIT(NULL, 0);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400631 int res = 0;
Michael Halcrowb3098482015-04-12 01:07:01 -0400632
633 name = de->name;
634 len = de->name_len;
Chandan Rajendrae68f6b82018-12-12 15:20:10 +0530635 if (IS_ENCRYPTED(dir))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400636 res = fscrypt_get_encryption_info(dir);
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400637 if (res) {
638 printk(KERN_WARNING "Error setting up"
639 " fname crypto: %d\n", res);
Michael Halcrowb3098482015-04-12 01:07:01 -0400640 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400641 if (!fscrypt_has_encryption_key(dir)) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400642 /* Directory is not encrypted */
643 ext4fs_dirhash(de->name,
644 de->name_len, &h);
645 printk("%*.s:(U)%x.%u ", len,
646 name, h.hash,
647 (unsigned) ((char *) de
648 - base));
649 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400650 struct fscrypt_str de_name =
651 FSTR_INIT(name, len);
652
Michael Halcrowb3098482015-04-12 01:07:01 -0400653 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400654 res = fscrypt_fname_alloc_buffer(
655 dir, len,
Michael Halcrowb3098482015-04-12 01:07:01 -0400656 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400657 if (res)
Michael Halcrowb3098482015-04-12 01:07:01 -0400658 printk(KERN_WARNING "Error "
659 "allocating crypto "
660 "buffer--skipping "
661 "crypto\n");
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400662 res = fscrypt_fname_disk_to_usr(dir,
663 0, 0, &de_name,
664 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400665 if (res) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400666 printk(KERN_WARNING "Error "
667 "converting filename "
668 "from disk to usr"
669 "\n");
670 name = "??";
671 len = 2;
672 } else {
673 name = fname_crypto_str.name;
674 len = fname_crypto_str.len;
675 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400676 ext4fs_dirhash(de->name, de->name_len,
677 &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400678 printk("%*.s:(E)%x.%u ", len, name,
679 h.hash, (unsigned) ((char *) de
680 - base));
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400681 fscrypt_fname_free_buffer(
682 &fname_crypto_str);
Michael Halcrowb3098482015-04-12 01:07:01 -0400683 }
684#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 int len = de->name_len;
686 char *name = de->name;
Mingming Cao617ba132006-10-11 01:20:53 -0700687 ext4fs_dirhash(de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400688 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400689 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400690#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 }
Mingming Cao617ba132006-10-11 01:20:53 -0700692 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 names++;
694 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500695 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400697 printk(KERN_CONT "(%i)\n", names);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 return (struct stats) { names, space, 1 };
699}
700
701struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
702 struct dx_entry *entries, int levels)
703{
704 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400705 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 unsigned bcount = 0;
707 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 printk("%i indexed blocks...\n", count);
709 for (i = 0; i < count; i++, entries++)
710 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500711 ext4_lblk_t block = dx_get_block(entries);
712 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
714 struct stats stats;
715 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400716 bh = ext4_bread(NULL,dir, block, 0);
717 if (!bh || IS_ERR(bh))
718 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 stats = levels?
720 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400721 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
722 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 names += stats.names;
724 space += stats.space;
725 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400726 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 }
728 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400729 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400730 levels ? "" : " ", names, space/bcount,
731 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732 return (struct stats) { names, space, bcount};
733}
734#endif /* DX_DEBUG */
735
736/*
737 * Probe for a directory leaf block to search.
738 *
739 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
740 * error in the directory index, and the caller should fall back to
741 * searching the directory normally. The callers of dx_probe **MUST**
742 * check for this error code, and make sure it never gets reflected
743 * back to userspace.
744 */
745static struct dx_frame *
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400746dx_probe(struct ext4_filename *fname, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400747 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748{
749 unsigned count, indirect;
750 struct dx_entry *at, *entries, *p, *q, *m;
751 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400753 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 u32 hash;
755
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400756 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
757 if (IS_ERR(frame->bh))
758 return (struct dx_frame *) frame->bh;
759
760 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 if (root->info.hash_version != DX_HASH_TEA &&
762 root->info.hash_version != DX_HASH_HALF_MD4 &&
763 root->info.hash_version != DX_HASH_LEGACY) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400764 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
765 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766 goto fail;
767 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400768 if (fname)
769 hinfo = &fname->hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400771 if (hinfo->hash_version <= DX_HASH_TEA)
772 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700773 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400774 if (fname && fname_name(fname))
775 ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 hash = hinfo->hash;
777
778 if (root->info.unused_flags & 1) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400779 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
780 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 goto fail;
782 }
783
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400784 indirect = root->info.indirect_levels;
785 if (indirect > 1) {
786 ext4_warning_inode(dir, "Unimplemented hash depth: %#06x",
787 root->info.indirect_levels);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 goto fail;
789 }
790
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400791 entries = (struct dx_entry *)(((char *)&root->info) +
792 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700793
794 if (dx_get_limit(entries) != dx_root_limit(dir,
795 root->info.info_length)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400796 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
797 dx_get_limit(entries),
798 dx_root_limit(dir, root->info.info_length));
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700799 goto fail;
800 }
801
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400802 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400803 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700805 if (!count || count > dx_get_limit(entries)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400806 ext4_warning_inode(dir,
807 "dx entry: count %u beyond limit %u",
808 count, dx_get_limit(entries));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400809 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700810 }
811
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 p = entries + 1;
813 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400814 while (p <= q) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400815 m = p + (q - p) / 2;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400816 dxtrace(printk(KERN_CONT "."));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 if (dx_get_hash(m) > hash)
818 q = m - 1;
819 else
820 p = m + 1;
821 }
822
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400823 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 unsigned n = count - 1;
825 at = entries;
826 while (n--)
827 {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400828 dxtrace(printk(KERN_CONT ","));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829 if (dx_get_hash(++at) > hash)
830 {
831 at--;
832 break;
833 }
834 }
835 assert (at == p - 1);
836 }
837
838 at = p - 1;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400839 dxtrace(printk(KERN_CONT " %x->%u\n",
840 at == entries ? 0 : dx_get_hash(at),
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400841 dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 frame->entries = entries;
843 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400844 if (!indirect--)
845 return frame;
846 frame++;
847 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
848 if (IS_ERR(frame->bh)) {
849 ret_err = (struct dx_frame *) frame->bh;
850 frame->bh = NULL;
851 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400852 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400853 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400854
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400855 if (dx_get_limit(entries) != dx_node_limit(dir)) {
856 ext4_warning_inode(dir,
857 "dx entry: limit %u != node limit %u",
858 dx_get_limit(entries), dx_node_limit(dir));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400859 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700860 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400862fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 while (frame >= frame_in) {
864 brelse(frame->bh);
865 frame--;
866 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400867
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400868 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400869 ext4_warning_inode(dir,
870 "Corrupt directory, running e2fsck is recommended");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400871 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872}
873
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400874static void dx_release(struct dx_frame *frames)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875{
876 if (frames[0].bh == NULL)
877 return;
878
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400879 if (((struct dx_root *)frames[0].bh->b_data)->info.indirect_levels)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880 brelse(frames[1].bh);
881 brelse(frames[0].bh);
882}
883
884/*
885 * This function increments the frame pointer to search the next leaf
886 * block, and reads in the necessary intervening nodes if the search
887 * should be necessary. Whether or not the search is necessary is
888 * controlled by the hash parameter. If the hash value is even, then
889 * the search is only continued if the next block starts with that
890 * hash value. This is used if we are searching for a specific file.
891 *
892 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
893 *
894 * This function returns 1 if the caller should continue to search,
895 * or 0 if it should not. If there is an error reading one of the
896 * index blocks, it will a negative error code.
897 *
898 * If start_hash is non-null, it will be filled in with the starting
899 * hash of the next page.
900 */
Mingming Cao617ba132006-10-11 01:20:53 -0700901static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 struct dx_frame *frame,
903 struct dx_frame *frames,
904 __u32 *start_hash)
905{
906 struct dx_frame *p;
907 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500908 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 __u32 bhash;
910
911 p = frame;
912 /*
913 * Find the next leaf page by incrementing the frame pointer.
914 * If we run out of entries in the interior node, loop around and
915 * increment pointer in the parent node. When we break out of
916 * this loop, num_frames indicates the number of interior
917 * nodes need to be read.
918 */
919 while (1) {
920 if (++(p->at) < p->entries + dx_get_count(p->entries))
921 break;
922 if (p == frames)
923 return 0;
924 num_frames++;
925 p--;
926 }
927
928 /*
929 * If the hash is 1, then continue only if the next page has a
930 * continuation hash of any value. This is used for readdir
931 * handling. Otherwise, check to see if the hash matches the
932 * desired contiuation hash. If it doesn't, return since
933 * there's no point to read in the successive index pages.
934 */
935 bhash = dx_get_hash(p->at);
936 if (start_hash)
937 *start_hash = bhash;
938 if ((hash & 1) == 0) {
939 if ((bhash & ~1) != hash)
940 return 0;
941 }
942 /*
943 * If the hash is HASH_NB_ALWAYS, we always go to the next
944 * block so no check is necessary
945 */
946 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500947 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
948 if (IS_ERR(bh))
949 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400951 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 p->bh = bh;
953 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
954 }
955 return 1;
956}
957
958
959/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 * This function fills a red-black tree with information from a
961 * directory block. It returns the number directory entries loaded
962 * into the tree. If there is an error it is returned in err.
963 */
964static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500965 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 struct dx_hash_info *hinfo,
967 __u32 start_hash, __u32 start_minor_hash)
968{
969 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700970 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400971 int err = 0, count = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400972 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500974 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
975 (unsigned long)block));
Theodore Ts'o3f0307b2019-06-20 21:19:02 -0400976 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500977 if (IS_ERR(bh))
978 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400979
Mingming Cao617ba132006-10-11 01:20:53 -0700980 de = (struct ext4_dir_entry_2 *) bh->b_data;
981 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700983 EXT4_DIR_REC_LEN(0));
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +0530984#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400985 /* Check if the directory is encrypted */
Chandan Rajendrae68f6b82018-12-12 15:20:10 +0530986 if (IS_ENCRYPTED(dir)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400987 err = fscrypt_get_encryption_info(dir);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400988 if (err < 0) {
989 brelse(bh);
990 return err;
991 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400992 err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400993 &fname_crypto_str);
994 if (err < 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400995 brelse(bh);
996 return err;
997 }
998 }
999#endif
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001000 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001001 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -05001002 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001003 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1004 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001005 /* silently ignore the rest of the block */
1006 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001007 }
Mingming Cao617ba132006-10-11 01:20:53 -07001008 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009 if ((hinfo->hash < start_hash) ||
1010 ((hinfo->hash == start_hash) &&
1011 (hinfo->minor_hash < start_minor_hash)))
1012 continue;
1013 if (de->inode == 0)
1014 continue;
Chandan Rajendrae68f6b82018-12-12 15:20:10 +05301015 if (!IS_ENCRYPTED(dir)) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001016 tmp_str.name = de->name;
1017 tmp_str.len = de->name_len;
1018 err = ext4_htree_store_dirent(dir_file,
1019 hinfo->hash, hinfo->minor_hash, de,
1020 &tmp_str);
1021 } else {
Theodore Ts'od2299592015-05-18 13:15:47 -04001022 int save_len = fname_crypto_str.len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001023 struct fscrypt_str de_name = FSTR_INIT(de->name,
1024 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -04001025
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001026 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001027 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1028 hinfo->minor_hash, &de_name,
1029 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001030 if (err) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001031 count = err;
1032 goto errout;
1033 }
1034 err = ext4_htree_store_dirent(dir_file,
1035 hinfo->hash, hinfo->minor_hash, de,
1036 &fname_crypto_str);
Theodore Ts'od2299592015-05-18 13:15:47 -04001037 fname_crypto_str.len = save_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001038 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001039 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001040 count = err;
1041 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042 }
1043 count++;
1044 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001045errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 brelse(bh);
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +05301047#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001048 fscrypt_fname_free_buffer(&fname_crypto_str);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001049#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 return count;
1051}
1052
1053
1054/*
1055 * This function fills a red-black tree with information from a
1056 * directory. We start scanning the directory in hash order, starting
1057 * at start_hash and start_minor_hash.
1058 *
1059 * This function returns the number of entries inserted into the tree,
1060 * or a negative error code.
1061 */
Mingming Cao617ba132006-10-11 01:20:53 -07001062int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 __u32 start_minor_hash, __u32 *next_hash)
1064{
1065 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001066 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 struct dx_frame frames[2], *frame;
1068 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001069 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001071 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 __u32 hashval;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001073 struct fscrypt_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074
Theodore Ts'o60e66792010-05-17 07:00:00 -04001075 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001076 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001077 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001078 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001079 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001080 if (hinfo.hash_version <= DX_HASH_TEA)
1081 hinfo.hash_version +=
1082 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001083 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001084 if (ext4_has_inline_data(dir)) {
1085 int has_inline_data = 1;
1086 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1087 &hinfo, start_hash,
1088 start_minor_hash,
1089 &has_inline_data);
1090 if (has_inline_data) {
1091 *next_hash = ~0;
1092 return count;
1093 }
1094 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1096 start_hash, start_minor_hash);
1097 *next_hash = ~0;
1098 return count;
1099 }
1100 hinfo.hash = start_hash;
1101 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001102 frame = dx_probe(NULL, dir, &hinfo, frames);
1103 if (IS_ERR(frame))
1104 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105
1106 /* Add '.' and '..' from the htree header */
1107 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001108 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001109 tmp_str.name = de->name;
1110 tmp_str.len = de->name_len;
1111 err = ext4_htree_store_dirent(dir_file, 0, 0,
1112 de, &tmp_str);
1113 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 goto errout;
1115 count++;
1116 }
1117 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001118 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001119 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001120 tmp_str.name = de->name;
1121 tmp_str.len = de->name_len;
1122 err = ext4_htree_store_dirent(dir_file, 2, 0,
1123 de, &tmp_str);
1124 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 goto errout;
1126 count++;
1127 }
1128
1129 while (1) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -04001130 if (fatal_signal_pending(current)) {
1131 err = -ERESTARTSYS;
1132 goto errout;
1133 }
1134 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 block = dx_get_block(frame->at);
1136 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1137 start_hash, start_minor_hash);
1138 if (ret < 0) {
1139 err = ret;
1140 goto errout;
1141 }
1142 count += ret;
1143 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001144 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145 frame, frames, &hashval);
1146 *next_hash = hashval;
1147 if (ret < 0) {
1148 err = ret;
1149 goto errout;
1150 }
1151 /*
1152 * Stop if: (a) there are no more entries, or
1153 * (b) we have inserted at least one entry and the
1154 * next hash value is not a continuation
1155 */
1156 if ((ret == 0) ||
1157 (count && ((hashval & 1) == 0)))
1158 break;
1159 }
1160 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001161 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1162 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 return count;
1164errout:
1165 dx_release(frames);
1166 return (err);
1167}
1168
Tao Ma7335cd32012-12-10 14:05:59 -05001169static inline int search_dirblock(struct buffer_head *bh,
1170 struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001171 struct ext4_filename *fname,
Tao Ma7335cd32012-12-10 14:05:59 -05001172 unsigned int offset,
1173 struct ext4_dir_entry_2 **res_dir)
1174{
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001175 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
Eric Biggers14404512017-05-24 18:10:49 -04001176 fname, offset, res_dir);
Tao Ma7335cd32012-12-10 14:05:59 -05001177}
1178
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179/*
1180 * Directory block splitting, compacting
1181 */
1182
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001183/*
1184 * Create map of hash values, offsets, and sizes, stored at end of block.
1185 * Returns number of entries mapped.
1186 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001187static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1188 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001189 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001190{
1191 int count = 0;
1192 char *base = (char *) de;
1193 struct dx_hash_info h = *hinfo;
1194
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001195 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001197 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198 map_tail--;
1199 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001200 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001201 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 count++;
1203 cond_resched();
1204 }
1205 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001206 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207 }
1208 return count;
1209}
1210
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001211/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1213{
Andrew Morton63f57932006-10-11 01:21:24 -07001214 struct dx_map_entry *p, *q, *top = map + count - 1;
1215 int more;
1216 /* Combsort until bubble sort doesn't suck */
1217 while (count > 2) {
1218 count = count*10/13;
1219 if (count - 9 < 2) /* 9, 10 -> 11 */
1220 count = 11;
1221 for (p = top, q = p - count; q >= map; p--, q--)
1222 if (p->hash < q->hash)
1223 swap(*p, *q);
1224 }
1225 /* Garden variety bubble sort */
1226 do {
1227 more = 0;
1228 q = top;
1229 while (q-- > map) {
1230 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001232 swap(*(q+1), *q);
1233 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 }
1235 } while(more);
1236}
1237
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001238static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239{
1240 struct dx_entry *entries = frame->entries;
1241 struct dx_entry *old = frame->at, *new = old + 1;
1242 int count = dx_get_count(entries);
1243
1244 assert(count < dx_get_limit(entries));
1245 assert(old < entries + count);
1246 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1247 dx_set_hash(new, hash);
1248 dx_set_block(new, block);
1249 dx_set_count(entries, count + 1);
1250}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252/*
Hyojun Kim63da4202017-10-06 17:10:08 -07001253 * Test whether a directory entry matches the filename being searched for.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001254 *
Hyojun Kim63da4202017-10-06 17:10:08 -07001255 * Return: %true if the directory entry matches, otherwise %false.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 */
Hyojun Kim63da4202017-10-06 17:10:08 -07001257static inline bool ext4_match(const struct ext4_filename *fname,
1258 const struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259{
Hyojun Kim63da4202017-10-06 17:10:08 -07001260 struct fscrypt_name f;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001261
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 if (!de->inode)
Hyojun Kim63da4202017-10-06 17:10:08 -07001263 return false;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001264
Hyojun Kim63da4202017-10-06 17:10:08 -07001265 f.usr_fname = fname->usr_fname;
1266 f.disk_name = fname->disk_name;
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +05301267#ifdef CONFIG_FS_ENCRYPTION
Hyojun Kim63da4202017-10-06 17:10:08 -07001268 f.crypto_buf = fname->crypto_buf;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001269#endif
Hyojun Kim63da4202017-10-06 17:10:08 -07001270 return fscrypt_match_name(&f, de->name, de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271}
1272
1273/*
1274 * Returns 0 if not found, -1 on failure, and 1 on success
1275 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001276int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1277 struct inode *dir, struct ext4_filename *fname,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001278 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279{
Mingming Cao617ba132006-10-11 01:20:53 -07001280 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 char * dlimit;
1282 int de_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001283
Tao Ma7335cd32012-12-10 14:05:59 -05001284 de = (struct ext4_dir_entry_2 *)search_buf;
1285 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 while ((char *) de < dlimit) {
1287 /* this code is executed quadratically often */
1288 /* do minimal checking `by hand' */
Hyojun Kim63da4202017-10-06 17:10:08 -07001289 if ((char *) de + de->name_len <= dlimit &&
1290 ext4_match(fname, de)) {
1291 /* found a match - just to be sure, do
1292 * a full check */
1293 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1294 bh->b_size, offset))
1295 return -1;
1296 *res_dir = de;
1297 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 }
1299 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001300 de_len = ext4_rec_len_from_disk(de->rec_len,
1301 dir->i_sb->s_blocksize);
Hyojun Kim63da4202017-10-06 17:10:08 -07001302 if (de_len <= 0)
1303 return -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001304 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001305 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306 }
Hyojun Kim63da4202017-10-06 17:10:08 -07001307 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308}
1309
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001310static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1311 struct ext4_dir_entry *de)
1312{
1313 struct super_block *sb = dir->i_sb;
1314
1315 if (!is_dx(dir))
1316 return 0;
1317 if (block == 0)
1318 return 1;
1319 if (de->inode == 0 &&
1320 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1321 sb->s_blocksize)
1322 return 1;
1323 return 0;
1324}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325
1326/*
Eric Biggers3a3ac012019-03-20 11:39:13 -07001327 * __ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001328 *
1329 * finds an entry in the specified directory with the wanted name. It
1330 * returns the cache buffer in which the entry was found, and the entry
1331 * itself (as a parameter - res_dir). It does NOT read the inode of the
1332 * entry - you'll have to do that yourself if you want to.
1333 *
1334 * The returned buffer_head has ->b_count elevated. The caller is expected
1335 * to brelse() it when appropriate.
1336 */
Eric Biggers3a3ac012019-03-20 11:39:13 -07001337static struct buffer_head *__ext4_find_entry(struct inode *dir,
1338 struct ext4_filename *fname,
1339 struct ext4_dir_entry_2 **res_dir,
1340 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001342 struct super_block *sb;
1343 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1344 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001345 ext4_lblk_t start, block, b;
Eric Biggers3a3ac012019-03-20 11:39:13 -07001346 const u8 *name = fname->usr_fname->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347 int ra_max = 0; /* Number of bh's in the readahead
1348 buffer, bh_use[] */
1349 int ra_ptr = 0; /* Current index into readahead
1350 buffer */
1351 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001352 ext4_lblk_t nblocks;
Jaegeuk Kimc9370c22019-08-02 10:08:12 -07001353 int i, namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354
1355 *res_dir = NULL;
1356 sb = dir->i_sb;
Eric Biggers3a3ac012019-03-20 11:39:13 -07001357 namelen = fname->usr_fname->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001358 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001359 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001360
1361 if (ext4_has_inline_data(dir)) {
1362 int has_inline_data = 1;
Eric Biggers3a3ac012019-03-20 11:39:13 -07001363 ret = ext4_find_inline_entry(dir, fname, res_dir,
Tao Mae8e948e2012-12-10 14:06:00 -05001364 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001365 if (has_inline_data) {
1366 if (inlined)
1367 *inlined = 1;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001368 goto cleanup_and_exit;
Tao Ma32f7f222012-12-10 14:06:01 -05001369 }
Tao Mae8e948e2012-12-10 14:06:00 -05001370 }
1371
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001372 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001373 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001374 /*
1375 * "." or ".." will only be in the first block
1376 * NFS may look up ".."; "." should be handled by the VFS
1377 */
1378 block = start = 0;
1379 nblocks = 1;
1380 goto restart;
1381 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001382 if (is_dx(dir)) {
Eric Biggers3a3ac012019-03-20 11:39:13 -07001383 ret = ext4_dx_find_entry(dir, fname, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384 /*
1385 * On success, or if the error was file not found,
1386 * return. Otherwise, fall back to doing a search the
1387 * old fashioned way.
1388 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001389 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1390 goto cleanup_and_exit;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001391 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1392 "falling back\n"));
Eric Sandeene85b9fb2018-07-29 17:13:42 -04001393 ret = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 }
Mingming Cao617ba132006-10-11 01:20:53 -07001395 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Chandan Rajendra32e2ae02017-12-11 15:00:57 -05001396 if (!nblocks) {
1397 ret = NULL;
1398 goto cleanup_and_exit;
1399 }
Mingming Cao617ba132006-10-11 01:20:53 -07001400 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 if (start >= nblocks)
1402 start = 0;
1403 block = start;
1404restart:
1405 do {
1406 /*
1407 * We deal with the read-ahead logic here.
1408 */
Shijie Luoc7071c32020-02-15 03:02:06 -05001409 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001410 if (ra_ptr >= ra_max) {
1411 /* Refill the readahead buffer */
1412 ra_ptr = 0;
1413 b = block;
1414 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1415 /*
1416 * Terminate if we reach the end of the
1417 * directory and must wrap, or if our
1418 * search has finished at this block.
1419 */
1420 if (b >= nblocks || (num && block == start)) {
1421 bh_use[ra_max] = NULL;
1422 break;
1423 }
1424 num++;
Theodore Ts'o10560082014-08-29 20:51:32 -04001425 bh = ext4_getblk(NULL, dir, b++, 0);
Viresh Kumara1c83682015-08-12 15:59:44 +05301426 if (IS_ERR(bh)) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001427 if (ra_max == 0) {
1428 ret = bh;
1429 goto cleanup_and_exit;
1430 }
Theodore Ts'o36de9282014-08-23 17:47:19 -04001431 break;
1432 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 bh_use[ra_max] = bh;
1434 if (bh)
Mike Christiedfec8a12016-06-05 14:31:44 -05001435 ll_rw_block(REQ_OP_READ,
1436 REQ_META | REQ_PRIO,
Christoph Hellwig65299a32011-08-23 14:50:29 +02001437 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 }
1439 }
1440 if ((bh = bh_use[ra_ptr++]) == NULL)
1441 goto next;
1442 wait_on_buffer(bh);
1443 if (!buffer_uptodate(bh)) {
1444 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001445 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1446 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447 brelse(bh);
1448 goto next;
1449 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001450 if (!buffer_verified(bh) &&
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001451 !is_dx_internal_node(dir, block,
1452 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001453 !ext4_dirent_csum_verify(dir,
1454 (struct ext4_dir_entry *)bh->b_data)) {
1455 EXT4_ERROR_INODE(dir, "checksumming directory "
1456 "block %lu", (unsigned long)block);
1457 brelse(bh);
1458 goto next;
1459 }
1460 set_buffer_verified(bh);
Eric Biggers3a3ac012019-03-20 11:39:13 -07001461 i = search_dirblock(bh, dir, fname,
Mingming Cao617ba132006-10-11 01:20:53 -07001462 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001463 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001464 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 ret = bh;
1466 goto cleanup_and_exit;
1467 } else {
1468 brelse(bh);
1469 if (i < 0)
1470 goto cleanup_and_exit;
1471 }
1472 next:
1473 if (++block >= nblocks)
1474 block = 0;
1475 } while (block != start);
1476
1477 /*
1478 * If the directory has grown while we were searching, then
1479 * search the last part of the directory before giving up.
1480 */
1481 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001482 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 if (block < nblocks) {
1484 start = 0;
1485 goto restart;
1486 }
1487
1488cleanup_and_exit:
1489 /* Clean up the read-ahead blocks */
1490 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001491 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 return ret;
1493}
1494
Eric Biggers3a3ac012019-03-20 11:39:13 -07001495static struct buffer_head *ext4_find_entry(struct inode *dir,
1496 const struct qstr *d_name,
1497 struct ext4_dir_entry_2 **res_dir,
1498 int *inlined)
1499{
1500 int err;
1501 struct ext4_filename fname;
1502 struct buffer_head *bh;
1503
1504 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1505 if (err == -ENOENT)
1506 return NULL;
1507 if (err)
1508 return ERR_PTR(err);
1509
1510 bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1511
1512 ext4_fname_free_filename(&fname);
1513 return bh;
1514}
1515
1516static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1517 struct dentry *dentry,
1518 struct ext4_dir_entry_2 **res_dir)
1519{
1520 int err;
1521 struct ext4_filename fname;
1522 struct buffer_head *bh;
1523
1524 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1525 if (err == -ENOENT)
1526 return NULL;
1527 if (err)
1528 return ERR_PTR(err);
1529
1530 bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1531
1532 ext4_fname_free_filename(&fname);
1533 return bh;
1534}
1535
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001536static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1537 struct ext4_filename *fname,
1538 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001540 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001542 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001543 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001544 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545
Chandan Rajendra3ce5fa32018-12-12 15:20:12 +05301546#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001547 *res_dir = NULL;
1548#endif
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001549 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001550 if (IS_ERR(frame))
1551 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 do {
1553 block = dx_get_block(frame->at);
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04001554 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001555 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001557
Eric Biggers14404512017-05-24 18:10:49 -04001558 retval = search_dirblock(bh, dir, fname,
Theodore Ts'o7845c042010-10-27 21:30:08 -04001559 block << EXT4_BLOCK_SIZE_BITS(sb),
1560 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001561 if (retval == 1)
1562 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001563 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001564 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001565 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001566 goto errout;
1567 }
1568
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001569 /* Check to see if we should continue to search */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001570 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 frames, NULL);
1572 if (retval < 0) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04001573 ext4_warning_inode(dir,
1574 "error %d reading directory index block",
1575 retval);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001576 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 goto errout;
1578 }
1579 } while (retval == 1);
1580
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001581 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582errout:
Eric Biggers14404512017-05-24 18:10:49 -04001583 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001584success:
1585 dx_release(frames);
1586 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588
Al Viro00cd8dd2012-06-10 17:13:09 -04001589static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001591 struct inode *inode;
1592 struct ext4_dir_entry_2 *de;
1593 struct buffer_head *bh;
Theodore Ts'o28b4c262016-02-07 19:35:05 -05001594
Eric Biggersb711ad82017-10-18 20:21:58 -04001595 if (dentry->d_name.len > EXT4_NAME_LEN)
1596 return ERR_PTR(-ENAMETOOLONG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597
Eric Biggers3a3ac012019-03-20 11:39:13 -07001598 bh = ext4_lookup_entry(dir, dentry, &de);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001599 if (IS_ERR(bh))
1600 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 inode = NULL;
1602 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001603 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001604 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001605 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001606 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001607 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001608 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001609 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001610 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1611 dentry);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001612 return ERR_PTR(-EFSCORRUPTED);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001613 }
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001614 inode = ext4_iget_normal(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001615 if (inode == ERR_PTR(-ESTALE)) {
1616 EXT4_ERROR_INODE(dir,
1617 "deleted inode referenced: %u",
1618 ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001619 return ERR_PTR(-EFSCORRUPTED);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001620 }
Chandan Rajendrae68f6b82018-12-12 15:20:10 +05301621 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
Theodore Ts'off978b02016-02-08 00:54:26 -05001622 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001623 !fscrypt_has_permitted_context(dir, inode)) {
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001624 ext4_warning(inode->i_sb,
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001625 "Inconsistent encryption contexts: %lu/%lu",
Hyojun Kim63da4202017-10-06 17:10:08 -07001626 dir->i_ino, inode->i_ino);
Eric Biggers68ca0fd2017-02-01 21:07:11 -05001627 iput(inode);
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001628 return ERR_PTR(-EPERM);
1629 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001630 }
1631 return d_splice_alias(inode, dentry);
1632}
1633
1634
Mingming Cao617ba132006-10-11 01:20:53 -07001635struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001636{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001637 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001638 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001639 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 struct buffer_head *bh;
1641
David Howells2b0143b2015-03-17 22:25:59 +00001642 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001643 if (IS_ERR(bh))
1644 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645 if (!bh)
1646 return ERR_PTR(-ENOENT);
1647 ino = le32_to_cpu(de->inode);
1648 brelse(bh);
1649
Al Virofc640052016-04-10 01:33:30 -04001650 if (!ext4_valid_inum(child->d_sb, ino)) {
David Howells2b0143b2015-03-17 22:25:59 +00001651 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001652 "bad parent inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001653 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001654 }
1655
Al Virofc640052016-04-10 01:33:30 -04001656 return d_obtain_alias(ext4_iget_normal(child->d_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657}
1658
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001659/*
1660 * Move count entries from end of map between two memory locations.
1661 * Returns pointer to last entry moved.
1662 */
Mingming Cao617ba132006-10-11 01:20:53 -07001663static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001664dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1665 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666{
1667 unsigned rec_len = 0;
1668
1669 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001670 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001671 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001672 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001674 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001675 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001676 de->inode = 0;
1677 map++;
1678 to += rec_len;
1679 }
Mingming Cao617ba132006-10-11 01:20:53 -07001680 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681}
1682
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001683/*
1684 * Compact each dir entry in the range to the minimal rec_len.
1685 * Returns pointer to last entry in range.
1686 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001687static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688{
Mingming Cao617ba132006-10-11 01:20:53 -07001689 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 unsigned rec_len = 0;
1691
1692 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001693 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001694 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001696 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697 if (de > to)
1698 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001699 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001701 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702 }
1703 de = next;
1704 }
1705 return prev;
1706}
1707
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001708/*
1709 * Split a full leaf block to make room for a new dir entry.
1710 * Allocate a new block, and move entries so that they are approx. equally full.
1711 * Returns pointer to de in block into which the new entry will be inserted.
1712 */
Mingming Cao617ba132006-10-11 01:20:53 -07001713static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001715 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716{
1717 unsigned blocksize = dir->i_sb->s_blocksize;
1718 unsigned count, continued;
1719 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001720 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001721 u32 hash2;
1722 struct dx_map_entry *map;
1723 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001724 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001725 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001726 struct ext4_dir_entry_tail *t;
1727 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001728 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001730 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001731 csum_size = sizeof(struct ext4_dir_entry_tail);
1732
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001733 bh2 = ext4_append(handle, dir, &newblock);
1734 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001735 brelse(*bh);
1736 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001737 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738 }
1739
1740 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001741 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001742 if (err)
1743 goto journal_error;
1744
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001746 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747 if (err)
1748 goto journal_error;
1749
1750 data2 = bh2->b_data;
1751
1752 /* create map in the end of data2 block */
1753 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001754 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 blocksize, hinfo, map);
1756 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001757 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001758 /* Split the existing block in the middle, size-wise */
1759 size = 0;
1760 move = 0;
1761 for (i = count-1; i >= 0; i--) {
1762 /* is more than half of this entry in 2nd half of the block? */
1763 if (size + map[i].size/2 > blocksize/2)
1764 break;
1765 size += map[i].size;
1766 move++;
1767 }
1768 /* map index at which we will split */
1769 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001770 hash2 = map[split].hash;
1771 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001772 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1773 (unsigned long)dx_get_block(frame->at),
1774 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775
1776 /* Fancy dance to stay within two buffers */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001777 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1778 blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001779 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001780 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1781 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001782 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001783 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1784 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001785 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001786 if (csum_size) {
1787 t = EXT4_DIRENT_TAIL(data2, blocksize);
1788 initialize_dirent_tail(t, blocksize);
1789
1790 t = EXT4_DIRENT_TAIL(data1, blocksize);
1791 initialize_dirent_tail(t, blocksize);
1792 }
1793
Michael Halcrowb3098482015-04-12 01:07:01 -04001794 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1795 blocksize, 1));
1796 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1797 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798
1799 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001800 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 swap(*bh, bh2);
1802 de = de2;
1803 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001804 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001805 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806 if (err)
1807 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001808 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001809 if (err)
1810 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001811 brelse(bh2);
1812 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001814
1815journal_error:
1816 brelse(*bh);
1817 brelse(bh2);
1818 *bh = NULL;
1819 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001820 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001822
Tao Ma978fef92012-12-10 14:05:58 -05001823int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1824 struct buffer_head *bh,
1825 void *buf, int buf_size,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001826 struct ext4_filename *fname,
Tao Ma978fef92012-12-10 14:05:58 -05001827 struct ext4_dir_entry_2 **dest_de)
1828{
1829 struct ext4_dir_entry_2 *de;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001830 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001831 int nlen, rlen;
1832 unsigned int offset = 0;
1833 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001834
Tao Ma978fef92012-12-10 14:05:58 -05001835 de = (struct ext4_dir_entry_2 *)buf;
1836 top = buf + buf_size - reclen;
1837 while ((char *) de <= top) {
1838 if (ext4_check_dir_entry(dir, NULL, de, bh,
Hyojun Kim63da4202017-10-06 17:10:08 -07001839 buf, buf_size, offset))
1840 return -EFSCORRUPTED;
1841 if (ext4_match(fname, de))
1842 return -EEXIST;
Tao Ma978fef92012-12-10 14:05:58 -05001843 nlen = EXT4_DIR_REC_LEN(de->name_len);
1844 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1845 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1846 break;
1847 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1848 offset += rlen;
1849 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001850 if ((char *) de > top)
Hyojun Kim63da4202017-10-06 17:10:08 -07001851 return -ENOSPC;
1852
1853 *dest_de = de;
1854 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001855}
1856
Hyojun Kim63da4202017-10-06 17:10:08 -07001857void ext4_insert_dentry(struct inode *inode,
1858 struct ext4_dir_entry_2 *de,
1859 int buf_size,
1860 struct ext4_filename *fname)
Tao Ma978fef92012-12-10 14:05:58 -05001861{
1862
1863 int nlen, rlen;
1864
1865 nlen = EXT4_DIR_REC_LEN(de->name_len);
1866 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1867 if (de->inode) {
1868 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001869 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001870 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1871 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1872 de = de1;
1873 }
1874 de->file_type = EXT4_FT_UNKNOWN;
1875 de->inode = cpu_to_le32(inode->i_ino);
1876 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001877 de->name_len = fname_len(fname);
1878 memcpy(de->name, fname_name(fname), fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001879}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001880
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881/*
1882 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1883 * it points to a directory entry which is guaranteed to be large
1884 * enough for new directory entry. If de is NULL, then
1885 * add_dirent_to_buf will attempt search the directory block for
1886 * space. It will return -ENOSPC if no space is available, and -EIO
1887 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001888 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001889static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
1890 struct inode *dir,
Mingming Cao617ba132006-10-11 01:20:53 -07001891 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001892 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001893{
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001894 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001895 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001896 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001897
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001898 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001899 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001901 if (!de) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001902 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
1903 blocksize - csum_size, fname, &de);
Tao Ma978fef92012-12-10 14:05:58 -05001904 if (err)
1905 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906 }
1907 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001908 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001910 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911 return err;
1912 }
1913
Hyojun Kim63da4202017-10-06 17:10:08 -07001914 /* By now the buffer is marked for journaling */
1915 ext4_insert_dentry(inode, de, blocksize, fname);
Tao Ma978fef92012-12-10 14:05:58 -05001916
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001917 /*
1918 * XXX shouldn't update any times until successful
1919 * completion of syscall, but too many callers depend
1920 * on this.
1921 *
1922 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001923 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001924 * recovery deletes the inode, so the worst that can
1925 * happen is that the times are slightly out of date
1926 * and/or different from the directory change time.
1927 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001928 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001929 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001931 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001932 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001933 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001934 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001935 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936 return 0;
1937}
1938
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001939/*
1940 * This converts a one block unindexed directory to a 3 block indexed
1941 * directory, and adds the dentry to the indexed directory.
1942 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001943static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001944 struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001945 struct inode *inode, struct buffer_head *bh)
1946{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001947 struct buffer_head *bh2;
1948 struct dx_root *root;
1949 struct dx_frame frames[2], *frame;
1950 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001951 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001952 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001953 char *data1, *top;
1954 unsigned len;
1955 int retval;
1956 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001957 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001958 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001959 int csum_size = 0;
1960
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001961 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001962 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963
1964 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001965 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04001966 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001967 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001969 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001970 brelse(bh);
1971 return retval;
1972 }
1973 root = (struct dx_root *) bh->b_data;
1974
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001975 /* The 0th block becomes the root, move the dirents out */
1976 fde = &root->dotdot;
1977 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001978 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001979 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001980 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001981 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001982 return -EFSCORRUPTED;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001983 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001984 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001985
1986 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001987 bh2 = ext4_append(handle, dir, &block);
1988 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001990 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001992 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 data1 = bh2->b_data;
1994
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001996 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001998 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002000 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
2001 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002002 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002003
2004 if (csum_size) {
2005 t = EXT4_DIRENT_TAIL(data1, blocksize);
2006 initialize_dirent_tail(t, blocksize);
2007 }
2008
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07002010 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002011 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2012 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013 memset (&root->info, 0, sizeof(root->info));
2014 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07002015 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002017 dx_set_block(entries, 1);
2018 dx_set_count(entries, 1);
2019 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020
2021 /* Initialize as for dx_probe */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002022 fname->hinfo.hash_version = root->info.hash_version;
2023 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2024 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2025 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
2026 ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
2027
Jan Kara6050d472014-10-30 10:53:17 -04002028 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029 frame = frames;
2030 frame->entries = entries;
2031 frame->at = entries;
2032 frame->bh = bh;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002033
Jan Kara6050d472014-10-30 10:53:17 -04002034 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2035 if (retval)
2036 goto out_frames;
gmaile81d4472016-09-30 01:33:37 -04002037 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002038 if (retval)
2039 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002040
gmaile81d4472016-09-30 01:33:37 -04002041 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002042 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002043 retval = PTR_ERR(de);
2044 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002045 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002046
gmaile81d4472016-09-30 01:33:37 -04002047 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002048out_frames:
2049 /*
2050 * Even if the block split failed, we have to properly write
2051 * out all the changes we did so far. Otherwise we can end up
2052 * with corrupted filesystem.
2053 */
gmaile81d4472016-09-30 01:33:37 -04002054 if (retval)
2055 ext4_mark_inode_dirty(handle, dir);
Jan Kara6050d472014-10-30 10:53:17 -04002056 dx_release(frames);
gmaile81d4472016-09-30 01:33:37 -04002057 brelse(bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002058 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060
2061/*
Mingming Cao617ba132006-10-11 01:20:53 -07002062 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002063 *
2064 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002065 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066 *
2067 * NOTE!! The inode part of 'de' is left at 0 - which means you
2068 * may not sleep between calling this and putting something into
2069 * the entry, as someone else might have used it while you slept.
2070 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002071static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2072 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002073{
David Howells2b0143b2015-03-17 22:25:59 +00002074 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002075 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002076 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002077 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002078 struct super_block *sb;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002079 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002083 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002084 int csum_size = 0;
2085
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002086 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002087 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088
2089 sb = dir->i_sb;
2090 blocksize = sb->s_blocksize;
2091 if (!dentry->d_name.len)
2092 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002093
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002094 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2095 if (retval)
2096 return retval;
2097
Tao Ma3c47d542012-12-10 14:05:59 -05002098 if (ext4_has_inline_data(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002099 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05002100 if (retval < 0)
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002101 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002102 if (retval == 1) {
2103 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002104 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002105 }
2106 }
2107
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 if (is_dx(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002109 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002111 goto out;
Jan Kara3f3beb22020-02-10 15:43:16 +01002112 /* Can we just ignore htree data? */
2113 if (ext4_has_metadata_csum(sb)) {
2114 EXT4_ERROR_INODE(dir,
2115 "Directory has corrupted htree index.");
2116 retval = -EFSCORRUPTED;
2117 goto out;
2118 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002119 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002121 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002123 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002124 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002125 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04002126 if (bh == NULL) {
2127 bh = ext4_bread(handle, dir, block,
2128 EXT4_GET_BLOCKS_CREATE);
2129 goto add_to_new_block;
2130 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002131 if (IS_ERR(bh)) {
2132 retval = PTR_ERR(bh);
2133 bh = NULL;
2134 goto out;
2135 }
2136 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2137 NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002138 if (retval != -ENOSPC)
2139 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002141 if (blocks == 1 && !dx_fallback &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002142 ext4_has_feature_dir_index(sb)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002143 retval = make_indexed_dir(handle, &fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002144 inode, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002145 bh = NULL; /* make_indexed_dir releases bh */
2146 goto out;
2147 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 brelse(bh);
2149 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002150 bh = ext4_append(handle, dir, &block);
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04002151add_to_new_block:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002152 if (IS_ERR(bh)) {
2153 retval = PTR_ERR(bh);
2154 bh = NULL;
2155 goto out;
2156 }
Mingming Cao617ba132006-10-11 01:20:53 -07002157 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002159 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2160
2161 if (csum_size) {
2162 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2163 initialize_dirent_tail(t, blocksize);
2164 }
2165
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002166 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002167out:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002168 ext4_fname_free_filename(&fname);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002169 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002170 if (retval == 0)
2171 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002172 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002173}
2174
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002175/*
2176 * Returns 0 for success, or a negative error value
2177 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002178static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002179 struct inode *dir, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180{
2181 struct dx_frame frames[2], *frame;
2182 struct dx_entry *entries, *at;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002183 struct buffer_head *bh;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002184 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002185 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186 int err;
2187
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002188 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002189 if (IS_ERR(frame))
2190 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002191 entries = frame->entries;
2192 at = frame->at;
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04002193 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002194 if (IS_ERR(bh)) {
2195 err = PTR_ERR(bh);
2196 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002198 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199
2200 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002201 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002202 if (err)
2203 goto journal_error;
2204
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002205 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002206 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002207 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002208
2209 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002210 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211 dx_get_count(entries), dx_get_limit(entries)));
2212 /* Need to split index? */
2213 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002214 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215 unsigned icount = dx_get_count(entries);
2216 int levels = frame - frames;
2217 struct dx_entry *entries2;
2218 struct dx_node *node2;
2219 struct buffer_head *bh2;
2220
2221 if (levels && (dx_get_count(frames->entries) ==
2222 dx_get_limit(frames->entries))) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04002223 ext4_warning_inode(dir, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224 err = -ENOSPC;
2225 goto cleanup;
2226 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002227 bh2 = ext4_append(handle, dir, &newblock);
2228 if (IS_ERR(bh2)) {
2229 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002231 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 node2 = (struct dx_node *)(bh2->b_data);
2233 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002234 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002235 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2236 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002237 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002238 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 if (err)
2240 goto journal_error;
2241 if (levels) {
2242 unsigned icount1 = icount/2, icount2 = icount - icount1;
2243 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002244 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2245 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246
2247 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002248 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 frames[0].bh);
2250 if (err)
2251 goto journal_error;
2252
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002253 memcpy((char *) entries2, (char *) (entries + icount1),
2254 icount2 * sizeof(struct dx_entry));
2255 dx_set_count(entries, icount1);
2256 dx_set_count(entries2, icount2);
2257 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258
2259 /* Which index block gets the new entry? */
2260 if (at - entries >= icount1) {
2261 frame->at = at = at - entries - icount1 + entries2;
2262 frame->entries = entries = entries2;
2263 swap(frame->bh, bh2);
2264 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002265 dx_insert_block(frames + 0, hash2, newblock);
2266 dxtrace(dx_show_index("node", frames[1].entries));
2267 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002269 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002270 if (err)
2271 goto journal_error;
2272 brelse (bh2);
2273 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002274 dxtrace(printk(KERN_DEBUG
2275 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276 memcpy((char *) entries2, (char *) entries,
2277 icount * sizeof(struct dx_entry));
2278 dx_set_limit(entries2, dx_node_limit(dir));
2279
2280 /* Set up root */
2281 dx_set_count(entries, 1);
2282 dx_set_block(entries + 0, newblock);
2283 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2284
2285 /* Add new access path frame */
2286 frame = frames + 1;
2287 frame->at = at = at - entries + entries2;
2288 frame->entries = entries = entries2;
2289 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002290 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002291 frame->bh);
2292 if (err)
2293 goto journal_error;
2294 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002295 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002296 if (err) {
2297 ext4_std_error(inode->i_sb, err);
2298 goto cleanup;
2299 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002301 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002302 if (IS_ERR(de)) {
2303 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002305 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002306 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307 goto cleanup;
2308
2309journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002310 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002311cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002312 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313 dx_release(frames);
2314 return err;
2315}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002316
2317/*
Tao Ma05019a92012-12-10 14:06:00 -05002318 * ext4_generic_delete_entry deletes a directory entry by merging it
2319 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320 */
Tao Ma05019a92012-12-10 14:06:00 -05002321int ext4_generic_delete_entry(handle_t *handle,
2322 struct inode *dir,
2323 struct ext4_dir_entry_2 *de_del,
2324 struct buffer_head *bh,
2325 void *entry_buf,
2326 int buf_size,
2327 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002329 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002330 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002331 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002332
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002333 i = 0;
2334 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002335 de = (struct ext4_dir_entry_2 *)entry_buf;
2336 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002337 if (ext4_check_dir_entry(dir, NULL, de, bh,
2338 bh->b_data, bh->b_size, i))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002339 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002340 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002341 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002342 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002343 ext4_rec_len_from_disk(pde->rec_len,
2344 blocksize) +
2345 ext4_rec_len_from_disk(de->rec_len,
2346 blocksize),
2347 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002348 else
2349 de->inode = 0;
2350 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351 return 0;
2352 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002353 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002354 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002355 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356 }
2357 return -ENOENT;
2358}
2359
Tao Ma05019a92012-12-10 14:06:00 -05002360static int ext4_delete_entry(handle_t *handle,
2361 struct inode *dir,
2362 struct ext4_dir_entry_2 *de_del,
2363 struct buffer_head *bh)
2364{
2365 int err, csum_size = 0;
2366
Tao Ma9f40fe52012-12-10 14:06:00 -05002367 if (ext4_has_inline_data(dir)) {
2368 int has_inline_data = 1;
2369 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2370 &has_inline_data);
2371 if (has_inline_data)
2372 return err;
2373 }
2374
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002375 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002376 csum_size = sizeof(struct ext4_dir_entry_tail);
2377
2378 BUFFER_TRACE(bh, "get_write_access");
2379 err = ext4_journal_get_write_access(handle, bh);
2380 if (unlikely(err))
2381 goto out;
2382
2383 err = ext4_generic_delete_entry(handle, dir, de_del,
2384 bh, bh->b_data,
2385 dir->i_sb->s_blocksize, csum_size);
2386 if (err)
2387 goto out;
2388
2389 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2390 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2391 if (unlikely(err))
2392 goto out;
2393
2394 return 0;
2395out:
2396 if (err != -ENOENT)
2397 ext4_std_error(dir->i_sb, err);
2398 return err;
2399}
2400
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002401/*
2402 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2403 * since this indicates that nlinks count was previously 1.
2404 */
2405static void ext4_inc_count(handle_t *handle, struct inode *inode)
2406{
2407 inc_nlink(inode);
2408 if (is_dx(inode) && inode->i_nlink > 1) {
2409 /* limit is 16-bit i_links_count */
2410 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002411 set_nlink(inode, 1);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002412 ext4_set_feature_dir_nlink(inode->i_sb);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002413 }
2414 }
2415}
2416
2417/*
2418 * If a directory had nlink == 1, then we should let it be 1. This indicates
2419 * directory has >EXT4_LINK_MAX subdirs.
2420 */
2421static void ext4_dec_count(handle_t *handle, struct inode *inode)
2422{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002423 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2424 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002425}
2426
2427
Mingming Cao617ba132006-10-11 01:20:53 -07002428static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429 struct dentry *dentry, struct inode *inode)
2430{
Mingming Cao617ba132006-10-11 01:20:53 -07002431 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002432 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002433 ext4_mark_inode_dirty(handle, inode);
Al Viro2d2d3f12018-05-04 08:23:01 -04002434 d_instantiate_new(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002435 return 0;
2436 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002437 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002438 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 iput(inode);
2440 return err;
2441}
2442
2443/*
2444 * By the time this is called, we already have created
2445 * the directory cache entry for the new file, but it
2446 * is so far negative - it has no inode.
2447 *
2448 * If the create succeeds, we fill in the inode information
2449 * with d_instantiate().
2450 */
Al Viro4acdaf22011-07-26 01:42:34 -04002451static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002452 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453{
2454 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002455 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002456 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457
Jan Karaa7cdade2015-06-29 16:22:54 +02002458 err = dquot_initialize(dir);
2459 if (err)
2460 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002461
Theodore Ts'o11395752013-02-09 16:27:09 -05002462 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002463 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002465 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2466 NULL, EXT4_HT_DIR, credits);
2467 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 err = PTR_ERR(inode);
2469 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002470 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002471 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002472 ext4_set_aops(inode);
Theodore Ts'oe709e9d2015-05-31 13:35:02 -04002473 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002474 if (!err && IS_DIRSYNC(dir))
2475 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002477 if (handle)
2478 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002479 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 goto retry;
2481 return err;
2482}
2483
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002484static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002485 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002486{
2487 handle_t *handle;
2488 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002489 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490
Jan Karaa7cdade2015-06-29 16:22:54 +02002491 err = dquot_initialize(dir);
2492 if (err)
2493 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002494
Theodore Ts'o11395752013-02-09 16:27:09 -05002495 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002496 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002498 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2499 NULL, EXT4_HT_DIR, credits);
2500 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002501 err = PTR_ERR(inode);
2502 if (!IS_ERR(inode)) {
2503 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002504 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002505 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002506 if (!err && IS_DIRSYNC(dir))
2507 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002508 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002509 if (handle)
2510 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002511 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002512 goto retry;
2513 return err;
2514}
2515
Al Viroaf51a2a2013-06-29 13:23:08 +04002516static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2517{
2518 handle_t *handle;
2519 struct inode *inode;
2520 int err, retries = 0;
2521
Jan Karaa7cdade2015-06-29 16:22:54 +02002522 err = dquot_initialize(dir);
2523 if (err)
2524 return err;
Al Viroaf51a2a2013-06-29 13:23:08 +04002525
2526retry:
2527 inode = ext4_new_inode_start_handle(dir, mode,
2528 NULL, 0, NULL,
2529 EXT4_HT_DIR,
2530 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2531 4 + EXT4_XATTR_TRANS_BLOCKS);
2532 handle = ext4_journal_current_handle();
2533 err = PTR_ERR(inode);
2534 if (!IS_ERR(inode)) {
2535 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002536 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002537 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002538 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002539 err = ext4_orphan_add(handle, inode);
2540 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002541 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002542 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002543 unlock_new_inode(inode);
2544 }
2545 if (handle)
2546 ext4_journal_stop(handle);
2547 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2548 goto retry;
2549 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002550err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002551 ext4_journal_stop(handle);
2552 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002553 return err;
2554}
2555
Tao Maa774f9c2012-12-10 14:05:57 -05002556struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2557 struct ext4_dir_entry_2 *de,
2558 int blocksize, int csum_size,
2559 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560{
Tao Maa774f9c2012-12-10 14:05:57 -05002561 de->inode = cpu_to_le32(inode->i_ino);
2562 de->name_len = 1;
2563 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2564 blocksize);
2565 strcpy(de->name, ".");
2566 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2567
2568 de = ext4_next_entry(de, blocksize);
2569 de->inode = cpu_to_le32(parent_ino);
2570 de->name_len = 2;
2571 if (!dotdot_real_len)
2572 de->rec_len = ext4_rec_len_to_disk(blocksize -
2573 (csum_size + EXT4_DIR_REC_LEN(1)),
2574 blocksize);
2575 else
2576 de->rec_len = ext4_rec_len_to_disk(
2577 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2578 strcpy(de->name, "..");
2579 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2580
2581 return ext4_next_entry(de, blocksize);
2582}
2583
2584static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2585 struct inode *inode)
2586{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002587 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002588 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002589 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002590 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002591 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002592 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002593 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002595 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002596 csum_size = sizeof(struct ext4_dir_entry_tail);
2597
Tao Ma3c47d542012-12-10 14:05:59 -05002598 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2599 err = ext4_try_create_inline_dir(handle, dir, inode);
2600 if (err < 0 && err != -ENOSPC)
2601 goto out;
2602 if (!err)
2603 goto out;
2604 }
2605
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002606 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002607 dir_block = ext4_append(handle, inode, &block);
2608 if (IS_ERR(dir_block))
2609 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002610 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2611 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2612 set_nlink(inode, 2);
2613 if (csum_size) {
2614 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2615 initialize_dirent_tail(t, blocksize);
2616 }
2617
2618 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2619 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2620 if (err)
2621 goto out;
2622 set_buffer_verified(dir_block);
2623out:
2624 brelse(dir_block);
2625 return err;
2626}
2627
2628static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2629{
2630 handle_t *handle;
2631 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002632 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002633
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002634 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 return -EMLINK;
2636
Jan Karaa7cdade2015-06-29 16:22:54 +02002637 err = dquot_initialize(dir);
2638 if (err)
2639 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002640
Theodore Ts'o11395752013-02-09 16:27:09 -05002641 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002642 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002643retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002644 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2645 &dentry->d_name,
2646 0, NULL, EXT4_HT_DIR, credits);
2647 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002648 err = PTR_ERR(inode);
2649 if (IS_ERR(inode))
2650 goto out_stop;
2651
Mingming Cao617ba132006-10-11 01:20:53 -07002652 inode->i_op = &ext4_dir_inode_operations;
2653 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002654 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002655 if (err)
2656 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002657 err = ext4_mark_inode_dirty(handle, inode);
2658 if (!err)
2659 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002660 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002661out_clear_inode:
2662 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002663 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002664 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002665 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002666 goto out_stop;
2667 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002668 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002669 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002670 err = ext4_mark_inode_dirty(handle, dir);
2671 if (err)
2672 goto out_clear_inode;
Al Viro2d2d3f12018-05-04 08:23:01 -04002673 d_instantiate_new(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002674 if (IS_DIRSYNC(dir))
2675 ext4_handle_sync(handle);
2676
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002678 if (handle)
2679 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002680 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 goto retry;
2682 return err;
2683}
2684
2685/*
2686 * routine to check that the specified directory is empty (for rmdir)
2687 */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002688bool ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002690 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002691 struct buffer_head *bh;
Jan Karadfcbd4072019-12-02 18:02:12 +01002692 struct ext4_dir_entry_2 *de;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002693 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002694
Tao Ma61f86632012-12-10 14:06:01 -05002695 if (ext4_has_inline_data(inode)) {
2696 int has_inline_data = 1;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002697 int ret;
Tao Ma61f86632012-12-10 14:06:01 -05002698
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002699 ret = empty_inline_dir(inode, &has_inline_data);
Tao Ma61f86632012-12-10 14:06:01 -05002700 if (has_inline_data)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002701 return ret;
Tao Ma61f86632012-12-10 14:06:01 -05002702 }
2703
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002704 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002705 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2706 EXT4_ERROR_INODE(inode, "invalid size");
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002707 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002708 }
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04002709 /* The first directory block must not be a hole,
2710 * so treat it as DIRENT_HTREE
2711 */
2712 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002713 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002714 return true;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002715
Mingming Cao617ba132006-10-11 01:20:53 -07002716 de = (struct ext4_dir_entry_2 *) bh->b_data;
Jan Karadfcbd4072019-12-02 18:02:12 +01002717 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2718 0) ||
2719 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
2720 ext4_warning_inode(inode, "directory missing '.'");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002721 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002722 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002723 }
Jan Karadfcbd4072019-12-02 18:02:12 +01002724 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2725 de = ext4_next_entry(de, sb->s_blocksize);
2726 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
2727 offset) ||
2728 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
2729 ext4_warning_inode(inode, "directory missing '..'");
2730 brelse(bh);
2731 return true;
2732 }
2733 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002734 while (offset < inode->i_size) {
Jan Karadfcbd4072019-12-02 18:02:12 +01002735 if (!(offset & (sb->s_blocksize - 1))) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002736 unsigned int lblock;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002737 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002738 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002739 bh = ext4_read_dirblock(inode, lblock, EITHER);
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04002740 if (bh == NULL) {
2741 offset += sb->s_blocksize;
2742 continue;
2743 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002744 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002745 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002746 }
Jan Karadfcbd4072019-12-02 18:02:12 +01002747 de = (struct ext4_dir_entry_2 *) (bh->b_data +
2748 (offset & (sb->s_blocksize - 1)));
Tao Ma226ba972012-12-10 14:05:58 -05002749 if (ext4_check_dir_entry(inode, NULL, de, bh,
2750 bh->b_data, bh->b_size, offset)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751 offset = (offset | (sb->s_blocksize - 1)) + 1;
2752 continue;
2753 }
2754 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002755 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002756 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002758 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002759 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002760 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002761 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002762}
2763
Jan Karad745a8c2014-05-26 11:56:53 -04002764/*
2765 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002766 * such inodes, starting at the superblock, in case we crash before the
2767 * file is closed/deleted, or in case the inode truncate spans multiple
2768 * transactions and the last transaction is not recovered after a crash.
2769 *
2770 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002771 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002772 *
2773 * Orphan list manipulation functions must be called under i_mutex unless
2774 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775 */
Mingming Cao617ba132006-10-11 01:20:53 -07002776int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777{
2778 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002779 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002780 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002781 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002782 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002784 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002785 return 0;
2786
Jan Karad745a8c2014-05-26 11:56:53 -04002787 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002788 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002789 /*
2790 * Exit early if inode already is on orphan list. This is a big speedup
2791 * since we don't have to contend on the global s_orphan_lock.
2792 */
Mingming Cao617ba132006-10-11 01:20:53 -07002793 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002794 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795
Lukas Czernerafb86172011-07-11 18:47:04 -04002796 /*
2797 * Orphan handling is only valid for files with data blocks
2798 * being truncated, or files being unlinked. Note that we either
2799 * hold i_mutex, or the inode can not be referenced from outside,
2800 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002801 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002802 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2803 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002804
Jan Karacd2c0802014-05-26 11:39:17 -04002805 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2806 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002807 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002808 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809
Mingming Cao617ba132006-10-11 01:20:53 -07002810 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002811 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002812 goto out;
2813
2814 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002815 /*
2816 * Due to previous errors inode may be already a part of on-disk
2817 * orphan list. If so skip on-disk list modification.
2818 */
Jan Karad745a8c2014-05-26 11:56:53 -04002819 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2820 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2821 /* Insert this inode at the head of the on-disk orphan list */
2822 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2823 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2824 dirty = true;
2825 }
2826 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2827 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828
Jan Karad745a8c2014-05-26 11:56:53 -04002829 if (dirty) {
2830 err = ext4_handle_dirty_super(handle, sb);
2831 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2832 if (!err)
2833 err = rc;
2834 if (err) {
2835 /*
2836 * We have to remove inode from in-memory list if
2837 * addition to on disk orphan list failed. Stray orphan
2838 * list entries can cause panics at unmount time.
2839 */
2840 mutex_lock(&sbi->s_orphan_lock);
Jan Kara74177f552016-05-05 11:10:15 -04002841 list_del_init(&EXT4_I(inode)->i_orphan);
Jan Karad745a8c2014-05-26 11:56:53 -04002842 mutex_unlock(&sbi->s_orphan_lock);
2843 }
Vasily Averinf30a52c2018-11-06 17:01:36 -05002844 } else
2845 brelse(iloc.bh);
2846
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2848 jbd_debug(4, "orphan inode %lu will point to %d\n",
2849 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002850out:
Jan Karacd2c0802014-05-26 11:39:17 -04002851 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002852 return err;
2853}
2854
2855/*
Mingming Cao617ba132006-10-11 01:20:53 -07002856 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 * of such inodes stored on disk, because it is finally being cleaned up.
2858 */
Mingming Cao617ba132006-10-11 01:20:53 -07002859int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860{
2861 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002862 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002863 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002864 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002865 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866 int err = 0;
2867
Jan Karacd2c0802014-05-26 11:39:17 -04002868 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002869 return 0;
2870
Jan Karad745a8c2014-05-26 11:56:53 -04002871 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002872 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002873 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002874 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002875 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876
Jan Karad745a8c2014-05-26 11:56:53 -04002877 if (handle) {
2878 /* Grab inode buffer early before taking global s_orphan_lock */
2879 err = ext4_reserve_inode_write(handle, inode, &iloc);
2880 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881
Jan Karad745a8c2014-05-26 11:56:53 -04002882 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2884
Jan Karad745a8c2014-05-26 11:56:53 -04002885 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002886 list_del_init(&ei->i_orphan);
2887
2888 /* If we're on an error path, we may not have a valid
2889 * transaction handle with which to update the orphan list on
2890 * disk, but we still need to remove the inode from the linked
2891 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002892 if (!handle || err) {
2893 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002895 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896
Jan Karad745a8c2014-05-26 11:56:53 -04002897 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002898 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002899 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002901 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04002902 if (err) {
2903 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002904 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002905 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04002907 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002908 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002910 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002912 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002913
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002914 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002916 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002917 if (err) {
2918 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002919 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002920 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002921 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002922 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002923 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002924 }
2925 if (err)
2926 goto out_brelse;
2927 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002928 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002929out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002930 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931 return err;
2932
2933out_brelse:
2934 brelse(iloc.bh);
2935 goto out_err;
2936}
2937
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002938static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939{
2940 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002941 struct inode *inode;
2942 struct buffer_head *bh;
2943 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002944 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945
2946 /* Initialize quotas before so that eventual writes go in
2947 * separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02002948 retval = dquot_initialize(dir);
2949 if (retval)
2950 return retval;
2951 retval = dquot_initialize(d_inode(dentry));
2952 if (retval)
2953 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002954
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002956 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002957 if (IS_ERR(bh))
2958 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959 if (!bh)
2960 goto end_rmdir;
2961
David Howells2b0143b2015-03-17 22:25:59 +00002962 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002963
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002964 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002965 if (le32_to_cpu(de->inode) != inode->i_ino)
2966 goto end_rmdir;
2967
2968 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002969 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002970 goto end_rmdir;
2971
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002972 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002973 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002974 if (IS_ERR(handle)) {
2975 retval = PTR_ERR(handle);
2976 handle = NULL;
2977 goto end_rmdir;
2978 }
2979
2980 if (IS_DIRSYNC(dir))
2981 ext4_handle_sync(handle);
2982
Mingming Cao617ba132006-10-11 01:20:53 -07002983 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984 if (retval)
2985 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002986 if (!EXT4_DIR_LINK_EMPTY(inode))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04002987 ext4_warning_inode(inode,
2988 "empty directory '%.*s' has too many links (%u)",
2989 dentry->d_name.len, dentry->d_name.name,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002990 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002991 inode->i_version++;
2992 clear_nlink(inode);
2993 /* There's no need to set i_disksize: the fact that i_nlink is
2994 * zero will ensure that the right thing happens during any
2995 * recovery. */
2996 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002997 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002998 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002999 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003000 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003001 ext4_update_dx_flag(dir);
3002 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003
3004end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003005 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05003006 if (handle)
3007 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008 return retval;
3009}
3010
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003011static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012{
3013 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003014 struct inode *inode;
3015 struct buffer_head *bh;
3016 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05003017 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003018
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003019 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003020 /* Initialize quotas before so that eventual writes go
3021 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003022 retval = dquot_initialize(dir);
3023 if (retval)
3024 return retval;
3025 retval = dquot_initialize(d_inode(dentry));
3026 if (retval)
3027 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003028
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003030 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003031 if (IS_ERR(bh))
3032 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003033 if (!bh)
3034 goto end_unlink;
3035
David Howells2b0143b2015-03-17 22:25:59 +00003036 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003037
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003038 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039 if (le32_to_cpu(de->inode) != inode->i_ino)
3040 goto end_unlink;
3041
Theodore Ts'o931b6862013-02-09 09:43:39 -05003042 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003043 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05003044 if (IS_ERR(handle)) {
3045 retval = PTR_ERR(handle);
3046 handle = NULL;
3047 goto end_unlink;
3048 }
3049
3050 if (IS_DIRSYNC(dir))
3051 ext4_handle_sync(handle);
3052
Mingming Cao617ba132006-10-11 01:20:53 -07003053 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054 if (retval)
3055 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04003056 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003057 ext4_update_dx_flag(dir);
3058 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'oe1513b32019-11-11 22:18:13 -05003059 if (inode->i_nlink == 0)
3060 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3061 dentry->d_name.len, dentry->d_name.name);
3062 else
3063 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003064 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003065 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003066 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003067 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068
3069end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003070 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003071 if (handle)
3072 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003073 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 return retval;
3075}
3076
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003077static int ext4_symlink(struct inode *dir,
3078 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079{
3080 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003081 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003082 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003083 int credits;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003084 struct fscrypt_str disk_link;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003086 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3087 &disk_link);
3088 if (err)
3089 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090
Jan Karaa7cdade2015-06-29 16:22:54 +02003091 err = dquot_initialize(dir);
3092 if (err)
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003093 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003094
Theodore Ts'of348c252015-04-16 01:55:00 -04003095 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003096 /*
3097 * For non-fast symlinks, we just allocate inode and put it on
3098 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003099 * group descriptor, sb, inode block, quota blocks, and
3100 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003101 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003102 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3103 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003104 } else {
3105 /*
3106 * Fast symlink. We have to add entry to directory
3107 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3108 * allocate new inode (bitmap, group descriptor, inode block,
3109 * quota blocks, sb is already counted in previous macros).
3110 */
3111 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003112 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003113 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003114
Theodore Ts'o11395752013-02-09 16:27:09 -05003115 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3116 &dentry->d_name, 0, NULL,
3117 EXT4_HT_DIR, credits);
3118 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003119 if (IS_ERR(inode)) {
3120 if (handle)
3121 ext4_journal_stop(handle);
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003122 return PTR_ERR(inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003123 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003125 if (IS_ENCRYPTED(inode)) {
3126 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04003127 if (err)
Theodore Ts'of348c252015-04-16 01:55:00 -04003128 goto err_drop_inode;
Al Viroa7a67e82015-04-27 17:51:30 -04003129 inode->i_op = &ext4_encrypted_symlink_inode_operations;
Theodore Ts'of348c252015-04-16 01:55:00 -04003130 }
3131
3132 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003133 if (!IS_ENCRYPTED(inode))
Al Viroa7a67e82015-04-27 17:51:30 -04003134 inode->i_op = &ext4_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003135 inode_nohighmem(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003136 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003138 * We cannot call page_symlink() with transaction started
3139 * because it calls into ext4_write_begin() which can wait
3140 * for transaction commit if we are running out of space
3141 * and thus we deadlock. So we have to stop transaction now
3142 * and restart it when symlink contents is written.
3143 *
3144 * To keep fs consistent in case of crash, we have to put inode
3145 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003146 */
Jan Karadf5e6222011-05-03 11:12:58 -04003147 drop_nlink(inode);
3148 err = ext4_orphan_add(handle, inode);
3149 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003150 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003151 if (err)
3152 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003153 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003154 if (err)
3155 goto err_drop_inode;
3156 /*
3157 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3158 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3159 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003160 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003161 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3162 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3163 if (IS_ERR(handle)) {
3164 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003165 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003166 goto err_drop_inode;
3167 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003168 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003169 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003170 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003171 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003172 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003173 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003174 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003175 if (!IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04003176 inode->i_op = &ext4_fast_symlink_inode_operations;
Al Viro75e75662015-05-02 10:13:58 -04003177 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3178 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003179 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3180 disk_link.len);
3181 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003182 }
Mingming Cao617ba132006-10-11 01:20:53 -07003183 EXT4_I(inode)->i_disksize = inode->i_size;
3184 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003185 if (!err && IS_DIRSYNC(dir))
3186 ext4_handle_sync(handle);
3187
Theodore Ts'o11395752013-02-09 16:27:09 -05003188 if (handle)
3189 ext4_journal_stop(handle);
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003190 goto out_free_encrypted_link;
3191
Jan Karadf5e6222011-05-03 11:12:58 -04003192err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003193 if (handle)
3194 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003195 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003196 unlock_new_inode(inode);
3197 iput(inode);
Jaegeuk Kimd9197652018-01-05 10:44:52 -08003198out_free_encrypted_link:
3199 if (disk_link.name != (unsigned char *)symname)
3200 kfree(disk_link.name);
Jan Karadf5e6222011-05-03 11:12:58 -04003201 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202}
3203
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003204static int ext4_link(struct dentry *old_dentry,
3205 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003206{
3207 handle_t *handle;
David Howells2b0143b2015-03-17 22:25:59 +00003208 struct inode *inode = d_inode(old_dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003209 int err, retries = 0;
3210
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003211 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212 return -EMLINK;
Eric Biggers14ddd822017-10-18 20:21:57 -04003213
3214 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3215 if (err)
3216 return err;
Li Xi040cb372016-01-08 16:01:21 -05003217
3218 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3219 (!projid_eq(EXT4_I(dir)->i_projid,
3220 EXT4_I(old_dentry->d_inode)->i_projid)))
3221 return -EXDEV;
3222
Jan Karaa7cdade2015-06-29 16:22:54 +02003223 err = dquot_initialize(dir);
3224 if (err)
3225 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003226
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003228 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3229 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003230 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231 if (IS_ERR(handle))
3232 return PTR_ERR(handle);
3233
3234 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003235 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236
Kalpak Shahef7f3832007-07-18 09:15:20 -04003237 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003238 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003239 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003240
Al Viro6b38e842008-12-30 02:03:31 -05003241 err = ext4_add_entry(handle, dentry, inode);
3242 if (!err) {
3243 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003244 /* this can happen only for tmpfile being
3245 * linked the first time
3246 */
3247 if (inode->i_nlink == 1)
3248 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003249 d_instantiate(dentry, inode);
3250 } else {
3251 drop_nlink(inode);
3252 iput(inode);
3253 }
Mingming Cao617ba132006-10-11 01:20:53 -07003254 ext4_journal_stop(handle);
3255 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003256 goto retry;
3257 return err;
3258}
3259
Tao Ma32f7f222012-12-10 14:06:01 -05003260
3261/*
3262 * Try to find buffer head where contains the parent block.
3263 * It should be the inode block if it is inlined or the 1st block
3264 * if it is a normal dir.
3265 */
3266static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3267 struct inode *inode,
3268 int *retval,
3269 struct ext4_dir_entry_2 **parent_de,
3270 int *inlined)
3271{
3272 struct buffer_head *bh;
3273
3274 if (!ext4_has_inline_data(inode)) {
Theodore Ts'o3f0307b2019-06-20 21:19:02 -04003275 /* The first directory block must not be a hole, so
3276 * treat it as DIRENT_HTREE
3277 */
3278 bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003279 if (IS_ERR(bh)) {
3280 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003281 return NULL;
3282 }
3283 *parent_de = ext4_next_entry(
3284 (struct ext4_dir_entry_2 *)bh->b_data,
3285 inode->i_sb->s_blocksize);
3286 return bh;
3287 }
3288
3289 *inlined = 1;
3290 return ext4_get_first_inline_block(inode, parent_de, retval);
3291}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003293struct ext4_renament {
3294 struct inode *dir;
3295 struct dentry *dentry;
3296 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003297 bool is_dir;
3298 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003299
3300 /* entry for "dentry" */
3301 struct buffer_head *bh;
3302 struct ext4_dir_entry_2 *de;
3303 int inlined;
3304
3305 /* entry for ".." in inode if it's a directory */
3306 struct buffer_head *dir_bh;
3307 struct ext4_dir_entry_2 *parent_de;
3308 int dir_inlined;
3309};
3310
Miklos Szeredibd1af142014-04-01 17:08:44 +02003311static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3312{
3313 int retval;
3314
3315 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3316 &retval, &ent->parent_de,
3317 &ent->dir_inlined);
3318 if (!ent->dir_bh)
3319 return retval;
3320 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003321 return -EFSCORRUPTED;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003322 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3323 return ext4_journal_get_write_access(handle, ent->dir_bh);
3324}
3325
3326static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3327 unsigned dir_ino)
3328{
3329 int retval;
3330
3331 ent->parent_de->inode = cpu_to_le32(dir_ino);
3332 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3333 if (!ent->dir_inlined) {
3334 if (is_dx(ent->inode)) {
3335 retval = ext4_handle_dirty_dx_node(handle,
3336 ent->inode,
3337 ent->dir_bh);
3338 } else {
3339 retval = ext4_handle_dirty_dirent_node(handle,
3340 ent->inode,
3341 ent->dir_bh);
3342 }
3343 } else {
3344 retval = ext4_mark_inode_dirty(handle, ent->inode);
3345 }
3346 if (retval) {
3347 ext4_std_error(ent->dir->i_sb, retval);
3348 return retval;
3349 }
3350 return 0;
3351}
3352
3353static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3354 unsigned ino, unsigned file_type)
3355{
3356 int retval;
3357
3358 BUFFER_TRACE(ent->bh, "get write access");
3359 retval = ext4_journal_get_write_access(handle, ent->bh);
3360 if (retval)
3361 return retval;
3362 ent->de->inode = cpu_to_le32(ino);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003363 if (ext4_has_feature_filetype(ent->dir->i_sb))
Miklos Szeredibd1af142014-04-01 17:08:44 +02003364 ent->de->file_type = file_type;
3365 ent->dir->i_version++;
3366 ent->dir->i_ctime = ent->dir->i_mtime =
3367 ext4_current_time(ent->dir);
3368 ext4_mark_inode_dirty(handle, ent->dir);
3369 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3370 if (!ent->inlined) {
3371 retval = ext4_handle_dirty_dirent_node(handle,
3372 ent->dir, ent->bh);
3373 if (unlikely(retval)) {
3374 ext4_std_error(ent->dir->i_sb, retval);
3375 return retval;
3376 }
3377 }
3378 brelse(ent->bh);
3379 ent->bh = NULL;
3380
3381 return 0;
3382}
3383
3384static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3385 const struct qstr *d_name)
3386{
3387 int retval = -ENOENT;
3388 struct buffer_head *bh;
3389 struct ext4_dir_entry_2 *de;
3390
3391 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003392 if (IS_ERR(bh))
3393 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003394 if (bh) {
3395 retval = ext4_delete_entry(handle, dir, de, bh);
3396 brelse(bh);
3397 }
3398 return retval;
3399}
3400
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003401static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3402 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003403{
3404 int retval;
3405 /*
3406 * ent->de could have moved from under us during htree split, so make
3407 * sure that we are deleting the right entry. We might also be pointing
3408 * to a stale entry in the unused part of ent->bh so just checking inum
3409 * and the name isn't enough.
3410 */
3411 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3412 ent->de->name_len != ent->dentry->d_name.len ||
3413 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003414 ent->de->name_len) ||
3415 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003416 retval = ext4_find_delete_entry(handle, ent->dir,
3417 &ent->dentry->d_name);
3418 } else {
3419 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3420 if (retval == -ENOENT) {
3421 retval = ext4_find_delete_entry(handle, ent->dir,
3422 &ent->dentry->d_name);
3423 }
3424 }
3425
3426 if (retval) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003427 ext4_warning_inode(ent->dir,
3428 "Deleting old file: nlink %d, error=%d",
3429 ent->dir->i_nlink, retval);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003430 }
3431}
3432
Miklos Szeredibd429982014-04-01 17:08:44 +02003433static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3434{
3435 if (ent->dir_nlink_delta) {
3436 if (ent->dir_nlink_delta == -1)
3437 ext4_dec_count(handle, ent->dir);
3438 else
3439 ext4_inc_count(handle, ent->dir);
3440 ext4_mark_inode_dirty(handle, ent->dir);
3441 }
3442}
3443
Miklos Szeredicd808de2014-10-24 00:14:37 +02003444static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3445 int credits, handle_t **h)
3446{
3447 struct inode *wh;
3448 handle_t *handle;
3449 int retries = 0;
3450
3451 /*
3452 * for inode block, sb block, group summaries,
3453 * and inode bitmap
3454 */
3455 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3456 EXT4_XATTR_TRANS_BLOCKS + 4);
3457retry:
3458 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3459 &ent->dentry->d_name, 0, NULL,
3460 EXT4_HT_DIR, credits);
3461
3462 handle = ext4_journal_current_handle();
3463 if (IS_ERR(wh)) {
3464 if (handle)
3465 ext4_journal_stop(handle);
3466 if (PTR_ERR(wh) == -ENOSPC &&
3467 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3468 goto retry;
3469 } else {
3470 *h = handle;
3471 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3472 wh->i_op = &ext4_special_inode_operations;
3473 }
3474 return wh;
3475}
3476
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003477/*
3478 * Anybody can rename anything with this: the permission checks are left to the
3479 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003480 *
3481 * n.b. old_{dentry,inode) refers to the source dentry/inode
3482 * while new_{dentry,inode) refers to the destination dentry/inode
3483 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003484 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003485static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003486 struct inode *new_dir, struct dentry *new_dentry,
3487 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003488{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003489 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003490 struct ext4_renament old = {
3491 .dir = old_dir,
3492 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003493 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003494 };
3495 struct ext4_renament new = {
3496 .dir = new_dir,
3497 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003498 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003499 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003500 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003501 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003502 struct inode *whiteout = NULL;
3503 int credits;
3504 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003505
Theodore Ts'o23f57cb2018-08-27 01:47:09 -04003506 if (new.inode && new.inode->i_nlink == 0) {
3507 EXT4_ERROR_INODE(new.inode,
3508 "target of rename is already freed");
3509 return -EFSCORRUPTED;
3510 }
3511
Li Xi040cb372016-01-08 16:01:21 -05003512 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3513 (!projid_eq(EXT4_I(new_dir)->i_projid,
3514 EXT4_I(old_dentry->d_inode)->i_projid)))
3515 return -EXDEV;
3516
Jan Karaa7cdade2015-06-29 16:22:54 +02003517 retval = dquot_initialize(old.dir);
3518 if (retval)
3519 return retval;
3520 retval = dquot_initialize(new.dir);
3521 if (retval)
3522 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003523
3524 /* Initialize quotas before so that eventual writes go
3525 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003526 if (new.inode) {
3527 retval = dquot_initialize(new.inode);
3528 if (retval)
3529 return retval;
3530 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003532 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003533 if (IS_ERR(old.bh))
3534 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003535 /*
3536 * Check for inode number is _not_ due to possible IO errors.
3537 * We might rmdir the source, keep it as pwd of some process
3538 * and merrily kill the link to whatever was created under the
3539 * same name. Goodbye sticky bit ;-<
3540 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003542 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003543 goto end_rename;
3544
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003545 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3546 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003547 if (IS_ERR(new.bh)) {
3548 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003549 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003550 goto end_rename;
3551 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003552 if (new.bh) {
3553 if (!new.inode) {
3554 brelse(new.bh);
3555 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003556 }
3557 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003558 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3559 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003560
Miklos Szeredicd808de2014-10-24 00:14:37 +02003561 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3562 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3563 if (!(flags & RENAME_WHITEOUT)) {
3564 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003565 if (IS_ERR(handle)) {
3566 retval = PTR_ERR(handle);
3567 handle = NULL;
3568 goto end_rename;
3569 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003570 } else {
3571 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003572 if (IS_ERR(whiteout)) {
3573 retval = PTR_ERR(whiteout);
3574 whiteout = NULL;
3575 goto end_rename;
3576 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003577 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003578
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003579 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003580 ext4_handle_sync(handle);
3581
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003582 if (S_ISDIR(old.inode->i_mode)) {
3583 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003584 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003585 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003587 } else {
3588 retval = -EMLINK;
3589 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3590 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003591 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003592 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003593 if (retval)
3594 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003595 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003596 /*
3597 * If we're renaming a file within an inline_data dir and adding or
3598 * setting the new dirent causes a conversion from inline_data to
3599 * extents/blockmap, we need to force the dirent delete code to
3600 * re-read the directory, or else we end up trying to delete a dirent
3601 * from what is now the extent tree root (or a block map).
3602 */
3603 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3604 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003605
3606 old_file_type = old.de->file_type;
3607 if (whiteout) {
3608 /*
3609 * Do this before adding a new entry, so the old entry is sure
3610 * to be still pointing to the valid old entry.
3611 */
3612 retval = ext4_setent(handle, &old, whiteout->i_ino,
3613 EXT4_FT_CHRDEV);
3614 if (retval)
3615 goto end_rename;
3616 ext4_mark_inode_dirty(handle, whiteout);
3617 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003618 if (!new.bh) {
3619 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003620 if (retval)
3621 goto end_rename;
3622 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003623 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003624 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003625 if (retval)
3626 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003627 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003628 if (force_reread)
3629 force_reread = !ext4_test_inode_flag(new.dir,
3630 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003631
3632 /*
3633 * Like most other Unix systems, set the ctime for inodes on a
3634 * rename.
3635 */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003636 old.inode->i_ctime = ext4_current_time(old.inode);
3637 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003638
Miklos Szeredicd808de2014-10-24 00:14:37 +02003639 if (!whiteout) {
3640 /*
3641 * ok, that's it
3642 */
3643 ext4_rename_delete(handle, &old, force_reread);
3644 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003645
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003646 if (new.inode) {
3647 ext4_dec_count(handle, new.inode);
3648 new.inode->i_ctime = ext4_current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003650 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3651 ext4_update_dx_flag(old.dir);
3652 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003653 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3654 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003655 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003656
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003657 ext4_dec_count(handle, old.dir);
3658 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003659 /* checked ext4_empty_dir above, can't have another
3660 * parent, ext4_dec_count() won't work for many-linked
3661 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003662 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003663 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003664 ext4_inc_count(handle, new.dir);
3665 ext4_update_dx_flag(new.dir);
3666 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003667 }
3668 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003669 ext4_mark_inode_dirty(handle, old.dir);
3670 if (new.inode) {
3671 ext4_mark_inode_dirty(handle, new.inode);
3672 if (!new.inode->i_nlink)
3673 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003674 }
3675 retval = 0;
3676
3677end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003678 brelse(old.dir_bh);
3679 brelse(old.bh);
3680 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003681 if (whiteout) {
3682 if (retval)
3683 drop_nlink(whiteout);
3684 unlock_new_inode(whiteout);
3685 iput(whiteout);
3686 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003687 if (handle)
3688 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003689 return retval;
3690}
3691
Miklos Szeredibd429982014-04-01 17:08:44 +02003692static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3693 struct inode *new_dir, struct dentry *new_dentry)
3694{
3695 handle_t *handle = NULL;
3696 struct ext4_renament old = {
3697 .dir = old_dir,
3698 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003699 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003700 };
3701 struct ext4_renament new = {
3702 .dir = new_dir,
3703 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003704 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003705 };
3706 u8 new_file_type;
3707 int retval;
3708
Li Xi040cb372016-01-08 16:01:21 -05003709 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
3710 !projid_eq(EXT4_I(new_dir)->i_projid,
3711 EXT4_I(old_dentry->d_inode)->i_projid)) ||
3712 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
3713 !projid_eq(EXT4_I(old_dir)->i_projid,
3714 EXT4_I(new_dentry->d_inode)->i_projid)))
3715 return -EXDEV;
3716
Jan Karaa7cdade2015-06-29 16:22:54 +02003717 retval = dquot_initialize(old.dir);
3718 if (retval)
3719 return retval;
3720 retval = dquot_initialize(new.dir);
3721 if (retval)
3722 return retval;
Miklos Szeredibd429982014-04-01 17:08:44 +02003723
3724 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3725 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003726 if (IS_ERR(old.bh))
3727 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003728 /*
3729 * Check for inode number is _not_ due to possible IO errors.
3730 * We might rmdir the source, keep it as pwd of some process
3731 * and merrily kill the link to whatever was created under the
3732 * same name. Goodbye sticky bit ;-<
3733 */
3734 retval = -ENOENT;
3735 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3736 goto end_rename;
3737
3738 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3739 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003740 if (IS_ERR(new.bh)) {
3741 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003742 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003743 goto end_rename;
3744 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003745
3746 /* RENAME_EXCHANGE case: old *and* new must both exist */
3747 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3748 goto end_rename;
3749
3750 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3751 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3752 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003753 if (IS_ERR(handle)) {
3754 retval = PTR_ERR(handle);
3755 handle = NULL;
3756 goto end_rename;
3757 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003758
3759 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3760 ext4_handle_sync(handle);
3761
3762 if (S_ISDIR(old.inode->i_mode)) {
3763 old.is_dir = true;
3764 retval = ext4_rename_dir_prepare(handle, &old);
3765 if (retval)
3766 goto end_rename;
3767 }
3768 if (S_ISDIR(new.inode->i_mode)) {
3769 new.is_dir = true;
3770 retval = ext4_rename_dir_prepare(handle, &new);
3771 if (retval)
3772 goto end_rename;
3773 }
3774
3775 /*
3776 * Other than the special case of overwriting a directory, parents'
3777 * nlink only needs to be modified if this is a cross directory rename.
3778 */
3779 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3780 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3781 new.dir_nlink_delta = -old.dir_nlink_delta;
3782 retval = -EMLINK;
3783 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3784 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3785 goto end_rename;
3786 }
3787
3788 new_file_type = new.de->file_type;
3789 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3790 if (retval)
3791 goto end_rename;
3792
3793 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3794 if (retval)
3795 goto end_rename;
3796
3797 /*
3798 * Like most other Unix systems, set the ctime for inodes on a
3799 * rename.
3800 */
3801 old.inode->i_ctime = ext4_current_time(old.inode);
3802 new.inode->i_ctime = ext4_current_time(new.inode);
3803 ext4_mark_inode_dirty(handle, old.inode);
3804 ext4_mark_inode_dirty(handle, new.inode);
3805
3806 if (old.dir_bh) {
3807 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3808 if (retval)
3809 goto end_rename;
3810 }
3811 if (new.dir_bh) {
3812 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3813 if (retval)
3814 goto end_rename;
3815 }
3816 ext4_update_dir_count(handle, &old);
3817 ext4_update_dir_count(handle, &new);
3818 retval = 0;
3819
3820end_rename:
3821 brelse(old.dir_bh);
3822 brelse(new.dir_bh);
3823 brelse(old.bh);
3824 brelse(new.bh);
3825 if (handle)
3826 ext4_journal_stop(handle);
3827 return retval;
3828}
3829
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003830static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3831 struct inode *new_dir, struct dentry *new_dentry,
3832 unsigned int flags)
3833{
Eric Biggersf11498c2017-10-18 20:21:57 -04003834 int err;
3835
Miklos Szeredicd808de2014-10-24 00:14:37 +02003836 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003837 return -EINVAL;
3838
Eric Biggersf11498c2017-10-18 20:21:57 -04003839 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
3840 flags);
3841 if (err)
3842 return err;
3843
Miklos Szeredibd429982014-04-01 17:08:44 +02003844 if (flags & RENAME_EXCHANGE) {
3845 return ext4_cross_rename(old_dir, old_dentry,
3846 new_dir, new_dentry);
3847 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003848
3849 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003850}
3851
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003852/*
3853 * directories can handle most operations...
3854 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003855const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003856 .create = ext4_create,
3857 .lookup = ext4_lookup,
3858 .link = ext4_link,
3859 .unlink = ext4_unlink,
3860 .symlink = ext4_symlink,
3861 .mkdir = ext4_mkdir,
3862 .rmdir = ext4_rmdir,
3863 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003864 .tmpfile = ext4_tmpfile,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003865 .rename = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003866 .setattr = ext4_setattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003867 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003868 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003869 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003870 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003871};
3872
Arjan van de Ven754661f2007-02-12 00:55:38 -08003873const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003874 .setattr = ext4_setattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003875 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003876 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003877 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878};