Skip to content

Commit 5400839

Browse files
Yan, ZhengSage Weil
authored andcommitted
ceph: preallocate buffer for readdir reply
Preallocate buffer for readdir reply. Limit number of entries in readdir reply according to the buffer size. Signed-off-by: Yan, Zheng <zheng.z.yan@intel.com>
1 parent 18cb95a commit 5400839

3 files changed

Lines changed: 59 additions & 21 deletions

File tree

‎fs/ceph/dir.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
252252
int err;
253253
u32 ftype;
254254
struct ceph_mds_reply_info_parsed *rinfo;
255-
const int max_entries = fsc->mount_options->max_readdir;
256-
const int max_bytes = fsc->mount_options->max_readdir_bytes;
257255

258256
dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
259257
if (fi->flags & CEPH_F_ATEND)
@@ -327,6 +325,11 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
327325
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
328326
if (IS_ERR(req))
329327
return PTR_ERR(req);
328+
err = ceph_alloc_readdir_reply_buffer(req, inode);
329+
if (err) {
330+
ceph_mdsc_put_request(req);
331+
return err;
332+
}
330333
req->r_inode = inode;
331334
ihold(inode);
332335
req->r_dentry = dget(file->f_dentry);
@@ -337,9 +340,6 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx)
337340
req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
338341
req->r_readdir_offset = fi->next_offset;
339342
req->r_args.readdir.frag = cpu_to_le32(frag);
340-
req->r_args.readdir.max_entries = cpu_to_le32(max_entries);
341-
req->r_args.readdir.max_bytes = cpu_to_le32(max_bytes);
342-
req->r_num_caps = max_entries + 1;
343343
err = ceph_mdsc_do_request(mdsc, NULL, req);
344344
if (err < 0) {
345345
ceph_mdsc_put_request(req);

‎fs/ceph/mds_client.c‎

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/fs.h>
44
#include <linux/wait.h>
55
#include <linux/slab.h>
6+
#include <linux/gfp.h>
67
#include <linux/sched.h>
78
#include <linux/debugfs.h>
89
#include <linux/seq_file.h>
@@ -165,21 +166,18 @@ static int parse_reply_info_dir(void **p, void *end,
165166
if (num == 0)
166167
goto done;
167168

168-
/* alloc large array */
169-
info->dir_nr = num;
170-
info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
171-
sizeof(*info->dir_dname) +
172-
sizeof(*info->dir_dname_len) +
173-
sizeof(*info->dir_dlease),
174-
GFP_NOFS);
175-
if (info->dir_in == NULL) {
176-
err = -ENOMEM;
177-
goto out_bad;
178-
}
169+
BUG_ON(!info->dir_in);
179170
info->dir_dname = (void *)(info->dir_in + num);
180171
info->dir_dname_len = (void *)(info->dir_dname + num);
181172
info->dir_dlease = (void *)(info->dir_dname_len + num);
173+
if ((unsigned long)(info->dir_dlease + num) >
174+
(unsigned long)info->dir_in + info->dir_buf_size) {
175+
pr_err("dir contents are larger than expected\n");
176+
WARN_ON(1);
177+
goto bad;
178+
}
182179

180+
info->dir_nr = num;
183181
while (num) {
184182
/* dentry */
185183
ceph_decode_need(p, end, sizeof(u32)*2, bad);
@@ -327,7 +325,9 @@ static int parse_reply_info(struct ceph_msg *msg,
327325

328326
static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
329327
{
330-
kfree(info->dir_in);
328+
if (!info->dir_in)
329+
return;
330+
free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
331331
}
332332

333333

@@ -512,12 +512,11 @@ void ceph_mdsc_release_request(struct kref *kref)
512512
struct ceph_mds_request *req = container_of(kref,
513513
struct ceph_mds_request,
514514
r_kref);
515+
destroy_reply_info(&req->r_reply_info);
515516
if (req->r_request)
516517
ceph_msg_put(req->r_request);
517-
if (req->r_reply) {
518+
if (req->r_reply)
518519
ceph_msg_put(req->r_reply);
519-
destroy_reply_info(&req->r_reply_info);
520-
}
521520
if (req->r_inode) {
522521
ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
523522
iput(req->r_inode);
@@ -1496,6 +1495,43 @@ static void discard_cap_releases(struct ceph_mds_client *mdsc,
14961495
* requests
14971496
*/
14981497

1498+
int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1499+
struct inode *dir)
1500+
{
1501+
struct ceph_inode_info *ci = ceph_inode(dir);
1502+
struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1503+
struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1504+
size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1505+
sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1506+
int order, num_entries;
1507+
1508+
spin_lock(&ci->i_ceph_lock);
1509+
num_entries = ci->i_files + ci->i_subdirs;
1510+
spin_unlock(&ci->i_ceph_lock);
1511+
num_entries = max(num_entries, 1);
1512+
num_entries = min(num_entries, opt->max_readdir);
1513+
1514+
order = get_order(size * num_entries);
1515+
while (order >= 0) {
1516+
rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1517+
order);
1518+
if (rinfo->dir_in)
1519+
break;
1520+
order--;
1521+
}
1522+
if (!rinfo->dir_in)
1523+
return -ENOMEM;
1524+
1525+
num_entries = (PAGE_SIZE << order) / size;
1526+
num_entries = min(num_entries, opt->max_readdir);
1527+
1528+
rinfo->dir_buf_size = PAGE_SIZE << order;
1529+
req->r_num_caps = num_entries + 1;
1530+
req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1531+
req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1532+
return 0;
1533+
}
1534+
14991535
/*
15001536
* Create an mds request.
15011537
*/

‎fs/ceph/mds_client.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct ceph_mds_reply_info_parsed {
6767
/* for readdir results */
6868
struct {
6969
struct ceph_mds_reply_dirfrag *dir_dir;
70+
size_t dir_buf_size;
7071
int dir_nr;
7172
char **dir_dname;
7273
u32 *dir_dname_len;
@@ -346,7 +347,8 @@ extern void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc,
346347
struct dentry *dn);
347348

348349
extern void ceph_invalidate_dir_request(struct ceph_mds_request *req);
349-
350+
extern int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
351+
struct inode *dir);
350352
extern struct ceph_mds_request *
351353
ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode);
352354
extern void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,

0 commit comments

Comments
 (0)