1 | /** @file
|
---|
2 | *
|
---|
3 | * vboxsf -- VirtualBox Guest Additions for Linux:
|
---|
4 | * Utility functions.
|
---|
5 | * Mainly conversion from/to VirtualBox/Linux data structures
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2018 Oracle Corporation
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
16 | * copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following
|
---|
18 | * conditions:
|
---|
19 | *
|
---|
20 | * The above copyright notice and this permission notice shall be
|
---|
21 | * included in all copies or substantial portions of the Software.
|
---|
22 | *
|
---|
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include "vfsmod.h"
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <linux/nfs_fs.h>
|
---|
36 | #include <linux/vfs.h>
|
---|
37 |
|
---|
38 | /* #define USE_VMALLOC */
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * sf_reg_aops and sf_backing_dev_info are just quick implementations to make
|
---|
42 | * sendfile work. For more information have a look at
|
---|
43 | *
|
---|
44 | * http://us1.samba.org/samba/ftp/cifs-cvs/ols2006-fs-tutorial-smf.odp
|
---|
45 | *
|
---|
46 | * and the sample implementation
|
---|
47 | *
|
---|
48 | * http://pserver.samba.org/samba/ftp/cifs-cvs/samplefs.tar.gz
|
---|
49 | */
|
---|
50 |
|
---|
51 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
52 | static void sf_ftime_from_timespec(time_t * time, RTTIMESPEC * ts)
|
---|
53 | {
|
---|
54 | int64_t t = RTTimeSpecGetNano(ts);
|
---|
55 |
|
---|
56 | do_div(t, 1000000000);
|
---|
57 | *time = t;
|
---|
58 | }
|
---|
59 |
|
---|
60 | static void sf_timespec_from_ftime(RTTIMESPEC * ts, time_t * time)
|
---|
61 | {
|
---|
62 | int64_t t = 1000000000 * *time;
|
---|
63 | RTTimeSpecSetNano(ts, t);
|
---|
64 | }
|
---|
65 | #else /* >= 2.6.0 */
|
---|
66 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
|
---|
67 | static void sf_ftime_from_timespec(struct timespec *tv, RTTIMESPEC *ts)
|
---|
68 | #else
|
---|
69 | static void sf_ftime_from_timespec(struct timespec64 *tv, RTTIMESPEC *ts)
|
---|
70 | #endif
|
---|
71 | {
|
---|
72 | int64_t t = RTTimeSpecGetNano(ts);
|
---|
73 | int64_t nsec;
|
---|
74 |
|
---|
75 | nsec = do_div(t, 1000000000);
|
---|
76 | tv->tv_sec = t;
|
---|
77 | tv->tv_nsec = nsec;
|
---|
78 | }
|
---|
79 |
|
---|
80 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
|
---|
81 | static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec *tv)
|
---|
82 | #else
|
---|
83 | static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec64 *tv)
|
---|
84 | #endif
|
---|
85 | {
|
---|
86 | int64_t t = (int64_t) tv->tv_nsec + (int64_t) tv->tv_sec * 1000000000;
|
---|
87 | RTTimeSpecSetNano(ts, t);
|
---|
88 | }
|
---|
89 | #endif /* >= 2.6.0 */
|
---|
90 |
|
---|
91 | /* set [inode] attributes based on [info], uid/gid based on [sf_g] */
|
---|
92 | void sf_init_inode(struct sf_glob_info *sf_g, struct inode *inode,
|
---|
93 | PSHFLFSOBJINFO info)
|
---|
94 | {
|
---|
95 | PSHFLFSOBJATTR attr;
|
---|
96 | int mode;
|
---|
97 |
|
---|
98 | TRACE();
|
---|
99 |
|
---|
100 | attr = &info->Attr;
|
---|
101 |
|
---|
102 | #define mode_set(r) attr->fMode & (RTFS_UNIX_##r) ? (S_##r) : 0;
|
---|
103 | mode = mode_set(IRUSR);
|
---|
104 | mode |= mode_set(IWUSR);
|
---|
105 | mode |= mode_set(IXUSR);
|
---|
106 |
|
---|
107 | mode |= mode_set(IRGRP);
|
---|
108 | mode |= mode_set(IWGRP);
|
---|
109 | mode |= mode_set(IXGRP);
|
---|
110 |
|
---|
111 | mode |= mode_set(IROTH);
|
---|
112 | mode |= mode_set(IWOTH);
|
---|
113 | mode |= mode_set(IXOTH);
|
---|
114 |
|
---|
115 | #undef mode_set
|
---|
116 |
|
---|
117 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
118 | inode->i_mapping->a_ops = &sf_reg_aops;
|
---|
119 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
|
---|
120 | /* XXX Was this ever necessary? */
|
---|
121 | inode->i_mapping->backing_dev_info = &sf_g->bdi;
|
---|
122 | #endif
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | if (RTFS_IS_DIRECTORY(attr->fMode)) {
|
---|
126 | inode->i_mode = sf_g->dmode != ~0 ? (sf_g->dmode & 0777) : mode;
|
---|
127 | inode->i_mode &= ~sf_g->dmask;
|
---|
128 | inode->i_mode |= S_IFDIR;
|
---|
129 | inode->i_op = &sf_dir_iops;
|
---|
130 | inode->i_fop = &sf_dir_fops;
|
---|
131 | /* XXX: this probably should be set to the number of entries
|
---|
132 | in the directory plus two (. ..) */
|
---|
133 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
---|
134 | set_nlink(inode, 1);
|
---|
135 | #else
|
---|
136 | inode->i_nlink = 1;
|
---|
137 | #endif
|
---|
138 | }
|
---|
139 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
140 | else if (RTFS_IS_SYMLINK(attr->fMode)) {
|
---|
141 | inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777) : mode;
|
---|
142 | inode->i_mode &= ~sf_g->fmask;
|
---|
143 | inode->i_mode |= S_IFLNK;
|
---|
144 | inode->i_op = &sf_lnk_iops;
|
---|
145 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
---|
146 | set_nlink(inode, 1);
|
---|
147 | #else
|
---|
148 | inode->i_nlink = 1;
|
---|
149 | #endif
|
---|
150 | }
|
---|
151 | #endif
|
---|
152 | else {
|
---|
153 | inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777) : mode;
|
---|
154 | inode->i_mode &= ~sf_g->fmask;
|
---|
155 | inode->i_mode |= S_IFREG;
|
---|
156 | inode->i_op = &sf_reg_iops;
|
---|
157 | inode->i_fop = &sf_reg_fops;
|
---|
158 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
---|
159 | set_nlink(inode, 1);
|
---|
160 | #else
|
---|
161 | inode->i_nlink = 1;
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
166 | inode->i_uid = make_kuid(current_user_ns(), sf_g->uid);
|
---|
167 | inode->i_gid = make_kgid(current_user_ns(), sf_g->gid);
|
---|
168 | #else
|
---|
169 | inode->i_uid = sf_g->uid;
|
---|
170 | inode->i_gid = sf_g->gid;
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | inode->i_size = info->cbObject;
|
---|
174 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
|
---|
175 | inode->i_blksize = 4096;
|
---|
176 | #endif
|
---|
177 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
|
---|
178 | inode->i_blkbits = 12;
|
---|
179 | #endif
|
---|
180 | /* i_blocks always in units of 512 bytes! */
|
---|
181 | inode->i_blocks = (info->cbAllocated + 511) / 512;
|
---|
182 |
|
---|
183 | sf_ftime_from_timespec(&inode->i_atime, &info->AccessTime);
|
---|
184 | sf_ftime_from_timespec(&inode->i_ctime, &info->ChangeTime);
|
---|
185 | sf_ftime_from_timespec(&inode->i_mtime, &info->ModificationTime);
|
---|
186 | }
|
---|
187 |
|
---|
188 | int sf_stat(const char *caller, struct sf_glob_info *sf_g,
|
---|
189 | SHFLSTRING * path, PSHFLFSOBJINFO result, int ok_to_fail)
|
---|
190 | {
|
---|
191 | int rc;
|
---|
192 | SHFLCREATEPARMS params;
|
---|
193 | NOREF(caller);
|
---|
194 |
|
---|
195 | TRACE();
|
---|
196 |
|
---|
197 | RT_ZERO(params);
|
---|
198 | params.Handle = SHFL_HANDLE_NIL;
|
---|
199 | params.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
|
---|
200 | LogFunc(("sf_stat: calling VbglR0SfCreate, file %s, flags %#x\n",
|
---|
201 | path->String.utf8, params.CreateFlags));
|
---|
202 | rc = VbglR0SfCreate(&client_handle, &sf_g->map, path, ¶ms);
|
---|
203 | if (rc == VERR_INVALID_NAME) {
|
---|
204 | /* this can happen for names like 'foo*' on a Windows host */
|
---|
205 | return -ENOENT;
|
---|
206 | }
|
---|
207 | if (RT_FAILURE(rc)) {
|
---|
208 | LogFunc(("VbglR0SfCreate(%s) failed. caller=%s, rc=%Rrc\n",
|
---|
209 | path->String.utf8, rc, caller));
|
---|
210 | return -EPROTO;
|
---|
211 | }
|
---|
212 | if (params.Result != SHFL_FILE_EXISTS) {
|
---|
213 | if (!ok_to_fail)
|
---|
214 | LogFunc(("VbglR0SfCreate(%s) file does not exist. caller=%s, result=%d\n", path->String.utf8, params.Result, caller));
|
---|
215 | return -ENOENT;
|
---|
216 | }
|
---|
217 |
|
---|
218 | *result = params.Info;
|
---|
219 | return 0;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* this is called directly as iop on 2.4, indirectly as dop
|
---|
223 | [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
|
---|
224 | [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
|
---|
225 | still valid. the test is failed if [dentry] does not have an inode
|
---|
226 | or [sf_stat] is unsuccessful, otherwise we return success and
|
---|
227 | update inode attributes */
|
---|
228 | int sf_inode_revalidate(struct dentry *dentry)
|
---|
229 | {
|
---|
230 | int err;
|
---|
231 | struct sf_glob_info *sf_g;
|
---|
232 | struct sf_inode_info *sf_i;
|
---|
233 | SHFLFSOBJINFO info;
|
---|
234 |
|
---|
235 | TRACE();
|
---|
236 | if (!dentry || !dentry->d_inode) {
|
---|
237 | LogFunc(("no dentry(%p) or inode(%p)\n", dentry,
|
---|
238 | dentry->d_inode));
|
---|
239 | return -EINVAL;
|
---|
240 | }
|
---|
241 |
|
---|
242 | sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
|
---|
243 | sf_i = GET_INODE_INFO(dentry->d_inode);
|
---|
244 |
|
---|
245 | #if 0
|
---|
246 | printk("%s called by %p:%p\n",
|
---|
247 | sf_i->path->String.utf8,
|
---|
248 | __builtin_return_address(0), __builtin_return_address(1));
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | BUG_ON(!sf_g);
|
---|
252 | BUG_ON(!sf_i);
|
---|
253 |
|
---|
254 | if (!sf_i->force_restat) {
|
---|
255 | if (jiffies - dentry->d_time < sf_g->ttl)
|
---|
256 | return 0;
|
---|
257 | }
|
---|
258 |
|
---|
259 | err = sf_stat(__func__, sf_g, sf_i->path, &info, 1);
|
---|
260 | if (err)
|
---|
261 | return err;
|
---|
262 |
|
---|
263 | dentry->d_time = jiffies;
|
---|
264 | sf_init_inode(sf_g, dentry->d_inode, &info);
|
---|
265 | return 0;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* this is called during name resolution/lookup to check if the
|
---|
269 | [dentry] in the cache is still valid. the job is handled by
|
---|
270 | [sf_inode_revalidate] */
|
---|
271 | static int
|
---|
272 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
|
---|
273 | sf_dentry_revalidate(struct dentry *dentry, unsigned flags)
|
---|
274 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
275 | sf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
|
---|
276 | #else
|
---|
277 | sf_dentry_revalidate(struct dentry *dentry, int flags)
|
---|
278 | #endif
|
---|
279 | {
|
---|
280 | TRACE();
|
---|
281 |
|
---|
282 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
|
---|
283 | if (flags & LOOKUP_RCU)
|
---|
284 | return -ECHILD;
|
---|
285 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
|
---|
286 | /* see Documentation/filesystems/vfs.txt */
|
---|
287 | if (nd && nd->flags & LOOKUP_RCU)
|
---|
288 | return -ECHILD;
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | if (sf_inode_revalidate(dentry))
|
---|
292 | return 0;
|
---|
293 |
|
---|
294 | return 1;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
|
---|
298 | effect) updates inode attributes for [dentry] (given that [dentry]
|
---|
299 | has inode at all) from these new attributes we derive [kstat] via
|
---|
300 | [generic_fillattr] */
|
---|
301 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
302 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
---|
303 | int sf_getattr(const struct path *path, struct kstat *kstat, u32 request_mask,
|
---|
304 | unsigned int flags)
|
---|
305 | #else
|
---|
306 | int sf_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
|
---|
307 | #endif
|
---|
308 | {
|
---|
309 | int err;
|
---|
310 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
---|
311 | struct dentry *dentry = path->dentry;
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | TRACE();
|
---|
315 | err = sf_inode_revalidate(dentry);
|
---|
316 | if (err)
|
---|
317 | return err;
|
---|
318 |
|
---|
319 | generic_fillattr(dentry->d_inode, kstat);
|
---|
320 | return 0;
|
---|
321 | }
|
---|
322 |
|
---|
323 | int sf_setattr(struct dentry *dentry, struct iattr *iattr)
|
---|
324 | {
|
---|
325 | struct sf_glob_info *sf_g;
|
---|
326 | struct sf_inode_info *sf_i;
|
---|
327 | SHFLCREATEPARMS params;
|
---|
328 | SHFLFSOBJINFO info;
|
---|
329 | uint32_t cbBuffer;
|
---|
330 | int rc, err;
|
---|
331 |
|
---|
332 | TRACE();
|
---|
333 |
|
---|
334 | sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
|
---|
335 | sf_i = GET_INODE_INFO(dentry->d_inode);
|
---|
336 | err = 0;
|
---|
337 |
|
---|
338 | RT_ZERO(params);
|
---|
339 | params.Handle = SHFL_HANDLE_NIL;
|
---|
340 | params.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
|
---|
341 | | SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_ATTR_WRITE;
|
---|
342 |
|
---|
343 | /* this is at least required for Posix hosts */
|
---|
344 | if (iattr->ia_valid & ATTR_SIZE)
|
---|
345 | params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
|
---|
346 |
|
---|
347 | rc = VbglR0SfCreate(&client_handle, &sf_g->map, sf_i->path, ¶ms);
|
---|
348 | if (RT_FAILURE(rc)) {
|
---|
349 | LogFunc(("VbglR0SfCreate(%s) failed rc=%Rrc\n",
|
---|
350 | sf_i->path->String.utf8, rc));
|
---|
351 | err = -RTErrConvertToErrno(rc);
|
---|
352 | goto fail2;
|
---|
353 | }
|
---|
354 | if (params.Result != SHFL_FILE_EXISTS) {
|
---|
355 | LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
|
---|
356 | err = -ENOENT;
|
---|
357 | goto fail1;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /* Setting the file size and setting the other attributes has to be
|
---|
361 | * handled separately, see implementation of vbsfSetFSInfo() in
|
---|
362 | * vbsf.cpp */
|
---|
363 | if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME)) {
|
---|
364 | #define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)
|
---|
365 |
|
---|
366 | RT_ZERO(info);
|
---|
367 | if (iattr->ia_valid & ATTR_MODE) {
|
---|
368 | info.Attr.fMode = mode_set(IRUSR);
|
---|
369 | info.Attr.fMode |= mode_set(IWUSR);
|
---|
370 | info.Attr.fMode |= mode_set(IXUSR);
|
---|
371 | info.Attr.fMode |= mode_set(IRGRP);
|
---|
372 | info.Attr.fMode |= mode_set(IWGRP);
|
---|
373 | info.Attr.fMode |= mode_set(IXGRP);
|
---|
374 | info.Attr.fMode |= mode_set(IROTH);
|
---|
375 | info.Attr.fMode |= mode_set(IWOTH);
|
---|
376 | info.Attr.fMode |= mode_set(IXOTH);
|
---|
377 |
|
---|
378 | if (iattr->ia_mode & S_IFDIR)
|
---|
379 | info.Attr.fMode |= RTFS_TYPE_DIRECTORY;
|
---|
380 | else
|
---|
381 | info.Attr.fMode |= RTFS_TYPE_FILE;
|
---|
382 | }
|
---|
383 |
|
---|
384 | if (iattr->ia_valid & ATTR_ATIME)
|
---|
385 | sf_timespec_from_ftime(&info.AccessTime,
|
---|
386 | &iattr->ia_atime);
|
---|
387 | if (iattr->ia_valid & ATTR_MTIME)
|
---|
388 | sf_timespec_from_ftime(&info.ModificationTime,
|
---|
389 | &iattr->ia_mtime);
|
---|
390 | /* ignore ctime (inode change time) as it can't be set from userland anyway */
|
---|
391 |
|
---|
392 | cbBuffer = sizeof(info);
|
---|
393 | rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, params.Handle,
|
---|
394 | SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
|
---|
395 | (PSHFLDIRINFO) & info);
|
---|
396 | if (RT_FAILURE(rc)) {
|
---|
397 | LogFunc(("VbglR0SfFsInfo(%s, FILE) failed rc=%Rrc\n",
|
---|
398 | sf_i->path->String.utf8, rc));
|
---|
399 | err = -RTErrConvertToErrno(rc);
|
---|
400 | goto fail1;
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | if (iattr->ia_valid & ATTR_SIZE) {
|
---|
405 | RT_ZERO(info);
|
---|
406 | info.cbObject = iattr->ia_size;
|
---|
407 | cbBuffer = sizeof(info);
|
---|
408 | rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, params.Handle,
|
---|
409 | SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
|
---|
410 | (PSHFLDIRINFO) & info);
|
---|
411 | if (RT_FAILURE(rc)) {
|
---|
412 | LogFunc(("VbglR0SfFsInfo(%s, SIZE) failed rc=%Rrc\n",
|
---|
413 | sf_i->path->String.utf8, rc));
|
---|
414 | err = -RTErrConvertToErrno(rc);
|
---|
415 | goto fail1;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | rc = VbglR0SfClose(&client_handle, &sf_g->map, params.Handle);
|
---|
420 | if (RT_FAILURE(rc))
|
---|
421 | LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
|
---|
422 | sf_i->path->String.utf8, rc));
|
---|
423 |
|
---|
424 | return sf_inode_revalidate(dentry);
|
---|
425 |
|
---|
426 | fail1:
|
---|
427 | rc = VbglR0SfClose(&client_handle, &sf_g->map, params.Handle);
|
---|
428 | if (RT_FAILURE(rc))
|
---|
429 | LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
|
---|
430 | sf_i->path->String.utf8, rc));
|
---|
431 |
|
---|
432 | fail2:
|
---|
433 | return err;
|
---|
434 | }
|
---|
435 | #endif /* >= 2.6.0 */
|
---|
436 |
|
---|
437 | static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
|
---|
438 | const char *d_name, size_t d_len, SHFLSTRING ** result)
|
---|
439 | {
|
---|
440 | size_t path_len, shflstring_len;
|
---|
441 | SHFLSTRING *tmp;
|
---|
442 | uint16_t p_len;
|
---|
443 | uint8_t *p_name;
|
---|
444 | int fRoot = 0;
|
---|
445 |
|
---|
446 | TRACE();
|
---|
447 | p_len = sf_i->path->u16Length;
|
---|
448 | p_name = sf_i->path->String.utf8;
|
---|
449 |
|
---|
450 | if (p_len == 1 && *p_name == '/') {
|
---|
451 | path_len = d_len + 1;
|
---|
452 | fRoot = 1;
|
---|
453 | } else {
|
---|
454 | /* lengths of constituents plus terminating zero plus slash */
|
---|
455 | path_len = p_len + d_len + 2;
|
---|
456 | if (path_len > 0xffff) {
|
---|
457 | LogFunc(("path too long. caller=%s, path_len=%zu\n",
|
---|
458 | caller, path_len));
|
---|
459 | return -ENAMETOOLONG;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
|
---|
464 | tmp = kmalloc(shflstring_len, GFP_KERNEL);
|
---|
465 | if (!tmp) {
|
---|
466 | LogRelFunc(("kmalloc failed, caller=%s\n", caller));
|
---|
467 | return -ENOMEM;
|
---|
468 | }
|
---|
469 | tmp->u16Length = path_len - 1;
|
---|
470 | tmp->u16Size = path_len;
|
---|
471 |
|
---|
472 | if (fRoot)
|
---|
473 | memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
|
---|
474 | else {
|
---|
475 | memcpy(&tmp->String.utf8[0], p_name, p_len);
|
---|
476 | tmp->String.utf8[p_len] = '/';
|
---|
477 | memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
|
---|
478 | tmp->String.utf8[p_len + 1 + d_len] = '\0';
|
---|
479 | }
|
---|
480 |
|
---|
481 | *result = tmp;
|
---|
482 | return 0;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * [dentry] contains string encoded in coding system that corresponds
|
---|
487 | * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
|
---|
488 | * [sf_make_path] which will allocate SHFLSTRING and fill it in
|
---|
489 | */
|
---|
490 | int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
|
---|
491 | struct sf_inode_info *sf_i, struct dentry *dentry,
|
---|
492 | SHFLSTRING ** result)
|
---|
493 | {
|
---|
494 | int err;
|
---|
495 | const char *d_name;
|
---|
496 | size_t d_len;
|
---|
497 | const char *name;
|
---|
498 | size_t len = 0;
|
---|
499 |
|
---|
500 | TRACE();
|
---|
501 | d_name = dentry->d_name.name;
|
---|
502 | d_len = dentry->d_name.len;
|
---|
503 |
|
---|
504 | if (sf_g->nls) {
|
---|
505 | size_t in_len, i, out_bound_len;
|
---|
506 | const char *in;
|
---|
507 | char *out;
|
---|
508 |
|
---|
509 | in = d_name;
|
---|
510 | in_len = d_len;
|
---|
511 |
|
---|
512 | out_bound_len = PATH_MAX;
|
---|
513 | out = kmalloc(out_bound_len, GFP_KERNEL);
|
---|
514 | name = out;
|
---|
515 |
|
---|
516 | for (i = 0; i < d_len; ++i) {
|
---|
517 | /* We renamed the linux kernel wchar_t type to linux_wchar_t in
|
---|
518 | the-linux-kernel.h, as it conflicts with the C++ type of that name. */
|
---|
519 | linux_wchar_t uni;
|
---|
520 | int nb;
|
---|
521 |
|
---|
522 | nb = sf_g->nls->char2uni(in, in_len, &uni);
|
---|
523 | if (nb < 0) {
|
---|
524 | LogFunc(("nls->char2uni failed %x %d\n",
|
---|
525 | *in, in_len));
|
---|
526 | err = -EINVAL;
|
---|
527 | goto fail1;
|
---|
528 | }
|
---|
529 | in_len -= nb;
|
---|
530 | in += nb;
|
---|
531 |
|
---|
532 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
|
---|
533 | nb = utf32_to_utf8(uni, out, out_bound_len);
|
---|
534 | #else
|
---|
535 | nb = utf8_wctomb(out, uni, out_bound_len);
|
---|
536 | #endif
|
---|
537 | if (nb < 0) {
|
---|
538 | LogFunc(("nls->uni2char failed %x %d\n",
|
---|
539 | uni, out_bound_len));
|
---|
540 | err = -EINVAL;
|
---|
541 | goto fail1;
|
---|
542 | }
|
---|
543 | out_bound_len -= nb;
|
---|
544 | out += nb;
|
---|
545 | len += nb;
|
---|
546 | }
|
---|
547 | if (len >= PATH_MAX - 1) {
|
---|
548 | err = -ENAMETOOLONG;
|
---|
549 | goto fail1;
|
---|
550 | }
|
---|
551 |
|
---|
552 | LogFunc(("result(%d) = %.*s\n", len, len, name));
|
---|
553 | *out = 0;
|
---|
554 | } else {
|
---|
555 | name = d_name;
|
---|
556 | len = d_len;
|
---|
557 | }
|
---|
558 |
|
---|
559 | err = sf_make_path(caller, sf_i, name, len, result);
|
---|
560 | if (name != d_name)
|
---|
561 | kfree(name);
|
---|
562 |
|
---|
563 | return err;
|
---|
564 |
|
---|
565 | fail1:
|
---|
566 | kfree(name);
|
---|
567 | return err;
|
---|
568 | }
|
---|
569 |
|
---|
570 | int sf_nlscpy(struct sf_glob_info *sf_g,
|
---|
571 | char *name, size_t name_bound_len,
|
---|
572 | const unsigned char *utf8_name, size_t utf8_len)
|
---|
573 | {
|
---|
574 | if (sf_g->nls) {
|
---|
575 | const char *in;
|
---|
576 | char *out;
|
---|
577 | size_t out_len;
|
---|
578 | size_t out_bound_len;
|
---|
579 | size_t in_bound_len;
|
---|
580 |
|
---|
581 | in = utf8_name;
|
---|
582 | in_bound_len = utf8_len;
|
---|
583 |
|
---|
584 | out = name;
|
---|
585 | out_len = 0;
|
---|
586 | out_bound_len = name_bound_len;
|
---|
587 |
|
---|
588 | while (in_bound_len) {
|
---|
589 | int nb;
|
---|
590 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
|
---|
591 | unicode_t uni;
|
---|
592 |
|
---|
593 | nb = utf8_to_utf32(in, in_bound_len, &uni);
|
---|
594 | #else
|
---|
595 | linux_wchar_t uni;
|
---|
596 |
|
---|
597 | nb = utf8_mbtowc(&uni, in, in_bound_len);
|
---|
598 | #endif
|
---|
599 | if (nb < 0) {
|
---|
600 | LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
|
---|
601 | (const char *)utf8_name, *in,
|
---|
602 | in_bound_len));
|
---|
603 | return -EINVAL;
|
---|
604 | }
|
---|
605 | in += nb;
|
---|
606 | in_bound_len -= nb;
|
---|
607 |
|
---|
608 | nb = sf_g->nls->uni2char(uni, out, out_bound_len);
|
---|
609 | if (nb < 0) {
|
---|
610 | LogFunc(("nls->uni2char failed(%s) %x:%d\n",
|
---|
611 | utf8_name, uni, out_bound_len));
|
---|
612 | return nb;
|
---|
613 | }
|
---|
614 | out += nb;
|
---|
615 | out_bound_len -= nb;
|
---|
616 | out_len += nb;
|
---|
617 | }
|
---|
618 |
|
---|
619 | *out = 0;
|
---|
620 | } else {
|
---|
621 | if (utf8_len + 1 > name_bound_len)
|
---|
622 | return -ENAMETOOLONG;
|
---|
623 |
|
---|
624 | memcpy(name, utf8_name, utf8_len + 1);
|
---|
625 | }
|
---|
626 | return 0;
|
---|
627 | }
|
---|
628 |
|
---|
629 | static struct sf_dir_buf *sf_dir_buf_alloc(void)
|
---|
630 | {
|
---|
631 | struct sf_dir_buf *b;
|
---|
632 |
|
---|
633 | TRACE();
|
---|
634 | b = kmalloc(sizeof(*b), GFP_KERNEL);
|
---|
635 | if (!b) {
|
---|
636 | LogRelFunc(("could not alloc directory buffer\n"));
|
---|
637 | return NULL;
|
---|
638 | }
|
---|
639 | #ifdef USE_VMALLOC
|
---|
640 | b->buf = vmalloc(DIR_BUFFER_SIZE);
|
---|
641 | #else
|
---|
642 | b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
|
---|
643 | #endif
|
---|
644 | if (!b->buf) {
|
---|
645 | kfree(b);
|
---|
646 | LogRelFunc(("could not alloc directory buffer storage\n"));
|
---|
647 | return NULL;
|
---|
648 | }
|
---|
649 |
|
---|
650 | INIT_LIST_HEAD(&b->head);
|
---|
651 | b->cEntries = 0;
|
---|
652 | b->cbUsed = 0;
|
---|
653 | b->cbFree = DIR_BUFFER_SIZE;
|
---|
654 | return b;
|
---|
655 | }
|
---|
656 |
|
---|
657 | static void sf_dir_buf_free(struct sf_dir_buf *b)
|
---|
658 | {
|
---|
659 | BUG_ON(!b || !b->buf);
|
---|
660 |
|
---|
661 | TRACE();
|
---|
662 | list_del(&b->head);
|
---|
663 | #ifdef USE_VMALLOC
|
---|
664 | vfree(b->buf);
|
---|
665 | #else
|
---|
666 | kfree(b->buf);
|
---|
667 | #endif
|
---|
668 | kfree(b);
|
---|
669 | }
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Free the directory buffer.
|
---|
673 | */
|
---|
674 | void sf_dir_info_free(struct sf_dir_info *p)
|
---|
675 | {
|
---|
676 | struct list_head *list, *pos, *tmp;
|
---|
677 |
|
---|
678 | TRACE();
|
---|
679 | list = &p->info_list;
|
---|
680 | list_for_each_safe(pos, tmp, list) {
|
---|
681 | struct sf_dir_buf *b;
|
---|
682 |
|
---|
683 | b = list_entry(pos, struct sf_dir_buf, head);
|
---|
684 | sf_dir_buf_free(b);
|
---|
685 | }
|
---|
686 | kfree(p);
|
---|
687 | }
|
---|
688 |
|
---|
689 | /**
|
---|
690 | * Empty (but not free) the directory buffer.
|
---|
691 | */
|
---|
692 | void sf_dir_info_empty(struct sf_dir_info *p)
|
---|
693 | {
|
---|
694 | struct list_head *list, *pos, *tmp;
|
---|
695 | TRACE();
|
---|
696 | list = &p->info_list;
|
---|
697 | list_for_each_safe(pos, tmp, list) {
|
---|
698 | struct sf_dir_buf *b;
|
---|
699 | b = list_entry(pos, struct sf_dir_buf, head);
|
---|
700 | b->cEntries = 0;
|
---|
701 | b->cbUsed = 0;
|
---|
702 | b->cbFree = DIR_BUFFER_SIZE;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Create a new directory buffer descriptor.
|
---|
708 | */
|
---|
709 | struct sf_dir_info *sf_dir_info_alloc(void)
|
---|
710 | {
|
---|
711 | struct sf_dir_info *p;
|
---|
712 |
|
---|
713 | TRACE();
|
---|
714 | p = kmalloc(sizeof(*p), GFP_KERNEL);
|
---|
715 | if (!p) {
|
---|
716 | LogRelFunc(("could not alloc directory info\n"));
|
---|
717 | return NULL;
|
---|
718 | }
|
---|
719 |
|
---|
720 | INIT_LIST_HEAD(&p->info_list);
|
---|
721 | return p;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Search for an empty directory content buffer.
|
---|
726 | */
|
---|
727 | static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
|
---|
728 | {
|
---|
729 | struct list_head *list, *pos;
|
---|
730 |
|
---|
731 | list = &sf_d->info_list;
|
---|
732 | list_for_each(pos, list) {
|
---|
733 | struct sf_dir_buf *b;
|
---|
734 |
|
---|
735 | b = list_entry(pos, struct sf_dir_buf, head);
|
---|
736 | if (!b)
|
---|
737 | return NULL;
|
---|
738 | else {
|
---|
739 | if (b->cbUsed == 0)
|
---|
740 | return b;
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | return NULL;
|
---|
745 | }
|
---|
746 |
|
---|
747 | int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
|
---|
748 | struct sf_dir_info *sf_d, SHFLHANDLE handle)
|
---|
749 | {
|
---|
750 | int err;
|
---|
751 | SHFLSTRING *mask;
|
---|
752 | struct sf_dir_buf *b;
|
---|
753 |
|
---|
754 | TRACE();
|
---|
755 | err = sf_make_path(__func__, sf_i, "*", 1, &mask);
|
---|
756 | if (err)
|
---|
757 | goto fail0;
|
---|
758 |
|
---|
759 | for (;;) {
|
---|
760 | int rc;
|
---|
761 | void *buf;
|
---|
762 | uint32_t cbSize;
|
---|
763 | uint32_t cEntries;
|
---|
764 |
|
---|
765 | b = sf_get_empty_dir_buf(sf_d);
|
---|
766 | if (!b) {
|
---|
767 | b = sf_dir_buf_alloc();
|
---|
768 | if (!b) {
|
---|
769 | err = -ENOMEM;
|
---|
770 | LogRelFunc(("could not alloc directory buffer\n"));
|
---|
771 | goto fail1;
|
---|
772 | }
|
---|
773 | list_add(&b->head, &sf_d->info_list);
|
---|
774 | }
|
---|
775 |
|
---|
776 | buf = b->buf;
|
---|
777 | cbSize = b->cbFree;
|
---|
778 |
|
---|
779 | rc = VbglR0SfDirInfo(&client_handle, &sf_g->map, handle, mask,
|
---|
780 | 0, 0, &cbSize, buf, &cEntries);
|
---|
781 | switch (rc) {
|
---|
782 | case VINF_SUCCESS:
|
---|
783 | RT_FALL_THRU();
|
---|
784 | case VERR_NO_MORE_FILES:
|
---|
785 | break;
|
---|
786 | case VERR_NO_TRANSLATION:
|
---|
787 | LogFunc(("host could not translate entry\n"));
|
---|
788 | /* XXX */
|
---|
789 | break;
|
---|
790 | default:
|
---|
791 | err = -RTErrConvertToErrno(rc);
|
---|
792 | LogFunc(("VbglR0SfDirInfo failed rc=%Rrc\n", rc));
|
---|
793 | goto fail1;
|
---|
794 | }
|
---|
795 |
|
---|
796 | b->cEntries += cEntries;
|
---|
797 | b->cbFree -= cbSize;
|
---|
798 | b->cbUsed += cbSize;
|
---|
799 |
|
---|
800 | if (RT_FAILURE(rc))
|
---|
801 | break;
|
---|
802 | }
|
---|
803 | err = 0;
|
---|
804 |
|
---|
805 | fail1:
|
---|
806 | kfree(mask);
|
---|
807 |
|
---|
808 | fail0:
|
---|
809 | return err;
|
---|
810 | }
|
---|
811 |
|
---|
812 | int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS * stat)
|
---|
813 | {
|
---|
814 | struct sf_glob_info *sf_g;
|
---|
815 | SHFLVOLINFO SHFLVolumeInfo;
|
---|
816 | uint32_t cbBuffer;
|
---|
817 | int rc;
|
---|
818 |
|
---|
819 | sf_g = GET_GLOB_INFO(sb);
|
---|
820 | cbBuffer = sizeof(SHFLVolumeInfo);
|
---|
821 | rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, 0,
|
---|
822 | SHFL_INFO_GET | SHFL_INFO_VOLUME, &cbBuffer,
|
---|
823 | (PSHFLDIRINFO) & SHFLVolumeInfo);
|
---|
824 | if (RT_FAILURE(rc))
|
---|
825 | return -RTErrConvertToErrno(rc);
|
---|
826 |
|
---|
827 | stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
|
---|
828 | stat->f_bsize = SHFLVolumeInfo.ulBytesPerAllocationUnit;
|
---|
829 | stat->f_blocks = SHFLVolumeInfo.ullTotalAllocationBytes
|
---|
830 | / SHFLVolumeInfo.ulBytesPerAllocationUnit;
|
---|
831 | stat->f_bfree = SHFLVolumeInfo.ullAvailableAllocationBytes
|
---|
832 | / SHFLVolumeInfo.ulBytesPerAllocationUnit;
|
---|
833 | stat->f_bavail = SHFLVolumeInfo.ullAvailableAllocationBytes
|
---|
834 | / SHFLVolumeInfo.ulBytesPerAllocationUnit;
|
---|
835 | stat->f_files = 1000;
|
---|
836 | stat->f_ffree = 1000; /* don't return 0 here since the guest may think
|
---|
837 | * that it is not possible to create any more files */
|
---|
838 | stat->f_fsid.val[0] = 0;
|
---|
839 | stat->f_fsid.val[1] = 0;
|
---|
840 | stat->f_namelen = 255;
|
---|
841 | return 0;
|
---|
842 | }
|
---|
843 |
|
---|
844 | struct dentry_operations sf_dentry_ops = {
|
---|
845 | .d_revalidate = sf_dentry_revalidate
|
---|
846 | };
|
---|
847 |
|
---|
848 | int sf_init_backing_dev(struct sf_glob_info *sf_g)
|
---|
849 | {
|
---|
850 | int rc = 0;
|
---|
851 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
|
---|
852 | /* Each new shared folder map gets a new uint64_t identifier,
|
---|
853 | * allocated in sequence. We ASSUME the sequence will not wrap. */
|
---|
854 | static uint64_t s_u64Sequence = 0;
|
---|
855 | uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);
|
---|
856 |
|
---|
857 | sf_g->bdi.ra_pages = 0; /* No readahead */
|
---|
858 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
|
---|
859 | sf_g->bdi.capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
|
---|
860 | | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
|
---|
861 | | BDI_CAP_READ_MAP /* can be mapped for reading */
|
---|
862 | | BDI_CAP_WRITE_MAP /* can be mapped for writing */
|
---|
863 | | BDI_CAP_EXEC_MAP; /* can be mapped for execution */
|
---|
864 | #endif /* >= 2.6.12 */
|
---|
865 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
|
---|
866 | rc = bdi_init(&sf_g->bdi);
|
---|
867 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
|
---|
868 | if (!rc)
|
---|
869 | rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
|
---|
870 | (unsigned long long)u64CurrentSequence);
|
---|
871 | #endif /* >= 2.6.26 */
|
---|
872 | #endif /* >= 2.6.24 */
|
---|
873 | #endif /* >= 2.6.0 && <= 3.19.0 */
|
---|
874 | return rc;
|
---|
875 | }
|
---|
876 |
|
---|
877 | void sf_done_backing_dev(struct sf_glob_info *sf_g)
|
---|
878 | {
|
---|
879 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
|
---|
880 | bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
|
---|
881 | #endif
|
---|
882 | }
|
---|