2 * Copyright (c) 1995, 2001-2003, 2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "platform_defs.h"
25 /* just pick a value we know is more than big enough */
29 * The actual content of a handle is supposed to be opaque here.
30 * But, to do handle_to_fshandle, we need to know what it is. Sigh.
31 * However we can get by with knowing only that the first 8 bytes of
32 * a file handle are the file system ID, and that a file system handle
33 * consists of only those 8 bytes.
43 static int obj_to_handle(char *, int, unsigned int, comarg_t
, void**, size_t*);
44 static int handle_to_fsfd(void *, char **);
45 static char *path_to_fspath(char *path
);
49 * Filesystem Handle -> Open File Descriptor Cache
51 * Maps filesystem handles to a corresponding open file descriptor for that
52 * filesystem. We need this because we're doing handle operations via xfsctl
53 * and we need to remember the open file descriptor for each filesystem.
60 char fspath
[MAXPATHLEN
];
63 static struct fdhash
*fdhash_head
= NULL
;
67 char *path
, /* input, path to convert */
68 void **fshanp
, /* output, pointer to data */
69 size_t *fshlen
) /* output, size of returned data */
78 fspath
= path_to_fspath(path
);
82 fd
= open(fspath
, O_RDONLY
);
87 result
= obj_to_handle(fspath
, fd
, XFS_IOC_PATH_TO_FSHANDLE
,
94 if (handle_to_fsfd(*fshanp
, &tmppath
) >= 0) {
95 /* this filesystem is already in the cache */
98 /* new filesystem. add it to the cache */
99 fdhp
= malloc(sizeof(struct fdhash
));
108 strncpy(fdhp
->fspath
, fspath
, sizeof(fdhp
->fspath
));
109 memcpy(fdhp
->fsh
, *fshanp
, FSIDSIZE
);
111 fdhp
->fnxt
= fdhash_head
;
120 char *path
, /* input, path to convert */
121 void **hanp
, /* output, pointer to data */
122 size_t *hlen
) /* output, size of returned data */
129 fspath
= path_to_fspath(path
);
133 fd
= open(fspath
, O_RDONLY
);
138 result
= obj_to_handle(fspath
, fd
, XFS_IOC_PATH_TO_HANDLE
,
144 /* Given a path, return a suitable "fspath" for use in obtaining
145 * an fd for xfsctl calls. For regular files and directories the
146 * input path is sufficient. For other types the parent directory
147 * is used to avoid issues with opening dangling symlinks and
148 * potentially blocking in an open on a named pipe. Also
149 * symlinks to files on other filesystems would be a problem,
150 * since an fd would be obtained for the wrong fs.
153 path_to_fspath(char *path
)
155 static char dirpath
[MAXPATHLEN
];
158 if (lstat(path
, &statbuf
) != 0)
161 if (S_ISREG(statbuf
.st_mode
) || S_ISDIR(statbuf
.st_mode
))
164 strncpy(dirpath
, path
, MAXPATHLEN
);
165 dirpath
[MAXPATHLEN
-1] = '\0';
166 return dirname(dirpath
);
171 int fd
, /* input, file descriptor */
172 void **hanp
, /* output, pointer to data */
173 size_t *hlen
) /* output, size of returned data */
178 return obj_to_handle(NULL
, fd
, XFS_IOC_FD_TO_HANDLE
, obj
, hanp
, hlen
);
189 if (hlen
< FSIDSIZE
) {
193 *fshanp
= malloc(FSIDSIZE
);
194 if (*fshanp
== NULL
) {
199 memcpy(*fshanp
, hanp
, FSIDSIZE
);
204 handle_to_fsfd(void *hanp
, char **path
)
209 * Look in cache for matching fsid field in the handle
210 * (which is at the start of the handle).
211 * When found return the file descriptor and path that
212 * we have in the cache.
214 for (fdhp
= fdhash_head
; fdhp
!= NULL
; fdhp
= fdhp
->fnxt
) {
215 if (memcmp(fdhp
->fsh
, hanp
, FSIDSIZE
) == 0) {
216 *path
= fdhp
->fspath
;
233 char hbuf
[MAXHANSIZ
];
236 xfs_fsop_handlereq_t hreq
;
238 if (opcode
== XFS_IOC_FD_TO_HANDLE
) {
243 hreq
.path
= obj
.path
;
246 hreq
.oflags
= O_LARGEFILE
;
250 hreq
.ohandlen
= &handlen
;
252 ret
= xfsctl(fspath
, fsfd
, opcode
, &hreq
);
256 *hanp
= malloc(handlen
);
262 memcpy(*hanp
, hbuf
, handlen
);
275 xfs_fsop_handlereq_t hreq
;
277 if ((fsfd
= handle_to_fsfd(fshanp
, &path
)) < 0)
282 hreq
.oflags
= rw
| O_LARGEFILE
;
283 hreq
.ihandle
= fshanp
;
284 hreq
.ihandlen
= fshlen
;
286 hreq
.ohandlen
= NULL
;
288 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
299 xfs_fsop_handlereq_t hreq
;
301 if ((fsfd
= handle_to_fsfd(hanp
, &path
)) < 0)
306 hreq
.oflags
= rw
| O_LARGEFILE
;
308 hreq
.ihandlen
= hlen
;
310 hreq
.ohandlen
= NULL
;
312 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
323 __u32 buflen
= (__u32
)bufsiz
;
325 xfs_fsop_handlereq_t hreq
;
327 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
332 hreq
.oflags
= O_LARGEFILE
;
334 hreq
.ihandlen
= hlen
;
336 hreq
.ohandlen
= &buflen
;
338 return xfsctl(path
, fd
, XFS_IOC_READLINK_BY_HANDLE
, &hreq
);
343 attr_multi_by_handle(
352 xfs_fsop_attrmulti_handlereq_t amhreq
;
354 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
358 amhreq
.hreq
.path
= NULL
;
359 amhreq
.hreq
.oflags
= O_LARGEFILE
;
360 amhreq
.hreq
.ihandle
= hanp
;
361 amhreq
.hreq
.ihandlen
= hlen
;
362 amhreq
.hreq
.ohandle
= NULL
;
363 amhreq
.hreq
.ohandlen
= NULL
;
365 amhreq
.opcount
= rtrvcnt
;
368 return xfsctl(path
, fd
, XFS_IOC_ATTRMULTI_BY_HANDLE
, &amhreq
);
378 struct attrlist_cursor
*cursor
)
382 xfs_fsop_attrlist_handlereq_t alhreq
;
384 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
388 alhreq
.hreq
.path
= NULL
;
389 alhreq
.hreq
.oflags
= O_LARGEFILE
;
390 alhreq
.hreq
.ihandle
= hanp
;
391 alhreq
.hreq
.ihandlen
= hlen
;
392 alhreq
.hreq
.ohandle
= NULL
;
393 alhreq
.hreq
.ohandlen
= NULL
;
395 memcpy(&alhreq
.pos
, cursor
, sizeof(alhreq
.pos
));
396 alhreq
.flags
= flags
;
398 alhreq
.buflen
= bufsize
;
399 /* prevent needless EINVAL from the kernel */
400 if (alhreq
.buflen
> XATTR_LIST_MAX
)
401 alhreq
.buflen
= XATTR_LIST_MAX
;
403 error
= xfsctl(path
, fd
, XFS_IOC_ATTRLIST_BY_HANDLE
, &alhreq
);
405 memcpy(cursor
, &alhreq
.pos
, sizeof(alhreq
.pos
));
423 parentpaths_by_handle(
438 struct fsdmidata
*fsdmidata
)
442 xfs_fsop_setdm_handlereq_t dmhreq
;
444 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
448 dmhreq
.hreq
.path
= NULL
;
449 dmhreq
.hreq
.oflags
= O_LARGEFILE
;
450 dmhreq
.hreq
.ihandle
= hanp
;
451 dmhreq
.hreq
.ihandlen
= hlen
;
452 dmhreq
.hreq
.ohandle
= NULL
;
453 dmhreq
.hreq
.ohandlen
= NULL
;
455 dmhreq
.data
= fsdmidata
;
457 return xfsctl(path
, fd
, XFS_IOC_FSSETDM_BY_HANDLE
, &dmhreq
);