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