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
19 #include <xfs/libxfs.h>
21 /* attributes.h (purposefully) unavailable to xfsprogs, make do */
22 struct attrlist_cursor
{ __u32 opaque
[4]; };
24 #include <xfs/handle.h>
26 /* just pick a value we know is more than big enough */
30 * The actual content of a handle is supposed to be opaque here.
31 * But, to do handle_to_fshandle, we need to know what it is. Sigh.
32 * However we can get by with knowing only that the first 8 bytes of
33 * a file handle are the file system ID, and that a file system handle
34 * consists of only those 8 bytes.
44 static int obj_to_handle(char *, int, unsigned int, comarg_t
, void**, size_t*);
45 static int handle_to_fsfd(void *, char **);
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 */
77 fd
= open(path
, O_RDONLY
);
82 result
= obj_to_handle(path
, fd
, XFS_IOC_PATH_TO_FSHANDLE
,
89 if (handle_to_fsfd(*fshanp
, &tmppath
) >= 0) {
90 /* this filesystem is already in the cache */
93 /* new filesystem. add it to the cache */
94 fdhp
= malloc(sizeof(struct fdhash
));
101 strncpy(fdhp
->fspath
, path
, sizeof(fdhp
->fspath
));
102 memcpy(fdhp
->fsh
, *fshanp
, FSIDSIZE
);
104 fdhp
->fnxt
= fdhash_head
;
113 char *path
, /* input, path to convert */
114 void **hanp
, /* output, pointer to data */
115 size_t *hlen
) /* output, size of returned data */
121 fd
= open(path
, O_RDONLY
);
126 result
= obj_to_handle(path
, fd
, XFS_IOC_PATH_TO_HANDLE
,
135 int fd
, /* input, file descriptor */
136 void **hanp
, /* output, pointer to data */
137 size_t *hlen
) /* output, size of returned data */
142 return obj_to_handle(NULL
, fd
, XFS_IOC_FD_TO_HANDLE
, obj
, hanp
, hlen
);
153 if (hlen
< FSIDSIZE
) {
157 *fshanp
= malloc(FSIDSIZE
);
158 if (*fshanp
== NULL
) {
163 memcpy(*fshanp
, hanp
, FSIDSIZE
);
168 handle_to_fsfd(void *hanp
, char **path
)
173 * Look in cache for matching fsid field in the handle
174 * (which is at the start of the handle).
175 * When found return the file descriptor and path that
176 * we have in the cache.
178 for (fdhp
= fdhash_head
; fdhp
!= NULL
; fdhp
= fdhp
->fnxt
) {
179 if (memcmp(fdhp
->fsh
, hanp
, FSIDSIZE
) == 0) {
180 *path
= fdhp
->fspath
;
197 char hbuf
[MAXHANSIZ
];
200 xfs_fsop_handlereq_t hreq
;
202 if (opcode
== XFS_IOC_FD_TO_HANDLE
) {
207 hreq
.path
= obj
.path
;
210 hreq
.oflags
= O_LARGEFILE
;
214 hreq
.ohandlen
= &handlen
;
216 ret
= xfsctl(fspath
, fsfd
, opcode
, &hreq
);
220 *hanp
= malloc(handlen
);
226 memcpy(*hanp
, hbuf
, handlen
);
239 xfs_fsop_handlereq_t hreq
;
241 if ((fsfd
= handle_to_fsfd(fshanp
, &path
)) < 0)
246 hreq
.oflags
= rw
| O_LARGEFILE
;
247 hreq
.ihandle
= fshanp
;
248 hreq
.ihandlen
= fshlen
;
250 hreq
.ohandlen
= NULL
;
252 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
265 xfs_fsop_handlereq_t hreq
;
267 if (handle_to_fshandle(hanp
, hlen
, &fshanp
, &fshlen
) != 0)
270 if ((fsfd
= handle_to_fsfd(fshanp
, &path
)) < 0)
275 hreq
.oflags
= rw
| O_LARGEFILE
;
277 hreq
.ihandlen
= hlen
;
279 hreq
.ohandlen
= NULL
;
281 return xfsctl(path
, fsfd
, XFS_IOC_OPEN_BY_HANDLE
, &hreq
);
292 __u32 buflen
= (__u32
)bufsiz
;
294 xfs_fsop_handlereq_t hreq
;
296 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
301 hreq
.oflags
= O_LARGEFILE
;
303 hreq
.ihandlen
= hlen
;
305 hreq
.ohandlen
= &buflen
;
307 return xfsctl(path
, fd
, XFS_IOC_READLINK_BY_HANDLE
, &hreq
);
312 attr_multi_by_handle(
321 xfs_fsop_attrmulti_handlereq_t amhreq
;
323 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
327 amhreq
.hreq
.path
= NULL
;
328 amhreq
.hreq
.oflags
= O_LARGEFILE
;
329 amhreq
.hreq
.ihandle
= hanp
;
330 amhreq
.hreq
.ihandlen
= hlen
;
331 amhreq
.hreq
.ohandle
= NULL
;
332 amhreq
.hreq
.ohandlen
= NULL
;
334 amhreq
.opcount
= rtrvcnt
;
337 return xfsctl(path
, fd
, XFS_IOC_ATTRMULTI_BY_HANDLE
, &amhreq
);
347 struct attrlist_cursor
*cursor
)
351 xfs_fsop_attrlist_handlereq_t alhreq
;
353 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
357 alhreq
.hreq
.path
= NULL
;
358 alhreq
.hreq
.oflags
= O_LARGEFILE
;
359 alhreq
.hreq
.ihandle
= hanp
;
360 alhreq
.hreq
.ihandlen
= hlen
;
361 alhreq
.hreq
.ohandle
= NULL
;
362 alhreq
.hreq
.ohandlen
= NULL
;
364 memcpy(&alhreq
.pos
, cursor
, sizeof(alhreq
.pos
));
365 alhreq
.flags
= flags
;
366 alhreq
.buflen
= bufsize
;
369 return xfsctl(path
, fd
, XFS_IOC_ATTRLIST_BY_HANDLE
, &alhreq
);
376 struct fsdmidata
*fsdmidata
)
380 xfs_fsop_setdm_handlereq_t dmhreq
;
382 if ((fd
= handle_to_fsfd(hanp
, &path
)) < 0)
386 dmhreq
.hreq
.path
= NULL
;
387 dmhreq
.hreq
.oflags
= O_LARGEFILE
;
388 dmhreq
.hreq
.ihandle
= hanp
;
389 dmhreq
.hreq
.ihandlen
= hlen
;
390 dmhreq
.hreq
.ohandle
= NULL
;
391 dmhreq
.hreq
.ohandlen
= NULL
;
393 dmhreq
.data
= fsdmidata
;
395 return xfsctl(path
, fd
, XFS_IOC_FSSETDM_BY_HANDLE
, &dmhreq
);