1 this patch implements feature which allows ext3 fs users (e.g. Lustre)
2 to store data in ext3 dirent.
3 data is stored in ext3 dirent after file-name, this space is accounted
4 in de->rec_len. flag EXT3_DIRENT_LUFID added to d_type if extra data
7 make use of dentry->d_fsdata to pass fid to ext3. so no
8 changes in ext3_add_entry() interface required.
10 Index: b/fs/ext3/namei.c
11 ===================================================================
14 @@ -174,7 +174,8 @@ static unsigned dx_get_count (struct dx_
15 static unsigned dx_get_limit (struct dx_entry *entries);
16 static void dx_set_count (struct dx_entry *entries, unsigned value);
17 static void dx_set_limit (struct dx_entry *entries, unsigned value);
18 -static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
19 +static unsigned dx_root_limit (__u32 blocksize, struct ext3_dir_entry_2*,
21 static unsigned dx_node_limit (struct inode *dir);
22 static struct dx_frame *dx_probe(struct dentry *dentry,
24 @@ -204,11 +205,13 @@ static int ext3_dx_add_entry(handle_t *h
26 struct dx_root_info * dx_get_dx_info(struct ext3_dir_entry_2 *de)
28 + BUG_ON(de->name_len != 1);
30 /* get dotdot first */
31 de = (struct ext3_dir_entry_2 *)((char *)de + le16_to_cpu(de->rec_len));
33 /* dx root info is after dotdot entry */
34 - de = (struct ext3_dir_entry_2 *)((char *)de + EXT3_DIR_REC_LEN(2));
35 + de = (struct ext3_dir_entry_2 *)((char *)de + EXT3_DIR_REC_LEN(de));
37 return (struct dx_root_info *) de;
39 @@ -253,16 +256,24 @@ static inline void dx_set_limit (struct
40 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
43 -static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
44 +static inline unsigned dx_root_limit (__u32 blocksize,
45 + struct ext3_dir_entry_2 * dot_de, unsigned infosize)
47 - unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
48 - EXT3_DIR_REC_LEN(2) - infosize;
49 - return 0? 20: entry_space / sizeof(struct dx_entry);
50 + struct ext3_dir_entry_2 * dotdot_de;
51 + unsigned entry_space;
53 + BUG_ON(dot_de->name_len != 1);
54 + dotdot_de = (struct ext3_dir_entry_2 *)((char *)dot_de
55 + + le16_to_cpu(dot_de->rec_len));
56 + entry_space = blocksize - EXT3_DIR_REC_LEN(dot_de) -
57 + EXT3_DIR_REC_LEN(dotdot_de) - infosize;
59 + return entry_space / sizeof(struct dx_entry);
62 static inline unsigned dx_node_limit (struct inode *dir)
64 - unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
65 + unsigned entry_space = dir->i_sb->s_blocksize - __EXT3_DIR_REC_LEN(1);
66 return 0? 22: entry_space / sizeof(struct dx_entry);
69 @@ -309,7 +320,7 @@ static struct stats dx_show_leaf(struct
70 printk(":%x.%u ", h.hash,
71 ((char *) de - base));
73 - space += EXT3_DIR_REC_LEN(de->name_len);
74 + space += EXT3_DIR_REC_LEN(de);
77 de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
78 @@ -411,7 +422,9 @@ dx_probe(struct dentry *dentry, struct i
80 entries = (struct dx_entry *) (((char *)info) + info->info_length);
82 - if (dx_get_limit(entries) != dx_root_limit(dir, info->info_length)) {
83 + if (dx_get_limit(entries) != dx_root_limit(dir->i_sb->s_blocksize,
84 + (struct ext3_dir_entry_2*)bh->b_data,
85 + info->info_length)) {
86 ext3_warning(dir->i_sb, __FUNCTION__,
87 "dx entry: limit != root limit");
89 @@ -583,7 +597,7 @@ static int htree_dirblock_to_tree(struct
90 de = (struct ext3_dir_entry_2 *) bh->b_data;
91 top = (struct ext3_dir_entry_2 *) ((char *) de +
92 dir->i_sb->s_blocksize -
93 - EXT3_DIR_REC_LEN(0));
94 + __EXT3_DIR_REC_LEN(0));
95 for (; de < top; de = ext3_next_entry(de)) {
96 if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
97 (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb))
98 @@ -1001,7 +1015,7 @@ static struct buffer_head * ext3_dx_find
100 de = (struct ext3_dir_entry_2 *) bh->b_data;
101 top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
102 - EXT3_DIR_REC_LEN(0));
103 + __EXT3_DIR_REC_LEN(0));
104 for (; de < top; de = ext3_next_entry(de))
105 if (ext3_match (namelen, name, de)) {
106 if (!ext3_check_dir_entry("ext3_find_entry",
107 @@ -1183,7 +1197,7 @@ dx_move_dirents(char *from, char *to, st
110 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
111 - rec_len = EXT3_DIR_REC_LEN(de->name_len);
112 + rec_len = EXT3_DIR_REC_LEN(de);
113 memcpy (to, de, rec_len);
114 ((struct ext3_dir_entry_2 *) to)->rec_len =
115 cpu_to_le16(rec_len);
116 @@ -1208,7 +1222,7 @@ static struct ext3_dir_entry_2* dx_pack_
117 next = (struct ext3_dir_entry_2 *) ((char *) de +
118 le16_to_cpu(de->rec_len));
119 if (de->inode && de->name_len) {
120 - rec_len = EXT3_DIR_REC_LEN(de->name_len);
121 + rec_len = EXT3_DIR_REC_LEN(de);
123 memmove(to, de, rec_len);
124 to->rec_len = cpu_to_le16(rec_len);
125 @@ -1334,12 +1348,18 @@ static int add_dirent_to_buf(handle_t *h
126 struct inode *dir = dentry->d_parent->d_inode;
127 const char *name = dentry->d_name.name;
128 int namelen = dentry->d_name.len;
129 + unsigned char *data;
130 unsigned long offset = 0;
131 unsigned short reclen;
132 - int nlen, rlen, err;
133 + int nlen, rlen, err, dlen = 0;
136 - reclen = EXT3_DIR_REC_LEN(namelen);
137 + data = ext3_dentry_get_data(inode->i_sb, (struct ext3_dentry_param *)
142 + reclen = __EXT3_DIR_REC_LEN(namelen + dlen);
144 de = (struct ext3_dir_entry_2 *)bh->b_data;
145 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
146 @@ -1353,7 +1373,7 @@ static int add_dirent_to_buf(handle_t *h
150 - nlen = EXT3_DIR_REC_LEN(de->name_len);
151 + nlen = EXT3_DIR_REC_LEN(de);
152 rlen = le16_to_cpu(de->rec_len);
153 if ((de->inode? rlen - nlen: rlen) >= reclen)
155 @@ -1372,7 +1392,7 @@ static int add_dirent_to_buf(handle_t *h
158 /* By now the buffer is marked for journaling */
159 - nlen = EXT3_DIR_REC_LEN(de->name_len);
160 + nlen = EXT3_DIR_REC_LEN(de);
161 rlen = le16_to_cpu(de->rec_len);
163 struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
164 @@ -1388,6 +1408,11 @@ static int add_dirent_to_buf(handle_t *h
166 de->name_len = namelen;
167 memcpy (de->name, name, namelen);
169 + de->name[namelen] = 0;
170 + memcpy(&de->name[namelen + 1], data, *(char*) data);
171 + de->file_type |= EXT3_DIRENT_LUFID;
174 * XXX shouldn't update any times until successful
175 * completion of syscall, but too many callers depend
176 @@ -1474,7 +1499,8 @@ static int make_indexed_dir(handle_t *ha
177 entries = (void *)dx_info + sizeof(*dx_info);
178 dx_set_block (entries, 1);
179 dx_set_count (entries, 1);
180 - dx_set_limit (entries, dx_root_limit(dir, sizeof(*dx_info)));
181 + dx_set_limit (entries, dx_root_limit(dir->i_sb->s_blocksize,
182 + dot_de, sizeof(*dx_info)));
184 /* Initialize as for dx_probe */
185 hinfo.hash_version = dx_info->hash_version;
186 @@ -1502,6 +1528,8 @@ static int ext3_update_dotdot(handle_t *
187 struct buffer_head * dir_block;
188 struct ext3_dir_entry_2 * de;
189 int len, journal = 0, err = 0;
194 return PTR_ERR(handle);
195 @@ -1517,19 +1545,23 @@ static int ext3_update_dotdot(handle_t *
196 /* the first item must be "." */
197 assert(de->name_len == 1 && de->name[0] == '.');
198 len = le16_to_cpu(de->rec_len);
199 - assert(len >= EXT3_DIR_REC_LEN(1));
200 - if (len > EXT3_DIR_REC_LEN(1)) {
201 + assert(len >= __EXT3_DIR_REC_LEN(1));
202 + if (len > __EXT3_DIR_REC_LEN(1)) {
203 BUFFER_TRACE(dir_block, "get_write_access");
204 err = ext3_journal_get_write_access(handle, dir_block);
209 - de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(1));
210 + de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de));
213 - len -= EXT3_DIR_REC_LEN(1);
214 - assert(len == 0 || len >= EXT3_DIR_REC_LEN(2));
215 + len -= EXT3_DIR_REC_LEN(de);
216 + data = ext3_dentry_get_data(dir->i_sb,
217 + (struct ext3_dentry_param *) dentry->d_fsdata);
220 + assert(len == 0 || len >= __EXT3_DIR_REC_LEN(2 + dlen));
221 de = (struct ext3_dir_entry_2 *)
222 ((char *) de + le16_to_cpu(de->rec_len));
224 @@ -1543,11 +1575,15 @@ static int ext3_update_dotdot(handle_t *
226 de->rec_len = cpu_to_le16(len);
228 - assert(le16_to_cpu(de->rec_len) >= EXT3_DIR_REC_LEN(2));
229 + assert(le16_to_cpu(de->rec_len) >= __EXT3_DIR_REC_LEN(2));
231 strcpy (de->name, "..");
232 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
236 + memcpy(&de->name[2 + 1], data, dlen);
237 + de->file_type |= EXT3_DIRENT_LUFID;
241 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
242 @@ -1958,12 +1994,12 @@ retry:
244 /* Initialize @inode as a subdirectory of @dir, and add the
245 * "." and ".." entries into the first directory block. */
246 -int ext3_add_dot_dotdot(handle_t *handle, struct inode * dir,
247 - struct inode *inode)
248 +int ext3_add_dot_dotdot(handle_t *handle, struct inode * dir, struct inode *inode,
249 + const void *data1, const void *data2)
251 struct buffer_head * dir_block;
252 struct ext3_dir_entry_2 * de;
254 + int err = 0, dot_reclen;
257 return PTR_ERR(handle);
258 @@ -1983,16 +2019,36 @@ int ext3_add_dot_dotdot(handle_t *handle
259 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
260 de->inode = cpu_to_le32(inode->i_ino);
262 - de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));
263 - strcpy (de->name, ".");
264 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
265 + strcpy (de->name, ".");
267 + /* get packed fid data*/
268 + data1 = ext3_dentry_get_data(dir->i_sb,
269 + (struct ext3_dentry_param *) data1);
272 + memcpy(&de->name[2], data1, *(char*) data1);
273 + de->file_type |= EXT3_DIRENT_LUFID;
275 + de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de));
276 + dot_reclen = cpu_to_le16(de->rec_len);
278 de = (struct ext3_dir_entry_2 *)
279 ((char *) de + le16_to_cpu(de->rec_len));
280 de->inode = cpu_to_le32(dir->i_ino);
281 - de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));
282 + de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-dot_reclen);
284 strcpy (de->name, "..");
285 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
287 + /* get packed fid data*/
288 + data2 = ext3_dentry_get_data(dir->i_sb,
289 + (struct ext3_dentry_param *) data2);
292 + memcpy(&de->name[3], data2, *(char*) data2);
293 + de->file_type |= EXT3_DIRENT_LUFID;
296 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
297 ext3_journal_dirty_metadata(handle, dir_block);
298 @@ -2029,7 +2085,7 @@ retry:
302 - err = ext3_add_dot_dotdot(handle, dir, inode);
303 + err = ext3_add_dot_dotdot(handle, dir, inode, NULL, NULL);
306 ext3_mark_inode_dirty(handle, inode);
307 @@ -2067,7 +2123,7 @@ static int empty_dir (struct inode * ino
311 - if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
312 + if (inode->i_size < __EXT3_DIR_REC_LEN(1) + __EXT3_DIR_REC_LEN(2) ||
313 !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
315 ext3_error(inode->i_sb, __FUNCTION__,
316 Index: b/include/linux/ext3_fs.h
317 ===================================================================
318 --- a/include/linux/ext3_fs.h
319 +++ b/include/linux/ext3_fs.h
320 @@ -717,13 +717,16 @@ static inline int ext3_valid_inum(struct
321 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
322 #define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
323 #define EXT3_FEATURE_INCOMPAT_MMP 0x0100
324 +#define EXT3_FEATURE_INCOMPAT_DIRDATA 0x1000
326 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
327 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
328 EXT3_FEATURE_INCOMPAT_RECOVER| \
329 EXT3_FEATURE_INCOMPAT_META_BG| \
330 EXT3_FEATURE_INCOMPAT_EXTENTS| \
331 - EXT3_FEATURE_INCOMPAT_MMP)
332 + EXT3_FEATURE_INCOMPAT_MMP| \
333 + EXT3_FEATURE_INCOMPAT_DIRDATA)
335 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
336 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
337 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
338 @@ -790,7 +793,44 @@ struct ext3_dir_entry_2 {
339 #define EXT3_FT_SYMLINK 7
341 #define EXT3_FT_MAX 8
342 +#define EXT3_FT_MASK 0xf
344 +#if EXT3_FT_MAX > EXT3_FT_MASK
345 +#error "conflicting EXT3_FT_MAX and EXT3_FT_MASK"
348 +#define EXT3_LUFID_MAGIC 0xAD200907UL
349 +struct ext3_dentry_param {
350 + __u32 edp_magic; /* EXT3_LUFID_MAGIC */
351 + char edp_len; /* size of edp_data in bytes */
352 + char edp_data[0]; /* packed array of data */
353 +} __attribute__((packed));
355 +static inline unsigned char *ext3_dentry_get_data(struct super_block *sb,
356 + struct ext3_dentry_param* p)
359 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_DIRDATA))
361 + if (p && p->edp_magic == EXT3_LUFID_MAGIC)
362 + return &p->edp_len;
368 + * d_type has 4 unused bits, so it can hold four types data. these different
369 + * type of data (e.g. lustre data, high 32 bits of 64-bit inode number) can be
370 + * stored, in flag order, after file-name in ext3 dirent.
373 + * this flag is added to d_type if ext3 dirent has extra data after
374 + * filename. this data length is variable and length is stored in first byte
375 + * of data. data start after filename NUL byte.
376 + * This is used by Lustre FS.
379 +#define EXT3_DIRENT_LUFID 0x10
381 * EXT3_DIR_PAD defines the directory entries boundaries
383 @@ -798,8 +838,12 @@ struct ext3_dir_entry_2 {
385 #define EXT3_DIR_PAD 4
386 #define EXT3_DIR_ROUND (EXT3_DIR_PAD - 1)
387 -#define EXT3_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT3_DIR_ROUND) & \
388 +#define __EXT3_DIR_REC_LEN(len) (((len) + 8 + EXT3_DIR_ROUND) & \
391 +#define EXT3_DIR_REC_LEN(de) (__EXT3_DIR_REC_LEN(de->name_len +\
392 + ext3_get_dirent_data_len(de)))
395 * Hash Tree Directory indexing
396 * (c) Daniel Phillips, 2001
397 @@ -1125,7 +1169,8 @@ extern struct buffer_head * ext3_find_en
398 struct ext3_dir_entry_2
400 extern int ext3_add_dot_dotdot(handle_t *handle, struct inode *dir,
401 - struct inode *inode);
402 + struct inode *inode, const void *data1,
403 + const void *data2);
404 extern struct inode_operations ext3_dir_inode_operations;
405 extern struct inode_operations ext3_special_inode_operations;
406 extern struct buffer_head *ext3_append(handle_t *handle, struct inode *inode,
407 @@ -1159,6 +1204,29 @@ ext3_get_blocks_wrap(handle_t *handle, s
412 + * Compute the total directory entry data length.
413 + * This includes the filename and an implicit NUL terminator (always present),
414 + * and optional extensions. Each extension has a bit set in the high 4 bits of
415 + * de->file_type, and the extension length is the first byte in each entry.
418 +static inline int ext3_get_dirent_data_len(struct ext3_dir_entry_2 *de)
420 + char *len = de->name + de->name_len + 1 /* NUL terminator */;
422 + __u8 extra_data_flags = (de->file_type & ~EXT3_FT_MASK) >> 4;
424 + while (extra_data_flags) {
425 + if (extra_data_flags & 1) {
426 + dlen += *len + (dlen == 0);
429 + extra_data_flags >>= 1;
434 #endif /* __KERNEL__ */
436 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
437 Index: b/fs/ext3/dir.c
438 ===================================================================
441 @@ -53,11 +53,17 @@ const struct file_operations ext3_dir_op
443 static unsigned char get_dtype(struct super_block *sb, int filetype)
445 + int fl_index = filetype & EXT3_FT_MASK;
447 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE) ||
448 - (filetype >= EXT3_FT_MAX))
449 + (fl_index >= EXT3_FT_MAX))
452 - return (ext3_filetype_table[filetype]);
453 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_DIRDATA))
454 + return (ext3_filetype_table[fl_index]);
456 + return (ext3_filetype_table[fl_index]) |
457 + (filetype & EXT3_DIRENT_LUFID);
461 @@ -69,11 +75,11 @@ int ext3_check_dir_entry (const char * f
462 const char * error_msg = NULL;
463 const int rlen = le16_to_cpu(de->rec_len);
465 - if (rlen < EXT3_DIR_REC_LEN(1))
466 + if (rlen < __EXT3_DIR_REC_LEN(1))
467 error_msg = "rec_len is smaller than minimal";
468 else if (rlen % 4 != 0)
469 error_msg = "rec_len % 4 != 0";
470 - else if (rlen < EXT3_DIR_REC_LEN(de->name_len))
471 + else if (rlen < EXT3_DIR_REC_LEN(de))
472 error_msg = "rec_len is too small for name_len";
473 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
474 error_msg = "directory entry across blocks";
475 @@ -177,7 +183,7 @@ revalidate:
476 * failure will be detected in the
477 * dirent test below. */
478 if (le16_to_cpu(de->rec_len) <
479 - EXT3_DIR_REC_LEN(1))
480 + __EXT3_DIR_REC_LEN(1))
482 i += le16_to_cpu(de->rec_len);
484 @@ -210,7 +216,6 @@ revalidate:
485 * during the copy operation.
487 unsigned long version = filp->f_version;
489 error = filldir(dirent, de->name,
492 @@ -342,13 +347,17 @@ int ext3_htree_store_dirent(struct file
493 struct rb_node **p, *parent = NULL;
494 struct fname * fname, *new_fn;
495 struct dir_private_info *info;
496 + int extra_data = 1;
499 info = (struct dir_private_info *) dir_file->private_data;
500 p = &info->root.rb_node;
502 /* Create and allocate the fname structure */
503 - len = sizeof(struct fname) + dirent->name_len + 1;
504 + if (dirent->file_type & EXT3_DIRENT_LUFID)
505 + extra_data = ext3_get_dirent_data_len(dirent);
507 + len = sizeof(struct fname) + dirent->name_len + extra_data;
508 new_fn = kmalloc(len, GFP_KERNEL);
511 @@ -358,7 +367,7 @@ int ext3_htree_store_dirent(struct file
512 new_fn->inode = le32_to_cpu(dirent->inode);
513 new_fn->name_len = dirent->name_len;
514 new_fn->file_type = dirent->file_type;
515 - memcpy(new_fn->name, dirent->name, dirent->name_len);
516 + memcpy(new_fn->name, dirent->name, dirent->name_len + extra_data);
517 new_fn->name[dirent->name_len] = 0;