blob: e3e20d0aa299c2e3d31e979063be0333a48b010d [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/namei.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/namei.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 * Directory entry file type support and forward compatibility hooks
18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
19 * Hash Tree Directory indexing (c)
20 * Daniel Phillips, 2001
21 * Hash Tree Directory indexing porting
22 * Christopher Li, 2002
23 * Hash Tree Directory indexing cleanup
24 * Theodore Ts'o, 2002
25 */
26
27#include <linux/fs.h>
28#include <linux/pagemap.h>
Mingming Caodab291a2006-10-11 01:21:01 -070029#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/quotaops.h>
35#include <linux/buffer_head.h>
36#include <linux/bio.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040037#include "ext4.h"
38#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070039
Dave Kleikampac27a0e2006-10-11 01:20:50 -070040#include "xattr.h"
41#include "acl.h"
42
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040043#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044/*
45 * define how far ahead to read directories while searching them.
46 */
47#define NAMEI_RA_CHUNKS 2
48#define NAMEI_RA_BLOCKS 4
Dave Kleikamp8c55e202007-05-24 13:04:54 -040049#define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050#define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
51
Mingming Cao617ba132006-10-11 01:20:53 -070052static struct buffer_head *ext4_append(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -050054 ext4_lblk_t *block, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055{
56 struct buffer_head *bh;
57
Theodore Ts'odf981d02012-08-17 09:48:17 -040058 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
60 EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) {
61 *err = -ENOSPC;
62 return NULL;
63 }
64
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
66
Akinobu Mitaa8716112008-04-17 10:38:59 -040067 bh = ext4_bread(handle, inode, *block, 1, err);
68 if (bh) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069 inode->i_size += inode->i_sb->s_blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -070070 EXT4_I(inode)->i_disksize = inode->i_size;
Akinobu Mitaa8716112008-04-17 10:38:59 -040071 *err = ext4_journal_get_write_access(handle, bh);
72 if (*err) {
73 brelse(bh);
74 bh = NULL;
75 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070076 }
Carlos Maiolino6d1ab102012-09-27 09:31:33 -040077 if (!bh && !(*err)) {
78 *err = -EIO;
79 ext4_error(inode->i_sb,
80 "Directory hole detected on inode %lu\n",
81 inode->i_ino);
82 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083 return bh;
84}
85
86#ifndef assert
87#define assert(test) J_ASSERT(test)
88#endif
89
Dave Kleikampac27a0e2006-10-11 01:20:50 -070090#ifdef DX_DEBUG
91#define dxtrace(command) command
92#else
93#define dxtrace(command)
94#endif
95
96struct fake_dirent
97{
98 __le32 inode;
99 __le16 rec_len;
100 u8 name_len;
101 u8 file_type;
102};
103
104struct dx_countlimit
105{
106 __le16 limit;
107 __le16 count;
108};
109
110struct dx_entry
111{
112 __le32 hash;
113 __le32 block;
114};
115
116/*
117 * dx_root_info is laid out so that if it should somehow get overlaid by a
118 * dirent the two low bits of the hash version will be zero. Therefore, the
119 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
120 */
121
122struct dx_root
123{
124 struct fake_dirent dot;
125 char dot_name[4];
126 struct fake_dirent dotdot;
127 char dotdot_name[4];
128 struct dx_root_info
129 {
130 __le32 reserved_zero;
131 u8 hash_version;
132 u8 info_length; /* 8 */
133 u8 indirect_levels;
134 u8 unused_flags;
135 }
136 info;
137 struct dx_entry entries[0];
138};
139
140struct dx_node
141{
142 struct fake_dirent fake;
143 struct dx_entry entries[0];
144};
145
146
147struct dx_frame
148{
149 struct buffer_head *bh;
150 struct dx_entry *entries;
151 struct dx_entry *at;
152};
153
154struct dx_map_entry
155{
156 u32 hash;
Eric Sandeenef2b02d2007-09-18 22:46:42 -0700157 u16 offs;
158 u16 size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159};
160
Darrick J. Wonge6153912012-04-29 18:23:10 -0400161/*
162 * This goes at the end of each htree block.
163 */
164struct dx_tail {
165 u32 dt_reserved;
166 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
167};
168
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500169static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
170static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400171static inline unsigned dx_get_hash(struct dx_entry *entry);
172static void dx_set_hash(struct dx_entry *entry, unsigned value);
173static unsigned dx_get_count(struct dx_entry *entries);
174static unsigned dx_get_limit(struct dx_entry *entries);
175static void dx_set_count(struct dx_entry *entries, unsigned value);
176static void dx_set_limit(struct dx_entry *entries, unsigned value);
177static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
178static unsigned dx_node_limit(struct inode *dir);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400179static struct dx_frame *dx_probe(const struct qstr *d_name,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 struct inode *dir,
181 struct dx_hash_info *hinfo,
182 struct dx_frame *frame,
183 int *err);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400184static void dx_release(struct dx_frame *frames);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500185static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400186 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187static void dx_sort_map(struct dx_map_entry *map, unsigned count);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400188static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500189 struct dx_map_entry *offsets, int count, unsigned blocksize);
Theodore Ts'o8bad4592009-02-14 21:46:54 -0500190static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500191static void dx_insert_block(struct dx_frame *frame,
192 u32 hash, ext4_lblk_t block);
Mingming Cao617ba132006-10-11 01:20:53 -0700193static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194 struct dx_frame *frame,
195 struct dx_frame *frames,
196 __u32 *start_hash);
Theodore Ts'of702ba02008-09-22 15:21:01 -0400197static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
198 const struct qstr *d_name,
199 struct ext4_dir_entry_2 **res_dir,
200 int *err);
Mingming Cao617ba132006-10-11 01:20:53 -0700201static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 struct inode *inode);
203
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400204/* checksumming functions */
Tao Ma3c47d542012-12-10 14:05:59 -0500205void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
206 unsigned int blocksize)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400207{
208 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
209 t->det_rec_len = ext4_rec_len_to_disk(
210 sizeof(struct ext4_dir_entry_tail), blocksize);
211 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
212}
213
214/* Walk through a dirent block to find a checksum "dirent" at the tail */
215static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
216 struct ext4_dir_entry *de)
217{
218 struct ext4_dir_entry_tail *t;
219
220#ifdef PARANOID
221 struct ext4_dir_entry *d, *top;
222
223 d = de;
224 top = (struct ext4_dir_entry *)(((void *)de) +
225 (EXT4_BLOCK_SIZE(inode->i_sb) -
226 sizeof(struct ext4_dir_entry_tail)));
227 while (d < top && d->rec_len)
228 d = (struct ext4_dir_entry *)(((void *)d) +
229 le16_to_cpu(d->rec_len));
230
231 if (d != top)
232 return NULL;
233
234 t = (struct ext4_dir_entry_tail *)d;
235#else
236 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
237#endif
238
239 if (t->det_reserved_zero1 ||
240 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
241 t->det_reserved_zero2 ||
242 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
243 return NULL;
244
245 return t;
246}
247
248static __le32 ext4_dirent_csum(struct inode *inode,
249 struct ext4_dir_entry *dirent, int size)
250{
251 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
252 struct ext4_inode_info *ei = EXT4_I(inode);
253 __u32 csum;
254
255 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
256 return cpu_to_le32(csum);
257}
258
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500259static void warn_no_space_for_csum(struct inode *inode)
260{
261 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
262 "checksum. Please run e2fsck -D.", inode->i_ino);
263}
264
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400265int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
266{
267 struct ext4_dir_entry_tail *t;
268
269 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
270 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
271 return 1;
272
273 t = get_dirent_tail(inode, dirent);
274 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500275 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400276 return 0;
277 }
278
279 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
280 (void *)t - (void *)dirent))
281 return 0;
282
283 return 1;
284}
285
286static void ext4_dirent_csum_set(struct inode *inode,
287 struct ext4_dir_entry *dirent)
288{
289 struct ext4_dir_entry_tail *t;
290
291 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
292 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
293 return;
294
295 t = get_dirent_tail(inode, dirent);
296 if (!t) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500297 warn_no_space_for_csum(inode);
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400298 return;
299 }
300
301 t->det_checksum = ext4_dirent_csum(inode, dirent,
302 (void *)t - (void *)dirent);
303}
304
Tao Ma3c47d542012-12-10 14:05:59 -0500305int ext4_handle_dirty_dirent_node(handle_t *handle,
306 struct inode *inode,
307 struct buffer_head *bh)
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400308{
309 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
310 return ext4_handle_dirty_metadata(handle, inode, bh);
311}
312
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400313static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
314 struct ext4_dir_entry *dirent,
315 int *offset)
316{
317 struct ext4_dir_entry *dp;
318 struct dx_root_info *root;
319 int count_offset;
320
321 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
322 count_offset = 8;
323 else if (le16_to_cpu(dirent->rec_len) == 12) {
324 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
325 if (le16_to_cpu(dp->rec_len) !=
326 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
327 return NULL;
328 root = (struct dx_root_info *)(((void *)dp + 12));
329 if (root->reserved_zero ||
330 root->info_length != sizeof(struct dx_root_info))
331 return NULL;
332 count_offset = 32;
333 } else
334 return NULL;
335
336 if (offset)
337 *offset = count_offset;
338 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
339}
340
341static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
342 int count_offset, int count, struct dx_tail *t)
343{
344 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
345 struct ext4_inode_info *ei = EXT4_I(inode);
346 __u32 csum, old_csum;
347 int size;
348
349 size = count_offset + (count * sizeof(struct dx_entry));
350 old_csum = t->dt_checksum;
351 t->dt_checksum = 0;
352 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
353 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
354 t->dt_checksum = old_csum;
355
356 return cpu_to_le32(csum);
357}
358
359static int ext4_dx_csum_verify(struct inode *inode,
360 struct ext4_dir_entry *dirent)
361{
362 struct dx_countlimit *c;
363 struct dx_tail *t;
364 int count_offset, limit, count;
365
366 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
367 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
368 return 1;
369
370 c = get_dx_countlimit(inode, dirent, &count_offset);
371 if (!c) {
372 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
373 return 1;
374 }
375 limit = le16_to_cpu(c->limit);
376 count = le16_to_cpu(c->count);
377 if (count_offset + (limit * sizeof(struct dx_entry)) >
378 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500379 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400380 return 1;
381 }
382 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
383
384 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
385 count, t))
386 return 0;
387 return 1;
388}
389
390static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
391{
392 struct dx_countlimit *c;
393 struct dx_tail *t;
394 int count_offset, limit, count;
395
396 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
397 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
398 return;
399
400 c = get_dx_countlimit(inode, dirent, &count_offset);
401 if (!c) {
402 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
403 return;
404 }
405 limit = le16_to_cpu(c->limit);
406 count = le16_to_cpu(c->count);
407 if (count_offset + (limit * sizeof(struct dx_entry)) >
408 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
Theodore Ts'odffe9d82012-11-10 22:20:05 -0500409 warn_no_space_for_csum(inode);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400410 return;
411 }
412 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
413
414 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
415}
416
417static inline int ext4_handle_dirty_dx_node(handle_t *handle,
418 struct inode *inode,
419 struct buffer_head *bh)
420{
421 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
422 return ext4_handle_dirty_metadata(handle, inode, bh);
423}
424
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425/*
Li Zefanf795e142008-07-11 19:27:31 -0400426 * p is at least 6 bytes before the end of page
427 */
428static inline struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500429ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
Li Zefanf795e142008-07-11 19:27:31 -0400430{
431 return (struct ext4_dir_entry_2 *)((char *)p +
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500432 ext4_rec_len_from_disk(p->rec_len, blocksize));
Li Zefanf795e142008-07-11 19:27:31 -0400433}
434
435/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 * Future: use high four bits of block for coalesce-on-delete flags
437 * Mask them off for now.
438 */
439
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500440static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441{
442 return le32_to_cpu(entry->block) & 0x00ffffff;
443}
444
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500445static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446{
447 entry->block = cpu_to_le32(value);
448}
449
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400450static inline unsigned dx_get_hash(struct dx_entry *entry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451{
452 return le32_to_cpu(entry->hash);
453}
454
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400455static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456{
457 entry->hash = cpu_to_le32(value);
458}
459
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400460static inline unsigned dx_get_count(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461{
462 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
463}
464
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400465static inline unsigned dx_get_limit(struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466{
467 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
468}
469
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400470static inline void dx_set_count(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471{
472 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
473}
474
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400475static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476{
477 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
478}
479
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400480static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481{
Mingming Cao617ba132006-10-11 01:20:53 -0700482 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
483 EXT4_DIR_REC_LEN(2) - infosize;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400484
485 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
486 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
487 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400488 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489}
490
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400491static inline unsigned dx_node_limit(struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492{
Mingming Cao617ba132006-10-11 01:20:53 -0700493 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400494
495 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
496 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
497 entry_space -= sizeof(struct dx_tail);
Li Zefand9c769b2008-07-11 19:27:31 -0400498 return entry_space / sizeof(struct dx_entry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499}
500
501/*
502 * Debug
503 */
504#ifdef DX_DEBUG
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400505static void dx_show_index(char * label, struct dx_entry *entries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506{
Andrew Morton63f57932006-10-11 01:21:24 -0700507 int i, n = dx_get_count (entries);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400508 printk(KERN_DEBUG "%s index ", label);
Andrew Morton63f57932006-10-11 01:21:24 -0700509 for (i = 0; i < n; i++) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400510 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500511 0, (unsigned long)dx_get_block(entries + i));
Andrew Morton63f57932006-10-11 01:21:24 -0700512 }
513 printk("\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514}
515
516struct stats
517{
518 unsigned names;
519 unsigned space;
520 unsigned bcount;
521};
522
Mingming Cao617ba132006-10-11 01:20:53 -0700523static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 int size, int show_names)
525{
526 unsigned names = 0, space = 0;
527 char *base = (char *) de;
528 struct dx_hash_info h = *hinfo;
529
530 printk("names: ");
531 while ((char *) de < base + size)
532 {
533 if (de->inode)
534 {
535 if (show_names)
536 {
537 int len = de->name_len;
538 char *name = de->name;
539 while (len--) printk("%c", *name++);
Mingming Cao617ba132006-10-11 01:20:53 -0700540 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 printk(":%x.%u ", h.hash,
Bernd Schubert265c6a02011-07-16 19:41:23 -0400542 (unsigned) ((char *) de - base));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 }
Mingming Cao617ba132006-10-11 01:20:53 -0700544 space += EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 names++;
546 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500547 de = ext4_next_entry(de, size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 }
549 printk("(%i)\n", names);
550 return (struct stats) { names, space, 1 };
551}
552
553struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
554 struct dx_entry *entries, int levels)
555{
556 unsigned blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400557 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 unsigned bcount = 0;
559 struct buffer_head *bh;
560 int err;
561 printk("%i indexed blocks...\n", count);
562 for (i = 0; i < count; i++, entries++)
563 {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500564 ext4_lblk_t block = dx_get_block(entries);
565 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
567 struct stats stats;
568 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
Mingming Cao617ba132006-10-11 01:20:53 -0700569 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 stats = levels?
571 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
Mingming Cao617ba132006-10-11 01:20:53 -0700572 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 names += stats.names;
574 space += stats.space;
575 bcount += stats.bcount;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400576 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 }
578 if (bcount)
Theodore Ts'o60e66792010-05-17 07:00:00 -0400579 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400580 levels ? "" : " ", names, space/bcount,
581 (space/bcount)*100/blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 return (struct stats) { names, space, bcount};
583}
584#endif /* DX_DEBUG */
585
586/*
587 * Probe for a directory leaf block to search.
588 *
589 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
590 * error in the directory index, and the caller should fall back to
591 * searching the directory normally. The callers of dx_probe **MUST**
592 * check for this error code, and make sure it never gets reflected
593 * back to userspace.
594 */
595static struct dx_frame *
Theodore Ts'of702ba02008-09-22 15:21:01 -0400596dx_probe(const struct qstr *d_name, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
598{
599 unsigned count, indirect;
600 struct dx_entry *at, *entries, *p, *q, *m;
601 struct dx_root *root;
602 struct buffer_head *bh;
603 struct dx_frame *frame = frame_in;
604 u32 hash;
605
606 frame->bh = NULL;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400607 if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) {
608 if (*err == 0)
609 *err = ERR_BAD_DX_DIR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610 goto fail;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400611 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 root = (struct dx_root *) bh->b_data;
613 if (root->info.hash_version != DX_HASH_TEA &&
614 root->info.hash_version != DX_HASH_HALF_MD4 &&
615 root->info.hash_version != DX_HASH_LEGACY) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500616 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 root->info.hash_version);
618 brelse(bh);
619 *err = ERR_BAD_DX_DIR;
620 goto fail;
621 }
622 hinfo->hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400623 if (hinfo->hash_version <= DX_HASH_TEA)
624 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700625 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400626 if (d_name)
627 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 hash = hinfo->hash;
629
630 if (root->info.unused_flags & 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500631 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 root->info.unused_flags);
633 brelse(bh);
634 *err = ERR_BAD_DX_DIR;
635 goto fail;
636 }
637
638 if ((indirect = root->info.indirect_levels) > 1) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500639 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 root->info.indirect_levels);
641 brelse(bh);
642 *err = ERR_BAD_DX_DIR;
643 goto fail;
644 }
645
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400646 if (!buffer_verified(bh) &&
647 !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
648 ext4_warning(dir->i_sb, "Root failed checksum");
649 brelse(bh);
650 *err = ERR_BAD_DX_DIR;
651 goto fail;
652 }
653 set_buffer_verified(bh);
654
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 entries = (struct dx_entry *) (((char *)&root->info) +
656 root->info.info_length);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700657
658 if (dx_get_limit(entries) != dx_root_limit(dir,
659 root->info.info_length)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500660 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700661 brelse(bh);
662 *err = ERR_BAD_DX_DIR;
663 goto fail;
664 }
665
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400666 dxtrace(printk("Look up %x", hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 while (1)
668 {
669 count = dx_get_count(entries);
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700670 if (!count || count > dx_get_limit(entries)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500671 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700672 "dx entry: no count or count > limit");
673 brelse(bh);
674 *err = ERR_BAD_DX_DIR;
675 goto fail2;
676 }
677
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 p = entries + 1;
679 q = entries + count - 1;
680 while (p <= q)
681 {
682 m = p + (q - p)/2;
683 dxtrace(printk("."));
684 if (dx_get_hash(m) > hash)
685 q = m - 1;
686 else
687 p = m + 1;
688 }
689
690 if (0) // linear search cross check
691 {
692 unsigned n = count - 1;
693 at = entries;
694 while (n--)
695 {
696 dxtrace(printk(","));
697 if (dx_get_hash(++at) > hash)
698 {
699 at--;
700 break;
701 }
702 }
703 assert (at == p - 1);
704 }
705
706 at = p - 1;
707 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
708 frame->bh = bh;
709 frame->entries = entries;
710 frame->at = at;
711 if (!indirect--) return frame;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400712 if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) {
713 if (!(*err))
714 *err = ERR_BAD_DX_DIR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 goto fail2;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400716 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 at = entries = ((struct dx_node *) bh->b_data)->entries;
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400718
719 if (!buffer_verified(bh) &&
720 !ext4_dx_csum_verify(dir,
721 (struct ext4_dir_entry *)bh->b_data)) {
722 ext4_warning(dir->i_sb, "Node failed checksum");
723 brelse(bh);
724 *err = ERR_BAD_DX_DIR;
725 goto fail;
726 }
727 set_buffer_verified(bh);
728
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700729 if (dx_get_limit(entries) != dx_node_limit (dir)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500730 ext4_warning(dir->i_sb,
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700731 "dx entry: limit != node limit");
732 brelse(bh);
733 *err = ERR_BAD_DX_DIR;
734 goto fail2;
735 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736 frame++;
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700737 frame->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 }
739fail2:
740 while (frame >= frame_in) {
741 brelse(frame->bh);
742 frame--;
743 }
744fail:
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700745 if (*err == ERR_BAD_DX_DIR)
Eric Sandeen12062dd2010-02-15 14:19:27 -0500746 ext4_warning(dir->i_sb,
Zheng Liu9ee49302012-02-20 23:09:36 -0500747 "Corrupt dir inode %lu, running e2fsck is "
Eric Sandeen3d82aba2007-09-18 22:46:38 -0700748 "recommended.", dir->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749 return NULL;
750}
751
752static void dx_release (struct dx_frame *frames)
753{
754 if (frames[0].bh == NULL)
755 return;
756
757 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
758 brelse(frames[1].bh);
759 brelse(frames[0].bh);
760}
761
762/*
763 * This function increments the frame pointer to search the next leaf
764 * block, and reads in the necessary intervening nodes if the search
765 * should be necessary. Whether or not the search is necessary is
766 * controlled by the hash parameter. If the hash value is even, then
767 * the search is only continued if the next block starts with that
768 * hash value. This is used if we are searching for a specific file.
769 *
770 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
771 *
772 * This function returns 1 if the caller should continue to search,
773 * or 0 if it should not. If there is an error reading one of the
774 * index blocks, it will a negative error code.
775 *
776 * If start_hash is non-null, it will be filled in with the starting
777 * hash of the next page.
778 */
Mingming Cao617ba132006-10-11 01:20:53 -0700779static int ext4_htree_next_block(struct inode *dir, __u32 hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 struct dx_frame *frame,
781 struct dx_frame *frames,
782 __u32 *start_hash)
783{
784 struct dx_frame *p;
785 struct buffer_head *bh;
786 int err, num_frames = 0;
787 __u32 bhash;
788
789 p = frame;
790 /*
791 * Find the next leaf page by incrementing the frame pointer.
792 * If we run out of entries in the interior node, loop around and
793 * increment pointer in the parent node. When we break out of
794 * this loop, num_frames indicates the number of interior
795 * nodes need to be read.
796 */
797 while (1) {
798 if (++(p->at) < p->entries + dx_get_count(p->entries))
799 break;
800 if (p == frames)
801 return 0;
802 num_frames++;
803 p--;
804 }
805
806 /*
807 * If the hash is 1, then continue only if the next page has a
808 * continuation hash of any value. This is used for readdir
809 * handling. Otherwise, check to see if the hash matches the
810 * desired contiuation hash. If it doesn't, return since
811 * there's no point to read in the successive index pages.
812 */
813 bhash = dx_get_hash(p->at);
814 if (start_hash)
815 *start_hash = bhash;
816 if ((hash & 1) == 0) {
817 if ((bhash & ~1) != hash)
818 return 0;
819 }
820 /*
821 * If the hash is HASH_NB_ALWAYS, we always go to the next
822 * block so no check is necessary
823 */
824 while (num_frames--) {
Mingming Cao617ba132006-10-11 01:20:53 -0700825 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400826 0, &err))) {
827 if (!err) {
828 ext4_error(dir->i_sb,
829 "Directory hole detected on inode %lu\n",
830 dir->i_ino);
831 return -EIO;
832 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 return err; /* Failure */
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400834 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -0400835
836 if (!buffer_verified(bh) &&
837 !ext4_dx_csum_verify(dir,
838 (struct ext4_dir_entry *)bh->b_data)) {
839 ext4_warning(dir->i_sb, "Node failed checksum");
840 return -EIO;
841 }
842 set_buffer_verified(bh);
843
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844 p++;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400845 brelse(p->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 p->bh = bh;
847 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
848 }
849 return 1;
850}
851
852
853/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 * This function fills a red-black tree with information from a
855 * directory block. It returns the number directory entries loaded
856 * into the tree. If there is an error it is returned in err.
857 */
858static int htree_dirblock_to_tree(struct file *dir_file,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500859 struct inode *dir, ext4_lblk_t block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 struct dx_hash_info *hinfo,
861 __u32 start_hash, __u32 start_minor_hash)
862{
863 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -0700864 struct ext4_dir_entry_2 *de, *top;
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400865 int err = 0, count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500867 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
868 (unsigned long)block));
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400869 if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) {
870 if (!err) {
871 err = -EIO;
872 ext4_error(dir->i_sb,
873 "Directory hole detected on inode %lu\n",
874 dir->i_ino);
875 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 return err;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -0400877 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878
Darrick J. Wongb0336e82012-04-29 18:41:10 -0400879 if (!buffer_verified(bh) &&
880 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
881 return -EIO;
882 set_buffer_verified(bh);
883
Mingming Cao617ba132006-10-11 01:20:53 -0700884 de = (struct ext4_dir_entry_2 *) bh->b_data;
885 top = (struct ext4_dir_entry_2 *) ((char *) de +
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 dir->i_sb->s_blocksize -
Mingming Cao617ba132006-10-11 01:20:53 -0700887 EXT4_DIR_REC_LEN(0));
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500888 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
Theodore Ts'of7c21172011-01-10 12:10:55 -0500889 if (ext4_check_dir_entry(dir, NULL, de, bh,
Tao Ma226ba972012-12-10 14:05:58 -0500890 bh->b_data, bh->b_size,
Theodore Ts'ocad3f002010-12-19 22:07:02 -0500891 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
892 + ((char *)de - bh->b_data))) {
Eric Sandeene6c40212006-12-06 20:36:28 -0800893 /* On error, skip the f_pos to the next block. */
894 dir_file->f_pos = (dir_file->f_pos |
895 (dir->i_sb->s_blocksize - 1)) + 1;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400896 brelse(bh);
Eric Sandeene6c40212006-12-06 20:36:28 -0800897 return count;
898 }
Mingming Cao617ba132006-10-11 01:20:53 -0700899 ext4fs_dirhash(de->name, de->name_len, hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900 if ((hinfo->hash < start_hash) ||
901 ((hinfo->hash == start_hash) &&
902 (hinfo->minor_hash < start_minor_hash)))
903 continue;
904 if (de->inode == 0)
905 continue;
Mingming Cao617ba132006-10-11 01:20:53 -0700906 if ((err = ext4_htree_store_dirent(dir_file,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 hinfo->hash, hinfo->minor_hash, de)) != 0) {
908 brelse(bh);
909 return err;
910 }
911 count++;
912 }
913 brelse(bh);
914 return count;
915}
916
917
918/*
919 * This function fills a red-black tree with information from a
920 * directory. We start scanning the directory in hash order, starting
921 * at start_hash and start_minor_hash.
922 *
923 * This function returns the number of entries inserted into the tree,
924 * or a negative error code.
925 */
Mingming Cao617ba132006-10-11 01:20:53 -0700926int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 __u32 start_minor_hash, __u32 *next_hash)
928{
929 struct dx_hash_info hinfo;
Mingming Cao617ba132006-10-11 01:20:53 -0700930 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 struct dx_frame frames[2], *frame;
932 struct inode *dir;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500933 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 int count = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500935 int ret, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 __u32 hashval;
937
Theodore Ts'o60e66792010-05-17 07:00:00 -0400938 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400939 start_hash, start_minor_hash));
Josef "Jeff" Sipek9d549892006-12-08 02:36:38 -0800940 dir = dir_file->f_path.dentry->d_inode;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400941 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
Mingming Cao617ba132006-10-11 01:20:53 -0700942 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -0400943 if (hinfo.hash_version <= DX_HASH_TEA)
944 hinfo.hash_version +=
945 EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -0700946 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
948 start_hash, start_minor_hash);
949 *next_hash = ~0;
950 return count;
951 }
952 hinfo.hash = start_hash;
953 hinfo.minor_hash = 0;
Theodore Ts'of702ba02008-09-22 15:21:01 -0400954 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 if (!frame)
956 return err;
957
958 /* Add '.' and '..' from the htree header */
959 if (!start_hash && !start_minor_hash) {
Mingming Cao617ba132006-10-11 01:20:53 -0700960 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
961 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 goto errout;
963 count++;
964 }
965 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700966 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -0500967 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
Mingming Cao617ba132006-10-11 01:20:53 -0700968 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 goto errout;
970 count++;
971 }
972
973 while (1) {
974 block = dx_get_block(frame->at);
975 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
976 start_hash, start_minor_hash);
977 if (ret < 0) {
978 err = ret;
979 goto errout;
980 }
981 count += ret;
982 hashval = ~0;
Mingming Cao617ba132006-10-11 01:20:53 -0700983 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 frame, frames, &hashval);
985 *next_hash = hashval;
986 if (ret < 0) {
987 err = ret;
988 goto errout;
989 }
990 /*
991 * Stop if: (a) there are no more entries, or
992 * (b) we have inserted at least one entry and the
993 * next hash value is not a continuation
994 */
995 if ((ret == 0) ||
996 (count && ((hashval & 1) == 0)))
997 break;
998 }
999 dx_release(frames);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001000 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1001 "next hash: %x\n", count, *next_hash));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 return count;
1003errout:
1004 dx_release(frames);
1005 return (err);
1006}
1007
Tao Ma7335cd32012-12-10 14:05:59 -05001008static inline int search_dirblock(struct buffer_head *bh,
1009 struct inode *dir,
1010 const struct qstr *d_name,
1011 unsigned int offset,
1012 struct ext4_dir_entry_2 **res_dir)
1013{
1014 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1015 d_name, offset, res_dir);
1016}
1017
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018/*
1019 * Directory block splitting, compacting
1020 */
1021
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001022/*
1023 * Create map of hash values, offsets, and sizes, stored at end of block.
1024 * Returns number of entries mapped.
1025 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001026static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1027 struct dx_hash_info *hinfo,
1028 struct dx_map_entry *map_tail)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029{
1030 int count = 0;
1031 char *base = (char *) de;
1032 struct dx_hash_info h = *hinfo;
1033
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001034 while ((char *) de < base + blocksize) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 if (de->name_len && de->inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001036 ext4fs_dirhash(de->name, de->name_len, &h);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001037 map_tail--;
1038 map_tail->hash = h.hash;
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001039 map_tail->offs = ((char *) de - base)>>2;
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001040 map_tail->size = le16_to_cpu(de->rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 count++;
1042 cond_resched();
1043 }
1044 /* XXX: do we need to check rec_len == 0 case? -Chris */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001045 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001046 }
1047 return count;
1048}
1049
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001050/* Sort map by hash value */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1052{
Andrew Morton63f57932006-10-11 01:21:24 -07001053 struct dx_map_entry *p, *q, *top = map + count - 1;
1054 int more;
1055 /* Combsort until bubble sort doesn't suck */
1056 while (count > 2) {
1057 count = count*10/13;
1058 if (count - 9 < 2) /* 9, 10 -> 11 */
1059 count = 11;
1060 for (p = top, q = p - count; q >= map; p--, q--)
1061 if (p->hash < q->hash)
1062 swap(*p, *q);
1063 }
1064 /* Garden variety bubble sort */
1065 do {
1066 more = 0;
1067 q = top;
1068 while (q-- > map) {
1069 if (q[1].hash >= q[0].hash)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 continue;
Andrew Morton63f57932006-10-11 01:21:24 -07001071 swap(*(q+1), *q);
1072 more = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 }
1074 } while(more);
1075}
1076
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001077static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001078{
1079 struct dx_entry *entries = frame->entries;
1080 struct dx_entry *old = frame->at, *new = old + 1;
1081 int count = dx_get_count(entries);
1082
1083 assert(count < dx_get_limit(entries));
1084 assert(old < entries + count);
1085 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1086 dx_set_hash(new, hash);
1087 dx_set_block(new, block);
1088 dx_set_count(entries, count + 1);
1089}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091/*
Mingming Cao617ba132006-10-11 01:20:53 -07001092 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 *
Mingming Cao617ba132006-10-11 01:20:53 -07001094 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095 * `de != NULL' is guaranteed by caller.
1096 */
Mingming Cao617ba132006-10-11 01:20:53 -07001097static inline int ext4_match (int len, const char * const name,
1098 struct ext4_dir_entry_2 * de)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099{
1100 if (len != de->name_len)
1101 return 0;
1102 if (!de->inode)
1103 return 0;
1104 return !memcmp(name, de->name, len);
1105}
1106
1107/*
1108 * Returns 0 if not found, -1 on failure, and 1 on success
1109 */
Tao Ma7335cd32012-12-10 14:05:59 -05001110int search_dir(struct buffer_head *bh,
1111 char *search_buf,
1112 int buf_size,
1113 struct inode *dir,
1114 const struct qstr *d_name,
1115 unsigned int offset,
1116 struct ext4_dir_entry_2 **res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117{
Mingming Cao617ba132006-10-11 01:20:53 -07001118 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 char * dlimit;
1120 int de_len;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001121 const char *name = d_name->name;
1122 int namelen = d_name->len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123
Tao Ma7335cd32012-12-10 14:05:59 -05001124 de = (struct ext4_dir_entry_2 *)search_buf;
1125 dlimit = search_buf + buf_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001126 while ((char *) de < dlimit) {
1127 /* this code is executed quadratically often */
1128 /* do minimal checking `by hand' */
1129
1130 if ((char *) de + namelen <= dlimit &&
Mingming Cao617ba132006-10-11 01:20:53 -07001131 ext4_match (namelen, name, de)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 /* found a match - just to be sure, do a full check */
Tao Ma226ba972012-12-10 14:05:58 -05001133 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1134 bh->b_size, offset))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135 return -1;
1136 *res_dir = de;
1137 return 1;
1138 }
1139 /* prevent looping on a bad block */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001140 de_len = ext4_rec_len_from_disk(de->rec_len,
1141 dir->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142 if (de_len <= 0)
1143 return -1;
1144 offset += de_len;
Mingming Cao617ba132006-10-11 01:20:53 -07001145 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001146 }
1147 return 0;
1148}
1149
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001150static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1151 struct ext4_dir_entry *de)
1152{
1153 struct super_block *sb = dir->i_sb;
1154
1155 if (!is_dx(dir))
1156 return 0;
1157 if (block == 0)
1158 return 1;
1159 if (de->inode == 0 &&
1160 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1161 sb->s_blocksize)
1162 return 1;
1163 return 0;
1164}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165
1166/*
Mingming Cao617ba132006-10-11 01:20:53 -07001167 * ext4_find_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 *
1169 * finds an entry in the specified directory with the wanted name. It
1170 * returns the cache buffer in which the entry was found, and the entry
1171 * itself (as a parameter - res_dir). It does NOT read the inode of the
1172 * entry - you'll have to do that yourself if you want to.
1173 *
1174 * The returned buffer_head has ->b_count elevated. The caller is expected
1175 * to brelse() it when appropriate.
1176 */
Theodore Ts'of702ba02008-09-22 15:21:01 -04001177static struct buffer_head * ext4_find_entry (struct inode *dir,
1178 const struct qstr *d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001179 struct ext4_dir_entry_2 ** res_dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001181 struct super_block *sb;
1182 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1183 struct buffer_head *bh, *ret = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001184 ext4_lblk_t start, block, b;
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001185 const u8 *name = d_name->name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186 int ra_max = 0; /* Number of bh's in the readahead
1187 buffer, bh_use[] */
1188 int ra_ptr = 0; /* Current index into readahead
1189 buffer */
1190 int num = 0;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001191 ext4_lblk_t nblocks;
1192 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193 int namelen;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194
1195 *res_dir = NULL;
1196 sb = dir->i_sb;
Theodore Ts'of702ba02008-09-22 15:21:01 -04001197 namelen = d_name->len;
Mingming Cao617ba132006-10-11 01:20:53 -07001198 if (namelen > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 return NULL;
Tao Mae8e948e2012-12-10 14:06:00 -05001200
1201 if (ext4_has_inline_data(dir)) {
1202 int has_inline_data = 1;
1203 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1204 &has_inline_data);
1205 if (has_inline_data)
1206 return ret;
1207 }
1208
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001209 if ((namelen <= 2) && (name[0] == '.') &&
Aaro Koskinen6d5c3aa2010-12-14 21:45:31 -05001210 (name[1] == '.' || name[1] == '\0')) {
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001211 /*
1212 * "." or ".." will only be in the first block
1213 * NFS may look up ".."; "." should be handled by the VFS
1214 */
1215 block = start = 0;
1216 nblocks = 1;
1217 goto restart;
1218 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219 if (is_dx(dir)) {
Theodore Ts'of702ba02008-09-22 15:21:01 -04001220 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 /*
1222 * On success, or if the error was file not found,
1223 * return. Otherwise, fall back to doing a search the
1224 * old fashioned way.
1225 */
1226 if (bh || (err != ERR_BAD_DX_DIR))
1227 return bh;
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001228 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1229 "falling back\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230 }
Mingming Cao617ba132006-10-11 01:20:53 -07001231 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1232 start = EXT4_I(dir)->i_dir_start_lookup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 if (start >= nblocks)
1234 start = 0;
1235 block = start;
1236restart:
1237 do {
1238 /*
1239 * We deal with the read-ahead logic here.
1240 */
1241 if (ra_ptr >= ra_max) {
1242 /* Refill the readahead buffer */
1243 ra_ptr = 0;
1244 b = block;
1245 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1246 /*
1247 * Terminate if we reach the end of the
1248 * directory and must wrap, or if our
1249 * search has finished at this block.
1250 */
1251 if (b >= nblocks || (num && block == start)) {
1252 bh_use[ra_max] = NULL;
1253 break;
1254 }
1255 num++;
Mingming Cao617ba132006-10-11 01:20:53 -07001256 bh = ext4_getblk(NULL, dir, b++, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 bh_use[ra_max] = bh;
1258 if (bh)
Christoph Hellwig65299a32011-08-23 14:50:29 +02001259 ll_rw_block(READ | REQ_META | REQ_PRIO,
1260 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261 }
1262 }
1263 if ((bh = bh_use[ra_ptr++]) == NULL)
1264 goto next;
1265 wait_on_buffer(bh);
1266 if (!buffer_uptodate(bh)) {
1267 /* read error, skip block & hope for the best */
Theodore Ts'o24676da2010-05-16 21:00:00 -04001268 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1269 (unsigned long) block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 brelse(bh);
1271 goto next;
1272 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001273 if (!buffer_verified(bh) &&
Darrick J. Wongc6af88032012-11-12 23:51:02 -05001274 !is_dx_internal_node(dir, block,
1275 (struct ext4_dir_entry *)bh->b_data) &&
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001276 !ext4_dirent_csum_verify(dir,
1277 (struct ext4_dir_entry *)bh->b_data)) {
1278 EXT4_ERROR_INODE(dir, "checksumming directory "
1279 "block %lu", (unsigned long)block);
1280 brelse(bh);
1281 goto next;
1282 }
1283 set_buffer_verified(bh);
Theodore Ts'of702ba02008-09-22 15:21:01 -04001284 i = search_dirblock(bh, dir, d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001285 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 if (i == 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07001287 EXT4_I(dir)->i_dir_start_lookup = block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001288 ret = bh;
1289 goto cleanup_and_exit;
1290 } else {
1291 brelse(bh);
1292 if (i < 0)
1293 goto cleanup_and_exit;
1294 }
1295 next:
1296 if (++block >= nblocks)
1297 block = 0;
1298 } while (block != start);
1299
1300 /*
1301 * If the directory has grown while we were searching, then
1302 * search the last part of the directory before giving up.
1303 */
1304 block = nblocks;
Mingming Cao617ba132006-10-11 01:20:53 -07001305 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306 if (block < nblocks) {
1307 start = 0;
1308 goto restart;
1309 }
1310
1311cleanup_and_exit:
1312 /* Clean up the read-ahead blocks */
1313 for (; ra_ptr < ra_max; ra_ptr++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001314 brelse(bh_use[ra_ptr]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001315 return ret;
1316}
1317
Theodore Ts'of702ba02008-09-22 15:21:01 -04001318static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
Mingming Cao617ba132006-10-11 01:20:53 -07001319 struct ext4_dir_entry_2 **res_dir, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320{
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001321 struct super_block * sb = dir->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 struct dx_hash_info hinfo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 struct dx_frame frames[2], *frame;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 struct buffer_head *bh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001325 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001327
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001328 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1329 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 do {
1331 block = dx_get_block(frame->at);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001332 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1333 if (!(*err)) {
1334 *err = -EIO;
1335 ext4_error(dir->i_sb,
1336 "Directory hole detected on inode %lu\n",
1337 dir->i_ino);
1338 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339 goto errout;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001340 }
Duane Griffinf3b35f02008-07-11 19:27:31 -04001341
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001342 if (!buffer_verified(bh) &&
1343 !ext4_dirent_csum_verify(dir,
1344 (struct ext4_dir_entry *)bh->b_data)) {
1345 EXT4_ERROR_INODE(dir, "checksumming directory "
1346 "block %lu", (unsigned long)block);
1347 brelse(bh);
1348 *err = -EIO;
1349 goto errout;
1350 }
1351 set_buffer_verified(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001352 retval = search_dirblock(bh, dir, d_name,
1353 block << EXT4_BLOCK_SIZE_BITS(sb),
1354 res_dir);
1355 if (retval == 1) { /* Success! */
1356 dx_release(frames);
1357 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001358 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001359 brelse(bh);
Theodore Ts'o7845c042010-10-27 21:30:08 -04001360 if (retval == -1) {
1361 *err = ERR_BAD_DX_DIR;
1362 goto errout;
1363 }
1364
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001365 /* Check to see if we should continue to search */
Theodore Ts'o8941ec82010-10-27 21:30:08 -04001366 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001367 frames, NULL);
1368 if (retval < 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05001369 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001370 "error reading index page in directory #%lu",
1371 dir->i_ino);
1372 *err = retval;
1373 goto errout;
1374 }
1375 } while (retval == 1);
1376
1377 *err = -ENOENT;
1378errout:
Bernd Schubert265c6a02011-07-16 19:41:23 -04001379 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001380 dx_release (frames);
1381 return NULL;
1382}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383
Al Viro00cd8dd2012-06-10 17:13:09 -04001384static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001385{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001386 struct inode *inode;
1387 struct ext4_dir_entry_2 *de;
1388 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389
Mingming Cao617ba132006-10-11 01:20:53 -07001390 if (dentry->d_name.len > EXT4_NAME_LEN)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391 return ERR_PTR(-ENAMETOOLONG);
1392
Theodore Ts'of702ba02008-09-22 15:21:01 -04001393 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 inode = NULL;
1395 if (bh) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001396 __u32 ino = le32_to_cpu(de->inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001397 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07001398 if (!ext4_valid_inum(dir->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001399 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001400 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001401 }
Andreas Dilger7e936b72012-05-28 17:02:25 -04001402 if (unlikely(ino == dir->i_ino)) {
1403 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1404 dentry->d_name.len,
1405 dentry->d_name.name);
1406 return ERR_PTR(-EIO);
1407 }
David Howells1d1fe1e2008-02-07 00:15:37 -08001408 inode = ext4_iget(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -04001409 if (inode == ERR_PTR(-ESTALE)) {
1410 EXT4_ERROR_INODE(dir,
1411 "deleted inode referenced: %u",
1412 ino);
1413 return ERR_PTR(-EIO);
Bryan Donlane6f009b2009-02-22 21:20:25 -05001414 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 }
1416 return d_splice_alias(inode, dentry);
1417}
1418
1419
Mingming Cao617ba132006-10-11 01:20:53 -07001420struct dentry *ext4_get_parent(struct dentry *child)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001422 __u32 ino;
Linus Torvalds26fe5752012-05-10 13:14:12 -07001423 static const struct qstr dotdot = QSTR_INIT("..", 2);
Mingming Cao617ba132006-10-11 01:20:53 -07001424 struct ext4_dir_entry_2 * de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 struct buffer_head *bh;
1426
Theodore Ts'of702ba02008-09-22 15:21:01 -04001427 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001428 if (!bh)
1429 return ERR_PTR(-ENOENT);
1430 ino = le32_to_cpu(de->inode);
1431 brelse(bh);
1432
Mingming Cao617ba132006-10-11 01:20:53 -07001433 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001434 EXT4_ERROR_INODE(child->d_inode,
1435 "bad parent inode number: %u", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001436 return ERR_PTR(-EIO);
Vasily Averina6c15c22007-07-15 23:40:46 -07001437 }
1438
Christoph Hellwig44003722008-08-11 15:49:04 +02001439 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440}
1441
1442#define S_SHIFT 12
Mingming Cao617ba132006-10-11 01:20:53 -07001443static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1444 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1445 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1446 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1447 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1448 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1449 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1450 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451};
1452
Mingming Cao617ba132006-10-11 01:20:53 -07001453static inline void ext4_set_de_type(struct super_block *sb,
1454 struct ext4_dir_entry_2 *de,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001455 umode_t mode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001456 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1457 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458}
1459
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001460/*
1461 * Move count entries from end of map between two memory locations.
1462 * Returns pointer to last entry moved.
1463 */
Mingming Cao617ba132006-10-11 01:20:53 -07001464static struct ext4_dir_entry_2 *
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001465dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1466 unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467{
1468 unsigned rec_len = 0;
1469
1470 while (count--) {
Theodore Ts'o60e66792010-05-17 07:00:00 -04001471 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
Toshiyuki Okajima9aee2282009-06-08 12:41:35 -04001472 (from + (map->offs<<2));
Mingming Cao617ba132006-10-11 01:20:53 -07001473 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474 memcpy (to, de, rec_len);
Mingming Cao617ba132006-10-11 01:20:53 -07001475 ((struct ext4_dir_entry_2 *) to)->rec_len =
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001476 ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001477 de->inode = 0;
1478 map++;
1479 to += rec_len;
1480 }
Mingming Cao617ba132006-10-11 01:20:53 -07001481 return (struct ext4_dir_entry_2 *) (to - rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482}
1483
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001484/*
1485 * Compact each dir entry in the range to the minimal rec_len.
1486 * Returns pointer to last entry in range.
1487 */
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001488static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489{
Mingming Cao617ba132006-10-11 01:20:53 -07001490 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001491 unsigned rec_len = 0;
1492
1493 prev = to = de;
Theodore Ts'o8bad4592009-02-14 21:46:54 -05001494 while ((char*)de < base + blocksize) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001495 next = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001496 if (de->inode && de->name_len) {
Mingming Cao617ba132006-10-11 01:20:53 -07001497 rec_len = EXT4_DIR_REC_LEN(de->name_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 if (de > to)
1499 memmove(to, de, rec_len);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001500 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 prev = to;
Mingming Cao617ba132006-10-11 01:20:53 -07001502 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503 }
1504 de = next;
1505 }
1506 return prev;
1507}
1508
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001509/*
1510 * Split a full leaf block to make room for a new dir entry.
1511 * Allocate a new block, and move entries so that they are approx. equally full.
1512 * Returns pointer to de in block into which the new entry will be inserted.
1513 */
Mingming Cao617ba132006-10-11 01:20:53 -07001514static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001515 struct buffer_head **bh,struct dx_frame *frame,
1516 struct dx_hash_info *hinfo, int *error)
1517{
1518 unsigned blocksize = dir->i_sb->s_blocksize;
1519 unsigned count, continued;
1520 struct buffer_head *bh2;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001521 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 u32 hash2;
1523 struct dx_map_entry *map;
1524 char *data1 = (*bh)->b_data, *data2;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001525 unsigned split, move, size;
Mingming Cao617ba132006-10-11 01:20:53 -07001526 struct ext4_dir_entry_2 *de = NULL, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001527 struct ext4_dir_entry_tail *t;
1528 int csum_size = 0;
Theodore Ts'o59e315b2008-12-06 16:58:39 -05001529 int err = 0, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001531 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1532 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1533 csum_size = sizeof(struct ext4_dir_entry_tail);
1534
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001535 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 if (!(bh2)) {
1537 brelse(*bh);
1538 *bh = NULL;
1539 goto errout;
1540 }
1541
1542 BUFFER_TRACE(*bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001543 err = ext4_journal_get_write_access(handle, *bh);
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001544 if (err)
1545 goto journal_error;
1546
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001547 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001548 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001549 if (err)
1550 goto journal_error;
1551
1552 data2 = bh2->b_data;
1553
1554 /* create map in the end of data2 block */
1555 map = (struct dx_map_entry *) (data2 + blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001556 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001557 blocksize, hinfo, map);
1558 map -= count;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001559 dx_sort_map(map, count);
Eric Sandeenef2b02d2007-09-18 22:46:42 -07001560 /* Split the existing block in the middle, size-wise */
1561 size = 0;
1562 move = 0;
1563 for (i = count-1; i >= 0; i--) {
1564 /* is more than half of this entry in 2nd half of the block? */
1565 if (size + map[i].size/2 > blocksize/2)
1566 break;
1567 size += map[i].size;
1568 move++;
1569 }
1570 /* map index at which we will split */
1571 split = count - move;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572 hash2 = map[split].hash;
1573 continued = hash2 == map[split - 1].hash;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001574 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1575 (unsigned long)dx_get_block(frame->at),
1576 hash2, split, count-split));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577
1578 /* Fancy dance to stay within two buffers */
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001579 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001580 de = dx_pack_dirents(data1, blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001581 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1582 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001583 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001584 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1585 (char *) de2,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001586 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001587 if (csum_size) {
1588 t = EXT4_DIRENT_TAIL(data2, blocksize);
1589 initialize_dirent_tail(t, blocksize);
1590
1591 t = EXT4_DIRENT_TAIL(data1, blocksize);
1592 initialize_dirent_tail(t, blocksize);
1593 }
1594
Mingming Cao617ba132006-10-11 01:20:53 -07001595 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1596 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597
1598 /* Which block gets the new entry? */
1599 if (hinfo->hash >= hash2)
1600 {
1601 swap(*bh, bh2);
1602 de = de2;
1603 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001604 dx_insert_block(frame, hash2 + continued, newblock);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001605 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 if (err)
1607 goto journal_error;
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001608 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609 if (err)
1610 goto journal_error;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001611 brelse(bh2);
1612 dxtrace(dx_show_index("frame", frame->entries));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001613 return de;
Dmitriy Monakhovfedee542007-05-08 00:25:34 -07001614
1615journal_error:
1616 brelse(*bh);
1617 brelse(bh2);
1618 *bh = NULL;
1619 ext4_std_error(dir->i_sb, err);
1620errout:
1621 *error = err;
1622 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624
Tao Ma978fef92012-12-10 14:05:58 -05001625int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1626 struct buffer_head *bh,
1627 void *buf, int buf_size,
1628 const char *name, int namelen,
1629 struct ext4_dir_entry_2 **dest_de)
1630{
1631 struct ext4_dir_entry_2 *de;
1632 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1633 int nlen, rlen;
1634 unsigned int offset = 0;
1635 char *top;
1636
1637 de = (struct ext4_dir_entry_2 *)buf;
1638 top = buf + buf_size - reclen;
1639 while ((char *) de <= top) {
1640 if (ext4_check_dir_entry(dir, NULL, de, bh,
1641 buf, buf_size, offset))
1642 return -EIO;
1643 if (ext4_match(namelen, name, de))
1644 return -EEXIST;
1645 nlen = EXT4_DIR_REC_LEN(de->name_len);
1646 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1647 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1648 break;
1649 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1650 offset += rlen;
1651 }
1652 if ((char *) de > top)
1653 return -ENOSPC;
1654
1655 *dest_de = de;
1656 return 0;
1657}
1658
1659void ext4_insert_dentry(struct inode *inode,
1660 struct ext4_dir_entry_2 *de,
1661 int buf_size,
1662 const char *name, int namelen)
1663{
1664
1665 int nlen, rlen;
1666
1667 nlen = EXT4_DIR_REC_LEN(de->name_len);
1668 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1669 if (de->inode) {
1670 struct ext4_dir_entry_2 *de1 =
1671 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1672 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1673 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1674 de = de1;
1675 }
1676 de->file_type = EXT4_FT_UNKNOWN;
1677 de->inode = cpu_to_le32(inode->i_ino);
1678 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1679 de->name_len = namelen;
1680 memcpy(de->name, name, namelen);
1681}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001682/*
1683 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1684 * it points to a directory entry which is guaranteed to be large
1685 * enough for new directory entry. If de is NULL, then
1686 * add_dirent_to_buf will attempt search the directory block for
1687 * space. It will return -ENOSPC if no space is available, and -EIO
1688 * and -EEXIST if directory entry already exists.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689 */
1690static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
Mingming Cao617ba132006-10-11 01:20:53 -07001691 struct inode *inode, struct ext4_dir_entry_2 *de,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001692 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693{
1694 struct inode *dir = dentry->d_parent->d_inode;
1695 const char *name = dentry->d_name.name;
1696 int namelen = dentry->d_name.len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001697 unsigned int blocksize = dir->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 unsigned short reclen;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001699 int csum_size = 0;
Tao Ma978fef92012-12-10 14:05:58 -05001700 int err;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001701
1702 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1703 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1704 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001705
Mingming Cao617ba132006-10-11 01:20:53 -07001706 reclen = EXT4_DIR_REC_LEN(namelen);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707 if (!de) {
Tao Ma978fef92012-12-10 14:05:58 -05001708 err = ext4_find_dest_de(dir, inode,
1709 bh, bh->b_data, blocksize - csum_size,
1710 name, namelen, &de);
1711 if (err)
1712 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 }
1714 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001715 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07001717 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 return err;
1719 }
1720
1721 /* By now the buffer is marked for journaling */
Tao Ma978fef92012-12-10 14:05:58 -05001722 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1723
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 /*
1725 * XXX shouldn't update any times until successful
1726 * completion of syscall, but too many callers depend
1727 * on this.
1728 *
1729 * XXX similarly, too many callers depend on
Mingming Cao617ba132006-10-11 01:20:53 -07001730 * ext4_new_inode() setting the times, but error
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001731 * recovery deletes the inode, so the worst that can
1732 * happen is that the times are slightly out of date
1733 * and/or different from the directory change time.
1734 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04001735 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07001736 ext4_update_dx_flag(dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 dir->i_version++;
Mingming Cao617ba132006-10-11 01:20:53 -07001738 ext4_mark_inode_dirty(handle, dir);
Frank Mayhar03901312009-01-07 00:06:22 -05001739 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001740 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -07001742 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 return 0;
1744}
1745
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746/*
1747 * This converts a one block unindexed directory to a 3 block indexed
1748 * directory, and adds the dentry to the indexed directory.
1749 */
1750static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1751 struct inode *inode, struct buffer_head *bh)
1752{
1753 struct inode *dir = dentry->d_parent->d_inode;
1754 const char *name = dentry->d_name.name;
1755 int namelen = dentry->d_name.len;
1756 struct buffer_head *bh2;
1757 struct dx_root *root;
1758 struct dx_frame frames[2], *frame;
1759 struct dx_entry *entries;
Mingming Cao617ba132006-10-11 01:20:53 -07001760 struct ext4_dir_entry_2 *de, *de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001761 struct ext4_dir_entry_tail *t;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762 char *data1, *top;
1763 unsigned len;
1764 int retval;
1765 unsigned blocksize;
1766 struct dx_hash_info hinfo;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001767 ext4_lblk_t block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001768 struct fake_dirent *fde;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001769 int csum_size = 0;
1770
1771 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1772 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1773 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001774
1775 blocksize = dir->i_sb->s_blocksize;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001776 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
Mingming Cao617ba132006-10-11 01:20:53 -07001777 retval = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001778 if (retval) {
Mingming Cao617ba132006-10-11 01:20:53 -07001779 ext4_std_error(dir->i_sb, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001780 brelse(bh);
1781 return retval;
1782 }
1783 root = (struct dx_root *) bh->b_data;
1784
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001785 /* The 0th block becomes the root, move the dirents out */
1786 fde = &root->dotdot;
1787 de = (struct ext4_dir_entry_2 *)((char *)fde +
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001788 ext4_rec_len_from_disk(fde->rec_len, blocksize));
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001789 if ((char *) de >= (((char *) root) + blocksize)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04001790 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001791 brelse(bh);
1792 return -EIO;
1793 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001794 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
Theodore Ts'oe6b8bc02009-01-16 11:13:40 -05001795
1796 /* Allocate new block for the 0th block's dirents */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001797 bh2 = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 if (!(bh2)) {
1799 brelse(bh);
1800 return retval;
1801 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001802 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803 data1 = bh2->b_data;
1804
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001805 memcpy (data1, de, len);
Mingming Cao617ba132006-10-11 01:20:53 -07001806 de = (struct ext4_dir_entry_2 *) data1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807 top = data1 + len;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001808 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001809 de = de2;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001810 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1811 (char *) de,
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001812 blocksize);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001813
1814 if (csum_size) {
1815 t = EXT4_DIRENT_TAIL(data1, blocksize);
1816 initialize_dirent_tail(t, blocksize);
1817 }
1818
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 /* Initialize the root; the dot dirents already exist */
Mingming Cao617ba132006-10-11 01:20:53 -07001820 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05001821 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1822 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 memset (&root->info, 0, sizeof(root->info));
1824 root->info.info_length = sizeof(root->info);
Mingming Cao617ba132006-10-11 01:20:53 -07001825 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 entries = root->entries;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001827 dx_set_block(entries, 1);
1828 dx_set_count(entries, 1);
1829 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001830
1831 /* Initialize as for dx_probe */
1832 hinfo.hash_version = root->info.hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04001833 if (hinfo.hash_version <= DX_HASH_TEA)
1834 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
Mingming Cao617ba132006-10-11 01:20:53 -07001835 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1836 ext4fs_dirhash(name, namelen, &hinfo);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001837 frame = frames;
1838 frame->entries = entries;
1839 frame->at = entries;
1840 frame->bh = bh;
1841 bh = bh2;
Allison Henderson6976a6f2011-05-15 00:19:41 -04001842
Darrick J. Wongdbe89442012-04-29 18:39:10 -04001843 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001844 ext4_handle_dirty_dirent_node(handle, dir, bh);
Allison Henderson6976a6f2011-05-15 00:19:41 -04001845
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001847 if (!de) {
1848 /*
1849 * Even if the block split failed, we have to properly write
1850 * out all the changes we did so far. Otherwise we can end up
1851 * with corrupted filesystem.
1852 */
1853 ext4_mark_inode_dirty(handle, dir);
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001854 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001855 return retval;
Jan Kara7ad8e4e2011-05-03 11:05:55 -04001856 }
1857 dx_release(frames);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001858
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001859 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1860 brelse(bh);
1861 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863
1864/*
Mingming Cao617ba132006-10-11 01:20:53 -07001865 * ext4_add_entry()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 *
1867 * adds a file entry to the specified directory, using the same
Mingming Cao617ba132006-10-11 01:20:53 -07001868 * semantics as ext4_find_entry(). It returns NULL if it failed.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 *
1870 * NOTE!! The inode part of 'de' is left at 0 - which means you
1871 * may not sleep between calling this and putting something into
1872 * the entry, as someone else might have used it while you slept.
1873 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001874static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1875 struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876{
1877 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001878 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001879 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001880 struct ext4_dir_entry_tail *t;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001881 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001882 int retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883 int dx_fallback=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884 unsigned blocksize;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001885 ext4_lblk_t block, blocks;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001886 int csum_size = 0;
1887
1888 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1889 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1890 csum_size = sizeof(struct ext4_dir_entry_tail);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001891
1892 sb = dir->i_sb;
1893 blocksize = sb->s_blocksize;
1894 if (!dentry->d_name.len)
1895 return -EINVAL;
Tao Ma3c47d542012-12-10 14:05:59 -05001896
1897 if (ext4_has_inline_data(dir)) {
1898 retval = ext4_try_add_inline_entry(handle, dentry, inode);
1899 if (retval < 0)
1900 return retval;
1901 if (retval == 1) {
1902 retval = 0;
1903 return retval;
1904 }
1905 }
1906
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001907 if (is_dx(dir)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001908 retval = ext4_dx_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909 if (!retval || (retval != ERR_BAD_DX_DIR))
1910 return retval;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001911 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001912 dx_fallback++;
Mingming Cao617ba132006-10-11 01:20:53 -07001913 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001914 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001915 blocks = dir->i_size >> sb->s_blocksize_bits;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001916 for (block = 0; block < blocks; block++) {
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001917 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1918 if (!retval) {
1919 retval = -EIO;
1920 ext4_error(inode->i_sb,
1921 "Directory hole detected on inode %lu\n",
1922 inode->i_ino);
1923 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001924 return retval;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001925 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001926 if (!buffer_verified(bh) &&
1927 !ext4_dirent_csum_verify(dir,
1928 (struct ext4_dir_entry *)bh->b_data))
1929 return -EIO;
1930 set_buffer_verified(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001931 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001932 if (retval != -ENOSPC) {
1933 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001934 return retval;
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001935 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001937 if (blocks == 1 && !dx_fallback &&
Theodore Ts'o1e424a3482009-11-08 15:45:44 -05001938 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1939 return make_indexed_dir(handle, dentry, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001940 brelse(bh);
1941 }
Mingming Cao617ba132006-10-11 01:20:53 -07001942 bh = ext4_append(handle, dir, &block, &retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001943 if (!bh)
1944 return retval;
Mingming Cao617ba132006-10-11 01:20:53 -07001945 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946 de->inode = 0;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001947 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1948
1949 if (csum_size) {
1950 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1951 initialize_dirent_tail(t, blocksize);
1952 }
1953
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001954 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1955 brelse(bh);
Frank Mayhar14ece102010-05-17 08:00:00 -04001956 if (retval == 0)
1957 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05001958 return retval;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001959}
1960
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961/*
1962 * Returns 0 for success, or a negative error value
1963 */
Mingming Cao617ba132006-10-11 01:20:53 -07001964static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965 struct inode *inode)
1966{
1967 struct dx_frame frames[2], *frame;
1968 struct dx_entry *entries, *at;
1969 struct dx_hash_info hinfo;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001970 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 struct inode *dir = dentry->d_parent->d_inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001972 struct super_block *sb = dir->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001973 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001974 int err;
1975
Theodore Ts'of702ba02008-09-22 15:21:01 -04001976 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001977 if (!frame)
1978 return err;
1979 entries = frame->entries;
1980 at = frame->at;
1981
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001982 if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1983 if (!err) {
1984 err = -EIO;
1985 ext4_error(dir->i_sb,
1986 "Directory hole detected on inode %lu\n",
1987 dir->i_ino);
1988 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 goto cleanup;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04001990 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991
Darrick J. Wongb0336e82012-04-29 18:41:10 -04001992 if (!buffer_verified(bh) &&
1993 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1994 goto journal_error;
1995 set_buffer_verified(bh);
1996
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 BUFFER_TRACE(bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001998 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001999 if (err)
2000 goto journal_error;
2001
2002 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
Theodore Ts'o2de770a2009-11-23 07:25:49 -05002003 if (err != -ENOSPC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002004 goto cleanup;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002005
2006 /* Block full, should compress but for now just split */
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002007 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002008 dx_get_count(entries), dx_get_limit(entries)));
2009 /* Need to split index? */
2010 if (dx_get_count(entries) == dx_get_limit(entries)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002011 ext4_lblk_t newblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 unsigned icount = dx_get_count(entries);
2013 int levels = frame - frames;
2014 struct dx_entry *entries2;
2015 struct dx_node *node2;
2016 struct buffer_head *bh2;
2017
2018 if (levels && (dx_get_count(frames->entries) ==
2019 dx_get_limit(frames->entries))) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002020 ext4_warning(sb, "Directory index full!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 err = -ENOSPC;
2022 goto cleanup;
2023 }
Mingming Cao617ba132006-10-11 01:20:53 -07002024 bh2 = ext4_append (handle, dir, &newblock, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 if (!(bh2))
2026 goto cleanup;
2027 node2 = (struct dx_node *)(bh2->b_data);
2028 entries2 = node2->entries;
Andreas Schlick1f7bebb2009-09-10 23:16:07 -04002029 memset(&node2->fake, 0, sizeof(struct fake_dirent));
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002030 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2031 sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032 BUFFER_TRACE(frame->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002033 err = ext4_journal_get_write_access(handle, frame->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002034 if (err)
2035 goto journal_error;
2036 if (levels) {
2037 unsigned icount1 = icount/2, icount2 = icount - icount1;
2038 unsigned hash2 = dx_get_hash(entries + icount1);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002039 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2040 icount1, icount2));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041
2042 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
Mingming Cao617ba132006-10-11 01:20:53 -07002043 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002044 frames[0].bh);
2045 if (err)
2046 goto journal_error;
2047
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002048 memcpy((char *) entries2, (char *) (entries + icount1),
2049 icount2 * sizeof(struct dx_entry));
2050 dx_set_count(entries, icount1);
2051 dx_set_count(entries2, icount2);
2052 dx_set_limit(entries2, dx_node_limit(dir));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002053
2054 /* Which index block gets the new entry? */
2055 if (at - entries >= icount1) {
2056 frame->at = at = at - entries - icount1 + entries2;
2057 frame->entries = entries = entries2;
2058 swap(frame->bh, bh2);
2059 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002060 dx_insert_block(frames + 0, hash2, newblock);
2061 dxtrace(dx_show_index("node", frames[1].entries));
2062 dxtrace(dx_show_index("node",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002063 ((struct dx_node *) bh2->b_data)->entries));
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002064 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065 if (err)
2066 goto journal_error;
2067 brelse (bh2);
2068 } else {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002069 dxtrace(printk(KERN_DEBUG
2070 "Creating second level index...\n"));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071 memcpy((char *) entries2, (char *) entries,
2072 icount * sizeof(struct dx_entry));
2073 dx_set_limit(entries2, dx_node_limit(dir));
2074
2075 /* Set up root */
2076 dx_set_count(entries, 1);
2077 dx_set_block(entries + 0, newblock);
2078 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2079
2080 /* Add new access path frame */
2081 frame = frames + 1;
2082 frame->at = at = at - entries + entries2;
2083 frame->entries = entries = entries2;
2084 frame->bh = bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07002085 err = ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086 frame->bh);
2087 if (err)
2088 goto journal_error;
2089 }
Darrick J. Wongdbe89442012-04-29 18:39:10 -04002090 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05002091 if (err) {
2092 ext4_std_error(inode->i_sb, err);
2093 goto cleanup;
2094 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 }
2096 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2097 if (!de)
2098 goto cleanup;
2099 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002100 goto cleanup;
2101
2102journal_error:
Mingming Cao617ba132006-10-11 01:20:53 -07002103 ext4_std_error(dir->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104cleanup:
2105 if (bh)
2106 brelse(bh);
2107 dx_release(frames);
2108 return err;
2109}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110
2111/*
Tao Ma05019a92012-12-10 14:06:00 -05002112 * ext4_generic_delete_entry deletes a directory entry by merging it
2113 * with the previous entry
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002114 */
Tao Ma05019a92012-12-10 14:06:00 -05002115int ext4_generic_delete_entry(handle_t *handle,
2116 struct inode *dir,
2117 struct ext4_dir_entry_2 *de_del,
2118 struct buffer_head *bh,
2119 void *entry_buf,
2120 int buf_size,
2121 int csum_size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002123 struct ext4_dir_entry_2 *de, *pde;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002124 unsigned int blocksize = dir->i_sb->s_blocksize;
Tao Ma05019a92012-12-10 14:06:00 -05002125 int i;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002126
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 i = 0;
2128 pde = NULL;
Tao Ma05019a92012-12-10 14:06:00 -05002129 de = (struct ext4_dir_entry_2 *)entry_buf;
2130 while (i < buf_size - csum_size) {
Tao Ma226ba972012-12-10 14:05:58 -05002131 if (ext4_check_dir_entry(dir, NULL, de, bh,
2132 bh->b_data, bh->b_size, i))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133 return -EIO;
2134 if (de == de_del) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 if (pde)
Jan Karaa72d7f82008-01-28 23:58:27 -05002136 pde->rec_len = ext4_rec_len_to_disk(
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002137 ext4_rec_len_from_disk(pde->rec_len,
2138 blocksize) +
2139 ext4_rec_len_from_disk(de->rec_len,
2140 blocksize),
2141 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002142 else
2143 de->inode = 0;
2144 dir->i_version++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002145 return 0;
2146 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002147 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 pde = de;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002149 de = ext4_next_entry(de, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002150 }
2151 return -ENOENT;
2152}
2153
Tao Ma05019a92012-12-10 14:06:00 -05002154static int ext4_delete_entry(handle_t *handle,
2155 struct inode *dir,
2156 struct ext4_dir_entry_2 *de_del,
2157 struct buffer_head *bh)
2158{
2159 int err, csum_size = 0;
2160
Tao Ma9f40fe52012-12-10 14:06:00 -05002161 if (ext4_has_inline_data(dir)) {
2162 int has_inline_data = 1;
2163 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2164 &has_inline_data);
2165 if (has_inline_data)
2166 return err;
2167 }
2168
Tao Ma05019a92012-12-10 14:06:00 -05002169 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2170 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2171 csum_size = sizeof(struct ext4_dir_entry_tail);
2172
2173 BUFFER_TRACE(bh, "get_write_access");
2174 err = ext4_journal_get_write_access(handle, bh);
2175 if (unlikely(err))
2176 goto out;
2177
2178 err = ext4_generic_delete_entry(handle, dir, de_del,
2179 bh, bh->b_data,
2180 dir->i_sb->s_blocksize, csum_size);
2181 if (err)
2182 goto out;
2183
2184 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2185 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2186 if (unlikely(err))
2187 goto out;
2188
2189 return 0;
2190out:
2191 if (err != -ENOENT)
2192 ext4_std_error(dir->i_sb, err);
2193 return err;
2194}
2195
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002196/*
2197 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2198 * since this indicates that nlinks count was previously 1.
2199 */
2200static void ext4_inc_count(handle_t *handle, struct inode *inode)
2201{
2202 inc_nlink(inode);
2203 if (is_dx(inode) && inode->i_nlink > 1) {
2204 /* limit is 16-bit i_links_count */
2205 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002206 set_nlink(inode, 1);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002207 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2208 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2209 }
2210 }
2211}
2212
2213/*
2214 * If a directory had nlink == 1, then we should let it be 1. This indicates
2215 * directory has >EXT4_LINK_MAX subdirs.
2216 */
2217static void ext4_dec_count(handle_t *handle, struct inode *inode)
2218{
Andreas Dilger909a4cf2011-10-26 03:22:31 -04002219 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2220 drop_nlink(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002221}
2222
2223
Mingming Cao617ba132006-10-11 01:20:53 -07002224static int ext4_add_nondir(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 struct dentry *dentry, struct inode *inode)
2226{
Mingming Cao617ba132006-10-11 01:20:53 -07002227 int err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002228 if (!err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002229 ext4_mark_inode_dirty(handle, inode);
Al Viro6b38e842008-12-30 02:03:31 -05002230 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002231 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 return 0;
2233 }
Eric Sandeen731b9a52007-02-10 01:46:16 -08002234 drop_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002235 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236 iput(inode);
2237 return err;
2238}
2239
2240/*
2241 * By the time this is called, we already have created
2242 * the directory cache entry for the new file, but it
2243 * is so far negative - it has no inode.
2244 *
2245 * If the create succeeds, we fill in the inode information
2246 * with d_instantiate().
2247 */
Al Viro4acdaf22011-07-26 01:42:34 -04002248static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002249 bool excl)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002250{
2251 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002252 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 int err, retries = 0;
2254
Christoph Hellwig871a2932010-03-03 09:05:07 -05002255 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002256
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002257retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002258 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2259 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002260 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002261 if (IS_ERR(handle))
2262 return PTR_ERR(handle);
2263
2264 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002265 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266
Dmitry Monakhov5cb81da2011-10-29 09:05:00 -04002267 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002268 err = PTR_ERR(inode);
2269 if (!IS_ERR(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002270 inode->i_op = &ext4_file_inode_operations;
2271 inode->i_fop = &ext4_file_operations;
2272 ext4_set_aops(inode);
2273 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 }
Mingming Cao617ba132006-10-11 01:20:53 -07002275 ext4_journal_stop(handle);
2276 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002277 goto retry;
2278 return err;
2279}
2280
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002281static int ext4_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04002282 umode_t mode, dev_t rdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283{
2284 handle_t *handle;
2285 struct inode *inode;
2286 int err, retries = 0;
2287
2288 if (!new_valid_dev(rdev))
2289 return -EINVAL;
2290
Christoph Hellwig871a2932010-03-03 09:05:07 -05002291 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002292
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002294 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2295 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002296 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002297 if (IS_ERR(handle))
2298 return PTR_ERR(handle);
2299
2300 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002301 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002302
Dmitry Monakhov5cb81da2011-10-29 09:05:00 -04002303 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002304 err = PTR_ERR(inode);
2305 if (!IS_ERR(inode)) {
2306 init_special_inode(inode, inode->i_mode, rdev);
Mingming Cao617ba132006-10-11 01:20:53 -07002307 inode->i_op = &ext4_special_inode_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07002308 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 }
Mingming Cao617ba132006-10-11 01:20:53 -07002310 ext4_journal_stop(handle);
2311 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002312 goto retry;
2313 return err;
2314}
2315
Tao Maa774f9c2012-12-10 14:05:57 -05002316struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2317 struct ext4_dir_entry_2 *de,
2318 int blocksize, int csum_size,
2319 unsigned int parent_ino, int dotdot_real_len)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002320{
Tao Maa774f9c2012-12-10 14:05:57 -05002321 de->inode = cpu_to_le32(inode->i_ino);
2322 de->name_len = 1;
2323 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2324 blocksize);
2325 strcpy(de->name, ".");
2326 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2327
2328 de = ext4_next_entry(de, blocksize);
2329 de->inode = cpu_to_le32(parent_ino);
2330 de->name_len = 2;
2331 if (!dotdot_real_len)
2332 de->rec_len = ext4_rec_len_to_disk(blocksize -
2333 (csum_size + EXT4_DIR_REC_LEN(1)),
2334 blocksize);
2335 else
2336 de->rec_len = ext4_rec_len_to_disk(
2337 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2338 strcpy(de->name, "..");
2339 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2340
2341 return ext4_next_entry(de, blocksize);
2342}
2343
2344static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2345 struct inode *inode)
2346{
Namhyung Kimdabd9912011-01-10 12:11:16 -05002347 struct buffer_head *dir_block = NULL;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002348 struct ext4_dir_entry_2 *de;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002349 struct ext4_dir_entry_tail *t;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002350 unsigned int blocksize = dir->i_sb->s_blocksize;
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002351 int csum_size = 0;
Tao Maa774f9c2012-12-10 14:05:57 -05002352 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002354 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2355 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2356 csum_size = sizeof(struct ext4_dir_entry_tail);
2357
Tao Ma3c47d542012-12-10 14:05:59 -05002358 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2359 err = ext4_try_create_inline_dir(handle, dir, inode);
2360 if (err < 0 && err != -ENOSPC)
2361 goto out;
2362 if (!err)
2363 goto out;
2364 }
2365
Tao Maa774f9c2012-12-10 14:05:57 -05002366 inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2367 dir_block = ext4_bread(handle, inode, 0, 1, &err);
2368 if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2369 if (!err) {
2370 err = -EIO;
2371 ext4_error(inode->i_sb,
2372 "Directory hole detected on inode %lu\n",
2373 inode->i_ino);
2374 }
2375 goto out;
2376 }
2377 BUFFER_TRACE(dir_block, "get_write_access");
2378 err = ext4_journal_get_write_access(handle, dir_block);
2379 if (err)
2380 goto out;
2381 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2382 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2383 set_nlink(inode, 2);
2384 if (csum_size) {
2385 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2386 initialize_dirent_tail(t, blocksize);
2387 }
2388
2389 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2390 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2391 if (err)
2392 goto out;
2393 set_buffer_verified(dir_block);
2394out:
2395 brelse(dir_block);
2396 return err;
2397}
2398
2399static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2400{
2401 handle_t *handle;
2402 struct inode *inode;
2403 int err, retries = 0;
2404
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002405 if (EXT4_DIR_LINK_MAX(dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406 return -EMLINK;
2407
Christoph Hellwig871a2932010-03-03 09:05:07 -05002408 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002409
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002411 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2412 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002413 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 if (IS_ERR(handle))
2415 return PTR_ERR(handle);
2416
2417 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002418 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002419
Andreas Dilger11013912009-06-13 11:45:35 -04002420 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
Dmitry Monakhov5cb81da2011-10-29 09:05:00 -04002421 &dentry->d_name, 0, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422 err = PTR_ERR(inode);
2423 if (IS_ERR(inode))
2424 goto out_stop;
2425
Mingming Cao617ba132006-10-11 01:20:53 -07002426 inode->i_op = &ext4_dir_inode_operations;
2427 inode->i_fop = &ext4_dir_operations;
Tao Maa774f9c2012-12-10 14:05:57 -05002428 err = ext4_init_new_dir(handle, dir, inode);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002429 if (err)
2430 goto out_clear_inode;
Namhyung Kimdabd9912011-01-10 12:11:16 -05002431 err = ext4_mark_inode_dirty(handle, inode);
2432 if (!err)
2433 err = ext4_add_entry(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002434 if (err) {
Aneesh Kumar K.V4cdeed82008-02-22 06:17:31 -05002435out_clear_inode:
2436 clear_nlink(inode);
Al Viro6b38e842008-12-30 02:03:31 -05002437 unlock_new_inode(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002438 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002439 iput(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440 goto out_stop;
2441 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002442 ext4_inc_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002443 ext4_update_dx_flag(dir);
Namhyung Kimdabd9912011-01-10 12:11:16 -05002444 err = ext4_mark_inode_dirty(handle, dir);
2445 if (err)
2446 goto out_clear_inode;
Al Viro6b38e842008-12-30 02:03:31 -05002447 unlock_new_inode(inode);
Al Viro8fc37ec2012-07-19 09:18:15 +04002448 d_instantiate(dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002449out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07002450 ext4_journal_stop(handle);
2451 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002452 goto retry;
2453 return err;
2454}
2455
2456/*
2457 * routine to check that the specified directory is empty (for rmdir)
2458 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002459static int empty_dir(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002460{
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002461 unsigned int offset;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002462 struct buffer_head *bh;
2463 struct ext4_dir_entry_2 *de, *de1;
2464 struct super_block *sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002465 int err = 0;
2466
Tao Ma61f86632012-12-10 14:06:01 -05002467 if (ext4_has_inline_data(inode)) {
2468 int has_inline_data = 1;
2469
2470 err = empty_inline_dir(inode, &has_inline_data);
2471 if (has_inline_data)
2472 return err;
2473 }
2474
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475 sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002476 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002477 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478 if (err)
Theodore Ts'o24676da2010-05-16 21:00:00 -04002479 EXT4_ERROR_INODE(inode,
2480 "error %d reading directory lblock 0", err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 else
Eric Sandeen12062dd2010-02-15 14:19:27 -05002482 ext4_warning(inode->i_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483 "bad directory (dir #%lu) - no data block",
2484 inode->i_ino);
2485 return 1;
2486 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002487 if (!buffer_verified(bh) &&
2488 !ext4_dirent_csum_verify(inode,
2489 (struct ext4_dir_entry *)bh->b_data)) {
2490 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2491 "lblock 0");
2492 return -EIO;
2493 }
2494 set_buffer_verified(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002495 de = (struct ext4_dir_entry_2 *) bh->b_data;
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002496 de1 = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497 if (le32_to_cpu(de->inode) != inode->i_ino ||
2498 !le32_to_cpu(de1->inode) ||
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002499 strcmp(".", de->name) ||
2500 strcmp("..", de1->name)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002501 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002502 "bad directory (dir #%lu) - no `.' or `..'",
2503 inode->i_ino);
2504 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002505 return 1;
2506 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002507 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2508 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2509 de = ext4_next_entry(de1, sb->s_blocksize);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002510 while (offset < inode->i_size) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002511 if (!bh ||
Theodore Ts'o24676da2010-05-16 21:00:00 -04002512 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2513 unsigned int lblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002514 err = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002515 brelse(bh);
Theodore Ts'o24676da2010-05-16 21:00:00 -04002516 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2517 bh = ext4_bread(NULL, inode, lblock, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002518 if (!bh) {
2519 if (err)
Theodore Ts'o24676da2010-05-16 21:00:00 -04002520 EXT4_ERROR_INODE(inode,
2521 "error %d reading directory "
2522 "lblock %u", err, lblock);
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04002523 else
2524 ext4_warning(inode->i_sb,
2525 "bad directory (dir #%lu) - no data block",
2526 inode->i_ino);
2527
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002528 offset += sb->s_blocksize;
2529 continue;
2530 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04002531 if (!buffer_verified(bh) &&
2532 !ext4_dirent_csum_verify(inode,
2533 (struct ext4_dir_entry *)bh->b_data)) {
2534 EXT4_ERROR_INODE(inode, "checksum error "
2535 "reading directory lblock 0");
2536 return -EIO;
2537 }
2538 set_buffer_verified(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07002539 de = (struct ext4_dir_entry_2 *) bh->b_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002540 }
Tao Ma226ba972012-12-10 14:05:58 -05002541 if (ext4_check_dir_entry(inode, NULL, de, bh,
2542 bh->b_data, bh->b_size, offset)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002543 de = (struct ext4_dir_entry_2 *)(bh->b_data +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002544 sb->s_blocksize);
2545 offset = (offset | (sb->s_blocksize - 1)) + 1;
2546 continue;
2547 }
2548 if (le32_to_cpu(de->inode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002549 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002550 return 0;
2551 }
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002552 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2553 de = ext4_next_entry(de, sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002554 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002555 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002556 return 1;
2557}
2558
Mingming Cao617ba132006-10-11 01:20:53 -07002559/* ext4_orphan_add() links an unlinked or truncated inode into a list of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560 * such inodes, starting at the superblock, in case we crash before the
2561 * file is closed/deleted, or in case the inode truncate spans multiple
2562 * transactions and the last transaction is not recovered after a crash.
2563 *
2564 * At filesystem recovery time, we walk this list deleting unlinked
Mingming Cao617ba132006-10-11 01:20:53 -07002565 * inodes and truncating linked inodes in ext4_orphan_cleanup().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002566 */
Mingming Cao617ba132006-10-11 01:20:53 -07002567int ext4_orphan_add(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002568{
2569 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002570 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002571 int err = 0, rc;
2572
Anatol Pomozovc9b925302012-09-18 13:38:59 -04002573 if (!EXT4_SB(sb)->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05002574 return 0;
2575
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002576 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
Mingming Cao617ba132006-10-11 01:20:53 -07002577 if (!list_empty(&EXT4_I(inode)->i_orphan))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 goto out_unlock;
2579
Lukas Czernerafb86172011-07-11 18:47:04 -04002580 /*
2581 * Orphan handling is only valid for files with data blocks
2582 * being truncated, or files being unlinked. Note that we either
2583 * hold i_mutex, or the inode can not be referenced from outside,
2584 * so i_nlink should not be bumped due to race
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002585 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002586 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2587 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588
Mingming Cao617ba132006-10-11 01:20:53 -07002589 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2590 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002591 if (err)
2592 goto out_unlock;
2593
Mingming Cao617ba132006-10-11 01:20:53 -07002594 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002595 if (err)
2596 goto out_unlock;
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002597 /*
2598 * Due to previous errors inode may be already a part of on-disk
2599 * orphan list. If so skip on-disk list modification.
2600 */
2601 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2602 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2603 goto mem_insert;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002604
2605 /* Insert this inode at the head of the on-disk orphan list... */
Mingming Cao617ba132006-10-11 01:20:53 -07002606 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2607 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002608 err = ext4_handle_dirty_super(handle, sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002609 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610 if (!err)
2611 err = rc;
2612
2613 /* Only add to the head of the in-memory list if all the
2614 * previous operations succeeded. If the orphan_add is going to
2615 * fail (possibly taking the journal offline), we can't risk
2616 * leaving the inode on the orphan list: stray orphan-list
2617 * entries can cause panics at unmount time.
2618 *
2619 * This is safe: on error we're going to ignore the orphan list
2620 * anyway on the next recovery. */
Dmitry Monakhov6e3617e2010-03-01 23:29:39 -05002621mem_insert:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07002623 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002624
2625 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2626 jbd_debug(4, "orphan inode %lu will point to %d\n",
2627 inode->i_ino, NEXT_ORPHAN(inode));
2628out_unlock:
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002629 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
Mingming Cao617ba132006-10-11 01:20:53 -07002630 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002631 return err;
2632}
2633
2634/*
Mingming Cao617ba132006-10-11 01:20:53 -07002635 * ext4_orphan_del() removes an unlinked or truncated inode from the list
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002636 * of such inodes stored on disk, because it is finally being cleaned up.
2637 */
Mingming Cao617ba132006-10-11 01:20:53 -07002638int ext4_orphan_del(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002639{
2640 struct list_head *prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002641 struct ext4_inode_info *ei = EXT4_I(inode);
2642 struct ext4_sb_info *sbi;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002643 __u32 ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002644 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002645 int err = 0;
2646
Anatol Pomozovc9b925302012-09-18 13:38:59 -04002647 if (!EXT4_SB(inode->i_sb)->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05002648 return 0;
2649
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002650 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2651 if (list_empty(&ei->i_orphan))
2652 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002653
2654 ino_next = NEXT_ORPHAN(inode);
2655 prev = ei->i_orphan.prev;
Mingming Cao617ba132006-10-11 01:20:53 -07002656 sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002657
2658 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2659
2660 list_del_init(&ei->i_orphan);
2661
2662 /* If we're on an error path, we may not have a valid
2663 * transaction handle with which to update the orphan list on
2664 * disk, but we still need to remove the inode from the linked
2665 * list in memory. */
Anatol Pomozovc9b925302012-09-18 13:38:59 -04002666 if (!handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667 goto out;
2668
Mingming Cao617ba132006-10-11 01:20:53 -07002669 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002670 if (err)
2671 goto out_err;
2672
2673 if (prev == &sbi->s_orphan) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002674 jbd_debug(4, "superblock will point to %u\n", ino_next);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07002676 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677 if (err)
2678 goto out_brelse;
2679 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04002680 err = ext4_handle_dirty_super(handle, inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002682 struct ext4_iloc iloc2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002683 struct inode *i_prev =
Mingming Cao617ba132006-10-11 01:20:53 -07002684 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002686 jbd_debug(4, "orphan inode %lu will point to %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002687 i_prev->i_ino, ino_next);
Mingming Cao617ba132006-10-11 01:20:53 -07002688 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689 if (err)
2690 goto out_brelse;
2691 NEXT_ORPHAN(i_prev) = ino_next;
Mingming Cao617ba132006-10-11 01:20:53 -07002692 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 }
2694 if (err)
2695 goto out_brelse;
2696 NEXT_ORPHAN(inode) = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002697 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002698
2699out_err:
Mingming Cao617ba132006-10-11 01:20:53 -07002700 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701out:
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002702 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002703 return err;
2704
2705out_brelse:
2706 brelse(iloc.bh);
2707 goto out_err;
2708}
2709
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002710static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002711{
2712 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002713 struct inode *inode;
2714 struct buffer_head *bh;
2715 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002716 handle_t *handle;
2717
2718 /* Initialize quotas before so that eventual writes go in
2719 * separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002720 dquot_initialize(dir);
2721 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002722
Mingming Cao617ba132006-10-11 01:20:53 -07002723 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002724 if (IS_ERR(handle))
2725 return PTR_ERR(handle);
2726
2727 retval = -ENOENT;
Theodore Ts'of702ba02008-09-22 15:21:01 -04002728 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 if (!bh)
2730 goto end_rmdir;
2731
2732 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002733 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734
2735 inode = dentry->d_inode;
2736
2737 retval = -EIO;
2738 if (le32_to_cpu(de->inode) != inode->i_ino)
2739 goto end_rmdir;
2740
2741 retval = -ENOTEMPTY;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002742 if (!empty_dir(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743 goto end_rmdir;
2744
Mingming Cao617ba132006-10-11 01:20:53 -07002745 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002746 if (retval)
2747 goto end_rmdir;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002748 if (!EXT4_DIR_LINK_EMPTY(inode))
Eric Sandeen12062dd2010-02-15 14:19:27 -05002749 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002750 "empty directory has too many links (%d)",
2751 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 inode->i_version++;
2753 clear_nlink(inode);
2754 /* There's no need to set i_disksize: the fact that i_nlink is
2755 * zero will ensure that the right thing happens during any
2756 * recovery. */
2757 inode->i_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002758 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002759 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002760 ext4_mark_inode_dirty(handle, inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002761 ext4_dec_count(handle, dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002762 ext4_update_dx_flag(dir);
2763 ext4_mark_inode_dirty(handle, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764
2765end_rmdir:
Mingming Cao617ba132006-10-11 01:20:53 -07002766 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002767 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002768 return retval;
2769}
2770
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002771static int ext4_unlink(struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772{
2773 int retval;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002774 struct inode *inode;
2775 struct buffer_head *bh;
2776 struct ext4_dir_entry_2 *de;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 handle_t *handle;
2778
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002779 trace_ext4_unlink_enter(dir, dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 /* Initialize quotas before so that eventual writes go
2781 * in separate transaction */
Christoph Hellwig871a2932010-03-03 09:05:07 -05002782 dquot_initialize(dir);
2783 dquot_initialize(dentry->d_inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002784
Mingming Cao617ba132006-10-11 01:20:53 -07002785 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786 if (IS_ERR(handle))
2787 return PTR_ERR(handle);
2788
2789 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002790 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002791
2792 retval = -ENOENT;
Theodore Ts'of702ba02008-09-22 15:21:01 -04002793 bh = ext4_find_entry(dir, &dentry->d_name, &de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002794 if (!bh)
2795 goto end_unlink;
2796
2797 inode = dentry->d_inode;
2798
2799 retval = -EIO;
2800 if (le32_to_cpu(de->inode) != inode->i_ino)
2801 goto end_unlink;
2802
2803 if (!inode->i_nlink) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002804 ext4_warning(inode->i_sb,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002805 "Deleting nonexistent file (%lu), %d",
2806 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002807 set_nlink(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002808 }
Mingming Cao617ba132006-10-11 01:20:53 -07002809 retval = ext4_delete_entry(handle, dir, de, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002810 if (retval)
2811 goto end_unlink;
Kalpak Shahef7f3832007-07-18 09:15:20 -04002812 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
Mingming Cao617ba132006-10-11 01:20:53 -07002813 ext4_update_dx_flag(dir);
2814 ext4_mark_inode_dirty(handle, dir);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002815 drop_nlink(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816 if (!inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07002817 ext4_orphan_add(handle, inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002818 inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002819 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002820 retval = 0;
2821
2822end_unlink:
Mingming Cao617ba132006-10-11 01:20:53 -07002823 ext4_journal_stop(handle);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002824 brelse(bh);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002825 trace_ext4_unlink_exit(dentry, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002826 return retval;
2827}
2828
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002829static int ext4_symlink(struct inode *dir,
2830 struct dentry *dentry, const char *symname)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002831{
2832 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002833 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002834 int l, err, retries = 0;
Jan Karadf5e6222011-05-03 11:12:58 -04002835 int credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002836
2837 l = strlen(symname)+1;
2838 if (l > dir->i_sb->s_blocksize)
2839 return -ENAMETOOLONG;
2840
Christoph Hellwig871a2932010-03-03 09:05:07 -05002841 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002842
Jan Karadf5e6222011-05-03 11:12:58 -04002843 if (l > EXT4_N_BLOCKS * 4) {
2844 /*
2845 * For non-fast symlinks, we just allocate inode and put it on
2846 * orphan list in the first transaction => we need bitmap,
Eric Sandeen8c208712011-08-11 09:54:31 -05002847 * group descriptor, sb, inode block, quota blocks, and
2848 * possibly selinux xattr blocks.
Jan Karadf5e6222011-05-03 11:12:58 -04002849 */
Eric Sandeen8c208712011-08-11 09:54:31 -05002850 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2851 EXT4_XATTR_TRANS_BLOCKS;
Jan Karadf5e6222011-05-03 11:12:58 -04002852 } else {
2853 /*
2854 * Fast symlink. We have to add entry to directory
2855 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2856 * allocate new inode (bitmap, group descriptor, inode block,
2857 * quota blocks, sb is already counted in previous macros).
2858 */
2859 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2860 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2861 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2862 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002863retry:
Jan Karadf5e6222011-05-03 11:12:58 -04002864 handle = ext4_journal_start(dir, credits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002865 if (IS_ERR(handle))
2866 return PTR_ERR(handle);
2867
2868 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002869 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002870
Andreas Dilger11013912009-06-13 11:45:35 -04002871 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
Dmitry Monakhov5cb81da2011-10-29 09:05:00 -04002872 &dentry->d_name, 0, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 err = PTR_ERR(inode);
2874 if (IS_ERR(inode))
2875 goto out_stop;
2876
Jan Karadf5e6222011-05-03 11:12:58 -04002877 if (l > EXT4_N_BLOCKS * 4) {
Mingming Cao617ba132006-10-11 01:20:53 -07002878 inode->i_op = &ext4_symlink_inode_operations;
2879 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002880 /*
Jan Karadf5e6222011-05-03 11:12:58 -04002881 * We cannot call page_symlink() with transaction started
2882 * because it calls into ext4_write_begin() which can wait
2883 * for transaction commit if we are running out of space
2884 * and thus we deadlock. So we have to stop transaction now
2885 * and restart it when symlink contents is written.
2886 *
2887 * To keep fs consistent in case of crash, we have to put inode
2888 * to orphan list in the mean time.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889 */
Jan Karadf5e6222011-05-03 11:12:58 -04002890 drop_nlink(inode);
2891 err = ext4_orphan_add(handle, inode);
2892 ext4_journal_stop(handle);
2893 if (err)
2894 goto err_drop_inode;
Nick Piggin54566b22009-01-04 12:00:53 -08002895 err = __page_symlink(inode, symname, l, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04002896 if (err)
2897 goto err_drop_inode;
2898 /*
2899 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2900 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2901 */
2902 handle = ext4_journal_start(dir,
2903 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2904 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2905 if (IS_ERR(handle)) {
2906 err = PTR_ERR(handle);
2907 goto err_drop_inode;
2908 }
Al Viro0ce8c0102012-01-08 19:50:23 -05002909 set_nlink(inode, 1);
Jan Karadf5e6222011-05-03 11:12:58 -04002910 err = ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 if (err) {
Jan Karadf5e6222011-05-03 11:12:58 -04002912 ext4_journal_stop(handle);
Theodore Ts'o825f1482008-02-15 15:00:38 -05002913 clear_nlink(inode);
Jan Karadf5e6222011-05-03 11:12:58 -04002914 goto err_drop_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 }
2916 } else {
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04002917 /* clear the extent format for fast symlink */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002918 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
Mingming Cao617ba132006-10-11 01:20:53 -07002919 inode->i_op = &ext4_fast_symlink_inode_operations;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002920 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002921 inode->i_size = l-1;
2922 }
Mingming Cao617ba132006-10-11 01:20:53 -07002923 EXT4_I(inode)->i_disksize = inode->i_size;
2924 err = ext4_add_nondir(handle, dentry, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925out_stop:
Mingming Cao617ba132006-10-11 01:20:53 -07002926 ext4_journal_stop(handle);
2927 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002928 goto retry;
2929 return err;
Jan Karadf5e6222011-05-03 11:12:58 -04002930err_drop_inode:
2931 unlock_new_inode(inode);
2932 iput(inode);
2933 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002934}
2935
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002936static int ext4_link(struct dentry *old_dentry,
2937 struct inode *dir, struct dentry *dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002938{
2939 handle_t *handle;
2940 struct inode *inode = old_dentry->d_inode;
2941 int err, retries = 0;
2942
Theodore Ts'ob05ab1d2009-08-29 21:08:08 -04002943 if (inode->i_nlink >= EXT4_LINK_MAX)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 return -EMLINK;
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002945
Christoph Hellwig871a2932010-03-03 09:05:07 -05002946 dquot_initialize(dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002947
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002948retry:
Mingming Cao617ba132006-10-11 01:20:53 -07002949 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2950 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 if (IS_ERR(handle))
2952 return PTR_ERR(handle);
2953
2954 if (IS_DIRSYNC(dir))
Frank Mayhar03901312009-01-07 00:06:22 -05002955 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002956
Kalpak Shahef7f3832007-07-18 09:15:20 -04002957 inode->i_ctime = ext4_current_time(inode);
Andreas Dilgerf8628a12007-07-18 08:38:01 -04002958 ext4_inc_count(handle, inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002959 ihold(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960
Al Viro6b38e842008-12-30 02:03:31 -05002961 err = ext4_add_entry(handle, dentry, inode);
2962 if (!err) {
2963 ext4_mark_inode_dirty(handle, inode);
2964 d_instantiate(dentry, inode);
2965 } else {
2966 drop_nlink(inode);
2967 iput(inode);
2968 }
Mingming Cao617ba132006-10-11 01:20:53 -07002969 ext4_journal_stop(handle);
2970 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002971 goto retry;
2972 return err;
2973}
2974
Wei Yongjun3d0518f2009-02-14 23:01:36 -05002975#define PARENT_INO(buffer, size) \
2976 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002977
2978/*
2979 * Anybody can rename anything with this: the permission checks are left to the
2980 * higher-level routines.
2981 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002982static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2983 struct inode *new_dir, struct dentry *new_dentry)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984{
2985 handle_t *handle;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002986 struct inode *old_inode, *new_inode;
2987 struct buffer_head *old_bh, *new_bh, *dir_bh;
2988 struct ext4_dir_entry_2 *old_de, *new_de;
Theodore Ts'o8750c6d2009-02-23 23:05:27 -05002989 int retval, force_da_alloc = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002990
Christoph Hellwig871a2932010-03-03 09:05:07 -05002991 dquot_initialize(old_dir);
2992 dquot_initialize(new_dir);
Christoph Hellwig907f4552010-03-03 09:05:06 -05002993
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002994 old_bh = new_bh = dir_bh = NULL;
2995
2996 /* Initialize quotas before so that eventual writes go
2997 * in separate transaction */
2998 if (new_dentry->d_inode)
Christoph Hellwig871a2932010-03-03 09:05:07 -05002999 dquot_initialize(new_dentry->d_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003000 handle = ext4_journal_start(old_dir, 2 *
3001 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
3002 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003 if (IS_ERR(handle))
3004 return PTR_ERR(handle);
3005
3006 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
Frank Mayhar03901312009-01-07 00:06:22 -05003007 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003008
Theodore Ts'of702ba02008-09-22 15:21:01 -04003009 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003010 /*
3011 * Check for inode number is _not_ due to possible IO errors.
3012 * We might rmdir the source, keep it as pwd of some process
3013 * and merrily kill the link to whatever was created under the
3014 * same name. Goodbye sticky bit ;-<
3015 */
3016 old_inode = old_dentry->d_inode;
3017 retval = -ENOENT;
3018 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
3019 goto end_rename;
3020
3021 new_inode = new_dentry->d_inode;
Theodore Ts'of702ba02008-09-22 15:21:01 -04003022 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023 if (new_bh) {
3024 if (!new_inode) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003025 brelse(new_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026 new_bh = NULL;
3027 }
3028 }
3029 if (S_ISDIR(old_inode->i_mode)) {
3030 if (new_inode) {
3031 retval = -ENOTEMPTY;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003032 if (!empty_dir(new_inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003033 goto end_rename;
3034 }
3035 retval = -EIO;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04003036 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
3037 if (!retval) {
3038 retval = -EIO;
3039 ext4_error(old_inode->i_sb,
3040 "Directory hole detected on inode %lu\n",
3041 old_inode->i_ino);
3042 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043 goto end_rename;
Carlos Maiolino6d1ab102012-09-27 09:31:33 -04003044 }
Darrick J. Wongb0336e82012-04-29 18:41:10 -04003045 if (!buffer_verified(dir_bh) &&
3046 !ext4_dirent_csum_verify(old_inode,
3047 (struct ext4_dir_entry *)dir_bh->b_data))
3048 goto end_rename;
3049 set_buffer_verified(dir_bh);
Wei Yongjun3d0518f2009-02-14 23:01:36 -05003050 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
3051 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003052 goto end_rename;
3053 retval = -EMLINK;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003054 if (!new_inode && new_dir != old_dir &&
Aneesh Kumar K.V2c94eb82009-08-28 21:43:15 -04003055 EXT4_DIR_LINK_MAX(new_dir))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 goto end_rename;
Amir Goldsteinef607892011-03-20 21:18:44 -04003057 BUFFER_TRACE(dir_bh, "get_write_access");
3058 retval = ext4_journal_get_write_access(handle, dir_bh);
3059 if (retval)
3060 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061 }
3062 if (!new_bh) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003063 retval = ext4_add_entry(handle, new_dentry, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003064 if (retval)
3065 goto end_rename;
3066 } else {
3067 BUFFER_TRACE(new_bh, "get write access");
Amir Goldsteinef607892011-03-20 21:18:44 -04003068 retval = ext4_journal_get_write_access(handle, new_bh);
3069 if (retval)
3070 goto end_rename;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 new_de->inode = cpu_to_le32(old_inode->i_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07003072 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
3073 EXT4_FEATURE_INCOMPAT_FILETYPE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 new_de->file_type = old_de->file_type;
3075 new_dir->i_version++;
Jan Kara53b7e9f2008-04-29 22:02:11 -04003076 new_dir->i_ctime = new_dir->i_mtime =
3077 ext4_current_time(new_dir);
3078 ext4_mark_inode_dirty(handle, new_dir);
Frank Mayhar03901312009-01-07 00:06:22 -05003079 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
Darrick J. Wongb0336e82012-04-29 18:41:10 -04003080 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
Theodore Ts'ob4097142011-01-10 12:46:59 -05003081 if (unlikely(retval)) {
3082 ext4_std_error(new_dir->i_sb, retval);
3083 goto end_rename;
3084 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 brelse(new_bh);
3086 new_bh = NULL;
3087 }
3088
3089 /*
3090 * Like most other Unix systems, set the ctime for inodes on a
3091 * rename.
3092 */
Kalpak Shahef7f3832007-07-18 09:15:20 -04003093 old_inode->i_ctime = ext4_current_time(old_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003094 ext4_mark_inode_dirty(handle, old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095
3096 /*
3097 * ok, that's it
3098 */
3099 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3100 old_de->name_len != old_dentry->d_name.len ||
3101 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
Mingming Cao617ba132006-10-11 01:20:53 -07003102 (retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 old_de, old_bh)) == -ENOENT) {
3104 /* old_de could have moved from under us during htree split, so
3105 * make sure that we are deleting the right entry. We might
3106 * also be pointing to a stale entry in the unused part of
3107 * old_bh so just checking inum and the name isn't enough. */
3108 struct buffer_head *old_bh2;
Mingming Cao617ba132006-10-11 01:20:53 -07003109 struct ext4_dir_entry_2 *old_de2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003110
Theodore Ts'of702ba02008-09-22 15:21:01 -04003111 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 if (old_bh2) {
Mingming Cao617ba132006-10-11 01:20:53 -07003113 retval = ext4_delete_entry(handle, old_dir,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003114 old_de2, old_bh2);
3115 brelse(old_bh2);
3116 }
3117 }
3118 if (retval) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003119 ext4_warning(old_dir->i_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 "Deleting old file (%lu), %d, error=%d",
3121 old_dir->i_ino, old_dir->i_nlink, retval);
3122 }
3123
3124 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003125 ext4_dec_count(handle, new_inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003126 new_inode->i_ctime = ext4_current_time(new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003127 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04003128 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003129 ext4_update_dx_flag(old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 if (dir_bh) {
Wei Yongjun3d0518f2009-02-14 23:01:36 -05003131 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3132 cpu_to_le32(new_dir->i_ino);
Frank Mayhar03901312009-01-07 00:06:22 -05003133 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
Tao Maef58f692012-07-09 16:29:05 -04003134 if (is_dx(old_inode)) {
3135 retval = ext4_handle_dirty_dx_node(handle,
3136 old_inode,
3137 dir_bh);
3138 } else {
3139 retval = ext4_handle_dirty_dirent_node(handle,
3140 old_inode,
3141 dir_bh);
3142 }
Theodore Ts'ob4097142011-01-10 12:46:59 -05003143 if (retval) {
3144 ext4_std_error(old_dir->i_sb, retval);
3145 goto end_rename;
3146 }
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003147 ext4_dec_count(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003148 if (new_inode) {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003149 /* checked empty_dir above, can't have another parent,
Theodore Ts'o825f1482008-02-15 15:00:38 -05003150 * ext4_dec_count() won't work for many-linked dirs */
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003151 clear_nlink(new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 } else {
Andreas Dilgerf8628a12007-07-18 08:38:01 -04003153 ext4_inc_count(handle, new_dir);
Mingming Cao617ba132006-10-11 01:20:53 -07003154 ext4_update_dx_flag(new_dir);
3155 ext4_mark_inode_dirty(handle, new_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003156 }
3157 }
Mingming Cao617ba132006-10-11 01:20:53 -07003158 ext4_mark_inode_dirty(handle, old_dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159 if (new_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07003160 ext4_mark_inode_dirty(handle, new_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 if (!new_inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003162 ext4_orphan_add(handle, new_inode);
Theodore Ts'oafd46722009-03-16 23:12:23 -04003163 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3164 force_da_alloc = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 }
3166 retval = 0;
3167
3168end_rename:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003169 brelse(dir_bh);
3170 brelse(old_bh);
3171 brelse(new_bh);
Mingming Cao617ba132006-10-11 01:20:53 -07003172 ext4_journal_stop(handle);
Theodore Ts'o8750c6d2009-02-23 23:05:27 -05003173 if (retval == 0 && force_da_alloc)
3174 ext4_alloc_da_blocks(old_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175 return retval;
3176}
3177
3178/*
3179 * directories can handle most operations...
3180 */
Arjan van de Ven754661f2007-02-12 00:55:38 -08003181const struct inode_operations ext4_dir_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003182 .create = ext4_create,
3183 .lookup = ext4_lookup,
3184 .link = ext4_link,
3185 .unlink = ext4_unlink,
3186 .symlink = ext4_symlink,
3187 .mkdir = ext4_mkdir,
3188 .rmdir = ext4_rmdir,
3189 .mknod = ext4_mknod,
3190 .rename = ext4_rename,
3191 .setattr = ext4_setattr,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003192#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193 .setxattr = generic_setxattr,
3194 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003195 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003196 .removexattr = generic_removexattr,
3197#endif
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003198 .get_acl = ext4_get_acl,
Aneesh Kumar K.Vabc87462009-05-02 22:54:32 -04003199 .fiemap = ext4_fiemap,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003200};
3201
Arjan van de Ven754661f2007-02-12 00:55:38 -08003202const struct inode_operations ext4_special_inode_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07003203 .setattr = ext4_setattr,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003204#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003205 .setxattr = generic_setxattr,
3206 .getxattr = generic_getxattr,
Mingming Cao617ba132006-10-11 01:20:53 -07003207 .listxattr = ext4_listxattr,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003208 .removexattr = generic_removexattr,
3209#endif
Christoph Hellwig4e34e712011-07-23 17:37:31 +02003210 .get_acl = ext4_get_acl,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211};