Whamcloud - gitweb
3c3fffce075e87645c12c5e95c61c090247d3e2e
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / linux-5.4 / ext4-data-in-dirent.patch
1 diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
2 index c7843b1..191304f 100644
3 --- a/fs/ext4/dir.c
4 +++ b/fs/ext4/dir.c
5 @@ -70,11 +70,11 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,
6         const int rlen = ext4_rec_len_from_disk(de->rec_len,
7                                                 dir->i_sb->s_blocksize);
8  
9 -       if (unlikely(rlen < EXT4_DIR_REC_LEN(1)))
10 +       if (unlikely(rlen < __EXT4_DIR_REC_LEN(1)))
11                 error_msg = "rec_len is smaller than minimal";
12         else if (unlikely(rlen % 4 != 0))
13                 error_msg = "rec_len % 4 != 0";
14 -       else if (unlikely(rlen < EXT4_DIR_REC_LEN(de->name_len)))
15 +       else if (unlikely(rlen < EXT4_DIR_REC_LEN(de)))
16                 error_msg = "rec_len is too small for name_len";
17         else if (unlikely(((char *) de - buf) + rlen > size))
18                 error_msg = "directory entry overrun";
19 @@ -219,7 +219,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
20                                  * failure will be detected in the
21                                  * dirent test below. */
22                                 if (ext4_rec_len_from_disk(de->rec_len,
23 -                                       sb->s_blocksize) < EXT4_DIR_REC_LEN(1))
24 +                                   sb->s_blocksize) < __EXT4_DIR_REC_LEN(1))
25                                         break;
26                                 i += ext4_rec_len_from_disk(de->rec_len,
27                                                             sb->s_blocksize);
28 @@ -442,12 +442,17 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
29         struct fname *fname, *new_fn;
30         struct dir_private_info *info;
31         int len;
32 +       int extra_data = 0;
33  
34         info = dir_file->private_data;
35         p = &info->root.rb_node;
36  
37         /* Create and allocate the fname structure */
38 -       len = sizeof(struct fname) + ent_name->len + 1;
39 +       if (dirent->file_type & EXT4_DIRENT_LUFID)
40 +               extra_data = ext4_get_dirent_data_len(dirent);
41 +
42 +       len = sizeof(struct fname) + ent_name->len + extra_data + 1;
43 +
44         new_fn = kzalloc(len, GFP_KERNEL);
45         if (!new_fn)
46                 return -ENOMEM;
47 @@ -456,7 +461,7 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
48         new_fn->inode = le32_to_cpu(dirent->inode);
49         new_fn->name_len = ent_name->len;
50         new_fn->file_type = dirent->file_type;
51 -       memcpy(new_fn->name, ent_name->name, ent_name->len);
52 +       memcpy(new_fn->name, ent_name->name, ent_name->len + extra_data);
53         new_fn->name[ent_name->len] = 0;
54  
55         while (*p) {
56 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
57 index ce12383..e89e6ce 100644
58 --- a/fs/ext4/ext4.h
59 +++ b/fs/ext4/ext4.h
60 @@ -1069,6 +1069,7 @@ struct ext4_inode_info {
61         __u32 i_csum_seed;
62  
63         kprojid_t i_projid;
64 +       void *i_dirdata;
65  };
66  
67  /*
68 @@ -1112,6 +1113,7 @@ struct ext4_inode_info {
69  #define EXT4_MOUNT_POSIX_ACL           0x08000 /* POSIX Access Control Lists */
70  #define EXT4_MOUNT_NO_AUTO_DA_ALLOC    0x10000 /* No auto delalloc mapping */
71  #define EXT4_MOUNT_BARRIER             0x20000 /* Use block barriers */
72 +#define EXT4_MOUNT_DIRDATA             0x40000 /* Data in directory entries*/
73  #define EXT4_MOUNT_QUOTA               0x40000 /* Some quota option set */
74  #define EXT4_MOUNT_USRQUOTA            0x80000 /* "old" user quota,
75                                                  * enable enforcement for hidden
76 @@ -1804,6 +1806,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(casefold,             CASEFOLD)
77                                          EXT4_FEATURE_INCOMPAT_FLEX_BG| \
78                                          EXT4_FEATURE_INCOMPAT_EA_INODE| \
79                                          EXT4_FEATURE_INCOMPAT_MMP | \
80 +                                        EXT4_FEATURE_INCOMPAT_DIRDATA| \
81                                          EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
82                                          EXT4_FEATURE_INCOMPAT_ENCRYPT | \
83                                          EXT4_FEATURE_INCOMPAT_CASEFOLD | \
84 @@ -1980,6 +1983,43 @@ struct ext4_dir_entry_tail {
85  #define EXT4_FT_SYMLINK                7
86  
87  #define EXT4_FT_MAX            8
88 +#define EXT4_FT_MASK           0xf
89 +
90 +#if EXT4_FT_MAX > EXT4_FT_MASK
91 +#error "conflicting EXT4_FT_MAX and EXT4_FT_MASK"
92 +#endif
93 +
94 +/*
95 + * d_type has 4 unused bits, so it can hold four types data. these different
96 + * type of data (e.g. lustre data, high 32 bits of 64-bit inode number) can be
97 + * stored, in flag order, after file-name in ext4 dirent.
98 +*/
99 +/*
100 + * this flag is added to d_type if ext4 dirent has extra data after
101 + * filename. this data length is variable and length is stored in first byte
102 + * of data. data start after filename NUL byte.
103 + * This is used by Lustre FS.
104 +  */
105 +#define EXT4_DIRENT_LUFID              0x10
106 +
107 +#define EXT4_LUFID_MAGIC    0xAD200907UL
108 +struct ext4_dentry_param {
109 +       __u32  edp_magic;       /* EXT4_LUFID_MAGIC */
110 +       char   edp_len;         /* size of edp_data in bytes */
111 +       char   edp_data[0];     /* packed array of data */
112 +} __packed;
113 +
114 +static inline unsigned char *ext4_dentry_get_data(struct super_block *sb,
115 +                                                 struct ext4_dentry_param *p)
116 +
117 +{
118 +       if (!ext4_has_feature_dirdata(sb))
119 +               return NULL;
120 +       if (p && p->edp_magic == EXT4_LUFID_MAGIC)
121 +               return &p->edp_len;
122 +       else
123 +               return NULL;
124 +}
125  
126  #define EXT4_FT_DIR_CSUM       0xDE
127  
128 @@ -1990,8 +2030,11 @@ struct ext4_dir_entry_tail {
129   */
130  #define EXT4_DIR_PAD                   4
131  #define EXT4_DIR_ROUND                 (EXT4_DIR_PAD - 1)
132 -#define EXT4_DIR_REC_LEN(name_len)     (((name_len) + 8 + EXT4_DIR_ROUND) & \
133 +#define __EXT4_DIR_REC_LEN(name_len)   (((name_len) + 8 + EXT4_DIR_ROUND) & \
134                                          ~EXT4_DIR_ROUND)
135 +#define EXT4_DIR_REC_LEN(de)           (__EXT4_DIR_REC_LEN((de)->name_len +\
136 +                                       ext4_get_dirent_data_len(de)))
137 +
138  #define EXT4_MAX_REC_LEN               ((1<<16)-1)
139  
140  /*
141 @@ -2418,11 +2461,11 @@ extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
142                              struct buffer_head *bh,
143                              void *buf, int buf_size,
144                              struct ext4_filename *fname,
145 -                            struct ext4_dir_entry_2 **dest_de);
146 +                            struct ext4_dir_entry_2 **dest_de, int *dlen);
147  void ext4_insert_dentry(struct inode *inode,
148                         struct ext4_dir_entry_2 *de,
149                         int buf_size,
150 -                       struct ext4_filename *fname);
151 +                       struct ext4_filename *fname, void *data);
152  static inline void ext4_update_dx_flag(struct inode *inode)
153  {
154         if (!ext4_has_feature_dir_index(inode->i_sb))
155 @@ -2434,10 +2477,17 @@ static const unsigned char ext4_filetype_table[] = {
156  
157  static inline  unsigned char get_dtype(struct super_block *sb, int filetype)
158  {
159 -       if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX)
160 +       int fl_index = filetype & EXT4_FT_MASK;
161 +
162 +       if (!ext4_has_feature_filetype(sb) || fl_index >= EXT4_FT_MAX)
163                 return DT_UNKNOWN;
164  
165 -       return ext4_filetype_table[filetype];
166 +       if (!test_opt(sb, DIRDATA))
167 +               return ext4_filetype_table[fl_index];
168 +
169 +       return (ext4_filetype_table[fl_index]) |
170 +               (filetype & EXT4_DIRENT_LUFID);
171 +
172  }
173  extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
174                              void *buf, int buf_size);
175 @@ -2600,6 +2650,8 @@ extern struct inode *ext4_create_inode(handle_t *handle,
176  extern int ext4_delete_entry(handle_t *handle, struct inode * dir,
177                              struct ext4_dir_entry_2 *de_del,
178                              struct buffer_head *bh);
179 +extern int ext4_add_dot_dotdot(handle_t *handle, struct inode *dir,
180 +                              struct inode *inode, const void *, const void *);
181  extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
182                                 __u32 start_minor_hash, __u32 *next_hash);
183  extern int ext4_search_dir(struct buffer_head *bh,
184 @@ -3335,6 +3387,36 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
185  
186  extern const struct iomap_ops ext4_iomap_ops;
187  
188 +/*
189 + * Compute the total directory entry data length.
190 + * This includes the filename and an implicit NUL terminator (always present),
191 + * and optional extensions.  Each extension has a bit set in the high 4 bits of
192 + * de->file_type, and the extension length is the first byte in each entry.
193 + */
194 +static inline int ext4_get_dirent_data_len(struct ext4_dir_entry_2 *de)
195 +{
196 +       char *len = de->name + de->name_len + 1 /* NUL terminator */;
197 +       int dlen = 0;
198 +       __u8 extra_data_flags = (de->file_type & ~EXT4_FT_MASK) >> 4;
199 +       struct ext4_dir_entry_tail *t = (struct ext4_dir_entry_tail *)de;
200 +
201 +       if (!t->det_reserved_zero1 &&
202 +           le16_to_cpu(t->det_rec_len) ==
203 +               sizeof(struct ext4_dir_entry_tail) &&
204 +           !t->det_reserved_zero2 &&
205 +           t->det_reserved_ft == EXT4_FT_DIR_CSUM)
206 +               return 0;
207 +
208 +       while (extra_data_flags) {
209 +               if (extra_data_flags & 1) {
210 +                       dlen += *len + (dlen == 0);
211 +                       len += *len;
212 +               }
213 +               extra_data_flags >>= 1;
214 +       }
215 +       return dlen;
216 +}
217 +
218  #endif /* __KERNEL__ */
219  
220  #define EFSBADCRC      EBADMSG         /* Bad CRC detected */
221 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
222 index f73bc39..7610cfe 100644
223 --- a/fs/ext4/inline.c
224 +++ b/fs/ext4/inline.c
225 @@ -1023,7 +1023,7 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
226         struct ext4_dir_entry_2 *de;
227  
228         err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
229 -                               inline_size, fname, &de);
230 +                               inline_size, fname, &de, NULL);
231         if (err)
232                 return err;
233  
234 @@ -1031,7 +1031,7 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
235         err = ext4_journal_get_write_access(handle, iloc->bh);
236         if (err)
237                 return err;
238 -       ext4_insert_dentry(inode, de, inline_size, fname);
239 +       ext4_insert_dentry(inode, de, inline_size, fname, NULL);
240  
241         ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
242  
243 @@ -1100,7 +1100,7 @@ static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
244         int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
245         int new_size = get_max_inline_xattr_value_size(dir, iloc);
246  
247 -       if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
248 +       if (new_size - old_size <= __EXT4_DIR_REC_LEN(1))
249                 return -ENOSPC;
250  
251         ret = ext4_update_inline_data(handle, dir,
252 @@ -1381,7 +1381,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,
253                         fake.name_len = 1;
254                         strcpy(fake.name, ".");
255                         fake.rec_len = ext4_rec_len_to_disk(
256 -                                               EXT4_DIR_REC_LEN(fake.name_len),
257 +                                               EXT4_DIR_REC_LEN(&fake),
258                                                 inline_size);
259                         ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
260                         de = &fake;
261 @@ -1391,7 +1391,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,
262                         fake.name_len = 2;
263                         strcpy(fake.name, "..");
264                         fake.rec_len = ext4_rec_len_to_disk(
265 -                                               EXT4_DIR_REC_LEN(fake.name_len),
266 +                                               EXT4_DIR_REC_LEN(&fake),
267                                                 inline_size);
268                         ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
269                         de = &fake;
270 @@ -1489,8 +1489,8 @@ int ext4_read_inline_dir(struct file *file,
271          * So we will use extra_offset and extra_size to indicate them
272          * during the inline dir iteration.
273          */
274 -       dotdot_offset = EXT4_DIR_REC_LEN(1);
275 -       dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
276 +       dotdot_offset = __EXT4_DIR_REC_LEN(1);
277 +       dotdot_size = dotdot_offset + __EXT4_DIR_REC_LEN(2);
278         extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
279         extra_size = extra_offset + inline_size;
280  
281 @@ -1525,7 +1525,7 @@ int ext4_read_inline_dir(struct file *file,
282                          * failure will be detected in the
283                          * dirent test below. */
284                         if (ext4_rec_len_from_disk(de->rec_len, extra_size)
285 -                               < EXT4_DIR_REC_LEN(1))
286 +                               < __EXT4_DIR_REC_LEN(1))
287                                 break;
288                         i += ext4_rec_len_from_disk(de->rec_len,
289                                                     extra_size);
290 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
291 index be8c35d..7c09936 100644
292 --- a/fs/ext4/namei.c
293 +++ b/fs/ext4/namei.c
294 @@ -245,7 +245,8 @@ static unsigned dx_get_count(struct dx_entry *entries);
295  static unsigned dx_get_limit(struct dx_entry *entries);
296  static void dx_set_count(struct dx_entry *entries, unsigned value);
297  static void dx_set_limit(struct dx_entry *entries, unsigned value);
298 -static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
299 +static inline unsigned dx_root_limit(struct inode *dir,
300 +               struct ext4_dir_entry_2 *dot_de, unsigned infosize);
301  static unsigned dx_node_limit(struct inode *dir);
302  static struct dx_frame *dx_probe(struct ext4_filename *fname,
303                                  struct inode *dir,
304 @@ -388,22 +389,23 @@ static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
305  {
306         struct ext4_dir_entry *dp;
307         struct dx_root_info *root;
308 -       int count_offset;
309 +       int count_offset, dot_rec_len, dotdot_rec_len;
310  
311         if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
312                 count_offset = 8;
313 -       else if (le16_to_cpu(dirent->rec_len) == 12) {
314 -               dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
315 +       else {
316 +               dot_rec_len = le16_to_cpu(dirent->rec_len);
317 +               dp = (struct ext4_dir_entry *)(((void *)dirent) + dot_rec_len);
318                 if (le16_to_cpu(dp->rec_len) !=
319 -                   EXT4_BLOCK_SIZE(inode->i_sb) - 12)
320 +                   EXT4_BLOCK_SIZE(inode->i_sb) - dot_rec_len)
321                         return NULL;
322 -               root = (struct dx_root_info *)(((void *)dp + 12));
323 +               dotdot_rec_len = EXT4_DIR_REC_LEN((struct ext4_dir_entry_2 *)dp);
324 +               root = (struct dx_root_info *)(((void *)dp + dotdot_rec_len));
325                 if (root->reserved_zero ||
326                     root->info_length != sizeof(struct dx_root_info))
327                         return NULL;
328 -               count_offset = 32;
329 -       } else
330 -               return NULL;
331 +               count_offset = 8 + dot_rec_len + dotdot_rec_len;
332 +       }
333  
334         if (offset)
335                 *offset = count_offset;
336 @@ -508,11 +510,12 @@ ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
337   */
338  struct dx_root_info *dx_get_dx_info(struct ext4_dir_entry_2 *de)
339  {
340 +       BUG_ON(de->name_len != 1);
341         /* get dotdot first */
342 -       de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(1));
343 +       de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(de));
344  
345         /* dx root info is after dotdot entry */
346 -       de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(2));
347 +       de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(de));
348  
349         return (struct dx_root_info *)de;
350  }
351 @@ -557,10 +560,16 @@ static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
352         ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
353  }
354  
355 -static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
356 +static inline unsigned dx_root_limit(struct inode *dir,
357 +               struct ext4_dir_entry_2 *dot_de, unsigned infosize)
358  {
359 -       unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
360 -               EXT4_DIR_REC_LEN(2) - infosize;
361 +       struct ext4_dir_entry_2 *dotdot_de;
362 +       unsigned entry_space;
363 +
364 +       BUG_ON(dot_de->name_len != 1);
365 +       dotdot_de = ext4_next_entry(dot_de, dir->i_sb->s_blocksize);
366 +       entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(dot_de) -
367 +                        EXT4_DIR_REC_LEN(dotdot_de) - infosize;
368  
369         if (ext4_has_metadata_csum(dir->i_sb))
370                 entry_space -= sizeof(struct dx_tail);
371 @@ -569,7 +578,7 @@ static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
372  
373  static inline unsigned dx_node_limit(struct inode *dir)
374  {
375 -       unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
376 +       unsigned entry_space = dir->i_sb->s_blocksize - __EXT4_DIR_REC_LEN(0);
377  
378         if (ext4_has_metadata_csum(dir->i_sb))
379                 entry_space -= sizeof(struct dx_tail);
380 @@ -681,7 +690,7 @@ static struct stats dx_show_leaf(struct inode *dir,
381                                        (unsigned) ((char *) de - base));
382  #endif
383                         }
384 -                       space += EXT4_DIR_REC_LEN(de->name_len);
385 +                       space += EXT4_DIR_REC_LEN(de);
386                         names++;
387                 }
388                 de = ext4_next_entry(de, size);
389 @@ -788,11 +797,14 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
390  
391         entries = (struct dx_entry *)(((char *)info) + info->info_length);
392  
393 -       if (dx_get_limit(entries) != dx_root_limit(dir,
394 -                                                  info->info_length)) {
395 +       if (dx_get_limit(entries) !=
396 +           dx_root_limit(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data,
397 +                         info->info_length)) {
398                 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
399                                    dx_get_limit(entries),
400 -                                  dx_root_limit(dir, info->info_length));
401 +                                  dx_root_limit(dir,
402 +                                         (struct ext4_dir_entry_2 *)frame->bh->b_data,
403 +                                         info->info_length));
404                 goto fail;
405         }
406  
407 @@ -987,7 +999,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
408         de = (struct ext4_dir_entry_2 *) bh->b_data;
409         top = (struct ext4_dir_entry_2 *) ((char *) de +
410                                            dir->i_sb->s_blocksize -
411 -                                          EXT4_DIR_REC_LEN(0));
412 +                                          __EXT4_DIR_REC_LEN(0));
413  #ifdef CONFIG_FS_ENCRYPTION
414         /* Check if the directory is encrypted */
415         if (IS_ENCRYPTED(dir)) {
416 @@ -1741,7 +1753,7 @@ dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
417         while (count--) {
418                 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
419                                                 (from + (map->offs<<2));
420 -               rec_len = EXT4_DIR_REC_LEN(de->name_len);
421 +               rec_len = EXT4_DIR_REC_LEN(de);
422                 memcpy (to, de, rec_len);
423                 ((struct ext4_dir_entry_2 *) to)->rec_len =
424                                 ext4_rec_len_to_disk(rec_len, blocksize);
425 @@ -1765,7 +1777,7 @@ static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
426         while ((char*)de < base + blocksize) {
427                 next = ext4_next_entry(de, blocksize);
428                 if (de->inode && de->name_len) {
429 -                       rec_len = EXT4_DIR_REC_LEN(de->name_len);
430 +                       rec_len = EXT4_DIR_REC_LEN(de);
431                         if (de > to)
432                                 memmove(to, de, rec_len);
433                         to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
434 @@ -1896,14 +1908,16 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,
435                       struct buffer_head *bh,
436                       void *buf, int buf_size,
437                       struct ext4_filename *fname,
438 -                     struct ext4_dir_entry_2 **dest_de)
439 +                     struct ext4_dir_entry_2 **dest_de, int *dlen)
440  {
441         struct ext4_dir_entry_2 *de;
442 -       unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
443 +       unsigned short reclen = __EXT4_DIR_REC_LEN(fname_len(fname)) +
444 +                                                 (dlen ? *dlen : 0);
445         int nlen, rlen;
446         unsigned int offset = 0;
447         char *top;
448  
449 +       dlen ? *dlen = 0 : 0; /* default set to 0 */
450         de = (struct ext4_dir_entry_2 *)buf;
451         top = buf + buf_size - reclen;
452         while ((char *) de <= top) {
453 @@ -1912,10 +1926,26 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,
454                         return -EFSCORRUPTED;
455                 if (ext4_match(dir, fname, de))
456                         return -EEXIST;
457 -               nlen = EXT4_DIR_REC_LEN(de->name_len);
458 +               nlen = EXT4_DIR_REC_LEN(de);
459                 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
460                 if ((de->inode ? rlen - nlen : rlen) >= reclen)
461                         break;
462 +               /* Then for dotdot entries, check for the smaller space
463 +                * required for just the entry, no FID */
464 +               if (fname_len(fname) == 2 && memcmp(fname_name(fname), "..", 2) == 0) {
465 +                       if ((de->inode ? rlen - nlen : rlen) >=
466 +                           __EXT4_DIR_REC_LEN(fname_len(fname))) {
467 +                               /* set dlen=1 to indicate not
468 +                                * enough space store fid */
469 +                               dlen ? *dlen = 1 : 0;
470 +                               break;
471 +                       }
472 +                       /* The new ".." entry must be written over the
473 +                        * previous ".." entry, which is the first
474 +                        * entry traversed by this scan. If it doesn't
475 +                        * fit, something is badly wrong, so -EIO. */
476 +                       return -EIO;
477 +               }
478                 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
479                 offset += rlen;
480         }
481 @@ -1929,12 +1959,12 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,
482  void ext4_insert_dentry(struct inode *inode,
483                         struct ext4_dir_entry_2 *de,
484                         int buf_size,
485 -                       struct ext4_filename *fname)
486 +                       struct ext4_filename *fname, void *data)
487  {
488  
489         int nlen, rlen;
490  
491 -       nlen = EXT4_DIR_REC_LEN(de->name_len);
492 +       nlen = EXT4_DIR_REC_LEN(de);
493         rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
494         if (de->inode) {
495                 struct ext4_dir_entry_2 *de1 =
496 @@ -1948,6 +1978,11 @@ void ext4_insert_dentry(struct inode *inode,
497         ext4_set_de_type(inode->i_sb, de, inode->i_mode);
498         de->name_len = fname_len(fname);
499         memcpy(de->name, fname_name(fname), fname_len(fname));
500 +       if (data) {
501 +               de->name[fname_len(fname)] = 0;
502 +               memcpy(&de->name[fname_len(fname) + 1], data, *(char *)data);
503 +               de->file_type |= EXT4_DIRENT_LUFID;
504 +       }
505  }
506  
507  /*
508 @@ -1965,14 +2000,19 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
509  {
510         unsigned int    blocksize = dir->i_sb->s_blocksize;
511         int             csum_size = 0;
512 -       int             err;
513 +       int             err, dlen = 0;
514 +       unsigned char   *data;
515  
516 +       data = ext4_dentry_get_data(inode->i_sb, (struct ext4_dentry_param *)
517 +                                               EXT4_I(inode)->i_dirdata);
518         if (ext4_has_metadata_csum(inode->i_sb))
519                 csum_size = sizeof(struct ext4_dir_entry_tail);
520  
521         if (!de) {
522 +               if (data)
523 +                       dlen = (*data) + 1;
524                 err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
525 -                                       blocksize - csum_size, fname, &de);
526 +                                       blocksize - csum_size, fname, &de, &dlen);
527                 if (err)
528                         return err;
529         }
530 @@ -1984,7 +2024,10 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
531         }
532  
533         /* By now the buffer is marked for journaling */
534 -       ext4_insert_dentry(inode, de, blocksize, fname);
535 +       /* If writing the short form of "dotdot", don't add the data section */
536 +       if (dlen == 1)
537 +               data = NULL;
538 +       ext4_insert_dentry(inode, de, blocksize, fname, data);
539  
540         /*
541          * XXX shouldn't update any times until successful
542 @@ -2093,7 +2136,8 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
543  
544         dx_set_block(entries, 1);
545         dx_set_count(entries, 1);
546 -       dx_set_limit(entries, dx_root_limit(dir, sizeof(*dx_info)));
547 +       dx_set_limit(entries, dx_root_limit(dir,
548 +                                        dot_de, sizeof(*dx_info)));
549  
550         /* Initialize as for dx_probe */
551         fname->hinfo.hash_version = dx_info->hash_version;
552 @@ -2143,6 +2187,8 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
553         struct buffer_head *dir_block;
554         struct ext4_dir_entry_2 *de;
555         int len, journal = 0, err = 0;
556 +       int dlen = 0;
557 +       char *data;
558  
559         if (IS_ERR(handle))
560                 return PTR_ERR(handle);
561 @@ -2160,19 +2206,24 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
562         /* the first item must be "." */
563         assert(de->name_len == 1 && de->name[0] == '.');
564         len = le16_to_cpu(de->rec_len);
565 -       assert(len >= EXT4_DIR_REC_LEN(1));
566 -       if (len > EXT4_DIR_REC_LEN(1)) {
567 +       assert(len >= __EXT4_DIR_REC_LEN(1));
568 +       if (len > __EXT4_DIR_REC_LEN(1)) {
569                 BUFFER_TRACE(dir_block, "get_write_access");
570                 err = ext4_journal_get_write_access(handle, dir_block);
571                 if (err)
572                         goto out_journal;
573  
574                 journal = 1;
575 -               de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(1));
576 +               de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de));
577         }
578  
579 -       len -= EXT4_DIR_REC_LEN(1);
580 -       assert(len == 0 || len >= EXT4_DIR_REC_LEN(2));
581 +       len -= EXT4_DIR_REC_LEN(de);
582 +       data = ext4_dentry_get_data(dir->i_sb,
583 +                       (struct ext4_dentry_param *)dentry->d_fsdata);
584 +       if (data)
585 +               dlen = *data + 1;
586 +       assert(len == 0 || len >= __EXT4_DIR_REC_LEN(2 + dlen));
587 +
588         de = (struct ext4_dir_entry_2 *)
589                         ((char *) de + le16_to_cpu(de->rec_len));
590         if (!journal) {
591 @@ -2186,10 +2237,15 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
592         if (len > 0)
593                 de->rec_len = cpu_to_le16(len);
594         else
595 -               assert(le16_to_cpu(de->rec_len) >= EXT4_DIR_REC_LEN(2));
596 +               assert(le16_to_cpu(de->rec_len) >= __EXT4_DIR_REC_LEN(2));
597         de->name_len = 2;
598         strcpy(de->name, "..");
599 -       ext4_set_de_type(dir->i_sb, de, S_IFDIR);
600 +       if (data != NULL && ext4_get_dirent_data_len(de) >= dlen) {
601 +               de->name[2] = 0;
602 +               memcpy(&de->name[2 + 1], data, *data);
603 +               ext4_set_de_type(dir->i_sb, de, S_IFDIR);
604 +               de->file_type |= EXT4_DIRENT_LUFID;
605 +       }
606  
607  out_journal:
608         if (journal) {
609 @@ -2229,6 +2285,7 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
610         ext4_lblk_t block, blocks;
611         int     csum_size = 0;
612  
613 +       EXT4_I(inode)->i_dirdata = dentry->d_fsdata;
614         if (ext4_has_metadata_csum(inode->i_sb))
615                 csum_size = sizeof(struct ext4_dir_entry_tail);
616  
617 @@ -2757,37 +2814,70 @@ err_unlock_inode:
618         return err;
619  }
620  
621 +struct tp_block {
622 +       struct inode *inode;
623 +       void *data1;
624 +       void *data2;
625 +};
626 +
627  struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
628                           struct ext4_dir_entry_2 *de,
629                           int blocksize, int csum_size,
630                           unsigned int parent_ino, int dotdot_real_len)
631  {
632 +       void *data1 = NULL, *data2 = NULL;
633 +       int dot_reclen = 0;
634 +
635 +       if (dotdot_real_len == 10) {
636 +               struct tp_block *tpb = (struct tp_block *)inode;
637 +               data1 = tpb->data1;
638 +               data2 = tpb->data2;
639 +               inode = tpb->inode;
640 +               dotdot_real_len = 0;
641 +       }
642         de->inode = cpu_to_le32(inode->i_ino);
643         de->name_len = 1;
644 -       de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
645 -                                          blocksize);
646         strcpy(de->name, ".");
647         ext4_set_de_type(inode->i_sb, de, S_IFDIR);
648  
649 +       /* get packed fid data*/
650 +       data1 = ext4_dentry_get_data(inode->i_sb,
651 +                               (struct ext4_dentry_param *) data1);
652 +       if (data1) {
653 +               de->name[1] = 0;
654 +               memcpy(&de->name[2], data1, *(char *) data1);
655 +               de->file_type |= EXT4_DIRENT_LUFID;
656 +       }
657 +       de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de));
658 +       dot_reclen = cpu_to_le16(de->rec_len);
659         de = ext4_next_entry(de, blocksize);
660         de->inode = cpu_to_le32(parent_ino);
661         de->name_len = 2;
662 +       strcpy(de->name, "..");
663 +       ext4_set_de_type(inode->i_sb, de, S_IFDIR);
664 +       data2 = ext4_dentry_get_data(inode->i_sb,
665 +                       (struct ext4_dentry_param *) data2);
666 +       if (data2) {
667 +               de->name[2] = 0;
668 +               memcpy(&de->name[3], data2, *(char *) data2);
669 +               de->file_type |= EXT4_DIRENT_LUFID;
670 +       }
671         if (!dotdot_real_len)
672                 de->rec_len = ext4_rec_len_to_disk(blocksize -
673 -                                       (csum_size + EXT4_DIR_REC_LEN(1)),
674 +                                       (csum_size + dot_reclen),
675                                         blocksize);
676         else
677                 de->rec_len = ext4_rec_len_to_disk(
678 -                               EXT4_DIR_REC_LEN(de->name_len), blocksize);
679 -       strcpy(de->name, "..");
680 -       ext4_set_de_type(inode->i_sb, de, S_IFDIR);
681 +                               EXT4_DIR_REC_LEN(de), blocksize);
682  
683         return ext4_next_entry(de, blocksize);
684  }
685  
686  static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
687 -                            struct inode *inode)
688 +                            struct inode *inode,
689 +                            const void *data1, const void *data2)
690  {
691 +       struct tp_block param;
692         struct buffer_head *dir_block = NULL;
693         struct ext4_dir_entry_2 *de;
694         struct ext4_dir_entry_tail *t;
695 @@ -2812,7 +2902,11 @@ static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
696         if (IS_ERR(dir_block))
697                 return PTR_ERR(dir_block);
698         de = (struct ext4_dir_entry_2 *)dir_block->b_data;
699 -       ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
700 +       param.inode = inode;
701 +       param.data1 = (void *)data1;
702 +       param.data2 = (void *)data2;
703 +       ext4_init_dot_dotdot((struct inode *)(&param), de, blocksize,
704 +                            csum_size, dir->i_ino, 10);
705         set_nlink(inode, 2);
706         if (csum_size) {
707                 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
708 @@ -2829,6 +2923,29 @@ out:
709         return err;
710  }
711  
712 +/* Initialize @inode as a subdirectory of @dir, and add the
713 + * "." and ".." entries into the first directory block. */
714 +int ext4_add_dot_dotdot(handle_t *handle, struct inode *dir,
715 +                       struct inode *inode,
716 +                       const void *data1, const void *data2)
717 +{
718 +       int rc;
719 +
720 +       if (IS_ERR(handle))
721 +               return PTR_ERR(handle);
722 +
723 +       if (IS_DIRSYNC(dir))
724 +               ext4_handle_sync(handle);
725 +
726 +       inode->i_op = &ext4_dir_inode_operations;
727 +       inode->i_fop = &ext4_dir_operations;
728 +       rc = ext4_init_new_dir(handle, dir, inode, data1, data2);
729 +       if (!rc)
730 +               rc = ext4_mark_inode_dirty(handle, inode);
731 +       return rc;
732 +}
733 +EXPORT_SYMBOL(ext4_add_dot_dotdot);
734 +
735  static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
736  {
737         handle_t *handle;
738 @@ -2855,7 +2972,7 @@ retry:
739  
740         inode->i_op = &ext4_dir_inode_operations;
741         inode->i_fop = &ext4_dir_operations;
742 -       err = ext4_init_new_dir(handle, dir, inode);
743 +       err = ext4_init_new_dir(handle, dir, inode, NULL, NULL);
744         if (err)
745                 goto out_clear_inode;
746         err = ext4_mark_inode_dirty(handle, inode);
747 @@ -2906,7 +3023,7 @@ bool ext4_empty_dir(struct inode *inode)
748         }
749  
750         sb = inode->i_sb;
751 -       if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
752 +       if (inode->i_size < __EXT4_DIR_REC_LEN(1) + __EXT4_DIR_REC_LEN(2)) {
753                 EXT4_ERROR_INODE(inode, "invalid size");
754                 return true;
755         }
756 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
757 index 1f1342f..33f7e88 100644
758 --- a/fs/ext4/super.c
759 +++ b/fs/ext4/super.c
760 @@ -1445,7 +1445,7 @@ enum {
761         Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
762         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
763         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
764 -       Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
765 +       Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, Opt_dirdata,
766         Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
767         Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
768         Opt_nowarn_on_error, Opt_mblk_io_submit,
769 @@ -1521,6 +1521,7 @@ static const match_table_t tokens = {
770         {Opt_nolazytime, "nolazytime"},
771         {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
772         {Opt_nodelalloc, "nodelalloc"},
773 +       {Opt_dirdata, "dirdata"},
774         {Opt_removed, "mblk_io_submit"},
775         {Opt_removed, "nomblk_io_submit"},
776         {Opt_block_validity, "block_validity"},
777 @@ -1744,6 +1745,7 @@ static const struct mount_opts {
778         {Opt_usrjquota, 0, MOPT_Q},
779         {Opt_grpjquota, 0, MOPT_Q},
780         {Opt_offusrjquota, 0, MOPT_Q},
781 +       {Opt_dirdata, EXT4_MOUNT_DIRDATA, MOPT_SET},
782         {Opt_offgrpjquota, 0, MOPT_Q},
783         {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
784         {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},