blob: 798b3ac680db1b4f8c4510a0bd66d0510d11216a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/namei.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
16 * Big-endian to little-endian byte-swapping/bitmaps by
17 * David S. Miller (davem@caip.rutgers.edu), 1995
18 * Directory entry file type support and forward compatibility hooks
19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Hash Tree Directory indexing (c)
21 * Daniel Phillips, 2001
22 * Hash Tree Directory indexing porting
23 * Christopher Li, 2002
24 * Hash Tree Directory indexing cleanup
25 * Theodore Ts'o, 2002
26 */
27
28#include <linux/fs.h>
29#include <linux/pagemap.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/quotaops.h>
35#include <linux/buffer_head.h>
36#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040037#include "ext4.h"
38#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039
Dave Kleikampac27a0e2006-10-11 01:20:50 -070040#include "xattr.h"
41#include "acl.h"
42
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040043#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044/*
45 * define how far ahead to read directories while searching them.
46 */
47#define NAMEI_RA_CHUNKS 2
48#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040049#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050
Mingming Cao617ba132006-10-11 01:20:53 -070051static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070052 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050053 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070054{
55 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040056 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057
Theodore Ts'odf981d02012-08-17 09:48:17 -040058 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050060 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
61 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040062
Dave Kleikampac27a0e2006-10-11 01:20:50 -070063 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
64
Theodore Ts'oc5e298a2015-06-21 01:25:29 -040065 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
Theodore Ts'o1c215022014-08-29 20:52:15 -040066 if (IS_ERR(bh))
67 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050068 inode->i_size += inode->i_sb->s_blocksize;
69 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040070 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050071 err = ext4_journal_get_write_access(handle, bh);
72 if (err) {
73 brelse(bh);
74 ext4_std_error(inode->i_sb, err);
75 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040076 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077 return bh;
78}
79
Theodore Ts'odc6982f2013-02-14 23:59:26 -050080static int ext4_dx_csum_verify(struct inode *inode,
81 struct ext4_dir_entry *dirent);
82
83typedef enum {
84 EITHER, INDEX, DIRENT
85} dirblock_type_t;
86
87#define ext4_read_dirblock(inode, block, type) \
Andreas Dilgerb03a2f72015-06-15 14:50:26 -040088 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
Theodore Ts'odc6982f2013-02-14 23:59:26 -050089
90static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
Andreas Dilgerb03a2f72015-06-15 14:50:26 -040091 ext4_lblk_t block,
92 dirblock_type_t type,
93 const char *func,
94 unsigned int line)
Theodore Ts'odc6982f2013-02-14 23:59:26 -050095{
96 struct buffer_head *bh;
97 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -040098 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -050099
Theodore Ts'o1c215022014-08-29 20:52:15 -0400100 bh = ext4_bread(NULL, inode, block, 0);
101 if (IS_ERR(bh)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400102 __ext4_warning(inode->i_sb, func, line,
103 "inode #%lu: lblock %lu: comm %s: "
104 "error %ld reading directory block",
105 inode->i_ino, (unsigned long)block,
106 current->comm, PTR_ERR(bh));
Theodore Ts'o1c215022014-08-29 20:52:15 -0400107
108 return bh;
109 }
110 if (!bh) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400111 ext4_error_inode(inode, func, line, block,
112 "Directory hole found");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400113 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500114 }
115 dirent = (struct ext4_dir_entry *) bh->b_data;
116 /* Determine whether or not we have an index block */
117 if (is_dx(inode)) {
118 if (block == 0)
119 is_dx_block = 1;
120 else if (ext4_rec_len_from_disk(dirent->rec_len,
121 inode->i_sb->s_blocksize) ==
122 inode->i_sb->s_blocksize)
123 is_dx_block = 1;
124 }
125 if (!is_dx_block && type == INDEX) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400126 ext4_error_inode(inode, func, line, block,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500127 "directory leaf block found instead of index block");
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400128 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500129 }
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400130 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500131 buffer_verified(bh))
132 return bh;
133
134 /*
135 * An empty leaf block can get mistaken for a index block; for
136 * this reason, we can only check the index checksum when the
137 * caller is sure it should be an index block.
138 */
139 if (is_dx_block && type == INDEX) {
140 if (ext4_dx_csum_verify(inode, dirent))
141 set_buffer_verified(bh);
142 else {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400143 ext4_error_inode(inode, func, line, block,
144 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400146 return ERR_PTR(-EFSBADCRC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 }
148 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500149 if (!is_dx_block) {
150 if (ext4_dirent_csum_verify(inode, dirent))
151 set_buffer_verified(bh);
152 else {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400153 ext4_error_inode(inode, func, line, block,
154 "Directory block failed checksum");
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500155 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400156 return ERR_PTR(-EFSBADCRC);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500157 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158 }
159 return bh;
160}
161
162#ifndef assert
163#define assert(test) J_ASSERT(test)
164#endif
165
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166#ifdef DX_DEBUG
167#define dxtrace(command) command
168#else
169#define dxtrace(command)
170#endif
171
172struct fake_dirent
173{
174 __le32 inode;
175 __le16 rec_len;
176 u8 name_len;
177 u8 file_type;
178};
179
180struct dx_countlimit
181{
182 __le16 limit;
183 __le16 count;
184};
185
186struct dx_entry
187{
188 __le32 hash;
189 __le32 block;
190};
191
192/*
193 * dx_root_info is laid out so that if it should somehow get overlaid by a
194 * dirent the two low bits of the hash version will be zero. Therefore, the
195 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
196 */
197
198struct dx_root
199{
200 struct fake_dirent dot;
201 char dot_name[4];
202 struct fake_dirent dotdot;
203 char dotdot_name[4];
204 struct dx_root_info
205 {
206 __le32 reserved_zero;
207 u8 hash_version;
208 u8 info_length; /* 8 */
209 u8 indirect_levels;
210 u8 unused_flags;
211 }
212 info;
213 struct dx_entry entries[0];
214};
215
216struct dx_node
217{
218 struct fake_dirent fake;
219 struct dx_entry entries[0];
220};
221
222
223struct dx_frame
224{
225 struct buffer_head *bh;
226 struct dx_entry *entries;
227 struct dx_entry *at;
228};
229
230struct dx_map_entry
231{
232 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700233 u16 offs;
234 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235};
236
Darrick J. Wonge6153912012-04-29 18:23:10 -0400237/*
238 * This goes at the end of each htree block.
239 */
240struct dx_tail {
241 u32 dt_reserved;
242 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
243};
244
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500245static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
246static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400247static inline unsigned dx_get_hash(struct dx_entry *entry);
248static void dx_set_hash(struct dx_entry *entry, unsigned value);
249static unsigned dx_get_count(struct dx_entry *entries);
250static unsigned dx_get_limit(struct dx_entry *entries);
251static void dx_set_count(struct dx_entry *entries, unsigned value);
252static void dx_set_limit(struct dx_entry *entries, unsigned value);
253static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
254static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400255static struct dx_frame *dx_probe(struct ext4_filename *fname,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 struct inode *dir,
257 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400258 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400259static void dx_release(struct dx_frame *frames);
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400260static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
261 unsigned blocksize, struct dx_hash_info *hinfo,
262 struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400264static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500265 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500266static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500267static void dx_insert_block(struct dx_frame *frame,
268 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700269static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 struct dx_frame *frame,
271 struct dx_frame *frames,
272 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400273static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400274 struct ext4_filename *fname,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400275 struct ext4_dir_entry_2 **res_dir);
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400276static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -0500277 struct inode *dir, struct inode *inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400279/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500280void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
281 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400282{
283 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
284 t->det_rec_len = ext4_rec_len_to_disk(
285 sizeof(struct ext4_dir_entry_tail), blocksize);
286 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
287}
288
289/* Walk through a dirent block to find a checksum "dirent" at the tail */
290static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
291 struct ext4_dir_entry *de)
292{
293 struct ext4_dir_entry_tail *t;
294
295#ifdef PARANOID
296 struct ext4_dir_entry *d, *top;
297
298 d = de;
299 top = (struct ext4_dir_entry *)(((void *)de) +
300 (EXT4_BLOCK_SIZE(inode->i_sb) -
301 sizeof(struct ext4_dir_entry_tail)));
302 while (d < top && d->rec_len)
303 d = (struct ext4_dir_entry *)(((void *)d) +
304 le16_to_cpu(d->rec_len));
305
306 if (d != top)
307 return NULL;
308
309 t = (struct ext4_dir_entry_tail *)d;
310#else
311 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
312#endif
313
314 if (t->det_reserved_zero1 ||
315 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
316 t->det_reserved_zero2 ||
317 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
318 return NULL;
319
320 return t;
321}
322
323static __le32 ext4_dirent_csum(struct inode *inode,
324 struct ext4_dir_entry *dirent, int size)
325{
326 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
327 struct ext4_inode_info *ei = EXT4_I(inode);
328 __u32 csum;
329
330 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
331 return cpu_to_le32(csum);
332}
333
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400334#define warn_no_space_for_csum(inode) \
335 __warn_no_space_for_csum((inode), __func__, __LINE__)
336
337static void __warn_no_space_for_csum(struct inode *inode, const char *func,
338 unsigned int line)
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500339{
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400340 __ext4_warning_inode(inode, func, line,
341 "No space for directory leaf checksum. Please run e2fsck -D.");
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500342}
343
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400344int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
345{
346 struct ext4_dir_entry_tail *t;
347
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400348 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400349 return 1;
350
351 t = get_dirent_tail(inode, dirent);
352 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500353 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400354 return 0;
355 }
356
357 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
358 (void *)t - (void *)dirent))
359 return 0;
360
361 return 1;
362}
363
364static void ext4_dirent_csum_set(struct inode *inode,
365 struct ext4_dir_entry *dirent)
366{
367 struct ext4_dir_entry_tail *t;
368
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400369 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400370 return;
371
372 t = get_dirent_tail(inode, dirent);
373 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500374 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400375 return;
376 }
377
378 t->det_checksum = ext4_dirent_csum(inode, dirent,
379 (void *)t - (void *)dirent);
380}
381
Tao Ma3c47d542012-12-10 14:05:59 -0500382int ext4_handle_dirty_dirent_node(handle_t *handle,
383 struct inode *inode,
384 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400385{
386 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
387 return ext4_handle_dirty_metadata(handle, inode, bh);
388}
389
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400390static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
391 struct ext4_dir_entry *dirent,
392 int *offset)
393{
394 struct ext4_dir_entry *dp;
395 struct dx_root_info *root;
396 int count_offset;
397
398 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
399 count_offset = 8;
400 else if (le16_to_cpu(dirent->rec_len) == 12) {
401 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
402 if (le16_to_cpu(dp->rec_len) !=
403 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
404 return NULL;
405 root = (struct dx_root_info *)(((void *)dp + 12));
406 if (root->reserved_zero ||
407 root->info_length != sizeof(struct dx_root_info))
408 return NULL;
409 count_offset = 32;
410 } else
411 return NULL;
412
413 if (offset)
414 *offset = count_offset;
415 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
416}
417
418static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
419 int count_offset, int count, struct dx_tail *t)
420{
421 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
422 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400423 __u32 csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400424 int size;
Daeho Jeongb47820e2016-07-03 17:51:39 -0400425 __u32 dummy_csum = 0;
426 int offset = offsetof(struct dx_tail, dt_checksum);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400427
428 size = count_offset + (count * sizeof(struct dx_entry));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400429 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
Daeho Jeongb47820e2016-07-03 17:51:39 -0400430 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
431 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400432
433 return cpu_to_le32(csum);
434}
435
436static int ext4_dx_csum_verify(struct inode *inode,
437 struct ext4_dir_entry *dirent)
438{
439 struct dx_countlimit *c;
440 struct dx_tail *t;
441 int count_offset, limit, count;
442
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400443 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400444 return 1;
445
446 c = get_dx_countlimit(inode, dirent, &count_offset);
447 if (!c) {
448 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
Daeho Jeongfa964542016-07-03 21:11:08 -0400449 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400450 }
451 limit = le16_to_cpu(c->limit);
452 count = le16_to_cpu(c->count);
453 if (count_offset + (limit * sizeof(struct dx_entry)) >
454 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500455 warn_no_space_for_csum(inode);
Daeho Jeongfa964542016-07-03 21:11:08 -0400456 return 0;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400457 }
458 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
459
460 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
461 count, t))
462 return 0;
463 return 1;
464}
465
466static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
467{
468 struct dx_countlimit *c;
469 struct dx_tail *t;
470 int count_offset, limit, count;
471
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400472 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400473 return;
474
475 c = get_dx_countlimit(inode, dirent, &count_offset);
476 if (!c) {
477 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
478 return;
479 }
480 limit = le16_to_cpu(c->limit);
481 count = le16_to_cpu(c->count);
482 if (count_offset + (limit * sizeof(struct dx_entry)) >
483 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500484 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400485 return;
486 }
487 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
488
489 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
490}
491
492static inline int ext4_handle_dirty_dx_node(handle_t *handle,
493 struct inode *inode,
494 struct buffer_head *bh)
495{
496 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
497 return ext4_handle_dirty_metadata(handle, inode, bh);
498}
499
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500/*
Li Zefanf795e142008-07-11 19:27:31 -0400501 * p is at least 6 bytes before the end of page
502 */
503static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500504ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400505{
506 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500507 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400508}
509
510/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 * Future: use high four bits of block for coalesce-on-delete flags
512 * Mask them off for now.
513 */
514
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500515static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400517 return le32_to_cpu(entry->block) & 0x0fffffff;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518}
519
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500520static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521{
522 entry->block = cpu_to_le32(value);
523}
524
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400525static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700526{
527 return le32_to_cpu(entry->hash);
528}
529
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400530static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531{
532 entry->hash = cpu_to_le32(value);
533}
534
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400535static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536{
537 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
538}
539
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400540static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541{
542 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
543}
544
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400545static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546{
547 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
548}
549
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400550static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551{
552 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
553}
554
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400555static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556{
Mingming Cao617ba132006-10-11 01:20:53 -0700557 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
558 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400559
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400560 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400561 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400562 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563}
564
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400565static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566{
Mingming Cao617ba132006-10-11 01:20:53 -0700567 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400568
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400569 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400570 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400571 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572}
573
574/*
575 * Debug
576 */
577#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400578static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579{
Andrew Morton63f57932006-10-11 01:21:24 -0700580 int i, n = dx_get_count (entries);
Joe Perchesd74f3d22016-10-15 09:57:31 -0400581 printk(KERN_DEBUG "%s index", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700582 for (i = 0; i < n; i++) {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400583 printk(KERN_CONT " %x->%lu",
584 i ? dx_get_hash(entries + i) : 0,
585 (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700586 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400587 printk(KERN_CONT "\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588}
589
590struct stats
591{
592 unsigned names;
593 unsigned space;
594 unsigned bcount;
595};
596
Michael Halcrowb3098482015-04-12 01:07:01 -0400597static struct stats dx_show_leaf(struct inode *dir,
598 struct dx_hash_info *hinfo,
599 struct ext4_dir_entry_2 *de,
600 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601{
602 unsigned names = 0, space = 0;
603 char *base = (char *) de;
604 struct dx_hash_info h = *hinfo;
605
606 printk("names: ");
607 while ((char *) de < base + size)
608 {
609 if (de->inode)
610 {
611 if (show_names)
612 {
Michael Halcrowb3098482015-04-12 01:07:01 -0400613#ifdef CONFIG_EXT4_FS_ENCRYPTION
614 int len;
615 char *name;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400616 struct fscrypt_str fname_crypto_str =
617 FSTR_INIT(NULL, 0);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400618 int res = 0;
Michael Halcrowb3098482015-04-12 01:07:01 -0400619
620 name = de->name;
621 len = de->name_len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400622 if (ext4_encrypted_inode(dir))
623 res = fscrypt_get_encryption_info(dir);
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400624 if (res) {
625 printk(KERN_WARNING "Error setting up"
626 " fname crypto: %d\n", res);
Michael Halcrowb3098482015-04-12 01:07:01 -0400627 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400628 if (!fscrypt_has_encryption_key(dir)) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400629 /* Directory is not encrypted */
630 ext4fs_dirhash(de->name,
631 de->name_len, &h);
632 printk("%*.s:(U)%x.%u ", len,
633 name, h.hash,
634 (unsigned) ((char *) de
635 - base));
636 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400637 struct fscrypt_str de_name =
638 FSTR_INIT(name, len);
639
Michael Halcrowb3098482015-04-12 01:07:01 -0400640 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400641 res = fscrypt_fname_alloc_buffer(
642 dir, len,
Michael Halcrowb3098482015-04-12 01:07:01 -0400643 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400644 if (res)
Michael Halcrowb3098482015-04-12 01:07:01 -0400645 printk(KERN_WARNING "Error "
646 "allocating crypto "
647 "buffer--skipping "
648 "crypto\n");
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400649 res = fscrypt_fname_disk_to_usr(dir,
650 0, 0, &de_name,
651 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -0400652 if (res) {
Michael Halcrowb3098482015-04-12 01:07:01 -0400653 printk(KERN_WARNING "Error "
654 "converting filename "
655 "from disk to usr"
656 "\n");
657 name = "??";
658 len = 2;
659 } else {
660 name = fname_crypto_str.name;
661 len = fname_crypto_str.len;
662 }
Theodore Ts'o5de0b4d2015-05-01 16:56:45 -0400663 ext4fs_dirhash(de->name, de->name_len,
664 &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400665 printk("%*.s:(E)%x.%u ", len, name,
666 h.hash, (unsigned) ((char *) de
667 - base));
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400668 fscrypt_fname_free_buffer(
669 &fname_crypto_str);
Michael Halcrowb3098482015-04-12 01:07:01 -0400670 }
671#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 int len = de->name_len;
673 char *name = de->name;
Mingming Cao617ba132006-10-11 01:20:53 -0700674 ext4fs_dirhash(de->name, de->name_len, &h);
Michael Halcrowb3098482015-04-12 01:07:01 -0400675 printk("%*.s:%x.%u ", len, name, h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400676 (unsigned) ((char *) de - base));
Michael Halcrowb3098482015-04-12 01:07:01 -0400677#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 }
Mingming Cao617ba132006-10-11 01:20:53 -0700679 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680 names++;
681 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500682 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683 }
Joe Perchesd74f3d22016-10-15 09:57:31 -0400684 printk(KERN_CONT "(%i)\n", names);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 return (struct stats) { names, space, 1 };
686}
687
688struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
689 struct dx_entry *entries, int levels)
690{
691 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400692 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 unsigned bcount = 0;
694 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695 printk("%i indexed blocks...\n", count);
696 for (i = 0; i < count; i++, entries++)
697 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500698 ext4_lblk_t block = dx_get_block(entries);
699 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
701 struct stats stats;
702 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400703 bh = ext4_bread(NULL,dir, block, 0);
704 if (!bh || IS_ERR(bh))
705 continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 stats = levels?
707 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Michael Halcrowb3098482015-04-12 01:07:01 -0400708 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
709 bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 names += stats.names;
711 space += stats.space;
712 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400713 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 }
715 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400716 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400717 levels ? "" : " ", names, space/bcount,
718 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 return (struct stats) { names, space, bcount};
720}
721#endif /* DX_DEBUG */
722
723/*
724 * Probe for a directory leaf block to search.
725 *
726 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
727 * error in the directory index, and the caller should fall back to
728 * searching the directory normally. The callers of dx_probe **MUST**
729 * check for this error code, and make sure it never gets reflected
730 * back to userspace.
731 */
732static struct dx_frame *
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400733dx_probe(struct ext4_filename *fname, struct inode *dir,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400734 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735{
736 unsigned count, indirect;
737 struct dx_entry *at, *entries, *p, *q, *m;
738 struct dx_root *root;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 struct dx_frame *frame = frame_in;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400740 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 u32 hash;
742
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400743 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400744 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
745 if (IS_ERR(frame->bh))
746 return (struct dx_frame *) frame->bh;
747
748 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 if (root->info.hash_version != DX_HASH_TEA &&
750 root->info.hash_version != DX_HASH_HALF_MD4 &&
751 root->info.hash_version != DX_HASH_LEGACY) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400752 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
753 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 goto fail;
755 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400756 if (fname)
757 hinfo = &fname->hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400759 if (hinfo->hash_version <= DX_HASH_TEA)
760 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700761 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'o5b643f92015-05-18 13:14:47 -0400762 if (fname && fname_name(fname))
763 ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764 hash = hinfo->hash;
765
766 if (root->info.unused_flags & 1) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400767 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
768 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 goto fail;
770 }
771
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400772 indirect = root->info.indirect_levels;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400773 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
774 ext4_warning(dir->i_sb,
775 "Directory (ino: %lu) htree depth %#06x exceed"
776 "supported value", dir->i_ino,
777 ext4_dir_htree_level(dir->i_sb));
778 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
779 ext4_warning(dir->i_sb, "Enable large directory "
780 "feature to access it");
781 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 goto fail;
783 }
784
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400785 entries = (struct dx_entry *)(((char *)&root->info) +
786 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700787
788 if (dx_get_limit(entries) != dx_root_limit(dir,
789 root->info.info_length)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400790 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
791 dx_get_limit(entries),
792 dx_root_limit(dir, root->info.info_length));
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700793 goto fail;
794 }
795
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400796 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400797 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700799 if (!count || count > dx_get_limit(entries)) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400800 ext4_warning_inode(dir,
801 "dx entry: count %u beyond limit %u",
802 count, dx_get_limit(entries));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400803 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700804 }
805
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 p = entries + 1;
807 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400808 while (p <= q) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400809 m = p + (q - p) / 2;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400810 dxtrace(printk(KERN_CONT "."));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 if (dx_get_hash(m) > hash)
812 q = m - 1;
813 else
814 p = m + 1;
815 }
816
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400817 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818 unsigned n = count - 1;
819 at = entries;
820 while (n--)
821 {
Joe Perchesd74f3d22016-10-15 09:57:31 -0400822 dxtrace(printk(KERN_CONT ","));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 if (dx_get_hash(++at) > hash)
824 {
825 at--;
826 break;
827 }
828 }
829 assert (at == p - 1);
830 }
831
832 at = p - 1;
Joe Perchesd74f3d22016-10-15 09:57:31 -0400833 dxtrace(printk(KERN_CONT " %x->%u\n",
834 at == entries ? 0 : dx_get_hash(at),
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400835 dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 frame->entries = entries;
837 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400838 if (!indirect--)
839 return frame;
840 frame++;
841 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
842 if (IS_ERR(frame->bh)) {
843 ret_err = (struct dx_frame *) frame->bh;
844 frame->bh = NULL;
845 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400846 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400847 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400848
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400849 if (dx_get_limit(entries) != dx_node_limit(dir)) {
850 ext4_warning_inode(dir,
851 "dx entry: limit %u != node limit %u",
852 dx_get_limit(entries), dx_node_limit(dir));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400853 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700854 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400856fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 while (frame >= frame_in) {
858 brelse(frame->bh);
859 frame--;
860 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400861
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400862 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400863 ext4_warning_inode(dir,
864 "Corrupt directory, running e2fsck is recommended");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400865 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866}
867
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400868static void dx_release(struct dx_frame *frames)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400870 struct dx_root_info *info;
871 int i;
872
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873 if (frames[0].bh == NULL)
874 return;
875
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -0400876 info = &((struct dx_root *)frames[0].bh->b_data)->info;
877 for (i = 0; i <= info->indirect_levels; i++) {
878 if (frames[i].bh == NULL)
879 break;
880 brelse(frames[i].bh);
881 frames[i].bh = NULL;
882 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883}
884
885/*
886 * This function increments the frame pointer to search the next leaf
887 * block, and reads in the necessary intervening nodes if the search
888 * should be necessary. Whether or not the search is necessary is
889 * controlled by the hash parameter. If the hash value is even, then
890 * the search is only continued if the next block starts with that
891 * hash value. This is used if we are searching for a specific file.
892 *
893 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
894 *
895 * This function returns 1 if the caller should continue to search,
896 * or 0 if it should not. If there is an error reading one of the
897 * index blocks, it will a negative error code.
898 *
899 * If start_hash is non-null, it will be filled in with the starting
900 * hash of the next page.
901 */
Mingming Cao617ba132006-10-11 01:20:53 -0700902static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 struct dx_frame *frame,
904 struct dx_frame *frames,
905 __u32 *start_hash)
906{
907 struct dx_frame *p;
908 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500909 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 __u32 bhash;
911
912 p = frame;
913 /*
914 * Find the next leaf page by incrementing the frame pointer.
915 * If we run out of entries in the interior node, loop around and
916 * increment pointer in the parent node. When we break out of
917 * this loop, num_frames indicates the number of interior
918 * nodes need to be read.
919 */
920 while (1) {
921 if (++(p->at) < p->entries + dx_get_count(p->entries))
922 break;
923 if (p == frames)
924 return 0;
925 num_frames++;
926 p--;
927 }
928
929 /*
930 * If the hash is 1, then continue only if the next page has a
931 * continuation hash of any value. This is used for readdir
932 * handling. Otherwise, check to see if the hash matches the
933 * desired contiuation hash. If it doesn't, return since
934 * there's no point to read in the successive index pages.
935 */
936 bhash = dx_get_hash(p->at);
937 if (start_hash)
938 *start_hash = bhash;
939 if ((hash & 1) == 0) {
940 if ((bhash & ~1) != hash)
941 return 0;
942 }
943 /*
944 * If the hash is HASH_NB_ALWAYS, we always go to the next
945 * block so no check is necessary
946 */
947 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500948 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
949 if (IS_ERR(bh))
950 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400952 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 p->bh = bh;
954 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
955 }
956 return 1;
957}
958
959
960/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 * This function fills a red-black tree with information from a
962 * directory block. It returns the number directory entries loaded
963 * into the tree. If there is an error it is returned in err.
964 */
965static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500966 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 struct dx_hash_info *hinfo,
968 __u32 start_hash, __u32 start_minor_hash)
969{
970 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700971 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400972 int err = 0, count = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400973 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500975 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
976 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500977 bh = ext4_read_dirblock(dir, block, DIRENT);
978 if (IS_ERR(bh))
979 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400980
Mingming Cao617ba132006-10-11 01:20:53 -0700981 de = (struct ext4_dir_entry_2 *) bh->b_data;
982 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700984 EXT4_DIR_REC_LEN(0));
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400985#ifdef CONFIG_EXT4_FS_ENCRYPTION
986 /* Check if the directory is encrypted */
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400987 if (ext4_encrypted_inode(dir)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400988 err = fscrypt_get_encryption_info(dir);
Theodore Ts'oc936e1e2015-05-31 13:34:22 -0400989 if (err < 0) {
990 brelse(bh);
991 return err;
992 }
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400993 err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400994 &fname_crypto_str);
995 if (err < 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -0400996 brelse(bh);
997 return err;
998 }
999 }
1000#endif
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001001 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -05001002 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -05001003 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -05001004 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1005 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -04001006 /* silently ignore the rest of the block */
1007 break;
Eric Sandeene6c40212006-12-06 20:36:28 -08001008 }
Mingming Cao617ba132006-10-11 01:20:53 -07001009 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 if ((hinfo->hash < start_hash) ||
1011 ((hinfo->hash == start_hash) &&
1012 (hinfo->minor_hash < start_minor_hash)))
1013 continue;
1014 if (de->inode == 0)
1015 continue;
Theodore Ts'ob7236e22015-05-18 13:17:47 -04001016 if (!ext4_encrypted_inode(dir)) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001017 tmp_str.name = de->name;
1018 tmp_str.len = de->name_len;
1019 err = ext4_htree_store_dirent(dir_file,
1020 hinfo->hash, hinfo->minor_hash, de,
1021 &tmp_str);
1022 } else {
Theodore Ts'od2299592015-05-18 13:15:47 -04001023 int save_len = fname_crypto_str.len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001024 struct fscrypt_str de_name = FSTR_INIT(de->name,
1025 de->name_len);
Theodore Ts'od2299592015-05-18 13:15:47 -04001026
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001027 /* Directory is encrypted */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001028 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1029 hinfo->minor_hash, &de_name,
1030 &fname_crypto_str);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04001031 if (err) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001032 count = err;
1033 goto errout;
1034 }
1035 err = ext4_htree_store_dirent(dir_file,
1036 hinfo->hash, hinfo->minor_hash, de,
1037 &fname_crypto_str);
Theodore Ts'od2299592015-05-18 13:15:47 -04001038 fname_crypto_str.len = save_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001039 }
Theodore Ts'o2f618302015-04-12 00:56:26 -04001040 if (err != 0) {
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001041 count = err;
1042 goto errout;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 }
1044 count++;
1045 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001046errout:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 brelse(bh);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001048#ifdef CONFIG_EXT4_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001049 fscrypt_fname_free_buffer(&fname_crypto_str);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001050#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 return count;
1052}
1053
1054
1055/*
1056 * This function fills a red-black tree with information from a
1057 * directory. We start scanning the directory in hash order, starting
1058 * at start_hash and start_minor_hash.
1059 *
1060 * This function returns the number of entries inserted into the tree,
1061 * or a negative error code.
1062 */
Mingming Cao617ba132006-10-11 01:20:53 -07001063int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 __u32 start_minor_hash, __u32 *next_hash)
1065{
1066 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001067 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001068 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001070 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001072 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 __u32 hashval;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001074 struct fscrypt_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075
Theodore Ts'o60e66792010-05-17 07:00:00 -04001076 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001077 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001078 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001079 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001080 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001081 if (hinfo.hash_version <= DX_HASH_TEA)
1082 hinfo.hash_version +=
1083 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001084 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001085 if (ext4_has_inline_data(dir)) {
1086 int has_inline_data = 1;
1087 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1088 &hinfo, start_hash,
1089 start_minor_hash,
1090 &has_inline_data);
1091 if (has_inline_data) {
1092 *next_hash = ~0;
1093 return count;
1094 }
1095 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1097 start_hash, start_minor_hash);
1098 *next_hash = ~0;
1099 return count;
1100 }
1101 hinfo.hash = start_hash;
1102 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001103 frame = dx_probe(NULL, dir, &hinfo, frames);
1104 if (IS_ERR(frame))
1105 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106
1107 /* Add '.' and '..' from the htree header */
1108 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001109 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001110 tmp_str.name = de->name;
1111 tmp_str.len = de->name_len;
1112 err = ext4_htree_store_dirent(dir_file, 0, 0,
1113 de, &tmp_str);
1114 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 goto errout;
1116 count++;
1117 }
1118 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001119 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001120 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001121 tmp_str.name = de->name;
1122 tmp_str.len = de->name_len;
1123 err = ext4_htree_store_dirent(dir_file, 2, 0,
1124 de, &tmp_str);
1125 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 goto errout;
1127 count++;
1128 }
1129
1130 while (1) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -04001131 if (fatal_signal_pending(current)) {
1132 err = -ERESTARTSYS;
1133 goto errout;
1134 }
1135 cond_resched();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 block = dx_get_block(frame->at);
1137 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1138 start_hash, start_minor_hash);
1139 if (ret < 0) {
1140 err = ret;
1141 goto errout;
1142 }
1143 count += ret;
1144 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001145 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 frame, frames, &hashval);
1147 *next_hash = hashval;
1148 if (ret < 0) {
1149 err = ret;
1150 goto errout;
1151 }
1152 /*
1153 * Stop if: (a) there are no more entries, or
1154 * (b) we have inserted at least one entry and the
1155 * next hash value is not a continuation
1156 */
1157 if ((ret == 0) ||
1158 (count && ((hashval & 1) == 0)))
1159 break;
1160 }
1161 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001162 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1163 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 return count;
1165errout:
1166 dx_release(frames);
1167 return (err);
1168}
1169
Tao Ma7335cd32012-12-10 14:05:59 -05001170static inline int search_dirblock(struct buffer_head *bh,
1171 struct inode *dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001172 struct ext4_filename *fname,
Tao Ma7335cd32012-12-10 14:05:59 -05001173 unsigned int offset,
1174 struct ext4_dir_entry_2 **res_dir)
1175{
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001176 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
Eric Biggersd6b97552017-05-24 18:10:49 -04001177 fname, offset, res_dir);
Tao Ma7335cd32012-12-10 14:05:59 -05001178}
1179
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180/*
1181 * Directory block splitting, compacting
1182 */
1183
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001184/*
1185 * Create map of hash values, offsets, and sizes, stored at end of block.
1186 * Returns number of entries mapped.
1187 */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001188static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
1189 unsigned blocksize, struct dx_hash_info *hinfo,
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001190 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191{
1192 int count = 0;
1193 char *base = (char *) de;
1194 struct dx_hash_info h = *hinfo;
1195
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001196 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001198 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 map_tail--;
1200 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001201 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001202 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 count++;
1204 cond_resched();
1205 }
1206 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001207 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 }
1209 return count;
1210}
1211
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001212/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1214{
Andrew Morton63f57932006-10-11 01:21:24 -07001215 struct dx_map_entry *p, *q, *top = map + count - 1;
1216 int more;
1217 /* Combsort until bubble sort doesn't suck */
1218 while (count > 2) {
1219 count = count*10/13;
1220 if (count - 9 < 2) /* 9, 10 -> 11 */
1221 count = 11;
1222 for (p = top, q = p - count; q >= map; p--, q--)
1223 if (p->hash < q->hash)
1224 swap(*p, *q);
1225 }
1226 /* Garden variety bubble sort */
1227 do {
1228 more = 0;
1229 q = top;
1230 while (q-- > map) {
1231 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001233 swap(*(q+1), *q);
1234 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 }
1236 } while(more);
1237}
1238
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001239static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001240{
1241 struct dx_entry *entries = frame->entries;
1242 struct dx_entry *old = frame->at, *new = old + 1;
1243 int count = dx_get_count(entries);
1244
1245 assert(count < dx_get_limit(entries));
1246 assert(old < entries + count);
1247 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1248 dx_set_hash(new, hash);
1249 dx_set_block(new, block);
1250 dx_set_count(entries, count + 1);
1251}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253/*
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001254 * Test whether a directory entry matches the filename being searched for.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 *
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001256 * Return: %true if the directory entry matches, otherwise %false.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 */
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001258static inline bool ext4_match(const struct ext4_filename *fname,
1259 const struct ext4_dir_entry_2 *de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260{
Eric Biggers067d1022017-04-24 10:00:11 -07001261 struct fscrypt_name f;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001262
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 if (!de->inode)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001264 return false;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001265
Eric Biggers067d1022017-04-24 10:00:11 -07001266 f.usr_fname = fname->usr_fname;
1267 f.disk_name = fname->disk_name;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001268#ifdef CONFIG_EXT4_FS_ENCRYPTION
Eric Biggers067d1022017-04-24 10:00:11 -07001269 f.crypto_buf = fname->crypto_buf;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001270#endif
Eric Biggers067d1022017-04-24 10:00:11 -07001271 return fscrypt_match_name(&f, de->name, de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272}
1273
1274/*
1275 * Returns 0 if not found, -1 on failure, and 1 on success
1276 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001277int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1278 struct inode *dir, struct ext4_filename *fname,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001279 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280{
Mingming Cao617ba132006-10-11 01:20:53 -07001281 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 char * dlimit;
1283 int de_len;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001284
Tao Ma7335cd32012-12-10 14:05:59 -05001285 de = (struct ext4_dir_entry_2 *)search_buf;
1286 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 while ((char *) de < dlimit) {
1288 /* this code is executed quadratically often */
1289 /* do minimal checking `by hand' */
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001290 if ((char *) de + de->name_len <= dlimit &&
1291 ext4_match(fname, de)) {
1292 /* found a match - just to be sure, do
1293 * a full check */
1294 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1295 bh->b_size, offset))
1296 return -1;
1297 *res_dir = de;
1298 return 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001299 }
1300 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001301 de_len = ext4_rec_len_from_disk(de->rec_len,
1302 dir->i_sb->s_blocksize);
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001303 if (de_len <= 0)
1304 return -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001306 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 }
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001308 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309}
1310
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001311static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1312 struct ext4_dir_entry *de)
1313{
1314 struct super_block *sb = dir->i_sb;
1315
1316 if (!is_dx(dir))
1317 return 0;
1318 if (block == 0)
1319 return 1;
1320 if (de->inode == 0 &&
1321 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1322 sb->s_blocksize)
1323 return 1;
1324 return 0;
1325}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326
1327/*
Mingming Cao617ba132006-10-11 01:20:53 -07001328 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001329 *
1330 * finds an entry in the specified directory with the wanted name. It
1331 * returns the cache buffer in which the entry was found, and the entry
1332 * itself (as a parameter - res_dir). It does NOT read the inode of the
1333 * entry - you'll have to do that yourself if you want to.
1334 *
1335 * The returned buffer_head has ->b_count elevated. The caller is expected
1336 * to brelse() it when appropriate.
1337 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001338static struct buffer_head * ext4_find_entry (struct inode *dir,
1339 const struct qstr *d_name,
Tao Ma32f7f222012-12-10 14:06:01 -05001340 struct ext4_dir_entry_2 **res_dir,
1341 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001343 struct super_block *sb;
1344 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1345 struct buffer_head *bh, *ret = NULL;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001346 ext4_lblk_t start, block;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001347 const u8 *name = d_name->name;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001348 size_t ra_max = 0; /* Number of bh's in the readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 buffer, bh_use[] */
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001350 size_t ra_ptr = 0; /* Current index into readahead
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 buffer */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001352 ext4_lblk_t nblocks;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001353 int i, namelen, retval;
1354 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001355
1356 *res_dir = NULL;
1357 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001358 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001359 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001360 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001361
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001362 retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
Eric Biggers54475f52016-12-05 11:12:44 -08001363 if (retval == -ENOENT)
1364 return NULL;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001365 if (retval)
1366 return ERR_PTR(retval);
1367
Tao Mae8e948e2012-12-10 14:06:00 -05001368 if (ext4_has_inline_data(dir)) {
1369 int has_inline_data = 1;
Eric Biggersd6b97552017-05-24 18:10:49 -04001370 ret = ext4_find_inline_entry(dir, &fname, res_dir,
Tao Mae8e948e2012-12-10 14:06:00 -05001371 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001372 if (has_inline_data) {
1373 if (inlined)
1374 *inlined = 1;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001375 goto cleanup_and_exit;
Tao Ma32f7f222012-12-10 14:06:01 -05001376 }
Tao Mae8e948e2012-12-10 14:06:00 -05001377 }
1378
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001379 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001380 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001381 /*
1382 * "." or ".." will only be in the first block
1383 * NFS may look up ".."; "." should be handled by the VFS
1384 */
1385 block = start = 0;
1386 nblocks = 1;
1387 goto restart;
1388 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389 if (is_dx(dir)) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001390 ret = ext4_dx_find_entry(dir, &fname, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391 /*
1392 * On success, or if the error was file not found,
1393 * return. Otherwise, fall back to doing a search the
1394 * old fashioned way.
1395 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001396 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1397 goto cleanup_and_exit;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001398 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1399 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 }
Mingming Cao617ba132006-10-11 01:20:53 -07001401 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1402 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 if (start >= nblocks)
1404 start = 0;
1405 block = start;
1406restart:
1407 do {
1408 /*
1409 * We deal with the read-ahead logic here.
1410 */
1411 if (ra_ptr >= ra_max) {
1412 /* Refill the readahead buffer */
1413 ra_ptr = 0;
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -04001414 if (block < start)
1415 ra_max = start - block;
1416 else
1417 ra_max = nblocks - block;
1418 ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1419 retval = ext4_bread_batch(dir, block, ra_max,
1420 false /* wait */, bh_use);
1421 if (retval) {
1422 ret = ERR_PTR(retval);
1423 ra_max = 0;
1424 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 }
1426 }
1427 if ((bh = bh_use[ra_ptr++]) == NULL)
1428 goto next;
1429 wait_on_buffer(bh);
1430 if (!buffer_uptodate(bh)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001431 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1432 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 brelse(bh);
Khazhismel Kumykov6febe6f2017-06-23 00:29:05 -04001434 ret = ERR_PTR(-EIO);
1435 goto cleanup_and_exit;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001437 if (!buffer_verified(bh) &&
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001438 !is_dx_internal_node(dir, block,
1439 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001440 !ext4_dirent_csum_verify(dir,
1441 (struct ext4_dir_entry *)bh->b_data)) {
1442 EXT4_ERROR_INODE(dir, "checksumming directory "
1443 "block %lu", (unsigned long)block);
1444 brelse(bh);
Theodore Ts'obdddf342017-06-23 00:47:05 -04001445 ret = ERR_PTR(-EFSBADCRC);
1446 goto cleanup_and_exit;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001447 }
1448 set_buffer_verified(bh);
Eric Biggersd6b97552017-05-24 18:10:49 -04001449 i = search_dirblock(bh, dir, &fname,
Mingming Cao617ba132006-10-11 01:20:53 -07001450 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001452 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 ret = bh;
1454 goto cleanup_and_exit;
1455 } else {
1456 brelse(bh);
1457 if (i < 0)
1458 goto cleanup_and_exit;
1459 }
1460 next:
1461 if (++block >= nblocks)
1462 block = 0;
1463 } while (block != start);
1464
1465 /*
1466 * If the directory has grown while we were searching, then
1467 * search the last part of the directory before giving up.
1468 */
1469 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001470 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 if (block < nblocks) {
1472 start = 0;
1473 goto restart;
1474 }
1475
1476cleanup_and_exit:
1477 /* Clean up the read-ahead blocks */
1478 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001479 brelse(bh_use[ra_ptr]);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001480 ext4_fname_free_filename(&fname);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001481 return ret;
1482}
1483
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001484static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1485 struct ext4_filename *fname,
1486 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001488 struct super_block * sb = dir->i_sb;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001489 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001491 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001494#ifdef CONFIG_EXT4_FS_ENCRYPTION
1495 *res_dir = NULL;
1496#endif
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001497 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001498 if (IS_ERR(frame))
1499 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 do {
1501 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001502 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001503 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001505
Eric Biggersd6b97552017-05-24 18:10:49 -04001506 retval = search_dirblock(bh, dir, fname,
Theodore Ts'o7845c042010-10-27 21:30:08 -04001507 block << EXT4_BLOCK_SIZE_BITS(sb),
1508 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001509 if (retval == 1)
1510 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001511 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001512 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001513 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001514 goto errout;
1515 }
1516
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001517 /* Check to see if we should continue to search */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001518 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001519 frames, NULL);
1520 if (retval < 0) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04001521 ext4_warning_inode(dir,
1522 "error %d reading directory index block",
1523 retval);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001524 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001525 goto errout;
1526 }
1527 } while (retval == 1);
1528
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001529 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530errout:
Eric Biggersd6b97552017-05-24 18:10:49 -04001531 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001532success:
1533 dx_release(frames);
1534 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536
Al Viro00cd8dd2012-06-10 17:13:09 -04001537static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001539 struct inode *inode;
1540 struct ext4_dir_entry_2 *de;
1541 struct buffer_head *bh;
Eric Biggers89904272017-10-18 20:21:58 -04001542 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543
Eric Biggers89904272017-10-18 20:21:58 -04001544 err = fscrypt_prepare_lookup(dir, dentry, flags);
1545 if (err)
1546 return ERR_PTR(err);
Theodore Ts'o28b4c262016-02-07 19:35:05 -05001547
Eric Biggers89904272017-10-18 20:21:58 -04001548 if (dentry->d_name.len > EXT4_NAME_LEN)
1549 return ERR_PTR(-ENAMETOOLONG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550
Tao Ma32f7f222012-12-10 14:06:01 -05001551 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001552 if (IS_ERR(bh))
1553 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 inode = NULL;
1555 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001556 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001557 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001558 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001559 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001560 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001561 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001562 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001563 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1564 dentry);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001565 return ERR_PTR(-EFSCORRUPTED);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001566 }
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001567 inode = ext4_iget_normal(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001568 if (inode == ERR_PTR(-ESTALE)) {
1569 EXT4_ERROR_INODE(dir,
1570 "deleted inode referenced: %u",
1571 ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001572 return ERR_PTR(-EFSCORRUPTED);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001573 }
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001574 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
Theodore Ts'off978b02016-02-08 00:54:26 -05001575 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001576 !fscrypt_has_permitted_context(dir, inode)) {
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001577 ext4_warning(inode->i_sb,
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04001578 "Inconsistent encryption contexts: %lu/%lu",
Eric Biggers8c680842017-04-07 10:58:38 -07001579 dir->i_ino, inode->i_ino);
Eric Biggersdd01b692017-02-01 21:07:11 -05001580 iput(inode);
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001581 return ERR_PTR(-EPERM);
1582 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 }
1584 return d_splice_alias(inode, dentry);
1585}
1586
1587
Mingming Cao617ba132006-10-11 01:20:53 -07001588struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001590 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001591 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001592 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 struct buffer_head *bh;
1594
David Howells2b0143b2015-03-17 22:25:59 +00001595 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001596 if (IS_ERR(bh))
1597 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598 if (!bh)
1599 return ERR_PTR(-ENOENT);
1600 ino = le32_to_cpu(de->inode);
1601 brelse(bh);
1602
Al Virofc640052016-04-10 01:33:30 -04001603 if (!ext4_valid_inum(child->d_sb, ino)) {
David Howells2b0143b2015-03-17 22:25:59 +00001604 EXT4_ERROR_INODE(d_inode(child),
Theodore Ts'o24676da2010-05-16 21:00:00 -04001605 "bad parent inode number: %u", ino);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001606 return ERR_PTR(-EFSCORRUPTED);
Vasily Averina6c15c22007-07-15 23:40:46 -07001607 }
1608
Al Virofc640052016-04-10 01:33:30 -04001609 return d_obtain_alias(ext4_iget_normal(child->d_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610}
1611
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001612/*
1613 * Move count entries from end of map between two memory locations.
1614 * Returns pointer to last entry moved.
1615 */
Mingming Cao617ba132006-10-11 01:20:53 -07001616static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001617dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1618 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619{
1620 unsigned rec_len = 0;
1621
1622 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001623 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001624 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001625 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001627 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001628 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 de->inode = 0;
1630 map++;
1631 to += rec_len;
1632 }
Mingming Cao617ba132006-10-11 01:20:53 -07001633 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634}
1635
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001636/*
1637 * Compact each dir entry in the range to the minimal rec_len.
1638 * Returns pointer to last entry in range.
1639 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001640static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001641{
Mingming Cao617ba132006-10-11 01:20:53 -07001642 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 unsigned rec_len = 0;
1644
1645 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001646 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001647 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001649 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650 if (de > to)
1651 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001652 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001654 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001655 }
1656 de = next;
1657 }
1658 return prev;
1659}
1660
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001661/*
1662 * Split a full leaf block to make room for a new dir entry.
1663 * Allocate a new block, and move entries so that they are approx. equally full.
1664 * Returns pointer to de in block into which the new entry will be inserted.
1665 */
Mingming Cao617ba132006-10-11 01:20:53 -07001666static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001668 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669{
1670 unsigned blocksize = dir->i_sb->s_blocksize;
1671 unsigned count, continued;
1672 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001673 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674 u32 hash2;
1675 struct dx_map_entry *map;
1676 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001677 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001678 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001679 struct ext4_dir_entry_tail *t;
1680 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001681 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001682
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001683 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001684 csum_size = sizeof(struct ext4_dir_entry_tail);
1685
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001686 bh2 = ext4_append(handle, dir, &newblock);
1687 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688 brelse(*bh);
1689 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001690 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 }
1692
1693 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001694 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001695 if (err)
1696 goto journal_error;
1697
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001699 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 if (err)
1701 goto journal_error;
1702
1703 data2 = bh2->b_data;
1704
1705 /* create map in the end of data2 block */
1706 map = (struct dx_map_entry *) (data2 + blocksize);
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001707 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001708 blocksize, hinfo, map);
1709 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001710 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001711 /* Split the existing block in the middle, size-wise */
1712 size = 0;
1713 move = 0;
1714 for (i = count-1; i >= 0; i--) {
1715 /* is more than half of this entry in 2nd half of the block? */
1716 if (size + map[i].size/2 > blocksize/2)
1717 break;
1718 size += map[i].size;
1719 move++;
1720 }
1721 /* map index at which we will split */
1722 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 hash2 = map[split].hash;
1724 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001725 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1726 (unsigned long)dx_get_block(frame->at),
1727 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728
1729 /* Fancy dance to stay within two buffers */
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001730 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1731 blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001732 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001733 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1734 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001735 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001736 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1737 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001738 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001739 if (csum_size) {
1740 t = EXT4_DIRENT_TAIL(data2, blocksize);
1741 initialize_dirent_tail(t, blocksize);
1742
1743 t = EXT4_DIRENT_TAIL(data1, blocksize);
1744 initialize_dirent_tail(t, blocksize);
1745 }
1746
Michael Halcrowb3098482015-04-12 01:07:01 -04001747 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1748 blocksize, 1));
1749 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1750 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751
1752 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001753 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001754 swap(*bh, bh2);
1755 de = de2;
1756 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001757 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001758 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759 if (err)
1760 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001761 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762 if (err)
1763 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001764 brelse(bh2);
1765 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001766 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001767
1768journal_error:
1769 brelse(*bh);
1770 brelse(bh2);
1771 *bh = NULL;
1772 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001773 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001775
Tao Ma978fef92012-12-10 14:05:58 -05001776int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1777 struct buffer_head *bh,
1778 void *buf, int buf_size,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001779 struct ext4_filename *fname,
Tao Ma978fef92012-12-10 14:05:58 -05001780 struct ext4_dir_entry_2 **dest_de)
1781{
1782 struct ext4_dir_entry_2 *de;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001783 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001784 int nlen, rlen;
1785 unsigned int offset = 0;
1786 char *top;
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001787
Tao Ma978fef92012-12-10 14:05:58 -05001788 de = (struct ext4_dir_entry_2 *)buf;
1789 top = buf + buf_size - reclen;
1790 while ((char *) de <= top) {
1791 if (ext4_check_dir_entry(dir, NULL, de, bh,
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001792 buf, buf_size, offset))
1793 return -EFSCORRUPTED;
1794 if (ext4_match(fname, de))
1795 return -EEXIST;
Tao Ma978fef92012-12-10 14:05:58 -05001796 nlen = EXT4_DIR_REC_LEN(de->name_len);
1797 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1798 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1799 break;
1800 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1801 offset += rlen;
1802 }
Michael Halcrow1f3862b2015-04-12 01:09:03 -04001803 if ((char *) de > top)
Eric Biggersd9b9f8d2017-04-24 10:00:13 -07001804 return -ENOSPC;
1805
1806 *dest_de = de;
1807 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001808}
1809
Eric Biggers1bc0af62017-04-29 23:27:26 -04001810void ext4_insert_dentry(struct inode *inode,
1811 struct ext4_dir_entry_2 *de,
1812 int buf_size,
1813 struct ext4_filename *fname)
Tao Ma978fef92012-12-10 14:05:58 -05001814{
1815
1816 int nlen, rlen;
1817
1818 nlen = EXT4_DIR_REC_LEN(de->name_len);
1819 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1820 if (de->inode) {
1821 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001822 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001823 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1824 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1825 de = de1;
1826 }
1827 de->file_type = EXT4_FT_UNKNOWN;
1828 de->inode = cpu_to_le32(inode->i_ino);
1829 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001830 de->name_len = fname_len(fname);
1831 memcpy(de->name, fname_name(fname), fname_len(fname));
Tao Ma978fef92012-12-10 14:05:58 -05001832}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001833
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834/*
1835 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1836 * it points to a directory entry which is guaranteed to be large
1837 * enough for new directory entry. If de is NULL, then
1838 * add_dirent_to_buf will attempt search the directory block for
1839 * space. It will return -ENOSPC if no space is available, and -EIO
1840 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001841 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001842static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
1843 struct inode *dir,
Mingming Cao617ba132006-10-11 01:20:53 -07001844 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001845 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846{
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001847 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001848 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001849 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001850
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001851 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001852 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001853
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 if (!de) {
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001855 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
1856 blocksize - csum_size, fname, &de);
Tao Ma978fef92012-12-10 14:05:58 -05001857 if (err)
1858 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001859 }
1860 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001861 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001863 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001864 return err;
1865 }
1866
Eric Biggers1bc0af62017-04-29 23:27:26 -04001867 /* By now the buffer is marked for journaling */
1868 ext4_insert_dentry(inode, de, blocksize, fname);
Tao Ma978fef92012-12-10 14:05:58 -05001869
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001870 /*
1871 * XXX shouldn't update any times until successful
1872 * completion of syscall, but too many callers depend
1873 * on this.
1874 *
1875 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001876 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 * recovery deletes the inode, so the worst that can
1878 * happen is that the times are slightly out of date
1879 * and/or different from the directory change time.
1880 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05001881 dir->i_mtime = dir->i_ctime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001882 ext4_update_dx_flag(dir);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001883 inode_inc_iversion(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001884 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001885 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001886 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001887 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001888 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001889 return 0;
1890}
1891
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001892/*
1893 * This converts a one block unindexed directory to a 3 block indexed
1894 * directory, and adds the dentry to the indexed directory.
1895 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001896static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05001897 struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001898 struct inode *inode, struct buffer_head *bh)
1899{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 struct buffer_head *bh2;
1901 struct dx_root *root;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04001902 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001903 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001904 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001905 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001906 char *data1, *top;
1907 unsigned len;
1908 int retval;
1909 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001910 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911 struct fake_dirent *fde;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001912 int csum_size = 0;
1913
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04001914 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001915 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916
1917 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001918 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04001919 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001920 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001921 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001922 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001923 brelse(bh);
1924 return retval;
1925 }
1926 root = (struct dx_root *) bh->b_data;
1927
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001928 /* The 0th block becomes the root, move the dirents out */
1929 fde = &root->dotdot;
1930 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001931 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001932 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001933 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001934 brelse(bh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001935 return -EFSCORRUPTED;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001936 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001937 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001938
1939 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001940 bh2 = ext4_append(handle, dir, &block);
1941 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001942 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001943 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001945 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946 data1 = bh2->b_data;
1947
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001948 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001949 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001951 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001953 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1954 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001955 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001956
1957 if (csum_size) {
1958 t = EXT4_DIRENT_TAIL(data1, blocksize);
1959 initialize_dirent_tail(t, blocksize);
1960 }
1961
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001963 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001964 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1965 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966 memset (&root->info, 0, sizeof(root->info));
1967 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001968 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001970 dx_set_block(entries, 1);
1971 dx_set_count(entries, 1);
1972 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973
1974 /* Initialize as for dx_probe */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04001975 fname->hinfo.hash_version = root->info.hash_version;
1976 if (fname->hinfo.hash_version <= DX_HASH_TEA)
1977 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1978 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1979 ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
1980
Jan Kara6050d472014-10-30 10:53:17 -04001981 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001982 frame = frames;
1983 frame->entries = entries;
1984 frame->at = entries;
1985 frame->bh = bh;
Allison Henderson6976a6f2011-05-15 00:19:41 -04001986
Jan Kara6050d472014-10-30 10:53:17 -04001987 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1988 if (retval)
1989 goto out_frames;
gmaile81d4472016-09-30 01:33:37 -04001990 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04001991 if (retval)
1992 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04001993
gmaile81d4472016-09-30 01:33:37 -04001994 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001995 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04001996 retval = PTR_ERR(de);
1997 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001998 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999
gmaile81d4472016-09-30 01:33:37 -04002000 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002001out_frames:
2002 /*
2003 * Even if the block split failed, we have to properly write
2004 * out all the changes we did so far. Otherwise we can end up
2005 * with corrupted filesystem.
2006 */
gmaile81d4472016-09-30 01:33:37 -04002007 if (retval)
2008 ext4_mark_inode_dirty(handle, dir);
Jan Kara6050d472014-10-30 10:53:17 -04002009 dx_release(frames);
gmaile81d4472016-09-30 01:33:37 -04002010 brelse(bh2);
Jan Kara6050d472014-10-30 10:53:17 -04002011 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013
2014/*
Mingming Cao617ba132006-10-11 01:20:53 -07002015 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 *
2017 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002018 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019 *
2020 * NOTE!! The inode part of 'de' is left at 0 - which means you
2021 * may not sleep between calling this and putting something into
2022 * the entry, as someone else might have used it while you slept.
2023 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002024static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2025 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026{
David Howells2b0143b2015-03-17 22:25:59 +00002027 struct inode *dir = d_inode(dentry->d_parent);
Lukas Czernere12fb972015-04-03 10:46:58 -04002028 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002029 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002030 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002031 struct super_block *sb;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002032 struct ext4_filename fname;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002033 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002035 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002036 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002037 int csum_size = 0;
2038
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002039 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002040 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041
2042 sb = dir->i_sb;
2043 blocksize = sb->s_blocksize;
2044 if (!dentry->d_name.len)
2045 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002046
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002047 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2048 if (retval)
2049 return retval;
2050
Tao Ma3c47d542012-12-10 14:05:59 -05002051 if (ext4_has_inline_data(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002052 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
Tao Ma3c47d542012-12-10 14:05:59 -05002053 if (retval < 0)
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002054 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002055 if (retval == 1) {
2056 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002057 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002058 }
2059 }
2060
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002061 if (is_dx(dir)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002062 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002063 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002064 goto out;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002065 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002067 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002068 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002069 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002070 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002071 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002072 if (IS_ERR(bh)) {
2073 retval = PTR_ERR(bh);
2074 bh = NULL;
2075 goto out;
2076 }
2077 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2078 NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002079 if (retval != -ENOSPC)
2080 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 if (blocks == 1 && !dx_fallback &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002083 ext4_has_feature_dir_index(sb)) {
Theodore Ts'o56a04912016-01-08 16:00:31 -05002084 retval = make_indexed_dir(handle, &fname, dir,
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002085 inode, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002086 bh = NULL; /* make_indexed_dir releases bh */
2087 goto out;
2088 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002089 brelse(bh);
2090 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002091 bh = ext4_append(handle, dir, &block);
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002092 if (IS_ERR(bh)) {
2093 retval = PTR_ERR(bh);
2094 bh = NULL;
2095 goto out;
2096 }
Mingming Cao617ba132006-10-11 01:20:53 -07002097 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002099 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2100
2101 if (csum_size) {
2102 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2103 initialize_dirent_tail(t, blocksize);
2104 }
2105
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002106 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002107out:
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002108 ext4_fname_free_filename(&fname);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002109 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002110 if (retval == 0)
2111 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002112 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002113}
2114
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115/*
2116 * Returns 0 for success, or a negative error value
2117 */
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002118static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
Theodore Ts'o56a04912016-01-08 16:00:31 -05002119 struct inode *dir, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120{
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002121 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 struct dx_entry *entries, *at;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002123 struct buffer_head *bh;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002124 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002125 struct ext4_dir_entry_2 *de;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002126 int restart;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 int err;
2128
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002129again:
2130 restart = 0;
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002131 frame = dx_probe(fname, dir, NULL, frames);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002132 if (IS_ERR(frame))
2133 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 entries = frame->entries;
2135 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002136 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2137 if (IS_ERR(bh)) {
2138 err = PTR_ERR(bh);
2139 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002141 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002142
2143 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002144 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002145 if (err)
2146 goto journal_error;
2147
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002148 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002149 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002152 err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002153 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002154 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002155 dx_get_count(entries), dx_get_limit(entries)));
2156 /* Need to split index? */
2157 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002158 ext4_lblk_t newblock;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002159 int levels = frame - frames + 1;
2160 unsigned int icount;
2161 int add_level = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162 struct dx_entry *entries2;
2163 struct dx_node *node2;
2164 struct buffer_head *bh2;
2165
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002166 while (frame > frames) {
2167 if (dx_get_count((frame - 1)->entries) <
2168 dx_get_limit((frame - 1)->entries)) {
2169 add_level = 0;
2170 break;
2171 }
2172 frame--; /* split higher index block */
2173 at = frame->at;
2174 entries = frame->entries;
2175 restart = 1;
2176 }
2177 if (add_level && levels == ext4_dir_htree_level(sb)) {
2178 ext4_warning(sb, "Directory (ino: %lu) index full, "
2179 "reach max htree level :%d",
2180 dir->i_ino, levels);
2181 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2182 ext4_warning(sb, "Large directory feature is "
2183 "not enabled on this "
2184 "filesystem");
2185 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186 err = -ENOSPC;
2187 goto cleanup;
2188 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002189 icount = dx_get_count(entries);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002190 bh2 = ext4_append(handle, dir, &newblock);
2191 if (IS_ERR(bh2)) {
2192 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002194 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195 node2 = (struct dx_node *)(bh2->b_data);
2196 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002197 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002198 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2199 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002200 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002201 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002202 if (err)
2203 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002204 if (!add_level) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205 unsigned icount1 = icount/2, icount2 = icount - icount1;
2206 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002207 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2208 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002209
2210 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002211 err = ext4_journal_get_write_access(handle,
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002212 (frame - 1)->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 if (err)
2214 goto journal_error;
2215
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002216 memcpy((char *) entries2, (char *) (entries + icount1),
2217 icount2 * sizeof(struct dx_entry));
2218 dx_set_count(entries, icount1);
2219 dx_set_count(entries2, icount2);
2220 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002221
2222 /* Which index block gets the new entry? */
2223 if (at - entries >= icount1) {
2224 frame->at = at = at - entries - icount1 + entries2;
2225 frame->entries = entries = entries2;
2226 swap(frame->bh, bh2);
2227 }
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002228 dx_insert_block((frame - 1), hash2, newblock);
2229 dxtrace(dx_show_index("node", frame->entries));
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002230 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002231 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002232 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 if (err)
2234 goto journal_error;
2235 brelse (bh2);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002236 err = ext4_handle_dirty_dx_node(handle, dir,
2237 (frame - 1)->bh);
2238 if (err)
2239 goto journal_error;
2240 if (restart) {
2241 err = ext4_handle_dirty_dx_node(handle, dir,
2242 frame->bh);
2243 goto journal_error;
2244 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002245 } else {
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002246 struct dx_root *dxroot;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002247 memcpy((char *) entries2, (char *) entries,
2248 icount * sizeof(struct dx_entry));
2249 dx_set_limit(entries2, dx_node_limit(dir));
2250
2251 /* Set up root */
2252 dx_set_count(entries, 1);
2253 dx_set_block(entries + 0, newblock);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002254 dxroot = (struct dx_root *)frames[0].bh->b_data;
2255 dxroot->info.indirect_levels += 1;
2256 dxtrace(printk(KERN_DEBUG
2257 "Creating %d level index...\n",
2258 info->indirect_levels));
2259 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 if (err)
2261 goto journal_error;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002262 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2263 brelse(bh2);
2264 restart = 1;
2265 goto journal_error;
Theodore Ts'ob4097142011-01-10 12:46:59 -05002266 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002268 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002269 if (IS_ERR(de)) {
2270 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002272 }
Theodore Ts'o5b643f92015-05-18 13:14:47 -04002273 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 goto cleanup;
2275
2276journal_error:
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002277 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002278cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002279 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280 dx_release(frames);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002281 /* @restart is true means htree-path has been changed, we need to
2282 * repeat dx_probe() to find out valid htree-path
2283 */
2284 if (restart && err == 0)
2285 goto again;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 return err;
2287}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288
2289/*
Tao Ma05019a92012-12-10 14:06:00 -05002290 * ext4_generic_delete_entry deletes a directory entry by merging it
2291 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002292 */
Tao Ma05019a92012-12-10 14:06:00 -05002293int ext4_generic_delete_entry(handle_t *handle,
2294 struct inode *dir,
2295 struct ext4_dir_entry_2 *de_del,
2296 struct buffer_head *bh,
2297 void *entry_buf,
2298 int buf_size,
2299 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002300{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002301 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002302 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002303 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002304
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 i = 0;
2306 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002307 de = (struct ext4_dir_entry_2 *)entry_buf;
2308 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002309 if (ext4_check_dir_entry(dir, NULL, de, bh,
2310 bh->b_data, bh->b_size, i))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002311 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002312 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002314 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002315 ext4_rec_len_from_disk(pde->rec_len,
2316 blocksize) +
2317 ext4_rec_len_from_disk(de->rec_len,
2318 blocksize),
2319 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320 else
2321 de->inode = 0;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04002322 inode_inc_iversion(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002323 return 0;
2324 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002325 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002326 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002327 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328 }
2329 return -ENOENT;
2330}
2331
Tao Ma05019a92012-12-10 14:06:00 -05002332static int ext4_delete_entry(handle_t *handle,
2333 struct inode *dir,
2334 struct ext4_dir_entry_2 *de_del,
2335 struct buffer_head *bh)
2336{
2337 int err, csum_size = 0;
2338
Tao Ma9f40fe52012-12-10 14:06:00 -05002339 if (ext4_has_inline_data(dir)) {
2340 int has_inline_data = 1;
2341 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2342 &has_inline_data);
2343 if (has_inline_data)
2344 return err;
2345 }
2346
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002347 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002348 csum_size = sizeof(struct ext4_dir_entry_tail);
2349
2350 BUFFER_TRACE(bh, "get_write_access");
2351 err = ext4_journal_get_write_access(handle, bh);
2352 if (unlikely(err))
2353 goto out;
2354
2355 err = ext4_generic_delete_entry(handle, dir, de_del,
2356 bh, bh->b_data,
2357 dir->i_sb->s_blocksize, csum_size);
2358 if (err)
2359 goto out;
2360
2361 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2362 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2363 if (unlikely(err))
2364 goto out;
2365
2366 return 0;
2367out:
2368 if (err != -ENOENT)
2369 ext4_std_error(dir->i_sb, err);
2370 return err;
2371}
2372
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002373/*
Andreas Dilgerc7414892017-08-05 19:47:34 -04002374 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2375 * since this indicates that nlinks count was previously 1 to avoid overflowing
2376 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2377 * that subdirectory link counts are not being maintained accurately.
2378 *
2379 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2380 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2381 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2382 * on regular files) and to avoid creating huge/slow non-HTREE directories.
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002383 */
2384static void ext4_inc_count(handle_t *handle, struct inode *inode)
2385{
2386 inc_nlink(inode);
Andreas Dilgerc7414892017-08-05 19:47:34 -04002387 if (is_dx(inode) &&
2388 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2389 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002390}
2391
2392/*
2393 * If a directory had nlink == 1, then we should let it be 1. This indicates
2394 * directory has >EXT4_LINK_MAX subdirs.
2395 */
2396static void ext4_dec_count(handle_t *handle, struct inode *inode)
2397{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002398 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2399 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002400}
2401
2402
Mingming Cao617ba132006-10-11 01:20:53 -07002403static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002404 struct dentry *dentry, struct inode *inode)
2405{
Mingming Cao617ba132006-10-11 01:20:53 -07002406 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002407 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002408 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002409 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002410 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411 return 0;
2412 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002413 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002414 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002415 iput(inode);
2416 return err;
2417}
2418
2419/*
2420 * By the time this is called, we already have created
2421 * the directory cache entry for the new file, but it
2422 * is so far negative - it has no inode.
2423 *
2424 * If the create succeeds, we fill in the inode information
2425 * with d_instantiate().
2426 */
Al Viro4acdaf22011-07-26 01:42:34 -04002427static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002428 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002429{
2430 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002431 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002432 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002433
Jan Karaa7cdade2015-06-29 16:22:54 +02002434 err = dquot_initialize(dir);
2435 if (err)
2436 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002437
Theodore Ts'o11395752013-02-09 16:27:09 -05002438 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002439 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002441 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2442 NULL, EXT4_HT_DIR, credits);
2443 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002444 err = PTR_ERR(inode);
2445 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002446 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002447 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002448 ext4_set_aops(inode);
Theodore Ts'oe709e9d2015-05-31 13:35:02 -04002449 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002450 if (!err && IS_DIRSYNC(dir))
2451 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002452 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002453 if (handle)
2454 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002455 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002456 goto retry;
2457 return err;
2458}
2459
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002460static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002461 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462{
2463 handle_t *handle;
2464 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002465 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002466
Jan Karaa7cdade2015-06-29 16:22:54 +02002467 err = dquot_initialize(dir);
2468 if (err)
2469 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002470
Theodore Ts'o11395752013-02-09 16:27:09 -05002471 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002472 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002474 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2475 NULL, EXT4_HT_DIR, credits);
2476 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477 err = PTR_ERR(inode);
2478 if (!IS_ERR(inode)) {
2479 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002480 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002481 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002482 if (!err && IS_DIRSYNC(dir))
2483 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002485 if (handle)
2486 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002487 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002488 goto retry;
2489 return err;
2490}
2491
Al Viroaf51a2a2013-06-29 13:23:08 +04002492static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2493{
2494 handle_t *handle;
2495 struct inode *inode;
2496 int err, retries = 0;
2497
Jan Karaa7cdade2015-06-29 16:22:54 +02002498 err = dquot_initialize(dir);
2499 if (err)
2500 return err;
Al Viroaf51a2a2013-06-29 13:23:08 +04002501
2502retry:
2503 inode = ext4_new_inode_start_handle(dir, mode,
2504 NULL, 0, NULL,
2505 EXT4_HT_DIR,
2506 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2507 4 + EXT4_XATTR_TRANS_BLOCKS);
2508 handle = ext4_journal_current_handle();
2509 err = PTR_ERR(inode);
2510 if (!IS_ERR(inode)) {
2511 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07002512 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002513 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002514 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002515 err = ext4_orphan_add(handle, inode);
2516 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002517 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002518 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002519 unlock_new_inode(inode);
2520 }
2521 if (handle)
2522 ext4_journal_stop(handle);
2523 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2524 goto retry;
2525 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002526err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002527 ext4_journal_stop(handle);
2528 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002529 return err;
2530}
2531
Tao Maa774f9c2012-12-10 14:05:57 -05002532struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2533 struct ext4_dir_entry_2 *de,
2534 int blocksize, int csum_size,
2535 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002536{
Tao Maa774f9c2012-12-10 14:05:57 -05002537 de->inode = cpu_to_le32(inode->i_ino);
2538 de->name_len = 1;
2539 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2540 blocksize);
2541 strcpy(de->name, ".");
2542 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2543
2544 de = ext4_next_entry(de, blocksize);
2545 de->inode = cpu_to_le32(parent_ino);
2546 de->name_len = 2;
2547 if (!dotdot_real_len)
2548 de->rec_len = ext4_rec_len_to_disk(blocksize -
2549 (csum_size + EXT4_DIR_REC_LEN(1)),
2550 blocksize);
2551 else
2552 de->rec_len = ext4_rec_len_to_disk(
2553 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2554 strcpy(de->name, "..");
2555 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2556
2557 return ext4_next_entry(de, blocksize);
2558}
2559
2560static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2561 struct inode *inode)
2562{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002563 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002564 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002565 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002566 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002567 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002568 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002569 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002570
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002571 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002572 csum_size = sizeof(struct ext4_dir_entry_tail);
2573
Tao Ma3c47d542012-12-10 14:05:59 -05002574 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2575 err = ext4_try_create_inline_dir(handle, dir, inode);
2576 if (err < 0 && err != -ENOSPC)
2577 goto out;
2578 if (!err)
2579 goto out;
2580 }
2581
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002582 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002583 dir_block = ext4_append(handle, inode, &block);
2584 if (IS_ERR(dir_block))
2585 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002586 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2587 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2588 set_nlink(inode, 2);
2589 if (csum_size) {
2590 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2591 initialize_dirent_tail(t, blocksize);
2592 }
2593
2594 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2595 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2596 if (err)
2597 goto out;
2598 set_buffer_verified(dir_block);
2599out:
2600 brelse(dir_block);
2601 return err;
2602}
2603
2604static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2605{
2606 handle_t *handle;
2607 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002608 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002609
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002610 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611 return -EMLINK;
2612
Jan Karaa7cdade2015-06-29 16:22:54 +02002613 err = dquot_initialize(dir);
2614 if (err)
2615 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002616
Theodore Ts'o11395752013-02-09 16:27:09 -05002617 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002618 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002619retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002620 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2621 &dentry->d_name,
2622 0, NULL, EXT4_HT_DIR, credits);
2623 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002624 err = PTR_ERR(inode);
2625 if (IS_ERR(inode))
2626 goto out_stop;
2627
Mingming Cao617ba132006-10-11 01:20:53 -07002628 inode->i_op = &ext4_dir_inode_operations;
2629 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002630 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002631 if (err)
2632 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002633 err = ext4_mark_inode_dirty(handle, inode);
2634 if (!err)
2635 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002636 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002637out_clear_inode:
2638 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002639 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002640 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002641 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 goto out_stop;
2643 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002644 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002645 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002646 err = ext4_mark_inode_dirty(handle, dir);
2647 if (err)
2648 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002649 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002650 d_instantiate(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002651 if (IS_DIRSYNC(dir))
2652 ext4_handle_sync(handle);
2653
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002655 if (handle)
2656 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002657 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 goto retry;
2659 return err;
2660}
2661
2662/*
2663 * routine to check that the specified directory is empty (for rmdir)
2664 */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002665bool ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002666{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002667 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002668 struct buffer_head *bh;
2669 struct ext4_dir_entry_2 *de, *de1;
2670 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002671
Tao Ma61f86632012-12-10 14:06:01 -05002672 if (ext4_has_inline_data(inode)) {
2673 int has_inline_data = 1;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002674 int ret;
Tao Ma61f86632012-12-10 14:06:01 -05002675
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002676 ret = empty_inline_dir(inode, &has_inline_data);
Tao Ma61f86632012-12-10 14:06:01 -05002677 if (has_inline_data)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002678 return ret;
Tao Ma61f86632012-12-10 14:06:01 -05002679 }
2680
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002682 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2683 EXT4_ERROR_INODE(inode, "invalid size");
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002684 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002686 bh = ext4_read_dirblock(inode, 0, EITHER);
2687 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002688 return true;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002689
Mingming Cao617ba132006-10-11 01:20:53 -07002690 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002691 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002692 if (le32_to_cpu(de->inode) != inode->i_ino ||
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04002693 le32_to_cpu(de1->inode) == 0 ||
2694 strcmp(".", de->name) || strcmp("..", de1->name)) {
2695 ext4_warning_inode(inode, "directory missing '.' and/or '..'");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002696 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002697 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002698 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002699 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2700 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2701 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002702 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002703 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002704 unsigned int lblock;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002705 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002706 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002707 bh = ext4_read_dirblock(inode, lblock, EITHER);
2708 if (IS_ERR(bh))
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002709 return true;
Mingming Cao617ba132006-10-11 01:20:53 -07002710 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002711 }
Tao Ma226ba972012-12-10 14:05:58 -05002712 if (ext4_check_dir_entry(inode, NULL, de, bh,
2713 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002714 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002715 sb->s_blocksize);
2716 offset = (offset | (sb->s_blocksize - 1)) + 1;
2717 continue;
2718 }
2719 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002720 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002721 return false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002723 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2724 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002726 brelse(bh);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04002727 return true;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002728}
2729
Jan Karad745a8c2014-05-26 11:56:53 -04002730/*
2731 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002732 * such inodes, starting at the superblock, in case we crash before the
2733 * file is closed/deleted, or in case the inode truncate spans multiple
2734 * transactions and the last transaction is not recovered after a crash.
2735 *
2736 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002737 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002738 *
2739 * Orphan list manipulation functions must be called under i_mutex unless
2740 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002741 */
Mingming Cao617ba132006-10-11 01:20:53 -07002742int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743{
2744 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002745 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002746 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002747 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002748 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002749
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002750 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002751 return 0;
2752
Jan Karad745a8c2014-05-26 11:56:53 -04002753 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002754 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002755 /*
2756 * Exit early if inode already is on orphan list. This is a big speedup
2757 * since we don't have to contend on the global s_orphan_lock.
2758 */
Mingming Cao617ba132006-10-11 01:20:53 -07002759 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002760 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761
Lukas Czernerafb86172011-07-11 18:47:04 -04002762 /*
2763 * Orphan handling is only valid for files with data blocks
2764 * being truncated, or files being unlinked. Note that we either
2765 * hold i_mutex, or the inode can not be referenced from outside,
2766 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002767 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002768 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2769 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002770
Jan Karacd2c0802014-05-26 11:39:17 -04002771 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2772 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002773 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002774 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775
Mingming Cao617ba132006-10-11 01:20:53 -07002776 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002778 goto out;
2779
2780 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002781 /*
2782 * Due to previous errors inode may be already a part of on-disk
2783 * orphan list. If so skip on-disk list modification.
2784 */
Jan Karad745a8c2014-05-26 11:56:53 -04002785 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2786 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2787 /* Insert this inode at the head of the on-disk orphan list */
2788 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2789 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2790 dirty = true;
2791 }
2792 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2793 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002794
Jan Karad745a8c2014-05-26 11:56:53 -04002795 if (dirty) {
2796 err = ext4_handle_dirty_super(handle, sb);
2797 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2798 if (!err)
2799 err = rc;
2800 if (err) {
2801 /*
2802 * We have to remove inode from in-memory list if
2803 * addition to on disk orphan list failed. Stray orphan
2804 * list entries can cause panics at unmount time.
2805 */
2806 mutex_lock(&sbi->s_orphan_lock);
Jan Kara74177f552016-05-05 11:10:15 -04002807 list_del_init(&EXT4_I(inode)->i_orphan);
Jan Karad745a8c2014-05-26 11:56:53 -04002808 mutex_unlock(&sbi->s_orphan_lock);
2809 }
2810 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002811 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2812 jbd_debug(4, "orphan inode %lu will point to %d\n",
2813 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002814out:
Jan Karacd2c0802014-05-26 11:39:17 -04002815 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816 return err;
2817}
2818
2819/*
Mingming Cao617ba132006-10-11 01:20:53 -07002820 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002821 * of such inodes stored on disk, because it is finally being cleaned up.
2822 */
Mingming Cao617ba132006-10-11 01:20:53 -07002823int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824{
2825 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002826 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002827 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002828 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002829 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 int err = 0;
2831
Jan Karacd2c0802014-05-26 11:39:17 -04002832 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002833 return 0;
2834
Jan Karad745a8c2014-05-26 11:56:53 -04002835 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
Al Viro59551022016-01-22 15:40:57 -05002836 !inode_is_locked(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002837 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002838 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002839 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840
Jan Karad745a8c2014-05-26 11:56:53 -04002841 if (handle) {
2842 /* Grab inode buffer early before taking global s_orphan_lock */
2843 err = ext4_reserve_inode_write(handle, inode, &iloc);
2844 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845
Jan Karad745a8c2014-05-26 11:56:53 -04002846 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2848
Jan Karad745a8c2014-05-26 11:56:53 -04002849 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850 list_del_init(&ei->i_orphan);
2851
2852 /* If we're on an error path, we may not have a valid
2853 * transaction handle with which to update the orphan list on
2854 * disk, but we still need to remove the inode from the linked
2855 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002856 if (!handle || err) {
2857 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002859 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860
Jan Karad745a8c2014-05-26 11:56:53 -04002861 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002862 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002863 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002865 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04002866 if (err) {
2867 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002868 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002869 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002870 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04002871 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002872 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002874 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002875 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002876 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002878 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002879 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002880 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002881 if (err) {
2882 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002884 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002885 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002886 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002887 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002888 }
2889 if (err)
2890 goto out_brelse;
2891 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002892 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002894 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002895 return err;
2896
2897out_brelse:
2898 brelse(iloc.bh);
2899 goto out_err;
2900}
2901
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002902static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002903{
2904 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002905 struct inode *inode;
2906 struct buffer_head *bh;
2907 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002908 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002910 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
2911 return -EIO;
2912
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002913 /* Initialize quotas before so that eventual writes go in
2914 * separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02002915 retval = dquot_initialize(dir);
2916 if (retval)
2917 return retval;
2918 retval = dquot_initialize(d_inode(dentry));
2919 if (retval)
2920 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002921
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002922 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002923 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002924 if (IS_ERR(bh))
2925 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002926 if (!bh)
2927 goto end_rmdir;
2928
David Howells2b0143b2015-03-17 22:25:59 +00002929 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002930
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002931 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002932 if (le32_to_cpu(de->inode) != inode->i_ino)
2933 goto end_rmdir;
2934
2935 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002936 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 goto end_rmdir;
2938
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002939 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002940 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002941 if (IS_ERR(handle)) {
2942 retval = PTR_ERR(handle);
2943 handle = NULL;
2944 goto end_rmdir;
2945 }
2946
2947 if (IS_DIRSYNC(dir))
2948 ext4_handle_sync(handle);
2949
Mingming Cao617ba132006-10-11 01:20:53 -07002950 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 if (retval)
2952 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002953 if (!EXT4_DIR_LINK_EMPTY(inode))
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04002954 ext4_warning_inode(inode,
2955 "empty directory '%.*s' has too many links (%u)",
2956 dentry->d_name.len, dentry->d_name.name,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002957 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958 inode->i_version++;
2959 clear_nlink(inode);
2960 /* There's no need to set i_disksize: the fact that i_nlink is
2961 * zero will ensure that the right thing happens during any
2962 * recovery. */
2963 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002964 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05002965 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002966 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002967 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002968 ext4_update_dx_flag(dir);
2969 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002970
2971end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002972 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002973 if (handle)
2974 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002975 return retval;
2976}
2977
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002978static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979{
2980 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002981 struct inode *inode;
2982 struct buffer_head *bh;
2983 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05002984 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002986 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
2987 return -EIO;
2988
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002989 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002990 /* Initialize quotas before so that eventual writes go
2991 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02002992 retval = dquot_initialize(dir);
2993 if (retval)
2994 return retval;
2995 retval = dquot_initialize(d_inode(dentry));
2996 if (retval)
2997 return retval;
Christoph Hellwig907f4552010-03-03 09:05:06 -05002998
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002999 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05003000 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003001 if (IS_ERR(bh))
3002 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003 if (!bh)
3004 goto end_unlink;
3005
David Howells2b0143b2015-03-17 22:25:59 +00003006 inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003008 retval = -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009 if (le32_to_cpu(de->inode) != inode->i_ino)
3010 goto end_unlink;
3011
Theodore Ts'o931b6862013-02-09 09:43:39 -05003012 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05003013 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05003014 if (IS_ERR(handle)) {
3015 retval = PTR_ERR(handle);
3016 handle = NULL;
3017 goto end_unlink;
3018 }
3019
3020 if (IS_DIRSYNC(dir))
3021 ext4_handle_sync(handle);
3022
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003023 if (inode->i_nlink == 0) {
3024 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3025 dentry->d_name.len, dentry->d_name.name);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003026 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027 }
Mingming Cao617ba132006-10-11 01:20:53 -07003028 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029 if (retval)
3030 goto end_unlink;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003031 dir->i_ctime = dir->i_mtime = current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003032 ext4_update_dx_flag(dir);
3033 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003034 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003036 ext4_orphan_add(handle, inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003037 inode->i_ctime = current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003038 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039
3040end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003041 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003042 if (handle)
3043 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003044 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003045 return retval;
3046}
3047
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003048static int ext4_symlink(struct inode *dir,
3049 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003050{
3051 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003052 struct inode *inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003053 int err, len = strlen(symname);
Jan Karadf5e6222011-05-03 11:12:58 -04003054 int credits;
Theodore Ts'of348c252015-04-16 01:55:00 -04003055 bool encryption_required;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003056 struct fscrypt_str disk_link;
3057 struct fscrypt_symlink_data *sd = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003059 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3060 return -EIO;
3061
Theodore Ts'of348c252015-04-16 01:55:00 -04003062 disk_link.len = len + 1;
3063 disk_link.name = (char *) symname;
3064
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04003065 encryption_required = (ext4_encrypted_inode(dir) ||
3066 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
Theodore Ts'o4d3c4e52015-05-31 13:35:32 -04003067 if (encryption_required) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003068 err = fscrypt_get_encryption_info(dir);
Theodore Ts'o4d3c4e52015-05-31 13:35:32 -04003069 if (err)
3070 return err;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003071 if (!fscrypt_has_encryption_key(dir))
Eric Biggers54475f52016-12-05 11:12:44 -08003072 return -ENOKEY;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003073 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
3074 sizeof(struct fscrypt_symlink_data));
Theodore Ts'o4d3c4e52015-05-31 13:35:32 -04003075 sd = kzalloc(disk_link.len, GFP_KERNEL);
3076 if (!sd)
3077 return -ENOMEM;
3078 }
3079
3080 if (disk_link.len > dir->i_sb->s_blocksize) {
3081 err = -ENAMETOOLONG;
3082 goto err_free_sd;
3083 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003084
Jan Karaa7cdade2015-06-29 16:22:54 +02003085 err = dquot_initialize(dir);
3086 if (err)
Dan Carpenter926631c2015-07-24 14:05:33 +03003087 goto err_free_sd;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003088
Theodore Ts'of348c252015-04-16 01:55:00 -04003089 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Jan Karadf5e6222011-05-03 11:12:58 -04003090 /*
3091 * For non-fast symlinks, we just allocate inode and put it on
3092 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003093 * group descriptor, sb, inode block, quota blocks, and
3094 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003095 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003096 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3097 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003098 } else {
3099 /*
3100 * Fast symlink. We have to add entry to directory
3101 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3102 * allocate new inode (bitmap, group descriptor, inode block,
3103 * quota blocks, sb is already counted in previous macros).
3104 */
3105 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003106 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003107 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003108
Theodore Ts'o11395752013-02-09 16:27:09 -05003109 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3110 &dentry->d_name, 0, NULL,
3111 EXT4_HT_DIR, credits);
3112 handle = ext4_journal_current_handle();
Theodore Ts'of348c252015-04-16 01:55:00 -04003113 if (IS_ERR(inode)) {
3114 if (handle)
3115 ext4_journal_stop(handle);
Theodore Ts'o4d3c4e52015-05-31 13:35:32 -04003116 err = PTR_ERR(inode);
3117 goto err_free_sd;
Theodore Ts'of348c252015-04-16 01:55:00 -04003118 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003119
Theodore Ts'of348c252015-04-16 01:55:00 -04003120 if (encryption_required) {
Theodore Ts'of348c252015-04-16 01:55:00 -04003121 struct qstr istr;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003122 struct fscrypt_str ostr =
3123 FSTR_INIT(sd->encrypted_path, disk_link.len);
Theodore Ts'of348c252015-04-16 01:55:00 -04003124
Theodore Ts'of348c252015-04-16 01:55:00 -04003125 istr.name = (const unsigned char *) symname;
3126 istr.len = len;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003127 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
Eric Biggersef1eb3a2016-09-15 17:25:55 -04003128 if (err)
Theodore Ts'of348c252015-04-16 01:55:00 -04003129 goto err_drop_inode;
3130 sd->len = cpu_to_le16(ostr.len);
3131 disk_link.name = (char *) sd;
Al Viroa7a67e82015-04-27 17:51:30 -04003132 inode->i_op = &ext4_encrypted_symlink_inode_operations;
Theodore Ts'of348c252015-04-16 01:55:00 -04003133 }
3134
3135 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
Al Viroa7a67e82015-04-27 17:51:30 -04003136 if (!encryption_required)
3137 inode->i_op = &ext4_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003138 inode_nohighmem(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003139 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003140 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003141 * We cannot call page_symlink() with transaction started
3142 * because it calls into ext4_write_begin() which can wait
3143 * for transaction commit if we are running out of space
3144 * and thus we deadlock. So we have to stop transaction now
3145 * and restart it when symlink contents is written.
3146 *
3147 * To keep fs consistent in case of crash, we have to put inode
3148 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003149 */
Jan Karadf5e6222011-05-03 11:12:58 -04003150 drop_nlink(inode);
3151 err = ext4_orphan_add(handle, inode);
3152 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003153 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003154 if (err)
3155 goto err_drop_inode;
Theodore Ts'of348c252015-04-16 01:55:00 -04003156 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003157 if (err)
3158 goto err_drop_inode;
3159 /*
3160 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3161 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3162 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003163 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003164 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3165 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3166 if (IS_ERR(handle)) {
3167 err = PTR_ERR(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003168 handle = NULL;
Jan Karadf5e6222011-05-03 11:12:58 -04003169 goto err_drop_inode;
3170 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003171 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003172 err = ext4_orphan_del(handle, inode);
Theodore Ts'of348c252015-04-16 01:55:00 -04003173 if (err)
Jan Karadf5e6222011-05-03 11:12:58 -04003174 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003176 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003177 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Al Viro75e75662015-05-02 10:13:58 -04003178 if (!encryption_required) {
Al Viroa7a67e82015-04-27 17:51:30 -04003179 inode->i_op = &ext4_fast_symlink_inode_operations;
Al Viro75e75662015-05-02 10:13:58 -04003180 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3181 }
Theodore Ts'of348c252015-04-16 01:55:00 -04003182 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3183 disk_link.len);
3184 inode->i_size = disk_link.len - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185 }
Mingming Cao617ba132006-10-11 01:20:53 -07003186 EXT4_I(inode)->i_disksize = inode->i_size;
3187 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003188 if (!err && IS_DIRSYNC(dir))
3189 ext4_handle_sync(handle);
3190
Theodore Ts'o11395752013-02-09 16:27:09 -05003191 if (handle)
3192 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003193 kfree(sd);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003194 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04003195err_drop_inode:
Theodore Ts'of348c252015-04-16 01:55:00 -04003196 if (handle)
3197 ext4_journal_stop(handle);
Theodore Ts'of348c252015-04-16 01:55:00 -04003198 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003199 unlock_new_inode(inode);
3200 iput(inode);
Theodore Ts'o4d3c4e52015-05-31 13:35:32 -04003201err_free_sd:
3202 kfree(sd);
Jan Karadf5e6222011-05-03 11:12:58 -04003203 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003204}
3205
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003206static int ext4_link(struct dentry *old_dentry,
3207 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003208{
3209 handle_t *handle;
David Howells2b0143b2015-03-17 22:25:59 +00003210 struct inode *inode = d_inode(old_dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211 int err, retries = 0;
3212
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003213 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003214 return -EMLINK;
Eric Biggers69725182017-10-18 20:21:57 -04003215
3216 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3217 if (err)
3218 return err;
Li Xi040cb372016-01-08 16:01:21 -05003219
3220 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3221 (!projid_eq(EXT4_I(dir)->i_projid,
3222 EXT4_I(old_dentry->d_inode)->i_projid)))
3223 return -EXDEV;
3224
Jan Karaa7cdade2015-06-29 16:22:54 +02003225 err = dquot_initialize(dir);
3226 if (err)
3227 return err;
Christoph Hellwig907f4552010-03-03 09:05:06 -05003228
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003230 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3231 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003232 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003233 if (IS_ERR(handle))
3234 return PTR_ERR(handle);
3235
3236 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003237 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003238
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003239 inode->i_ctime = current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003240 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003241 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003242
Al Viro6b38e842008-12-30 02:03:31 -05003243 err = ext4_add_entry(handle, dentry, inode);
3244 if (!err) {
3245 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003246 /* this can happen only for tmpfile being
3247 * linked the first time
3248 */
3249 if (inode->i_nlink == 1)
3250 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003251 d_instantiate(dentry, inode);
3252 } else {
3253 drop_nlink(inode);
3254 iput(inode);
3255 }
Mingming Cao617ba132006-10-11 01:20:53 -07003256 ext4_journal_stop(handle);
3257 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258 goto retry;
3259 return err;
3260}
3261
Tao Ma32f7f222012-12-10 14:06:01 -05003262
3263/*
3264 * Try to find buffer head where contains the parent block.
3265 * It should be the inode block if it is inlined or the 1st block
3266 * if it is a normal dir.
3267 */
3268static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3269 struct inode *inode,
3270 int *retval,
3271 struct ext4_dir_entry_2 **parent_de,
3272 int *inlined)
3273{
3274 struct buffer_head *bh;
3275
3276 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003277 bh = ext4_read_dirblock(inode, 0, EITHER);
3278 if (IS_ERR(bh)) {
3279 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003280 return NULL;
3281 }
3282 *parent_de = ext4_next_entry(
3283 (struct ext4_dir_entry_2 *)bh->b_data,
3284 inode->i_sb->s_blocksize);
3285 return bh;
3286 }
3287
3288 *inlined = 1;
3289 return ext4_get_first_inline_block(inode, parent_de, retval);
3290}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003291
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003292struct ext4_renament {
3293 struct inode *dir;
3294 struct dentry *dentry;
3295 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003296 bool is_dir;
3297 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003298
3299 /* entry for "dentry" */
3300 struct buffer_head *bh;
3301 struct ext4_dir_entry_2 *de;
3302 int inlined;
3303
3304 /* entry for ".." in inode if it's a directory */
3305 struct buffer_head *dir_bh;
3306 struct ext4_dir_entry_2 *parent_de;
3307 int dir_inlined;
3308};
3309
Miklos Szeredibd1af142014-04-01 17:08:44 +02003310static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3311{
3312 int retval;
3313
3314 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3315 &retval, &ent->parent_de,
3316 &ent->dir_inlined);
3317 if (!ent->dir_bh)
3318 return retval;
3319 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003320 return -EFSCORRUPTED;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003321 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3322 return ext4_journal_get_write_access(handle, ent->dir_bh);
3323}
3324
3325static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3326 unsigned dir_ino)
3327{
3328 int retval;
3329
3330 ent->parent_de->inode = cpu_to_le32(dir_ino);
3331 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3332 if (!ent->dir_inlined) {
3333 if (is_dx(ent->inode)) {
3334 retval = ext4_handle_dirty_dx_node(handle,
3335 ent->inode,
3336 ent->dir_bh);
3337 } else {
3338 retval = ext4_handle_dirty_dirent_node(handle,
3339 ent->inode,
3340 ent->dir_bh);
3341 }
3342 } else {
3343 retval = ext4_mark_inode_dirty(handle, ent->inode);
3344 }
3345 if (retval) {
3346 ext4_std_error(ent->dir->i_sb, retval);
3347 return retval;
3348 }
3349 return 0;
3350}
3351
3352static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3353 unsigned ino, unsigned file_type)
3354{
3355 int retval;
3356
3357 BUFFER_TRACE(ent->bh, "get write access");
3358 retval = ext4_journal_get_write_access(handle, ent->bh);
3359 if (retval)
3360 return retval;
3361 ent->de->inode = cpu_to_le32(ino);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003362 if (ext4_has_feature_filetype(ent->dir->i_sb))
Miklos Szeredibd1af142014-04-01 17:08:44 +02003363 ent->de->file_type = file_type;
3364 ent->dir->i_version++;
3365 ent->dir->i_ctime = ent->dir->i_mtime =
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003366 current_time(ent->dir);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003367 ext4_mark_inode_dirty(handle, ent->dir);
3368 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3369 if (!ent->inlined) {
3370 retval = ext4_handle_dirty_dirent_node(handle,
3371 ent->dir, ent->bh);
3372 if (unlikely(retval)) {
3373 ext4_std_error(ent->dir->i_sb, retval);
3374 return retval;
3375 }
3376 }
3377 brelse(ent->bh);
3378 ent->bh = NULL;
3379
3380 return 0;
3381}
3382
3383static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3384 const struct qstr *d_name)
3385{
3386 int retval = -ENOENT;
3387 struct buffer_head *bh;
3388 struct ext4_dir_entry_2 *de;
3389
3390 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003391 if (IS_ERR(bh))
3392 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003393 if (bh) {
3394 retval = ext4_delete_entry(handle, dir, de, bh);
3395 brelse(bh);
3396 }
3397 return retval;
3398}
3399
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003400static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3401 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003402{
3403 int retval;
3404 /*
3405 * ent->de could have moved from under us during htree split, so make
3406 * sure that we are deleting the right entry. We might also be pointing
3407 * to a stale entry in the unused part of ent->bh so just checking inum
3408 * and the name isn't enough.
3409 */
3410 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3411 ent->de->name_len != ent->dentry->d_name.len ||
3412 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003413 ent->de->name_len) ||
3414 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003415 retval = ext4_find_delete_entry(handle, ent->dir,
3416 &ent->dentry->d_name);
3417 } else {
3418 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3419 if (retval == -ENOENT) {
3420 retval = ext4_find_delete_entry(handle, ent->dir,
3421 &ent->dentry->d_name);
3422 }
3423 }
3424
3425 if (retval) {
Andreas Dilgerb03a2f72015-06-15 14:50:26 -04003426 ext4_warning_inode(ent->dir,
3427 "Deleting old file: nlink %d, error=%d",
3428 ent->dir->i_nlink, retval);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003429 }
3430}
3431
Miklos Szeredibd429982014-04-01 17:08:44 +02003432static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3433{
3434 if (ent->dir_nlink_delta) {
3435 if (ent->dir_nlink_delta == -1)
3436 ext4_dec_count(handle, ent->dir);
3437 else
3438 ext4_inc_count(handle, ent->dir);
3439 ext4_mark_inode_dirty(handle, ent->dir);
3440 }
3441}
3442
Miklos Szeredicd808de2014-10-24 00:14:37 +02003443static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3444 int credits, handle_t **h)
3445{
3446 struct inode *wh;
3447 handle_t *handle;
3448 int retries = 0;
3449
3450 /*
3451 * for inode block, sb block, group summaries,
3452 * and inode bitmap
3453 */
3454 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3455 EXT4_XATTR_TRANS_BLOCKS + 4);
3456retry:
3457 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3458 &ent->dentry->d_name, 0, NULL,
3459 EXT4_HT_DIR, credits);
3460
3461 handle = ext4_journal_current_handle();
3462 if (IS_ERR(wh)) {
3463 if (handle)
3464 ext4_journal_stop(handle);
3465 if (PTR_ERR(wh) == -ENOSPC &&
3466 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3467 goto retry;
3468 } else {
3469 *h = handle;
3470 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3471 wh->i_op = &ext4_special_inode_operations;
3472 }
3473 return wh;
3474}
3475
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003476/*
3477 * Anybody can rename anything with this: the permission checks are left to the
3478 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003479 *
3480 * n.b. old_{dentry,inode) refers to the source dentry/inode
3481 * while new_{dentry,inode) refers to the destination dentry/inode
3482 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003483 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003484static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003485 struct inode *new_dir, struct dentry *new_dentry,
3486 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003487{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003488 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003489 struct ext4_renament old = {
3490 .dir = old_dir,
3491 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003492 .inode = d_inode(old_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003493 };
3494 struct ext4_renament new = {
3495 .dir = new_dir,
3496 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003497 .inode = d_inode(new_dentry),
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003498 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003499 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003500 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003501 struct inode *whiteout = NULL;
3502 int credits;
3503 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504
Li Xi040cb372016-01-08 16:01:21 -05003505 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3506 (!projid_eq(EXT4_I(new_dir)->i_projid,
3507 EXT4_I(old_dentry->d_inode)->i_projid)))
3508 return -EXDEV;
3509
Jan Karaa7cdade2015-06-29 16:22:54 +02003510 retval = dquot_initialize(old.dir);
3511 if (retval)
3512 return retval;
3513 retval = dquot_initialize(new.dir);
3514 if (retval)
3515 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003516
3517 /* Initialize quotas before so that eventual writes go
3518 * in separate transaction */
Jan Karaa7cdade2015-06-29 16:22:54 +02003519 if (new.inode) {
3520 retval = dquot_initialize(new.inode);
3521 if (retval)
3522 return retval;
3523 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003524
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003525 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003526 if (IS_ERR(old.bh))
3527 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003528 /*
3529 * Check for inode number is _not_ due to possible IO errors.
3530 * We might rmdir the source, keep it as pwd of some process
3531 * and merrily kill the link to whatever was created under the
3532 * same name. Goodbye sticky bit ;-<
3533 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003535 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003536 goto end_rename;
3537
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003538 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3539 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003540 if (IS_ERR(new.bh)) {
3541 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003542 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003543 goto end_rename;
3544 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003545 if (new.bh) {
3546 if (!new.inode) {
3547 brelse(new.bh);
3548 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003549 }
3550 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003551 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3552 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003553
Miklos Szeredicd808de2014-10-24 00:14:37 +02003554 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3555 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3556 if (!(flags & RENAME_WHITEOUT)) {
3557 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003558 if (IS_ERR(handle)) {
3559 retval = PTR_ERR(handle);
3560 handle = NULL;
3561 goto end_rename;
3562 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003563 } else {
3564 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003565 if (IS_ERR(whiteout)) {
3566 retval = PTR_ERR(whiteout);
3567 whiteout = NULL;
3568 goto end_rename;
3569 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003570 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003571
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003572 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003573 ext4_handle_sync(handle);
3574
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003575 if (S_ISDIR(old.inode->i_mode)) {
3576 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003577 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003578 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003579 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003580 } else {
3581 retval = -EMLINK;
3582 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3583 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003584 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003585 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003586 if (retval)
3587 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003588 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003589 /*
3590 * If we're renaming a file within an inline_data dir and adding or
3591 * setting the new dirent causes a conversion from inline_data to
3592 * extents/blockmap, we need to force the dirent delete code to
3593 * re-read the directory, or else we end up trying to delete a dirent
3594 * from what is now the extent tree root (or a block map).
3595 */
3596 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3597 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003598
3599 old_file_type = old.de->file_type;
3600 if (whiteout) {
3601 /*
3602 * Do this before adding a new entry, so the old entry is sure
3603 * to be still pointing to the valid old entry.
3604 */
3605 retval = ext4_setent(handle, &old, whiteout->i_ino,
3606 EXT4_FT_CHRDEV);
3607 if (retval)
3608 goto end_rename;
3609 ext4_mark_inode_dirty(handle, whiteout);
3610 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003611 if (!new.bh) {
3612 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003613 if (retval)
3614 goto end_rename;
3615 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003616 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003617 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003618 if (retval)
3619 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003620 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003621 if (force_reread)
3622 force_reread = !ext4_test_inode_flag(new.dir,
3623 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003624
3625 /*
3626 * Like most other Unix systems, set the ctime for inodes on a
3627 * rename.
3628 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003629 old.inode->i_ctime = current_time(old.inode);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003630 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003631
Miklos Szeredicd808de2014-10-24 00:14:37 +02003632 if (!whiteout) {
3633 /*
3634 * ok, that's it
3635 */
3636 ext4_rename_delete(handle, &old, force_reread);
3637 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003638
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003639 if (new.inode) {
3640 ext4_dec_count(handle, new.inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003641 new.inode->i_ctime = current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003642 }
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003643 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003644 ext4_update_dx_flag(old.dir);
3645 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003646 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3647 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003648 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003649
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003650 ext4_dec_count(handle, old.dir);
3651 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003652 /* checked ext4_empty_dir above, can't have another
3653 * parent, ext4_dec_count() won't work for many-linked
3654 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003655 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003657 ext4_inc_count(handle, new.dir);
3658 ext4_update_dx_flag(new.dir);
3659 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660 }
3661 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003662 ext4_mark_inode_dirty(handle, old.dir);
3663 if (new.inode) {
3664 ext4_mark_inode_dirty(handle, new.inode);
3665 if (!new.inode->i_nlink)
3666 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003667 }
3668 retval = 0;
3669
3670end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003671 brelse(old.dir_bh);
3672 brelse(old.bh);
3673 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003674 if (whiteout) {
3675 if (retval)
3676 drop_nlink(whiteout);
3677 unlock_new_inode(whiteout);
3678 iput(whiteout);
3679 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003680 if (handle)
3681 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003682 return retval;
3683}
3684
Miklos Szeredibd429982014-04-01 17:08:44 +02003685static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3686 struct inode *new_dir, struct dentry *new_dentry)
3687{
3688 handle_t *handle = NULL;
3689 struct ext4_renament old = {
3690 .dir = old_dir,
3691 .dentry = old_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003692 .inode = d_inode(old_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003693 };
3694 struct ext4_renament new = {
3695 .dir = new_dir,
3696 .dentry = new_dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003697 .inode = d_inode(new_dentry),
Miklos Szeredibd429982014-04-01 17:08:44 +02003698 };
3699 u8 new_file_type;
3700 int retval;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003701 struct timespec ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02003702
Li Xi040cb372016-01-08 16:01:21 -05003703 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
3704 !projid_eq(EXT4_I(new_dir)->i_projid,
3705 EXT4_I(old_dentry->d_inode)->i_projid)) ||
3706 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
3707 !projid_eq(EXT4_I(old_dir)->i_projid,
3708 EXT4_I(new_dentry->d_inode)->i_projid)))
3709 return -EXDEV;
3710
Jan Karaa7cdade2015-06-29 16:22:54 +02003711 retval = dquot_initialize(old.dir);
3712 if (retval)
3713 return retval;
3714 retval = dquot_initialize(new.dir);
3715 if (retval)
3716 return retval;
Miklos Szeredibd429982014-04-01 17:08:44 +02003717
3718 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3719 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003720 if (IS_ERR(old.bh))
3721 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003722 /*
3723 * Check for inode number is _not_ due to possible IO errors.
3724 * We might rmdir the source, keep it as pwd of some process
3725 * and merrily kill the link to whatever was created under the
3726 * same name. Goodbye sticky bit ;-<
3727 */
3728 retval = -ENOENT;
3729 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3730 goto end_rename;
3731
3732 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3733 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003734 if (IS_ERR(new.bh)) {
3735 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003736 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003737 goto end_rename;
3738 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003739
3740 /* RENAME_EXCHANGE case: old *and* new must both exist */
3741 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3742 goto end_rename;
3743
3744 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3745 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3746 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003747 if (IS_ERR(handle)) {
3748 retval = PTR_ERR(handle);
3749 handle = NULL;
3750 goto end_rename;
3751 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003752
3753 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3754 ext4_handle_sync(handle);
3755
3756 if (S_ISDIR(old.inode->i_mode)) {
3757 old.is_dir = true;
3758 retval = ext4_rename_dir_prepare(handle, &old);
3759 if (retval)
3760 goto end_rename;
3761 }
3762 if (S_ISDIR(new.inode->i_mode)) {
3763 new.is_dir = true;
3764 retval = ext4_rename_dir_prepare(handle, &new);
3765 if (retval)
3766 goto end_rename;
3767 }
3768
3769 /*
3770 * Other than the special case of overwriting a directory, parents'
3771 * nlink only needs to be modified if this is a cross directory rename.
3772 */
3773 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3774 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3775 new.dir_nlink_delta = -old.dir_nlink_delta;
3776 retval = -EMLINK;
3777 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3778 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3779 goto end_rename;
3780 }
3781
3782 new_file_type = new.de->file_type;
3783 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3784 if (retval)
3785 goto end_rename;
3786
3787 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3788 if (retval)
3789 goto end_rename;
3790
3791 /*
3792 * Like most other Unix systems, set the ctime for inodes on a
3793 * rename.
3794 */
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05003795 ctime = current_time(old.inode);
3796 old.inode->i_ctime = ctime;
3797 new.inode->i_ctime = ctime;
Miklos Szeredibd429982014-04-01 17:08:44 +02003798 ext4_mark_inode_dirty(handle, old.inode);
3799 ext4_mark_inode_dirty(handle, new.inode);
3800
3801 if (old.dir_bh) {
3802 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3803 if (retval)
3804 goto end_rename;
3805 }
3806 if (new.dir_bh) {
3807 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3808 if (retval)
3809 goto end_rename;
3810 }
3811 ext4_update_dir_count(handle, &old);
3812 ext4_update_dir_count(handle, &new);
3813 retval = 0;
3814
3815end_rename:
3816 brelse(old.dir_bh);
3817 brelse(new.dir_bh);
3818 brelse(old.bh);
3819 brelse(new.bh);
3820 if (handle)
3821 ext4_journal_stop(handle);
3822 return retval;
3823}
3824
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003825static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3826 struct inode *new_dir, struct dentry *new_dentry,
3827 unsigned int flags)
3828{
Eric Biggers07543d12017-10-18 20:21:57 -04003829 int err;
3830
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05003831 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
3832 return -EIO;
3833
Miklos Szeredicd808de2014-10-24 00:14:37 +02003834 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003835 return -EINVAL;
3836
Eric Biggers07543d12017-10-18 20:21:57 -04003837 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
3838 flags);
3839 if (err)
3840 return err;
3841
Miklos Szeredibd429982014-04-01 17:08:44 +02003842 if (flags & RENAME_EXCHANGE) {
3843 return ext4_cross_rename(old_dir, old_dentry,
3844 new_dir, new_dentry);
3845 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003846
3847 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003848}
3849
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003850/*
3851 * directories can handle most operations...
3852 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003853const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003854 .create = ext4_create,
3855 .lookup = ext4_lookup,
3856 .link = ext4_link,
3857 .unlink = ext4_unlink,
3858 .symlink = ext4_symlink,
3859 .mkdir = ext4_mkdir,
3860 .rmdir = ext4_rmdir,
3861 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003862 .tmpfile = ext4_tmpfile,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003863 .rename = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003864 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01003865 .getattr = ext4_getattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003866 .listxattr = ext4_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003867 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003868 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003869 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870};
3871
Arjan van de Ven754661f2007-02-12 00:55:38 -08003872const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003873 .setattr = ext4_setattr,
David Howells99652ea2017-03-31 18:31:56 +01003874 .getattr = ext4_getattr,
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};