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