diff options
author | JP Abgrall <jpa@google.com> | 2014-03-19 19:08:39 -0700 |
---|---|---|
committer | JP Abgrall <jpa@google.com> | 2014-03-19 19:08:39 -0700 |
commit | e0ed7404719a9ddd2ba427a80db5365c8bad18c0 (patch) | |
tree | 115ce4b0113994aa23ea22ae0c3ba7587b2eaeb3 /contrib | |
parent | 893d0a0f84a0a9dc7cc37507f974f6a695af465f (diff) | |
download | android_external_e2fsprogs-e0ed7404719a9ddd2ba427a80db5365c8bad18c0.tar.gz android_external_e2fsprogs-e0ed7404719a9ddd2ba427a80db5365c8bad18c0.tar.bz2 android_external_e2fsprogs-e0ed7404719a9ddd2ba427a80db5365c8bad18c0.zip |
Switch back to 1.42.9 now that there is a fix
Revert "Revert changes that moved from 1.41.14 to 1.42.9"
This reverts commit 65f0aab98b20b5994a726ab90d355248bcddfffd.
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/fallocate.c | 174 | ||||
-rwxr-xr-x | contrib/populate-extfs.sh | 105 | ||||
-rw-r--r-- | contrib/spd_readdir.c | 458 |
3 files changed, 737 insertions, 0 deletions
diff --git a/contrib/fallocate.c b/contrib/fallocate.c new file mode 100644 index 00000000..1f9b59ad --- /dev/null +++ b/contrib/fallocate.c @@ -0,0 +1,174 @@ +/* + * fallocate - utility to use the fallocate system call + * + * Copyright (C) 2008 Red Hat, Inc. All rights reserved. + * Written by Eric Sandeen <sandeen@redhat.com> + * + * cvtnum routine taken from xfsprogs, + * Copyright (c) 2003-2005 Silicon Graphics, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define _LARGEFILE_SOURCE +#define _LARGEFILE64_SOURCE + +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <ctype.h> + +// #include <linux/falloc.h> +#define FALLOC_FL_KEEP_SIZE 0x01 +#define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */ + +void usage(void) +{ + printf("Usage: fallocate [-npt] [-o offset] -l length filename\n"); + exit(EXIT_FAILURE); +} + +#define EXABYTES(x) ((long long)(x) << 60) +#define PETABYTES(x) ((long long)(x) << 50) +#define TERABYTES(x) ((long long)(x) << 40) +#define GIGABYTES(x) ((long long)(x) << 30) +#define MEGABYTES(x) ((long long)(x) << 20) +#define KILOBYTES(x) ((long long)(x) << 10) + +long long +cvtnum(char *s) +{ + long long i; + char *sp; + int c; + + i = strtoll(s, &sp, 0); + if (i == 0 && sp == s) + return -1LL; + if (*sp == '\0') + return i; + if (sp[1] != '\0') + return -1LL; + + c = tolower(*sp); + switch (c) { + case 'k': + return KILOBYTES(i); + case 'm': + return MEGABYTES(i); + case 'g': + return GIGABYTES(i); + case 't': + return TERABYTES(i); + case 'p': + return PETABYTES(i); + case 'e': + return EXABYTES(i); + } + + return -1LL; +} + +int main(int argc, char **argv) +{ + int fd; + char *fname; + int opt; + loff_t length = -2LL; + loff_t offset = 0; + int falloc_mode = 0; + int error; + int tflag = 0; + + while ((opt = getopt(argc, argv, "npl:o:t")) != -1) { + switch(opt) { + case 'n': + /* do not change filesize */ + falloc_mode = FALLOC_FL_KEEP_SIZE; + break; + case 'p': + /* punch mode */ + falloc_mode = (FALLOC_FL_PUNCH_HOLE | + FALLOC_FL_KEEP_SIZE); + break; + case 'l': + length = cvtnum(optarg); + break; + case 'o': + offset = cvtnum(optarg); + break; + case 't': + tflag++; + break; + default: + usage(); + } + } + + if (length == -2LL) { + printf("Error: no length argument specified\n"); + usage(); + } + + if (length <= 0) { + printf("Error: invalid length value specified\n"); + usage(); + } + + if (offset < 0) { + printf("Error: invalid offset value specified\n"); + usage(); + } + + if (tflag && (falloc_mode & FALLOC_FL_KEEP_SIZE)) { + printf("-n and -t options incompatible\n"); + usage(); + } + + if (tflag && offset) { + printf("-n and -o options incompatible\n"); + usage(); + } + + if (optind == argc) { + printf("Error: no filename specified\n"); + usage(); + } + + fname = argv[optind++]; + + /* Should we create the file if it doesn't already exist? */ + fd = open(fname, O_WRONLY|O_LARGEFILE); + if (fd < 0) { + perror("Error opening file"); + exit(EXIT_FAILURE); + } + + if (tflag) + error = ftruncate(fd, length); + else + error = syscall(SYS_fallocate, fd, falloc_mode, offset, length); + + if (error < 0) { + perror("fallocate failed"); + exit(EXIT_FAILURE); + } + + close(fd); + return 0; +} diff --git a/contrib/populate-extfs.sh b/contrib/populate-extfs.sh new file mode 100755 index 00000000..b1d3d1f8 --- /dev/null +++ b/contrib/populate-extfs.sh @@ -0,0 +1,105 @@ +#!/bin/sh +# +# This script uses debugfs command to populate the ext2/3/4 filesystem +# from a given directory. +# + +do_usage () { + cat << _EOF +Usage: populate-extfs.sh <source> <device> +Create an ext2/ext3/ext4 filesystem from a directory or file + + source: The source directory or file + device: The target device + +_EOF + exit 1 +} + +[ $# -ne 2 ] && do_usage + +SRCDIR=${1%%/} +DEVICE=$2 + +# Find where is the debugfs command if not found in the env. +if [ -z "$DEBUGFS" ]; then + CONTRIB_DIR=$(dirname $(readlink -f $0)) + DEBUGFS="$CONTRIB_DIR/../debugfs/debugfs" +fi + +{ + CWD="/" + find $SRCDIR | while read FILE; do + TGT="${FILE##*/}" + DIR="${FILE#$SRCDIR}" + DIR="${DIR%$TGT}" + + # Skip the root dir + [ ! -z "$DIR" ] || continue + [ ! -z "$TGT" ] || continue + + if [ "$DIR" != "$CWD" ]; then + echo "cd $DIR" + CWD="$DIR" + fi + + # Only stat once since stat is a time consuming command + STAT=$(stat -c "TYPE=\"%F\";DEVNO=\"0x%t 0x%T\";MODE=\"%f\";U=\"%u\";G=\"%g\"" $FILE) + eval $STAT + + case $TYPE in + "directory") + echo "mkdir $TGT" + ;; + "regular file" | "regular empty file") + echo "write $FILE $TGT" + ;; + "symbolic link") + LINK_TGT=$(readlink $FILE) + echo "symlink $TGT $LINK_TGT" + ;; + "block special file") + echo "mknod $TGT b $DEVNO" + ;; + "character special file") + echo "mknod $TGT c $DEVNO" + ;; + "fifo") + echo "mknod $TGT p" + ;; + *) + echo "Unknown/unhandled file type '$TYPE' file: $FILE" 1>&2 + ;; + esac + + # Set the file mode + echo "sif $TGT mode 0x$MODE" + + # Set uid and gid + echo "sif $TGT uid $U" + echo "sif $TGT gid $G" + done + + # Handle the hard links. + # Save the hard links to a file, use the inode number as the filename, for example: + # If a and b's inode number is 6775928, save a and b to /tmp/tmp.VrCwHh5gdt/6775928. + INODE_DIR=`mktemp -d` || exit 1 + for i in `find $SRCDIR -type f -links +1 -printf 'INODE=%i###FN=%p\n'`; do + eval `echo $i | sed 's$###$ $'` + echo ${FN#$SRCDIR} >>$INODE_DIR/$INODE + done + # Use the debugfs' ln and "sif links_count" to handle them. + for i in `ls $INODE_DIR`; do + # The link source + SRC=`head -1 $INODE_DIR/$i` + # Remove the files and link them again except the first one + for TGT in `sed -n -e '1!p' $INODE_DIR/$i`; do + echo "rm $TGT" + echo "ln $SRC $TGT" + done + LN_CNT=`cat $INODE_DIR/$i | wc -l` + # Set the links count + echo "sif $SRC links_count $LN_CNT" + done + rm -fr $INODE_DIR +} | $DEBUGFS -w -f - $DEVICE diff --git a/contrib/spd_readdir.c b/contrib/spd_readdir.c new file mode 100644 index 00000000..8345fa1a --- /dev/null +++ b/contrib/spd_readdir.c @@ -0,0 +1,458 @@ +/* + * readdir accelerator + * + * (C) Copyright 2003, 2004, 2008 by Theodore Ts'o. + * + * 2008-06-08 Modified by Ross Boylan <RossBoylan stanfordalumni org> + * Added support for readdir_r and readdir64_r calls. Note + * this has not been tested on anything other than GNU/Linux i386, + * and that the regular readdir wrapper will take slightly more + * space than Ted's original since it now includes a lock. + * + * Compile using the command: + * + * gcc -o spd_readdir.so -shared -fpic spd_readdir.c -ldl + * + * Use it by setting the LD_PRELOAD environment variable: + * + * export LD_PRELOAD=/usr/local/sbin/spd_readdir.so + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Public + * License. + * %End-Header% + * + */ + +#define ALLOC_STEPSIZE 100 +#define MAX_DIRSIZE 0 + +#define DEBUG +/* Util we autoconfiscate spd_readdir... */ +#define HAVE___SECURE_GETENV 1 +#define HAVE_PRCTL 1 +#define HAVE_SYS_PRCTL_H 1 + +#ifdef DEBUG +#define DEBUG_DIR(x) {if (do_debug) { x; }} +#else +#define DEBUG_DIR(x) +#endif + +#define _GNU_SOURCE +#define __USE_LARGEFILE64 + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stdlib.h> +#include <string.h> +#include <dirent.h> +#include <errno.h> +#include <dlfcn.h> +#ifdef HAVE_SYS_PRCTL_H +#include <sys/prctl.h> +#else +#define PR_GET_DUMPABLE 3 +#endif +#include <pthread.h> + +struct dirent_s { + unsigned long long d_ino; + long long d_off; + unsigned short int d_reclen; + unsigned char d_type; + char *d_name; +}; + +struct dir_s { + DIR *dir; + pthread_mutex_t lock; /* Mutex lock for this structure. */ + int num; + int max; + struct dirent_s *dp; + int pos; + int direct; + struct dirent ret_dir; + struct dirent64 ret_dir64; +}; + +static int (*real_closedir)(DIR *dir) = 0; +static DIR *(*real_opendir)(const char *name) = 0; +static DIR *(*real_fdopendir)(int fd) = 0; +static void *(*real_rewinddir)(DIR *dirp) = 0; +static struct dirent *(*real_readdir)(DIR *dir) = 0; +static int (*real_readdir_r)(DIR *dir, struct dirent *entry, + struct dirent **result) = 0; +static struct dirent64 *(*real_readdir64)(DIR *dir) = 0; +static int (*real_readdir64_r)(DIR *dir, struct dirent64 *entry, + struct dirent64 **result) = 0; +static off_t (*real_telldir)(DIR *dir) = 0; +static void (*real_seekdir)(DIR *dir, off_t offset) = 0; +static int (*real_dirfd)(DIR *dir) = 0; +static unsigned long max_dirsize = MAX_DIRSIZE; +static int num_open = 0; +#ifdef DEBUG +static int do_debug = 0; +#endif + +static char *safe_getenv(const char *arg) +{ + if ((getuid() != geteuid()) || (getgid() != getegid())) + return NULL; +#if HAVE_PRCTL + if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) + return NULL; +#else +#if (defined(linux) && defined(SYS_prctl)) + if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) + return NULL; +#endif +#endif + +#if HAVE___SECURE_GETENV + return __secure_getenv(arg); +#else + return getenv(arg); +#endif +} + +static void setup_ptr() +{ + char *cp; + + real_opendir = dlsym(RTLD_NEXT, "opendir"); + real_fdopendir = dlsym(RTLD_NEXT, "fdopendir"); + real_closedir = dlsym(RTLD_NEXT, "closedir"); + real_rewinddir = dlsym(RTLD_NEXT, "rewinddir"); + real_readdir = dlsym(RTLD_NEXT, "readdir"); + real_readdir_r = dlsym(RTLD_NEXT, "readdir_r"); + real_readdir64 = dlsym(RTLD_NEXT, "readdir64"); + real_readdir64_r = dlsym(RTLD_NEXT, "readdir64_r"); + real_telldir = dlsym(RTLD_NEXT, "telldir"); + real_seekdir = dlsym(RTLD_NEXT, "seekdir"); + real_dirfd = dlsym(RTLD_NEXT, "dirfd"); + if ((cp = safe_getenv("SPD_READDIR_MAX_SIZE")) != NULL) { + max_dirsize = atol(cp); + } +#ifdef DEBUG + if (safe_getenv("SPD_READDIR_DEBUG")) { + printf("initialized!\n"); + do_debug++; + } +#endif +} + +static void free_cached_dir(struct dir_s *dirstruct) +{ + int i; + + pthread_mutex_destroy(&(dirstruct->lock)); + + if (!dirstruct->dp) + return; + + for (i=0; i < dirstruct->num; i++) { + free(dirstruct->dp[i].d_name); + } + free(dirstruct->dp); + dirstruct->dp = 0; + dirstruct->max = dirstruct->num = 0; +} + +static int ino_cmp(const void *a, const void *b) +{ + const struct dirent_s *ds_a = (const struct dirent_s *) a; + const struct dirent_s *ds_b = (const struct dirent_s *) b; + ino_t i_a, i_b; + + i_a = ds_a->d_ino; + i_b = ds_b->d_ino; + + if (ds_a->d_name[0] == '.') { + if (ds_a->d_name[1] == 0) + i_a = 0; + else if ((ds_a->d_name[1] == '.') && (ds_a->d_name[2] == 0)) + i_a = 1; + } + if (ds_b->d_name[0] == '.') { + if (ds_b->d_name[1] == 0) + i_b = 0; + else if ((ds_b->d_name[1] == '.') && (ds_b->d_name[2] == 0)) + i_b = 1; + } + + return (i_a - i_b); +} + +static struct dir_s *alloc_dirstruct(DIR *dir) +{ + struct dir_s *dirstruct; + static pthread_mutexattr_t mutexattr; + mutexattr.__align = PTHREAD_MUTEX_RECURSIVE; + + dirstruct = malloc(sizeof(struct dir_s)); + if (dirstruct) + memset(dirstruct, 0, sizeof(struct dir_s)); + dirstruct->dir = dir; + pthread_mutex_init(&(dirstruct->lock), &mutexattr); + return dirstruct; +} + +static void cache_dirstruct(struct dir_s *dirstruct) +{ + struct dirent_s *ds, *dnew; + struct dirent64 *d; + + while ((d = (*real_readdir64)(dirstruct->dir)) != NULL) { + if (dirstruct->num >= dirstruct->max) { + dirstruct->max += ALLOC_STEPSIZE; + DEBUG_DIR(printf("Reallocating to size %d\n", + dirstruct->max)); + dnew = realloc(dirstruct->dp, + dirstruct->max * sizeof(struct dir_s)); + if (!dnew) + goto nomem; + dirstruct->dp = dnew; + } + ds = &dirstruct->dp[dirstruct->num++]; + ds->d_ino = d->d_ino; + ds->d_off = d->d_off; + ds->d_reclen = d->d_reclen; + ds->d_type = d->d_type; + if ((ds->d_name = malloc(strlen(d->d_name)+1)) == NULL) { + dirstruct->num--; + goto nomem; + } + strcpy(ds->d_name, d->d_name); + DEBUG_DIR(printf("readdir: %lu %s\n", + (unsigned long) d->d_ino, d->d_name)); + } + qsort(dirstruct->dp, dirstruct->num, sizeof(struct dirent_s), ino_cmp); + return; +nomem: + DEBUG_DIR(printf("No memory, backing off to direct readdir\n")); + free_cached_dir(dirstruct); + dirstruct->direct = 1; +} + +DIR *opendir(const char *name) +{ + DIR *dir; + struct dir_s *dirstruct; + struct stat st; + + if (!real_opendir) + setup_ptr(); + + DEBUG_DIR(printf("Opendir(%s) (%d open)\n", name, num_open++)); + dir = (*real_opendir)(name); + if (!dir) + return NULL; + + dirstruct = alloc_dirstruct(dir); + if (!dirstruct) { + (*real_closedir)(dir); + errno = -ENOMEM; + return NULL; + } + + if (max_dirsize && (stat(name, &st) == 0) && + (st.st_size > max_dirsize)) { + DEBUG_DIR(printf("Directory size %ld, using direct readdir\n", + st.st_size)); + dirstruct->direct = 1; + return (DIR *) dirstruct; + } + + cache_dirstruct(dirstruct); + return ((DIR *) dirstruct); +} + +DIR *fdopendir(int fd) +{ + DIR *dir; + struct dir_s *dirstruct; + struct stat st; + + if (!real_fdopendir) + setup_ptr(); + + DEBUG_DIR(printf("fdopendir(%d) (%d open)\n", fd, num_open++)); + dir = (*real_fdopendir)(fd); + if (!dir) + return NULL; + + dirstruct = alloc_dirstruct(dir); + if (!dirstruct) { + (*real_closedir)(dir); + errno = -ENOMEM; + return NULL; + } + + if (max_dirsize && (fstat(fd, &st) == 0) && + (st.st_size > max_dirsize)) { + DEBUG_DIR(printf("Directory size %ld, using direct readdir\n", + st.st_size)); + dirstruct->dir = dir; + dirstruct->direct = 1; + return (DIR *) dirstruct; + } + + cache_dirstruct(dirstruct); + return ((DIR *) dirstruct); +} + +int closedir(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + + DEBUG_DIR(printf("Closedir (%d open)\n", --num_open)); + if (dirstruct->dir) + (*real_closedir)(dirstruct->dir); + + free_cached_dir(dirstruct); + free(dirstruct); + return 0; +} + +struct dirent *readdir(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + struct dirent_s *ds; + + if (dirstruct->direct) + return (*real_readdir)(dirstruct->dir); + + if (dirstruct->pos >= dirstruct->num) + return NULL; + + ds = &dirstruct->dp[dirstruct->pos++]; + dirstruct->ret_dir.d_ino = ds->d_ino; + dirstruct->ret_dir.d_off = ds->d_off; + dirstruct->ret_dir.d_reclen = ds->d_reclen; + dirstruct->ret_dir.d_type = ds->d_type; + strncpy(dirstruct->ret_dir.d_name, ds->d_name, + sizeof(dirstruct->ret_dir.d_name)); + + return (&dirstruct->ret_dir); +} + +int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + struct dirent_s *ds; + + if (dirstruct->direct) + return (*real_readdir_r)(dirstruct->dir, entry, result); + + pthread_mutex_lock(&(dirstruct->lock)); + if (dirstruct->pos >= dirstruct->num) { + *result = NULL; + } else { + ds = &dirstruct->dp[dirstruct->pos++]; + entry->d_ino = ds->d_ino; + entry->d_off = ds->d_off; + entry->d_reclen = ds->d_reclen; + entry->d_type = ds->d_type; + strncpy(entry->d_name, ds->d_name, sizeof(entry->d_name)); + *result = entry; + } + pthread_mutex_unlock(&(dirstruct->lock)); + return 0; +} + +struct dirent64 *readdir64(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + struct dirent_s *ds; + + if (dirstruct->direct) + return (*real_readdir64)(dirstruct->dir); + + if (dirstruct->pos >= dirstruct->num) + return NULL; + + ds = &dirstruct->dp[dirstruct->pos++]; + dirstruct->ret_dir64.d_ino = ds->d_ino; + dirstruct->ret_dir64.d_off = ds->d_off; + dirstruct->ret_dir64.d_reclen = ds->d_reclen; + dirstruct->ret_dir64.d_type = ds->d_type; + strncpy(dirstruct->ret_dir64.d_name, ds->d_name, + sizeof(dirstruct->ret_dir64.d_name)); + + return (&dirstruct->ret_dir64); +} + +int readdir64_r (DIR *__restrict dir, + struct dirent64 *__restrict entry, + struct dirent64 **__restrict result) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + struct dirent_s *ds; + + if (dirstruct->direct) + return (*real_readdir64_r)(dir, entry, result); + pthread_mutex_lock(&(dirstruct->lock)); + if (dirstruct->pos >= dirstruct->num) { + *result = NULL; + } else { + ds = &dirstruct->dp[dirstruct->pos++]; + entry->d_ino = ds->d_ino; + entry->d_off = ds->d_off; + entry->d_reclen = ds->d_reclen; + entry->d_type = ds->d_type; + strncpy(entry->d_name, ds->d_name, + sizeof(entry->d_name)); + *result = entry; + } + pthread_mutex_unlock(&(dirstruct->lock)); + return 0; +} + +off_t telldir(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + + if (dirstruct->direct) + return (*real_telldir)(dirstruct->dir); + + return ((off_t) dirstruct->pos); +} + +void seekdir(DIR *dir, off_t offset) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + + if (dirstruct->direct) { + (*real_seekdir)(dirstruct->dir, offset); + return; + } + + dirstruct->pos = offset; +} + +void rewinddir(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + + (*real_rewinddir)(dirstruct->dir); + if (dirstruct->direct) + return; + + pthread_mutex_lock(&(dirstruct->lock)); + dirstruct->pos = 0; + free_cached_dir(dirstruct); + cache_dirstruct(dirstruct); + pthread_mutex_unlock(&(dirstruct->lock)); +} + +int dirfd(DIR *dir) +{ + struct dir_s *dirstruct = (struct dir_s *) dir; + int fd = (*real_dirfd)(dirstruct->dir); + + DEBUG_DIR(printf("dirfd %d, %p\n", fd, real_dirfd)); + return fd; +} |