"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-xattr.c" (15 Mar 2019, 5475 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-xattr.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.09.55_vs_0.09.56.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #if (defined(HAVE_SYS_XATTR_H) ||   \
   28      defined(HAVE_ATTR_XATTR_H)) && \
   29     defined(HAVE_FGETXATTR) &&      \
   30     defined(HAVE_FLISTXATTR) &&     \
   31     defined(HAVE_FREMOVEXATTR) &&   \
   32     defined(HAVE_FSETXATTR) &&      \
   33     defined(HAVE_GETXATTR) &&       \
   34     defined(HAVE_LISTXATTR) &&      \
   35     defined(HAVE_LGETXATTR) &&      \
   36     defined(HAVE_LSETXATTR) &&      \
   37     defined(HAVE_SETXATTR)
   38 
   39 #define MAX_XATTRS      (4096)
   40 
   41 /*
   42  *  stress_xattr
   43  *  stress the xattr operations
   44  */
   45 static int stress_xattr(const args_t *args)
   46 {
   47     int ret, fd, rc = EXIT_FAILURE;
   48     char filename[PATH_MAX];
   49 
   50     ret = stress_temp_dir_mk_args(args);
   51     if (ret < 0)
   52         return exit_status(-ret);
   53 
   54     (void)stress_temp_filename_args(args, filename, sizeof(filename), mwc32());
   55     if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
   56         rc = exit_status(errno);
   57         pr_fail_err("open");
   58         goto out;
   59     }
   60 
   61     do {
   62         int i, j;
   63         char attrname[32];
   64         char value[32];
   65         ssize_t sz;
   66         char *buffer;
   67 
   68         for (i = 0; i < MAX_XATTRS; i++) {
   69             (void)snprintf(attrname, sizeof(attrname), "user.var_%d", i);
   70             (void)snprintf(value, sizeof(value), "orig-value-%d", i);
   71 
   72             ret = fsetxattr(fd, attrname, value, strlen(value), XATTR_CREATE);
   73             if (ret < 0) {
   74                 if (errno == ENOTSUP) {
   75                     pr_inf("%s stressor will be "
   76                         "skipped, filesystem does not "
   77                         "support xattr.\n", args->name);
   78                 }
   79                 if (errno == ENOSPC || errno == EDQUOT)
   80                     break;
   81                 pr_fail_err("fsetxattr");
   82                 goto out_close;
   83             }
   84         }
   85         for (j = 0; j < i; j++) {
   86             (void)snprintf(attrname, sizeof(attrname), "user.var_%d", j);
   87             (void)snprintf(value, sizeof(value), "value-%d", j);
   88 
   89             ret = fsetxattr(fd, attrname, value, strlen(value),
   90                 XATTR_REPLACE);
   91             if (ret < 0) {
   92                 if (errno == ENOSPC || errno == EDQUOT)
   93                     break;
   94                 pr_fail_err("fsetxattr");
   95                 goto out_close;
   96             }
   97 
   98             /* ..and do it again using setxattr */
   99             ret = setxattr(filename, attrname, value, strlen(value),
  100                 XATTR_REPLACE);
  101             if (ret < 0) {
  102                 if (errno == ENOSPC || errno == EDQUOT)
  103                     break;
  104                 pr_fail_err("setxattr");
  105                 goto out_close;
  106             }
  107 
  108             /* Although not a link, it's good to exercise this call */
  109             ret = lsetxattr(filename, attrname, value, strlen(value),
  110                 XATTR_REPLACE);
  111             if (ret < 0) {
  112                 if (errno == ENOSPC || errno == EDQUOT)
  113                     break;
  114                 pr_fail_err("lsetxattr");
  115                 goto out_close;
  116             }
  117         }
  118         for (j = 0; j < i; j++) {
  119             char tmp[sizeof(value)];
  120 
  121             (void)snprintf(attrname, sizeof(attrname), "user.var_%d", j);
  122             (void)snprintf(value, sizeof(value), "value-%d", j);
  123 
  124             ret = fgetxattr(fd, attrname, tmp, sizeof(tmp));
  125             if (ret < 0) {
  126                 pr_fail_err("fgetxattr");
  127                 goto out_close;
  128             }
  129             if (strncmp(value, tmp, ret)) {
  130                 pr_fail("%s: fgetxattr values "
  131                     "different %.*s vs %.*s\n",
  132                     args->name, ret, value, ret, tmp);
  133                 goto out_close;
  134             }
  135 
  136             ret = shim_getxattr(filename, attrname, tmp, sizeof(tmp));
  137             if (ret < 0) {
  138                 pr_fail_err("getxattr");
  139                 goto out_close;
  140             }
  141             if (strncmp(value, tmp, ret)) {
  142                 pr_fail("%s: getxattr values "
  143                     "different %.*s vs %.*s\n",
  144                     args->name, ret, value, ret, tmp);
  145                 goto out_close;
  146             }
  147 
  148             ret = lgetxattr(filename, attrname, tmp, sizeof(tmp));
  149             if (ret < 0) {
  150                 pr_fail_err("getxattr");
  151                 goto out_close;
  152             }
  153             if (strncmp(value, tmp, ret)) {
  154                 pr_fail("%s: lgetxattr values "
  155                     "different %.*s vs %.*s\n",
  156                     args->name, ret, value, ret, tmp);
  157                 goto out_close;
  158             }
  159         }
  160         /* Determine how large a buffer we required... */
  161         sz = flistxattr(fd, NULL, 0);
  162         if (sz < 0) {
  163             pr_fail_err("flistxattr");
  164             goto out_close;
  165         }
  166         buffer = malloc(sz);
  167         if (buffer) {
  168             /* ...and fetch */
  169             sz = listxattr(filename, buffer, sz);
  170             free(buffer);
  171 
  172             if (sz < 0) {
  173                 pr_fail_err("listxattr");
  174                 goto out_close;
  175             }
  176         }
  177         for (j = 0; j < i; j++) {
  178             (void)snprintf(attrname, sizeof(attrname), "user.var_%d", j);
  179 
  180             ret = fremovexattr(fd, attrname);
  181             if (ret < 0) {
  182                 pr_fail_err("fremovexattr");
  183                 goto out_close;
  184             }
  185         }
  186         inc_counter(args);
  187     } while (keep_stressing());
  188 
  189     rc = EXIT_SUCCESS;
  190 out_close:
  191     (void)close(fd);
  192 out:
  193     (void)unlink(filename);
  194     (void)stress_temp_dir_rm_args(args);
  195     return rc;
  196 }
  197 
  198 stressor_info_t stress_xattr_info = {
  199     .stressor = stress_xattr,
  200     .class = CLASS_FILESYSTEM | CLASS_OS
  201 };
  202 #else
  203 stressor_info_t stress_xattr_info = {
  204     .stressor = stress_not_implemented,
  205     .class = CLASS_FILESYSTEM | CLASS_OS
  206 };
  207 #endif