1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Viresh Kumar <viresh.kumar@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci#ifndef LAPI_NAME_TO_HANDLE_AT_H__
8f08c3bdfSopenharmony_ci#define LAPI_NAME_TO_HANDLE_AT_H__
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <sys/syscall.h>
11f08c3bdfSopenharmony_ci#include "config.h"
12f08c3bdfSopenharmony_ci#include "tst_test.h"
13f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
14f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
15f08c3bdfSopenharmony_ci#include "tst_buffers.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#ifndef HAVE_NAME_TO_HANDLE_AT
18f08c3bdfSopenharmony_cistatic inline int name_to_handle_at(int dfd, const char *pathname,
19f08c3bdfSopenharmony_ci                                    struct file_handle *handle,
20f08c3bdfSopenharmony_ci                                    int *mount_id, int flags)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	return tst_syscall(__NR_name_to_handle_at, dfd, pathname, handle,
23f08c3bdfSopenharmony_ci			   mount_id, flags);
24f08c3bdfSopenharmony_ci}
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic inline int open_by_handle_at(int mount_fd, struct file_handle *handle,
27f08c3bdfSopenharmony_ci                                    int flags)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	return tst_syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
30f08c3bdfSopenharmony_ci}
31f08c3bdfSopenharmony_ci#endif /* HAVE_NAME_TO_HANDLE_AT */
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci/* Returns a valid pointer on success, NULL on errors */
34f08c3bdfSopenharmony_cistatic inline struct file_handle *
35f08c3bdfSopenharmony_ciallocate_file_handle(int dfd, const char *pathname)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	long ret;
38f08c3bdfSopenharmony_ci	struct file_handle fh = {}, *fhp;
39f08c3bdfSopenharmony_ci	int mount_id;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	/*
42f08c3bdfSopenharmony_ci	 * Make an initial call to name_to_handle_at() to discover the size
43f08c3bdfSopenharmony_ci	 * required for the file handle.
44f08c3bdfSopenharmony_ci	 */
45f08c3bdfSopenharmony_ci	ret = name_to_handle_at(dfd, pathname, &fh, &mount_id, 0);
46f08c3bdfSopenharmony_ci	if (ret != -1 || errno != EOVERFLOW) {
47f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO,
48f08c3bdfSopenharmony_ci			"name_to_handle_at() should fail with EOVERFLOW");
49f08c3bdfSopenharmony_ci		return NULL;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	/* Valid file handle */
53f08c3bdfSopenharmony_ci	fhp = tst_alloc(sizeof(*fhp) + fh.handle_bytes);
54f08c3bdfSopenharmony_ci	fhp->handle_type = fh.handle_type;
55f08c3bdfSopenharmony_ci	fhp->handle_bytes = fh.handle_bytes;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	return fhp;
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci#endif /* LAPI_NAME_TO_HANDLE_AT_H__ */
61