History

gfs2: change gfs2 readdir cookie

gfs2 currently returns 31 bits of filename hash as a cookie that readdir uses for an offset into the directory. When there are a large number of directory entries, the likelihood of a collision goes up way too quickly. GFS2 will now return cookies that are guaranteed unique for a while, and then fail back to using 30 bits of filename hash. Specifically, the directory leaf blocks are divided up into chunks based on the minimum size of a gfs2 directory entry (48 bytes). Each entry's cookie is based off the chunk where it starts, in the linked list of leaf blocks that it hashes to (there are 131072 hash buckets). Directory entries will have unique names until they take reach chunk 8192. Assuming the largest filenames possible, and the least efficient spacing possible, this new method will still be able to return unique names when the previous method has statistically more than a 99% chance of a collision. The non-unique names it fails back to are guaranteed to not collide with the unique names. unique cookies will be in this format: - 1 bit "0" to make sure the the returned cookie is positive - 17 bits for the hash table index - 1 bit for the mode "0" - 13 bits for the offset non-unique cookies will be in this format: - 1 bit "0" to make sure the the returned cookie is positive - 17 bits for the hash table index - 1 bit for the mode "1" - 13 more bits of the name hash Another benefit of location based cookies, is that once a directory's exhash table is fully extended (so that multiple hash table indexs do not use the same leaf blocks), gfs2 can skip sorting the directory entries until it reaches the non-unique ones, and then it only needs to sort these. This provides a significant speed up for directory reads of very large directories. The only issue is that for these cookies to continue to point to the correct entry as files are added and removed from the directory, gfs2 must keep the entries at the same offset in the leaf block when they are split (see my previous patch). This means that until all the nodes in a cluster are running with code that will split the directory leaf blocks this way, none of the nodes can use the new cookie code. To deal with this, gfs2 now has the mount option loccookie, which, if set, will make it return these new location based cookies. This option must not be set until all nodes in the cluster are at least running this version of the kernel code, and you have guaranteed that there are no outstanding cookies required by other software, such as NFS. gfs2 uses some of the extra space at the end of the gfs2_dirent structure to store the calculated readdir cookies. This keeps us from needing to allocate a seperate array to hold these values. gfs2 recomputes the cookie stored in de_cookie for every readdir call. The time it takes to do so is small, and if gfs2 expected this value to be saved on disk, the new code wouldn't work correctly on filesystems created with an earlier version of gfs2. One issue with adding de_cookie to the union in the gfs2_dirent structure is that it caused the union to align itself to a 4 byte boundary, instead of its previous 2 byte boundary. This changed the offset of de_rahead. To solve that, I pulled de_rahead out of the union, since it does not need to be there.

Benjamin Marzinski committed 10 years ago

Commit 471f3db1 parent 34017475 files · +95 −23Browse files

Unified · Split

5 files
  1. fs/gfs2/dir.c +71 −20
  2. fs/gfs2/incore.h +3 −0
  3. fs/gfs2/ops_fstype.c +3 −0
  4. fs/gfs2/super.c +12 −0
  5. include/uapi/linux/gfs2_ondisk.h +6 −3
fs/gfs2/dir.c+71−20
@@ -82,6 +82,8 @@ · 81 unmodified lines
8282
8383 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
8484 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
85+#define GFS2_HASH_INDEX_MASK 0xffffc000
86+#define GFS2_USE_HASH_FLAG 0x2000
8587
8688 struct qstr gfs2_qdot __read_mostly;
8789 struct qstr gfs2_qdotdot __read_mostly;
@@ -1223,10 +1225,10 @@ static int compare_dents(const void *a, const void *b) · 1135 unmodified lines
12231225 int ret = 0;
12241226
12251227 dent_a = *(const struct gfs2_dirent **)a;
1226− hash_a = be32_to_cpu(dent_a->de_hash);
1228+ hash_a = dent_a->de_cookie;
12271229
12281230 dent_b = *(const struct gfs2_dirent **)b;
1229− hash_b = be32_to_cpu(dent_b->de_hash);
1231+ hash_b = dent_b->de_cookie;
12301232
12311233 if (hash_a > hash_b)
12321234 ret = 1;
@@ -1264,19 +1266,20 @@ static int compare_dents(const void *a, const void *b) · 31 unmodified lines
12641266 */
12651267
12661268 static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
1267− const struct gfs2_dirent **darr, u32 entries,
1268− int *copied)
1269+ struct gfs2_dirent **darr, u32 entries,
1270+ u32 sort_start, int *copied)
12691271 {
12701272 const struct gfs2_dirent *dent, *dent_next;
12711273 u64 off, off_next;
12721274 unsigned int x, y;
12731275 int run = 0;
12741276
1275− sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1277+ if (sort_start < entries)
1278+ sort(&darr[sort_start], entries - sort_start,
1279+ sizeof(struct gfs2_dirent *), compare_dents, NULL);
12761280
12771281 dent_next = darr[0];
1278− off_next = be32_to_cpu(dent_next->de_hash);
1279− off_next = gfs2_disk_hash2offset(off_next);
1282+ off_next = dent_next->de_cookie;
12801283
12811284 for (x = 0, y = 1; x < entries; x++, y++) {
12821285 dent = dent_next;
@@ -1284,8 +1287,7 @@ static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx, · 1 unmodified line
12841287
12851288 if (y < entries) {
12861289 dent_next = darr[y];
1287− off_next = be32_to_cpu(dent_next->de_hash);
1288− off_next = gfs2_disk_hash2offset(off_next);
1290+ off_next = dent_next->de_cookie;
12891291
12901292 if (off < ctx->pos)
12911293 continue;
@@ -1332,6 +1334,40 @@ static void *gfs2_alloc_sort_buffer(unsigned size) · 40 unmodified lines
13321334 return ptr;
13331335 }
13341336
1337+
1338+static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
1339+ unsigned leaf_nr, struct gfs2_dirent **darr,
1340+ unsigned entries)
1341+{
1342+ int sort_id = -1;
1343+ int i;
1344+
1345+ for (i = 0; i < entries; i++) {
1346+ unsigned offset;
1347+
1348+ darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
1349+ darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
1350+
1351+ if (!sdp->sd_args.ar_loccookie)
1352+ continue;
1353+ offset = (char *)(darr[i]) -
1354+ (bh->b_data + gfs2_dirent_offset(bh->b_data));
1355+ offset /= GFS2_MIN_DIRENT_SIZE;
1356+ offset += leaf_nr * sdp->sd_max_dents_per_leaf;
1357+ if (offset >= GFS2_USE_HASH_FLAG ||
1358+ leaf_nr >= GFS2_USE_HASH_FLAG) {
1359+ darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
1360+ if (sort_id < 0)
1361+ sort_id = i;
1362+ continue;
1363+ }
1364+ darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
1365+ darr[i]->de_cookie |= offset;
1366+ }
1367+ return sort_id;
1368+}
1369+
1370+
13351371 static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
13361372 int *copied, unsigned *depth,
13371373 u64 leaf_no)
@@ -1341,12 +1377,11 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, · 3 unmodified lines
13411377 struct buffer_head *bh;
13421378 struct gfs2_leaf *lf;
13431379 unsigned entries = 0, entries2 = 0;
1344− unsigned leaves = 0;
1345− const struct gfs2_dirent **darr, *dent;
1380+ unsigned leaves = 0, leaf = 0, offset, sort_offset;
1381+ struct gfs2_dirent **darr, *dent;
13461382 struct dirent_gather g;
13471383 struct buffer_head **larr;
1348− int leaf = 0;
1349− int error, i;
1384+ int error, i, need_sort = 0, sort_id;
13501385 u64 lfn = leaf_no;
13511386
13521387 do {
@@ -1362,6 +1397,11 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, · 9 unmodified lines
13621397 brelse(bh);
13631398 } while(lfn);
13641399
1400+ if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
1401+ need_sort = 1;
1402+ sort_offset = 0;
1403+ }
1404+
13651405 if (!entries)
13661406 return 0;
13671407
@@ -1375,8 +1415,8 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, · 7 unmodified lines
13751415 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
13761416 if (!larr)
13771417 goto out;
1378− darr = (const struct gfs2_dirent **)(larr + leaves);
1379− g.pdent = darr;
1418+ darr = (struct gfs2_dirent **)(larr + leaves);
1419+ g.pdent = (const struct gfs2_dirent **)darr;
13801420 g.offset = 0;
13811421 lfn = leaf_no;
13821422
@@ -1387,6 +1427,7 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, · 4 unmodified lines
13871427 lf = (struct gfs2_leaf *)bh->b_data;
13881428 lfn = be64_to_cpu(lf->lf_next);
13891429 if (lf->lf_entries) {
1430+ offset = g.offset;
13901431 entries2 += be16_to_cpu(lf->lf_entries);
13911432 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
13921433 gfs2_dirent_gather, NULL, &g);
@@ -1404,17 +1445,26 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, · 11 unmodified lines
14041445 goto out_free;
14051446 }
14061447 error = 0;
1448+ sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
1449+ be16_to_cpu(lf->lf_entries));
1450+ if (!need_sort && sort_id >= 0) {
1451+ need_sort = 1;
1452+ sort_offset = offset + sort_id;
1453+ }
14071454 larr[leaf++] = bh;
14081455 } else {
1456+ larr[leaf++] = NULL;
14091457 brelse(bh);
14101458 }
14111459 } while(lfn);
14121460
14131461 BUG_ON(entries2 != entries);
1414− error = do_filldir_main(ip, ctx, darr, entries, copied);
1462+ error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
1463+ sort_offset : entries, copied);
14151464 out_free:
14161465 for(i = 0; i < leaf; i++)
1417− brelse(larr[i]);
1466+ if (larr[i])
1467+ brelse(larr[i]);
14181468 kvfree(larr);
14191469 out:
14201470 return error;
@@ -1520,7 +1570,7 @@ int gfs2_dir_read(struct inode *inode, struct dir_context *ctx, · 99 unmodified lines
15201570 struct gfs2_inode *dip = GFS2_I(inode);
15211571 struct gfs2_sbd *sdp = GFS2_SB(inode);
15221572 struct dirent_gather g;
1523− const struct gfs2_dirent **darr, *dent;
1573+ struct gfs2_dirent **darr, *dent;
15241574 struct buffer_head *dibh;
15251575 int copied = 0;
15261576 int error;
@@ -1544,7 +1594,7 @@ int gfs2_dir_read(struct inode *inode, struct dir_context *ctx, · 17 unmodified lines
15441594 /* 96 is max number of dirents which can be stuffed into an inode */
15451595 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
15461596 if (darr) {
1547− g.pdent = darr;
1597+ g.pdent = (const struct gfs2_dirent **)darr;
15481598 g.offset = 0;
15491599 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
15501600 gfs2_dirent_gather, NULL, &g);
@@ -1561,8 +1611,9 @@ int gfs2_dir_read(struct inode *inode, struct dir_context *ctx, · 10 unmodified lines
15611611 error = -EIO;
15621612 goto out;
15631613 }
1614+ gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
15641615 error = do_filldir_main(dip, ctx, darr,
1565− dip->i_entries, &copied);
1616+ dip->i_entries, 0, &copied);
15661617 out:
15671618 kfree(darr);
15681619 }
fs/gfs2/incore.h+3−0
@@ -562,6 +562,8 @@ struct gfs2_args { · 561 unmodified lines
562562 unsigned int ar_errors:2; /* errors=withdraw | panic */
563563 unsigned int ar_nobarrier:1; /* do not send barriers */
564564 unsigned int ar_rgrplvb:1; /* use lvbs for rgrp info */
565+ unsigned int ar_loccookie:1; /* use location based readdir
566+ cookies */
565567 int ar_commit; /* Commit interval */
566568 int ar_statfs_quantum; /* The fast statfs interval */
567569 int ar_quota_quantum; /* The quota interval */
@@ -689,6 +691,7 @@ struct gfs2_sbd { · 121 unmodified lines
689691 u64 sd_heightsize[GFS2_MAX_META_HEIGHT + 1];
690692 u32 sd_max_jheight; /* Max height of journaled file's meta tree */
691693 u64 sd_jheightsize[GFS2_MAX_META_HEIGHT + 1];
694+ u32 sd_max_dents_per_leaf; /* Max number of dirents in a leaf block */
692695
693696 struct gfs2_args sd_args; /* Mount arguments */
694697 struct gfs2_tune sd_tune; /* Filesystem tuning structure */
fs/gfs2/ops_fstype.c+3−0
@@ -352,6 +352,9 @@ static int gfs2_read_sb(struct gfs2_sbd *sdp, int silent) · 351 unmodified lines
352352 sdp->sd_jheightsize[x] = ~0;
353353 gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
354354
355+ sdp->sd_max_dents_per_leaf = (sdp->sd_sb.sb_bsize -
356+ sizeof(struct gfs2_leaf)) /
357+ GFS2_MIN_DIRENT_SIZE;
355358 return 0;
356359 }
357360
fs/gfs2/super.c+12−0
@@ -83,6 +83,8 @@ enum { · 82 unmodified lines
8383 Opt_nobarrier,
8484 Opt_rgrplvb,
8585 Opt_norgrplvb,
86+ Opt_loccookie,
87+ Opt_noloccookie,
8688 Opt_error,
8789 };
8890
@@ -122,6 +124,8 @@ static const match_table_t tokens = { · 33 unmodified lines
122124 {Opt_nobarrier, "nobarrier"},
123125 {Opt_rgrplvb, "rgrplvb"},
124126 {Opt_norgrplvb, "norgrplvb"},
127+ {Opt_loccookie, "loccookie"},
128+ {Opt_noloccookie, "noloccookie"},
125129 {Opt_error, NULL}
126130 };
127131
@@ -278,6 +282,12 @@ int gfs2_mount_args(struct gfs2_args *args, char *options) · 150 unmodified lines
278282 case Opt_norgrplvb:
279283 args->ar_rgrplvb = 0;
280284 break;
285+ case Opt_loccookie:
286+ args->ar_loccookie = 1;
287+ break;
288+ case Opt_noloccookie:
289+ args->ar_loccookie = 0;
290+ break;
281291 case Opt_error:
282292 default:
283293 pr_warn("invalid mount option: %s\n", o);
@@ -1418,6 +1428,8 @@ static int gfs2_show_options(struct seq_file *s, struct dentry *root) · 1134 unmodified lines
14181428 seq_puts(s, ",demote_interface_used");
14191429 if (args->ar_rgrplvb)
14201430 seq_puts(s, ",rgrplvb");
1431+ if (args->ar_loccookie)
1432+ seq_puts(s, ",loccookie");
14211433 return 0;
14221434 }
14231435
include/uapi/linux/gfs2_ondisk.h+6−3
@@ -297,6 +297,8 @@ struct gfs2_dinode { · 296 unmodified lines
297297
298298 #define GFS2_FNAMESIZE 255
299299 #define GFS2_DIRENT_SIZE(name_len) ((sizeof(struct gfs2_dirent) + (name_len) + 7) & ~7)
300+#define GFS2_MIN_DIRENT_SIZE (GFS2_DIRENT_SIZE(1))
301+
300302
301303 struct gfs2_dirent {
302304 struct gfs2_inum de_inum;
@@ -304,11 +306,12 @@ struct gfs2_dirent { · 1 unmodified line
304306 __be16 de_rec_len;
305307 __be16 de_name_len;
306308 __be16 de_type;
309+ __be16 de_rahead;
307310 union {
308− __u8 __pad[14];
311+ __u8 __pad[12];
309312 struct {
310− __be16 de_rahead;
311− __u8 pad2[12];
313+ __u32 de_cookie; /* ondisk value not used */
314+ __u8 pad3[8];
312315 };
313316 };
314317 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t