4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * (C) Copyright (c) 2015, DataDirect Networks Inc, all rights reserved.
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
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.
22 * lustre/utils/liblustreapi_ladvise.c
24 * lustreapi library for ladvise
26 * Author: Li Xi <lixi@ddn.com>
33 #include <sys/types.h>
36 #include <sys/syscall.h>
37 #include <sys/ioctl.h>
40 #include <lustre/lustreapi.h>
41 #include "lustreapi_internal.h"
44 * Give file access advices
46 * \param fd File to give advice on.
47 * \param ladvise Advice to give.
49 * \retval 0 on success.
50 * \retval -1 on failure, errno set
52 int llapi_ladvise(int fd, unsigned long long flags, int num_advise,
53 struct llapi_lu_ladvise *ladvise)
55 struct llapi_ladvise_hdr *ladvise_hdr;
59 if (num_advise < 1 || num_advise >= LAH_COUNT_MAX) {
61 llapi_error(LLAPI_MSG_ERROR, -EINVAL,
62 "bad advice number %d", num_advise);
66 ladvise_hdr = calloc(1, offsetof(typeof(*ladvise_hdr),
67 lah_advise[num_advise]));
68 if (ladvise_hdr == NULL) {
70 llapi_error(LLAPI_MSG_ERROR, -ENOMEM, "not enough memory");
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);
78 rc = ioctl(fd, LL_IOC_LADVISE, ladvise_hdr);
80 llapi_error(LLAPI_MSG_ERROR, -errno, "cannot give advice");
86 /* Copy results back in to caller provided structs */
87 for (i = 0; i < num_advise; i++) {
88 struct llapi_lu_ladvise *ladvise_iter;
90 ladvise_iter = &ladvise_hdr->lah_advise[i];
92 if (ladvise_iter->lla_advice == LU_LADVISE_LOCKAHEAD)
93 ladvise[i].lla_lockahead_result =
94 ladvise_iter->lla_lockahead_result;