1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (c) 1995, 2001-2003, 2005 Silicon Graphics, Inc.
8 #include "platform_defs.h"
13 /* just pick a value we know is more than big enough */
17 * The actual content of a handle is supposed to be opaque here.
18 * But, to do handle_to_fshandle, we need to know what it is. Sigh.
19 * However we can get by with knowing only that the first 8 bytes of
20 * a file handle are the file system ID, and that a file system handle
21 * consists of only those 8 bytes.
31 static int obj_to_handle(char *, int, unsigned int, comarg_t
, void**, size_t*);
32 static int handle_to_fsfd(void *, char **);
33 static char *path_to_fspath(char *path
);
37 * Filesystem Handle -> Open File Descriptor Cache
39 * Maps filesystem handles to a corresponding open file descriptor for that
40 * filesystem. We need this because we're doing handle operations via xfsctl
41 * and we need to remember the open file descriptor for each filesystem.
48 char fspath
[MAXPATHLEN
];
51 static struct fdhash
*fdhash_head
= NULL
;
54 fshandle_destroy(void)
57 struct fdhash
*h
= fdhash_head
;
69 char *path
, /* input, path to convert */
70 void **fshanp
, /* output, pointer to data */
71 size_t *fshlen
) /* output, size of returned data */
80 fspath
= path_to_fspath(path
);
84 fd
= open(fspath
, O_RDONLY
);
89 result
= obj_to_handle(fspath
, fd
, XFS_IOC_PATH_TO_FSHANDLE
,
96 if (handle_to_fsfd(*fshanp
, &tmppath
) >= 0) {
97 /* this filesystem is already in the cache */
100 /* new filesystem. add it to the cache */
101 fdhp
= malloc(sizeof(struct fdhash
));
110 strncpy(fdhp
->fspath
, fspath
, sizeof(fdhp
->fspath
));
111 memcpy(fdhp
->fsh
, *fshanp
, FSIDSIZE
);
113 fdhp
->fnxt
= fdhash_head
;
122 char *path
, /* input, path to convert */
123 void **hanp
, /* output, pointer to data */
124 size_t *hlen
) /* output, size of returned data */
131 fspath
= path_to_fspath(path
);
135 fd
= open(fspath
, O_RDONLY
);
140 result
= obj_to_handle(fspath
, fd
, XFS_IOC_PATH_TO_HANDLE
,
146 /* Given a path, return a suitable "fspath" for use in obtaining
147 * an fd for xfsctl calls. For regular files and directories the
148 * input path is sufficient. For other types the parent directory
149 * is used to avoid issues with opening dangling symlinks and
150 * potentially blocking in an open on a named pipe. Also
151 * symlinks to files on other filesystems would be a problem,
152 * since an fd would be obtained for the wrong fs.
155 path_to_fspath(char *path
)
157 static char dirpath
[MAXPATHLEN
];
160 if (lstat(path
, &statbuf
) != 0)
163 if (S_ISREG(statbuf
.st_mode
) || S_ISDIR(statbuf
.st_mode
))
166 strncpy(dirpath
, path
, MAXPATHLEN
);
167 dirpath
[MAXPATHLEN
-1] = '\0';
168 return dirname(dirpath
);
173 int fd
, /* input, file descriptor */
174 void **hanp
, /* output, pointer to data */
175 size_t *hlen
) /* output, size of returned data */
180 return obj_to_handle(NULL
, fd
, XFS_IOC_FD_TO_HANDLE
, obj
, hanp
, hlen
);
191 if (hlen
< FSIDSIZE
) {
195 *fshanp
= malloc(FSIDSIZE
);
196 if (*fshanp
== NULL
) {
201 memcpy(*fshanp
, hanp
, FSIDSIZE
);
206 handle_to_fsfd(void *hanp
, char **path
)
211 * Look in cache for matching fsid field in the handle
212 * (which is at the start of the handle).
213 * When found return the file descriptor and path that
214 * we have in the cache.
216 for (fdhp
= fdhash_head
; fdhp
!= NULL
; fdhp
= fdhp
->fnxt
) {
217 if (memcmp(fdhp
->fsh
, hanp
, FSIDSIZE
) == 0) {
218 *path
= fdhp
->fspath
;
235 char hbuf
[MAXHANSIZ
];
238 xfs_fsop_handlereq_t hreq
;
240 if (opcode
== XFS_IOC_FD_TO_HANDLE
) {
245 hreq
.path
= obj
.path
;
248 hreq
.oflags
= O_LARGEFILE
;
252 hreq
.ohandlen
= &handlen
;
254 ret
= xfsctl(fspath
, fsfd
, opcode
, &hreq
);
258 *hanp
= malloc(handlen
);
264 memcpy(*hanp
, hbuf
, handlen
);
277 xfs_fsop_handlereq_t hreq
;
279 if ((fsfd
= handle_to_fsfd(fshanp
, &path
)) < 0)
284 hreq
.oflags
= rw
| O_LARGEFILE
;
285 hreq
.ihandle
= fshanp
;
286 hreq
.ihandlen
= fshlen
;
288 hreq
.ohandlen
= NULL
;
290 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
301 xfs_fsop_handlereq_t hreq
;
303 if ((fsfd
= handle_to_fsfd(hanp
, &path
)) < 0)
308 hreq
.oflags
= rw
| O_LARGEFILE
;
310 hreq
.ihandlen
= hlen
;
312 hreq
.ohandlen
= NULL
;
314 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
325 __u32 buflen
= (__u32
)bufsiz
;
327 xfs_fsop_handlereq_t hreq
;
329 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
334 hreq
.oflags
= O_LARGEFILE
;
336 hreq
.ihandlen
= hlen
;
338 hreq
.ohandlen
= &buflen
;
340 return xfsctl(path
, fd
, XFS_IOC_READLINK_BY_HANDLE
, &hreq
);
345 attr_multi_by_handle(
354 xfs_fsop_attrmulti_handlereq_t amhreq
;
356 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
360 amhreq
.hreq
.path
= NULL
;
361 amhreq
.hreq
.oflags
= O_LARGEFILE
;
362 amhreq
.hreq
.ihandle
= hanp
;
363 amhreq
.hreq
.ihandlen
= hlen
;
364 amhreq
.hreq
.ohandle
= NULL
;
365 amhreq
.hreq
.ohandlen
= NULL
;
367 amhreq
.opcount
= rtrvcnt
;
370 return xfsctl(path
, fd
, XFS_IOC_ATTRMULTI_BY_HANDLE
, &amhreq
);
380 struct attrlist_cursor
*cursor
)
384 xfs_fsop_attrlist_handlereq_t alhreq
;
386 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
390 alhreq
.hreq
.path
= NULL
;
391 alhreq
.hreq
.oflags
= O_LARGEFILE
;
392 alhreq
.hreq
.ihandle
= hanp
;
393 alhreq
.hreq
.ihandlen
= hlen
;
394 alhreq
.hreq
.ohandle
= NULL
;
395 alhreq
.hreq
.ohandlen
= NULL
;
397 memcpy(&alhreq
.pos
, cursor
, sizeof(alhreq
.pos
));
398 alhreq
.flags
= flags
;
400 alhreq
.buflen
= bufsize
;
401 /* prevent needless EINVAL from the kernel */
402 if (alhreq
.buflen
> XFS_XATTR_LIST_MAX
)
403 alhreq
.buflen
= XFS_XATTR_LIST_MAX
;
405 error
= xfsctl(path
, fd
, XFS_IOC_ATTRLIST_BY_HANDLE
, &alhreq
);
407 memcpy(cursor
, &alhreq
.pos
, sizeof(alhreq
.pos
));
425 parentpaths_by_handle(
440 struct fsdmidata
*fsdmidata
)
444 xfs_fsop_setdm_handlereq_t dmhreq
;
446 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
450 dmhreq
.hreq
.path
= NULL
;
451 dmhreq
.hreq
.oflags
= O_LARGEFILE
;
452 dmhreq
.hreq
.ihandle
= hanp
;
453 dmhreq
.hreq
.ihandlen
= hlen
;
454 dmhreq
.hreq
.ohandle
= NULL
;
455 dmhreq
.hreq
.ohandlen
= NULL
;
457 dmhreq
.data
= fsdmidata
;
459 return xfsctl(path
, fd
, XFS_IOC_FSSETDM_BY_HANDLE
, &dmhreq
);