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