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