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