1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ext4/namei.c
4 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/namei.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
16 * Big-endian to little-endian byte-swapping/bitmaps by
17 * David S. Miller (davem@caip.rutgers.edu), 1995
18 * Directory entry file type support and forward compatibility hooks
19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
20 * Hash Tree Directory indexing (c)
21 * Daniel Phillips, 2001
22 * Hash Tree Directory indexing porting
23 * Christopher Li, 2002
24 * Hash Tree Directory indexing cleanup
25 * Theodore Ts'o, 2002
26 */
27
28 #include <linux/fs.h>
29 #include <linux/pagemap.h>
30 #include <linux/time.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/quotaops.h>
35 #include <linux/buffer_head.h>
36 #include <linux/bio.h>
37 #include <linux/iversion.h>
38 #include <linux/unicode.h>
39 #include "ext4.h"
40 #include "ext4_jbd2.h"
41
42 #include "xattr.h"
43 #include "acl.h"
44
45 #include <trace/events/ext4.h>
46 /*
47 * define how far ahead to read directories while searching them.
48 */
49 #define NAMEI_RA_CHUNKS 2
50 #define NAMEI_RA_BLOCKS 4
51 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
52
ext4_append(handle_t *handle, struct inode *inode, ext4_lblk_t *block)53 static struct buffer_head *ext4_append(handle_t *handle,
54 struct inode *inode,
55 ext4_lblk_t *block)
56 {
57 struct ext4_map_blocks map;
58 struct buffer_head *bh;
59 int err;
60
61 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
62 ((inode->i_size >> 10) >=
63 EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
64 return ERR_PTR(-ENOSPC);
65
66 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
67 map.m_lblk = *block;
68 map.m_len = 1;
69
70 /*
71 * We're appending new directory block. Make sure the block is not
72 * allocated yet, otherwise we will end up corrupting the
73 * directory.
74 */
75 err = ext4_map_blocks(NULL, inode, &map, 0);
76 if (err < 0)
77 return ERR_PTR(err);
78 if (err) {
79 EXT4_ERROR_INODE(inode, "Logical block already allocated");
80 return ERR_PTR(-EFSCORRUPTED);
81 }
82
83 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
84 if (IS_ERR(bh))
85 return bh;
86 inode->i_size += inode->i_sb->s_blocksize;
87 EXT4_I(inode)->i_disksize = inode->i_size;
88 BUFFER_TRACE(bh, "get_write_access");
89 err = ext4_journal_get_write_access(handle, bh);
90 if (err) {
91 brelse(bh);
92 ext4_std_error(inode->i_sb, err);
93 return ERR_PTR(err);
94 }
95 return bh;
96 }
97
98 static int ext4_dx_csum_verify(struct inode *inode,
99 struct ext4_dir_entry *dirent);
100
101 /*
102 * Hints to ext4_read_dirblock regarding whether we expect a directory
103 * block being read to be an index block, or a block containing
104 * directory entries (and if the latter, whether it was found via a
105 * logical block in an htree index block). This is used to control
106 * what sort of sanity checkinig ext4_read_dirblock() will do on the
107 * directory block read from the storage device. EITHER will means
108 * the caller doesn't know what kind of directory block will be read,
109 * so no specific verification will be done.
110 */
111 typedef enum {
112 EITHER, INDEX, DIRENT, DIRENT_HTREE
113 } dirblock_type_t;
114
115 #define ext4_read_dirblock(inode, block, type) \
116 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
117
__ext4_read_dirblock(struct inode *inode, ext4_lblk_t block, dirblock_type_t type, const char *func, unsigned int line)118 static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
119 ext4_lblk_t block,
120 dirblock_type_t type,
121 const char *func,
122 unsigned int line)
123 {
124 struct buffer_head *bh;
125 struct ext4_dir_entry *dirent;
126 int is_dx_block = 0;
127
128 if (block >= inode->i_size >> inode->i_blkbits) {
129 ext4_error_inode(inode, func, line, block,
130 "Attempting to read directory block (%u) that is past i_size (%llu)",
131 block, inode->i_size);
132 return ERR_PTR(-EFSCORRUPTED);
133 }
134
135 if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
136 bh = ERR_PTR(-EIO);
137 else
138 bh = ext4_bread(NULL, inode, block, 0);
139 if (IS_ERR(bh)) {
140 __ext4_warning(inode->i_sb, func, line,
141 "inode #%lu: lblock %lu: comm %s: "
142 "error %ld reading directory block",
143 inode->i_ino, (unsigned long)block,
144 current->comm, PTR_ERR(bh));
145
146 return bh;
147 }
148 /* The first directory block must not be a hole. */
149 if (!bh && (type == INDEX || type == DIRENT_HTREE || block == 0)) {
150 ext4_error_inode(inode, func, line, block,
151 "Directory hole found for htree %s block %u",
152 (type == INDEX) ? "index" : "leaf", block);
153 return ERR_PTR(-EFSCORRUPTED);
154 }
155 if (!bh)
156 return NULL;
157 dirent = (struct ext4_dir_entry *) bh->b_data;
158 /* Determine whether or not we have an index block */
159 if (is_dx(inode)) {
160 if (block == 0)
161 is_dx_block = 1;
162 else if (ext4_rec_len_from_disk(dirent->rec_len,
163 inode->i_sb->s_blocksize) ==
164 inode->i_sb->s_blocksize)
165 is_dx_block = 1;
166 }
167 if (!is_dx_block && type == INDEX) {
168 ext4_error_inode(inode, func, line, block,
169 "directory leaf block found instead of index block");
170 brelse(bh);
171 return ERR_PTR(-EFSCORRUPTED);
172 }
173 if (!ext4_has_metadata_csum(inode->i_sb) ||
174 buffer_verified(bh))
175 return bh;
176
177 /*
178 * An empty leaf block can get mistaken for a index block; for
179 * this reason, we can only check the index checksum when the
180 * caller is sure it should be an index block.
181 */
182 if (is_dx_block && type == INDEX) {
183 if (ext4_dx_csum_verify(inode, dirent) &&
184 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
185 set_buffer_verified(bh);
186 else {
187 ext4_error_inode_err(inode, func, line, block,
188 EFSBADCRC,
189 "Directory index failed checksum");
190 brelse(bh);
191 return ERR_PTR(-EFSBADCRC);
192 }
193 }
194 if (!is_dx_block) {
195 if (ext4_dirblock_csum_verify(inode, bh) &&
196 !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
197 set_buffer_verified(bh);
198 else {
199 ext4_error_inode_err(inode, func, line, block,
200 EFSBADCRC,
201 "Directory block failed checksum");
202 brelse(bh);
203 return ERR_PTR(-EFSBADCRC);
204 }
205 }
206 return bh;
207 }
208
209 #ifndef assert
210 #define assert(test) J_ASSERT(test)
211 #endif
212
213 #ifdef DX_DEBUG
214 #define dxtrace(command) command
215 #else
216 #define dxtrace(command)
217 #endif
218
219 struct fake_dirent
220 {
221 __le32 inode;
222 __le16 rec_len;
223 u8 name_len;
224 u8 file_type;
225 };
226
227 struct dx_countlimit
228 {
229 __le16 limit;
230 __le16 count;
231 };
232
233 struct dx_entry
234 {
235 __le32 hash;
236 __le32 block;
237 };
238
239 /*
240 * dx_root_info is laid out so that if it should somehow get overlaid by a
241 * dirent the two low bits of the hash version will be zero. Therefore, the
242 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
243 */
244
245 struct dx_root
246 {
247 struct fake_dirent dot;
248 char dot_name[4];
249 struct fake_dirent dotdot;
250 char dotdot_name[4];
251 struct dx_root_info
252 {
253 __le32 reserved_zero;
254 u8 hash_version;
255 u8 info_length; /* 8 */
256 u8 indirect_levels;
257 u8 unused_flags;
258 }
259 info;
260 struct dx_entry entries[];
261 };
262
263 struct dx_node
264 {
265 struct fake_dirent fake;
266 struct dx_entry entries[];
267 };
268
269
270 struct dx_frame
271 {
272 struct buffer_head *bh;
273 struct dx_entry *entries;
274 struct dx_entry *at;
275 };
276
277 struct dx_map_entry
278 {
279 u32 hash;
280 u16 offs;
281 u16 size;
282 };
283
284 /*
285 * This goes at the end of each htree block.
286 */
287 struct dx_tail {
288 u32 dt_reserved;
289 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
290 };
291
292 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
293 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
294 static inline unsigned dx_get_hash(struct dx_entry *entry);
295 static void dx_set_hash(struct dx_entry *entry, unsigned value);
296 static unsigned dx_get_count(struct dx_entry *entries);
297 static unsigned dx_get_limit(struct dx_entry *entries);
298 static void dx_set_count(struct dx_entry *entries, unsigned value);
299 static void dx_set_limit(struct dx_entry *entries, unsigned value);
300 static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
301 static unsigned dx_node_limit(struct inode *dir);
302 static struct dx_frame *dx_probe(struct ext4_filename *fname,
303 struct inode *dir,
304 struct dx_hash_info *hinfo,
305 struct dx_frame *frame);
306 static void dx_release(struct dx_frame *frames);
307 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
308 struct dx_hash_info *hinfo,
309 struct dx_map_entry *map_tail);
310 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
311 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
312 struct dx_map_entry *offsets, int count, unsigned blocksize);
313 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
314 static void dx_insert_block(struct dx_frame *frame,
315 u32 hash, ext4_lblk_t block);
316 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
317 struct dx_frame *frame,
318 struct dx_frame *frames,
319 __u32 *start_hash);
320 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
321 struct ext4_filename *fname,
322 struct ext4_dir_entry_2 **res_dir);
323 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
324 struct inode *dir, struct inode *inode);
325
326 /* checksumming functions */
ext4_initialize_dirent_tail(struct buffer_head *bh, unsigned int blocksize)327 void ext4_initialize_dirent_tail(struct buffer_head *bh,
328 unsigned int blocksize)
329 {
330 struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
331
332 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
333 t->det_rec_len = ext4_rec_len_to_disk(
334 sizeof(struct ext4_dir_entry_tail), blocksize);
335 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
336 }
337
338 /* Walk through a dirent block to find a checksum "dirent" at the tail */
get_dirent_tail(struct inode *inode, struct buffer_head *bh)339 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
340 struct buffer_head *bh)
341 {
342 struct ext4_dir_entry_tail *t;
343 int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
344
345 #ifdef PARANOID
346 struct ext4_dir_entry *d, *top;
347
348 d = (struct ext4_dir_entry *)bh->b_data;
349 top = (struct ext4_dir_entry *)(bh->b_data +
350 (blocksize - sizeof(struct ext4_dir_entry_tail)));
351 while (d < top && ext4_rec_len_from_disk(d->rec_len, blocksize))
352 d = (struct ext4_dir_entry *)(((void *)d) +
353 ext4_rec_len_from_disk(d->rec_len, blocksize));
354
355 if (d != top)
356 return NULL;
357
358 t = (struct ext4_dir_entry_tail *)d;
359 #else
360 t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
361 #endif
362
363 if (t->det_reserved_zero1 ||
364 (ext4_rec_len_from_disk(t->det_rec_len, blocksize) !=
365 sizeof(struct ext4_dir_entry_tail)) ||
366 t->det_reserved_zero2 ||
367 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
368 return NULL;
369
370 return t;
371 }
372
ext4_dirblock_csum(struct inode *inode, void *dirent, int size)373 static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
374 {
375 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
376 struct ext4_inode_info *ei = EXT4_I(inode);
377 __u32 csum;
378
379 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
380 return cpu_to_le32(csum);
381 }
382
383 #define warn_no_space_for_csum(inode) \
384 __warn_no_space_for_csum((inode), __func__, __LINE__)
385
__warn_no_space_for_csum(struct inode *inode, const char *func, unsigned int line)386 static void __warn_no_space_for_csum(struct inode *inode, const char *func,
387 unsigned int line)
388 {
389 __ext4_warning_inode(inode, func, line,
390 "No space for directory leaf checksum. Please run e2fsck -D.");
391 }
392
ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)393 int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
394 {
395 struct ext4_dir_entry_tail *t;
396
397 if (!ext4_has_metadata_csum(inode->i_sb))
398 return 1;
399
400 t = get_dirent_tail(inode, bh);
401 if (!t) {
402 warn_no_space_for_csum(inode);
403 return 0;
404 }
405
406 if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
407 (char *)t - bh->b_data))
408 return 0;
409
410 return 1;
411 }
412
ext4_dirblock_csum_set(struct inode *inode, struct buffer_head *bh)413 static void ext4_dirblock_csum_set(struct inode *inode,
414 struct buffer_head *bh)
415 {
416 struct ext4_dir_entry_tail *t;
417
418 if (!ext4_has_metadata_csum(inode->i_sb))
419 return;
420
421 t = get_dirent_tail(inode, bh);
422 if (!t) {
423 warn_no_space_for_csum(inode);
424 return;
425 }
426
427 t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
428 (char *)t - bh->b_data);
429 }
430
ext4_handle_dirty_dirblock(handle_t *handle, struct inode *inode, struct buffer_head *bh)431 int ext4_handle_dirty_dirblock(handle_t *handle,
432 struct inode *inode,
433 struct buffer_head *bh)
434 {
435 ext4_dirblock_csum_set(inode, bh);
436 return ext4_handle_dirty_metadata(handle, inode, bh);
437 }
438
get_dx_countlimit(struct inode *inode, struct ext4_dir_entry *dirent, int *offset)439 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
440 struct ext4_dir_entry *dirent,
441 int *offset)
442 {
443 struct ext4_dir_entry *dp;
444 struct dx_root_info *root;
445 int count_offset;
446 int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
447 unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
448
449 if (rlen == blocksize)
450 count_offset = 8;
451 else if (rlen == 12) {
452 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
453 if (ext4_rec_len_from_disk(dp->rec_len, blocksize) != blocksize - 12)
454 return NULL;
455 root = (struct dx_root_info *)(((void *)dp + 12));
456 if (root->reserved_zero ||
457 root->info_length != sizeof(struct dx_root_info))
458 return NULL;
459 count_offset = 32;
460 } else
461 return NULL;
462
463 if (offset)
464 *offset = count_offset;
465 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
466 }
467
ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent, int count_offset, int count, struct dx_tail *t)468 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
469 int count_offset, int count, struct dx_tail *t)
470 {
471 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
472 struct ext4_inode_info *ei = EXT4_I(inode);
473 __u32 csum;
474 int size;
475 __u32 dummy_csum = 0;
476 int offset = offsetof(struct dx_tail, dt_checksum);
477
478 size = count_offset + (count * sizeof(struct dx_entry));
479 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
480 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
481 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
482
483 return cpu_to_le32(csum);
484 }
485
ext4_dx_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)486 static int ext4_dx_csum_verify(struct inode *inode,
487 struct ext4_dir_entry *dirent)
488 {
489 struct dx_countlimit *c;
490 struct dx_tail *t;
491 int count_offset, limit, count;
492
493 if (!ext4_has_metadata_csum(inode->i_sb))
494 return 1;
495
496 c = get_dx_countlimit(inode, dirent, &count_offset);
497 if (!c) {
498 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
499 return 0;
500 }
501 limit = le16_to_cpu(c->limit);
502 count = le16_to_cpu(c->count);
503 if (count_offset + (limit * sizeof(struct dx_entry)) >
504 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
505 warn_no_space_for_csum(inode);
506 return 0;
507 }
508 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
509
510 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
511 count, t))
512 return 0;
513 return 1;
514 }
515
ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)516 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
517 {
518 struct dx_countlimit *c;
519 struct dx_tail *t;
520 int count_offset, limit, count;
521
522 if (!ext4_has_metadata_csum(inode->i_sb))
523 return;
524
525 c = get_dx_countlimit(inode, dirent, &count_offset);
526 if (!c) {
527 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
528 return;
529 }
530 limit = le16_to_cpu(c->limit);
531 count = le16_to_cpu(c->count);
532 if (count_offset + (limit * sizeof(struct dx_entry)) >
533 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
534 warn_no_space_for_csum(inode);
535 return;
536 }
537 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
538
539 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
540 }
541
ext4_handle_dirty_dx_node(handle_t *handle, struct inode *inode, struct buffer_head *bh)542 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
543 struct inode *inode,
544 struct buffer_head *bh)
545 {
546 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
547 return ext4_handle_dirty_metadata(handle, inode, bh);
548 }
549
550 /*
551 * p is at least 6 bytes before the end of page
552 */
553 static inline struct ext4_dir_entry_2 *
ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)554 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
555 {
556 return (struct ext4_dir_entry_2 *)((char *)p +
557 ext4_rec_len_from_disk(p->rec_len, blocksize));
558 }
559
560 /*
561 * Future: use high four bits of block for coalesce-on-delete flags
562 * Mask them off for now.
563 */
564
dx_get_block(struct dx_entry *entry)565 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
566 {
567 return le32_to_cpu(entry->block) & 0x0fffffff;
568 }
569
dx_set_block(struct dx_entry *entry, ext4_lblk_t value)570 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
571 {
572 entry->block = cpu_to_le32(value);
573 }
574
dx_get_hash(struct dx_entry *entry)575 static inline unsigned dx_get_hash(struct dx_entry *entry)
576 {
577 return le32_to_cpu(entry->hash);
578 }
579
dx_set_hash(struct dx_entry *entry, unsigned value)580 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
581 {
582 entry->hash = cpu_to_le32(value);
583 }
584
dx_get_count(struct dx_entry *entries)585 static inline unsigned dx_get_count(struct dx_entry *entries)
586 {
587 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
588 }
589
dx_get_limit(struct dx_entry *entries)590 static inline unsigned dx_get_limit(struct dx_entry *entries)
591 {
592 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
593 }
594
dx_set_count(struct dx_entry *entries, unsigned value)595 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
596 {
597 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
598 }
599
dx_set_limit(struct dx_entry *entries, unsigned value)600 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
601 {
602 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
603 }
604
dx_root_limit(struct inode *dir, unsigned infosize)605 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
606 {
607 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
608 EXT4_DIR_REC_LEN(2) - infosize;
609
610 if (ext4_has_metadata_csum(dir->i_sb))
611 entry_space -= sizeof(struct dx_tail);
612 return entry_space / sizeof(struct dx_entry);
613 }
614
dx_node_limit(struct inode *dir)615 static inline unsigned dx_node_limit(struct inode *dir)
616 {
617 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
618
619 if (ext4_has_metadata_csum(dir->i_sb))
620 entry_space -= sizeof(struct dx_tail);
621 return entry_space / sizeof(struct dx_entry);
622 }
623
624 /*
625 * Debug
626 */
627 #ifdef DX_DEBUG
dx_show_index(char * label, struct dx_entry *entries)628 static void dx_show_index(char * label, struct dx_entry *entries)
629 {
630 int i, n = dx_get_count (entries);
631 printk(KERN_DEBUG "%s index", label);
632 for (i = 0; i < n; i++) {
633 printk(KERN_CONT " %x->%lu",
634 i ? dx_get_hash(entries + i) : 0,
635 (unsigned long)dx_get_block(entries + i));
636 }
637 printk(KERN_CONT "\n");
638 }
639
640 struct stats
641 {
642 unsigned names;
643 unsigned space;
644 unsigned bcount;
645 };
646
dx_show_leaf(struct inode *dir, struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de, int size, int show_names)647 static struct stats dx_show_leaf(struct inode *dir,
648 struct dx_hash_info *hinfo,
649 struct ext4_dir_entry_2 *de,
650 int size, int show_names)
651 {
652 unsigned names = 0, space = 0;
653 char *base = (char *) de;
654 struct dx_hash_info h = *hinfo;
655
656 printk("names: ");
657 while ((char *) de < base + size)
658 {
659 if (de->inode)
660 {
661 if (show_names)
662 {
663 #ifdef CONFIG_FS_ENCRYPTION
664 int len;
665 char *name;
666 struct fscrypt_str fname_crypto_str =
667 FSTR_INIT(NULL, 0);
668 int res = 0;
669
670 name = de->name;
671 len = de->name_len;
672 if (IS_ENCRYPTED(dir))
673 res = fscrypt_get_encryption_info(dir);
674 if (res) {
675 printk(KERN_WARNING "Error setting up"
676 " fname crypto: %d\n", res);
677 }
678 if (!fscrypt_has_encryption_key(dir)) {
679 /* Directory is not encrypted */
680 ext4fs_dirhash(dir, de->name,
681 de->name_len, &h);
682 printk("%*.s:(U)%x.%u ", len,
683 name, h.hash,
684 (unsigned) ((char *) de
685 - base));
686 } else {
687 struct fscrypt_str de_name =
688 FSTR_INIT(name, len);
689
690 /* Directory is encrypted */
691 res = fscrypt_fname_alloc_buffer(
692 len, &fname_crypto_str);
693 if (res)
694 printk(KERN_WARNING "Error "
695 "allocating crypto "
696 "buffer--skipping "
697 "crypto\n");
698 res = fscrypt_fname_disk_to_usr(dir,
699 0, 0, &de_name,
700 &fname_crypto_str);
701 if (res) {
702 printk(KERN_WARNING "Error "
703 "converting filename "
704 "from disk to usr"
705 "\n");
706 name = "??";
707 len = 2;
708 } else {
709 name = fname_crypto_str.name;
710 len = fname_crypto_str.len;
711 }
712 ext4fs_dirhash(dir, de->name,
713 de->name_len, &h);
714 printk("%*.s:(E)%x.%u ", len, name,
715 h.hash, (unsigned) ((char *) de
716 - base));
717 fscrypt_fname_free_buffer(
718 &fname_crypto_str);
719 }
720 #else
721 int len = de->name_len;
722 char *name = de->name;
723 ext4fs_dirhash(dir, de->name, de->name_len, &h);
724 printk("%*.s:%x.%u ", len, name, h.hash,
725 (unsigned) ((char *) de - base));
726 #endif
727 }
728 space += EXT4_DIR_REC_LEN(de->name_len);
729 names++;
730 }
731 de = ext4_next_entry(de, size);
732 }
733 printk(KERN_CONT "(%i)\n", names);
734 return (struct stats) { names, space, 1 };
735 }
736
dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, struct dx_entry *entries, int levels)737 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
738 struct dx_entry *entries, int levels)
739 {
740 unsigned blocksize = dir->i_sb->s_blocksize;
741 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
742 unsigned bcount = 0;
743 struct buffer_head *bh;
744 printk("%i indexed blocks...\n", count);
745 for (i = 0; i < count; i++, entries++)
746 {
747 ext4_lblk_t block = dx_get_block(entries);
748 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
749 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
750 struct stats stats;
751 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
752 bh = ext4_bread(NULL,dir, block, 0);
753 if (!bh || IS_ERR(bh))
754 continue;
755 stats = levels?
756 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
757 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
758 bh->b_data, blocksize, 0);
759 names += stats.names;
760 space += stats.space;
761 bcount += stats.bcount;
762 brelse(bh);
763 }
764 if (bcount)
765 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
766 levels ? "" : " ", names, space/bcount,
767 (space/bcount)*100/blocksize);
768 return (struct stats) { names, space, bcount};
769 }
770 #endif /* DX_DEBUG */
771
772 /*
773 * Probe for a directory leaf block to search.
774 *
775 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
776 * error in the directory index, and the caller should fall back to
777 * searching the directory normally. The callers of dx_probe **MUST**
778 * check for this error code, and make sure it never gets reflected
779 * back to userspace.
780 */
781 static struct dx_frame *
dx_probe(struct ext4_filename *fname, struct inode *dir, struct dx_hash_info *hinfo, struct dx_frame *frame_in)782 dx_probe(struct ext4_filename *fname, struct inode *dir,
783 struct dx_hash_info *hinfo, struct dx_frame *frame_in)
784 {
785 unsigned count, indirect, level, i;
786 struct dx_entry *at, *entries, *p, *q, *m;
787 struct dx_root *root;
788 struct dx_frame *frame = frame_in;
789 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
790 u32 hash;
791 ext4_lblk_t block;
792 ext4_lblk_t blocks[EXT4_HTREE_LEVEL];
793
794 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
795 frame->bh = ext4_read_dirblock(dir, 0, INDEX);
796 if (IS_ERR(frame->bh))
797 return (struct dx_frame *) frame->bh;
798
799 root = (struct dx_root *) frame->bh->b_data;
800 if (root->info.hash_version != DX_HASH_TEA &&
801 root->info.hash_version != DX_HASH_HALF_MD4 &&
802 root->info.hash_version != DX_HASH_LEGACY) {
803 ext4_warning_inode(dir, "Unrecognised inode hash code %u",
804 root->info.hash_version);
805 goto fail;
806 }
807 if (fname)
808 hinfo = &fname->hinfo;
809 hinfo->hash_version = root->info.hash_version;
810 if (hinfo->hash_version <= DX_HASH_TEA)
811 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
812 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
813 if (fname && fname_name(fname))
814 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo);
815 hash = hinfo->hash;
816
817 if (root->info.unused_flags & 1) {
818 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
819 root->info.unused_flags);
820 goto fail;
821 }
822
823 indirect = root->info.indirect_levels;
824 if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
825 ext4_warning(dir->i_sb,
826 "Directory (ino: %lu) htree depth %#06x exceed"
827 "supported value", dir->i_ino,
828 ext4_dir_htree_level(dir->i_sb));
829 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
830 ext4_warning(dir->i_sb, "Enable large directory "
831 "feature to access it");
832 }
833 goto fail;
834 }
835
836 entries = (struct dx_entry *)(((char *)&root->info) +
837 root->info.info_length);
838
839 if (dx_get_limit(entries) != dx_root_limit(dir,
840 root->info.info_length)) {
841 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
842 dx_get_limit(entries),
843 dx_root_limit(dir, root->info.info_length));
844 goto fail;
845 }
846
847 dxtrace(printk("Look up %x", hash));
848 level = 0;
849 blocks[0] = 0;
850 while (1) {
851 count = dx_get_count(entries);
852 if (!count || count > dx_get_limit(entries)) {
853 ext4_warning_inode(dir,
854 "dx entry: count %u beyond limit %u",
855 count, dx_get_limit(entries));
856 goto fail;
857 }
858
859 p = entries + 1;
860 q = entries + count - 1;
861 while (p <= q) {
862 m = p + (q - p) / 2;
863 dxtrace(printk(KERN_CONT "."));
864 if (dx_get_hash(m) > hash)
865 q = m - 1;
866 else
867 p = m + 1;
868 }
869
870 if (0) { // linear search cross check
871 unsigned n = count - 1;
872 at = entries;
873 while (n--)
874 {
875 dxtrace(printk(KERN_CONT ","));
876 if (dx_get_hash(++at) > hash)
877 {
878 at--;
879 break;
880 }
881 }
882 assert (at == p - 1);
883 }
884
885 at = p - 1;
886 dxtrace(printk(KERN_CONT " %x->%u\n",
887 at == entries ? 0 : dx_get_hash(at),
888 dx_get_block(at)));
889 frame->entries = entries;
890 frame->at = at;
891
892 block = dx_get_block(at);
893 for (i = 0; i <= level; i++) {
894 if (blocks[i] == block) {
895 ext4_warning_inode(dir,
896 "dx entry: tree cycle block %u points back to block %u",
897 blocks[level], block);
898 goto fail;
899 }
900 }
901 if (++level > indirect)
902 return frame;
903 blocks[level] = block;
904 frame++;
905 frame->bh = ext4_read_dirblock(dir, block, INDEX);
906 if (IS_ERR(frame->bh)) {
907 ret_err = (struct dx_frame *) frame->bh;
908 frame->bh = NULL;
909 goto fail;
910 }
911
912 entries = ((struct dx_node *) frame->bh->b_data)->entries;
913
914 if (dx_get_limit(entries) != dx_node_limit(dir)) {
915 ext4_warning_inode(dir,
916 "dx entry: limit %u != node limit %u",
917 dx_get_limit(entries), dx_node_limit(dir));
918 goto fail;
919 }
920 }
921 fail:
922 while (frame >= frame_in) {
923 brelse(frame->bh);
924 frame--;
925 }
926
927 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
928 ext4_warning_inode(dir,
929 "Corrupt directory, running e2fsck is recommended");
930 return ret_err;
931 }
932
dx_release(struct dx_frame *frames)933 static void dx_release(struct dx_frame *frames)
934 {
935 struct dx_root_info *info;
936 int i;
937 unsigned int indirect_levels;
938
939 if (frames[0].bh == NULL)
940 return;
941
942 info = &((struct dx_root *)frames[0].bh->b_data)->info;
943 /* save local copy, "info" may be freed after brelse() */
944 indirect_levels = info->indirect_levels;
945 for (i = 0; i <= indirect_levels; i++) {
946 if (frames[i].bh == NULL)
947 break;
948 brelse(frames[i].bh);
949 frames[i].bh = NULL;
950 }
951 }
952
953 /*
954 * This function increments the frame pointer to search the next leaf
955 * block, and reads in the necessary intervening nodes if the search
956 * should be necessary. Whether or not the search is necessary is
957 * controlled by the hash parameter. If the hash value is even, then
958 * the search is only continued if the next block starts with that
959 * hash value. This is used if we are searching for a specific file.
960 *
961 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
962 *
963 * This function returns 1 if the caller should continue to search,
964 * or 0 if it should not. If there is an error reading one of the
965 * index blocks, it will a negative error code.
966 *
967 * If start_hash is non-null, it will be filled in with the starting
968 * hash of the next page.
969 */
ext4_htree_next_block(struct inode *dir, __u32 hash, struct dx_frame *frame, struct dx_frame *frames, __u32 *start_hash)970 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
971 struct dx_frame *frame,
972 struct dx_frame *frames,
973 __u32 *start_hash)
974 {
975 struct dx_frame *p;
976 struct buffer_head *bh;
977 int num_frames = 0;
978 __u32 bhash;
979
980 p = frame;
981 /*
982 * Find the next leaf page by incrementing the frame pointer.
983 * If we run out of entries in the interior node, loop around and
984 * increment pointer in the parent node. When we break out of
985 * this loop, num_frames indicates the number of interior
986 * nodes need to be read.
987 */
988 while (1) {
989 if (++(p->at) < p->entries + dx_get_count(p->entries))
990 break;
991 if (p == frames)
992 return 0;
993 num_frames++;
994 p--;
995 }
996
997 /*
998 * If the hash is 1, then continue only if the next page has a
999 * continuation hash of any value. This is used for readdir
1000 * handling. Otherwise, check to see if the hash matches the
1001 * desired continuation hash. If it doesn't, return since
1002 * there's no point to read in the successive index pages.
1003 */
1004 bhash = dx_get_hash(p->at);
1005 if (start_hash)
1006 *start_hash = bhash;
1007 if ((hash & 1) == 0) {
1008 if ((bhash & ~1) != hash)
1009 return 0;
1010 }
1011 /*
1012 * If the hash is HASH_NB_ALWAYS, we always go to the next
1013 * block so no check is necessary
1014 */
1015 while (num_frames--) {
1016 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
1017 if (IS_ERR(bh))
1018 return PTR_ERR(bh);
1019 p++;
1020 brelse(p->bh);
1021 p->bh = bh;
1022 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1023 }
1024 return 1;
1025 }
1026
1027
1028 /*
1029 * This function fills a red-black tree with information from a
1030 * directory block. It returns the number directory entries loaded
1031 * into the tree. If there is an error it is returned in err.
1032 */
htree_dirblock_to_tree(struct file *dir_file, struct inode *dir, ext4_lblk_t block, struct dx_hash_info *hinfo, __u32 start_hash, __u32 start_minor_hash)1033 static int htree_dirblock_to_tree(struct file *dir_file,
1034 struct inode *dir, ext4_lblk_t block,
1035 struct dx_hash_info *hinfo,
1036 __u32 start_hash, __u32 start_minor_hash)
1037 {
1038 struct buffer_head *bh;
1039 struct ext4_dir_entry_2 *de, *top;
1040 int err = 0, count = 0;
1041 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
1042
1043 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
1044 (unsigned long)block));
1045 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
1046 if (IS_ERR(bh))
1047 return PTR_ERR(bh);
1048
1049 de = (struct ext4_dir_entry_2 *) bh->b_data;
1050 top = (struct ext4_dir_entry_2 *) ((char *) de +
1051 dir->i_sb->s_blocksize -
1052 EXT4_DIR_REC_LEN(0));
1053 /* Check if the directory is encrypted */
1054 if (IS_ENCRYPTED(dir)) {
1055 err = fscrypt_get_encryption_info(dir);
1056 if (err < 0) {
1057 brelse(bh);
1058 return err;
1059 }
1060 err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
1061 &fname_crypto_str);
1062 if (err < 0) {
1063 brelse(bh);
1064 return err;
1065 }
1066 }
1067
1068 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
1069 if (ext4_check_dir_entry(dir, NULL, de, bh,
1070 bh->b_data, bh->b_size,
1071 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
1072 + ((char *)de - bh->b_data))) {
1073 /* silently ignore the rest of the block */
1074 break;
1075 }
1076 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1077 if ((hinfo->hash < start_hash) ||
1078 ((hinfo->hash == start_hash) &&
1079 (hinfo->minor_hash < start_minor_hash)))
1080 continue;
1081 if (de->inode == 0)
1082 continue;
1083 if (!IS_ENCRYPTED(dir)) {
1084 tmp_str.name = de->name;
1085 tmp_str.len = de->name_len;
1086 err = ext4_htree_store_dirent(dir_file,
1087 hinfo->hash, hinfo->minor_hash, de,
1088 &tmp_str);
1089 } else {
1090 int save_len = fname_crypto_str.len;
1091 struct fscrypt_str de_name = FSTR_INIT(de->name,
1092 de->name_len);
1093
1094 /* Directory is encrypted */
1095 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
1096 hinfo->minor_hash, &de_name,
1097 &fname_crypto_str);
1098 if (err) {
1099 count = err;
1100 goto errout;
1101 }
1102 err = ext4_htree_store_dirent(dir_file,
1103 hinfo->hash, hinfo->minor_hash, de,
1104 &fname_crypto_str);
1105 fname_crypto_str.len = save_len;
1106 }
1107 if (err != 0) {
1108 count = err;
1109 goto errout;
1110 }
1111 count++;
1112 }
1113 errout:
1114 brelse(bh);
1115 fscrypt_fname_free_buffer(&fname_crypto_str);
1116 return count;
1117 }
1118
1119
1120 /*
1121 * This function fills a red-black tree with information from a
1122 * directory. We start scanning the directory in hash order, starting
1123 * at start_hash and start_minor_hash.
1124 *
1125 * This function returns the number of entries inserted into the tree,
1126 * or a negative error code.
1127 */
ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, __u32 start_minor_hash, __u32 *next_hash)1128 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
1129 __u32 start_minor_hash, __u32 *next_hash)
1130 {
1131 struct dx_hash_info hinfo;
1132 struct ext4_dir_entry_2 *de;
1133 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1134 struct inode *dir;
1135 ext4_lblk_t block;
1136 int count = 0;
1137 int ret, err;
1138 __u32 hashval;
1139 struct fscrypt_str tmp_str;
1140
1141 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
1142 start_hash, start_minor_hash));
1143 dir = file_inode(dir_file);
1144 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
1145 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1146 if (hinfo.hash_version <= DX_HASH_TEA)
1147 hinfo.hash_version +=
1148 EXT4_SB(dir->i_sb)->s_hash_unsigned;
1149 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1150 if (ext4_has_inline_data(dir)) {
1151 int has_inline_data = 1;
1152 count = ext4_inlinedir_to_tree(dir_file, dir, 0,
1153 &hinfo, start_hash,
1154 start_minor_hash,
1155 &has_inline_data);
1156 if (has_inline_data) {
1157 *next_hash = ~0;
1158 return count;
1159 }
1160 }
1161 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
1162 start_hash, start_minor_hash);
1163 *next_hash = ~0;
1164 return count;
1165 }
1166 hinfo.hash = start_hash;
1167 hinfo.minor_hash = 0;
1168 frame = dx_probe(NULL, dir, &hinfo, frames);
1169 if (IS_ERR(frame))
1170 return PTR_ERR(frame);
1171
1172 /* Add '.' and '..' from the htree header */
1173 if (!start_hash && !start_minor_hash) {
1174 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1175 tmp_str.name = de->name;
1176 tmp_str.len = de->name_len;
1177 err = ext4_htree_store_dirent(dir_file, 0, 0,
1178 de, &tmp_str);
1179 if (err != 0)
1180 goto errout;
1181 count++;
1182 }
1183 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
1184 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1185 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
1186 tmp_str.name = de->name;
1187 tmp_str.len = de->name_len;
1188 err = ext4_htree_store_dirent(dir_file, 2, 0,
1189 de, &tmp_str);
1190 if (err != 0)
1191 goto errout;
1192 count++;
1193 }
1194
1195 while (1) {
1196 if (fatal_signal_pending(current)) {
1197 err = -ERESTARTSYS;
1198 goto errout;
1199 }
1200 cond_resched();
1201 block = dx_get_block(frame->at);
1202 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
1203 start_hash, start_minor_hash);
1204 if (ret < 0) {
1205 err = ret;
1206 goto errout;
1207 }
1208 count += ret;
1209 hashval = ~0;
1210 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1211 frame, frames, &hashval);
1212 *next_hash = hashval;
1213 if (ret < 0) {
1214 err = ret;
1215 goto errout;
1216 }
1217 /*
1218 * Stop if: (a) there are no more entries, or
1219 * (b) we have inserted at least one entry and the
1220 * next hash value is not a continuation
1221 */
1222 if ((ret == 0) ||
1223 (count && ((hashval & 1) == 0)))
1224 break;
1225 }
1226 dx_release(frames);
1227 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1228 "next hash: %x\n", count, *next_hash));
1229 return count;
1230 errout:
1231 dx_release(frames);
1232 return (err);
1233 }
1234
search_dirblock(struct buffer_head *bh, struct inode *dir, struct ext4_filename *fname, unsigned int offset, struct ext4_dir_entry_2 **res_dir)1235 static inline int search_dirblock(struct buffer_head *bh,
1236 struct inode *dir,
1237 struct ext4_filename *fname,
1238 unsigned int offset,
1239 struct ext4_dir_entry_2 **res_dir)
1240 {
1241 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1242 fname, offset, res_dir);
1243 }
1244
1245 /*
1246 * Directory block splitting, compacting
1247 */
1248
1249 /*
1250 * Create map of hash values, offsets, and sizes, stored at end of block.
1251 * Returns number of entries mapped.
1252 */
dx_make_map(struct inode *dir, struct buffer_head *bh, struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)1253 static int dx_make_map(struct inode *dir, struct buffer_head *bh,
1254 struct dx_hash_info *hinfo,
1255 struct dx_map_entry *map_tail)
1256 {
1257 int count = 0;
1258 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data;
1259 unsigned int buflen = bh->b_size;
1260 char *base = bh->b_data;
1261 struct dx_hash_info h = *hinfo;
1262 int blocksize = EXT4_BLOCK_SIZE(dir->i_sb);
1263
1264 if (ext4_has_metadata_csum(dir->i_sb))
1265 buflen -= sizeof(struct ext4_dir_entry_tail);
1266
1267 while ((char *) de < base + buflen) {
1268 if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen,
1269 ((char *)de) - base))
1270 return -EFSCORRUPTED;
1271 if (de->name_len && de->inode) {
1272 ext4fs_dirhash(dir, de->name, de->name_len, &h);
1273 map_tail--;
1274 map_tail->hash = h.hash;
1275 map_tail->offs = ((char *) de - base)>>2;
1276 map_tail->size = ext4_rec_len_from_disk(de->rec_len,
1277 blocksize);
1278 count++;
1279 cond_resched();
1280 }
1281 de = ext4_next_entry(de, blocksize);
1282 }
1283 return count;
1284 }
1285
1286 /* Sort map by hash value */
dx_sort_map(struct dx_map_entry *map, unsigned count)1287 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1288 {
1289 struct dx_map_entry *p, *q, *top = map + count - 1;
1290 int more;
1291 /* Combsort until bubble sort doesn't suck */
1292 while (count > 2) {
1293 count = count*10/13;
1294 if (count - 9 < 2) /* 9, 10 -> 11 */
1295 count = 11;
1296 for (p = top, q = p - count; q >= map; p--, q--)
1297 if (p->hash < q->hash)
1298 swap(*p, *q);
1299 }
1300 /* Garden variety bubble sort */
1301 do {
1302 more = 0;
1303 q = top;
1304 while (q-- > map) {
1305 if (q[1].hash >= q[0].hash)
1306 continue;
1307 swap(*(q+1), *q);
1308 more = 1;
1309 }
1310 } while(more);
1311 }
1312
dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)1313 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1314 {
1315 struct dx_entry *entries = frame->entries;
1316 struct dx_entry *old = frame->at, *new = old + 1;
1317 int count = dx_get_count(entries);
1318
1319 assert(count < dx_get_limit(entries));
1320 assert(old < entries + count);
1321 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1322 dx_set_hash(new, hash);
1323 dx_set_block(new, block);
1324 dx_set_count(entries, count + 1);
1325 }
1326
1327 #ifdef CONFIG_UNICODE
1328 /*
1329 * Test whether a case-insensitive directory entry matches the filename
1330 * being searched for. If quick is set, assume the name being looked up
1331 * is already in the casefolded form.
1332 *
1333 * Returns: 0 if the directory entry matches, more than 0 if it
1334 * doesn't match or less than zero on error.
1335 */
ext4_ci_compare(const struct inode *parent, const struct qstr *name, const struct qstr *entry, bool quick)1336 int ext4_ci_compare(const struct inode *parent, const struct qstr *name,
1337 const struct qstr *entry, bool quick)
1338 {
1339 const struct super_block *sb = parent->i_sb;
1340 const struct unicode_map *um = sb->s_encoding;
1341 int ret;
1342
1343 if (quick)
1344 ret = utf8_strncasecmp_folded(um, name, entry);
1345 else
1346 ret = utf8_strncasecmp(um, name, entry);
1347
1348 if (ret < 0) {
1349 /* Handle invalid character sequence as either an error
1350 * or as an opaque byte sequence.
1351 */
1352 if (sb_has_strict_encoding(sb))
1353 return -EINVAL;
1354
1355 if (name->len != entry->len)
1356 return 1;
1357
1358 return !!memcmp(name->name, entry->name, name->len);
1359 }
1360
1361 return ret;
1362 }
1363
ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, struct fscrypt_str *cf_name)1364 void ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1365 struct fscrypt_str *cf_name)
1366 {
1367 int len;
1368
1369 if (!IS_CASEFOLDED(dir) || !dir->i_sb->s_encoding) {
1370 cf_name->name = NULL;
1371 return;
1372 }
1373
1374 cf_name->name = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
1375 if (!cf_name->name)
1376 return;
1377
1378 len = utf8_casefold(dir->i_sb->s_encoding,
1379 iname, cf_name->name,
1380 EXT4_NAME_LEN);
1381 if (len <= 0) {
1382 kfree(cf_name->name);
1383 cf_name->name = NULL;
1384 return;
1385 }
1386 cf_name->len = (unsigned) len;
1387
1388 }
1389 #endif
1390
1391 /*
1392 * Test whether a directory entry matches the filename being searched for.
1393 *
1394 * Return: %true if the directory entry matches, otherwise %false.
1395 */
ext4_match(const struct inode *parent, const struct ext4_filename *fname, const struct ext4_dir_entry_2 *de)1396 static inline bool ext4_match(const struct inode *parent,
1397 const struct ext4_filename *fname,
1398 const struct ext4_dir_entry_2 *de)
1399 {
1400 struct fscrypt_name f;
1401 #ifdef CONFIG_UNICODE
1402 const struct qstr entry = {.name = de->name, .len = de->name_len};
1403 #endif
1404
1405 if (!de->inode)
1406 return false;
1407
1408 f.usr_fname = fname->usr_fname;
1409 f.disk_name = fname->disk_name;
1410 #ifdef CONFIG_FS_ENCRYPTION
1411 f.crypto_buf = fname->crypto_buf;
1412 #endif
1413
1414 #ifdef CONFIG_UNICODE
1415 if (parent->i_sb->s_encoding && IS_CASEFOLDED(parent)) {
1416 if (fname->cf_name.name) {
1417 struct qstr cf = {.name = fname->cf_name.name,
1418 .len = fname->cf_name.len};
1419 return !ext4_ci_compare(parent, &cf, &entry, true);
1420 }
1421 return !ext4_ci_compare(parent, fname->usr_fname, &entry,
1422 false);
1423 }
1424 #endif
1425
1426 return fscrypt_match_name(&f, de->name, de->name_len);
1427 }
1428
1429 /*
1430 * Returns 0 if not found, -1 on failure, and 1 on success
1431 */
ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size, struct inode *dir, struct ext4_filename *fname, unsigned int offset, struct ext4_dir_entry_2 **res_dir)1432 int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
1433 struct inode *dir, struct ext4_filename *fname,
1434 unsigned int offset, struct ext4_dir_entry_2 **res_dir)
1435 {
1436 struct ext4_dir_entry_2 * de;
1437 char * dlimit;
1438 int de_len;
1439
1440 de = (struct ext4_dir_entry_2 *)search_buf;
1441 dlimit = search_buf + buf_size;
1442 while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
1443 /* this code is executed quadratically often */
1444 /* do minimal checking `by hand' */
1445 if (de->name + de->name_len <= dlimit &&
1446 ext4_match(dir, fname, de)) {
1447 /* found a match - just to be sure, do
1448 * a full check */
1449 if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
1450 buf_size, offset))
1451 return -1;
1452 *res_dir = de;
1453 return 1;
1454 }
1455 /* prevent looping on a bad block */
1456 de_len = ext4_rec_len_from_disk(de->rec_len,
1457 dir->i_sb->s_blocksize);
1458 if (de_len <= 0)
1459 return -1;
1460 offset += de_len;
1461 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1462 }
1463 return 0;
1464 }
1465
is_dx_internal_node(struct inode *dir, ext4_lblk_t block, struct ext4_dir_entry *de)1466 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1467 struct ext4_dir_entry *de)
1468 {
1469 struct super_block *sb = dir->i_sb;
1470
1471 if (!is_dx(dir))
1472 return 0;
1473 if (block == 0)
1474 return 1;
1475 if (de->inode == 0 &&
1476 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1477 sb->s_blocksize)
1478 return 1;
1479 return 0;
1480 }
1481
1482 /*
1483 * __ext4_find_entry()
1484 *
1485 * finds an entry in the specified directory with the wanted name. It
1486 * returns the cache buffer in which the entry was found, and the entry
1487 * itself (as a parameter - res_dir). It does NOT read the inode of the
1488 * entry - you'll have to do that yourself if you want to.
1489 *
1490 * The returned buffer_head has ->b_count elevated. The caller is expected
1491 * to brelse() it when appropriate.
1492 */
__ext4_find_entry(struct inode *dir, struct ext4_filename *fname, struct ext4_dir_entry_2 **res_dir, int *inlined)1493 static struct buffer_head *__ext4_find_entry(struct inode *dir,
1494 struct ext4_filename *fname,
1495 struct ext4_dir_entry_2 **res_dir,
1496 int *inlined)
1497 {
1498 struct super_block *sb;
1499 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1500 struct buffer_head *bh, *ret = NULL;
1501 ext4_lblk_t start, block;
1502 const u8 *name = fname->usr_fname->name;
1503 size_t ra_max = 0; /* Number of bh's in the readahead
1504 buffer, bh_use[] */
1505 size_t ra_ptr = 0; /* Current index into readahead
1506 buffer */
1507 ext4_lblk_t nblocks;
1508 int i, namelen, retval;
1509
1510 *res_dir = NULL;
1511 sb = dir->i_sb;
1512 namelen = fname->usr_fname->len;
1513 if (namelen > EXT4_NAME_LEN)
1514 return NULL;
1515
1516 if (ext4_has_inline_data(dir)) {
1517 int has_inline_data = 1;
1518 ret = ext4_find_inline_entry(dir, fname, res_dir,
1519 &has_inline_data);
1520 if (inlined)
1521 *inlined = has_inline_data;
1522 if (has_inline_data)
1523 goto cleanup_and_exit;
1524 }
1525
1526 if ((namelen <= 2) && (name[0] == '.') &&
1527 (name[1] == '.' || name[1] == '\0')) {
1528 /*
1529 * "." or ".." will only be in the first block
1530 * NFS may look up ".."; "." should be handled by the VFS
1531 */
1532 block = start = 0;
1533 nblocks = 1;
1534 goto restart;
1535 }
1536 if (is_dx(dir)) {
1537 ret = ext4_dx_find_entry(dir, fname, res_dir);
1538 /*
1539 * On success, or if the error was file not found,
1540 * return. Otherwise, fall back to doing a search the
1541 * old fashioned way.
1542 */
1543 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
1544 goto cleanup_and_exit;
1545 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1546 "falling back\n"));
1547 ret = NULL;
1548 }
1549 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1550 if (!nblocks) {
1551 ret = NULL;
1552 goto cleanup_and_exit;
1553 }
1554 start = EXT4_I(dir)->i_dir_start_lookup;
1555 if (start >= nblocks)
1556 start = 0;
1557 block = start;
1558 restart:
1559 do {
1560 /*
1561 * We deal with the read-ahead logic here.
1562 */
1563 cond_resched();
1564 if (ra_ptr >= ra_max) {
1565 /* Refill the readahead buffer */
1566 ra_ptr = 0;
1567 if (block < start)
1568 ra_max = start - block;
1569 else
1570 ra_max = nblocks - block;
1571 ra_max = min(ra_max, ARRAY_SIZE(bh_use));
1572 retval = ext4_bread_batch(dir, block, ra_max,
1573 false /* wait */, bh_use);
1574 if (retval) {
1575 ret = ERR_PTR(retval);
1576 ra_max = 0;
1577 goto cleanup_and_exit;
1578 }
1579 }
1580 if ((bh = bh_use[ra_ptr++]) == NULL)
1581 goto next;
1582 wait_on_buffer(bh);
1583 if (!buffer_uptodate(bh)) {
1584 EXT4_ERROR_INODE_ERR(dir, EIO,
1585 "reading directory lblock %lu",
1586 (unsigned long) block);
1587 brelse(bh);
1588 ret = ERR_PTR(-EIO);
1589 goto cleanup_and_exit;
1590 }
1591 if (!buffer_verified(bh) &&
1592 !is_dx_internal_node(dir, block,
1593 (struct ext4_dir_entry *)bh->b_data) &&
1594 !ext4_dirblock_csum_verify(dir, bh)) {
1595 EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
1596 "checksumming directory "
1597 "block %lu", (unsigned long)block);
1598 brelse(bh);
1599 ret = ERR_PTR(-EFSBADCRC);
1600 goto cleanup_and_exit;
1601 }
1602 set_buffer_verified(bh);
1603 i = search_dirblock(bh, dir, fname,
1604 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1605 if (i == 1) {
1606 EXT4_I(dir)->i_dir_start_lookup = block;
1607 ret = bh;
1608 goto cleanup_and_exit;
1609 } else {
1610 brelse(bh);
1611 if (i < 0)
1612 goto cleanup_and_exit;
1613 }
1614 next:
1615 if (++block >= nblocks)
1616 block = 0;
1617 } while (block != start);
1618
1619 /*
1620 * If the directory has grown while we were searching, then
1621 * search the last part of the directory before giving up.
1622 */
1623 block = nblocks;
1624 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1625 if (block < nblocks) {
1626 start = 0;
1627 goto restart;
1628 }
1629
1630 cleanup_and_exit:
1631 /* Clean up the read-ahead blocks */
1632 for (; ra_ptr < ra_max; ra_ptr++)
1633 brelse(bh_use[ra_ptr]);
1634 return ret;
1635 }
1636
ext4_find_entry(struct inode *dir, const struct qstr *d_name, struct ext4_dir_entry_2 **res_dir, int *inlined)1637 static struct buffer_head *ext4_find_entry(struct inode *dir,
1638 const struct qstr *d_name,
1639 struct ext4_dir_entry_2 **res_dir,
1640 int *inlined)
1641 {
1642 int err;
1643 struct ext4_filename fname;
1644 struct buffer_head *bh;
1645
1646 err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1647 if (err == -ENOENT)
1648 return NULL;
1649 if (err)
1650 return ERR_PTR(err);
1651
1652 bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1653
1654 ext4_fname_free_filename(&fname);
1655 return bh;
1656 }
1657
ext4_lookup_entry(struct inode *dir, struct dentry *dentry, struct ext4_dir_entry_2 **res_dir)1658 static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1659 struct dentry *dentry,
1660 struct ext4_dir_entry_2 **res_dir)
1661 {
1662 int err;
1663 struct ext4_filename fname;
1664 struct buffer_head *bh;
1665
1666 err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1667 if (err == -ENOENT)
1668 return NULL;
1669 if (err)
1670 return ERR_PTR(err);
1671
1672 bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1673
1674 ext4_fname_free_filename(&fname);
1675 return bh;
1676 }
1677
ext4_dx_find_entry(struct inode *dir, struct ext4_filename *fname, struct ext4_dir_entry_2 **res_dir)1678 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1679 struct ext4_filename *fname,
1680 struct ext4_dir_entry_2 **res_dir)
1681 {
1682 struct super_block * sb = dir->i_sb;
1683 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1684 struct buffer_head *bh;
1685 ext4_lblk_t block;
1686 int retval;
1687
1688 #ifdef CONFIG_FS_ENCRYPTION
1689 *res_dir = NULL;
1690 #endif
1691 frame = dx_probe(fname, dir, NULL, frames);
1692 if (IS_ERR(frame))
1693 return (struct buffer_head *) frame;
1694 do {
1695 block = dx_get_block(frame->at);
1696 bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
1697 if (IS_ERR(bh))
1698 goto errout;
1699
1700 retval = search_dirblock(bh, dir, fname,
1701 block << EXT4_BLOCK_SIZE_BITS(sb),
1702 res_dir);
1703 if (retval == 1)
1704 goto success;
1705 brelse(bh);
1706 if (retval == -1) {
1707 bh = ERR_PTR(ERR_BAD_DX_DIR);
1708 goto errout;
1709 }
1710
1711 /* Check to see if we should continue to search */
1712 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
1713 frames, NULL);
1714 if (retval < 0) {
1715 ext4_warning_inode(dir,
1716 "error %d reading directory index block",
1717 retval);
1718 bh = ERR_PTR(retval);
1719 goto errout;
1720 }
1721 } while (retval == 1);
1722
1723 bh = NULL;
1724 errout:
1725 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
1726 success:
1727 dx_release(frames);
1728 return bh;
1729 }
1730
ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)1731 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1732 {
1733 struct inode *inode;
1734 struct ext4_dir_entry_2 *de;
1735 struct buffer_head *bh;
1736
1737 if (dentry->d_name.len > EXT4_NAME_LEN)
1738 return ERR_PTR(-ENAMETOOLONG);
1739
1740 bh = ext4_lookup_entry(dir, dentry, &de);
1741 if (IS_ERR(bh))
1742 return ERR_CAST(bh);
1743 inode = NULL;
1744 if (bh) {
1745 __u32 ino = le32_to_cpu(de->inode);
1746 brelse(bh);
1747 if (!ext4_valid_inum(dir->i_sb, ino)) {
1748 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1749 return ERR_PTR(-EFSCORRUPTED);
1750 }
1751 if (unlikely(ino == dir->i_ino)) {
1752 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
1753 dentry);
1754 return ERR_PTR(-EFSCORRUPTED);
1755 }
1756 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
1757 if (inode == ERR_PTR(-ESTALE)) {
1758 EXT4_ERROR_INODE(dir,
1759 "deleted inode referenced: %u",
1760 ino);
1761 return ERR_PTR(-EFSCORRUPTED);
1762 }
1763 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
1764 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
1765 !fscrypt_has_permitted_context(dir, inode)) {
1766 ext4_warning(inode->i_sb,
1767 "Inconsistent encryption contexts: %lu/%lu",
1768 dir->i_ino, inode->i_ino);
1769 iput(inode);
1770 return ERR_PTR(-EPERM);
1771 }
1772 }
1773
1774 #ifdef CONFIG_UNICODE
1775 if (!inode && IS_CASEFOLDED(dir)) {
1776 /* Eventually we want to call d_add_ci(dentry, NULL)
1777 * for negative dentries in the encoding case as
1778 * well. For now, prevent the negative dentry
1779 * from being cached.
1780 */
1781 return NULL;
1782 }
1783 #endif
1784 return d_splice_alias(inode, dentry);
1785 }
1786
1787
ext4_get_parent(struct dentry *child)1788 struct dentry *ext4_get_parent(struct dentry *child)
1789 {
1790 __u32 ino;
1791 static const struct qstr dotdot = QSTR_INIT("..", 2);
1792 struct ext4_dir_entry_2 * de;
1793 struct buffer_head *bh;
1794
1795 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
1796 if (IS_ERR(bh))
1797 return ERR_CAST(bh);
1798 if (!bh)
1799 return ERR_PTR(-ENOENT);
1800 ino = le32_to_cpu(de->inode);
1801 brelse(bh);
1802
1803 if (!ext4_valid_inum(child->d_sb, ino)) {
1804 EXT4_ERROR_INODE(d_inode(child),
1805 "bad parent inode number: %u", ino);
1806 return ERR_PTR(-EFSCORRUPTED);
1807 }
1808
1809 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
1810 }
1811
1812 /*
1813 * Move count entries from end of map between two memory locations.
1814 * Returns pointer to last entry moved.
1815 */
1816 static struct ext4_dir_entry_2 *
dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count, unsigned blocksize)1817 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1818 unsigned blocksize)
1819 {
1820 unsigned rec_len = 0;
1821
1822 while (count--) {
1823 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1824 (from + (map->offs<<2));
1825 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1826 memcpy (to, de, rec_len);
1827 ((struct ext4_dir_entry_2 *) to)->rec_len =
1828 ext4_rec_len_to_disk(rec_len, blocksize);
1829
1830 /* wipe dir_entry excluding the rec_len field */
1831 de->inode = 0;
1832 memset(&de->name_len, 0, ext4_rec_len_from_disk(de->rec_len,
1833 blocksize) -
1834 offsetof(struct ext4_dir_entry_2,
1835 name_len));
1836
1837 map++;
1838 to += rec_len;
1839 }
1840 return (struct ext4_dir_entry_2 *) (to - rec_len);
1841 }
1842
1843 /*
1844 * Compact each dir entry in the range to the minimal rec_len.
1845 * Returns pointer to last entry in range.
1846 */
dx_pack_dirents(char *base, unsigned blocksize)1847 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1848 {
1849 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1850 unsigned rec_len = 0;
1851
1852 prev = to = de;
1853 while ((char*)de < base + blocksize) {
1854 next = ext4_next_entry(de, blocksize);
1855 if (de->inode && de->name_len) {
1856 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1857 if (de > to)
1858 memmove(to, de, rec_len);
1859 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1860 prev = to;
1861 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1862 }
1863 de = next;
1864 }
1865 return prev;
1866 }
1867
1868 /*
1869 * Split a full leaf block to make room for a new dir entry.
1870 * Allocate a new block, and move entries so that they are approx. equally full.
1871 * Returns pointer to de in block into which the new entry will be inserted.
1872 */
do_split(handle_t *handle, struct inode *dir, struct buffer_head **bh,struct dx_frame *frame, struct dx_hash_info *hinfo)1873 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1874 struct buffer_head **bh,struct dx_frame *frame,
1875 struct dx_hash_info *hinfo)
1876 {
1877 unsigned blocksize = dir->i_sb->s_blocksize;
1878 unsigned continued;
1879 int count;
1880 struct buffer_head *bh2;
1881 ext4_lblk_t newblock;
1882 u32 hash2;
1883 struct dx_map_entry *map;
1884 char *data1 = (*bh)->b_data, *data2;
1885 unsigned split, move, size;
1886 struct ext4_dir_entry_2 *de = NULL, *de2;
1887 int csum_size = 0;
1888 int err = 0, i;
1889
1890 if (ext4_has_metadata_csum(dir->i_sb))
1891 csum_size = sizeof(struct ext4_dir_entry_tail);
1892
1893 bh2 = ext4_append(handle, dir, &newblock);
1894 if (IS_ERR(bh2)) {
1895 brelse(*bh);
1896 *bh = NULL;
1897 return (struct ext4_dir_entry_2 *) bh2;
1898 }
1899
1900 BUFFER_TRACE(*bh, "get_write_access");
1901 err = ext4_journal_get_write_access(handle, *bh);
1902 if (err)
1903 goto journal_error;
1904
1905 BUFFER_TRACE(frame->bh, "get_write_access");
1906 err = ext4_journal_get_write_access(handle, frame->bh);
1907 if (err)
1908 goto journal_error;
1909
1910 data2 = bh2->b_data;
1911
1912 /* create map in the end of data2 block */
1913 map = (struct dx_map_entry *) (data2 + blocksize);
1914 count = dx_make_map(dir, *bh, hinfo, map);
1915 if (count < 0) {
1916 err = count;
1917 goto journal_error;
1918 }
1919 map -= count;
1920 dx_sort_map(map, count);
1921 /* Ensure that neither split block is over half full */
1922 size = 0;
1923 move = 0;
1924 for (i = count-1; i >= 0; i--) {
1925 /* is more than half of this entry in 2nd half of the block? */
1926 if (size + map[i].size/2 > blocksize/2)
1927 break;
1928 size += map[i].size;
1929 move++;
1930 }
1931 /*
1932 * map index at which we will split
1933 *
1934 * If the sum of active entries didn't exceed half the block size, just
1935 * split it in half by count; each resulting block will have at least
1936 * half the space free.
1937 */
1938 if (i > 0)
1939 split = count - move;
1940 else
1941 split = count/2;
1942
1943 hash2 = map[split].hash;
1944 continued = hash2 == map[split - 1].hash;
1945 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1946 (unsigned long)dx_get_block(frame->at),
1947 hash2, split, count-split));
1948
1949 /* Fancy dance to stay within two buffers */
1950 de2 = dx_move_dirents(data1, data2, map + split, count - split,
1951 blocksize);
1952 de = dx_pack_dirents(data1, blocksize);
1953 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1954 (char *) de,
1955 blocksize);
1956 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1957 (char *) de2,
1958 blocksize);
1959 if (csum_size) {
1960 ext4_initialize_dirent_tail(*bh, blocksize);
1961 ext4_initialize_dirent_tail(bh2, blocksize);
1962 }
1963
1964 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
1965 blocksize, 1));
1966 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
1967 blocksize, 1));
1968
1969 /* Which block gets the new entry? */
1970 if (hinfo->hash >= hash2) {
1971 swap(*bh, bh2);
1972 de = de2;
1973 }
1974 dx_insert_block(frame, hash2 + continued, newblock);
1975 err = ext4_handle_dirty_dirblock(handle, dir, bh2);
1976 if (err)
1977 goto journal_error;
1978 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1979 if (err)
1980 goto journal_error;
1981 brelse(bh2);
1982 dxtrace(dx_show_index("frame", frame->entries));
1983 return de;
1984
1985 journal_error:
1986 brelse(*bh);
1987 brelse(bh2);
1988 *bh = NULL;
1989 ext4_std_error(dir->i_sb, err);
1990 return ERR_PTR(err);
1991 }
1992
ext4_find_dest_de(struct inode *dir, struct inode *inode, struct buffer_head *bh, void *buf, int buf_size, struct ext4_filename *fname, struct ext4_dir_entry_2 **dest_de)1993 int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1994 struct buffer_head *bh,
1995 void *buf, int buf_size,
1996 struct ext4_filename *fname,
1997 struct ext4_dir_entry_2 **dest_de)
1998 {
1999 struct ext4_dir_entry_2 *de;
2000 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
2001 int nlen, rlen;
2002 unsigned int offset = 0;
2003 char *top;
2004
2005 de = (struct ext4_dir_entry_2 *)buf;
2006 top = buf + buf_size - reclen;
2007 while ((char *) de <= top) {
2008 if (ext4_check_dir_entry(dir, NULL, de, bh,
2009 buf, buf_size, offset))
2010 return -EFSCORRUPTED;
2011 if (ext4_match(dir, fname, de))
2012 return -EEXIST;
2013 nlen = EXT4_DIR_REC_LEN(de->name_len);
2014 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2015 if ((de->inode ? rlen - nlen : rlen) >= reclen)
2016 break;
2017 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
2018 offset += rlen;
2019 }
2020 if ((char *) de > top)
2021 return -ENOSPC;
2022
2023 *dest_de = de;
2024 return 0;
2025 }
2026
ext4_insert_dentry(struct inode *inode, struct ext4_dir_entry_2 *de, int buf_size, struct ext4_filename *fname)2027 void ext4_insert_dentry(struct inode *inode,
2028 struct ext4_dir_entry_2 *de,
2029 int buf_size,
2030 struct ext4_filename *fname)
2031 {
2032
2033 int nlen, rlen;
2034
2035 nlen = EXT4_DIR_REC_LEN(de->name_len);
2036 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
2037 if (de->inode) {
2038 struct ext4_dir_entry_2 *de1 =
2039 (struct ext4_dir_entry_2 *)((char *)de + nlen);
2040 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
2041 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
2042 de = de1;
2043 }
2044 de->file_type = EXT4_FT_UNKNOWN;
2045 de->inode = cpu_to_le32(inode->i_ino);
2046 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
2047 de->name_len = fname_len(fname);
2048 memcpy(de->name, fname_name(fname), fname_len(fname));
2049 }
2050
2051 /*
2052 * Add a new entry into a directory (leaf) block. If de is non-NULL,
2053 * it points to a directory entry which is guaranteed to be large
2054 * enough for new directory entry. If de is NULL, then
2055 * add_dirent_to_buf will attempt search the directory block for
2056 * space. It will return -ENOSPC if no space is available, and -EIO
2057 * and -EEXIST if directory entry already exists.
2058 */
add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname, struct inode *dir, struct inode *inode, struct ext4_dir_entry_2 *de, struct buffer_head *bh)2059 static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
2060 struct inode *dir,
2061 struct inode *inode, struct ext4_dir_entry_2 *de,
2062 struct buffer_head *bh)
2063 {
2064 unsigned int blocksize = dir->i_sb->s_blocksize;
2065 int csum_size = 0;
2066 int err, err2;
2067
2068 if (ext4_has_metadata_csum(inode->i_sb))
2069 csum_size = sizeof(struct ext4_dir_entry_tail);
2070
2071 if (!de) {
2072 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
2073 blocksize - csum_size, fname, &de);
2074 if (err)
2075 return err;
2076 }
2077 BUFFER_TRACE(bh, "get_write_access");
2078 err = ext4_journal_get_write_access(handle, bh);
2079 if (err) {
2080 ext4_std_error(dir->i_sb, err);
2081 return err;
2082 }
2083
2084 /* By now the buffer is marked for journaling */
2085 ext4_insert_dentry(inode, de, blocksize, fname);
2086
2087 /*
2088 * XXX shouldn't update any times until successful
2089 * completion of syscall, but too many callers depend
2090 * on this.
2091 *
2092 * XXX similarly, too many callers depend on
2093 * ext4_new_inode() setting the times, but error
2094 * recovery deletes the inode, so the worst that can
2095 * happen is that the times are slightly out of date
2096 * and/or different from the directory change time.
2097 */
2098 dir->i_mtime = dir->i_ctime = current_time(dir);
2099 ext4_update_dx_flag(dir);
2100 inode_inc_iversion(dir);
2101 err2 = ext4_mark_inode_dirty(handle, dir);
2102 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2103 err = ext4_handle_dirty_dirblock(handle, dir, bh);
2104 if (err)
2105 ext4_std_error(dir->i_sb, err);
2106 return err ? err : err2;
2107 }
2108
ext4_check_dx_root(struct inode *dir, struct dx_root *root)2109 static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root)
2110 {
2111 struct fake_dirent *fde;
2112 const char *error_msg;
2113 unsigned int rlen;
2114 unsigned int blocksize = dir->i_sb->s_blocksize;
2115 char *blockend = (char *)root + dir->i_sb->s_blocksize;
2116
2117 fde = &root->dot;
2118 if (unlikely(fde->name_len != 1)) {
2119 error_msg = "invalid name_len for '.'";
2120 goto corrupted;
2121 }
2122 if (unlikely(strncmp(root->dot_name, ".", fde->name_len))) {
2123 error_msg = "invalid name for '.'";
2124 goto corrupted;
2125 }
2126 rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
2127 if (unlikely((char *)fde + rlen >= blockend)) {
2128 error_msg = "invalid rec_len for '.'";
2129 goto corrupted;
2130 }
2131
2132 fde = &root->dotdot;
2133 if (unlikely(fde->name_len != 2)) {
2134 error_msg = "invalid name_len for '..'";
2135 goto corrupted;
2136 }
2137 if (unlikely(strncmp(root->dotdot_name, "..", fde->name_len))) {
2138 error_msg = "invalid name for '..'";
2139 goto corrupted;
2140 }
2141 rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
2142 if (unlikely((char *)fde + rlen >= blockend)) {
2143 error_msg = "invalid rec_len for '..'";
2144 goto corrupted;
2145 }
2146
2147 return true;
2148
2149 corrupted:
2150 EXT4_ERROR_INODE(dir, "Corrupt dir, %s, running e2fsck is recommended",
2151 error_msg);
2152 return false;
2153 }
2154
2155 /*
2156 * This converts a one block unindexed directory to a 3 block indexed
2157 * directory, and adds the dentry to the indexed directory.
2158 */
make_indexed_dir(handle_t *handle, struct ext4_filename *fname, struct inode *dir, struct inode *inode, struct buffer_head *bh)2159 static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
2160 struct inode *dir,
2161 struct inode *inode, struct buffer_head *bh)
2162 {
2163 struct buffer_head *bh2;
2164 struct dx_root *root;
2165 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
2166 struct dx_entry *entries;
2167 struct ext4_dir_entry_2 *de, *de2;
2168 char *data2, *top;
2169 unsigned len;
2170 int retval;
2171 unsigned blocksize;
2172 ext4_lblk_t block;
2173 struct fake_dirent *fde;
2174 int csum_size = 0;
2175
2176 if (ext4_has_metadata_csum(inode->i_sb))
2177 csum_size = sizeof(struct ext4_dir_entry_tail);
2178
2179 blocksize = dir->i_sb->s_blocksize;
2180 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
2181 BUFFER_TRACE(bh, "get_write_access");
2182 retval = ext4_journal_get_write_access(handle, bh);
2183 if (retval) {
2184 ext4_std_error(dir->i_sb, retval);
2185 brelse(bh);
2186 return retval;
2187 }
2188
2189 root = (struct dx_root *) bh->b_data;
2190 if (!ext4_check_dx_root(dir, root)) {
2191 brelse(bh);
2192 return -EFSCORRUPTED;
2193 }
2194
2195 /* The 0th block becomes the root, move the dirents out */
2196 fde = &root->dotdot;
2197 de = (struct ext4_dir_entry_2 *)((char *)fde +
2198 ext4_rec_len_from_disk(fde->rec_len, blocksize));
2199 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
2200
2201 /* Allocate new block for the 0th block's dirents */
2202 bh2 = ext4_append(handle, dir, &block);
2203 if (IS_ERR(bh2)) {
2204 brelse(bh);
2205 return PTR_ERR(bh2);
2206 }
2207 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
2208 data2 = bh2->b_data;
2209
2210 memcpy(data2, de, len);
2211 memset(de, 0, len); /* wipe old data */
2212 de = (struct ext4_dir_entry_2 *) data2;
2213 top = data2 + len;
2214 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
2215 if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
2216 (data2 + (blocksize - csum_size) -
2217 (char *) de))) {
2218 brelse(bh2);
2219 brelse(bh);
2220 return -EFSCORRUPTED;
2221 }
2222 de = de2;
2223 }
2224 de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
2225 (char *) de, blocksize);
2226
2227 if (csum_size)
2228 ext4_initialize_dirent_tail(bh2, blocksize);
2229
2230 /* Initialize the root; the dot dirents already exist */
2231 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
2232 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
2233 blocksize);
2234 memset (&root->info, 0, sizeof(root->info));
2235 root->info.info_length = sizeof(root->info);
2236 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
2237 entries = root->entries;
2238 dx_set_block(entries, 1);
2239 dx_set_count(entries, 1);
2240 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
2241
2242 /* Initialize as for dx_probe */
2243 fname->hinfo.hash_version = root->info.hash_version;
2244 if (fname->hinfo.hash_version <= DX_HASH_TEA)
2245 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
2246 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
2247 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), &fname->hinfo);
2248
2249 memset(frames, 0, sizeof(frames));
2250 frame = frames;
2251 frame->entries = entries;
2252 frame->at = entries;
2253 frame->bh = bh;
2254
2255 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2256 if (retval)
2257 goto out_frames;
2258 retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
2259 if (retval)
2260 goto out_frames;
2261
2262 de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
2263 if (IS_ERR(de)) {
2264 retval = PTR_ERR(de);
2265 goto out_frames;
2266 }
2267
2268 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
2269 out_frames:
2270 /*
2271 * Even if the block split failed, we have to properly write
2272 * out all the changes we did so far. Otherwise we can end up
2273 * with corrupted filesystem.
2274 */
2275 if (retval)
2276 ext4_mark_inode_dirty(handle, dir);
2277 dx_release(frames);
2278 brelse(bh2);
2279 return retval;
2280 }
2281
2282 /*
2283 * ext4_add_entry()
2284 *
2285 * adds a file entry to the specified directory, using the same
2286 * semantics as ext4_find_entry(). It returns NULL if it failed.
2287 *
2288 * NOTE!! The inode part of 'de' is left at 0 - which means you
2289 * may not sleep between calling this and putting something into
2290 * the entry, as someone else might have used it while you slept.
2291 */
ext4_add_entry(handle_t *handle, struct dentry *dentry, struct inode *inode)2292 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
2293 struct inode *inode)
2294 {
2295 struct inode *dir = d_inode(dentry->d_parent);
2296 struct buffer_head *bh = NULL;
2297 struct ext4_dir_entry_2 *de;
2298 struct super_block *sb;
2299 struct ext4_filename fname;
2300 int retval;
2301 int dx_fallback=0;
2302 unsigned blocksize;
2303 ext4_lblk_t block, blocks;
2304 int csum_size = 0;
2305
2306 if (ext4_has_metadata_csum(inode->i_sb))
2307 csum_size = sizeof(struct ext4_dir_entry_tail);
2308
2309 sb = dir->i_sb;
2310 blocksize = sb->s_blocksize;
2311 if (!dentry->d_name.len)
2312 return -EINVAL;
2313
2314 if (fscrypt_is_nokey_name(dentry))
2315 return -ENOKEY;
2316
2317 #ifdef CONFIG_UNICODE
2318 if (sb_has_strict_encoding(sb) && IS_CASEFOLDED(dir) &&
2319 sb->s_encoding && utf8_validate(sb->s_encoding, &dentry->d_name))
2320 return -EINVAL;
2321 #endif
2322
2323 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
2324 if (retval)
2325 return retval;
2326
2327 if (ext4_has_inline_data(dir)) {
2328 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
2329 if (retval < 0)
2330 goto out;
2331 if (retval == 1) {
2332 retval = 0;
2333 goto out;
2334 }
2335 }
2336
2337 if (is_dx(dir)) {
2338 retval = ext4_dx_add_entry(handle, &fname, dir, inode);
2339 if (!retval || (retval != ERR_BAD_DX_DIR))
2340 goto out;
2341 /* Can we just ignore htree data? */
2342 if (ext4_has_metadata_csum(sb)) {
2343 EXT4_ERROR_INODE(dir,
2344 "Directory has corrupted htree index.");
2345 retval = -EFSCORRUPTED;
2346 goto out;
2347 }
2348 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
2349 dx_fallback++;
2350 retval = ext4_mark_inode_dirty(handle, dir);
2351 if (unlikely(retval))
2352 goto out;
2353 }
2354 blocks = dir->i_size >> sb->s_blocksize_bits;
2355 for (block = 0; block < blocks; block++) {
2356 bh = ext4_read_dirblock(dir, block, DIRENT);
2357 if (bh == NULL) {
2358 bh = ext4_bread(handle, dir, block,
2359 EXT4_GET_BLOCKS_CREATE);
2360 goto add_to_new_block;
2361 }
2362 if (IS_ERR(bh)) {
2363 retval = PTR_ERR(bh);
2364 bh = NULL;
2365 goto out;
2366 }
2367 retval = add_dirent_to_buf(handle, &fname, dir, inode,
2368 NULL, bh);
2369 if (retval != -ENOSPC)
2370 goto out;
2371
2372 if (blocks == 1 && !dx_fallback &&
2373 ext4_has_feature_dir_index(sb)) {
2374 retval = make_indexed_dir(handle, &fname, dir,
2375 inode, bh);
2376 bh = NULL; /* make_indexed_dir releases bh */
2377 goto out;
2378 }
2379 brelse(bh);
2380 }
2381 bh = ext4_append(handle, dir, &block);
2382 add_to_new_block:
2383 if (IS_ERR(bh)) {
2384 retval = PTR_ERR(bh);
2385 bh = NULL;
2386 goto out;
2387 }
2388 de = (struct ext4_dir_entry_2 *) bh->b_data;
2389 de->inode = 0;
2390 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
2391
2392 if (csum_size)
2393 ext4_initialize_dirent_tail(bh, blocksize);
2394
2395 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
2396 out:
2397 ext4_fname_free_filename(&fname);
2398 brelse(bh);
2399 if (retval == 0)
2400 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
2401 return retval;
2402 }
2403
2404 /*
2405 * Returns 0 for success, or a negative error value
2406 */
ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, struct inode *dir, struct inode *inode)2407 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
2408 struct inode *dir, struct inode *inode)
2409 {
2410 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
2411 struct dx_entry *entries, *at;
2412 struct buffer_head *bh;
2413 struct super_block *sb = dir->i_sb;
2414 struct ext4_dir_entry_2 *de;
2415 int restart;
2416 int err;
2417
2418 again:
2419 restart = 0;
2420 frame = dx_probe(fname, dir, NULL, frames);
2421 if (IS_ERR(frame))
2422 return PTR_ERR(frame);
2423 entries = frame->entries;
2424 at = frame->at;
2425 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
2426 if (IS_ERR(bh)) {
2427 err = PTR_ERR(bh);
2428 bh = NULL;
2429 goto cleanup;
2430 }
2431
2432 BUFFER_TRACE(bh, "get_write_access");
2433 err = ext4_journal_get_write_access(handle, bh);
2434 if (err)
2435 goto journal_error;
2436
2437 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
2438 if (err != -ENOSPC)
2439 goto cleanup;
2440
2441 err = 0;
2442 /* Block full, should compress but for now just split */
2443 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2444 dx_get_count(entries), dx_get_limit(entries)));
2445 /* Need to split index? */
2446 if (dx_get_count(entries) == dx_get_limit(entries)) {
2447 ext4_lblk_t newblock;
2448 int levels = frame - frames + 1;
2449 unsigned int icount;
2450 int add_level = 1;
2451 struct dx_entry *entries2;
2452 struct dx_node *node2;
2453 struct buffer_head *bh2;
2454
2455 while (frame > frames) {
2456 if (dx_get_count((frame - 1)->entries) <
2457 dx_get_limit((frame - 1)->entries)) {
2458 add_level = 0;
2459 break;
2460 }
2461 frame--; /* split higher index block */
2462 at = frame->at;
2463 entries = frame->entries;
2464 restart = 1;
2465 }
2466 if (add_level && levels == ext4_dir_htree_level(sb)) {
2467 ext4_warning(sb, "Directory (ino: %lu) index full, "
2468 "reach max htree level :%d",
2469 dir->i_ino, levels);
2470 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2471 ext4_warning(sb, "Large directory feature is "
2472 "not enabled on this "
2473 "filesystem");
2474 }
2475 err = -ENOSPC;
2476 goto cleanup;
2477 }
2478 icount = dx_get_count(entries);
2479 bh2 = ext4_append(handle, dir, &newblock);
2480 if (IS_ERR(bh2)) {
2481 err = PTR_ERR(bh2);
2482 goto cleanup;
2483 }
2484 node2 = (struct dx_node *)(bh2->b_data);
2485 entries2 = node2->entries;
2486 memset(&node2->fake, 0, sizeof(struct fake_dirent));
2487 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2488 sb->s_blocksize);
2489 BUFFER_TRACE(frame->bh, "get_write_access");
2490 err = ext4_journal_get_write_access(handle, frame->bh);
2491 if (err)
2492 goto journal_error;
2493 if (!add_level) {
2494 unsigned icount1 = icount/2, icount2 = icount - icount1;
2495 unsigned hash2 = dx_get_hash(entries + icount1);
2496 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2497 icount1, icount2));
2498
2499 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2500 err = ext4_journal_get_write_access(handle,
2501 (frame - 1)->bh);
2502 if (err)
2503 goto journal_error;
2504
2505 memcpy((char *) entries2, (char *) (entries + icount1),
2506 icount2 * sizeof(struct dx_entry));
2507 dx_set_count(entries, icount1);
2508 dx_set_count(entries2, icount2);
2509 dx_set_limit(entries2, dx_node_limit(dir));
2510
2511 /* Which index block gets the new entry? */
2512 if (at - entries >= icount1) {
2513 frame->at = at = at - entries - icount1 + entries2;
2514 frame->entries = entries = entries2;
2515 swap(frame->bh, bh2);
2516 }
2517 dx_insert_block((frame - 1), hash2, newblock);
2518 dxtrace(dx_show_index("node", frame->entries));
2519 dxtrace(dx_show_index("node",
2520 ((struct dx_node *) bh2->b_data)->entries));
2521 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2522 if (err)
2523 goto journal_error;
2524 brelse (bh2);
2525 err = ext4_handle_dirty_dx_node(handle, dir,
2526 (frame - 1)->bh);
2527 if (err)
2528 goto journal_error;
2529 err = ext4_handle_dirty_dx_node(handle, dir,
2530 frame->bh);
2531 if (restart || err)
2532 goto journal_error;
2533 } else {
2534 struct dx_root *dxroot;
2535 memcpy((char *) entries2, (char *) entries,
2536 icount * sizeof(struct dx_entry));
2537 dx_set_limit(entries2, dx_node_limit(dir));
2538
2539 /* Set up root */
2540 dx_set_count(entries, 1);
2541 dx_set_block(entries + 0, newblock);
2542 dxroot = (struct dx_root *)frames[0].bh->b_data;
2543 dxroot->info.indirect_levels += 1;
2544 dxtrace(printk(KERN_DEBUG
2545 "Creating %d level index...\n",
2546 dxroot->info.indirect_levels));
2547 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
2548 if (err)
2549 goto journal_error;
2550 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2551 brelse(bh2);
2552 restart = 1;
2553 goto journal_error;
2554 }
2555 }
2556 de = do_split(handle, dir, &bh, frame, &fname->hinfo);
2557 if (IS_ERR(de)) {
2558 err = PTR_ERR(de);
2559 goto cleanup;
2560 }
2561 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
2562 goto cleanup;
2563
2564 journal_error:
2565 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
2566 cleanup:
2567 brelse(bh);
2568 dx_release(frames);
2569 /* @restart is true means htree-path has been changed, we need to
2570 * repeat dx_probe() to find out valid htree-path
2571 */
2572 if (restart && err == 0)
2573 goto again;
2574 return err;
2575 }
2576
2577 /*
2578 * ext4_generic_delete_entry deletes a directory entry by merging it
2579 * with the previous entry
2580 */
ext4_generic_delete_entry(struct inode *dir, struct ext4_dir_entry_2 *de_del, struct buffer_head *bh, void *entry_buf, int buf_size, int csum_size)2581 int ext4_generic_delete_entry(struct inode *dir,
2582 struct ext4_dir_entry_2 *de_del,
2583 struct buffer_head *bh,
2584 void *entry_buf,
2585 int buf_size,
2586 int csum_size)
2587 {
2588 struct ext4_dir_entry_2 *de, *pde;
2589 unsigned int blocksize = dir->i_sb->s_blocksize;
2590 int i;
2591
2592 i = 0;
2593 pde = NULL;
2594 de = (struct ext4_dir_entry_2 *)entry_buf;
2595 while (i < buf_size - csum_size) {
2596 if (ext4_check_dir_entry(dir, NULL, de, bh,
2597 entry_buf, buf_size, i))
2598 return -EFSCORRUPTED;
2599 if (de == de_del) {
2600 if (pde) {
2601 pde->rec_len = ext4_rec_len_to_disk(
2602 ext4_rec_len_from_disk(pde->rec_len,
2603 blocksize) +
2604 ext4_rec_len_from_disk(de->rec_len,
2605 blocksize),
2606 blocksize);
2607
2608 /* wipe entire dir_entry */
2609 memset(de, 0, ext4_rec_len_from_disk(de->rec_len,
2610 blocksize));
2611 } else {
2612 /* wipe dir_entry excluding the rec_len field */
2613 de->inode = 0;
2614 memset(&de->name_len, 0,
2615 ext4_rec_len_from_disk(de->rec_len,
2616 blocksize) -
2617 offsetof(struct ext4_dir_entry_2,
2618 name_len));
2619 }
2620
2621 inode_inc_iversion(dir);
2622 return 0;
2623 }
2624 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2625 pde = de;
2626 de = ext4_next_entry(de, blocksize);
2627 }
2628 return -ENOENT;
2629 }
2630
ext4_delete_entry(handle_t *handle, struct inode *dir, struct ext4_dir_entry_2 *de_del, struct buffer_head *bh)2631 static int ext4_delete_entry(handle_t *handle,
2632 struct inode *dir,
2633 struct ext4_dir_entry_2 *de_del,
2634 struct buffer_head *bh)
2635 {
2636 int err, csum_size = 0;
2637
2638 if (ext4_has_inline_data(dir)) {
2639 int has_inline_data = 1;
2640 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2641 &has_inline_data);
2642 if (has_inline_data)
2643 return err;
2644 }
2645
2646 if (ext4_has_metadata_csum(dir->i_sb))
2647 csum_size = sizeof(struct ext4_dir_entry_tail);
2648
2649 BUFFER_TRACE(bh, "get_write_access");
2650 err = ext4_journal_get_write_access(handle, bh);
2651 if (unlikely(err))
2652 goto out;
2653
2654 err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data,
2655 dir->i_sb->s_blocksize, csum_size);
2656 if (err)
2657 goto out;
2658
2659 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2660 err = ext4_handle_dirty_dirblock(handle, dir, bh);
2661 if (unlikely(err))
2662 goto out;
2663
2664 return 0;
2665 out:
2666 if (err != -ENOENT)
2667 ext4_std_error(dir->i_sb, err);
2668 return err;
2669 }
2670
2671 /*
2672 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
2673 * since this indicates that nlinks count was previously 1 to avoid overflowing
2674 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
2675 * that subdirectory link counts are not being maintained accurately.
2676 *
2677 * The caller has already checked for i_nlink overflow in case the DIR_LINK
2678 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
2679 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
2680 * on regular files) and to avoid creating huge/slow non-HTREE directories.
2681 */
ext4_inc_count(struct inode *inode)2682 static void ext4_inc_count(struct inode *inode)
2683 {
2684 inc_nlink(inode);
2685 if (is_dx(inode) &&
2686 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
2687 set_nlink(inode, 1);
2688 }
2689
2690 /*
2691 * If a directory had nlink == 1, then we should let it be 1. This indicates
2692 * directory has >EXT4_LINK_MAX subdirs.
2693 */
ext4_dec_count(struct inode *inode)2694 static void ext4_dec_count(struct inode *inode)
2695 {
2696 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2697 drop_nlink(inode);
2698 }
2699
2700
2701 /*
2702 * Add non-directory inode to a directory. On success, the inode reference is
2703 * consumed by dentry is instantiation. This is also indicated by clearing of
2704 * *inodep pointer. On failure, the caller is responsible for dropping the
2705 * inode reference in the safe context.
2706 */
ext4_add_nondir(handle_t *handle, struct dentry *dentry, struct inode **inodep)2707 static int ext4_add_nondir(handle_t *handle,
2708 struct dentry *dentry, struct inode **inodep)
2709 {
2710 struct inode *dir = d_inode(dentry->d_parent);
2711 struct inode *inode = *inodep;
2712 int err = ext4_add_entry(handle, dentry, inode);
2713 if (!err) {
2714 err = ext4_mark_inode_dirty(handle, inode);
2715 if (IS_DIRSYNC(dir))
2716 ext4_handle_sync(handle);
2717 d_instantiate_new(dentry, inode);
2718 *inodep = NULL;
2719 return err;
2720 }
2721 drop_nlink(inode);
2722 ext4_orphan_add(handle, inode);
2723 unlock_new_inode(inode);
2724 return err;
2725 }
2726
2727 /*
2728 * By the time this is called, we already have created
2729 * the directory cache entry for the new file, but it
2730 * is so far negative - it has no inode.
2731 *
2732 * If the create succeeds, we fill in the inode information
2733 * with d_instantiate().
2734 */
ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)2735 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2736 bool excl)
2737 {
2738 handle_t *handle;
2739 struct inode *inode;
2740 int err, credits, retries = 0;
2741
2742 err = dquot_initialize(dir);
2743 if (err)
2744 return err;
2745
2746 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2747 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2748 retry:
2749 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2750 NULL, EXT4_HT_DIR, credits);
2751 handle = ext4_journal_current_handle();
2752 err = PTR_ERR(inode);
2753 if (!IS_ERR(inode)) {
2754 inode->i_op = &ext4_file_inode_operations;
2755 inode->i_fop = &ext4_file_operations;
2756 ext4_set_aops(inode);
2757 err = ext4_add_nondir(handle, dentry, &inode);
2758 if (!err)
2759 ext4_fc_track_create(handle, dentry);
2760 }
2761 if (handle)
2762 ext4_journal_stop(handle);
2763 if (!IS_ERR_OR_NULL(inode))
2764 iput(inode);
2765 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2766 goto retry;
2767 return err;
2768 }
2769
ext4_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)2770 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2771 umode_t mode, dev_t rdev)
2772 {
2773 handle_t *handle;
2774 struct inode *inode;
2775 int err, credits, retries = 0;
2776
2777 err = dquot_initialize(dir);
2778 if (err)
2779 return err;
2780
2781 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2782 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2783 retry:
2784 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
2785 NULL, EXT4_HT_DIR, credits);
2786 handle = ext4_journal_current_handle();
2787 err = PTR_ERR(inode);
2788 if (!IS_ERR(inode)) {
2789 init_special_inode(inode, inode->i_mode, rdev);
2790 inode->i_op = &ext4_special_inode_operations;
2791 err = ext4_add_nondir(handle, dentry, &inode);
2792 if (!err)
2793 ext4_fc_track_create(handle, dentry);
2794 }
2795 if (handle)
2796 ext4_journal_stop(handle);
2797 if (!IS_ERR_OR_NULL(inode))
2798 iput(inode);
2799 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2800 goto retry;
2801 return err;
2802 }
2803
ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)2804 static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2805 {
2806 handle_t *handle;
2807 struct inode *inode;
2808 int err, retries = 0;
2809
2810 err = dquot_initialize(dir);
2811 if (err)
2812 return err;
2813
2814 retry:
2815 inode = ext4_new_inode_start_handle(dir, mode,
2816 NULL, 0, NULL,
2817 EXT4_HT_DIR,
2818 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2819 4 + EXT4_XATTR_TRANS_BLOCKS);
2820 handle = ext4_journal_current_handle();
2821 err = PTR_ERR(inode);
2822 if (!IS_ERR(inode)) {
2823 inode->i_op = &ext4_file_inode_operations;
2824 inode->i_fop = &ext4_file_operations;
2825 ext4_set_aops(inode);
2826 d_tmpfile(dentry, inode);
2827 err = ext4_orphan_add(handle, inode);
2828 if (err)
2829 goto err_unlock_inode;
2830 mark_inode_dirty(inode);
2831 unlock_new_inode(inode);
2832 }
2833 if (handle)
2834 ext4_journal_stop(handle);
2835 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2836 goto retry;
2837 return err;
2838 err_unlock_inode:
2839 ext4_journal_stop(handle);
2840 unlock_new_inode(inode);
2841 return err;
2842 }
2843
ext4_init_dot_dotdot(struct inode *inode, struct ext4_dir_entry_2 *de, int blocksize, int csum_size, unsigned int parent_ino, int dotdot_real_len)2844 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2845 struct ext4_dir_entry_2 *de,
2846 int blocksize, int csum_size,
2847 unsigned int parent_ino, int dotdot_real_len)
2848 {
2849 de->inode = cpu_to_le32(inode->i_ino);
2850 de->name_len = 1;
2851 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2852 blocksize);
2853 strcpy(de->name, ".");
2854 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2855
2856 de = ext4_next_entry(de, blocksize);
2857 de->inode = cpu_to_le32(parent_ino);
2858 de->name_len = 2;
2859 if (!dotdot_real_len)
2860 de->rec_len = ext4_rec_len_to_disk(blocksize -
2861 (csum_size + EXT4_DIR_REC_LEN(1)),
2862 blocksize);
2863 else
2864 de->rec_len = ext4_rec_len_to_disk(
2865 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2866 strcpy(de->name, "..");
2867 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2868
2869 return ext4_next_entry(de, blocksize);
2870 }
2871
ext4_init_new_dir(handle_t *handle, struct inode *dir, struct inode *inode)2872 int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2873 struct inode *inode)
2874 {
2875 struct buffer_head *dir_block = NULL;
2876 struct ext4_dir_entry_2 *de;
2877 ext4_lblk_t block = 0;
2878 unsigned int blocksize = dir->i_sb->s_blocksize;
2879 int csum_size = 0;
2880 int err;
2881
2882 if (ext4_has_metadata_csum(dir->i_sb))
2883 csum_size = sizeof(struct ext4_dir_entry_tail);
2884
2885 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2886 err = ext4_try_create_inline_dir(handle, dir, inode);
2887 if (err < 0 && err != -ENOSPC)
2888 goto out;
2889 if (!err)
2890 goto out;
2891 }
2892
2893 inode->i_size = 0;
2894 dir_block = ext4_append(handle, inode, &block);
2895 if (IS_ERR(dir_block))
2896 return PTR_ERR(dir_block);
2897 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2898 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2899 set_nlink(inode, 2);
2900 if (csum_size)
2901 ext4_initialize_dirent_tail(dir_block, blocksize);
2902
2903 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2904 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
2905 if (err)
2906 goto out;
2907 set_buffer_verified(dir_block);
2908 out:
2909 brelse(dir_block);
2910 return err;
2911 }
2912
ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)2913 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2914 {
2915 handle_t *handle;
2916 struct inode *inode;
2917 int err, err2 = 0, credits, retries = 0;
2918
2919 if (EXT4_DIR_LINK_MAX(dir))
2920 return -EMLINK;
2921
2922 err = dquot_initialize(dir);
2923 if (err)
2924 return err;
2925
2926 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2927 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
2928 retry:
2929 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
2930 &dentry->d_name,
2931 0, NULL, EXT4_HT_DIR, credits);
2932 handle = ext4_journal_current_handle();
2933 err = PTR_ERR(inode);
2934 if (IS_ERR(inode))
2935 goto out_stop;
2936
2937 inode->i_op = &ext4_dir_inode_operations;
2938 inode->i_fop = &ext4_dir_operations;
2939 err = ext4_init_new_dir(handle, dir, inode);
2940 if (err)
2941 goto out_clear_inode;
2942 err = ext4_mark_inode_dirty(handle, inode);
2943 if (!err)
2944 err = ext4_add_entry(handle, dentry, inode);
2945 if (err) {
2946 out_clear_inode:
2947 clear_nlink(inode);
2948 ext4_orphan_add(handle, inode);
2949 unlock_new_inode(inode);
2950 err2 = ext4_mark_inode_dirty(handle, inode);
2951 if (unlikely(err2))
2952 err = err2;
2953 ext4_journal_stop(handle);
2954 iput(inode);
2955 goto out_retry;
2956 }
2957 ext4_inc_count(dir);
2958
2959 ext4_update_dx_flag(dir);
2960 err = ext4_mark_inode_dirty(handle, dir);
2961 if (err)
2962 goto out_clear_inode;
2963 d_instantiate_new(dentry, inode);
2964 ext4_fc_track_create(handle, dentry);
2965 if (IS_DIRSYNC(dir))
2966 ext4_handle_sync(handle);
2967
2968 out_stop:
2969 if (handle)
2970 ext4_journal_stop(handle);
2971 out_retry:
2972 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2973 goto retry;
2974 return err;
2975 }
2976
2977 /*
2978 * routine to check that the specified directory is empty (for rmdir)
2979 */
ext4_empty_dir(struct inode *inode)2980 bool ext4_empty_dir(struct inode *inode)
2981 {
2982 unsigned int offset;
2983 struct buffer_head *bh;
2984 struct ext4_dir_entry_2 *de;
2985 struct super_block *sb;
2986
2987 if (ext4_has_inline_data(inode)) {
2988 int has_inline_data = 1;
2989 int ret;
2990
2991 ret = empty_inline_dir(inode, &has_inline_data);
2992 if (has_inline_data)
2993 return ret;
2994 }
2995
2996 sb = inode->i_sb;
2997 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2998 EXT4_ERROR_INODE(inode, "invalid size");
2999 return false;
3000 }
3001 bh = ext4_read_dirblock(inode, 0, EITHER);
3002 if (IS_ERR(bh))
3003 return false;
3004
3005 de = (struct ext4_dir_entry_2 *) bh->b_data;
3006 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
3007 0) ||
3008 le32_to_cpu(de->inode) != inode->i_ino || strcmp(".", de->name)) {
3009 ext4_warning_inode(inode, "directory missing '.'");
3010 brelse(bh);
3011 return false;
3012 }
3013 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
3014 de = ext4_next_entry(de, sb->s_blocksize);
3015 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
3016 offset) ||
3017 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
3018 ext4_warning_inode(inode, "directory missing '..'");
3019 brelse(bh);
3020 return false;
3021 }
3022 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
3023 while (offset < inode->i_size) {
3024 if (!(offset & (sb->s_blocksize - 1))) {
3025 unsigned int lblock;
3026 brelse(bh);
3027 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
3028 bh = ext4_read_dirblock(inode, lblock, EITHER);
3029 if (bh == NULL) {
3030 offset += sb->s_blocksize;
3031 continue;
3032 }
3033 if (IS_ERR(bh))
3034 return false;
3035 }
3036 de = (struct ext4_dir_entry_2 *) (bh->b_data +
3037 (offset & (sb->s_blocksize - 1)));
3038 if (ext4_check_dir_entry(inode, NULL, de, bh,
3039 bh->b_data, bh->b_size, offset) ||
3040 le32_to_cpu(de->inode)) {
3041 brelse(bh);
3042 return false;
3043 }
3044 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
3045 }
3046 brelse(bh);
3047 return true;
3048 }
3049
3050 /*
3051 * ext4_orphan_add() links an unlinked or truncated inode into a list of
3052 * such inodes, starting at the superblock, in case we crash before the
3053 * file is closed/deleted, or in case the inode truncate spans multiple
3054 * transactions and the last transaction is not recovered after a crash.
3055 *
3056 * At filesystem recovery time, we walk this list deleting unlinked
3057 * inodes and truncating linked inodes in ext4_orphan_cleanup().
3058 *
3059 * Orphan list manipulation functions must be called under i_mutex unless
3060 * we are just creating the inode or deleting it.
3061 */
ext4_orphan_add(handle_t *handle, struct inode *inode)3062 int ext4_orphan_add(handle_t *handle, struct inode *inode)
3063 {
3064 struct super_block *sb = inode->i_sb;
3065 struct ext4_sb_info *sbi = EXT4_SB(sb);
3066 struct ext4_iloc iloc;
3067 int err = 0, rc;
3068 bool dirty = false;
3069
3070 if (!sbi->s_journal || is_bad_inode(inode))
3071 return 0;
3072
3073 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
3074 !inode_is_locked(inode));
3075 /*
3076 * Exit early if inode already is on orphan list. This is a big speedup
3077 * since we don't have to contend on the global s_orphan_lock.
3078 */
3079 if (!list_empty(&EXT4_I(inode)->i_orphan))
3080 return 0;
3081
3082 /*
3083 * Orphan handling is only valid for files with data blocks
3084 * being truncated, or files being unlinked. Note that we either
3085 * hold i_mutex, or the inode can not be referenced from outside,
3086 * so i_nlink should not be bumped due to race
3087 */
3088 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3089 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
3090
3091 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3092 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
3093 if (err)
3094 goto out;
3095
3096 err = ext4_reserve_inode_write(handle, inode, &iloc);
3097 if (err)
3098 goto out;
3099
3100 mutex_lock(&sbi->s_orphan_lock);
3101 /*
3102 * Due to previous errors inode may be already a part of on-disk
3103 * orphan list. If so skip on-disk list modification.
3104 */
3105 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
3106 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
3107 /* Insert this inode at the head of the on-disk orphan list */
3108 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
3109 lock_buffer(sbi->s_sbh);
3110 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
3111 ext4_superblock_csum_set(sb);
3112 unlock_buffer(sbi->s_sbh);
3113 dirty = true;
3114 }
3115 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
3116 mutex_unlock(&sbi->s_orphan_lock);
3117
3118 if (dirty) {
3119 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
3120 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
3121 if (!err)
3122 err = rc;
3123 if (err) {
3124 /*
3125 * We have to remove inode from in-memory list if
3126 * addition to on disk orphan list failed. Stray orphan
3127 * list entries can cause panics at unmount time.
3128 */
3129 mutex_lock(&sbi->s_orphan_lock);
3130 list_del_init(&EXT4_I(inode)->i_orphan);
3131 mutex_unlock(&sbi->s_orphan_lock);
3132 }
3133 } else
3134 brelse(iloc.bh);
3135
3136 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
3137 jbd_debug(4, "orphan inode %lu will point to %d\n",
3138 inode->i_ino, NEXT_ORPHAN(inode));
3139 out:
3140 ext4_std_error(sb, err);
3141 return err;
3142 }
3143
3144 /*
3145 * ext4_orphan_del() removes an unlinked or truncated inode from the list
3146 * of such inodes stored on disk, because it is finally being cleaned up.
3147 */
ext4_orphan_del(handle_t *handle, struct inode *inode)3148 int ext4_orphan_del(handle_t *handle, struct inode *inode)
3149 {
3150 struct list_head *prev;
3151 struct ext4_inode_info *ei = EXT4_I(inode);
3152 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3153 __u32 ino_next;
3154 struct ext4_iloc iloc;
3155 int err = 0;
3156
3157 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
3158 return 0;
3159
3160 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
3161 !inode_is_locked(inode));
3162 /* Do this quick check before taking global s_orphan_lock. */
3163 if (list_empty(&ei->i_orphan))
3164 return 0;
3165
3166 if (handle) {
3167 /* Grab inode buffer early before taking global s_orphan_lock */
3168 err = ext4_reserve_inode_write(handle, inode, &iloc);
3169 }
3170
3171 mutex_lock(&sbi->s_orphan_lock);
3172 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
3173
3174 prev = ei->i_orphan.prev;
3175 list_del_init(&ei->i_orphan);
3176
3177 /* If we're on an error path, we may not have a valid
3178 * transaction handle with which to update the orphan list on
3179 * disk, but we still need to remove the inode from the linked
3180 * list in memory. */
3181 if (!handle || err) {
3182 mutex_unlock(&sbi->s_orphan_lock);
3183 goto out_err;
3184 }
3185
3186 ino_next = NEXT_ORPHAN(inode);
3187 if (prev == &sbi->s_orphan) {
3188 jbd_debug(4, "superblock will point to %u\n", ino_next);
3189 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
3190 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
3191 if (err) {
3192 mutex_unlock(&sbi->s_orphan_lock);
3193 goto out_brelse;
3194 }
3195 lock_buffer(sbi->s_sbh);
3196 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
3197 ext4_superblock_csum_set(inode->i_sb);
3198 unlock_buffer(sbi->s_sbh);
3199 mutex_unlock(&sbi->s_orphan_lock);
3200 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
3201 } else {
3202 struct ext4_iloc iloc2;
3203 struct inode *i_prev =
3204 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
3205
3206 jbd_debug(4, "orphan inode %lu will point to %u\n",
3207 i_prev->i_ino, ino_next);
3208 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
3209 if (err) {
3210 mutex_unlock(&sbi->s_orphan_lock);
3211 goto out_brelse;
3212 }
3213 NEXT_ORPHAN(i_prev) = ino_next;
3214 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
3215 mutex_unlock(&sbi->s_orphan_lock);
3216 }
3217 if (err)
3218 goto out_brelse;
3219 NEXT_ORPHAN(inode) = 0;
3220 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
3221 out_err:
3222 ext4_std_error(inode->i_sb, err);
3223 return err;
3224
3225 out_brelse:
3226 brelse(iloc.bh);
3227 goto out_err;
3228 }
3229
ext4_rmdir(struct inode *dir, struct dentry *dentry)3230 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
3231 {
3232 int retval;
3233 struct inode *inode;
3234 struct buffer_head *bh;
3235 struct ext4_dir_entry_2 *de;
3236 handle_t *handle = NULL;
3237
3238 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3239 return -EIO;
3240
3241 /* Initialize quotas before so that eventual writes go in
3242 * separate transaction */
3243 retval = dquot_initialize(dir);
3244 if (retval)
3245 return retval;
3246 retval = dquot_initialize(d_inode(dentry));
3247 if (retval)
3248 return retval;
3249
3250 retval = -ENOENT;
3251 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
3252 if (IS_ERR(bh))
3253 return PTR_ERR(bh);
3254 if (!bh)
3255 goto end_rmdir;
3256
3257 inode = d_inode(dentry);
3258
3259 retval = -EFSCORRUPTED;
3260 if (le32_to_cpu(de->inode) != inode->i_ino)
3261 goto end_rmdir;
3262
3263 retval = -ENOTEMPTY;
3264 if (!ext4_empty_dir(inode))
3265 goto end_rmdir;
3266
3267 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3268 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3269 if (IS_ERR(handle)) {
3270 retval = PTR_ERR(handle);
3271 handle = NULL;
3272 goto end_rmdir;
3273 }
3274
3275 if (IS_DIRSYNC(dir))
3276 ext4_handle_sync(handle);
3277
3278 retval = ext4_delete_entry(handle, dir, de, bh);
3279 if (retval)
3280 goto end_rmdir;
3281 if (!EXT4_DIR_LINK_EMPTY(inode))
3282 ext4_warning_inode(inode,
3283 "empty directory '%.*s' has too many links (%u)",
3284 dentry->d_name.len, dentry->d_name.name,
3285 inode->i_nlink);
3286 inode_inc_iversion(inode);
3287 clear_nlink(inode);
3288 /* There's no need to set i_disksize: the fact that i_nlink is
3289 * zero will ensure that the right thing happens during any
3290 * recovery. */
3291 inode->i_size = 0;
3292 ext4_orphan_add(handle, inode);
3293 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
3294 retval = ext4_mark_inode_dirty(handle, inode);
3295 if (retval)
3296 goto end_rmdir;
3297 ext4_dec_count(dir);
3298 ext4_update_dx_flag(dir);
3299 ext4_fc_track_unlink(handle, dentry);
3300 retval = ext4_mark_inode_dirty(handle, dir);
3301
3302 #ifdef CONFIG_UNICODE
3303 /* VFS negative dentries are incompatible with Encoding and
3304 * Case-insensitiveness. Eventually we'll want avoid
3305 * invalidating the dentries here, alongside with returning the
3306 * negative dentries at ext4_lookup(), when it is better
3307 * supported by the VFS for the CI case.
3308 */
3309 if (IS_CASEFOLDED(dir))
3310 d_invalidate(dentry);
3311 #endif
3312
3313 end_rmdir:
3314 brelse(bh);
3315 if (handle)
3316 ext4_journal_stop(handle);
3317 return retval;
3318 }
3319
__ext4_unlink(struct inode *dir, const struct qstr *d_name, struct inode *inode, struct dentry *dentry )3320 int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
3321 struct inode *inode,
3322 struct dentry *dentry /* NULL during fast_commit recovery */)
3323 {
3324 int retval = -ENOENT;
3325 struct buffer_head *bh;
3326 struct ext4_dir_entry_2 *de;
3327 handle_t *handle;
3328 int skip_remove_dentry = 0;
3329
3330 /*
3331 * Keep this outside the transaction; it may have to set up the
3332 * directory's encryption key, which isn't GFP_NOFS-safe.
3333 */
3334 bh = ext4_find_entry(dir, d_name, &de, NULL);
3335 if (IS_ERR(bh))
3336 return PTR_ERR(bh);
3337
3338 if (!bh)
3339 return -ENOENT;
3340
3341 if (le32_to_cpu(de->inode) != inode->i_ino) {
3342 /*
3343 * It's okay if we find dont find dentry which matches
3344 * the inode. That's because it might have gotten
3345 * renamed to a different inode number
3346 */
3347 if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
3348 skip_remove_dentry = 1;
3349 else
3350 goto out_bh;
3351 }
3352
3353 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3354 EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
3355 if (IS_ERR(handle)) {
3356 retval = PTR_ERR(handle);
3357 goto out_bh;
3358 }
3359
3360 if (IS_DIRSYNC(dir))
3361 ext4_handle_sync(handle);
3362
3363 if (!skip_remove_dentry) {
3364 retval = ext4_delete_entry(handle, dir, de, bh);
3365 if (retval)
3366 goto out_handle;
3367 dir->i_ctime = dir->i_mtime = current_time(dir);
3368 ext4_update_dx_flag(dir);
3369 retval = ext4_mark_inode_dirty(handle, dir);
3370 if (retval)
3371 goto out_handle;
3372 } else {
3373 retval = 0;
3374 }
3375 if (inode->i_nlink == 0)
3376 ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
3377 d_name->len, d_name->name);
3378 else
3379 drop_nlink(inode);
3380 if (!inode->i_nlink)
3381 ext4_orphan_add(handle, inode);
3382 inode->i_ctime = current_time(inode);
3383 retval = ext4_mark_inode_dirty(handle, inode);
3384 if (dentry && !retval)
3385 ext4_fc_track_unlink(handle, dentry);
3386 out_handle:
3387 ext4_journal_stop(handle);
3388 out_bh:
3389 brelse(bh);
3390 return retval;
3391 }
3392
ext4_unlink(struct inode *dir, struct dentry *dentry)3393 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3394 {
3395 int retval;
3396
3397 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3398 return -EIO;
3399
3400 trace_ext4_unlink_enter(dir, dentry);
3401 /*
3402 * Initialize quotas before so that eventual writes go
3403 * in separate transaction
3404 */
3405 retval = dquot_initialize(dir);
3406 if (retval)
3407 goto out_trace;
3408 retval = dquot_initialize(d_inode(dentry));
3409 if (retval)
3410 goto out_trace;
3411
3412 retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry), dentry);
3413 #ifdef CONFIG_UNICODE
3414 /* VFS negative dentries are incompatible with Encoding and
3415 * Case-insensitiveness. Eventually we'll want avoid
3416 * invalidating the dentries here, alongside with returning the
3417 * negative dentries at ext4_lookup(), when it is better
3418 * supported by the VFS for the CI case.
3419 */
3420 if (IS_CASEFOLDED(dir))
3421 d_invalidate(dentry);
3422 #endif
3423
3424 out_trace:
3425 trace_ext4_unlink_exit(dentry, retval);
3426 return retval;
3427 }
3428
ext4_symlink(struct inode *dir, struct dentry *dentry, const char *symname)3429 static int ext4_symlink(struct inode *dir,
3430 struct dentry *dentry, const char *symname)
3431 {
3432 handle_t *handle;
3433 struct inode *inode;
3434 int err, len = strlen(symname);
3435 int credits;
3436 struct fscrypt_str disk_link;
3437
3438 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
3439 return -EIO;
3440
3441 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
3442 &disk_link);
3443 if (err)
3444 return err;
3445
3446 err = dquot_initialize(dir);
3447 if (err)
3448 return err;
3449
3450 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3451 /*
3452 * For non-fast symlinks, we just allocate inode and put it on
3453 * orphan list in the first transaction => we need bitmap,
3454 * group descriptor, sb, inode block, quota blocks, and
3455 * possibly selinux xattr blocks.
3456 */
3457 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
3458 EXT4_XATTR_TRANS_BLOCKS;
3459 } else {
3460 /*
3461 * Fast symlink. We have to add entry to directory
3462 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
3463 * allocate new inode (bitmap, group descriptor, inode block,
3464 * quota blocks, sb is already counted in previous macros).
3465 */
3466 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3467 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
3468 }
3469
3470 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
3471 &dentry->d_name, 0, NULL,
3472 EXT4_HT_DIR, credits);
3473 handle = ext4_journal_current_handle();
3474 if (IS_ERR(inode)) {
3475 if (handle)
3476 ext4_journal_stop(handle);
3477 return PTR_ERR(inode);
3478 }
3479
3480 if (IS_ENCRYPTED(inode)) {
3481 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
3482 if (err)
3483 goto err_drop_inode;
3484 inode->i_op = &ext4_encrypted_symlink_inode_operations;
3485 }
3486
3487 if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
3488 if (!IS_ENCRYPTED(inode))
3489 inode->i_op = &ext4_symlink_inode_operations;
3490 inode_nohighmem(inode);
3491 ext4_set_aops(inode);
3492 /*
3493 * We cannot call page_symlink() with transaction started
3494 * because it calls into ext4_write_begin() which can wait
3495 * for transaction commit if we are running out of space
3496 * and thus we deadlock. So we have to stop transaction now
3497 * and restart it when symlink contents is written.
3498 *
3499 * To keep fs consistent in case of crash, we have to put inode
3500 * to orphan list in the mean time.
3501 */
3502 drop_nlink(inode);
3503 err = ext4_orphan_add(handle, inode);
3504 if (handle)
3505 ext4_journal_stop(handle);
3506 handle = NULL;
3507 if (err)
3508 goto err_drop_inode;
3509 err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
3510 if (err)
3511 goto err_drop_inode;
3512 /*
3513 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
3514 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
3515 */
3516 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3517 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3518 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
3519 if (IS_ERR(handle)) {
3520 err = PTR_ERR(handle);
3521 handle = NULL;
3522 goto err_drop_inode;
3523 }
3524 set_nlink(inode, 1);
3525 err = ext4_orphan_del(handle, inode);
3526 if (err)
3527 goto err_drop_inode;
3528 } else {
3529 /* clear the extent format for fast symlink */
3530 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
3531 if (!IS_ENCRYPTED(inode)) {
3532 inode->i_op = &ext4_fast_symlink_inode_operations;
3533 inode->i_link = (char *)&EXT4_I(inode)->i_data;
3534 }
3535 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
3536 disk_link.len);
3537 inode->i_size = disk_link.len - 1;
3538 }
3539 EXT4_I(inode)->i_disksize = inode->i_size;
3540 err = ext4_add_nondir(handle, dentry, &inode);
3541 if (handle)
3542 ext4_journal_stop(handle);
3543 if (inode)
3544 iput(inode);
3545 goto out_free_encrypted_link;
3546
3547 err_drop_inode:
3548 if (handle)
3549 ext4_journal_stop(handle);
3550 clear_nlink(inode);
3551 unlock_new_inode(inode);
3552 iput(inode);
3553 out_free_encrypted_link:
3554 if (disk_link.name != (unsigned char *)symname)
3555 kfree(disk_link.name);
3556 return err;
3557 }
3558
__ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)3559 int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
3560 {
3561 handle_t *handle;
3562 int err, retries = 0;
3563 retry:
3564 handle = ext4_journal_start(dir, EXT4_HT_DIR,
3565 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
3566 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
3567 if (IS_ERR(handle))
3568 return PTR_ERR(handle);
3569
3570 if (IS_DIRSYNC(dir))
3571 ext4_handle_sync(handle);
3572
3573 inode->i_ctime = current_time(inode);
3574 ext4_inc_count(inode);
3575 ihold(inode);
3576
3577 err = ext4_add_entry(handle, dentry, inode);
3578 if (!err) {
3579 err = ext4_mark_inode_dirty(handle, inode);
3580 /* this can happen only for tmpfile being
3581 * linked the first time
3582 */
3583 if (inode->i_nlink == 1)
3584 ext4_orphan_del(handle, inode);
3585 d_instantiate(dentry, inode);
3586 ext4_fc_track_link(handle, dentry);
3587 } else {
3588 drop_nlink(inode);
3589 iput(inode);
3590 }
3591 ext4_journal_stop(handle);
3592 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
3593 goto retry;
3594 return err;
3595 }
3596
ext4_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)3597 static int ext4_link(struct dentry *old_dentry,
3598 struct inode *dir, struct dentry *dentry)
3599 {
3600 struct inode *inode = d_inode(old_dentry);
3601 int err;
3602
3603 if (inode->i_nlink >= EXT4_LINK_MAX)
3604 return -EMLINK;
3605
3606 err = fscrypt_prepare_link(old_dentry, dir, dentry);
3607 if (err)
3608 return err;
3609
3610 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
3611 (!projid_eq(EXT4_I(dir)->i_projid,
3612 EXT4_I(old_dentry->d_inode)->i_projid)))
3613 return -EXDEV;
3614
3615 err = dquot_initialize(dir);
3616 if (err)
3617 return err;
3618 return __ext4_link(dir, inode, dentry);
3619 }
3620
3621 /*
3622 * Try to find buffer head where contains the parent block.
3623 * It should be the inode block if it is inlined or the 1st block
3624 * if it is a normal dir.
3625 */
ext4_get_first_dir_block(handle_t *handle, struct inode *inode, int *retval, struct ext4_dir_entry_2 **parent_de, int *inlined)3626 static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3627 struct inode *inode,
3628 int *retval,
3629 struct ext4_dir_entry_2 **parent_de,
3630 int *inlined)
3631 {
3632 struct buffer_head *bh;
3633
3634 if (!ext4_has_inline_data(inode)) {
3635 struct ext4_dir_entry_2 *de;
3636 unsigned int offset;
3637
3638 bh = ext4_read_dirblock(inode, 0, EITHER);
3639 if (IS_ERR(bh)) {
3640 *retval = PTR_ERR(bh);
3641 return NULL;
3642 }
3643
3644 de = (struct ext4_dir_entry_2 *) bh->b_data;
3645 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3646 bh->b_size, 0) ||
3647 le32_to_cpu(de->inode) != inode->i_ino ||
3648 strcmp(".", de->name)) {
3649 EXT4_ERROR_INODE(inode, "directory missing '.'");
3650 brelse(bh);
3651 *retval = -EFSCORRUPTED;
3652 return NULL;
3653 }
3654 offset = ext4_rec_len_from_disk(de->rec_len,
3655 inode->i_sb->s_blocksize);
3656 de = ext4_next_entry(de, inode->i_sb->s_blocksize);
3657 if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
3658 bh->b_size, offset) ||
3659 le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) {
3660 EXT4_ERROR_INODE(inode, "directory missing '..'");
3661 brelse(bh);
3662 *retval = -EFSCORRUPTED;
3663 return NULL;
3664 }
3665 *parent_de = de;
3666
3667 return bh;
3668 }
3669
3670 *inlined = 1;
3671 return ext4_get_first_inline_block(inode, parent_de, retval);
3672 }
3673
3674 struct ext4_renament {
3675 struct inode *dir;
3676 struct dentry *dentry;
3677 struct inode *inode;
3678 bool is_dir;
3679 int dir_nlink_delta;
3680
3681 /* entry for "dentry" */
3682 struct buffer_head *bh;
3683 struct ext4_dir_entry_2 *de;
3684 int inlined;
3685
3686 /* entry for ".." in inode if it's a directory */
3687 struct buffer_head *dir_bh;
3688 struct ext4_dir_entry_2 *parent_de;
3689 int dir_inlined;
3690 };
3691
ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)3692 static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
3693 {
3694 int retval;
3695
3696 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
3697 &retval, &ent->parent_de,
3698 &ent->dir_inlined);
3699 if (!ent->dir_bh)
3700 return retval;
3701 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
3702 return -EFSCORRUPTED;
3703 BUFFER_TRACE(ent->dir_bh, "get_write_access");
3704 return ext4_journal_get_write_access(handle, ent->dir_bh);
3705 }
3706
ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent, unsigned dir_ino)3707 static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
3708 unsigned dir_ino)
3709 {
3710 int retval;
3711
3712 ent->parent_de->inode = cpu_to_le32(dir_ino);
3713 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
3714 if (!ent->dir_inlined) {
3715 if (is_dx(ent->inode)) {
3716 retval = ext4_handle_dirty_dx_node(handle,
3717 ent->inode,
3718 ent->dir_bh);
3719 } else {
3720 retval = ext4_handle_dirty_dirblock(handle, ent->inode,
3721 ent->dir_bh);
3722 }
3723 } else {
3724 retval = ext4_mark_inode_dirty(handle, ent->inode);
3725 }
3726 if (retval) {
3727 ext4_std_error(ent->dir->i_sb, retval);
3728 return retval;
3729 }
3730 return 0;
3731 }
3732
ext4_setent(handle_t *handle, struct ext4_renament *ent, unsigned ino, unsigned file_type)3733 static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
3734 unsigned ino, unsigned file_type)
3735 {
3736 int retval, retval2;
3737
3738 BUFFER_TRACE(ent->bh, "get write access");
3739 retval = ext4_journal_get_write_access(handle, ent->bh);
3740 if (retval)
3741 return retval;
3742 ent->de->inode = cpu_to_le32(ino);
3743 if (ext4_has_feature_filetype(ent->dir->i_sb))
3744 ent->de->file_type = file_type;
3745 inode_inc_iversion(ent->dir);
3746 ent->dir->i_ctime = ent->dir->i_mtime =
3747 current_time(ent->dir);
3748 retval = ext4_mark_inode_dirty(handle, ent->dir);
3749 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
3750 if (!ent->inlined) {
3751 retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
3752 if (unlikely(retval2)) {
3753 ext4_std_error(ent->dir->i_sb, retval2);
3754 return retval2;
3755 }
3756 }
3757 return retval;
3758 }
3759
ext4_resetent(handle_t *handle, struct ext4_renament *ent, unsigned ino, unsigned file_type)3760 static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
3761 unsigned ino, unsigned file_type)
3762 {
3763 struct ext4_renament old = *ent;
3764 int retval = 0;
3765
3766 /*
3767 * old->de could have moved from under us during make indexed dir,
3768 * so the old->de may no longer valid and need to find it again
3769 * before reset old inode info.
3770 */
3771 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3772 &old.inlined);
3773 if (IS_ERR(old.bh))
3774 retval = PTR_ERR(old.bh);
3775 if (!old.bh)
3776 retval = -ENOENT;
3777 if (retval) {
3778 ext4_std_error(old.dir->i_sb, retval);
3779 return;
3780 }
3781
3782 ext4_setent(handle, &old, ino, file_type);
3783 brelse(old.bh);
3784 }
3785
ext4_find_delete_entry(handle_t *handle, struct inode *dir, const struct qstr *d_name)3786 static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
3787 const struct qstr *d_name)
3788 {
3789 int retval = -ENOENT;
3790 struct buffer_head *bh;
3791 struct ext4_dir_entry_2 *de;
3792
3793 bh = ext4_find_entry(dir, d_name, &de, NULL);
3794 if (IS_ERR(bh))
3795 return PTR_ERR(bh);
3796 if (bh) {
3797 retval = ext4_delete_entry(handle, dir, de, bh);
3798 brelse(bh);
3799 }
3800 return retval;
3801 }
3802
ext4_rename_delete(handle_t *handle, struct ext4_renament *ent, int force_reread)3803 static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
3804 int force_reread)
3805 {
3806 int retval;
3807 /*
3808 * ent->de could have moved from under us during htree split, so make
3809 * sure that we are deleting the right entry. We might also be pointing
3810 * to a stale entry in the unused part of ent->bh so just checking inum
3811 * and the name isn't enough.
3812 */
3813 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
3814 ent->de->name_len != ent->dentry->d_name.len ||
3815 strncmp(ent->de->name, ent->dentry->d_name.name,
3816 ent->de->name_len) ||
3817 force_reread) {
3818 retval = ext4_find_delete_entry(handle, ent->dir,
3819 &ent->dentry->d_name);
3820 } else {
3821 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
3822 if (retval == -ENOENT) {
3823 retval = ext4_find_delete_entry(handle, ent->dir,
3824 &ent->dentry->d_name);
3825 }
3826 }
3827
3828 if (retval) {
3829 ext4_warning_inode(ent->dir,
3830 "Deleting old file: nlink %d, error=%d",
3831 ent->dir->i_nlink, retval);
3832 }
3833 }
3834
ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)3835 static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
3836 {
3837 if (ent->dir_nlink_delta) {
3838 if (ent->dir_nlink_delta == -1)
3839 ext4_dec_count(ent->dir);
3840 else
3841 ext4_inc_count(ent->dir);
3842 ext4_mark_inode_dirty(handle, ent->dir);
3843 }
3844 }
3845
ext4_whiteout_for_rename(struct ext4_renament *ent, int credits, handle_t **h)3846 static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
3847 int credits, handle_t **h)
3848 {
3849 struct inode *wh;
3850 handle_t *handle;
3851 int retries = 0;
3852
3853 /*
3854 * for inode block, sb block, group summaries,
3855 * and inode bitmap
3856 */
3857 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
3858 EXT4_XATTR_TRANS_BLOCKS + 4);
3859 retry:
3860 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
3861 &ent->dentry->d_name, 0, NULL,
3862 EXT4_HT_DIR, credits);
3863
3864 handle = ext4_journal_current_handle();
3865 if (IS_ERR(wh)) {
3866 if (handle)
3867 ext4_journal_stop(handle);
3868 if (PTR_ERR(wh) == -ENOSPC &&
3869 ext4_should_retry_alloc(ent->dir->i_sb, &retries))
3870 goto retry;
3871 } else {
3872 *h = handle;
3873 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
3874 wh->i_op = &ext4_special_inode_operations;
3875 }
3876 return wh;
3877 }
3878
3879 /*
3880 * Anybody can rename anything with this: the permission checks are left to the
3881 * higher-level routines.
3882 *
3883 * n.b. old_{dentry,inode) refers to the source dentry/inode
3884 * while new_{dentry,inode) refers to the destination dentry/inode
3885 * This comes from rename(const char *oldpath, const char *newpath)
3886 */
ext4_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)3887 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3888 struct inode *new_dir, struct dentry *new_dentry,
3889 unsigned int flags)
3890 {
3891 handle_t *handle = NULL;
3892 struct ext4_renament old = {
3893 .dir = old_dir,
3894 .dentry = old_dentry,
3895 .inode = d_inode(old_dentry),
3896 };
3897 struct ext4_renament new = {
3898 .dir = new_dir,
3899 .dentry = new_dentry,
3900 .inode = d_inode(new_dentry),
3901 };
3902 int force_reread;
3903 int retval;
3904 struct inode *whiteout = NULL;
3905 int credits;
3906 u8 old_file_type;
3907
3908 if (new.inode && new.inode->i_nlink == 0) {
3909 EXT4_ERROR_INODE(new.inode,
3910 "target of rename is already freed");
3911 return -EFSCORRUPTED;
3912 }
3913
3914 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
3915 (!projid_eq(EXT4_I(new_dir)->i_projid,
3916 EXT4_I(old_dentry->d_inode)->i_projid)))
3917 return -EXDEV;
3918
3919 retval = dquot_initialize(old.dir);
3920 if (retval)
3921 return retval;
3922 retval = dquot_initialize(old.inode);
3923 if (retval)
3924 return retval;
3925 retval = dquot_initialize(new.dir);
3926 if (retval)
3927 return retval;
3928
3929 /* Initialize quotas before so that eventual writes go
3930 * in separate transaction */
3931 if (new.inode) {
3932 retval = dquot_initialize(new.inode);
3933 if (retval)
3934 return retval;
3935 }
3936
3937 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
3938 &old.inlined);
3939 if (IS_ERR(old.bh))
3940 return PTR_ERR(old.bh);
3941
3942 /*
3943 * Check for inode number is _not_ due to possible IO errors.
3944 * We might rmdir the source, keep it as pwd of some process
3945 * and merrily kill the link to whatever was created under the
3946 * same name. Goodbye sticky bit ;-<
3947 */
3948 retval = -ENOENT;
3949 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
3950 goto release_bh;
3951
3952 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
3953 &new.de, &new.inlined);
3954 if (IS_ERR(new.bh)) {
3955 retval = PTR_ERR(new.bh);
3956 new.bh = NULL;
3957 goto release_bh;
3958 }
3959 if (new.bh) {
3960 if (!new.inode) {
3961 brelse(new.bh);
3962 new.bh = NULL;
3963 }
3964 }
3965 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
3966 ext4_alloc_da_blocks(old.inode);
3967
3968 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
3969 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
3970 if (!(flags & RENAME_WHITEOUT)) {
3971 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
3972 if (IS_ERR(handle)) {
3973 retval = PTR_ERR(handle);
3974 goto release_bh;
3975 }
3976 } else {
3977 whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
3978 if (IS_ERR(whiteout)) {
3979 retval = PTR_ERR(whiteout);
3980 goto release_bh;
3981 }
3982 }
3983
3984 old_file_type = old.de->file_type;
3985 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
3986 ext4_handle_sync(handle);
3987
3988 if (S_ISDIR(old.inode->i_mode)) {
3989 if (new.inode) {
3990 retval = -ENOTEMPTY;
3991 if (!ext4_empty_dir(new.inode))
3992 goto end_rename;
3993 } else {
3994 retval = -EMLINK;
3995 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
3996 goto end_rename;
3997 }
3998 retval = ext4_rename_dir_prepare(handle, &old);
3999 if (retval)
4000 goto end_rename;
4001 }
4002 /*
4003 * If we're renaming a file within an inline_data dir and adding or
4004 * setting the new dirent causes a conversion from inline_data to
4005 * extents/blockmap, we need to force the dirent delete code to
4006 * re-read the directory, or else we end up trying to delete a dirent
4007 * from what is now the extent tree root (or a block map).
4008 */
4009 force_reread = (new.dir->i_ino == old.dir->i_ino &&
4010 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
4011
4012 if (whiteout) {
4013 /*
4014 * Do this before adding a new entry, so the old entry is sure
4015 * to be still pointing to the valid old entry.
4016 */
4017 retval = ext4_setent(handle, &old, whiteout->i_ino,
4018 EXT4_FT_CHRDEV);
4019 if (retval)
4020 goto end_rename;
4021 retval = ext4_mark_inode_dirty(handle, whiteout);
4022 if (unlikely(retval))
4023 goto end_rename;
4024
4025 }
4026 if (!new.bh) {
4027 retval = ext4_add_entry(handle, new.dentry, old.inode);
4028 if (retval)
4029 goto end_rename;
4030 } else {
4031 retval = ext4_setent(handle, &new,
4032 old.inode->i_ino, old_file_type);
4033 if (retval)
4034 goto end_rename;
4035 }
4036 if (force_reread)
4037 force_reread = !ext4_test_inode_flag(new.dir,
4038 EXT4_INODE_INLINE_DATA);
4039
4040 /*
4041 * Like most other Unix systems, set the ctime for inodes on a
4042 * rename.
4043 */
4044 old.inode->i_ctime = current_time(old.inode);
4045 retval = ext4_mark_inode_dirty(handle, old.inode);
4046 if (unlikely(retval))
4047 goto end_rename;
4048
4049 if (!whiteout) {
4050 /*
4051 * ok, that's it
4052 */
4053 ext4_rename_delete(handle, &old, force_reread);
4054 }
4055
4056 if (new.inode) {
4057 ext4_dec_count(new.inode);
4058 new.inode->i_ctime = current_time(new.inode);
4059 }
4060 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
4061 ext4_update_dx_flag(old.dir);
4062 if (old.dir_bh) {
4063 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4064 if (retval)
4065 goto end_rename;
4066
4067 ext4_dec_count(old.dir);
4068 if (new.inode) {
4069 /* checked ext4_empty_dir above, can't have another
4070 * parent, ext4_dec_count() won't work for many-linked
4071 * dirs */
4072 clear_nlink(new.inode);
4073 } else {
4074 ext4_inc_count(new.dir);
4075 ext4_update_dx_flag(new.dir);
4076 retval = ext4_mark_inode_dirty(handle, new.dir);
4077 if (unlikely(retval))
4078 goto end_rename;
4079 }
4080 }
4081 retval = ext4_mark_inode_dirty(handle, old.dir);
4082 if (unlikely(retval))
4083 goto end_rename;
4084
4085 if (S_ISDIR(old.inode->i_mode)) {
4086 /*
4087 * We disable fast commits here that's because the
4088 * replay code is not yet capable of changing dot dot
4089 * dirents in directories.
4090 */
4091 ext4_fc_mark_ineligible(old.inode->i_sb,
4092 EXT4_FC_REASON_RENAME_DIR);
4093 } else {
4094 if (new.inode)
4095 ext4_fc_track_unlink(handle, new.dentry);
4096 __ext4_fc_track_link(handle, old.inode, new.dentry);
4097 __ext4_fc_track_unlink(handle, old.inode, old.dentry);
4098 if (whiteout)
4099 __ext4_fc_track_create(handle, whiteout, old.dentry);
4100 }
4101
4102 if (new.inode) {
4103 retval = ext4_mark_inode_dirty(handle, new.inode);
4104 if (unlikely(retval))
4105 goto end_rename;
4106 if (!new.inode->i_nlink)
4107 ext4_orphan_add(handle, new.inode);
4108 }
4109 retval = 0;
4110
4111 end_rename:
4112 if (whiteout) {
4113 if (retval) {
4114 ext4_resetent(handle, &old,
4115 old.inode->i_ino, old_file_type);
4116 drop_nlink(whiteout);
4117 ext4_orphan_add(handle, whiteout);
4118 }
4119 unlock_new_inode(whiteout);
4120 ext4_journal_stop(handle);
4121 iput(whiteout);
4122 } else {
4123 ext4_journal_stop(handle);
4124 }
4125 release_bh:
4126 brelse(old.dir_bh);
4127 brelse(old.bh);
4128 brelse(new.bh);
4129
4130 return retval;
4131 }
4132
ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)4133 static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
4134 struct inode *new_dir, struct dentry *new_dentry)
4135 {
4136 handle_t *handle = NULL;
4137 struct ext4_renament old = {
4138 .dir = old_dir,
4139 .dentry = old_dentry,
4140 .inode = d_inode(old_dentry),
4141 };
4142 struct ext4_renament new = {
4143 .dir = new_dir,
4144 .dentry = new_dentry,
4145 .inode = d_inode(new_dentry),
4146 };
4147 u8 new_file_type;
4148 int retval;
4149 struct timespec64 ctime;
4150
4151 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
4152 !projid_eq(EXT4_I(new_dir)->i_projid,
4153 EXT4_I(old_dentry->d_inode)->i_projid)) ||
4154 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
4155 !projid_eq(EXT4_I(old_dir)->i_projid,
4156 EXT4_I(new_dentry->d_inode)->i_projid)))
4157 return -EXDEV;
4158
4159 retval = dquot_initialize(old.dir);
4160 if (retval)
4161 return retval;
4162 retval = dquot_initialize(new.dir);
4163 if (retval)
4164 return retval;
4165
4166 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
4167 &old.de, &old.inlined);
4168 if (IS_ERR(old.bh))
4169 return PTR_ERR(old.bh);
4170 /*
4171 * Check for inode number is _not_ due to possible IO errors.
4172 * We might rmdir the source, keep it as pwd of some process
4173 * and merrily kill the link to whatever was created under the
4174 * same name. Goodbye sticky bit ;-<
4175 */
4176 retval = -ENOENT;
4177 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
4178 goto end_rename;
4179
4180 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
4181 &new.de, &new.inlined);
4182 if (IS_ERR(new.bh)) {
4183 retval = PTR_ERR(new.bh);
4184 new.bh = NULL;
4185 goto end_rename;
4186 }
4187
4188 /* RENAME_EXCHANGE case: old *and* new must both exist */
4189 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
4190 goto end_rename;
4191
4192 handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
4193 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
4194 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
4195 if (IS_ERR(handle)) {
4196 retval = PTR_ERR(handle);
4197 handle = NULL;
4198 goto end_rename;
4199 }
4200
4201 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
4202 ext4_handle_sync(handle);
4203
4204 if (S_ISDIR(old.inode->i_mode)) {
4205 old.is_dir = true;
4206 retval = ext4_rename_dir_prepare(handle, &old);
4207 if (retval)
4208 goto end_rename;
4209 }
4210 if (S_ISDIR(new.inode->i_mode)) {
4211 new.is_dir = true;
4212 retval = ext4_rename_dir_prepare(handle, &new);
4213 if (retval)
4214 goto end_rename;
4215 }
4216
4217 /*
4218 * Other than the special case of overwriting a directory, parents'
4219 * nlink only needs to be modified if this is a cross directory rename.
4220 */
4221 if (old.dir != new.dir && old.is_dir != new.is_dir) {
4222 old.dir_nlink_delta = old.is_dir ? -1 : 1;
4223 new.dir_nlink_delta = -old.dir_nlink_delta;
4224 retval = -EMLINK;
4225 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
4226 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
4227 goto end_rename;
4228 }
4229
4230 new_file_type = new.de->file_type;
4231 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
4232 if (retval)
4233 goto end_rename;
4234
4235 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
4236 if (retval)
4237 goto end_rename;
4238
4239 /*
4240 * Like most other Unix systems, set the ctime for inodes on a
4241 * rename.
4242 */
4243 ctime = current_time(old.inode);
4244 old.inode->i_ctime = ctime;
4245 new.inode->i_ctime = ctime;
4246 retval = ext4_mark_inode_dirty(handle, old.inode);
4247 if (unlikely(retval))
4248 goto end_rename;
4249 retval = ext4_mark_inode_dirty(handle, new.inode);
4250 if (unlikely(retval))
4251 goto end_rename;
4252 ext4_fc_mark_ineligible(new.inode->i_sb,
4253 EXT4_FC_REASON_CROSS_RENAME);
4254 if (old.dir_bh) {
4255 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
4256 if (retval)
4257 goto end_rename;
4258 }
4259 if (new.dir_bh) {
4260 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
4261 if (retval)
4262 goto end_rename;
4263 }
4264 ext4_update_dir_count(handle, &old);
4265 ext4_update_dir_count(handle, &new);
4266 retval = 0;
4267
4268 end_rename:
4269 brelse(old.dir_bh);
4270 brelse(new.dir_bh);
4271 brelse(old.bh);
4272 brelse(new.bh);
4273 if (handle)
4274 ext4_journal_stop(handle);
4275 return retval;
4276 }
4277
ext4_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)4278 static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
4279 struct inode *new_dir, struct dentry *new_dentry,
4280 unsigned int flags)
4281 {
4282 int err;
4283
4284 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
4285 return -EIO;
4286
4287 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4288 return -EINVAL;
4289
4290 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
4291 flags);
4292 if (err)
4293 return err;
4294
4295 if (flags & RENAME_EXCHANGE) {
4296 return ext4_cross_rename(old_dir, old_dentry,
4297 new_dir, new_dentry);
4298 }
4299
4300 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
4301 }
4302
4303 /*
4304 * directories can handle most operations...
4305 */
4306 const struct inode_operations ext4_dir_inode_operations = {
4307 .create = ext4_create,
4308 .lookup = ext4_lookup,
4309 .link = ext4_link,
4310 .unlink = ext4_unlink,
4311 .symlink = ext4_symlink,
4312 .mkdir = ext4_mkdir,
4313 .rmdir = ext4_rmdir,
4314 .mknod = ext4_mknod,
4315 .tmpfile = ext4_tmpfile,
4316 .rename = ext4_rename2,
4317 .setattr = ext4_setattr,
4318 .getattr = ext4_getattr,
4319 .listxattr = ext4_listxattr,
4320 .get_acl = ext4_get_acl,
4321 .set_acl = ext4_set_acl,
4322 .fiemap = ext4_fiemap,
4323 };
4324
4325 const struct inode_operations ext4_special_inode_operations = {
4326 .setattr = ext4_setattr,
4327 .getattr = ext4_getattr,
4328 .listxattr = ext4_listxattr,
4329 .get_acl = ext4_get_acl,
4330 .set_acl = ext4_set_acl,
4331 };
4332