From 81cbbfd1d870bea49b8aafe7bebb9e8251190918 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Sat, 4 Aug 2018 09:51:26 +0300 Subject: Multiple files: calloc -> malloc xlators/storage/posix/src/posix-inode-fd-ops.c: xlators/storage/posix/src/posix-helpers.c: xlators/storage/bd/src/bd.c: xlators/protocol/client/src/client-lk.c: xlators/performance/quick-read/src/quick-read.c: xlators/performance/io-cache/src/page.c xlators/nfs/server/src/nfs3-helpers.c xlators/nfs/server/src/nfs-fops.c xlators/nfs/server/src/mount3udp_svc.c xlators/nfs/server/src/mount3.c xlators/mount/fuse/src/fuse-helpers.c xlators/mount/fuse/src/fuse-bridge.c xlators/mgmt/glusterd/src/glusterd-utils.c xlators/mgmt/glusterd/src/glusterd-syncop.h xlators/mgmt/glusterd/src/glusterd-snapshot.c xlators/mgmt/glusterd/src/glusterd-rpc-ops.c xlators/mgmt/glusterd/src/glusterd-replace-brick.c xlators/mgmt/glusterd/src/glusterd-op-sm.c xlators/mgmt/glusterd/src/glusterd-mgmt.c xlators/meta/src/subvolumes-dir.c xlators/meta/src/graph-dir.c xlators/features/trash/src/trash.c xlators/features/shard/src/shard.h xlators/features/shard/src/shard.c xlators/features/marker/src/marker-quota.c xlators/features/locks/src/common.c xlators/features/leases/src/leases-internal.c xlators/features/gfid-access/src/gfid-access.c xlators/features/cloudsync/src/cloudsync-plugins/src/cloudsyncs3/src/libcloudsyncs3.c xlators/features/bit-rot/src/bitd/bit-rot.c xlators/features/bit-rot/src/bitd/bit-rot-scrub.c bxlators/encryption/crypt/src/metadata.c xlators/encryption/crypt/src/crypt.c xlators/performance/md-cache/src/md-cache.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible It doesn't make sense to calloc (allocate and clear) memory when the code right away fills that memory with data. It may be optimized by the compiler, or have a microscopic performance improvement. In some cases, also changed allocation size to be sizeof some struct or type instead of a pointer - easier to read. In some cases, removed redundant strlen() calls by saving the result into a variable. 1. Only done for the straightforward cases. There's room for improvement. 2. Please review carefully, especially for string allocation, with the terminating NULL string. Only compile-tested! .. and allocate memory as much as needed. xlators/nfs/server/src/mount3.c : Don't blindly allocate PATH_MAX, but strlen() the string and allocate appropriately. Also, align error messges. updates: bz#1193929 Original-Author: Yaniv Kaul Signed-off-by: Yaniv Kaul Signed-off-by: Yaniv Kaul Change-Id: Ibda6f33dd180b7f7694f20a12af1e9576fe197f5 --- xlators/nfs/server/src/mount3.c | 44 +++++++++++++++++++--------------- xlators/nfs/server/src/mount3udp_svc.c | 2 +- xlators/nfs/server/src/nfs-fops.c | 2 +- xlators/nfs/server/src/nfs3-helpers.c | 14 +++++++---- 4 files changed, 36 insertions(+), 26 deletions(-) (limited to 'xlators/nfs/server') diff --git a/xlators/nfs/server/src/mount3.c b/xlators/nfs/server/src/mount3.c index 187c975081d..f3d7def64d0 100644 --- a/xlators/nfs/server/src/mount3.c +++ b/xlators/nfs/server/src/mount3.c @@ -761,6 +761,7 @@ mnt3svc_lookup_mount_cbk (call_frame_t *frame, void *cookie, char *path = NULL; uuid_t mountid = {1, }; char fhstr[1536]; + int alloclen = 0; req = (rpcsvc_request_t *)frame->local; @@ -786,14 +787,16 @@ mnt3svc_lookup_mount_cbk (call_frame_t *frame, void *cookie, if (status != MNT3_OK) goto xmit_res; - path = GF_CALLOC (PATH_MAX, sizeof (char), gf_nfs_mt_char); + alloclen = strlen(mntxl->name) + 2; + path = GF_MALLOC (alloclen, gf_nfs_mt_char); if (!path) { - gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY, - "Out of memory"); + gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, + NFS_MSG_NO_MEMORY, + "Memory allocation failed."); goto xmit_res; } - snprintf (path, PATH_MAX, "/%s", mntxl->name); + snprintf (path, alloclen, "/%s", mntxl->name); mnt3svc_update_mountlist (ms, req, path, NULL); GF_FREE (path); if (gf_nfs_dvm_off (nfs_state (ms->nfsx))) { @@ -1148,18 +1151,19 @@ mnt3_resolve_subdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this, nfs3_fh_build_child_fh (&mres->parentfh, buf, &fh); if (strlen (mres->remainingdir) <= 0) { - size_t alloclen; + int alloclen; op_ret = -1; mntstat = MNT3_OK; /* Construct the full path */ + int resolveloc_path_len = strlen(mres->resolveloc.path); alloclen = strlen (mres->exp->expname) + - strlen (mres->resolveloc.path) + 1; - mres->exp->fullpath = GF_CALLOC (alloclen, sizeof (char), - gf_nfs_mt_char); + resolveloc_path_len + 1; + mres->exp->fullpath = GF_MALLOC (alloclen, gf_nfs_mt_char); if (!mres->exp->fullpath) { - gf_msg (GF_MNT, GF_LOG_CRITICAL, ENOMEM, - NFS_MSG_NO_MEMORY, "Allocation failed."); + gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, + NFS_MSG_NO_MEMORY, + "Memory allocation failed."); goto err; } snprintf (mres->exp->fullpath, alloclen, "%s%s", @@ -1178,7 +1182,9 @@ mnt3_resolve_subdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this, goto err; } - path = GF_CALLOC (PATH_MAX, sizeof (char), gf_nfs_mt_char); + alloclen = strlen (mres->exp->vol->name) + + resolveloc_path_len + 2; + path = GF_MALLOC (alloclen, gf_nfs_mt_char); if (!path) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY, @@ -1190,7 +1196,7 @@ mnt3_resolve_subdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this, */ __mnt3_build_mountid_from_path (authorized_path, fh.mountid); - snprintf (path, PATH_MAX, "/%s%s", mres->exp->vol->name, + snprintf (path, alloclen, "/%s%s", mres->exp->vol->name, mres->resolveloc.path); mnt3svc_update_mountlist (mres->mstate, mres->req, @@ -1293,12 +1299,12 @@ mnt3_readlink_cbk (call_frame_t *frame, void *cookie, xlator_t *this, /* Building the actual mount path to be mounted */ path_len = strlen (mres->exp->vol->name) + strlen (absolute_path) + strlen (mres->remainingdir) + 1; - real_loc = GF_CALLOC (1, path_len, gf_nfs_mt_char); + real_loc = GF_MALLOC (path_len, gf_nfs_mt_char); if (!real_loc) { ret = -ENOMEM; goto mnterr; } - sprintf (real_loc , "%s%s", mres->exp->vol->name, absolute_path); + snprintf (real_loc, path_len, "%s%s", mres->exp->vol->name, absolute_path); gf_path_strip_trailing_slashes (real_loc); /* There may entries after symlink in the mount path, @@ -2314,7 +2320,7 @@ __build_mountlist (struct mount3_state *ms, int *count) if (!first) first = mlist; - mlist->ml_directory = GF_CALLOC (namelen + 2, sizeof (char), + mlist->ml_directory = GF_MALLOC (namelen + 2, gf_nfs_mt_char); if (!mlist->ml_directory) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, @@ -2325,7 +2331,7 @@ __build_mountlist (struct mount3_state *ms, int *count) strcpy (mlist->ml_directory, me->exname); namelen = strlen (me->hostname); - mlist->ml_hostname = GF_CALLOC (namelen + 2, sizeof (char), + mlist->ml_hostname = GF_MALLOC (namelen + 2, gf_nfs_mt_char); if (!mlist->ml_hostname) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, @@ -2654,7 +2660,6 @@ mnt3_xlchildren_to_exports (rpcsvc_t *svc, struct mount3_state *ms) if (!nfs_subvolume_started (nfs, ent->vol)) continue; - namelen = strlen (ent->expname) + 1; elist = GF_CALLOC (1, sizeof (*elist), gf_nfs_mt_exportnode); if (!elist) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, @@ -2663,7 +2668,8 @@ mnt3_xlchildren_to_exports (rpcsvc_t *svc, struct mount3_state *ms) } if (!first) first = elist; - elist->ex_dir = GF_CALLOC (namelen + 2, sizeof (char), + namelen = strlen (ent->expname); + elist->ex_dir = GF_MALLOC (namelen + 2, gf_nfs_mt_char); if (!elist->ex_dir) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, @@ -3324,7 +3330,7 @@ mnt3_init_export_ent (struct mount3_state *ms, xlator_t *xl, char *exportpath, else alloclen = strlen (xl->name) + 2; - exp->expname = GF_CALLOC (alloclen, sizeof (char), gf_nfs_mt_char); + exp->expname = GF_MALLOC (alloclen, gf_nfs_mt_char); if (!exp->expname) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY, "Memory allocation failed"); diff --git a/xlators/nfs/server/src/mount3udp_svc.c b/xlators/nfs/server/src/mount3udp_svc.c index 8256a5970bb..a9499bf0038 100644 --- a/xlators/nfs/server/src/mount3udp_svc.c +++ b/xlators/nfs/server/src/mount3udp_svc.c @@ -110,7 +110,7 @@ mountudpproc3_umnt_3_svc(dirpath **dp, struct svc_req *req) char *mpath = (char *) *dp; xlator_t *nfsx = THIS; - stat = GF_CALLOC (1, sizeof(mountstat3), gf_nfs_mt_mountstat3); + stat = GF_MALLOC (sizeof(mountstat3), gf_nfs_mt_mountstat3); if (stat == NULL) { gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY, "Unable to allocate memory"); diff --git a/xlators/nfs/server/src/nfs-fops.c b/xlators/nfs/server/src/nfs-fops.c index 7c2aab53573..44141001361 100644 --- a/xlators/nfs/server/src/nfs-fops.c +++ b/xlators/nfs/server/src/nfs-fops.c @@ -334,7 +334,7 @@ nfs_gfid_dict (inode_t *inode) int ret = -1; uuid_t rootgfid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; - dyngfid = GF_CALLOC (1, sizeof (uuid_t), gf_common_mt_char); + dyngfid = GF_MALLOC (sizeof (uuid_t), gf_common_mt_char); if (dyngfid == NULL) return (NULL); diff --git a/xlators/nfs/server/src/nfs3-helpers.c b/xlators/nfs/server/src/nfs3-helpers.c index 0b977092fbb..27edf471db2 100644 --- a/xlators/nfs/server/src/nfs3-helpers.c +++ b/xlators/nfs/server/src/nfs3-helpers.c @@ -673,6 +673,7 @@ entry3 * nfs3_fill_entry3 (gf_dirent_t *entry, struct nfs3_fh *dfh) { entry3 *ent = NULL; + int name_len = 0; if ((!entry) || (!dfh)) return NULL; @@ -692,14 +693,15 @@ nfs3_fill_entry3 (gf_dirent_t *entry, struct nfs3_fh *dfh) nfs3_funge_root_dotdot_dirent (entry, dfh); ent->fileid = entry->d_ino; ent->cookie = entry->d_off; - ent->name = GF_CALLOC ((strlen (entry->d_name) + 1), sizeof (char), - gf_nfs_mt_char); + name_len = strlen(entry->d_name); + ent->name = GF_MALLOC (name_len + 1, gf_nfs_mt_char); if (!ent->name) { GF_FREE (ent); ent = NULL; goto err; } strcpy (ent->name, entry->d_name); + ent->name[name_len] = '\0'; err: return ent; @@ -732,7 +734,7 @@ nfs3_fh_to_post_op_fh3 (struct nfs3_fh *fh) pfh.handle_follows = 1; - fhp = GF_CALLOC (1, sizeof (*fh), gf_nfs_mt_char); + fhp = GF_MALLOC (sizeof (*fh), gf_nfs_mt_char); if (!fhp) return pfh; @@ -747,6 +749,7 @@ nfs3_fill_entryp3 (gf_dirent_t *entry, struct nfs3_fh *dirfh, uint64_t devid) { entryp3 *ent = NULL; struct nfs3_fh newfh = {{0}, }; + int name_len = 0; if ((!entry) || (!dirfh)) return NULL; @@ -767,14 +770,15 @@ nfs3_fill_entryp3 (gf_dirent_t *entry, struct nfs3_fh *dirfh, uint64_t devid) ent->fileid = entry->d_ino; ent->cookie = entry->d_off; - ent->name = GF_CALLOC ((strlen (entry->d_name) + 1), sizeof (char), - gf_nfs_mt_char); + name_len = strlen (entry->d_name); + ent->name = GF_MALLOC (name_len + 1, gf_nfs_mt_char); if (!ent->name) { GF_FREE (ent); ent = NULL; goto err; } strcpy (ent->name, entry->d_name); + ent->name[name_len] = '\0'; nfs3_fh_build_child_fh (dirfh, &entry->d_stat, &newfh); nfs3_map_deviceid_to_statdev (&entry->d_stat, devid); -- cgit