Whamcloud - gitweb
85083f1810329ebf932d39fb55baf072127c14a0
[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 <lustre/lustre_idl.h>
42 #include "lustreapi_internal.h"
43
44 /*
45  * Give file access advices
46  *
47  * \param fd       File to give advice on.
48  * \param ladvise  Advice to give.
49  *
50  * \retval 0 on success.
51  * \retval -errno on failure.
52  */
53 int llapi_ladvise(int fd, unsigned long long flags, int num_advise,
54                   struct lu_ladvise *ladvise)
55 {
56         int rc;
57         struct ladvise_hdr *ladvise_hdr;
58
59         if (num_advise < 1 || num_advise >= LAH_COUNT_MAX) {
60                 errno = EINVAL;
61                 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
62                             "bad advice number %d", num_advise);
63                 return -1;
64         }
65
66         ladvise_hdr = calloc(1, offsetof(typeof(*ladvise_hdr),
67                              lah_advise[num_advise]));
68         if (ladvise_hdr == NULL) {
69                 errno = ENOMEM;
70                 llapi_error(LLAPI_MSG_ERROR, -ENOMEM, "not enough memory");
71                 return -1;
72         }
73         ladvise_hdr->lah_magic = LADVISE_MAGIC;
74         ladvise_hdr->lah_count = num_advise;
75         ladvise_hdr->lah_flags = flags & LF_MASK;
76         memcpy(ladvise_hdr->lah_advise, ladvise, sizeof(*ladvise) * num_advise);
77
78         rc = ioctl(fd, LL_IOC_LADVISE, ladvise_hdr);
79         if (rc < 0) {
80                 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot give advice");
81                 return -1;
82         }
83         return 0;
84 }
85