~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext4/namei.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/ext4/namei.c (Version linux-6.12-rc7) and /fs/ext4/namei.c (Version linux-4.4.302)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php