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