Skip to contentSkip to conversation

Extend index to save more flags

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Extend index to save more flags

The on-disk format of index only saves 16 bit flags, nearly all have been used. The last bit (CE_EXTENDED) is used to for future extension.

This patch extends index entry format to save more flags in future. The new entry format will be used when CE_EXTENDED bit is 1.

Because older implementation may not understand CE_EXTENDED bit and misread the new format, if there is any extended entry in index, index header version will turn 3, which makes it incompatible for older git. If there is none, header version will return to 2 again.

---

Quoted 11 lines

Ok. We would need to use an extra bit for this.

The bit 0x4000 is the last one available, so we would want to use it as "this index entry uses more bits than the traditional format" bit, and define a backward incompatible on-disk index entry format to actually record CE_NO_CHECKOUT and other flags we will invent in the future.

Perhaps ondisk_cache_entry structure will have an extra "unsigned int flags2" after "flags" when that bit is on, and we can have 31 more bits in flags2, with the highest bit of flags2 signalling the presense of flags3 word in the future, or something like that.

I was about to add a new field in cache_entry in order to have 31 new bits, but that led to initialization nightmare because there is no common routine to initialize cache_entry, the new field must be initialized too many places.

So new ondisk bits will stay in cache_entry.ce_flags. That means 9 bits left for both new on-disk bits and in-memory ones. Narrow/sparse checkout will use 2. The remaining 7 bits will probably be enough for coming years?

 cache.h      |   34 +++++++++++++++++++++++++++++++---
 read-cache.c |   49 +++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 70 insertions(+), 13 deletions(-)
cache.h +31 -3
index f725783..120ed07 100644
@@ -109,6 +109,26 @@ struct ondisk_cache_entry {
 	char name[FLEX_ARRAY]; /* more */
 };
 
+/*
+ * This struct is used when CE_EXTENDED bit is 1
+ * The struct must match ondisk_cache_entry exactly from
+ * ctime till flags
+ */
+struct ondisk_cache_entry_extended {
+	struct cache_time ctime;
+	struct cache_time mtime;
+	unsigned int dev;
+	unsigned int ino;
+	unsigned int mode;
+	unsigned int uid;
+	unsigned int gid;
+	unsigned int size;
+	unsigned char sha1[20];
+	unsigned short flags;
+	unsigned short flags2;
+	char name[FLEX_ARRAY]; /* more */
+};
+
 struct cache_entry {
 	unsigned int ce_ctime;
 	unsigned int ce_mtime;
@@ -139,6 +159,10 @@ struct cache_entry {
 #define CE_HASHED    (0x100000)
 #define CE_UNHASHED  (0x200000)
 
+/* Extended flags in ondisk_cache_entry_extended */
+#define CE_EXTENDED2 (0x80000000)
+#define CE_EXTENDED_MASK (CE_EXTENDED2)
+
 /*
  * Copy the sha1 and stat state of a cache entry from one to
  * another. But we never change the name, or the hash state!
@@ -171,7 +195,9 @@ static inline size_t ce_namelen(const struct cache_entry *ce)
 }
 
 #define ce_size(ce) cache_entry_size(ce_namelen(ce))
-#define ondisk_ce_size(ce) ondisk_cache_entry_size(ce_namelen(ce))
+#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \
+			    ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
+			    ondisk_cache_entry_size(ce_namelen(ce)))
 #define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
 #define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
 #define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
@@ -214,8 +240,10 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
 	(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
 	S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
 
-#define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
-#define ondisk_cache_entry_size(len) ((offsetof(struct ondisk_cache_entry,name) + (len) + 8) & ~7)
+#define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
+#define cache_entry_size(len) flexible_size(cache_entry,len)
+#define ondisk_cache_entry_size(len) flexible_size(ondisk_cache_entry,len)
+#define ondisk_cache_entry_extended_size(len) flexible_size(ondisk_cache_entry_extended,len)
 
 struct index_state {
 	struct cache_entry **cache;
read-cache.c +39 -10

100 lines not on this page · raw message

Signature · 1 line
1.6.0.96.g2fad1.dirty
#1 lore raw

cache.h struct cache_entry { · line 164

Quoted 3 lines · Nguyễn Thái Ngọc Duy
cache.h +3 -0
+/* Extended flags in ondisk_cache_entry_extended */
+#define CE_EXTENDED2 (0x80000000)
+#define CE_EXTENDED_MASK (CE_EXTENDED2)

Wouldn't it be better, from a maintainance point of view, to have

#define CE_EXTENDED_MASK 0xffff0000

right from the beginning?

Quoted 8 lines
read-cache.c +0 -3
@@ -1143,19 +1144,29 @@ static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_en
 	/* On-disk flags are just 16 bits */
 	ce->ce_flags = ntohs(ondisk->flags);
 
-	/* For future extension: we do not understand this entry yet */
-	if (ce->ce_flags & CE_EXTENDED)
-		die("Unknown index entry format");
 	hashcpy(ce->sha1, ondisk->sha1);

read-cache.c static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_en · line 1157

Quoted 10 lines
read-cache.c +8 -0
 
 	len = ce->ce_flags & CE_NAMEMASK;
+
+	if (ce->ce_flags & CE_EXTENDED) {
+		struct ondisk_cache_entry_extended *ondisk2;
+		ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
+		ce->ce_flags |= (ntohs(ondisk2->flags2) << 16) & CE_EXTENDED_MASK;
+		/* For future extension: we do not understand the last bit yet */
+		if (ce->ce_flags & CE_EXTENDED2)
+			die("Unknown index entry format");

At this point, we do not understand the CE_EXTENDED2 flag; but we do not understand any of the other 15 extended flags, either. So, you should error out if *any* of them is non-zero.

read-cache.c static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_en · line 1161

Quoted 4 lines
read-cache.c +4 -0
+		name = ondisk2->name;
+	}
+	else
+		name = ondisk->name;

-- Hannes

#2 lore raw

PATCH 1/1 Extend index to save more flags

The on-disk format of index only saves 16 bit flags, nearly all have been used. The last bit (CE_EXTENDED) is used to for future extension.

This patch extends index entry format to save more flags in future. The new entry format will be used when CE_EXTENDED bit is 1.

Because older implementation may not understand CE_EXTENDED bit and misread the new format, if there is any extended entry in index, index header version will turn 3, which makes it incompatible for older git. If there is none, header version will return to 2 again.

---

Quoted 10 lines · Johannes Sixt

Review

Quoted 3 lines
(quoted diff) +3 -0
+/* Extended flags in ondisk_cache_entry_extended */
+#define CE_EXTENDED2 (0x80000000)
+#define CE_EXTENDED_MASK (CE_EXTENDED2)

Wouldn't it be better, from a maintainance point of view, to have

#define CE_EXTENDED_MASK 0xffff0000

right from the beginning?

Actually it's not 16 bits. We have 16 bits on disk, but will not use all of them until cache_entry is adjusted. But I think extending the mask makes sense.

Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+             /* For future extension: we do not understand the last bit yet */
+             if (ce->ce_flags & CE_EXTENDED2)
+                     die("Unknown index entry format");

At this point, we do not understand the CE_EXTENDED2 flag; but we do not
understand any of the other 15 extended flags, either. So, you should error out if *any* of them is non-zero.

Fixed with CE_KNOWN_EXTENDED_FLAGS

 cache.h      |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 read-cache.c |   49 +++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 89 insertions(+), 14 deletions(-)
cache.h +50 -4
index f725783..f8578d1 100644
@@ -109,6 +109,26 @@ struct ondisk_cache_entry {
 	char name[FLEX_ARRAY]; /* more */
 };
 
+/*
+ * This struct is used when CE_EXTENDED bit is 1
+ * The struct must match ondisk_cache_entry exactly from
+ * ctime till flags
+ */
+struct ondisk_cache_entry_extended {
+	struct cache_time ctime;
+	struct cache_time mtime;
+	unsigned int dev;
+	unsigned int ino;
+	unsigned int mode;
+	unsigned int uid;
+	unsigned int gid;
+	unsigned int size;
+	unsigned char sha1[20];
+	unsigned short flags;
+	unsigned short flags2;
+	char name[FLEX_ARRAY]; /* more */
+};
+
 struct cache_entry {
 	unsigned int ce_ctime;
 	unsigned int ce_mtime;
@@ -130,7 +150,15 @@ struct cache_entry {
 #define CE_VALID     (0x8000)
 #define CE_STAGESHIFT 12
 
-/* In-memory only */
+/*
+ * Range 0xFFFF0000 in ce_flags is divided into
+ * two parts: in-memory flags and on-disk ones.
+ * Flags in CE_EXTENDED_MASK will get saved on-disk
+ * so if you want an in-memory flag, remove it
+ * from CE_EXTENDED_MASK.
+ *
+ * In-memory only flags
+ */
 #define CE_UPDATE    (0x10000)
 #define CE_REMOVE    (0x20000)
 #define CE_UPTODATE  (0x40000)
@@ -140,6 +168,20 @@ struct cache_entry {
 #define CE_UNHASHED  (0x200000)
 
 /*
+ * We'll be careful on new extended on-disk flags,
+ * Git will refuse to read unknown flags. When you
+ * add a new flag, add it to CE_KNOWN_EXTENDED_FLAGS.
+ * CE_EXTENDED2 is for future extension, so it will
+ * remain unknown until future is set.
+ *
+ * Extended on-disk flags
+ */
+#define CE_EXTENDED2 (0x80000000)
+
+#define CE_KNOWN_EXTENDED_FLAGS (0)
+#define CE_EXTENDED_MASK (0xFFC00000)
+
+/*
  * Copy the sha1 and stat state of a cache entry from one to
  * another. But we never change the name, or the hash state!
  */
@@ -171,7 +213,9 @@ static inline size_t ce_namelen(const struct cache_entry *ce)
 }
 
 #define ce_size(ce) cache_entry_size(ce_namelen(ce))
-#define ondisk_ce_size(ce) ondisk_cache_entry_size(ce_namelen(ce))
+#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \
+			    ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
+			    ondisk_cache_entry_size(ce_namelen(ce)))
 #define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
 #define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
 #define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
@@ -214,8 +258,10 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
 	(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
 	S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
 
-#define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
-#define ondisk_cache_entry_size(len) ((offsetof(struct ondisk_cache_entry,name) + (len) + 8) & ~7)
+#define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
+#define cache_entry_size(len) flexible_size(cache_entry,len)
+#define ondisk_cache_entry_size(len) flexible_size(ondisk_cache_entry,len)
+#define ondisk_cache_entry_extended_size(len) flexible_size(ondisk_cache_entry_extended,len)
 
 struct index_state {
 	struct cache_entry **cache;
read-cache.c +39 -10

100 lines not on this page · raw message

Signature · 1 line
1.6.0.96.g2fad1.dirty
#3 lore raw
Quoted 10 lines · Nguyễn Thái Ngọc Duy

The on-disk format of index only saves 16 bit flags, nearly all have been used. The last bit (CE_EXTENDED) is used to for future extension.

This patch extends index entry format to save more flags in future. The new entry format will be used when CE_EXTENDED bit is 1.

Because older implementation may not understand CE_EXTENDED bit and misread the new format, if there is any extended entry in index, index header version will turn 3, which makes it incompatible for older git. If there is none, header version will return to 2 again.

I think this is a good change.

Quoted 18 lines
cache.h +10 -0
index f725783..f8578d1 100644
@@ -109,6 +109,26 @@ struct ondisk_cache_entry {
 	char name[FLEX_ARRAY]; /* more */
 };
 
+/*
+ * This struct is used when CE_EXTENDED bit is 1
+ * The struct must match ondisk_cache_entry exactly from
+ * ctime till flags
+ */
+struct ondisk_cache_entry_extended {
+	struct cache_time ctime;
+	struct cache_time mtime;
+	unsigned int dev;
+	unsigned int ino;

cache.h struct ondisk_cache_entry { · line 131

Quoted 10 lines
cache.h +10 -0
+	unsigned int mode;
+	unsigned int uid;
+	unsigned int gid;
+	unsigned int size;
+	unsigned char sha1[20];
+	unsigned short flags;
+	unsigned short flags2;
+	char name[FLEX_ARRAY]; /* more */
+};
+

We should change these to more explicitly sized uint32_t both in the original and this extended structure, but after this patch (or later variant of it) lands in the tree.

#4 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Nguyễn Thái Ngọc Duy

The on-disk format of index only saves 16 bit flags, nearly all have been used. The last bit (CE_EXTENDED) is used to for future extension.

This patch extends index entry format to save more flags in future. The new entry format will be used when CE_EXTENDED bit is 1.

Quoted 7 lines
Quoted 4 lines

Because older implementation may not understand CE_EXTENDED bit and misread the new format, if there is any extended entry in index, index header version will turn 3, which makes it incompatible for older git. If there is none, header version will return to 2 again.

I think this is a good change.

Just don't apply it now. There is a bug in die() code for unknown flags. I'll resend with narrow checkout series, which will also be a test case for these change.

Signature · 1 line
Duy