1 // SPDX-License-Identifier: MIT
3 * VirtualBox Guest Shared Folders support: Directory inode and file operations
5 * Copyright (C) 2006-2018 Oracle Corporation
8 #include <linux/namei.h>
9 #include <linux/vbox_utils.h>
12 static int vboxsf_dir_open(struct inode *inode, struct file *file)
14 struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
15 struct shfl_createparms params = {};
16 struct vboxsf_dir_info *sf_d;
19 sf_d = vboxsf_dir_info_alloc();
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;
27 err = vboxsf_create_at_dentry(file_dentry(file), ¶ms);
29 goto err_free_dir_info;
31 if (params.result != SHFL_FILE_EXISTS) {
36 err = vboxsf_dir_read_all(sbi, sf_d, params.handle);
40 vboxsf_close(sbi->root, params.handle);
41 file->private_data = sf_d;
45 vboxsf_close(sbi->root, params.handle);
47 vboxsf_dir_info_free(sf_d);
51 static int vboxsf_dir_release(struct inode *inode, struct file *file)
53 if (file->private_data)
54 vboxsf_dir_info_free(file->private_data);
59 static unsigned int vboxsf_get_d_type(u32 mode)
63 switch (mode & SHFL_TYPE_MASK) {
67 case SHFL_TYPE_DEV_CHAR:
70 case SHFL_TYPE_DIRECTORY:
73 case SHFL_TYPE_DEV_BLOCK:
79 case SHFL_TYPE_SYMLINK:
82 case SHFL_TYPE_SOCKET:
85 case SHFL_TYPE_WHITEOUT:
95 static bool vboxsf_dir_emit(struct file *dir, struct dir_context *ctx)
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;
107 list_for_each_entry(b, &sf_d->info_list, head) {
109 if (ctx->pos >= cur + b->entries) {
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
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)))
129 end = &info->name.string.utf8[info->name.size];
130 if (WARN_ON(end > (b->buf + b->used)))
133 /* Info now points to the right entry, emit it. */
134 d_type = vboxsf_get_d_type(info->info.attr.mode);
137 * On 32-bit systems pos is 64-bit signed, while ino is 32-bit
138 * unsigned so fake_ino may overflow, check for this.
140 if ((ino_t)(ctx->pos + 1) != (u64)(ctx->pos + 1)) {
141 vbg_err("vboxsf: fake ino overflow, truncating dir\n");
144 fake_ino = ctx->pos + 1;
147 char d_name[NAME_MAX];
149 err = vboxsf_nlscpy(sbi, d_name, NAME_MAX,
150 info->name.string.utf8,
153 /* skip erroneous entry and proceed */
158 return dir_emit(ctx, d_name, strlen(d_name),
162 return dir_emit(ctx, info->name.string.utf8, info->name.length,
169 static int vboxsf_dir_iterate(struct file *dir, struct dir_context *ctx)
174 emitted = vboxsf_dir_emit(dir, ctx);
182 const 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,
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.
194 static int vboxsf_dentry_revalidate(struct dentry *dentry, unsigned int flags)
196 if (flags & LOOKUP_RCU)
199 if (d_really_is_positive(dentry))
200 return vboxsf_inode_revalidate(dentry) == 0;
202 return vboxsf_stat_dentry(dentry, NULL) == -ENOENT;
205 const struct dentry_operations vboxsf_dentry_ops = {
206 .d_revalidate = vboxsf_dentry_revalidate
211 static struct dentry *vboxsf_dir_lookup(struct inode *parent,
212 struct dentry *dentry,
215 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
216 struct shfl_fsobjinfo fsinfo;
220 dentry->d_time = jiffies;
222 err = vboxsf_stat_dentry(dentry, &fsinfo);
224 inode = (err == -ENOENT) ? NULL : ERR_PTR(err);
226 inode = vboxsf_new_inode(parent->i_sb);
228 vboxsf_init_inode(sbi, inode, &fsinfo, false);
231 return d_splice_alias(inode, dentry);
234 static int vboxsf_dir_instantiate(struct inode *parent, struct dentry *dentry,
235 struct shfl_fsobjinfo *info)
237 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
238 struct vboxsf_inode *sf_i;
241 inode = vboxsf_new_inode(parent->i_sb);
243 return PTR_ERR(inode);
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, false);
250 d_instantiate(dentry, inode);
255 static int vboxsf_dir_create(struct inode *parent, struct dentry *dentry,
256 umode_t mode, bool is_dir, bool excl, u64 *handle_ret)
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 = {};
263 params.handle = SHFL_HANDLE_NIL;
264 params.create_flags = SHFL_CF_ACT_CREATE_IF_NEW | SHFL_CF_ACCESS_READWRITE;
266 params.create_flags |= SHFL_CF_DIRECTORY;
268 params.create_flags |= SHFL_CF_ACT_FAIL_IF_EXISTS;
270 params.info.attr.mode = (mode & 0777) |
271 (is_dir ? SHFL_TYPE_DIRECTORY : SHFL_TYPE_FILE);
272 params.info.attr.additional = SHFLFSOBJATTRADD_NOTHING;
274 err = vboxsf_create_at_dentry(dentry, ¶ms);
278 if (params.result != SHFL_FILE_CREATED)
281 err = vboxsf_dir_instantiate(parent, dentry, ¶ms.info);
285 /* parent directory access/change time changed */
286 sf_parent_i->force_restat = 1;
289 if (err == 0 && handle_ret)
290 *handle_ret = params.handle;
292 vboxsf_close(sbi->root, params.handle);
297 static int vboxsf_dir_mkfile(struct mnt_idmap *idmap,
298 struct inode *parent, struct dentry *dentry,
299 umode_t mode, bool excl)
301 return vboxsf_dir_create(parent, dentry, mode, false, excl, NULL);
304 static int vboxsf_dir_mkdir(struct mnt_idmap *idmap,
305 struct inode *parent, struct dentry *dentry,
308 return vboxsf_dir_create(parent, dentry, mode, true, true, NULL);
311 static int vboxsf_dir_atomic_open(struct inode *parent, struct dentry *dentry,
312 struct file *file, unsigned int flags, umode_t mode)
314 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
315 struct vboxsf_handle *sf_handle;
316 struct dentry *res = NULL;
320 if (d_in_lookup(dentry)) {
321 res = vboxsf_dir_lookup(parent, dentry, 0);
330 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
331 return finish_no_open(file, res);
333 err = vboxsf_dir_create(parent, dentry, mode, false, flags & O_EXCL, &handle);
337 sf_handle = vboxsf_create_sf_handle(d_inode(dentry), handle, SHFL_CF_ACCESS_READWRITE);
338 if (IS_ERR(sf_handle)) {
339 vboxsf_close(sbi->root, handle);
340 err = PTR_ERR(sf_handle);
344 err = finish_open(file, dentry, generic_file_open);
346 /* This also closes the handle passed to vboxsf_create_sf_handle() */
347 vboxsf_release_sf_handle(d_inode(dentry), sf_handle);
351 file->private_data = sf_handle;
352 file->f_mode |= FMODE_CREATED;
358 static int vboxsf_dir_unlink(struct inode *parent, struct dentry *dentry)
360 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
361 struct vboxsf_inode *sf_parent_i = VBOXSF_I(parent);
362 struct inode *inode = d_inode(dentry);
363 struct shfl_string *path;
367 if (S_ISDIR(inode->i_mode))
368 flags = SHFL_REMOVE_DIR;
370 flags = SHFL_REMOVE_FILE;
372 if (S_ISLNK(inode->i_mode))
373 flags |= SHFL_REMOVE_SYMLINK;
375 path = vboxsf_path_from_dentry(sbi, dentry);
377 return PTR_ERR(path);
379 err = vboxsf_remove(sbi->root, path, flags);
384 /* parent directory access/change time changed */
385 sf_parent_i->force_restat = 1;
390 static int vboxsf_dir_rename(struct mnt_idmap *idmap,
391 struct inode *old_parent,
392 struct dentry *old_dentry,
393 struct inode *new_parent,
394 struct dentry *new_dentry,
397 struct vboxsf_sbi *sbi = VBOXSF_SBI(old_parent->i_sb);
398 struct vboxsf_inode *sf_old_parent_i = VBOXSF_I(old_parent);
399 struct vboxsf_inode *sf_new_parent_i = VBOXSF_I(new_parent);
400 u32 shfl_flags = SHFL_RENAME_FILE | SHFL_RENAME_REPLACE_IF_EXISTS;
401 struct shfl_string *old_path, *new_path;
407 old_path = vboxsf_path_from_dentry(sbi, old_dentry);
408 if (IS_ERR(old_path))
409 return PTR_ERR(old_path);
411 new_path = vboxsf_path_from_dentry(sbi, new_dentry);
412 if (IS_ERR(new_path)) {
413 err = PTR_ERR(new_path);
414 goto err_put_old_path;
417 if (d_inode(old_dentry)->i_mode & S_IFDIR)
420 err = vboxsf_rename(sbi->root, old_path, new_path, shfl_flags);
422 /* parent directories access/change time changed */
423 sf_new_parent_i->force_restat = 1;
424 sf_old_parent_i->force_restat = 1;
433 static int vboxsf_dir_symlink(struct mnt_idmap *idmap,
434 struct inode *parent, struct dentry *dentry,
437 struct vboxsf_inode *sf_parent_i = VBOXSF_I(parent);
438 struct vboxsf_sbi *sbi = VBOXSF_SBI(parent->i_sb);
439 int symname_size = strlen(symname) + 1;
440 struct shfl_string *path, *ssymname;
441 struct shfl_fsobjinfo info;
444 path = vboxsf_path_from_dentry(sbi, dentry);
446 return PTR_ERR(path);
448 ssymname = kmalloc(SHFLSTRING_HEADER_SIZE + symname_size, GFP_KERNEL);
453 ssymname->length = symname_size - 1;
454 ssymname->size = symname_size;
455 memcpy(ssymname->string.utf8, symname, symname_size);
457 err = vboxsf_symlink(sbi->root, path, ssymname, &info);
461 /* -EROFS means symlinks are note support -> -EPERM */
462 return (err == -EROFS) ? -EPERM : err;
465 err = vboxsf_dir_instantiate(parent, dentry, &info);
469 /* parent directory access/change time changed */
470 sf_parent_i->force_restat = 1;
474 const struct inode_operations vboxsf_dir_iops = {
475 .lookup = vboxsf_dir_lookup,
476 .create = vboxsf_dir_mkfile,
477 .mkdir = vboxsf_dir_mkdir,
478 .atomic_open = vboxsf_dir_atomic_open,
479 .rmdir = vboxsf_dir_unlink,
480 .unlink = vboxsf_dir_unlink,
481 .rename = vboxsf_dir_rename,
482 .symlink = vboxsf_dir_symlink,
483 .getattr = vboxsf_getattr,
484 .setattr = vboxsf_setattr,