Whamcloud - gitweb
LU-8769 lnet: removal of obsolete LNDs
[fs/lustre-release.git] / lustre / utils / liblustreapi_ladvise.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * (C) Copyright (c) 2015, DataDirect Networks Inc, all rights reserved.
7  *
8  * All rights reserved. This program and the accompanying materials
9  * are made available under the terms of the GNU Lesser General Public License
10  * LGPL version 2.1 or (at your discretion) any later version.
11  * LGPL version 2.1 accompanies this distribution, and is available at
12  * http://www.gnu.org/licenses/lgpl-2.1.html
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * LGPL HEADER END
20  */
21 /*
22  * lustre/utils/liblustreapi_ladvise.c
23  *
24  * lustreapi library for ladvise
25  *
26  * Author: Li Xi <lixi@ddn.com>
27  */
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdbool.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <sys/syscall.h>
37 #include <sys/ioctl.h>
38 #include <errno.h>
39
40 #include <lustre/lustreapi.h>
41 #include "lustreapi_internal.h"
42
43 /*
44  * Give file access advices
45  *
46  * \param fd       File to give advice on.
47  * \param ladvise  Advice to give.
48  *
49  * \retval 0 on success.
50  * \retval -1 on failure, errno set
51  */
52 int llapi_ladvise(int fd, unsigned long long flags, int num_advise,
53                   struct llapi_lu_ladvise *ladvise)
54 {
55         int rc;
56         struct llapi_ladvise_hdr *ladvise_hdr;
57
58         if (num_advise < 1 || num_advise >= LAH_COUNT_MAX) {
59                 errno = EINVAL;
60                 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
61                             "bad advice number %d", num_advise);
62                 return -1;
63         }
64
65         ladvise_hdr = calloc(1, offsetof(typeof(*ladvise_hdr),
66                              lah_advise[num_advise]));
67         if (ladvise_hdr == NULL) {
68                 errno = ENOMEM;
69                 llapi_error(LLAPI_MSG_ERROR, -ENOMEM, "not enough memory");
70                 return -1;
71         }
72         ladvise_hdr->lah_magic = LADVISE_MAGIC;
73         ladvise_hdr->lah_count = num_advise;
74         ladvise_hdr->lah_flags = flags & LF_MASK;
75         memcpy(ladvise_hdr->lah_advise, ladvise, sizeof(*ladvise) * num_advise);
76
77         rc = ioctl(fd, LL_IOC_LADVISE, ladvise_hdr);
78         if (rc < 0) {
79                 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot give advice");
80                 return -1;
81         }
82         return 0;
83 }
84