LCOV - code coverage report
Current view: top level - fs/ext4 - namei.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 717 1433 50.0 %
Date: 2020-09-30 20:25:40 Functions: 44 67 65.7 %
Branches: 417 1210 34.5 %

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

Generated by: LCOV version 1.14