Whamcloud - gitweb
LU-11546 utils: enable large_dir for ldiskfs
[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         struct llapi_ladvise_hdr *ladvise_hdr;
56         int rc;
57         int i;
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
84         /* Copy results back in to caller provided structs */
85         for (i = 0; i < num_advise; i++) {
86                 struct llapi_lu_ladvise *ladvise_iter;
87
88                 ladvise_iter = &ladvise_hdr->lah_advise[i];
89
90                 if (ladvise_iter->lla_advice == LU_LADVISE_LOCKAHEAD)
91                         ladvise[i].lla_lockahead_result =
92                                         ladvise_iter->lla_lockahead_result;
93         }
94
95         return 0;
96 }
97