fsstress: add operation for reading xattrs from files and directories
[xfstests-dev.git] / ltp / fsstress.c
index b3ff8d92abf943dd5e0cdcd78c95c74686c27e44..f24dd42d597baf13d304a493aab0ca007e5547d2 100644 (file)
@@ -69,6 +69,7 @@ typedef enum {
        OP_FSYNC,
        OP_GETATTR,
        OP_GETDENTS,
+       OP_GETFATTR,
        OP_LINK,
        OP_MKDIR,
        OP_MKNOD,
@@ -184,6 +185,7 @@ void        fsync_f(int, long);
 char   *gen_random_string(int);
 void   getattr_f(int, long);
 void   getdents_f(int, long);
+void   getfattr_f(int, long);
 void   link_f(int, long);
 void   mkdir_f(int, long);
 void   mknod_f(int, long);
@@ -237,6 +239,8 @@ opdesc_t    ops[] = {
        { OP_FSYNC, "fsync", fsync_f, 1, 1 },
        { OP_GETATTR, "getattr", getattr_f, 1, 0 },
        { OP_GETDENTS, "getdents", getdents_f, 1, 0 },
+       /* get extended attribute */
+       { OP_GETFATTR, "getfattr", getfattr_f, 1, 0 },
        { OP_LINK, "link", link_f, 1, 1 },
        { OP_MKDIR, "mkdir", mkdir_f, 2, 1 },
        { OP_MKNOD, "mknod", mknod_f, 2, 1 },
@@ -3593,6 +3597,75 @@ getdents_f(int opno, long r)
        closedir(dir);
 }
 
+void
+getfattr_f(int opno, long r)
+{
+       fent_t          *fep;
+       int             e;
+       pathname_t      f;
+       int             v;
+       char            name[XATTR_NAME_BUF_SIZE];
+       char            *value = NULL;
+       int             value_len;
+       int             xattr_num;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE | FT_DIRm, r, &f, NULL, &fep, &v)) {
+               if (v)
+                       printf("%d/%d: getfattr - no filename\n", procid, opno);
+               goto out;
+       }
+       check_cwd();
+
+       /*
+        * If the file/dir has xattrs, pick one randomly, otherwise attempt
+        * to read a xattr that doesn't exist (fgetxattr should fail with
+        * errno set to ENOATTR (61) in this case).
+        */
+       if (fep->xattr_counter > 0)
+               xattr_num = (random() % fep->xattr_counter) + 1;
+       else
+               xattr_num = 0;
+
+       e = generate_xattr_name(xattr_num, name, sizeof(name));
+       if (e < 0) {
+               printf("%d/%d: getfattr - file %s failed to generate xattr name: %d\n",
+                      procid, opno, f.path, e);
+               goto out;
+       }
+
+       value_len = getxattr(f.path, name, NULL, 0);
+       if (value_len < 0) {
+               if (v)
+                       printf("%d/%d: getfattr file %s name %s failed %d\n",
+                              procid, opno, f.path, name, errno);
+               goto out;
+       }
+
+       /* A xattr without value.*/
+       if (value_len == 0) {
+               e = 0;
+               goto out_log;
+       }
+
+       value = malloc(value_len);
+       if (!value) {
+               if (v)
+                       printf("%d/%d: getfattr file %s failed to allocate buffer with %d bytes\n",
+                              procid, opno, f.path, value_len);
+               goto out;
+       }
+
+       e = getxattr(f.path, name, value, value_len) < 0 ? errno : 0;
+out_log:
+       if (v)
+               printf("%d/%d: getfattr file %s name %s value length %d %d\n",
+                      procid, opno, f.path, name, value_len, e);
+out:
+       free(value);
+       free_pathname(&f);
+}
+
 void
 link_f(int opno, long r)
 {