]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libhandle/jdm.c
libxfs: add more bounds checking to sb sanity checks
[thirdparty/xfsprogs-dev.git] / libhandle / jdm.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (c) 1995, 2001-2002, 2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "platform_defs.h"
8 #include "xfs.h"
9 #include "handle.h"
10 #include "jdm.h"
11 #include "parent.h"
12
13 /* internal fshandle - typecast to a void for external use */
14 #define FSHANDLE_SZ 8
15 typedef struct fshandle {
16 char fsh_space[FSHANDLE_SZ];
17 } fshandle_t;
18
19 /* private file handle - for use by open_by_fshandle */
20 #define FILEHANDLE_SZ 24
21 #define FILEHANDLE_SZ_FOLLOWING 14
22 #define FILEHANDLE_SZ_PAD 2
23 typedef struct filehandle {
24 fshandle_t fh_fshandle; /* handle of fs containing this inode */
25 int16_t fh_sz_following; /* bytes in handle after this member */
26 char fh_pad[FILEHANDLE_SZ_PAD]; /* padding, must be zeroed */
27 uint32_t fh_gen; /* generation count */
28 xfs_ino_t fh_ino; /* 64 bit ino */
29 } filehandle_t;
30
31
32 static void
33 jdm_fill_filehandle( filehandle_t *handlep,
34 fshandle_t *fshandlep,
35 xfs_bstat_t *statp )
36 {
37 handlep->fh_fshandle = *fshandlep;
38 handlep->fh_sz_following = FILEHANDLE_SZ_FOLLOWING;
39 memset(handlep->fh_pad, 0, FILEHANDLE_SZ_PAD);
40 handlep->fh_gen = statp->bs_gen;
41 handlep->fh_ino = statp->bs_ino;
42 }
43
44 jdm_fshandle_t *
45 jdm_getfshandle( char *mntpnt )
46 {
47 fshandle_t *fshandlep;
48 size_t fshandlesz;
49 char resolved[MAXPATHLEN];
50
51 /* sanity checks */
52 ASSERT( sizeof( fshandle_t ) == FSHANDLE_SZ );
53 ASSERT( sizeof( filehandle_t ) == FILEHANDLE_SZ );
54 ASSERT( sizeof( filehandle_t )
55 -
56 offsetofmember( filehandle_t, fh_pad )
57 ==
58 FILEHANDLE_SZ_FOLLOWING );
59 ASSERT( sizeofmember( filehandle_t, fh_pad ) == FILEHANDLE_SZ_PAD );
60 ASSERT( FILEHANDLE_SZ_PAD == sizeof( int16_t ));
61
62 fshandlep = NULL; /* for lint */
63 fshandlesz = sizeof( *fshandlep );
64
65 if (!realpath( mntpnt, resolved ))
66 return NULL;
67
68 if (path_to_fshandle( resolved, ( void ** )&fshandlep, &fshandlesz ))
69 return NULL;
70
71 assert( fshandlesz == sizeof( *fshandlep ));
72
73 return ( jdm_fshandle_t * )fshandlep;
74 }
75
76
77 /* externally visible functions */
78
79 void
80 jdm_new_filehandle( jdm_filehandle_t **handlep,
81 size_t *hlen,
82 jdm_fshandle_t *fshandlep,
83 xfs_bstat_t *statp)
84 {
85 /* allocate and fill filehandle */
86 *hlen = sizeof(filehandle_t);
87 *handlep = (filehandle_t *) malloc(*hlen);
88
89 if (*handlep)
90 jdm_fill_filehandle(*handlep, (fshandle_t *) fshandlep, statp);
91 }
92
93 /* ARGSUSED */
94 void
95 jdm_delete_filehandle( jdm_filehandle_t *handlep, size_t hlen )
96 {
97 free(handlep);
98 }
99
100 intgen_t
101 jdm_open( jdm_fshandle_t *fshp, xfs_bstat_t *statp, intgen_t oflags )
102 {
103 fshandle_t *fshandlep = ( fshandle_t * )fshp;
104 filehandle_t filehandle;
105 intgen_t fd;
106
107 jdm_fill_filehandle( &filehandle, fshandlep, statp );
108 fd = open_by_fshandle( ( void * )&filehandle,
109 sizeof( filehandle ),
110 oflags );
111 return fd;
112 }
113
114 intgen_t
115 jdm_readlink( jdm_fshandle_t *fshp,
116 xfs_bstat_t *statp,
117 char *bufp, size_t bufsz )
118 {
119 fshandle_t *fshandlep = ( fshandle_t * )fshp;
120 filehandle_t filehandle;
121 intgen_t rval;
122
123 jdm_fill_filehandle( &filehandle, fshandlep, statp );
124 rval = readlink_by_handle( ( void * )&filehandle,
125 sizeof( filehandle ),
126 ( void * )bufp,
127 bufsz );
128 return rval;
129 }
130
131 int
132 jdm_attr_multi( jdm_fshandle_t *fshp,
133 xfs_bstat_t *statp,
134 char *bufp, int rtrvcnt, int flags)
135 {
136 fshandle_t *fshandlep = ( fshandle_t * )fshp;
137 filehandle_t filehandle;
138 int rval;
139
140 jdm_fill_filehandle( &filehandle, fshandlep, statp );
141 rval = attr_multi_by_handle ( ( void * )&filehandle,
142 sizeof( filehandle ),
143 (void *) bufp,
144 rtrvcnt, flags);
145 return rval;
146 }
147
148 int
149 jdm_attr_list( jdm_fshandle_t *fshp,
150 xfs_bstat_t *statp,
151 char *bufp, size_t bufsz, int flags,
152 struct attrlist_cursor *cursor)
153 {
154 fshandle_t *fshandlep = ( fshandle_t * )fshp;
155 filehandle_t filehandle;
156 int rval;
157
158 /* prevent needless EINVAL from the kernel */
159 if (bufsz > XFS_XATTR_LIST_MAX)
160 bufsz = XFS_XATTR_LIST_MAX;
161
162 jdm_fill_filehandle( &filehandle, fshandlep, statp );
163 rval = attr_list_by_handle (( void * )&filehandle,
164 sizeof( filehandle ),
165 bufp, bufsz, flags, cursor);
166 return rval;
167 }
168
169 int
170 jdm_parents( jdm_fshandle_t *fshp,
171 xfs_bstat_t *statp,
172 parent_t *bufp, size_t bufsz,
173 unsigned int *count)
174 {
175 errno = EOPNOTSUPP;
176 return -1;
177 }
178
179 int
180 jdm_parentpaths( jdm_fshandle_t *fshp,
181 xfs_bstat_t *statp,
182 parent_t *bufp, size_t bufsz,
183 unsigned int *count)
184 {
185 errno = EOPNOTSUPP;
186 return -1;
187 }