blob: 85add9a9e1cd646629b1affa023de4b10f2bdf5a [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/fcntl.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/quotaops.h>
34#include <linux/buffer_head.h>
35#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040036#include "ext4.h"
37#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039#include "xattr.h"
40#include "acl.h"
41
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040042#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043/*
44 * define how far ahead to read directories while searching them.
45 */
46#define NAMEI_RA_CHUNKS 2
47#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040048#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Mingming Cao617ba132006-10-11 01:20:53 -070050static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 struct inode *inode,
Theodore Ts'o0f70b402013-02-15 03:35:57 -050052 ext4_lblk_t *block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053{
54 struct buffer_head *bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -040055 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056
Theodore Ts'odf981d02012-08-17 09:48:17 -040057 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
58 ((inode->i_size >> 10) >=
Theodore Ts'o0f70b402013-02-15 03:35:57 -050059 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
60 return ERR_PTR(-ENOSPC);
Theodore Ts'odf981d02012-08-17 09:48:17 -040061
Dave Kleikampac27a0e2006-10-11 01:20:50 -070062 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
63
Theodore Ts'o1c215022014-08-29 20:52:15 -040064 bh = ext4_bread(handle, inode, *block, 1);
65 if (IS_ERR(bh))
66 return bh;
Theodore Ts'o0f70b402013-02-15 03:35:57 -050067 inode->i_size += inode->i_sb->s_blocksize;
68 EXT4_I(inode)->i_disksize = inode->i_size;
liang xie5d601252014-05-12 22:06:43 -040069 BUFFER_TRACE(bh, "get_write_access");
Theodore Ts'o0f70b402013-02-15 03:35:57 -050070 err = ext4_journal_get_write_access(handle, bh);
71 if (err) {
72 brelse(bh);
73 ext4_std_error(inode->i_sb, err);
74 return ERR_PTR(err);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040075 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 return bh;
77}
78
Theodore Ts'odc6982f2013-02-14 23:59:26 -050079static int ext4_dx_csum_verify(struct inode *inode,
80 struct ext4_dir_entry *dirent);
81
82typedef enum {
83 EITHER, INDEX, DIRENT
84} dirblock_type_t;
85
86#define ext4_read_dirblock(inode, block, type) \
87 __ext4_read_dirblock((inode), (block), (type), __LINE__)
88
89static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
90 ext4_lblk_t block,
91 dirblock_type_t type,
92 unsigned int line)
93{
94 struct buffer_head *bh;
95 struct ext4_dir_entry *dirent;
Theodore Ts'o1c215022014-08-29 20:52:15 -040096 int is_dx_block = 0;
Theodore Ts'odc6982f2013-02-14 23:59:26 -050097
Theodore Ts'o1c215022014-08-29 20:52:15 -040098 bh = ext4_bread(NULL, inode, block, 0);
99 if (IS_ERR(bh)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500100 __ext4_warning(inode->i_sb, __func__, line,
Theodore Ts'o1c215022014-08-29 20:52:15 -0400101 "error %ld reading directory block "
102 "(ino %lu, block %lu)", PTR_ERR(bh), inode->i_ino,
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500103 (unsigned long) block);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400104
105 return bh;
106 }
107 if (!bh) {
108 ext4_error_inode(inode, __func__, line, block, "Directory hole found");
109 return ERR_PTR(-EIO);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500110 }
111 dirent = (struct ext4_dir_entry *) bh->b_data;
112 /* Determine whether or not we have an index block */
113 if (is_dx(inode)) {
114 if (block == 0)
115 is_dx_block = 1;
116 else if (ext4_rec_len_from_disk(dirent->rec_len,
117 inode->i_sb->s_blocksize) ==
118 inode->i_sb->s_blocksize)
119 is_dx_block = 1;
120 }
121 if (!is_dx_block && type == INDEX) {
122 ext4_error_inode(inode, __func__, line, block,
123 "directory leaf block found instead of index block");
124 return ERR_PTR(-EIO);
125 }
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400126 if (!ext4_has_metadata_csum(inode->i_sb) ||
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500127 buffer_verified(bh))
128 return bh;
129
130 /*
131 * An empty leaf block can get mistaken for a index block; for
132 * this reason, we can only check the index checksum when the
133 * caller is sure it should be an index block.
134 */
135 if (is_dx_block && type == INDEX) {
136 if (ext4_dx_csum_verify(inode, dirent))
137 set_buffer_verified(bh);
138 else {
139 ext4_error_inode(inode, __func__, line, block,
140 "Directory index failed checksum");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 brelse(bh);
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500142 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143 }
144 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500145 if (!is_dx_block) {
146 if (ext4_dirent_csum_verify(inode, dirent))
147 set_buffer_verified(bh);
148 else {
149 ext4_error_inode(inode, __func__, line, block,
150 "Directory block failed checksum");
151 brelse(bh);
152 return ERR_PTR(-EIO);
153 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154 }
155 return bh;
156}
157
158#ifndef assert
159#define assert(test) J_ASSERT(test)
160#endif
161
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162#ifdef DX_DEBUG
163#define dxtrace(command) command
164#else
165#define dxtrace(command)
166#endif
167
168struct fake_dirent
169{
170 __le32 inode;
171 __le16 rec_len;
172 u8 name_len;
173 u8 file_type;
174};
175
176struct dx_countlimit
177{
178 __le16 limit;
179 __le16 count;
180};
181
182struct dx_entry
183{
184 __le32 hash;
185 __le32 block;
186};
187
188/*
189 * dx_root_info is laid out so that if it should somehow get overlaid by a
190 * dirent the two low bits of the hash version will be zero. Therefore, the
191 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
192 */
193
194struct dx_root
195{
196 struct fake_dirent dot;
197 char dot_name[4];
198 struct fake_dirent dotdot;
199 char dotdot_name[4];
200 struct dx_root_info
201 {
202 __le32 reserved_zero;
203 u8 hash_version;
204 u8 info_length; /* 8 */
205 u8 indirect_levels;
206 u8 unused_flags;
207 }
208 info;
209 struct dx_entry entries[0];
210};
211
212struct dx_node
213{
214 struct fake_dirent fake;
215 struct dx_entry entries[0];
216};
217
218
219struct dx_frame
220{
221 struct buffer_head *bh;
222 struct dx_entry *entries;
223 struct dx_entry *at;
224};
225
226struct dx_map_entry
227{
228 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700229 u16 offs;
230 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231};
232
Darrick J. Wonge6153912012-04-29 18:23:10 -0400233/*
234 * This goes at the end of each htree block.
235 */
236struct dx_tail {
237 u32 dt_reserved;
238 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
239};
240
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500241static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
242static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400243static inline unsigned dx_get_hash(struct dx_entry *entry);
244static void dx_set_hash(struct dx_entry *entry, unsigned value);
245static unsigned dx_get_count(struct dx_entry *entries);
246static unsigned dx_get_limit(struct dx_entry *entries);
247static void dx_set_count(struct dx_entry *entries, unsigned value);
248static void dx_set_limit(struct dx_entry *entries, unsigned value);
249static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
250static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400251static struct dx_frame *dx_probe(const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 struct inode *dir,
253 struct dx_hash_info *hinfo,
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400254 struct dx_frame *frame);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400255static void dx_release(struct dx_frame *frames);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500256static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400257 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400259static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500260 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500261static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500262static void dx_insert_block(struct dx_frame *frame,
263 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700264static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 struct dx_frame *frame,
266 struct dx_frame *frames,
267 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400268static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
269 const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -0400270 struct ext4_dir_entry_2 **res_dir);
Mingming Cao617ba132006-10-11 01:20:53 -0700271static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 struct inode *inode);
273
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400274/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500275void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
276 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400277{
278 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
279 t->det_rec_len = ext4_rec_len_to_disk(
280 sizeof(struct ext4_dir_entry_tail), blocksize);
281 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
282}
283
284/* Walk through a dirent block to find a checksum "dirent" at the tail */
285static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
286 struct ext4_dir_entry *de)
287{
288 struct ext4_dir_entry_tail *t;
289
290#ifdef PARANOID
291 struct ext4_dir_entry *d, *top;
292
293 d = de;
294 top = (struct ext4_dir_entry *)(((void *)de) +
295 (EXT4_BLOCK_SIZE(inode->i_sb) -
296 sizeof(struct ext4_dir_entry_tail)));
297 while (d < top && d->rec_len)
298 d = (struct ext4_dir_entry *)(((void *)d) +
299 le16_to_cpu(d->rec_len));
300
301 if (d != top)
302 return NULL;
303
304 t = (struct ext4_dir_entry_tail *)d;
305#else
306 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
307#endif
308
309 if (t->det_reserved_zero1 ||
310 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
311 t->det_reserved_zero2 ||
312 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
313 return NULL;
314
315 return t;
316}
317
318static __le32 ext4_dirent_csum(struct inode *inode,
319 struct ext4_dir_entry *dirent, int size)
320{
321 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
322 struct ext4_inode_info *ei = EXT4_I(inode);
323 __u32 csum;
324
325 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
326 return cpu_to_le32(csum);
327}
328
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500329static void warn_no_space_for_csum(struct inode *inode)
330{
331 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
332 "checksum. Please run e2fsck -D.", inode->i_ino);
333}
334
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400335int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
336{
337 struct ext4_dir_entry_tail *t;
338
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400339 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400340 return 1;
341
342 t = get_dirent_tail(inode, dirent);
343 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500344 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400345 return 0;
346 }
347
348 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
349 (void *)t - (void *)dirent))
350 return 0;
351
352 return 1;
353}
354
355static void ext4_dirent_csum_set(struct inode *inode,
356 struct ext4_dir_entry *dirent)
357{
358 struct ext4_dir_entry_tail *t;
359
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400360 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400361 return;
362
363 t = get_dirent_tail(inode, dirent);
364 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500365 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400366 return;
367 }
368
369 t->det_checksum = ext4_dirent_csum(inode, dirent,
370 (void *)t - (void *)dirent);
371}
372
Tao Ma3c47d542012-12-10 14:05:59 -0500373int ext4_handle_dirty_dirent_node(handle_t *handle,
374 struct inode *inode,
375 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400376{
377 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
378 return ext4_handle_dirty_metadata(handle, inode, bh);
379}
380
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400381static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
382 struct ext4_dir_entry *dirent,
383 int *offset)
384{
385 struct ext4_dir_entry *dp;
386 struct dx_root_info *root;
387 int count_offset;
388
389 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
390 count_offset = 8;
391 else if (le16_to_cpu(dirent->rec_len) == 12) {
392 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
393 if (le16_to_cpu(dp->rec_len) !=
394 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
395 return NULL;
396 root = (struct dx_root_info *)(((void *)dp + 12));
397 if (root->reserved_zero ||
398 root->info_length != sizeof(struct dx_root_info))
399 return NULL;
400 count_offset = 32;
401 } else
402 return NULL;
403
404 if (offset)
405 *offset = count_offset;
406 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
407}
408
409static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
410 int count_offset, int count, struct dx_tail *t)
411{
412 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
413 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'od6a77102013-04-09 23:59:55 -0400414 __u32 csum;
415 __le32 save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400416 int size;
417
418 size = count_offset + (count * sizeof(struct dx_entry));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400419 save_csum = t->dt_checksum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400420 t->dt_checksum = 0;
421 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
422 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
Theodore Ts'od6a77102013-04-09 23:59:55 -0400423 t->dt_checksum = save_csum;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400424
425 return cpu_to_le32(csum);
426}
427
428static int ext4_dx_csum_verify(struct inode *inode,
429 struct ext4_dir_entry *dirent)
430{
431 struct dx_countlimit *c;
432 struct dx_tail *t;
433 int count_offset, limit, count;
434
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400435 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400436 return 1;
437
438 c = get_dx_countlimit(inode, dirent, &count_offset);
439 if (!c) {
440 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
441 return 1;
442 }
443 limit = le16_to_cpu(c->limit);
444 count = le16_to_cpu(c->count);
445 if (count_offset + (limit * sizeof(struct dx_entry)) >
446 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500447 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400448 return 1;
449 }
450 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
451
452 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
453 count, t))
454 return 0;
455 return 1;
456}
457
458static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
459{
460 struct dx_countlimit *c;
461 struct dx_tail *t;
462 int count_offset, limit, count;
463
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400464 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400465 return;
466
467 c = get_dx_countlimit(inode, dirent, &count_offset);
468 if (!c) {
469 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
470 return;
471 }
472 limit = le16_to_cpu(c->limit);
473 count = le16_to_cpu(c->count);
474 if (count_offset + (limit * sizeof(struct dx_entry)) >
475 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500476 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400477 return;
478 }
479 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
480
481 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
482}
483
484static inline int ext4_handle_dirty_dx_node(handle_t *handle,
485 struct inode *inode,
486 struct buffer_head *bh)
487{
488 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
489 return ext4_handle_dirty_metadata(handle, inode, bh);
490}
491
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492/*
Li Zefanf795e142008-07-11 19:27:31 -0400493 * p is at least 6 bytes before the end of page
494 */
495static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500496ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400497{
498 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500499 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400500}
501
502/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 * Future: use high four bits of block for coalesce-on-delete flags
504 * Mask them off for now.
505 */
506
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500507static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508{
509 return le32_to_cpu(entry->block) & 0x00ffffff;
510}
511
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500512static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513{
514 entry->block = cpu_to_le32(value);
515}
516
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400517static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518{
519 return le32_to_cpu(entry->hash);
520}
521
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400522static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523{
524 entry->hash = cpu_to_le32(value);
525}
526
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400527static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528{
529 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
530}
531
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400532static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700533{
534 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
535}
536
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400537static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538{
539 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
540}
541
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400542static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543{
544 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
545}
546
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400547static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548{
Mingming Cao617ba132006-10-11 01:20:53 -0700549 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
550 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400551
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400552 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400553 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400554 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555}
556
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400557static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558{
Mingming Cao617ba132006-10-11 01:20:53 -0700559 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400560
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400561 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400562 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400563 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564}
565
566/*
567 * Debug
568 */
569#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400570static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571{
Andrew Morton63f57932006-10-11 01:21:24 -0700572 int i, n = dx_get_count (entries);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400573 printk(KERN_DEBUG "%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700574 for (i = 0; i < n; i++) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400575 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500576 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700577 }
578 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579}
580
581struct stats
582{
583 unsigned names;
584 unsigned space;
585 unsigned bcount;
586};
587
Michael Halcrowb3098482015-04-12 01:07:01 -0400588static struct stats dx_show_leaf(struct inode *dir,
589 struct dx_hash_info *hinfo,
590 struct ext4_dir_entry_2 *de,
591 int size, int show_names)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592{
593 unsigned names = 0, space = 0;
594 char *base = (char *) de;
595 struct dx_hash_info h = *hinfo;
596
597 printk("names: ");
598 while ((char *) de < base + size)
599 {
600 if (de->inode)
601 {
602 if (show_names)
603 {
Michael Halcrowb3098482015-04-12 01:07:01 -0400604#ifdef CONFIG_EXT4_FS_ENCRYPTION
605 int len;
606 char *name;
607 struct ext4_str fname_crypto_str
608 = {.name = NULL, .len = 0};
609 struct ext4_fname_crypto_ctx *ctx = NULL;
610 int res;
611
612 name = de->name;
613 len = de->name_len;
614 ctx = ext4_get_fname_crypto_ctx(dir,
615 EXT4_NAME_LEN);
616 if (IS_ERR(ctx)) {
617 printk(KERN_WARNING "Error acquiring"
618 " crypto ctxt--skipping crypto\n");
619 ctx = NULL;
620 }
621 if (ctx == NULL) {
622 /* Directory is not encrypted */
623 ext4fs_dirhash(de->name,
624 de->name_len, &h);
625 printk("%*.s:(U)%x.%u ", len,
626 name, h.hash,
627 (unsigned) ((char *) de
628 - base));
629 } else {
630 /* Directory is encrypted */
631 res = ext4_fname_crypto_alloc_buffer(
632 ctx, de->name_len,
633 &fname_crypto_str);
634 if (res < 0) {
635 printk(KERN_WARNING "Error "
636 "allocating crypto "
637 "buffer--skipping "
638 "crypto\n");
639 ext4_put_fname_crypto_ctx(&ctx);
640 ctx = NULL;
641 }
642 res = ext4_fname_disk_to_usr(ctx, de,
643 &fname_crypto_str);
644 if (res < 0) {
645 printk(KERN_WARNING "Error "
646 "converting filename "
647 "from disk to usr"
648 "\n");
649 name = "??";
650 len = 2;
651 } else {
652 name = fname_crypto_str.name;
653 len = fname_crypto_str.len;
654 }
655 res = ext4_fname_disk_to_hash(ctx, de,
656 &h);
657 if (res < 0) {
658 printk(KERN_WARNING "Error "
659 "converting filename "
660 "from disk to htree"
661 "\n");
662 h.hash = 0xDEADBEEF;
663 }
664 printk("%*.s:(E)%x.%u ", len, name,
665 h.hash, (unsigned) ((char *) de
666 - base));
667 ext4_put_fname_crypto_ctx(&ctx);
668 ext4_fname_crypto_free_buffer(
669 &fname_crypto_str);
670 }
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 }
684 printk("(%i)\n", names);
685 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'of702ba02008-09-22 15:21:01 -0400733dx_probe(const struct qstr *d_name, 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
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400743 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
744 if (IS_ERR(frame->bh))
745 return (struct dx_frame *) frame->bh;
746
747 root = (struct dx_root *) frame->bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748 if (root->info.hash_version != DX_HASH_TEA &&
749 root->info.hash_version != DX_HASH_HALF_MD4 &&
750 root->info.hash_version != DX_HASH_LEGACY) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500751 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 root->info.hash_version);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753 goto fail;
754 }
755 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400756 if (hinfo->hash_version <= DX_HASH_TEA)
757 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700758 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrowb3098482015-04-12 01:07:01 -0400759#ifdef CONFIG_EXT4_FS_ENCRYPTION
760 if (d_name) {
761 struct ext4_fname_crypto_ctx *ctx = NULL;
762 int res;
763
764 /* Check if the directory is encrypted */
765 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
766 if (IS_ERR(ctx)) {
767 ret_err = ERR_PTR(PTR_ERR(ctx));
768 goto fail;
769 }
770 res = ext4_fname_usr_to_hash(ctx, d_name, hinfo);
771 if (res < 0) {
772 ret_err = ERR_PTR(res);
773 goto fail;
774 }
775 ext4_put_fname_crypto_ctx(&ctx);
776 }
777#else
Theodore Ts'of702ba02008-09-22 15:21:01 -0400778 if (d_name)
779 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Michael Halcrowb3098482015-04-12 01:07:01 -0400780#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 hash = hinfo->hash;
782
783 if (root->info.unused_flags & 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500784 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 root->info.unused_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 goto fail;
787 }
788
789 if ((indirect = root->info.indirect_levels) > 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500790 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 root->info.indirect_levels);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 goto fail;
793 }
794
795 entries = (struct dx_entry *) (((char *)&root->info) +
796 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700797
798 if (dx_get_limit(entries) != dx_root_limit(dir,
799 root->info.info_length)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500800 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700801 goto fail;
802 }
803
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400804 dxtrace(printk("Look up %x", hash));
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400805 while (1) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700807 if (!count || count > dx_get_limit(entries)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500808 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700809 "dx entry: no count or count > limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400810 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700811 }
812
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 p = entries + 1;
814 q = entries + count - 1;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400815 while (p <= q) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 m = p + (q - p)/2;
817 dxtrace(printk("."));
818 if (dx_get_hash(m) > hash)
819 q = m - 1;
820 else
821 p = m + 1;
822 }
823
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400824 if (0) { // linear search cross check
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825 unsigned n = count - 1;
826 at = entries;
827 while (n--)
828 {
829 dxtrace(printk(","));
830 if (dx_get_hash(++at) > hash)
831 {
832 at--;
833 break;
834 }
835 }
836 assert (at == p - 1);
837 }
838
839 at = p - 1;
840 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 frame->entries = entries;
842 frame->at = at;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400843 if (!indirect--)
844 return frame;
845 frame++;
846 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
847 if (IS_ERR(frame->bh)) {
848 ret_err = (struct dx_frame *) frame->bh;
849 frame->bh = NULL;
850 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400851 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400852 entries = ((struct dx_node *) frame->bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400853
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700854 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500855 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700856 "dx entry: limit != node limit");
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400857 goto fail;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700858 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859 }
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400860fail:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 while (frame >= frame_in) {
862 brelse(frame->bh);
863 frame--;
864 }
Michael Halcrowb3098482015-04-12 01:07:01 -0400865
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400866 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
Eric Sandeen12062dd2010-02-15 14:19:27 -0500867 ext4_warning(dir->i_sb,
Zheng Liu9ee49302012-02-20 23:09:36 -0500868 "Corrupt dir inode %lu, running e2fsck is "
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700869 "recommended.", dir->i_ino);
Theodore Ts'odd73b5d2014-08-29 20:52:17 -0400870 return ret_err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871}
872
873static void dx_release (struct dx_frame *frames)
874{
875 if (frames[0].bh == NULL)
876 return;
877
878 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
879 brelse(frames[1].bh);
880 brelse(frames[0].bh);
881}
882
883/*
884 * This function increments the frame pointer to search the next leaf
885 * block, and reads in the necessary intervening nodes if the search
886 * should be necessary. Whether or not the search is necessary is
887 * controlled by the hash parameter. If the hash value is even, then
888 * the search is only continued if the next block starts with that
889 * hash value. This is used if we are searching for a specific file.
890 *
891 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
892 *
893 * This function returns 1 if the caller should continue to search,
894 * or 0 if it should not. If there is an error reading one of the
895 * index blocks, it will a negative error code.
896 *
897 * If start_hash is non-null, it will be filled in with the starting
898 * hash of the next page.
899 */
Mingming Cao617ba132006-10-11 01:20:53 -0700900static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 struct dx_frame *frame,
902 struct dx_frame *frames,
903 __u32 *start_hash)
904{
905 struct dx_frame *p;
906 struct buffer_head *bh;
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500907 int num_frames = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 __u32 bhash;
909
910 p = frame;
911 /*
912 * Find the next leaf page by incrementing the frame pointer.
913 * If we run out of entries in the interior node, loop around and
914 * increment pointer in the parent node. When we break out of
915 * this loop, num_frames indicates the number of interior
916 * nodes need to be read.
917 */
918 while (1) {
919 if (++(p->at) < p->entries + dx_get_count(p->entries))
920 break;
921 if (p == frames)
922 return 0;
923 num_frames++;
924 p--;
925 }
926
927 /*
928 * If the hash is 1, then continue only if the next page has a
929 * continuation hash of any value. This is used for readdir
930 * handling. Otherwise, check to see if the hash matches the
931 * desired contiuation hash. If it doesn't, return since
932 * there's no point to read in the successive index pages.
933 */
934 bhash = dx_get_hash(p->at);
935 if (start_hash)
936 *start_hash = bhash;
937 if ((hash & 1) == 0) {
938 if ((bhash & ~1) != hash)
939 return 0;
940 }
941 /*
942 * If the hash is HASH_NB_ALWAYS, we always go to the next
943 * block so no check is necessary
944 */
945 while (num_frames--) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500946 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
947 if (IS_ERR(bh))
948 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400950 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 p->bh = bh;
952 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
953 }
954 return 1;
955}
956
957
958/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 * This function fills a red-black tree with information from a
960 * directory block. It returns the number directory entries loaded
961 * into the tree. If there is an error it is returned in err.
962 */
963static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500964 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 struct dx_hash_info *hinfo,
966 __u32 start_hash, __u32 start_minor_hash)
967{
968 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700969 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400970 int err = 0, count = 0;
Theodore Ts'o2f618302015-04-12 00:56:26 -0400971 struct ext4_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500973 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
974 (unsigned long)block));
Theodore Ts'odc6982f2013-02-14 23:59:26 -0500975 bh = ext4_read_dirblock(dir, block, DIRENT);
976 if (IS_ERR(bh))
977 return PTR_ERR(bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400978
Mingming Cao617ba132006-10-11 01:20:53 -0700979 de = (struct ext4_dir_entry_2 *) bh->b_data;
980 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700982 EXT4_DIR_REC_LEN(0));
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500983 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -0500984 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -0500985 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -0500986 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
987 + ((char *)de - bh->b_data))) {
Al Viro64cb9272013-07-01 08:12:38 -0400988 /* silently ignore the rest of the block */
989 break;
Eric Sandeene6c40212006-12-06 20:36:28 -0800990 }
Mingming Cao617ba132006-10-11 01:20:53 -0700991 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 if ((hinfo->hash < start_hash) ||
993 ((hinfo->hash == start_hash) &&
994 (hinfo->minor_hash < start_minor_hash)))
995 continue;
996 if (de->inode == 0)
997 continue;
Theodore Ts'o2f618302015-04-12 00:56:26 -0400998 tmp_str.name = de->name;
999 tmp_str.len = de->name_len;
1000 err = ext4_htree_store_dirent(dir_file,
1001 hinfo->hash, hinfo->minor_hash, de, &tmp_str);
1002 if (err != 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 brelse(bh);
1004 return err;
1005 }
1006 count++;
1007 }
1008 brelse(bh);
1009 return count;
1010}
1011
1012
1013/*
1014 * This function fills a red-black tree with information from a
1015 * directory. We start scanning the directory in hash order, starting
1016 * at start_hash and start_minor_hash.
1017 *
1018 * This function returns the number of entries inserted into the tree,
1019 * or a negative error code.
1020 */
Mingming Cao617ba132006-10-11 01:20:53 -07001021int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001022 __u32 start_minor_hash, __u32 *next_hash)
1023{
1024 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -07001025 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 struct dx_frame frames[2], *frame;
1027 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001028 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001030 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031 __u32 hashval;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001032 struct ext4_str tmp_str;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001033
Theodore Ts'o60e66792010-05-17 07:00:00 -04001034 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001035 start_hash, start_minor_hash));
Al Viro496ad9a2013-01-23 17:07:38 -05001036 dir = file_inode(dir_file);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001037 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001038 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001039 if (hinfo.hash_version <= DX_HASH_TEA)
1040 hinfo.hash_version +=
1041 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001042 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Tao Ma8af0f082013-04-19 17:53:09 -04001043 if (ext4_has_inline_data(dir)) {
1044 int has_inline_data = 1;
1045 count = htree_inlinedir_to_tree(dir_file, dir, 0,
1046 &hinfo, start_hash,
1047 start_minor_hash,
1048 &has_inline_data);
1049 if (has_inline_data) {
1050 *next_hash = ~0;
1051 return count;
1052 }
1053 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1055 start_hash, start_minor_hash);
1056 *next_hash = ~0;
1057 return count;
1058 }
1059 hinfo.hash = start_hash;
1060 hinfo.minor_hash = 0;
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001061 frame = dx_probe(NULL, dir, &hinfo, frames);
1062 if (IS_ERR(frame))
1063 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064
1065 /* Add '.' and '..' from the htree header */
1066 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -07001067 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Theodore Ts'o2f618302015-04-12 00:56:26 -04001068 tmp_str.name = de->name;
1069 tmp_str.len = de->name_len;
1070 err = ext4_htree_store_dirent(dir_file, 0, 0,
1071 de, &tmp_str);
1072 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 goto errout;
1074 count++;
1075 }
1076 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001077 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001078 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Theodore Ts'o2f618302015-04-12 00:56:26 -04001079 tmp_str.name = de->name;
1080 tmp_str.len = de->name_len;
1081 err = ext4_htree_store_dirent(dir_file, 2, 0,
1082 de, &tmp_str);
1083 if (err != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 goto errout;
1085 count++;
1086 }
1087
1088 while (1) {
1089 block = dx_get_block(frame->at);
1090 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1091 start_hash, start_minor_hash);
1092 if (ret < 0) {
1093 err = ret;
1094 goto errout;
1095 }
1096 count += ret;
1097 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -07001098 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099 frame, frames, &hashval);
1100 *next_hash = hashval;
1101 if (ret < 0) {
1102 err = ret;
1103 goto errout;
1104 }
1105 /*
1106 * Stop if: (a) there are no more entries, or
1107 * (b) we have inserted at least one entry and the
1108 * next hash value is not a continuation
1109 */
1110 if ((ret == 0) ||
1111 (count && ((hashval & 1) == 0)))
1112 break;
1113 }
1114 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001115 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1116 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 return count;
1118errout:
1119 dx_release(frames);
1120 return (err);
1121}
1122
Tao Ma7335cd32012-12-10 14:05:59 -05001123static inline int search_dirblock(struct buffer_head *bh,
1124 struct inode *dir,
1125 const struct qstr *d_name,
1126 unsigned int offset,
1127 struct ext4_dir_entry_2 **res_dir)
1128{
1129 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1130 d_name, offset, res_dir);
1131}
1132
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133/*
1134 * Directory block splitting, compacting
1135 */
1136
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001137/*
1138 * Create map of hash values, offsets, and sizes, stored at end of block.
1139 * Returns number of entries mapped.
1140 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001141static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1142 struct dx_hash_info *hinfo,
1143 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144{
1145 int count = 0;
1146 char *base = (char *) de;
1147 struct dx_hash_info h = *hinfo;
1148
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001149 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001151 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 map_tail--;
1153 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001154 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001155 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 count++;
1157 cond_resched();
1158 }
1159 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001160 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 }
1162 return count;
1163}
1164
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001165/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1167{
Andrew Morton63f57932006-10-11 01:21:24 -07001168 struct dx_map_entry *p, *q, *top = map + count - 1;
1169 int more;
1170 /* Combsort until bubble sort doesn't suck */
1171 while (count > 2) {
1172 count = count*10/13;
1173 if (count - 9 < 2) /* 9, 10 -> 11 */
1174 count = 11;
1175 for (p = top, q = p - count; q >= map; p--, q--)
1176 if (p->hash < q->hash)
1177 swap(*p, *q);
1178 }
1179 /* Garden variety bubble sort */
1180 do {
1181 more = 0;
1182 q = top;
1183 while (q-- > map) {
1184 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001186 swap(*(q+1), *q);
1187 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 }
1189 } while(more);
1190}
1191
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001192static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193{
1194 struct dx_entry *entries = frame->entries;
1195 struct dx_entry *old = frame->at, *new = old + 1;
1196 int count = dx_get_count(entries);
1197
1198 assert(count < dx_get_limit(entries));
1199 assert(old < entries + count);
1200 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1201 dx_set_hash(new, hash);
1202 dx_set_block(new, block);
1203 dx_set_count(entries, count + 1);
1204}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001205
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206/*
Mingming Cao617ba132006-10-11 01:20:53 -07001207 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 *
Mingming Cao617ba132006-10-11 01:20:53 -07001209 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 * `de != NULL' is guaranteed by caller.
1211 */
Mingming Cao617ba132006-10-11 01:20:53 -07001212static inline int ext4_match (int len, const char * const name,
1213 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001214{
1215 if (len != de->name_len)
1216 return 0;
1217 if (!de->inode)
1218 return 0;
1219 return !memcmp(name, de->name, len);
1220}
1221
1222/*
1223 * Returns 0 if not found, -1 on failure, and 1 on success
1224 */
Tao Ma7335cd32012-12-10 14:05:59 -05001225int search_dir(struct buffer_head *bh,
1226 char *search_buf,
1227 int buf_size,
1228 struct inode *dir,
1229 const struct qstr *d_name,
1230 unsigned int offset,
1231 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232{
Mingming Cao617ba132006-10-11 01:20:53 -07001233 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 char * dlimit;
1235 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001236 const char *name = d_name->name;
1237 int namelen = d_name->len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238
Tao Ma7335cd32012-12-10 14:05:59 -05001239 de = (struct ext4_dir_entry_2 *)search_buf;
1240 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 while ((char *) de < dlimit) {
1242 /* this code is executed quadratically often */
1243 /* do minimal checking `by hand' */
1244
1245 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -07001246 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 /* found a match - just to be sure, do a full check */
Tao Ma226ba972012-12-10 14:05:58 -05001248 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1249 bh->b_size, offset))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001250 return -1;
1251 *res_dir = de;
1252 return 1;
1253 }
1254 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001255 de_len = ext4_rec_len_from_disk(de->rec_len,
1256 dir->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 if (de_len <= 0)
1258 return -1;
1259 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001260 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261 }
1262 return 0;
1263}
1264
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001265static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1266 struct ext4_dir_entry *de)
1267{
1268 struct super_block *sb = dir->i_sb;
1269
1270 if (!is_dx(dir))
1271 return 0;
1272 if (block == 0)
1273 return 1;
1274 if (de->inode == 0 &&
1275 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1276 sb->s_blocksize)
1277 return 1;
1278 return 0;
1279}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280
1281/*
Mingming Cao617ba132006-10-11 01:20:53 -07001282 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 *
1284 * finds an entry in the specified directory with the wanted name. It
1285 * returns the cache buffer in which the entry was found, and the entry
1286 * itself (as a parameter - res_dir). It does NOT read the inode of the
1287 * entry - you'll have to do that yourself if you want to.
1288 *
1289 * The returned buffer_head has ->b_count elevated. The caller is expected
1290 * to brelse() it when appropriate.
1291 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001292static struct buffer_head * ext4_find_entry (struct inode *dir,
1293 const struct qstr *d_name,
Tao Ma32f7f222012-12-10 14:06:01 -05001294 struct ext4_dir_entry_2 **res_dir,
1295 int *inlined)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001296{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001297 struct super_block *sb;
1298 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1299 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001300 ext4_lblk_t start, block, b;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001301 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 int ra_max = 0; /* Number of bh's in the readahead
1303 buffer, bh_use[] */
1304 int ra_ptr = 0; /* Current index into readahead
1305 buffer */
1306 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001307 ext4_lblk_t nblocks;
Theodore Ts'o10560082014-08-29 20:51:32 -04001308 int i, namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309
1310 *res_dir = NULL;
1311 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001312 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001313 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001314 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001315
1316 if (ext4_has_inline_data(dir)) {
1317 int has_inline_data = 1;
1318 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1319 &has_inline_data);
Tao Ma32f7f222012-12-10 14:06:01 -05001320 if (has_inline_data) {
1321 if (inlined)
1322 *inlined = 1;
Tao Mae8e948e2012-12-10 14:06:00 -05001323 return ret;
Tao Ma32f7f222012-12-10 14:06:01 -05001324 }
Tao Mae8e948e2012-12-10 14:06:00 -05001325 }
1326
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001327 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001328 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001329 /*
1330 * "." or ".." will only be in the first block
1331 * NFS may look up ".."; "." should be handled by the VFS
1332 */
1333 block = start = 0;
1334 nblocks = 1;
1335 goto restart;
1336 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001337 if (is_dx(dir)) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001338 bh = ext4_dx_find_entry(dir, d_name, res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339 /*
1340 * On success, or if the error was file not found,
1341 * return. Otherwise, fall back to doing a search the
1342 * old fashioned way.
1343 */
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001344 if (!IS_ERR(bh) || PTR_ERR(bh) != ERR_BAD_DX_DIR)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001346 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1347 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001348 }
Mingming Cao617ba132006-10-11 01:20:53 -07001349 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1350 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 if (start >= nblocks)
1352 start = 0;
1353 block = start;
1354restart:
1355 do {
1356 /*
1357 * We deal with the read-ahead logic here.
1358 */
1359 if (ra_ptr >= ra_max) {
1360 /* Refill the readahead buffer */
1361 ra_ptr = 0;
1362 b = block;
1363 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1364 /*
1365 * Terminate if we reach the end of the
1366 * directory and must wrap, or if our
1367 * search has finished at this block.
1368 */
1369 if (b >= nblocks || (num && block == start)) {
1370 bh_use[ra_max] = NULL;
1371 break;
1372 }
1373 num++;
Theodore Ts'o10560082014-08-29 20:51:32 -04001374 bh = ext4_getblk(NULL, dir, b++, 0);
1375 if (unlikely(IS_ERR(bh))) {
Theodore Ts'o36de9282014-08-23 17:47:19 -04001376 if (ra_max == 0)
Theodore Ts'o10560082014-08-29 20:51:32 -04001377 return bh;
Theodore Ts'o36de9282014-08-23 17:47:19 -04001378 break;
1379 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001380 bh_use[ra_max] = bh;
1381 if (bh)
Christoph Hellwig65299a32011-08-23 14:50:29 +02001382 ll_rw_block(READ | REQ_META | REQ_PRIO,
1383 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384 }
1385 }
1386 if ((bh = bh_use[ra_ptr++]) == NULL)
1387 goto next;
1388 wait_on_buffer(bh);
1389 if (!buffer_uptodate(bh)) {
1390 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001391 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1392 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 brelse(bh);
1394 goto next;
1395 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001396 if (!buffer_verified(bh) &&
Darrick J. Wongc6af8802012-11-12 23:51:02 -05001397 !is_dx_internal_node(dir, block,
1398 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001399 !ext4_dirent_csum_verify(dir,
1400 (struct ext4_dir_entry *)bh->b_data)) {
1401 EXT4_ERROR_INODE(dir, "checksumming directory "
1402 "block %lu", (unsigned long)block);
1403 brelse(bh);
1404 goto next;
1405 }
1406 set_buffer_verified(bh);
Theodore Ts'of702ba02008-09-22 15:21:01 -04001407 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001408 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001410 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001411 ret = bh;
1412 goto cleanup_and_exit;
1413 } else {
1414 brelse(bh);
1415 if (i < 0)
1416 goto cleanup_and_exit;
1417 }
1418 next:
1419 if (++block >= nblocks)
1420 block = 0;
1421 } while (block != start);
1422
1423 /*
1424 * If the directory has grown while we were searching, then
1425 * search the last part of the directory before giving up.
1426 */
1427 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001428 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 if (block < nblocks) {
1430 start = 0;
1431 goto restart;
1432 }
1433
1434cleanup_and_exit:
1435 /* Clean up the read-ahead blocks */
1436 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001437 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 return ret;
1439}
1440
Theodore Ts'of702ba02008-09-22 15:21:01 -04001441static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001442 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001444 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445 struct dx_hash_info hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001446 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001448 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04001451 frame = dx_probe(d_name, dir, &hinfo, frames);
1452 if (IS_ERR(frame))
1453 return (struct buffer_head *) frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 do {
1455 block = dx_get_block(frame->at);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05001456 bh = ext4_read_dirblock(dir, block, DIRENT);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001457 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 goto errout;
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001459
Theodore Ts'o7845c042010-10-27 21:30:08 -04001460 retval = search_dirblock(bh, dir, d_name,
1461 block << EXT4_BLOCK_SIZE_BITS(sb),
1462 res_dir);
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001463 if (retval == 1)
1464 goto success;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001465 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001466 if (retval == -1) {
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001467 bh = ERR_PTR(ERR_BAD_DX_DIR);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001468 goto errout;
1469 }
1470
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 /* Check to see if we should continue to search */
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001472 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473 frames, NULL);
1474 if (retval < 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001475 ext4_warning(sb,
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001476 "error %d reading index page in directory #%lu",
1477 retval, dir->i_ino);
1478 bh = ERR_PTR(retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001479 goto errout;
1480 }
1481 } while (retval == 1);
1482
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001483 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484errout:
Bernd Schubert265c6a02011-07-16 19:41:23 -04001485 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
Theodore Ts'o537d8f92014-08-29 20:49:51 -04001486success:
1487 dx_release(frames);
1488 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490
Al Viro00cd8dd2012-06-10 17:13:09 -04001491static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001493 struct inode *inode;
1494 struct ext4_dir_entry_2 *de;
1495 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001496
Mingming Cao617ba132006-10-11 01:20:53 -07001497 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 return ERR_PTR(-ENAMETOOLONG);
1499
Tao Ma32f7f222012-12-10 14:06:01 -05001500 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001501 if (IS_ERR(bh))
1502 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503 inode = NULL;
1504 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001505 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001506 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001507 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001508 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001509 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001510 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001511 if (unlikely(ino == dir->i_ino)) {
David Howellsa34e15c2014-01-06 14:04:23 -05001512 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1513 dentry);
Andreas Dilger7e936b72012-05-28 17:02:25 -04001514 return ERR_PTR(-EIO);
1515 }
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001516 inode = ext4_iget_normal(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001517 if (inode == ERR_PTR(-ESTALE)) {
1518 EXT4_ERROR_INODE(dir,
1519 "deleted inode referenced: %u",
1520 ino);
1521 return ERR_PTR(-EIO);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001522 }
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04001523 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
1524 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1525 S_ISLNK(inode->i_mode)) &&
1526 !ext4_is_child_context_consistent_with_parent(dir,
1527 inode)) {
1528 iput(inode);
1529 ext4_warning(inode->i_sb,
1530 "Inconsistent encryption contexts: %lu/%lu\n",
1531 (unsigned long) dir->i_ino,
1532 (unsigned long) inode->i_ino);
1533 return ERR_PTR(-EPERM);
1534 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535 }
1536 return d_splice_alias(inode, dentry);
1537}
1538
1539
Mingming Cao617ba132006-10-11 01:20:53 -07001540struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001542 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001543 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001544 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 struct buffer_head *bh;
1546
Tao Ma32f7f222012-12-10 14:06:01 -05001547 bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04001548 if (IS_ERR(bh))
1549 return (struct dentry *) bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550 if (!bh)
1551 return ERR_PTR(-ENOENT);
1552 ino = le32_to_cpu(de->inode);
1553 brelse(bh);
1554
Mingming Cao617ba132006-10-11 01:20:53 -07001555 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001556 EXT4_ERROR_INODE(child->d_inode,
1557 "bad parent inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001558 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001559 }
1560
Theodore Ts'of4bb2982014-10-05 22:56:00 -04001561 return d_obtain_alias(ext4_iget_normal(child->d_inode->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562}
1563
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001564/*
1565 * Move count entries from end of map between two memory locations.
1566 * Returns pointer to last entry moved.
1567 */
Mingming Cao617ba132006-10-11 01:20:53 -07001568static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001569dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1570 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571{
1572 unsigned rec_len = 0;
1573
1574 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001575 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001576 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001577 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001579 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001580 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001581 de->inode = 0;
1582 map++;
1583 to += rec_len;
1584 }
Mingming Cao617ba132006-10-11 01:20:53 -07001585 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586}
1587
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001588/*
1589 * Compact each dir entry in the range to the minimal rec_len.
1590 * Returns pointer to last entry in range.
1591 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001592static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593{
Mingming Cao617ba132006-10-11 01:20:53 -07001594 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001595 unsigned rec_len = 0;
1596
1597 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001598 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001599 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001600 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001601 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 if (de > to)
1603 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001604 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001606 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 }
1608 de = next;
1609 }
1610 return prev;
1611}
1612
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001613/*
1614 * Split a full leaf block to make room for a new dir entry.
1615 * Allocate a new block, and move entries so that they are approx. equally full.
1616 * Returns pointer to de in block into which the new entry will be inserted.
1617 */
Mingming Cao617ba132006-10-11 01:20:53 -07001618static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 struct buffer_head **bh,struct dx_frame *frame,
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001620 struct dx_hash_info *hinfo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621{
1622 unsigned blocksize = dir->i_sb->s_blocksize;
1623 unsigned count, continued;
1624 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001625 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626 u32 hash2;
1627 struct dx_map_entry *map;
1628 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001629 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001630 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001631 struct ext4_dir_entry_tail *t;
1632 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001633 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001635 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001636 csum_size = sizeof(struct ext4_dir_entry_tail);
1637
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001638 bh2 = ext4_append(handle, dir, &newblock);
1639 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640 brelse(*bh);
1641 *bh = NULL;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001642 return (struct ext4_dir_entry_2 *) bh2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643 }
1644
1645 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001646 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001647 if (err)
1648 goto journal_error;
1649
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001651 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 if (err)
1653 goto journal_error;
1654
1655 data2 = bh2->b_data;
1656
1657 /* create map in the end of data2 block */
1658 map = (struct dx_map_entry *) (data2 + blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001659 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660 blocksize, hinfo, map);
1661 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001662 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001663 /* Split the existing block in the middle, size-wise */
1664 size = 0;
1665 move = 0;
1666 for (i = count-1; i >= 0; i--) {
1667 /* is more than half of this entry in 2nd half of the block? */
1668 if (size + map[i].size/2 > blocksize/2)
1669 break;
1670 size += map[i].size;
1671 move++;
1672 }
1673 /* map index at which we will split */
1674 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675 hash2 = map[split].hash;
1676 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001677 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1678 (unsigned long)dx_get_block(frame->at),
1679 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001680
1681 /* Fancy dance to stay within two buffers */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001682 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001683 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001684 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1685 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001686 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001687 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1688 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001689 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001690 if (csum_size) {
1691 t = EXT4_DIRENT_TAIL(data2, blocksize);
1692 initialize_dirent_tail(t, blocksize);
1693
1694 t = EXT4_DIRENT_TAIL(data1, blocksize);
1695 initialize_dirent_tail(t, blocksize);
1696 }
1697
Michael Halcrowb3098482015-04-12 01:07:01 -04001698 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1699 blocksize, 1));
1700 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1701 blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001702
1703 /* Which block gets the new entry? */
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001704 if (hinfo->hash >= hash2) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705 swap(*bh, bh2);
1706 de = de2;
1707 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001708 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001709 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 if (err)
1711 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001712 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 if (err)
1714 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001715 brelse(bh2);
1716 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001718
1719journal_error:
1720 brelse(*bh);
1721 brelse(bh2);
1722 *bh = NULL;
1723 ext4_std_error(dir->i_sb, err);
Theodore Ts'of8b3b592014-08-29 20:52:18 -04001724 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001725}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001726
Tao Ma978fef92012-12-10 14:05:58 -05001727int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1728 struct buffer_head *bh,
1729 void *buf, int buf_size,
1730 const char *name, int namelen,
1731 struct ext4_dir_entry_2 **dest_de)
1732{
1733 struct ext4_dir_entry_2 *de;
1734 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1735 int nlen, rlen;
1736 unsigned int offset = 0;
1737 char *top;
1738
1739 de = (struct ext4_dir_entry_2 *)buf;
1740 top = buf + buf_size - reclen;
1741 while ((char *) de <= top) {
1742 if (ext4_check_dir_entry(dir, NULL, de, bh,
1743 buf, buf_size, offset))
1744 return -EIO;
1745 if (ext4_match(namelen, name, de))
1746 return -EEXIST;
1747 nlen = EXT4_DIR_REC_LEN(de->name_len);
1748 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1749 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1750 break;
1751 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1752 offset += rlen;
1753 }
1754 if ((char *) de > top)
1755 return -ENOSPC;
1756
1757 *dest_de = de;
1758 return 0;
1759}
1760
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001761int ext4_insert_dentry(struct inode *dir,
1762 struct inode *inode,
1763 struct ext4_dir_entry_2 *de,
1764 int buf_size,
1765 const struct qstr *iname,
1766 const char *name, int namelen)
Tao Ma978fef92012-12-10 14:05:58 -05001767{
1768
1769 int nlen, rlen;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001770 struct ext4_fname_crypto_ctx *ctx = NULL;
1771 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1772 struct ext4_str tmp_str;
1773 int res;
1774
1775 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1776 if (IS_ERR(ctx))
1777 return -EIO;
1778 /* By default, the input name would be written to the disk */
1779 tmp_str.name = (unsigned char *)name;
1780 tmp_str.len = namelen;
1781 if (ctx != NULL) {
1782 /* Directory is encrypted */
1783 res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1784 &fname_crypto_str);
1785 if (res < 0) {
1786 ext4_put_fname_crypto_ctx(&ctx);
1787 return -ENOMEM;
1788 }
1789 res = ext4_fname_usr_to_disk(ctx, iname, &fname_crypto_str);
1790 if (res < 0) {
1791 ext4_put_fname_crypto_ctx(&ctx);
1792 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1793 return res;
1794 }
1795 tmp_str.name = fname_crypto_str.name;
1796 tmp_str.len = fname_crypto_str.len;
1797 }
Tao Ma978fef92012-12-10 14:05:58 -05001798
1799 nlen = EXT4_DIR_REC_LEN(de->name_len);
1800 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1801 if (de->inode) {
1802 struct ext4_dir_entry_2 *de1 =
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001803 (struct ext4_dir_entry_2 *)((char *)de + nlen);
Tao Ma978fef92012-12-10 14:05:58 -05001804 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1805 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1806 de = de1;
1807 }
1808 de->file_type = EXT4_FT_UNKNOWN;
1809 de->inode = cpu_to_le32(inode->i_ino);
1810 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001811 de->name_len = tmp_str.len;
1812
1813 memcpy(de->name, tmp_str.name, tmp_str.len);
1814 ext4_put_fname_crypto_ctx(&ctx);
1815 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1816 return 0;
Tao Ma978fef92012-12-10 14:05:58 -05001817}
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001818
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819/*
1820 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1821 * it points to a directory entry which is guaranteed to be large
1822 * enough for new directory entry. If de is NULL, then
1823 * add_dirent_to_buf will attempt search the directory block for
1824 * space. It will return -ENOSPC if no space is available, and -EIO
1825 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 */
1827static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001828 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001829 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001830{
1831 struct inode *dir = dentry->d_parent->d_inode;
1832 const char *name = dentry->d_name.name;
1833 int namelen = dentry->d_name.len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001834 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001835 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001836 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001837
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001838 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001839 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001841 if (!de) {
Tao Ma978fef92012-12-10 14:05:58 -05001842 err = ext4_find_dest_de(dir, inode,
1843 bh, bh->b_data, blocksize - csum_size,
1844 name, namelen, &de);
1845 if (err)
1846 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847 }
1848 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001849 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001851 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001852 return err;
1853 }
1854
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001855 /* By now the buffer is marked for journaling. Due to crypto operations,
1856 * the following function call may fail */
1857 err = ext4_insert_dentry(dir, inode, de, blocksize, &dentry->d_name,
1858 name, namelen);
1859 if (err < 0)
1860 return err;
Tao Ma978fef92012-12-10 14:05:58 -05001861
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862 /*
1863 * XXX shouldn't update any times until successful
1864 * completion of syscall, but too many callers depend
1865 * on this.
1866 *
1867 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001868 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 * recovery deletes the inode, so the worst that can
1870 * happen is that the times are slightly out of date
1871 * and/or different from the directory change time.
1872 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001873 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001874 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001876 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001877 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001878 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001880 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881 return 0;
1882}
1883
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884/*
1885 * This converts a one block unindexed directory to a 3 block indexed
1886 * directory, and adds the dentry to the indexed directory.
1887 */
1888static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1889 struct inode *inode, struct buffer_head *bh)
1890{
1891 struct inode *dir = dentry->d_parent->d_inode;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001892#ifdef CONFIG_EXT4_FS_ENCRYPTION
1893 struct ext4_fname_crypto_ctx *ctx = NULL;
1894 int res;
1895#else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 const char *name = dentry->d_name.name;
1897 int namelen = dentry->d_name.len;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001898#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001899 struct buffer_head *bh2;
1900 struct dx_root *root;
1901 struct dx_frame frames[2], *frame;
1902 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001903 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001904 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001905 char *data1, *top;
1906 unsigned len;
1907 int retval;
1908 unsigned blocksize;
1909 struct dx_hash_info hinfo;
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
1914#ifdef CONFIG_EXT4_FS_ENCRYPTION
1915 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1916 if (IS_ERR(ctx))
1917 return PTR_ERR(ctx);
1918#endif
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001919
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04001920 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001921 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922
1923 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001924 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
liang xie5d601252014-05-12 22:06:43 -04001925 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001926 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001927 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001928 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001929 brelse(bh);
1930 return retval;
1931 }
1932 root = (struct dx_root *) bh->b_data;
1933
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001934 /* The 0th block becomes the root, move the dirents out */
1935 fde = &root->dotdot;
1936 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001937 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001938 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001939 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001940 brelse(bh);
1941 return -EIO;
1942 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001943 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001944
1945 /* Allocate new block for the 0th block's dirents */
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001946 bh2 = ext4_append(handle, dir, &block);
1947 if (IS_ERR(bh2)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001948 brelse(bh);
Theodore Ts'o0f70b402013-02-15 03:35:57 -05001949 return PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001951 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952 data1 = bh2->b_data;
1953
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001954 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001955 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001957 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001958 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001959 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1960 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001961 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001962
1963 if (csum_size) {
1964 t = EXT4_DIRENT_TAIL(data1, blocksize);
1965 initialize_dirent_tail(t, blocksize);
1966 }
1967
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001969 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001970 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1971 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 memset (&root->info, 0, sizeof(root->info));
1973 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001974 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001976 dx_set_block(entries, 1);
1977 dx_set_count(entries, 1);
1978 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979
1980 /* Initialize as for dx_probe */
1981 hinfo.hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001982 if (hinfo.hash_version <= DX_HASH_TEA)
1983 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001984 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001985#ifdef CONFIG_EXT4_FS_ENCRYPTION
1986 res = ext4_fname_usr_to_hash(ctx, &dentry->d_name, &hinfo);
1987 if (res < 0) {
1988 ext4_put_fname_crypto_ctx(&ctx);
1989 ext4_mark_inode_dirty(handle, dir);
1990 brelse(bh);
1991 return res;
1992 }
1993 ext4_put_fname_crypto_ctx(&ctx);
1994#else
Mingming Cao617ba132006-10-11 01:20:53 -07001995 ext4fs_dirhash(name, namelen, &hinfo);
Michael Halcrow4bdfc872015-04-12 00:56:28 -04001996#endif
Jan Kara6050d472014-10-30 10:53:17 -04001997 memset(frames, 0, sizeof(frames));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 frame = frames;
1999 frame->entries = entries;
2000 frame->at = entries;
2001 frame->bh = bh;
2002 bh = bh2;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002003
Jan Kara6050d472014-10-30 10:53:17 -04002004 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2005 if (retval)
2006 goto out_frames;
2007 retval = ext4_handle_dirty_dirent_node(handle, dir, bh);
2008 if (retval)
2009 goto out_frames;
Allison Henderson6976a6f2011-05-15 00:19:41 -04002010
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002011 de = do_split(handle,dir, &bh, frame, &hinfo);
2012 if (IS_ERR(de)) {
Jan Kara6050d472014-10-30 10:53:17 -04002013 retval = PTR_ERR(de);
2014 goto out_frames;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04002015 }
2016 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002017
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002018 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
2019 brelse(bh);
2020 return retval;
Jan Kara6050d472014-10-30 10:53:17 -04002021out_frames:
2022 /*
2023 * Even if the block split failed, we have to properly write
2024 * out all the changes we did so far. Otherwise we can end up
2025 * with corrupted filesystem.
2026 */
2027 ext4_mark_inode_dirty(handle, dir);
2028 dx_release(frames);
2029 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031
2032/*
Mingming Cao617ba132006-10-11 01:20:53 -07002033 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 *
2035 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07002036 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 *
2038 * NOTE!! The inode part of 'de' is left at 0 - which means you
2039 * may not sleep between calling this and putting something into
2040 * the entry, as someone else might have used it while you slept.
2041 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002042static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2043 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044{
2045 struct inode *dir = dentry->d_parent->d_inode;
Lukas Czernere12fb972015-04-03 10:46:58 -04002046 struct buffer_head *bh = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07002047 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002048 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002049 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002051 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002053 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002054 int csum_size = 0;
2055
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002056 if (ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002057 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058
2059 sb = dir->i_sb;
2060 blocksize = sb->s_blocksize;
2061 if (!dentry->d_name.len)
2062 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05002063
2064 if (ext4_has_inline_data(dir)) {
2065 retval = ext4_try_add_inline_entry(handle, dentry, inode);
2066 if (retval < 0)
2067 return retval;
2068 if (retval == 1) {
2069 retval = 0;
Lukas Czernere12fb972015-04-03 10:46:58 -04002070 goto out;
Tao Ma3c47d542012-12-10 14:05:59 -05002071 }
2072 }
2073
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002074 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002075 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 if (!retval || (retval != ERR_BAD_DX_DIR))
Lukas Czernere12fb972015-04-03 10:46:58 -04002077 goto out;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002078 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07002080 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002083 for (block = 0; block < blocks; block++) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002084 bh = ext4_read_dirblock(dir, block, DIRENT);
2085 if (IS_ERR(bh))
2086 return PTR_ERR(bh);
2087
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002089 if (retval != -ENOSPC)
2090 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002091
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092 if (blocks == 1 && !dx_fallback &&
Lukas Czernere12fb972015-04-03 10:46:58 -04002093 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
2094 retval = make_indexed_dir(handle, dentry, inode, bh);
2095 bh = NULL; /* make_indexed_dir releases bh */
2096 goto out;
2097 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002098 brelse(bh);
2099 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002100 bh = ext4_append(handle, dir, &block);
2101 if (IS_ERR(bh))
2102 return PTR_ERR(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002103 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002105 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2106
2107 if (csum_size) {
2108 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
2109 initialize_dirent_tail(t, blocksize);
2110 }
2111
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002112 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
Lukas Czernere12fb972015-04-03 10:46:58 -04002113out:
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002114 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04002115 if (retval == 0)
2116 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002117 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118}
2119
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002120/*
2121 * Returns 0 for success, or a negative error value
2122 */
Mingming Cao617ba132006-10-11 01:20:53 -07002123static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002124 struct inode *inode)
2125{
2126 struct dx_frame frames[2], *frame;
2127 struct dx_entry *entries, *at;
2128 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002129 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002131 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002132 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133 int err;
2134
Theodore Ts'odd73b5d2014-08-29 20:52:17 -04002135 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames);
2136 if (IS_ERR(frame))
2137 return PTR_ERR(frame);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138 entries = frame->entries;
2139 at = frame->at;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002140 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2141 if (IS_ERR(bh)) {
2142 err = PTR_ERR(bh);
2143 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002145 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146
2147 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002148 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149 if (err)
2150 goto journal_error;
2151
2152 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002153 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002155
2156 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002157 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158 dx_get_count(entries), dx_get_limit(entries)));
2159 /* Need to split index? */
2160 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002161 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162 unsigned icount = dx_get_count(entries);
2163 int levels = frame - frames;
2164 struct dx_entry *entries2;
2165 struct dx_node *node2;
2166 struct buffer_head *bh2;
2167
2168 if (levels && (dx_get_count(frames->entries) ==
2169 dx_get_limit(frames->entries))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002170 ext4_warning(sb, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002171 err = -ENOSPC;
2172 goto cleanup;
2173 }
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002174 bh2 = ext4_append(handle, dir, &newblock);
2175 if (IS_ERR(bh2)) {
2176 err = PTR_ERR(bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002177 goto cleanup;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002178 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 node2 = (struct dx_node *)(bh2->b_data);
2180 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002181 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002182 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2183 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002185 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186 if (err)
2187 goto journal_error;
2188 if (levels) {
2189 unsigned icount1 = icount/2, icount2 = icount - icount1;
2190 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002191 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2192 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193
2194 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002195 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196 frames[0].bh);
2197 if (err)
2198 goto journal_error;
2199
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002200 memcpy((char *) entries2, (char *) (entries + icount1),
2201 icount2 * sizeof(struct dx_entry));
2202 dx_set_count(entries, icount1);
2203 dx_set_count(entries2, icount2);
2204 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205
2206 /* Which index block gets the new entry? */
2207 if (at - entries >= icount1) {
2208 frame->at = at = at - entries - icount1 + entries2;
2209 frame->entries = entries = entries2;
2210 swap(frame->bh, bh2);
2211 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002212 dx_insert_block(frames + 0, hash2, newblock);
2213 dxtrace(dx_show_index("node", frames[1].entries));
2214 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002216 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217 if (err)
2218 goto journal_error;
2219 brelse (bh2);
2220 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002221 dxtrace(printk(KERN_DEBUG
2222 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002223 memcpy((char *) entries2, (char *) entries,
2224 icount * sizeof(struct dx_entry));
2225 dx_set_limit(entries2, dx_node_limit(dir));
2226
2227 /* Set up root */
2228 dx_set_count(entries, 1);
2229 dx_set_block(entries + 0, newblock);
2230 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2231
2232 /* Add new access path frame */
2233 frame = frames + 1;
2234 frame->at = at = at - entries + entries2;
2235 frame->entries = entries = entries2;
2236 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002237 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238 frame->bh);
2239 if (err)
2240 goto journal_error;
2241 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002242 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002243 if (err) {
2244 ext4_std_error(inode->i_sb, err);
2245 goto cleanup;
2246 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002247 }
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002248 de = do_split(handle, dir, &bh, frame, &hinfo);
2249 if (IS_ERR(de)) {
2250 err = PTR_ERR(de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002251 goto cleanup;
Theodore Ts'of8b3b592014-08-29 20:52:18 -04002252 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002254 goto cleanup;
2255
2256journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002257 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258cleanup:
Guo Chaob1deefc2013-01-28 21:41:02 -05002259 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 dx_release(frames);
2261 return err;
2262}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263
2264/*
Tao Ma05019a92012-12-10 14:06:00 -05002265 * ext4_generic_delete_entry deletes a directory entry by merging it
2266 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 */
Tao Ma05019a92012-12-10 14:06:00 -05002268int ext4_generic_delete_entry(handle_t *handle,
2269 struct inode *dir,
2270 struct ext4_dir_entry_2 *de_del,
2271 struct buffer_head *bh,
2272 void *entry_buf,
2273 int buf_size,
2274 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002275{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002276 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002277 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002278 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002279
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280 i = 0;
2281 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002282 de = (struct ext4_dir_entry_2 *)entry_buf;
2283 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002284 if (ext4_check_dir_entry(dir, NULL, de, bh,
2285 bh->b_data, bh->b_size, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 return -EIO;
2287 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002289 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002290 ext4_rec_len_from_disk(pde->rec_len,
2291 blocksize) +
2292 ext4_rec_len_from_disk(de->rec_len,
2293 blocksize),
2294 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 else
2296 de->inode = 0;
2297 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298 return 0;
2299 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002300 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002301 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002302 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002303 }
2304 return -ENOENT;
2305}
2306
Tao Ma05019a92012-12-10 14:06:00 -05002307static int ext4_delete_entry(handle_t *handle,
2308 struct inode *dir,
2309 struct ext4_dir_entry_2 *de_del,
2310 struct buffer_head *bh)
2311{
2312 int err, csum_size = 0;
2313
Tao Ma9f40fe52012-12-10 14:06:00 -05002314 if (ext4_has_inline_data(dir)) {
2315 int has_inline_data = 1;
2316 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2317 &has_inline_data);
2318 if (has_inline_data)
2319 return err;
2320 }
2321
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002322 if (ext4_has_metadata_csum(dir->i_sb))
Tao Ma05019a92012-12-10 14:06:00 -05002323 csum_size = sizeof(struct ext4_dir_entry_tail);
2324
2325 BUFFER_TRACE(bh, "get_write_access");
2326 err = ext4_journal_get_write_access(handle, bh);
2327 if (unlikely(err))
2328 goto out;
2329
2330 err = ext4_generic_delete_entry(handle, dir, de_del,
2331 bh, bh->b_data,
2332 dir->i_sb->s_blocksize, csum_size);
2333 if (err)
2334 goto out;
2335
2336 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2337 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2338 if (unlikely(err))
2339 goto out;
2340
2341 return 0;
2342out:
2343 if (err != -ENOENT)
2344 ext4_std_error(dir->i_sb, err);
2345 return err;
2346}
2347
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002348/*
2349 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2350 * since this indicates that nlinks count was previously 1.
2351 */
2352static void ext4_inc_count(handle_t *handle, struct inode *inode)
2353{
2354 inc_nlink(inode);
2355 if (is_dx(inode) && inode->i_nlink > 1) {
2356 /* limit is 16-bit i_links_count */
2357 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002358 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002359 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2360 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2361 }
2362 }
2363}
2364
2365/*
2366 * If a directory had nlink == 1, then we should let it be 1. This indicates
2367 * directory has >EXT4_LINK_MAX subdirs.
2368 */
2369static void ext4_dec_count(handle_t *handle, struct inode *inode)
2370{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002371 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2372 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002373}
2374
2375
Mingming Cao617ba132006-10-11 01:20:53 -07002376static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002377 struct dentry *dentry, struct inode *inode)
2378{
Mingming Cao617ba132006-10-11 01:20:53 -07002379 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002381 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002382 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002383 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002384 return 0;
2385 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002386 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002387 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002388 iput(inode);
2389 return err;
2390}
2391
2392/*
2393 * By the time this is called, we already have created
2394 * the directory cache entry for the new file, but it
2395 * is so far negative - it has no inode.
2396 *
2397 * If the create succeeds, we fill in the inode information
2398 * with d_instantiate().
2399 */
Al Viro4acdaf22011-07-26 01:42:34 -04002400static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002401 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002402{
2403 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002404 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002405 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406
Christoph Hellwig871a2932010-03-03 09:05:07 -05002407 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002408
Theodore Ts'o11395752013-02-09 16:27:09 -05002409 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002410 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002411retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002412 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2413 NULL, EXT4_HT_DIR, credits);
2414 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002415 err = PTR_ERR(inode);
2416 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002417 inode->i_op = &ext4_file_inode_operations;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002418 if (test_opt(inode->i_sb, DAX))
2419 inode->i_fop = &ext4_dax_file_operations;
2420 else
2421 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002422 ext4_set_aops(inode);
Michael Halcrowdde680c2015-04-12 00:55:09 -04002423 err = 0;
2424#ifdef CONFIG_EXT4_FS_ENCRYPTION
2425 if (!err && ext4_encrypted_inode(dir)) {
2426 err = ext4_inherit_context(dir, inode);
2427 if (err) {
2428 clear_nlink(inode);
2429 unlock_new_inode(inode);
2430 iput(inode);
2431 }
2432 }
2433#endif
2434 if (!err)
2435 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002436 if (!err && IS_DIRSYNC(dir))
2437 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002438 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002439 if (handle)
2440 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002441 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442 goto retry;
2443 return err;
2444}
2445
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002446static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002447 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002448{
2449 handle_t *handle;
2450 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002451 int err, credits, retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002452
2453 if (!new_valid_dev(rdev))
2454 return -EINVAL;
2455
Christoph Hellwig871a2932010-03-03 09:05:07 -05002456 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002457
Theodore Ts'o11395752013-02-09 16:27:09 -05002458 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002459 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002460retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002461 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2462 NULL, EXT4_HT_DIR, credits);
2463 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464 err = PTR_ERR(inode);
2465 if (!IS_ERR(inode)) {
2466 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002467 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002468 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002469 if (!err && IS_DIRSYNC(dir))
2470 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471 }
Theodore Ts'o11395752013-02-09 16:27:09 -05002472 if (handle)
2473 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002474 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475 goto retry;
2476 return err;
2477}
2478
Al Viroaf51a2a2013-06-29 13:23:08 +04002479static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2480{
2481 handle_t *handle;
2482 struct inode *inode;
2483 int err, retries = 0;
2484
2485 dquot_initialize(dir);
2486
2487retry:
2488 inode = ext4_new_inode_start_handle(dir, mode,
2489 NULL, 0, NULL,
2490 EXT4_HT_DIR,
2491 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2492 4 + EXT4_XATTR_TRANS_BLOCKS);
2493 handle = ext4_journal_current_handle();
2494 err = PTR_ERR(inode);
2495 if (!IS_ERR(inode)) {
2496 inode->i_op = &ext4_file_inode_operations;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002497 if (test_opt(inode->i_sb, DAX))
2498 inode->i_fop = &ext4_dax_file_operations;
2499 else
2500 inode->i_fop = &ext4_file_operations;
Al Viroaf51a2a2013-06-29 13:23:08 +04002501 ext4_set_aops(inode);
Zheng Liue94bd342013-07-20 21:58:38 -04002502 d_tmpfile(dentry, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002503 err = ext4_orphan_add(handle, inode);
2504 if (err)
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002505 goto err_unlock_inode;
Al Viroaf51a2a2013-06-29 13:23:08 +04002506 mark_inode_dirty(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002507 unlock_new_inode(inode);
2508 }
2509 if (handle)
2510 ext4_journal_stop(handle);
2511 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2512 goto retry;
2513 return err;
Miklos Szeredi43ae9e32013-10-10 16:48:19 +02002514err_unlock_inode:
Al Viroaf51a2a2013-06-29 13:23:08 +04002515 ext4_journal_stop(handle);
2516 unlock_new_inode(inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04002517 return err;
2518}
2519
Tao Maa774f9c2012-12-10 14:05:57 -05002520struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2521 struct ext4_dir_entry_2 *de,
2522 int blocksize, int csum_size,
2523 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524{
Tao Maa774f9c2012-12-10 14:05:57 -05002525 de->inode = cpu_to_le32(inode->i_ino);
2526 de->name_len = 1;
2527 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2528 blocksize);
2529 strcpy(de->name, ".");
2530 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2531
2532 de = ext4_next_entry(de, blocksize);
2533 de->inode = cpu_to_le32(parent_ino);
2534 de->name_len = 2;
2535 if (!dotdot_real_len)
2536 de->rec_len = ext4_rec_len_to_disk(blocksize -
2537 (csum_size + EXT4_DIR_REC_LEN(1)),
2538 blocksize);
2539 else
2540 de->rec_len = ext4_rec_len_to_disk(
2541 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2542 strcpy(de->name, "..");
2543 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2544
2545 return ext4_next_entry(de, blocksize);
2546}
2547
2548static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2549 struct inode *inode)
2550{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002551 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002552 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002553 struct ext4_dir_entry_tail *t;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002554 ext4_lblk_t block = 0;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002555 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002556 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002557 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002558
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04002559 if (ext4_has_metadata_csum(dir->i_sb))
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002560 csum_size = sizeof(struct ext4_dir_entry_tail);
2561
Tao Ma3c47d542012-12-10 14:05:59 -05002562 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2563 err = ext4_try_create_inline_dir(handle, dir, inode);
2564 if (err < 0 && err != -ENOSPC)
2565 goto out;
2566 if (!err)
2567 goto out;
2568 }
2569
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002570 inode->i_size = 0;
Theodore Ts'o0f70b402013-02-15 03:35:57 -05002571 dir_block = ext4_append(handle, inode, &block);
2572 if (IS_ERR(dir_block))
2573 return PTR_ERR(dir_block);
Tao Maa774f9c2012-12-10 14:05:57 -05002574 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2575 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2576 set_nlink(inode, 2);
2577 if (csum_size) {
2578 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2579 initialize_dirent_tail(t, blocksize);
2580 }
2581
2582 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2583 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2584 if (err)
2585 goto out;
2586 set_buffer_verified(dir_block);
2587out:
2588 brelse(dir_block);
2589 return err;
2590}
2591
2592static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2593{
2594 handle_t *handle;
2595 struct inode *inode;
Theodore Ts'o11395752013-02-09 16:27:09 -05002596 int err, credits, retries = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002597
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002598 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002599 return -EMLINK;
2600
Christoph Hellwig871a2932010-03-03 09:05:07 -05002601 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002602
Theodore Ts'o11395752013-02-09 16:27:09 -05002603 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04002604 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002605retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05002606 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2607 &dentry->d_name,
2608 0, NULL, EXT4_HT_DIR, credits);
2609 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610 err = PTR_ERR(inode);
2611 if (IS_ERR(inode))
2612 goto out_stop;
2613
Mingming Cao617ba132006-10-11 01:20:53 -07002614 inode->i_op = &ext4_dir_inode_operations;
2615 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002616 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002617 if (err)
2618 goto out_clear_inode;
Michael Halcrowdde680c2015-04-12 00:55:09 -04002619#ifdef CONFIG_EXT4_FS_ENCRYPTION
2620 if (ext4_encrypted_inode(dir)) {
2621 err = ext4_inherit_context(dir, inode);
2622 if (err)
2623 goto out_clear_inode;
2624 }
2625#endif
Namhyung Kimdabd9912011-01-10 12:11:16 -05002626 err = ext4_mark_inode_dirty(handle, inode);
2627 if (!err)
2628 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002629 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002630out_clear_inode:
2631 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002632 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002633 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002634 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 goto out_stop;
2636 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002637 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002638 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002639 err = ext4_mark_inode_dirty(handle, dir);
2640 if (err)
2641 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002642 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002643 d_instantiate(dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05002644 if (IS_DIRSYNC(dir))
2645 ext4_handle_sync(handle);
2646
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002647out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05002648 if (handle)
2649 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07002650 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002651 goto retry;
2652 return err;
2653}
2654
2655/*
2656 * routine to check that the specified directory is empty (for rmdir)
2657 */
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002658int ext4_empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002659{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002660 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002661 struct buffer_head *bh;
2662 struct ext4_dir_entry_2 *de, *de1;
2663 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002664 int err = 0;
2665
Tao Ma61f86632012-12-10 14:06:01 -05002666 if (ext4_has_inline_data(inode)) {
2667 int has_inline_data = 1;
2668
2669 err = empty_inline_dir(inode, &has_inline_data);
2670 if (has_inline_data)
2671 return err;
2672 }
2673
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002674 sb = inode->i_sb;
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002675 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2676 EXT4_ERROR_INODE(inode, "invalid size");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677 return 1;
2678 }
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002679 bh = ext4_read_dirblock(inode, 0, EITHER);
2680 if (IS_ERR(bh))
2681 return 1;
2682
Mingming Cao617ba132006-10-11 01:20:53 -07002683 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002684 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685 if (le32_to_cpu(de->inode) != inode->i_ino ||
2686 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002687 strcmp(".", de->name) ||
2688 strcmp("..", de1->name)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002689 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002690 "bad directory (dir #%lu) - no `.' or `..'",
2691 inode->i_ino);
2692 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 return 1;
2694 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002695 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2696 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2697 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002698 while (offset < inode->i_size) {
Dmitry Monakhov236f5ec2014-04-21 14:38:14 -04002699 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04002700 unsigned int lblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002702 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002703 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'odc6982f2013-02-14 23:59:26 -05002704 bh = ext4_read_dirblock(inode, lblock, EITHER);
2705 if (IS_ERR(bh))
2706 return 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002707 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002708 }
Tao Ma226ba972012-12-10 14:05:58 -05002709 if (ext4_check_dir_entry(inode, NULL, de, bh,
2710 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002711 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002712 sb->s_blocksize);
2713 offset = (offset | (sb->s_blocksize - 1)) + 1;
2714 continue;
2715 }
2716 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002717 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 return 0;
2719 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002720 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2721 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002723 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002724 return 1;
2725}
2726
Jan Karad745a8c2014-05-26 11:56:53 -04002727/*
2728 * ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 * such inodes, starting at the superblock, in case we crash before the
2730 * file is closed/deleted, or in case the inode truncate spans multiple
2731 * transactions and the last transaction is not recovered after a crash.
2732 *
2733 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002734 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Jan Karad745a8c2014-05-26 11:56:53 -04002735 *
2736 * Orphan list manipulation functions must be called under i_mutex unless
2737 * we are just creating the inode or deleting it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002738 */
Mingming Cao617ba132006-10-11 01:20:53 -07002739int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740{
2741 struct super_block *sb = inode->i_sb;
Jan Karacd2c0802014-05-26 11:39:17 -04002742 struct ext4_sb_info *sbi = EXT4_SB(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002743 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744 int err = 0, rc;
Jan Karad745a8c2014-05-26 11:56:53 -04002745 bool dirty = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002746
Theodore Ts'oe2bfb082014-10-05 22:47:07 -04002747 if (!sbi->s_journal || is_bad_inode(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05002748 return 0;
2749
Jan Karad745a8c2014-05-26 11:56:53 -04002750 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2751 !mutex_is_locked(&inode->i_mutex));
2752 /*
2753 * Exit early if inode already is on orphan list. This is a big speedup
2754 * since we don't have to contend on the global s_orphan_lock.
2755 */
Mingming Cao617ba132006-10-11 01:20:53 -07002756 if (!list_empty(&EXT4_I(inode)->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002757 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002758
Lukas Czernerafb86172011-07-11 18:47:04 -04002759 /*
2760 * Orphan handling is only valid for files with data blocks
2761 * being truncated, or files being unlinked. Note that we either
2762 * hold i_mutex, or the inode can not be referenced from outside,
2763 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002765 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2766 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002767
Jan Karacd2c0802014-05-26 11:39:17 -04002768 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2769 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002770 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002771 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772
Mingming Cao617ba132006-10-11 01:20:53 -07002773 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002774 if (err)
Jan Karad745a8c2014-05-26 11:56:53 -04002775 goto out;
2776
2777 mutex_lock(&sbi->s_orphan_lock);
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002778 /*
2779 * Due to previous errors inode may be already a part of on-disk
2780 * orphan list. If so skip on-disk list modification.
2781 */
Jan Karad745a8c2014-05-26 11:56:53 -04002782 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
2783 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
2784 /* Insert this inode at the head of the on-disk orphan list */
2785 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
2786 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2787 dirty = true;
2788 }
2789 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
2790 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002791
Jan Karad745a8c2014-05-26 11:56:53 -04002792 if (dirty) {
2793 err = ext4_handle_dirty_super(handle, sb);
2794 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2795 if (!err)
2796 err = rc;
2797 if (err) {
2798 /*
2799 * We have to remove inode from in-memory list if
2800 * addition to on disk orphan list failed. Stray orphan
2801 * list entries can cause panics at unmount time.
2802 */
2803 mutex_lock(&sbi->s_orphan_lock);
2804 list_del(&EXT4_I(inode)->i_orphan);
2805 mutex_unlock(&sbi->s_orphan_lock);
2806 }
2807 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002808 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2809 jbd_debug(4, "orphan inode %lu will point to %d\n",
2810 inode->i_ino, NEXT_ORPHAN(inode));
Jan Karad745a8c2014-05-26 11:56:53 -04002811out:
Jan Karacd2c0802014-05-26 11:39:17 -04002812 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002813 return err;
2814}
2815
2816/*
Mingming Cao617ba132006-10-11 01:20:53 -07002817 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002818 * of such inodes stored on disk, because it is finally being cleaned up.
2819 */
Mingming Cao617ba132006-10-11 01:20:53 -07002820int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002821{
2822 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002823 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Karacd2c0802014-05-26 11:39:17 -04002824 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002825 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002826 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002827 int err = 0;
2828
Jan Karacd2c0802014-05-26 11:39:17 -04002829 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
Frank Mayhar03901312009-01-07 00:06:22 -05002830 return 0;
2831
Jan Karad745a8c2014-05-26 11:56:53 -04002832 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
2833 !mutex_is_locked(&inode->i_mutex));
2834 /* Do this quick check before taking global s_orphan_lock. */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002835 if (list_empty(&ei->i_orphan))
Jan Karad745a8c2014-05-26 11:56:53 -04002836 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002837
Jan Karad745a8c2014-05-26 11:56:53 -04002838 if (handle) {
2839 /* Grab inode buffer early before taking global s_orphan_lock */
2840 err = ext4_reserve_inode_write(handle, inode, &iloc);
2841 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002842
Jan Karad745a8c2014-05-26 11:56:53 -04002843 mutex_lock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002844 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2845
Jan Karad745a8c2014-05-26 11:56:53 -04002846 prev = ei->i_orphan.prev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 list_del_init(&ei->i_orphan);
2848
2849 /* If we're on an error path, we may not have a valid
2850 * transaction handle with which to update the orphan list on
2851 * disk, but we still need to remove the inode from the linked
2852 * list in memory. */
Jan Karad745a8c2014-05-26 11:56:53 -04002853 if (!handle || err) {
2854 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002855 goto out_err;
Jan Karad745a8c2014-05-26 11:56:53 -04002856 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857
Jan Karad745a8c2014-05-26 11:56:53 -04002858 ino_next = NEXT_ORPHAN(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002859 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002860 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002861 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002862 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Jan Karad745a8c2014-05-26 11:56:53 -04002863 if (err) {
2864 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002865 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002866 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002867 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Jan Karad745a8c2014-05-26 11:56:53 -04002868 mutex_unlock(&sbi->s_orphan_lock);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002869 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002870 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002871 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002872 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002873 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002875 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002877 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002878 if (err) {
2879 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002880 goto out_brelse;
Jan Karad745a8c2014-05-26 11:56:53 -04002881 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002882 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002883 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Jan Karad745a8c2014-05-26 11:56:53 -04002884 mutex_unlock(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002885 }
2886 if (err)
2887 goto out_brelse;
2888 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002889 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002890out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002891 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002892 return err;
2893
2894out_brelse:
2895 brelse(iloc.bh);
2896 goto out_err;
2897}
2898
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002899static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900{
2901 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002902 struct inode *inode;
2903 struct buffer_head *bh;
2904 struct ext4_dir_entry_2 *de;
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002905 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906
2907 /* Initialize quotas before so that eventual writes go in
2908 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002909 dquot_initialize(dir);
2910 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002911
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002913 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002914 if (IS_ERR(bh))
2915 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916 if (!bh)
2917 goto end_rmdir;
2918
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002919 inode = dentry->d_inode;
2920
2921 retval = -EIO;
2922 if (le32_to_cpu(de->inode) != inode->i_ino)
2923 goto end_rmdir;
2924
2925 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04002926 if (!ext4_empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927 goto end_rmdir;
2928
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002929 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002930 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002931 if (IS_ERR(handle)) {
2932 retval = PTR_ERR(handle);
2933 handle = NULL;
2934 goto end_rmdir;
2935 }
2936
2937 if (IS_DIRSYNC(dir))
2938 ext4_handle_sync(handle);
2939
Mingming Cao617ba132006-10-11 01:20:53 -07002940 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941 if (retval)
2942 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002943 if (!EXT4_DIR_LINK_EMPTY(inode))
Eric Sandeen12062dd2010-02-15 14:19:27 -05002944 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002945 "empty directory has too many links (%d)",
2946 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 inode->i_version++;
2948 clear_nlink(inode);
2949 /* There's no need to set i_disksize: the fact that i_nlink is
2950 * zero will ensure that the right thing happens during any
2951 * recovery. */
2952 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002953 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002954 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002955 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002956 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002957 ext4_update_dx_flag(dir);
2958 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959
2960end_rmdir:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002961 brelse(bh);
Theodore Ts'o8dcfaad2013-02-09 09:45:11 -05002962 if (handle)
2963 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964 return retval;
2965}
2966
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002967static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002968{
2969 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002970 struct inode *inode;
2971 struct buffer_head *bh;
2972 struct ext4_dir_entry_2 *de;
Theodore Ts'o931b6862013-02-09 09:43:39 -05002973 handle_t *handle = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002975 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002976 /* Initialize quotas before so that eventual writes go
2977 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002978 dquot_initialize(dir);
2979 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002980
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 retval = -ENOENT;
Tao Ma32f7f222012-12-10 14:06:01 -05002982 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04002983 if (IS_ERR(bh))
2984 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985 if (!bh)
2986 goto end_unlink;
2987
2988 inode = dentry->d_inode;
2989
2990 retval = -EIO;
2991 if (le32_to_cpu(de->inode) != inode->i_ino)
2992 goto end_unlink;
2993
Theodore Ts'o931b6862013-02-09 09:43:39 -05002994 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Theodore Ts'o64044ab2013-02-09 15:06:24 -05002995 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
Theodore Ts'o931b6862013-02-09 09:43:39 -05002996 if (IS_ERR(handle)) {
2997 retval = PTR_ERR(handle);
2998 handle = NULL;
2999 goto end_unlink;
3000 }
3001
3002 if (IS_DIRSYNC(dir))
3003 ext4_handle_sync(handle);
3004
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003005 if (!inode->i_nlink) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003006 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003007 "Deleting nonexistent file (%lu), %d",
3008 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003009 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 }
Mingming Cao617ba132006-10-11 01:20:53 -07003011 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 if (retval)
3013 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04003014 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003015 ext4_update_dx_flag(dir);
3016 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003017 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003018 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003019 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003020 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003021 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022
3023end_unlink:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003024 brelse(bh);
Theodore Ts'o931b6862013-02-09 09:43:39 -05003025 if (handle)
3026 ext4_journal_stop(handle);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003027 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028 return retval;
3029}
3030
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003031static int ext4_symlink(struct inode *dir,
3032 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003033{
3034 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003035 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 int l, err, retries = 0;
Jan Karadf5e6222011-05-03 11:12:58 -04003037 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003038
3039 l = strlen(symname)+1;
3040 if (l > dir->i_sb->s_blocksize)
3041 return -ENAMETOOLONG;
3042
Christoph Hellwig871a2932010-03-03 09:05:07 -05003043 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003044
Jan Karadf5e6222011-05-03 11:12:58 -04003045 if (l > EXT4_N_BLOCKS * 4) {
3046 /*
3047 * For non-fast symlinks, we just allocate inode and put it on
3048 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05003049 * group descriptor, sb, inode block, quota blocks, and
3050 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04003051 */
Eric Sandeen8c208712011-08-11 09:54:31 -05003052 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3053 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04003054 } else {
3055 /*
3056 * Fast symlink. We have to add entry to directory
3057 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3058 * allocate new inode (bitmap, group descriptor, inode block,
3059 * quota blocks, sb is already counted in previous macros).
3060 */
3061 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Jan Karaeb9cc7e2013-04-19 13:38:14 -04003062 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
Jan Karadf5e6222011-05-03 11:12:58 -04003063 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003064retry:
Theodore Ts'o11395752013-02-09 16:27:09 -05003065 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3066 &dentry->d_name, 0, NULL,
3067 EXT4_HT_DIR, credits);
3068 handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003069 err = PTR_ERR(inode);
3070 if (IS_ERR(inode))
3071 goto out_stop;
3072
Jan Karadf5e6222011-05-03 11:12:58 -04003073 if (l > EXT4_N_BLOCKS * 4) {
Mingming Cao617ba132006-10-11 01:20:53 -07003074 inode->i_op = &ext4_symlink_inode_operations;
3075 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003076 /*
Jan Karadf5e6222011-05-03 11:12:58 -04003077 * We cannot call page_symlink() with transaction started
3078 * because it calls into ext4_write_begin() which can wait
3079 * for transaction commit if we are running out of space
3080 * and thus we deadlock. So we have to stop transaction now
3081 * and restart it when symlink contents is written.
3082 *
3083 * To keep fs consistent in case of crash, we have to put inode
3084 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 */
Jan Karadf5e6222011-05-03 11:12:58 -04003086 drop_nlink(inode);
3087 err = ext4_orphan_add(handle, inode);
3088 ext4_journal_stop(handle);
3089 if (err)
3090 goto err_drop_inode;
Nick Piggin54566b22009-01-04 12:00:53 -08003091 err = __page_symlink(inode, symname, l, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003092 if (err)
3093 goto err_drop_inode;
3094 /*
3095 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3096 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3097 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05003098 handle = ext4_journal_start(dir, EXT4_HT_DIR,
Jan Karadf5e6222011-05-03 11:12:58 -04003099 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3100 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3101 if (IS_ERR(handle)) {
3102 err = PTR_ERR(handle);
3103 goto err_drop_inode;
3104 }
Al Viro0ce8c0102012-01-08 19:50:23 -05003105 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04003106 err = ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003107 if (err) {
Jan Karadf5e6222011-05-03 11:12:58 -04003108 ext4_journal_stop(handle);
Theodore Ts'o825f1482008-02-15 15:00:38 -05003109 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04003110 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003111 }
3112 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04003113 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003114 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Mingming Cao617ba132006-10-11 01:20:53 -07003115 inode->i_op = &ext4_fast_symlink_inode_operations;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003116 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003117 inode->i_size = l-1;
3118 }
Mingming Cao617ba132006-10-11 01:20:53 -07003119 EXT4_I(inode)->i_disksize = inode->i_size;
3120 err = ext4_add_nondir(handle, dentry, inode);
Theodore Ts'o11395752013-02-09 16:27:09 -05003121 if (!err && IS_DIRSYNC(dir))
3122 ext4_handle_sync(handle);
3123
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124out_stop:
Theodore Ts'o11395752013-02-09 16:27:09 -05003125 if (handle)
3126 ext4_journal_stop(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07003127 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 goto retry;
3129 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04003130err_drop_inode:
3131 unlock_new_inode(inode);
3132 iput(inode);
3133 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003134}
3135
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003136static int ext4_link(struct dentry *old_dentry,
3137 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003138{
3139 handle_t *handle;
3140 struct inode *inode = old_dentry->d_inode;
3141 int err, retries = 0;
3142
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04003143 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144 return -EMLINK;
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003145 if (ext4_encrypted_inode(dir) &&
3146 !ext4_is_child_context_consistent_with_parent(dir, inode))
3147 return -EPERM;
Christoph Hellwig871a2932010-03-03 09:05:07 -05003148 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05003149
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003150retry:
Theodore Ts'o9924a922013-02-08 21:59:22 -05003151 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3152 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
Al Viroaf51a2a2013-06-29 13:23:08 +04003153 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003154 if (IS_ERR(handle))
3155 return PTR_ERR(handle);
3156
3157 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003158 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159
Kalpak Shahef7f3832007-07-18 09:15:20 -04003160 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003161 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003162 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163
Al Viro6b38e842008-12-30 02:03:31 -05003164 err = ext4_add_entry(handle, dentry, inode);
3165 if (!err) {
3166 ext4_mark_inode_dirty(handle, inode);
Al Viroaf51a2a2013-06-29 13:23:08 +04003167 /* this can happen only for tmpfile being
3168 * linked the first time
3169 */
3170 if (inode->i_nlink == 1)
3171 ext4_orphan_del(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05003172 d_instantiate(dentry, inode);
3173 } else {
3174 drop_nlink(inode);
3175 iput(inode);
3176 }
Mingming Cao617ba132006-10-11 01:20:53 -07003177 ext4_journal_stop(handle);
3178 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 goto retry;
3180 return err;
3181}
3182
Tao Ma32f7f222012-12-10 14:06:01 -05003183
3184/*
3185 * Try to find buffer head where contains the parent block.
3186 * It should be the inode block if it is inlined or the 1st block
3187 * if it is a normal dir.
3188 */
3189static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3190 struct inode *inode,
3191 int *retval,
3192 struct ext4_dir_entry_2 **parent_de,
3193 int *inlined)
3194{
3195 struct buffer_head *bh;
3196
3197 if (!ext4_has_inline_data(inode)) {
Theodore Ts'odc6982f2013-02-14 23:59:26 -05003198 bh = ext4_read_dirblock(inode, 0, EITHER);
3199 if (IS_ERR(bh)) {
3200 *retval = PTR_ERR(bh);
Tao Ma32f7f222012-12-10 14:06:01 -05003201 return NULL;
3202 }
3203 *parent_de = ext4_next_entry(
3204 (struct ext4_dir_entry_2 *)bh->b_data,
3205 inode->i_sb->s_blocksize);
3206 return bh;
3207 }
3208
3209 *inlined = 1;
3210 return ext4_get_first_inline_block(inode, parent_de, retval);
3211}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003213struct ext4_renament {
3214 struct inode *dir;
3215 struct dentry *dentry;
3216 struct inode *inode;
Miklos Szeredibd429982014-04-01 17:08:44 +02003217 bool is_dir;
3218 int dir_nlink_delta;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003219
3220 /* entry for "dentry" */
3221 struct buffer_head *bh;
3222 struct ext4_dir_entry_2 *de;
3223 int inlined;
3224
3225 /* entry for ".." in inode if it's a directory */
3226 struct buffer_head *dir_bh;
3227 struct ext4_dir_entry_2 *parent_de;
3228 int dir_inlined;
3229};
3230
Miklos Szeredibd1af142014-04-01 17:08:44 +02003231static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3232{
3233 int retval;
3234
3235 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3236 &retval, &ent->parent_de,
3237 &ent->dir_inlined);
3238 if (!ent->dir_bh)
3239 return retval;
3240 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3241 return -EIO;
3242 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3243 return ext4_journal_get_write_access(handle, ent->dir_bh);
3244}
3245
3246static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3247 unsigned dir_ino)
3248{
3249 int retval;
3250
3251 ent->parent_de->inode = cpu_to_le32(dir_ino);
3252 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3253 if (!ent->dir_inlined) {
3254 if (is_dx(ent->inode)) {
3255 retval = ext4_handle_dirty_dx_node(handle,
3256 ent->inode,
3257 ent->dir_bh);
3258 } else {
3259 retval = ext4_handle_dirty_dirent_node(handle,
3260 ent->inode,
3261 ent->dir_bh);
3262 }
3263 } else {
3264 retval = ext4_mark_inode_dirty(handle, ent->inode);
3265 }
3266 if (retval) {
3267 ext4_std_error(ent->dir->i_sb, retval);
3268 return retval;
3269 }
3270 return 0;
3271}
3272
3273static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3274 unsigned ino, unsigned file_type)
3275{
3276 int retval;
3277
3278 BUFFER_TRACE(ent->bh, "get write access");
3279 retval = ext4_journal_get_write_access(handle, ent->bh);
3280 if (retval)
3281 return retval;
3282 ent->de->inode = cpu_to_le32(ino);
3283 if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
3284 EXT4_FEATURE_INCOMPAT_FILETYPE))
3285 ent->de->file_type = file_type;
3286 ent->dir->i_version++;
3287 ent->dir->i_ctime = ent->dir->i_mtime =
3288 ext4_current_time(ent->dir);
3289 ext4_mark_inode_dirty(handle, ent->dir);
3290 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3291 if (!ent->inlined) {
3292 retval = ext4_handle_dirty_dirent_node(handle,
3293 ent->dir, ent->bh);
3294 if (unlikely(retval)) {
3295 ext4_std_error(ent->dir->i_sb, retval);
3296 return retval;
3297 }
3298 }
3299 brelse(ent->bh);
3300 ent->bh = NULL;
3301
3302 return 0;
3303}
3304
3305static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3306 const struct qstr *d_name)
3307{
3308 int retval = -ENOENT;
3309 struct buffer_head *bh;
3310 struct ext4_dir_entry_2 *de;
3311
3312 bh = ext4_find_entry(dir, d_name, &de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003313 if (IS_ERR(bh))
3314 return PTR_ERR(bh);
Miklos Szeredibd1af142014-04-01 17:08:44 +02003315 if (bh) {
3316 retval = ext4_delete_entry(handle, dir, de, bh);
3317 brelse(bh);
3318 }
3319 return retval;
3320}
3321
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003322static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3323 int force_reread)
Miklos Szeredibd1af142014-04-01 17:08:44 +02003324{
3325 int retval;
3326 /*
3327 * ent->de could have moved from under us during htree split, so make
3328 * sure that we are deleting the right entry. We might also be pointing
3329 * to a stale entry in the unused part of ent->bh so just checking inum
3330 * and the name isn't enough.
3331 */
3332 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3333 ent->de->name_len != ent->dentry->d_name.len ||
3334 strncmp(ent->de->name, ent->dentry->d_name.name,
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003335 ent->de->name_len) ||
3336 force_reread) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003337 retval = ext4_find_delete_entry(handle, ent->dir,
3338 &ent->dentry->d_name);
3339 } else {
3340 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3341 if (retval == -ENOENT) {
3342 retval = ext4_find_delete_entry(handle, ent->dir,
3343 &ent->dentry->d_name);
3344 }
3345 }
3346
3347 if (retval) {
3348 ext4_warning(ent->dir->i_sb,
3349 "Deleting old file (%lu), %d, error=%d",
3350 ent->dir->i_ino, ent->dir->i_nlink, retval);
3351 }
3352}
3353
Miklos Szeredibd429982014-04-01 17:08:44 +02003354static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3355{
3356 if (ent->dir_nlink_delta) {
3357 if (ent->dir_nlink_delta == -1)
3358 ext4_dec_count(handle, ent->dir);
3359 else
3360 ext4_inc_count(handle, ent->dir);
3361 ext4_mark_inode_dirty(handle, ent->dir);
3362 }
3363}
3364
Miklos Szeredicd808de2014-10-24 00:14:37 +02003365static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3366 int credits, handle_t **h)
3367{
3368 struct inode *wh;
3369 handle_t *handle;
3370 int retries = 0;
3371
3372 /*
3373 * for inode block, sb block, group summaries,
3374 * and inode bitmap
3375 */
3376 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3377 EXT4_XATTR_TRANS_BLOCKS + 4);
3378retry:
3379 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3380 &ent->dentry->d_name, 0, NULL,
3381 EXT4_HT_DIR, credits);
3382
3383 handle = ext4_journal_current_handle();
3384 if (IS_ERR(wh)) {
3385 if (handle)
3386 ext4_journal_stop(handle);
3387 if (PTR_ERR(wh) == -ENOSPC &&
3388 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3389 goto retry;
3390 } else {
3391 *h = handle;
3392 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3393 wh->i_op = &ext4_special_inode_operations;
3394 }
3395 return wh;
3396}
3397
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398/*
3399 * Anybody can rename anything with this: the permission checks are left to the
3400 * higher-level routines.
Theodore Ts'o0e202702013-08-16 22:06:53 -04003401 *
3402 * n.b. old_{dentry,inode) refers to the source dentry/inode
3403 * while new_{dentry,inode) refers to the destination dentry/inode
3404 * This comes from rename(const char *oldpath, const char *newpath)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003406static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003407 struct inode *new_dir, struct dentry *new_dentry,
3408 unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003409{
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003410 handle_t *handle = NULL;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003411 struct ext4_renament old = {
3412 .dir = old_dir,
3413 .dentry = old_dentry,
3414 .inode = old_dentry->d_inode,
3415 };
3416 struct ext4_renament new = {
3417 .dir = new_dir,
3418 .dentry = new_dentry,
3419 .inode = new_dentry->d_inode,
3420 };
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003421 int force_reread;
Theodore Ts'o0e202702013-08-16 22:06:53 -04003422 int retval;
Miklos Szeredicd808de2014-10-24 00:14:37 +02003423 struct inode *whiteout = NULL;
3424 int credits;
3425 u8 old_file_type;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003427 dquot_initialize(old.dir);
3428 dquot_initialize(new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003429
3430 /* Initialize quotas before so that eventual writes go
3431 * in separate transaction */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003432 if (new.inode)
3433 dquot_initialize(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003434
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003435 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003436 if (IS_ERR(old.bh))
3437 return PTR_ERR(old.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438 /*
3439 * Check for inode number is _not_ due to possible IO errors.
3440 * We might rmdir the source, keep it as pwd of some process
3441 * and merrily kill the link to whatever was created under the
3442 * same name. Goodbye sticky bit ;-<
3443 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003444 retval = -ENOENT;
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003445 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003446 goto end_rename;
3447
Theodore Ts'od9cdc9032015-04-12 00:55:08 -04003448 if ((old.dir != new.dir) &&
3449 ext4_encrypted_inode(new.dir) &&
3450 !ext4_is_child_context_consistent_with_parent(new.dir,
3451 old.inode)) {
3452 retval = -EPERM;
3453 goto end_rename;
3454 }
3455
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003456 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3457 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003458 if (IS_ERR(new.bh)) {
3459 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003460 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003461 goto end_rename;
3462 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003463 if (new.bh) {
3464 if (!new.inode) {
3465 brelse(new.bh);
3466 new.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003467 }
3468 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003469 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3470 ext4_alloc_da_blocks(old.inode);
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003471
Miklos Szeredicd808de2014-10-24 00:14:37 +02003472 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3473 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3474 if (!(flags & RENAME_WHITEOUT)) {
3475 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003476 if (IS_ERR(handle)) {
3477 retval = PTR_ERR(handle);
3478 handle = NULL;
3479 goto end_rename;
3480 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003481 } else {
3482 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003483 if (IS_ERR(whiteout)) {
3484 retval = PTR_ERR(whiteout);
3485 whiteout = NULL;
3486 goto end_rename;
3487 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003488 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003489
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003490 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003491 ext4_handle_sync(handle);
3492
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003493 if (S_ISDIR(old.inode->i_mode)) {
3494 if (new.inode) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003495 retval = -ENOTEMPTY;
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003496 if (!ext4_empty_dir(new.inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003497 goto end_rename;
Miklos Szeredi0d7d5d62014-04-01 17:08:44 +02003498 } else {
3499 retval = -EMLINK;
3500 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3501 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003502 }
Miklos Szeredibd1af142014-04-01 17:08:44 +02003503 retval = ext4_rename_dir_prepare(handle, &old);
Amir Goldsteinef607892011-03-20 21:18:44 -04003504 if (retval)
3505 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003506 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003507 /*
3508 * If we're renaming a file within an inline_data dir and adding or
3509 * setting the new dirent causes a conversion from inline_data to
3510 * extents/blockmap, we need to force the dirent delete code to
3511 * re-read the directory, or else we end up trying to delete a dirent
3512 * from what is now the extent tree root (or a block map).
3513 */
3514 force_reread = (new.dir->i_ino == old.dir->i_ino &&
3515 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
Miklos Szeredicd808de2014-10-24 00:14:37 +02003516
3517 old_file_type = old.de->file_type;
3518 if (whiteout) {
3519 /*
3520 * Do this before adding a new entry, so the old entry is sure
3521 * to be still pointing to the valid old entry.
3522 */
3523 retval = ext4_setent(handle, &old, whiteout->i_ino,
3524 EXT4_FT_CHRDEV);
3525 if (retval)
3526 goto end_rename;
3527 ext4_mark_inode_dirty(handle, whiteout);
3528 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003529 if (!new.bh) {
3530 retval = ext4_add_entry(handle, new.dentry, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531 if (retval)
3532 goto end_rename;
3533 } else {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003534 retval = ext4_setent(handle, &new,
Miklos Szeredicd808de2014-10-24 00:14:37 +02003535 old.inode->i_ino, old_file_type);
Amir Goldsteinef607892011-03-20 21:18:44 -04003536 if (retval)
3537 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538 }
Darrick J. Wongd80d4482014-08-27 18:40:09 -04003539 if (force_reread)
3540 force_reread = !ext4_test_inode_flag(new.dir,
3541 EXT4_INODE_INLINE_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542
3543 /*
3544 * Like most other Unix systems, set the ctime for inodes on a
3545 * rename.
3546 */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003547 old.inode->i_ctime = ext4_current_time(old.inode);
3548 ext4_mark_inode_dirty(handle, old.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003549
Miklos Szeredicd808de2014-10-24 00:14:37 +02003550 if (!whiteout) {
3551 /*
3552 * ok, that's it
3553 */
3554 ext4_rename_delete(handle, &old, force_reread);
3555 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003556
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003557 if (new.inode) {
3558 ext4_dec_count(handle, new.inode);
3559 new.inode->i_ctime = ext4_current_time(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003560 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003561 old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
3562 ext4_update_dx_flag(old.dir);
3563 if (old.dir_bh) {
Miklos Szeredibd1af142014-04-01 17:08:44 +02003564 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3565 if (retval)
Theodore Ts'ob4097142011-01-10 12:46:59 -05003566 goto end_rename;
Miklos Szeredibd1af142014-04-01 17:08:44 +02003567
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003568 ext4_dec_count(handle, old.dir);
3569 if (new.inode) {
Michael Halcrowe875a2d2015-04-11 07:46:49 -04003570 /* checked ext4_empty_dir above, can't have another
3571 * parent, ext4_dec_count() won't work for many-linked
3572 * dirs */
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003573 clear_nlink(new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003574 } else {
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003575 ext4_inc_count(handle, new.dir);
3576 ext4_update_dx_flag(new.dir);
3577 ext4_mark_inode_dirty(handle, new.dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578 }
3579 }
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003580 ext4_mark_inode_dirty(handle, old.dir);
3581 if (new.inode) {
3582 ext4_mark_inode_dirty(handle, new.inode);
3583 if (!new.inode->i_nlink)
3584 ext4_orphan_add(handle, new.inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003585 }
3586 retval = 0;
3587
3588end_rename:
Miklos Szeredic0d268c2014-04-01 17:08:43 +02003589 brelse(old.dir_bh);
3590 brelse(old.bh);
3591 brelse(new.bh);
Miklos Szeredicd808de2014-10-24 00:14:37 +02003592 if (whiteout) {
3593 if (retval)
3594 drop_nlink(whiteout);
3595 unlock_new_inode(whiteout);
3596 iput(whiteout);
3597 }
Theodore Ts'o5b61de72013-08-16 22:06:14 -04003598 if (handle)
3599 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003600 return retval;
3601}
3602
Miklos Szeredibd429982014-04-01 17:08:44 +02003603static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
3604 struct inode *new_dir, struct dentry *new_dentry)
3605{
3606 handle_t *handle = NULL;
3607 struct ext4_renament old = {
3608 .dir = old_dir,
3609 .dentry = old_dentry,
3610 .inode = old_dentry->d_inode,
3611 };
3612 struct ext4_renament new = {
3613 .dir = new_dir,
3614 .dentry = new_dentry,
3615 .inode = new_dentry->d_inode,
3616 };
3617 u8 new_file_type;
3618 int retval;
3619
3620 dquot_initialize(old.dir);
3621 dquot_initialize(new.dir);
3622
3623 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
3624 &old.de, &old.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003625 if (IS_ERR(old.bh))
3626 return PTR_ERR(old.bh);
Miklos Szeredibd429982014-04-01 17:08:44 +02003627 /*
3628 * Check for inode number is _not_ due to possible IO errors.
3629 * We might rmdir the source, keep it as pwd of some process
3630 * and merrily kill the link to whatever was created under the
3631 * same name. Goodbye sticky bit ;-<
3632 */
3633 retval = -ENOENT;
3634 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3635 goto end_rename;
3636
3637 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3638 &new.de, &new.inlined);
Theodore Ts'o36de9282014-08-23 17:47:19 -04003639 if (IS_ERR(new.bh)) {
3640 retval = PTR_ERR(new.bh);
Theodore Ts'oa9cfcd62014-09-03 09:33:00 -04003641 new.bh = NULL;
Theodore Ts'o36de9282014-08-23 17:47:19 -04003642 goto end_rename;
3643 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003644
3645 /* RENAME_EXCHANGE case: old *and* new must both exist */
3646 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
3647 goto end_rename;
3648
3649 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
3650 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3651 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
Konstantin Khlebnikov7071b712015-04-02 16:32:15 -04003652 if (IS_ERR(handle)) {
3653 retval = PTR_ERR(handle);
3654 handle = NULL;
3655 goto end_rename;
3656 }
Miklos Szeredibd429982014-04-01 17:08:44 +02003657
3658 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3659 ext4_handle_sync(handle);
3660
3661 if (S_ISDIR(old.inode->i_mode)) {
3662 old.is_dir = true;
3663 retval = ext4_rename_dir_prepare(handle, &old);
3664 if (retval)
3665 goto end_rename;
3666 }
3667 if (S_ISDIR(new.inode->i_mode)) {
3668 new.is_dir = true;
3669 retval = ext4_rename_dir_prepare(handle, &new);
3670 if (retval)
3671 goto end_rename;
3672 }
3673
3674 /*
3675 * Other than the special case of overwriting a directory, parents'
3676 * nlink only needs to be modified if this is a cross directory rename.
3677 */
3678 if (old.dir != new.dir && old.is_dir != new.is_dir) {
3679 old.dir_nlink_delta = old.is_dir ? -1 : 1;
3680 new.dir_nlink_delta = -old.dir_nlink_delta;
3681 retval = -EMLINK;
3682 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
3683 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
3684 goto end_rename;
3685 }
3686
3687 new_file_type = new.de->file_type;
3688 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
3689 if (retval)
3690 goto end_rename;
3691
3692 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
3693 if (retval)
3694 goto end_rename;
3695
3696 /*
3697 * Like most other Unix systems, set the ctime for inodes on a
3698 * rename.
3699 */
3700 old.inode->i_ctime = ext4_current_time(old.inode);
3701 new.inode->i_ctime = ext4_current_time(new.inode);
3702 ext4_mark_inode_dirty(handle, old.inode);
3703 ext4_mark_inode_dirty(handle, new.inode);
3704
3705 if (old.dir_bh) {
3706 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
3707 if (retval)
3708 goto end_rename;
3709 }
3710 if (new.dir_bh) {
3711 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
3712 if (retval)
3713 goto end_rename;
3714 }
3715 ext4_update_dir_count(handle, &old);
3716 ext4_update_dir_count(handle, &new);
3717 retval = 0;
3718
3719end_rename:
3720 brelse(old.dir_bh);
3721 brelse(new.dir_bh);
3722 brelse(old.bh);
3723 brelse(new.bh);
3724 if (handle)
3725 ext4_journal_stop(handle);
3726 return retval;
3727}
3728
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003729static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3730 struct inode *new_dir, struct dentry *new_dentry,
3731 unsigned int flags)
3732{
Miklos Szeredicd808de2014-10-24 00:14:37 +02003733 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003734 return -EINVAL;
3735
Miklos Szeredibd429982014-04-01 17:08:44 +02003736 if (flags & RENAME_EXCHANGE) {
3737 return ext4_cross_rename(old_dir, old_dentry,
3738 new_dir, new_dentry);
3739 }
Miklos Szeredicd808de2014-10-24 00:14:37 +02003740
3741 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003742}
3743
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003744/*
3745 * directories can handle most operations...
3746 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003747const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003748 .create = ext4_create,
3749 .lookup = ext4_lookup,
3750 .link = ext4_link,
3751 .unlink = ext4_unlink,
3752 .symlink = ext4_symlink,
3753 .mkdir = ext4_mkdir,
3754 .rmdir = ext4_rmdir,
3755 .mknod = ext4_mknod,
Al Viroaf51a2a2013-06-29 13:23:08 +04003756 .tmpfile = ext4_tmpfile,
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02003757 .rename2 = ext4_rename2,
Mingming Cao617ba132006-10-11 01:20:53 -07003758 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003759 .setxattr = generic_setxattr,
3760 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003761 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003762 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003763 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003764 .set_acl = ext4_set_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003765 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003766};
3767
Arjan van de Ven754661f2007-02-12 00:55:38 -08003768const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003769 .setattr = ext4_setattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770 .setxattr = generic_setxattr,
3771 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003772 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003773 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003774 .get_acl = ext4_get_acl,
Christoph Hellwig64e178a2013-12-20 05:16:44 -08003775 .set_acl = ext4_set_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003776};