blob: 0d85959be0d55f3f25cb5bf310f0926626315628 [file] [log] [blame]
Hans de Goede0fd16952019-12-12 15:09:14 +01001// SPDX-License-Identifier: MIT
2/*
3 * VirtualBox Guest Shared Folders support: Directory inode and file operations
4 *
5 * Copyright (C) 2006-2018 Oracle Corporation
6 */
7
8#include <linux/namei.h>
9#include <linux/vbox_utils.h>
10#include "vfsmod.h"
11
12static int vboxsf_dir_open(struct inode *inode, struct file *file)
13{
14 struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
15 struct shfl_createparms params = {};
16 struct vboxsf_dir_info *sf_d;
17 int err;
18
19 sf_d = vboxsf_dir_info_alloc();
20 if (!sf_d)
21 return -ENOMEM;
22
23 params.handle = SHFL_HANDLE_NIL;
24 params.create_flags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_OPEN_IF_EXISTS |
25 SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_READ;
26
27 err = vboxsf_create_at_dentry(file_dentry(file), &params);
28 if (err)
29 goto err_free_dir_info;
30
31 if (params.result != SHFL_FILE_EXISTS) {
32 err = -ENOENT;
33 goto err_close;
34 }
35
36 err = vboxsf_dir_read_all(sbi, sf_d, params.handle);
37 if (err)
38 goto err_close;
39
40 vboxsf_close(sbi->root, params.handle);
41 file->private_data = sf_d;
42 return 0;
43
44err_close:
45 vboxsf_close(sbi->root, params.handle);
46err_free_dir_info:
47 vboxsf_dir_info_free(sf_d);
48 return err;
49}
50
51static int vboxsf_dir_release(struct inode *inode, struct file *file)
52{
53 if (file->private_data)
54 vboxsf_dir_info_free(file->private_data);
55
56 return 0;
57}
58
59static unsigned int vboxsf_get_d_type(u32 mode)
60{
61 unsigned int d_type;
62
63 switch (mode & SHFL_TYPE_MASK) {
64 case SHFL_TYPE_FIFO:
65 d_type = DT_FIFO;
66 break;
67 case SHFL_TYPE_DEV_CHAR:
68 d_type = DT_CHR;
69 break;
70 case SHFL_TYPE_DIRECTORY:
71 d_type = DT_DIR;
72 break;
73 case SHFL_TYPE_DEV_BLOCK:
74 d_type = DT_BLK;
75 break;
76 case SHFL_TYPE_FILE:
77 d_type = DT_REG;
78 break;
79 case SHFL_TYPE_SYMLINK:
80 d_type = DT_LNK;
81 break;
82 case SHFL_TYPE_SOCKET:
83 d_type = DT_SOCK;
84 break;
85 case SHFL_TYPE_WHITEOUT:
86 d_type = DT_WHT;
87 break;
88 default:
89 d_type = DT_UNKNOWN;
90 break;
91 }
92 return d_type;
93}
94
95static bool vboxsf_dir_emit(struct file *dir, struct dir_context *ctx)
96{
97 struct vboxsf_sbi *sbi = VBOXSF_SBI(file_inode(dir)->i_sb);
98 struct vboxsf_dir_info *sf_d = dir->private_data;
99 struct shfl_dirinfo *info;
100 struct vboxsf_dir_buf *b;
101 unsigned int d_type;
102 loff_t i, cur = 0;
103 ino_t fake_ino;
104 void *end;
105 int err;
106
107 list_for_each_entry(b, &sf_d->info_list, head) {
108try_next_entry:
109 if (ctx->pos >= cur + b->entries) {
110 cur += b->entries;
111 continue;
112 }
113
114 /*
115 * Note the vboxsf_dir_info objects we are iterating over here
116 * are variable sized, so the info pointer may end up being
117 * unaligned. This is how we get the data from the host.
118 * Since vboxsf is only supported on x86 machines this is not
119 * a problem.
120 */
121 for (i = 0, info = b->buf; i < ctx->pos - cur; i++) {
122 end = &info->name.string.utf8[info->name.size];
123 /* Only happens if the host gives us corrupt data */
124 if (WARN_ON(end > (b->buf + b->used)))
125 return false;
126 info = end;
127 }
128
129 end = &info->name.string.utf8[info->name.size];
130 if (WARN_ON(end > (b->buf + b->used)))
131 return false;
132
133 /* Info now points to the right entry, emit it. */
134 d_type = vboxsf_get_d_type(info->info.attr.mode);
135
136 /*
Chucheng Luo15f9f632020-03-14 12:13:11 +0800137 * On 32-bit systems pos is 64-bit signed, while ino is 32-bit
Hans de Goede0fd16952019-12-12 15:09:14 +0100138 * unsigned so fake_ino may overflow, check for this.
139 */
140 if ((ino_t)(ctx->pos + 1) != (u64)(ctx->pos + 1)) {
141 vbg_err("vboxsf: fake ino overflow, truncating dir\n");
142 return false;
143 }
144 fake_ino = ctx->pos + 1;
145
146 if (sbi->nls) {
147 char d_name[NAME_MAX];
148
149 err = vboxsf_nlscpy(sbi, d_name, NAME_MAX,
150 info->name.string.utf8,
151 info->name.length);
152 if (err) {
153 /* skip erroneous entry and proceed */
154 ctx->pos += 1;
155 goto try_next_entry;
156 }
157
158 return dir_emit(ctx, d_name, strlen(d_name),
159 fake_ino, d_type);
160 }
161
162 return dir_emit(ctx, info->name.string.utf8, info->name.length,
163 fake_ino, d_type);
164 }
165
166 return false;
167}
168
169static int vboxsf_dir_iterate(struct file *dir, struct dir_context *ctx)
170{
171 bool emitted;
172
173 do {
174 emitted = vboxsf_dir_emit(dir, ctx);
175 if (emitted)
176 ctx->pos += 1;
177 } while (emitted);
178
179 return 0;
180}
181
182const struct file_operations vboxsf_dir_fops = {
183 .open = vboxsf_dir_open,
184 .iterate = vboxsf_dir_iterate,
185 .release = vboxsf_dir_release,
186 .read = generic_read_dir,
187 .llseek = generic_file_llseek,
188};
189
190/*
191 * This is called during name resolution/lookup to check if the @dentry in
192 * the cache is still valid. the job is handled by vboxsf_inode_revalidate.
193 */
194static int vboxsf_dentry_revalidate(struct dentry *dentry, unsigned int flags)
195{
196 if (flags & LOOKUP_RCU)
197 return -ECHILD;
198
199 if (d_really_is_positive(dentry))
200 return vboxsf_inode_revalidate(dentry) == 0;
201 else
202 return vboxsf_stat_dentry(dentry, NULL) == -ENOENT;
203}
204
205const struct dentry_operations vboxsf_dentry_ops = {
206 .d_revalidate = vboxsf_dentry_revalidate
207};
208
209/* iops */
210
211static struct dentry *vboxsf_dir_lookup(struct inode *parent,
212 struct dentry *dentry,
213 unsigned int flags)
214{
215 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
216 struct shfl_fsobjinfo fsinfo;
217 struct inode *inode;
218 int err;
219
220 dentry->d_time = jiffies;
221
222 err = vboxsf_stat_dentry(dentry, &fsinfo);
223 if (err) {
224 inode = (err == -ENOENT) ? NULL : ERR_PTR(err);
225 } else {
226 inode = vboxsf_new_inode(parent->i_sb);
227 if (!IS_ERR(inode))
228 vboxsf_init_inode(sbi, inode, &fsinfo);
229 }
230
231 return d_splice_alias(inode, dentry);
232}
233
234static int vboxsf_dir_instantiate(struct inode *parent, struct dentry *dentry,
235 struct shfl_fsobjinfo *info)
236{
237 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
238 struct vboxsf_inode *sf_i;
239 struct inode *inode;
240
241 inode = vboxsf_new_inode(parent->i_sb);
242 if (IS_ERR(inode))
243 return PTR_ERR(inode);
244
245 sf_i = VBOXSF_I(inode);
246 /* The host may have given us different attr then requested */
247 sf_i->force_restat = 1;
248 vboxsf_init_inode(sbi, inode, info);
249
250 d_instantiate(dentry, inode);
251
252 return 0;
253}
254
255static int vboxsf_dir_create(struct inode *parent, struct dentry *dentry,
Hans de Goedebd909fd2021-01-21 10:22:27 +0100256 umode_t mode, bool is_dir, bool excl, u64 *handle_ret)
Hans de Goede0fd16952019-12-12 15:09:14 +0100257{
258 struct vboxsf_inode *sf_parent_i = VBOXSF_I(parent);
259 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
260 struct shfl_createparms params = {};
261 int err;
262
263 params.handle = SHFL_HANDLE_NIL;
Hans de Goede971703f2021-01-21 10:08:59 +0100264 params.create_flags = SHFL_CF_ACT_CREATE_IF_NEW | SHFL_CF_ACCESS_READWRITE;
265 if (is_dir)
266 params.create_flags |= SHFL_CF_DIRECTORY;
267 if (excl)
268 params.create_flags |= SHFL_CF_ACT_FAIL_IF_EXISTS;
269
Hans de Goede0fd16952019-12-12 15:09:14 +0100270 params.info.attr.mode = (mode & 0777) |
271 (is_dir ? SHFL_TYPE_DIRECTORY : SHFL_TYPE_FILE);
272 params.info.attr.additional = SHFLFSOBJATTRADD_NOTHING;
273
274 err = vboxsf_create_at_dentry(dentry, &params);
275 if (err)
276 return err;
277
278 if (params.result != SHFL_FILE_CREATED)
279 return -EPERM;
280
Hans de Goede0fd16952019-12-12 15:09:14 +0100281 err = vboxsf_dir_instantiate(parent, dentry, &params.info);
282 if (err)
Hans de Goedebd909fd2021-01-21 10:22:27 +0100283 goto out;
Hans de Goede0fd16952019-12-12 15:09:14 +0100284
285 /* parent directory access/change time changed */
286 sf_parent_i->force_restat = 1;
287
Hans de Goedebd909fd2021-01-21 10:22:27 +0100288out:
289 if (err == 0 && handle_ret)
290 *handle_ret = params.handle;
291 else
292 vboxsf_close(sbi->root, params.handle);
293
294 return err;
Hans de Goede0fd16952019-12-12 15:09:14 +0100295}
296
297static int vboxsf_dir_mkfile(struct inode *parent, struct dentry *dentry,
298 umode_t mode, bool excl)
299{
Hans de Goedebd909fd2021-01-21 10:22:27 +0100300 return vboxsf_dir_create(parent, dentry, mode, false, excl, NULL);
Hans de Goede0fd16952019-12-12 15:09:14 +0100301}
302
303static int vboxsf_dir_mkdir(struct inode *parent, struct dentry *dentry,
304 umode_t mode)
305{
Hans de Goedebd909fd2021-01-21 10:22:27 +0100306 return vboxsf_dir_create(parent, dentry, mode, true, true, NULL);
Hans de Goede0fd16952019-12-12 15:09:14 +0100307}
308
Hans de Goededcdb5872021-01-21 12:54:18 +0100309static int vboxsf_dir_atomic_open(struct inode *parent, struct dentry *dentry,
310 struct file *file, unsigned int flags, umode_t mode)
311{
312 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
313 struct vboxsf_handle *sf_handle;
314 struct dentry *res = NULL;
315 u64 handle;
316 int err;
317
318 if (d_in_lookup(dentry)) {
319 res = vboxsf_dir_lookup(parent, dentry, 0);
320 if (IS_ERR(res))
321 return PTR_ERR(res);
322
323 if (res)
324 dentry = res;
325 }
326
327 /* Only creates */
328 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
329 return finish_no_open(file, res);
330
331 err = vboxsf_dir_create(parent, dentry, mode, false, flags & O_EXCL, &handle);
332 if (err)
333 goto out;
334
335 sf_handle = vboxsf_create_sf_handle(d_inode(dentry), handle, SHFL_CF_ACCESS_READWRITE);
336 if (IS_ERR(sf_handle)) {
337 vboxsf_close(sbi->root, handle);
338 err = PTR_ERR(sf_handle);
339 goto out;
340 }
341
342 err = finish_open(file, dentry, generic_file_open);
343 if (err) {
344 /* This also closes the handle passed to vboxsf_create_sf_handle() */
345 vboxsf_release_sf_handle(d_inode(dentry), sf_handle);
346 goto out;
347 }
348
349 file->private_data = sf_handle;
350 file->f_mode |= FMODE_CREATED;
351out:
352 dput(res);
353 return err;
354}
355
Hans de Goede0fd16952019-12-12 15:09:14 +0100356static int vboxsf_dir_unlink(struct inode *parent, struct dentry *dentry)
357{
358 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
359 struct vboxsf_inode *sf_parent_i = VBOXSF_I(parent);
360 struct inode *inode = d_inode(dentry);
361 struct shfl_string *path;
362 u32 flags;
363 int err;
364
365 if (S_ISDIR(inode->i_mode))
366 flags = SHFL_REMOVE_DIR;
367 else
368 flags = SHFL_REMOVE_FILE;
369
370 if (S_ISLNK(inode->i_mode))
371 flags |= SHFL_REMOVE_SYMLINK;
372
373 path = vboxsf_path_from_dentry(sbi, dentry);
374 if (IS_ERR(path))
375 return PTR_ERR(path);
376
377 err = vboxsf_remove(sbi->root, path, flags);
378 __putname(path);
379 if (err)
380 return err;
381
382 /* parent directory access/change time changed */
383 sf_parent_i->force_restat = 1;
384
385 return 0;
386}
387
388static int vboxsf_dir_rename(struct inode *old_parent,
389 struct dentry *old_dentry,
390 struct inode *new_parent,
391 struct dentry *new_dentry,
392 unsigned int flags)
393{
394 struct vboxsf_sbi *sbi = VBOXSF_SBI(old_parent->i_sb);
395 struct vboxsf_inode *sf_old_parent_i = VBOXSF_I(old_parent);
396 struct vboxsf_inode *sf_new_parent_i = VBOXSF_I(new_parent);
397 u32 shfl_flags = SHFL_RENAME_FILE | SHFL_RENAME_REPLACE_IF_EXISTS;
398 struct shfl_string *old_path, *new_path;
399 int err;
400
401 if (flags)
402 return -EINVAL;
403
404 old_path = vboxsf_path_from_dentry(sbi, old_dentry);
405 if (IS_ERR(old_path))
406 return PTR_ERR(old_path);
407
408 new_path = vboxsf_path_from_dentry(sbi, new_dentry);
409 if (IS_ERR(new_path)) {
410 err = PTR_ERR(new_path);
411 goto err_put_old_path;
412 }
413
414 if (d_inode(old_dentry)->i_mode & S_IFDIR)
415 shfl_flags = 0;
416
417 err = vboxsf_rename(sbi->root, old_path, new_path, shfl_flags);
418 if (err == 0) {
419 /* parent directories access/change time changed */
420 sf_new_parent_i->force_restat = 1;
421 sf_old_parent_i->force_restat = 1;
422 }
423
424 __putname(new_path);
425err_put_old_path:
426 __putname(old_path);
427 return err;
428}
429
430static int vboxsf_dir_symlink(struct inode *parent, struct dentry *dentry,
431 const char *symname)
432{
433 struct vboxsf_inode *sf_parent_i = VBOXSF_I(parent);
434 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
435 int symname_size = strlen(symname) + 1;
436 struct shfl_string *path, *ssymname;
437 struct shfl_fsobjinfo info;
438 int err;
439
440 path = vboxsf_path_from_dentry(sbi, dentry);
441 if (IS_ERR(path))
442 return PTR_ERR(path);
443
444 ssymname = kmalloc(SHFLSTRING_HEADER_SIZE + symname_size, GFP_KERNEL);
445 if (!ssymname) {
446 __putname(path);
447 return -ENOMEM;
448 }
449 ssymname->length = symname_size - 1;
450 ssymname->size = symname_size;
451 memcpy(ssymname->string.utf8, symname, symname_size);
452
453 err = vboxsf_symlink(sbi->root, path, ssymname, &info);
454 kfree(ssymname);
455 __putname(path);
456 if (err) {
457 /* -EROFS means symlinks are note support -> -EPERM */
458 return (err == -EROFS) ? -EPERM : err;
459 }
460
461 err = vboxsf_dir_instantiate(parent, dentry, &info);
462 if (err)
463 return err;
464
465 /* parent directory access/change time changed */
466 sf_parent_i->force_restat = 1;
467 return 0;
468}
469
470const struct inode_operations vboxsf_dir_iops = {
471 .lookup = vboxsf_dir_lookup,
472 .create = vboxsf_dir_mkfile,
473 .mkdir = vboxsf_dir_mkdir,
Hans de Goededcdb5872021-01-21 12:54:18 +0100474 .atomic_open = vboxsf_dir_atomic_open,
Hans de Goede0fd16952019-12-12 15:09:14 +0100475 .rmdir = vboxsf_dir_unlink,
476 .unlink = vboxsf_dir_unlink,
477 .rename = vboxsf_dir_rename,
478 .symlink = vboxsf_dir_symlink,
479 .getattr = vboxsf_getattr,
480 .setattr = vboxsf_setattr,
481};