1 | /** @file
|
---|
2 | *
|
---|
3 | * vboxsf -- VirtualBox Guest Additions for Linux:
|
---|
4 | * Directory inode and file operations
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "vfsmod.h"
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Open a directory. Read the complete content into a buffer.
|
---|
23 | *
|
---|
24 | * @param inode inode
|
---|
25 | * @param file file
|
---|
26 | * @returns 0 on success, Linux error code otherwise
|
---|
27 | */
|
---|
28 | static int sf_dir_open(struct inode *inode, struct file *file)
|
---|
29 | {
|
---|
30 | int rc;
|
---|
31 | int err;
|
---|
32 | struct sf_glob_info *sf_g = GET_GLOB_INFO(inode->i_sb);
|
---|
33 | struct sf_dir_info *sf_d;
|
---|
34 | struct sf_inode_info *sf_i = GET_INODE_INFO(inode);
|
---|
35 | SHFLCREATEPARMS params;
|
---|
36 |
|
---|
37 | TRACE();
|
---|
38 | BUG_ON(!sf_g);
|
---|
39 | BUG_ON(!sf_i);
|
---|
40 |
|
---|
41 | if (file->private_data)
|
---|
42 | {
|
---|
43 | LogFunc(("sf_dir_open() called on already opened directory '%s'\n",
|
---|
44 | sf_i->path->String.utf8));
|
---|
45 | return 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | sf_d = sf_dir_info_alloc();
|
---|
49 | if (!sf_d)
|
---|
50 | {
|
---|
51 | LogRelFunc(("could not allocate directory info for '%s'\n",
|
---|
52 | sf_i->path->String.utf8));
|
---|
53 | return -ENOMEM;
|
---|
54 | }
|
---|
55 |
|
---|
56 | RT_ZERO(params);
|
---|
57 | params.Handle = SHFL_HANDLE_NIL;
|
---|
58 | params.CreateFlags = 0
|
---|
59 | | SHFL_CF_DIRECTORY
|
---|
60 | | SHFL_CF_ACT_OPEN_IF_EXISTS
|
---|
61 | | SHFL_CF_ACT_FAIL_IF_NEW
|
---|
62 | | SHFL_CF_ACCESS_READ
|
---|
63 | ;
|
---|
64 |
|
---|
65 | LogFunc(("sf_dir_open(): calling vboxCallCreate, folder %s, flags %#x\n",
|
---|
66 | sf_i->path->String.utf8, params.CreateFlags));
|
---|
67 | rc = vboxCallCreate(&client_handle, &sf_g->map, sf_i->path, ¶ms);
|
---|
68 | if (RT_SUCCESS(rc))
|
---|
69 | {
|
---|
70 | if (params.Result == SHFL_FILE_EXISTS)
|
---|
71 | {
|
---|
72 | err = sf_dir_read_all(sf_g, sf_i, sf_d, params.Handle);
|
---|
73 | if (!err)
|
---|
74 | file->private_data = sf_d;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | err = -ENOENT;
|
---|
78 |
|
---|
79 | rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
|
---|
80 | if (RT_FAILURE(rc))
|
---|
81 | LogFunc(("sf_dir_open(): vboxCallClose(%s) after err=%d failed rc=%Rrc\n",
|
---|
82 | sf_i->path->String.utf8, err, rc));
|
---|
83 | }
|
---|
84 | else
|
---|
85 | err = -EPERM;
|
---|
86 |
|
---|
87 | if (err)
|
---|
88 | sf_dir_info_free(sf_d);
|
---|
89 |
|
---|
90 | return err;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * This is called when reference count of [file] goes to zero. Notify
|
---|
96 | * the host that it can free whatever is associated with this directory
|
---|
97 | * and deallocate our own internal buffers
|
---|
98 | *
|
---|
99 | * @param inode inode
|
---|
100 | * @param file file
|
---|
101 | * returns 0 on success, Linux error code otherwise
|
---|
102 | */
|
---|
103 | static int sf_dir_release(struct inode *inode, struct file *file)
|
---|
104 | {
|
---|
105 | TRACE();
|
---|
106 |
|
---|
107 | if (file->private_data)
|
---|
108 | sf_dir_info_free(file->private_data);
|
---|
109 |
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Extract element ([dir]->f_pos) from the directory [dir] into [d_name].
|
---|
115 | *
|
---|
116 | * @returns 0 for success, 1 for end reached, Linux error code otherwise.
|
---|
117 | */
|
---|
118 | static int sf_getdent(struct file *dir, char d_name[NAME_MAX])
|
---|
119 | {
|
---|
120 | loff_t cur;
|
---|
121 | struct sf_glob_info *sf_g;
|
---|
122 | struct sf_dir_info *sf_d;
|
---|
123 | struct sf_inode_info *sf_i;
|
---|
124 | struct inode *inode;
|
---|
125 | struct list_head *pos, *list;
|
---|
126 |
|
---|
127 | TRACE();
|
---|
128 |
|
---|
129 | sf_g = GET_GLOB_INFO(dir->f_dentry->d_inode->i_sb);
|
---|
130 | sf_d = dir->private_data;
|
---|
131 |
|
---|
132 | BUG_ON(!sf_g);
|
---|
133 | BUG_ON(!sf_d);
|
---|
134 |
|
---|
135 | inode = dir->f_dentry->d_inode;
|
---|
136 | sf_i = GET_INODE_INFO(inode);
|
---|
137 |
|
---|
138 | BUG_ON(!sf_i);
|
---|
139 |
|
---|
140 | if (sf_i->force_reread)
|
---|
141 | {
|
---|
142 | int rc;
|
---|
143 | int err;
|
---|
144 | SHFLCREATEPARMS params;
|
---|
145 |
|
---|
146 | RT_ZERO(params);
|
---|
147 | params.Handle = SHFL_HANDLE_NIL;
|
---|
148 | params.CreateFlags = 0
|
---|
149 | | SHFL_CF_DIRECTORY
|
---|
150 | | SHFL_CF_ACT_OPEN_IF_EXISTS
|
---|
151 | | SHFL_CF_ACT_FAIL_IF_NEW
|
---|
152 | | SHFL_CF_ACCESS_READ
|
---|
153 | ;
|
---|
154 |
|
---|
155 | LogFunc(("sf_getdent: calling vboxCallCreate, folder %s, flags %#x\n",
|
---|
156 | sf_i->path->String.utf8, params.CreateFlags));
|
---|
157 | rc = vboxCallCreate(&client_handle, &sf_g->map, sf_i->path, ¶ms);
|
---|
158 | if (RT_FAILURE(rc))
|
---|
159 | {
|
---|
160 | LogFunc(("vboxCallCreate(%s) failed rc=%Rrc\n",
|
---|
161 | sf_i->path->String.utf8, rc));
|
---|
162 | return -EPERM;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (params.Result != SHFL_FILE_EXISTS)
|
---|
166 | {
|
---|
167 | LogFunc(("directory %s does not exist\n", sf_i->path->String.utf8));
|
---|
168 | sf_dir_info_free(sf_d);
|
---|
169 | return -ENOENT;
|
---|
170 | }
|
---|
171 |
|
---|
172 | sf_dir_info_empty(sf_d);
|
---|
173 | err = sf_dir_read_all(sf_g, sf_i, sf_d, params.Handle);
|
---|
174 | rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
|
---|
175 | if (RT_FAILURE(rc))
|
---|
176 | LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));
|
---|
177 | if (err)
|
---|
178 | return err;
|
---|
179 |
|
---|
180 | sf_i->force_reread = 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | cur = 0;
|
---|
184 | list = &sf_d->info_list;
|
---|
185 | list_for_each(pos, list)
|
---|
186 | {
|
---|
187 | struct sf_dir_buf *b;
|
---|
188 | SHFLDIRINFO *info;
|
---|
189 | loff_t i;
|
---|
190 |
|
---|
191 | b = list_entry(pos, struct sf_dir_buf, head);
|
---|
192 | if (dir->f_pos >= cur + b->cEntries)
|
---|
193 | {
|
---|
194 | cur += b->cEntries;
|
---|
195 | continue;
|
---|
196 | }
|
---|
197 |
|
---|
198 | for (i = 0, info = b->buf; i < dir->f_pos - cur; ++i)
|
---|
199 | {
|
---|
200 | size_t size;
|
---|
201 |
|
---|
202 | size = offsetof(SHFLDIRINFO, name.String) + info->name.u16Size;
|
---|
203 | info = (SHFLDIRINFO *) ((uintptr_t) info + size);
|
---|
204 | }
|
---|
205 |
|
---|
206 | return sf_nlscpy(sf_g, d_name, NAME_MAX,
|
---|
207 | info->name.String.utf8, info->name.u16Length);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return 1;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * This is called when vfs wants to populate internal buffers with
|
---|
215 | * directory [dir]s contents. [opaque] is an argument to the
|
---|
216 | * [filldir]. [filldir] magically modifies it's argument - [opaque]
|
---|
217 | * and takes following additional arguments (which i in turn get from
|
---|
218 | * the host via sf_getdent):
|
---|
219 | *
|
---|
220 | * name : name of the entry (i must also supply it's length huh?)
|
---|
221 | * type : type of the entry (FILE | DIR | etc) (i ellect to use DT_UNKNOWN)
|
---|
222 | * pos : position/index of the entry
|
---|
223 | * ino : inode number of the entry (i fake those)
|
---|
224 | *
|
---|
225 | * [dir] contains:
|
---|
226 | * f_pos : cursor into the directory listing
|
---|
227 | * private_data : mean of communication with the host side
|
---|
228 | *
|
---|
229 | * Extract elements from the directory listing (incrementing f_pos
|
---|
230 | * along the way) and feed them to [filldir] until:
|
---|
231 | *
|
---|
232 | * a. there are no more entries (i.e. sf_getdent set done to 1)
|
---|
233 | * b. failure to compute fake inode number
|
---|
234 | * c. filldir returns an error (see comment on that)
|
---|
235 | */
|
---|
236 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
237 | static int sf_dir_iterate(struct file *dir, struct dir_context *ctx)
|
---|
238 | #else
|
---|
239 | static int sf_dir_read(struct file *dir, void *opaque, filldir_t filldir)
|
---|
240 | #endif
|
---|
241 | {
|
---|
242 | TRACE();
|
---|
243 | for (;;)
|
---|
244 | {
|
---|
245 | int err;
|
---|
246 | ino_t fake_ino;
|
---|
247 | loff_t sanity;
|
---|
248 | char d_name[NAME_MAX];
|
---|
249 |
|
---|
250 | err = sf_getdent(dir, d_name);
|
---|
251 | switch (err)
|
---|
252 | {
|
---|
253 | case 1:
|
---|
254 | return 0;
|
---|
255 |
|
---|
256 | case 0:
|
---|
257 | break;
|
---|
258 |
|
---|
259 | case -1:
|
---|
260 | default:
|
---|
261 | /* skip erroneous entry and proceed */
|
---|
262 | LogFunc(("sf_getdent error %d\n", err));
|
---|
263 | dir->f_pos += 1;
|
---|
264 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
265 | ctx->pos += 1;
|
---|
266 | #endif
|
---|
267 | continue;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* d_name now contains a valid entry name */
|
---|
271 |
|
---|
272 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
273 | sanity = ctx->pos + 0xbeef;
|
---|
274 | #else
|
---|
275 | sanity = dir->f_pos + 0xbeef;
|
---|
276 | #endif
|
---|
277 | fake_ino = sanity;
|
---|
278 | if (sanity - fake_ino)
|
---|
279 | {
|
---|
280 | LogRelFunc(("can not compute ino\n"));
|
---|
281 | return -EINVAL;
|
---|
282 | }
|
---|
283 |
|
---|
284 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
285 | if (!dir_emit(ctx, d_name, strlen(d_name), fake_ino, DT_UNKNOWN))
|
---|
286 | {
|
---|
287 | LogFunc(("dir_emit failed\n"));
|
---|
288 | return 0;
|
---|
289 | }
|
---|
290 | #else
|
---|
291 | err = filldir(opaque, d_name, strlen(d_name), dir->f_pos, fake_ino, DT_UNKNOWN);
|
---|
292 | if (err)
|
---|
293 | {
|
---|
294 | LogFunc(("filldir returned error %d\n", err));
|
---|
295 | /* Rely on the fact that filldir returns error
|
---|
296 | only when it runs out of space in opaque */
|
---|
297 | return 0;
|
---|
298 | }
|
---|
299 | #endif
|
---|
300 |
|
---|
301 | dir->f_pos += 1;
|
---|
302 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
303 | ctx->pos += 1;
|
---|
304 | #endif
|
---|
305 | }
|
---|
306 |
|
---|
307 | BUG();
|
---|
308 | }
|
---|
309 |
|
---|
310 | struct file_operations sf_dir_fops =
|
---|
311 | {
|
---|
312 | .open = sf_dir_open,
|
---|
313 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
|
---|
314 | .iterate = sf_dir_iterate,
|
---|
315 | #else
|
---|
316 | .readdir = sf_dir_read,
|
---|
317 | #endif
|
---|
318 | .release = sf_dir_release,
|
---|
319 | .read = generic_read_dir
|
---|
320 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
|
---|
321 | , .llseek = generic_file_llseek
|
---|
322 | #endif
|
---|
323 | };
|
---|
324 |
|
---|
325 |
|
---|
326 | /* iops */
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * This is called when vfs failed to locate dentry in the cache. The
|
---|
330 | * job of this function is to allocate inode and link it to dentry.
|
---|
331 | * [dentry] contains the name to be looked in the [parent] directory.
|
---|
332 | * Failure to locate the name is not a "hard" error, in this case NULL
|
---|
333 | * inode is added to [dentry] and vfs should proceed trying to create
|
---|
334 | * the entry via other means. NULL(or "positive" pointer) ought to be
|
---|
335 | * returned in case of success and "negative" pointer on error
|
---|
336 | */
|
---|
337 | static struct dentry *sf_lookup(struct inode *parent, struct dentry *dentry
|
---|
338 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
|
---|
339 | , unsigned int flags
|
---|
340 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
341 | , struct nameidata *nd
|
---|
342 | #endif
|
---|
343 | )
|
---|
344 | {
|
---|
345 | int err;
|
---|
346 | struct sf_inode_info *sf_i, *sf_new_i;
|
---|
347 | struct sf_glob_info *sf_g;
|
---|
348 | SHFLSTRING *path;
|
---|
349 | struct inode *inode;
|
---|
350 | ino_t ino;
|
---|
351 | SHFLFSOBJINFO fsinfo;
|
---|
352 |
|
---|
353 | TRACE();
|
---|
354 | sf_g = GET_GLOB_INFO(parent->i_sb);
|
---|
355 | sf_i = GET_INODE_INFO(parent);
|
---|
356 |
|
---|
357 | BUG_ON(!sf_g);
|
---|
358 | BUG_ON(!sf_i);
|
---|
359 |
|
---|
360 | err = sf_path_from_dentry(__func__, sf_g, sf_i, dentry, &path);
|
---|
361 | if (err)
|
---|
362 | goto fail0;
|
---|
363 |
|
---|
364 | err = sf_stat(__func__, sf_g, path, &fsinfo, 1);
|
---|
365 | if (err)
|
---|
366 | {
|
---|
367 | if (err == -ENOENT)
|
---|
368 | {
|
---|
369 | /* -ENOENT: add NULL inode to dentry so it later can be
|
---|
370 | created via call to create/mkdir/open */
|
---|
371 | kfree(path);
|
---|
372 | inode = NULL;
|
---|
373 | }
|
---|
374 | else
|
---|
375 | goto fail1;
|
---|
376 | }
|
---|
377 | else
|
---|
378 | {
|
---|
379 | sf_new_i = kmalloc(sizeof(*sf_new_i), GFP_KERNEL);
|
---|
380 | if (!sf_new_i)
|
---|
381 | {
|
---|
382 | LogRelFunc(("could not allocate memory for new inode info\n"));
|
---|
383 | err = -ENOMEM;
|
---|
384 | goto fail1;
|
---|
385 | }
|
---|
386 | sf_new_i->handle = SHFL_HANDLE_NIL;
|
---|
387 | sf_new_i->force_reread = 0;
|
---|
388 |
|
---|
389 | ino = iunique(parent->i_sb, 1);
|
---|
390 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
|
---|
391 | inode = iget_locked(parent->i_sb, ino);
|
---|
392 | #else
|
---|
393 | inode = iget(parent->i_sb, ino);
|
---|
394 | #endif
|
---|
395 | if (!inode)
|
---|
396 | {
|
---|
397 | LogFunc(("iget failed\n"));
|
---|
398 | err = -ENOMEM; /* XXX: ??? */
|
---|
399 | goto fail2;
|
---|
400 | }
|
---|
401 |
|
---|
402 | SET_INODE_INFO(inode, sf_new_i);
|
---|
403 | sf_init_inode(sf_g, inode, &fsinfo);
|
---|
404 | sf_new_i->path = path;
|
---|
405 |
|
---|
406 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
|
---|
407 | unlock_new_inode(inode);
|
---|
408 | #endif
|
---|
409 | }
|
---|
410 |
|
---|
411 | sf_i->force_restat = 0;
|
---|
412 | dentry->d_time = jiffies;
|
---|
413 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
|
---|
414 | d_set_d_op(dentry, &sf_dentry_ops);
|
---|
415 | #else
|
---|
416 | dentry->d_op = &sf_dentry_ops;
|
---|
417 | #endif
|
---|
418 | d_add(dentry, inode);
|
---|
419 | return NULL;
|
---|
420 |
|
---|
421 | fail2:
|
---|
422 | kfree(sf_new_i);
|
---|
423 |
|
---|
424 | fail1:
|
---|
425 | kfree(path);
|
---|
426 |
|
---|
427 | fail0:
|
---|
428 | return ERR_PTR(err);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * This should allocate memory for sf_inode_info, compute a unique inode
|
---|
433 | * number, get an inode from vfs, initialize inode info, instantiate
|
---|
434 | * dentry.
|
---|
435 | *
|
---|
436 | * @param parent inode entry of the directory
|
---|
437 | * @param dentry directory cache entry
|
---|
438 | * @param path path name
|
---|
439 | * @param info file information
|
---|
440 | * @param handle handle
|
---|
441 | * @returns 0 on success, Linux error code otherwise
|
---|
442 | */
|
---|
443 | static int sf_instantiate(struct inode *parent, struct dentry *dentry,
|
---|
444 | SHFLSTRING *path, PSHFLFSOBJINFO info, SHFLHANDLE handle)
|
---|
445 | {
|
---|
446 | int err;
|
---|
447 | ino_t ino;
|
---|
448 | struct inode *inode;
|
---|
449 | struct sf_inode_info *sf_new_i;
|
---|
450 | struct sf_glob_info *sf_g = GET_GLOB_INFO(parent->i_sb);
|
---|
451 |
|
---|
452 | TRACE();
|
---|
453 | BUG_ON(!sf_g);
|
---|
454 |
|
---|
455 | sf_new_i = kmalloc(sizeof(*sf_new_i), GFP_KERNEL);
|
---|
456 | if (!sf_new_i)
|
---|
457 | {
|
---|
458 | LogRelFunc(("could not allocate inode info.\n"));
|
---|
459 | err = -ENOMEM;
|
---|
460 | goto fail0;
|
---|
461 | }
|
---|
462 |
|
---|
463 | ino = iunique(parent->i_sb, 1);
|
---|
464 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
|
---|
465 | inode = iget_locked(parent->i_sb, ino);
|
---|
466 | #else
|
---|
467 | inode = iget(parent->i_sb, ino);
|
---|
468 | #endif
|
---|
469 | if (!inode)
|
---|
470 | {
|
---|
471 | LogFunc(("iget failed\n"));
|
---|
472 | err = -ENOMEM;
|
---|
473 | goto fail1;
|
---|
474 | }
|
---|
475 |
|
---|
476 | sf_init_inode(sf_g, inode, info);
|
---|
477 | sf_new_i->path = path;
|
---|
478 | SET_INODE_INFO(inode, sf_new_i);
|
---|
479 | sf_new_i->force_restat = 1;
|
---|
480 | sf_new_i->force_reread = 0;
|
---|
481 |
|
---|
482 | d_instantiate(dentry, inode);
|
---|
483 |
|
---|
484 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
|
---|
485 | unlock_new_inode(inode);
|
---|
486 | #endif
|
---|
487 |
|
---|
488 | /* Store this handle if we leave the handle open. */
|
---|
489 | sf_new_i->handle = handle;
|
---|
490 | return 0;
|
---|
491 |
|
---|
492 | fail1:
|
---|
493 | kfree(sf_new_i);
|
---|
494 |
|
---|
495 | fail0:
|
---|
496 | return err;
|
---|
497 |
|
---|
498 | }
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Create a new regular file / directory.
|
---|
502 | *
|
---|
503 | * @param parent inode of the directory
|
---|
504 | * @param dentry directory cache entry
|
---|
505 | * @param mode file mode
|
---|
506 | * @param fDirectory true if directory, false otherwise
|
---|
507 | * @returns 0 on success, Linux error code otherwise
|
---|
508 | */
|
---|
509 | static int sf_create_aux(struct inode *parent, struct dentry *dentry,
|
---|
510 | umode_t mode, int fDirectory)
|
---|
511 | {
|
---|
512 | int rc, err;
|
---|
513 | SHFLCREATEPARMS params;
|
---|
514 | SHFLSTRING *path;
|
---|
515 | struct sf_inode_info *sf_i = GET_INODE_INFO(parent);
|
---|
516 | struct sf_glob_info *sf_g = GET_GLOB_INFO(parent->i_sb);
|
---|
517 |
|
---|
518 | TRACE();
|
---|
519 | BUG_ON(!sf_i);
|
---|
520 | BUG_ON(!sf_g);
|
---|
521 |
|
---|
522 | err = sf_path_from_dentry(__func__, sf_g, sf_i, dentry, &path);
|
---|
523 | if (err)
|
---|
524 | goto fail0;
|
---|
525 |
|
---|
526 | RT_ZERO(params);
|
---|
527 | params.Handle = SHFL_HANDLE_NIL;
|
---|
528 | params.CreateFlags = 0
|
---|
529 | | SHFL_CF_ACT_CREATE_IF_NEW
|
---|
530 | | SHFL_CF_ACT_FAIL_IF_EXISTS
|
---|
531 | | SHFL_CF_ACCESS_READWRITE
|
---|
532 | | (fDirectory ? SHFL_CF_DIRECTORY : 0)
|
---|
533 | ;
|
---|
534 | params.Info.Attr.fMode = 0
|
---|
535 | | (fDirectory ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE)
|
---|
536 | | (mode & S_IRWXUGO)
|
---|
537 | ;
|
---|
538 | params.Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
|
---|
539 |
|
---|
540 | LogFunc(("sf_create_aux: calling vboxCallCreate, folder %s, flags %#x\n",
|
---|
541 | path->String.utf8, params.CreateFlags));
|
---|
542 | rc = vboxCallCreate(&client_handle, &sf_g->map, path, ¶ms);
|
---|
543 | if (RT_FAILURE(rc))
|
---|
544 | {
|
---|
545 | if (rc == VERR_WRITE_PROTECT)
|
---|
546 | {
|
---|
547 | err = -EROFS;
|
---|
548 | goto fail1;
|
---|
549 | }
|
---|
550 | err = -EPROTO;
|
---|
551 | LogFunc(("(%d): vboxCallCreate(%s) failed rc=%Rrc\n",
|
---|
552 | fDirectory, sf_i->path->String.utf8, rc));
|
---|
553 | goto fail1;
|
---|
554 | }
|
---|
555 |
|
---|
556 | if (params.Result != SHFL_FILE_CREATED)
|
---|
557 | {
|
---|
558 | err = -EPERM;
|
---|
559 | LogFunc(("(%d): could not create file %s result=%d\n",
|
---|
560 | fDirectory, sf_i->path->String.utf8, params.Result));
|
---|
561 | goto fail1;
|
---|
562 | }
|
---|
563 |
|
---|
564 | err = sf_instantiate(parent, dentry, path, ¶ms.Info,
|
---|
565 | fDirectory ? SHFL_HANDLE_NIL : params.Handle);
|
---|
566 | if (err)
|
---|
567 | {
|
---|
568 | LogFunc(("(%d): could not instantiate dentry for %s err=%d\n",
|
---|
569 | fDirectory, sf_i->path->String.utf8, err));
|
---|
570 | goto fail2;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Don't close this handle right now. We assume that the same file is
|
---|
575 | * opened with sf_reg_open() and later closed with sf_reg_close(). Save
|
---|
576 | * the handle in between. Does not apply to directories. True?
|
---|
577 | */
|
---|
578 | if (fDirectory)
|
---|
579 | {
|
---|
580 | rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
|
---|
581 | if (RT_FAILURE(rc))
|
---|
582 | LogFunc(("(%d): vboxCallClose failed rc=%Rrc\n", fDirectory, rc));
|
---|
583 | }
|
---|
584 |
|
---|
585 | sf_i->force_restat = 1;
|
---|
586 | return 0;
|
---|
587 |
|
---|
588 | fail2:
|
---|
589 | rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
|
---|
590 | if (RT_FAILURE(rc))
|
---|
591 | LogFunc(("(%d): vboxCallClose failed rc=%Rrc\n", fDirectory, rc));
|
---|
592 |
|
---|
593 | fail1:
|
---|
594 | kfree(path);
|
---|
595 |
|
---|
596 | fail0:
|
---|
597 | return err;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Create a new regular file.
|
---|
602 | *
|
---|
603 | * @param parent inode of the directory
|
---|
604 | * @param dentry directory cache entry
|
---|
605 | * @param mode file mode
|
---|
606 | * @returns 0 on success, Linux error code otherwise
|
---|
607 | */
|
---|
608 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
|
---|
609 | static int sf_create(struct inode *parent, struct dentry *dentry, umode_t mode, bool excl)
|
---|
610 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
|
---|
611 | static int sf_create(struct inode *parent, struct dentry *dentry, umode_t mode, struct nameidata *nd)
|
---|
612 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
613 | static int sf_create(struct inode *parent, struct dentry *dentry, int mode, struct nameidata *nd)
|
---|
614 | #else
|
---|
615 | static int sf_create(struct inode *parent, struct dentry *dentry, int mode)
|
---|
616 | #endif
|
---|
617 | {
|
---|
618 | TRACE();
|
---|
619 | return sf_create_aux(parent, dentry, mode, 0);
|
---|
620 | }
|
---|
621 |
|
---|
622 | /**
|
---|
623 | * Create a new directory.
|
---|
624 | *
|
---|
625 | * @param parent inode of the directory
|
---|
626 | * @param dentry directory cache entry
|
---|
627 | * @param mode file mode
|
---|
628 | * @returns 0 on success, Linux error code otherwise
|
---|
629 | */
|
---|
630 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
|
---|
631 | static int sf_mkdir(struct inode *parent, struct dentry *dentry, umode_t mode)
|
---|
632 | #else
|
---|
633 | static int sf_mkdir(struct inode *parent, struct dentry *dentry, int mode)
|
---|
634 | #endif
|
---|
635 | {
|
---|
636 | TRACE();
|
---|
637 | return sf_create_aux(parent, dentry, mode, 1);
|
---|
638 | }
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Remove a regular file / directory.
|
---|
642 | *
|
---|
643 | * @param parent inode of the directory
|
---|
644 | * @param dentry directory cache entry
|
---|
645 | * @param fDirectory true if directory, false otherwise
|
---|
646 | * @returns 0 on success, Linux error code otherwise
|
---|
647 | */
|
---|
648 | static int sf_unlink_aux(struct inode *parent, struct dentry *dentry, int fDirectory)
|
---|
649 | {
|
---|
650 | int rc, err;
|
---|
651 | struct sf_glob_info *sf_g = GET_GLOB_INFO(parent->i_sb);
|
---|
652 | struct sf_inode_info *sf_i = GET_INODE_INFO(parent);
|
---|
653 | SHFLSTRING *path;
|
---|
654 | uint32_t fFlags;
|
---|
655 |
|
---|
656 | TRACE();
|
---|
657 | BUG_ON(!sf_g);
|
---|
658 |
|
---|
659 | err = sf_path_from_dentry(__func__, sf_g, sf_i, dentry, &path);
|
---|
660 | if (err)
|
---|
661 | goto fail0;
|
---|
662 |
|
---|
663 | fFlags = fDirectory ? SHFL_REMOVE_DIR : SHFL_REMOVE_FILE;
|
---|
664 | if ( dentry
|
---|
665 | && dentry->d_inode
|
---|
666 | && ((dentry->d_inode->i_mode & S_IFLNK) == S_IFLNK))
|
---|
667 | fFlags |= SHFL_REMOVE_SYMLINK;
|
---|
668 | rc = vboxCallRemove(&client_handle, &sf_g->map, path, fFlags);
|
---|
669 | if (RT_FAILURE(rc))
|
---|
670 | {
|
---|
671 | LogFunc(("(%d): vboxCallRemove(%s) failed rc=%Rrc\n", fDirectory,
|
---|
672 | path->String.utf8, rc));
|
---|
673 | err = -RTErrConvertToErrno(rc);
|
---|
674 | goto fail1;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* directory access/change time changed */
|
---|
678 | sf_i->force_restat = 1;
|
---|
679 | /* directory content changed */
|
---|
680 | sf_i->force_reread = 1;
|
---|
681 |
|
---|
682 | err = 0;
|
---|
683 |
|
---|
684 | fail1:
|
---|
685 | kfree(path);
|
---|
686 |
|
---|
687 | fail0:
|
---|
688 | return err;
|
---|
689 | }
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Remove a regular file.
|
---|
693 | *
|
---|
694 | * @param parent inode of the directory
|
---|
695 | * @param dentry directory cache entry
|
---|
696 | * @returns 0 on success, Linux error code otherwise
|
---|
697 | */
|
---|
698 | static int sf_unlink(struct inode *parent, struct dentry *dentry)
|
---|
699 | {
|
---|
700 | TRACE();
|
---|
701 | return sf_unlink_aux(parent, dentry, 0);
|
---|
702 | }
|
---|
703 |
|
---|
704 | /**
|
---|
705 | * Remove a directory.
|
---|
706 | *
|
---|
707 | * @param parent inode of the directory
|
---|
708 | * @param dentry directory cache entry
|
---|
709 | * @returns 0 on success, Linux error code otherwise
|
---|
710 | */
|
---|
711 | static int sf_rmdir(struct inode *parent, struct dentry *dentry)
|
---|
712 | {
|
---|
713 | TRACE();
|
---|
714 | return sf_unlink_aux(parent, dentry, 1);
|
---|
715 | }
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Rename a regular file / directory.
|
---|
719 | *
|
---|
720 | * @param old_parent inode of the old parent directory
|
---|
721 | * @param old_dentry old directory cache entry
|
---|
722 | * @param new_parent inode of the new parent directory
|
---|
723 | * @param new_dentry new directory cache entry
|
---|
724 | * @returns 0 on success, Linux error code otherwise
|
---|
725 | */
|
---|
726 | static int sf_rename(struct inode *old_parent, struct dentry *old_dentry,
|
---|
727 | struct inode *new_parent, struct dentry *new_dentry)
|
---|
728 | {
|
---|
729 | int err = 0, rc = VINF_SUCCESS;
|
---|
730 | struct sf_glob_info *sf_g = GET_GLOB_INFO(old_parent->i_sb);
|
---|
731 |
|
---|
732 | TRACE();
|
---|
733 |
|
---|
734 | if (sf_g != GET_GLOB_INFO(new_parent->i_sb))
|
---|
735 | {
|
---|
736 | LogFunc(("rename with different roots\n"));
|
---|
737 | err = -EINVAL;
|
---|
738 | }
|
---|
739 | else
|
---|
740 | {
|
---|
741 | struct sf_inode_info *sf_old_i = GET_INODE_INFO(old_parent);
|
---|
742 | struct sf_inode_info *sf_new_i = GET_INODE_INFO(new_parent);
|
---|
743 | /* As we save the relative path inside the inode structure, we need to change
|
---|
744 | this if the rename is successful. */
|
---|
745 | struct sf_inode_info *sf_file_i = GET_INODE_INFO(old_dentry->d_inode);
|
---|
746 | SHFLSTRING *old_path;
|
---|
747 | SHFLSTRING *new_path;
|
---|
748 |
|
---|
749 | BUG_ON(!sf_old_i);
|
---|
750 | BUG_ON(!sf_new_i);
|
---|
751 | BUG_ON(!sf_file_i);
|
---|
752 |
|
---|
753 | old_path = sf_file_i->path;
|
---|
754 | err = sf_path_from_dentry(__func__, sf_g, sf_new_i,
|
---|
755 | new_dentry, &new_path);
|
---|
756 | if (err)
|
---|
757 | LogFunc(("failed to create new path\n"));
|
---|
758 | else
|
---|
759 | {
|
---|
760 | int fDir = ((old_dentry->d_inode->i_mode & S_IFDIR) != 0);
|
---|
761 |
|
---|
762 | rc = vboxCallRename(&client_handle, &sf_g->map, old_path,
|
---|
763 | new_path, fDir ? 0 : SHFL_RENAME_FILE | SHFL_RENAME_REPLACE_IF_EXISTS);
|
---|
764 | if (RT_SUCCESS(rc))
|
---|
765 | {
|
---|
766 | kfree(old_path);
|
---|
767 | sf_new_i->force_restat = 1;
|
---|
768 | sf_old_i->force_restat = 1; /* XXX: needed? */
|
---|
769 | /* Set the new relative path in the inode. */
|
---|
770 | sf_file_i->path = new_path;
|
---|
771 | }
|
---|
772 | else
|
---|
773 | {
|
---|
774 | LogFunc(("vboxCallRename failed rc=%Rrc\n", rc));
|
---|
775 | err = -RTErrConvertToErrno(rc);
|
---|
776 | kfree(new_path);
|
---|
777 | }
|
---|
778 | }
|
---|
779 | }
|
---|
780 | return err;
|
---|
781 | }
|
---|
782 |
|
---|
783 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
784 | static int sf_symlink(struct inode *parent, struct dentry *dentry, const char *symname)
|
---|
785 | {
|
---|
786 | int err;
|
---|
787 | int rc;
|
---|
788 | struct sf_inode_info *sf_i;
|
---|
789 | struct sf_glob_info *sf_g;
|
---|
790 | SHFLSTRING *path, *ssymname;
|
---|
791 | SHFLFSOBJINFO info;
|
---|
792 | int symname_len = strlen(symname) + 1;
|
---|
793 |
|
---|
794 | TRACE();
|
---|
795 | sf_g = GET_GLOB_INFO(parent->i_sb);
|
---|
796 | sf_i = GET_INODE_INFO(parent);
|
---|
797 |
|
---|
798 | BUG_ON(!sf_g);
|
---|
799 | BUG_ON(!sf_i);
|
---|
800 |
|
---|
801 | err = sf_path_from_dentry(__func__, sf_g, sf_i, dentry, &path);
|
---|
802 | if (err)
|
---|
803 | goto fail0;
|
---|
804 |
|
---|
805 | ssymname = kmalloc(offsetof(SHFLSTRING, String.utf8) + symname_len, GFP_KERNEL);
|
---|
806 | if (!ssymname)
|
---|
807 | {
|
---|
808 | LogRelFunc(("kmalloc failed, caller=sf_symlink\n"));
|
---|
809 | err = -ENOMEM;
|
---|
810 | goto fail1;
|
---|
811 | }
|
---|
812 |
|
---|
813 | ssymname->u16Length = symname_len - 1;
|
---|
814 | ssymname->u16Size = symname_len;
|
---|
815 | memcpy(ssymname->String.utf8, symname, symname_len);
|
---|
816 |
|
---|
817 | rc = vboxCallSymlink(&client_handle, &sf_g->map, path, ssymname, &info);
|
---|
818 | kfree(ssymname);
|
---|
819 |
|
---|
820 | if (RT_FAILURE(rc))
|
---|
821 | {
|
---|
822 | if (rc == VERR_WRITE_PROTECT)
|
---|
823 | {
|
---|
824 | err = -EROFS;
|
---|
825 | goto fail1;
|
---|
826 | }
|
---|
827 | LogFunc(("vboxCallSymlink(%s) failed rc=%Rrc\n",
|
---|
828 | sf_i->path->String.utf8, rc));
|
---|
829 | err = -EPROTO;
|
---|
830 | goto fail1;
|
---|
831 | }
|
---|
832 |
|
---|
833 | err = sf_instantiate(parent, dentry, path, &info, SHFL_HANDLE_NIL);
|
---|
834 | if (err)
|
---|
835 | {
|
---|
836 | LogFunc(("could not instantiate dentry for %s err=%d\n",
|
---|
837 | sf_i->path->String.utf8, err));
|
---|
838 | goto fail1;
|
---|
839 | }
|
---|
840 |
|
---|
841 | sf_i->force_restat = 1;
|
---|
842 | return 0;
|
---|
843 |
|
---|
844 | fail1:
|
---|
845 | kfree(path);
|
---|
846 | fail0:
|
---|
847 | return err;
|
---|
848 | }
|
---|
849 | #endif
|
---|
850 |
|
---|
851 | struct inode_operations sf_dir_iops =
|
---|
852 | {
|
---|
853 | .lookup = sf_lookup,
|
---|
854 | .create = sf_create,
|
---|
855 | .mkdir = sf_mkdir,
|
---|
856 | .rmdir = sf_rmdir,
|
---|
857 | .unlink = sf_unlink,
|
---|
858 | .rename = sf_rename,
|
---|
859 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
|
---|
860 | .revalidate = sf_inode_revalidate
|
---|
861 | #else
|
---|
862 | .getattr = sf_getattr,
|
---|
863 | .setattr = sf_setattr,
|
---|
864 | .symlink = sf_symlink
|
---|
865 | #endif
|
---|
866 | };
|
---|