Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: MIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * VirtualBox Guest Shared Folders support: Regular file inode and file ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006-2018 Oracle Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "vfsmod.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) struct vboxsf_handle {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	u64 handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	u32 root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u32 access_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct kref refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct vboxsf_handle *vboxsf_create_sf_handle(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 					      u64 handle, u32 access_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct vboxsf_inode *sf_i = VBOXSF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct vboxsf_handle *sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	sf_handle = kmalloc(sizeof(*sf_handle), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (!sf_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	/* the host may have given us different attr then requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	sf_i->force_restat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/* init our handle struct and add it to the inode's handles list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	sf_handle->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	sf_handle->root = VBOXSF_SBI(inode->i_sb)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	sf_handle->access_flags = access_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	kref_init(&sf_handle->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	mutex_lock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	list_add(&sf_handle->head, &sf_i->handle_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	mutex_unlock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int vboxsf_file_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct shfl_createparms params = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct vboxsf_handle *sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u32 access_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * We check the value of params.handle afterwards to find out if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * the call succeeded or failed, as the API does not seem to cleanly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * distinguish error and informational messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * Furthermore, we must set params.handle to SHFL_HANDLE_NIL to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 * make the shared folders host service use our mode parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	params.handle = SHFL_HANDLE_NIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (file->f_flags & O_CREAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		params.create_flags |= SHFL_CF_ACT_CREATE_IF_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		 * We ignore O_EXCL, as the Linux kernel seems to call create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		 * beforehand itself, so O_EXCL should always fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if (file->f_flags & O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			params.create_flags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			params.create_flags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		params.create_flags |= SHFL_CF_ACT_FAIL_IF_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (file->f_flags & O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			params.create_flags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	switch (file->f_flags & O_ACCMODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	case O_RDONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		access_flags |= SHFL_CF_ACCESS_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	case O_WRONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		access_flags |= SHFL_CF_ACCESS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	case O_RDWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		access_flags |= SHFL_CF_ACCESS_READWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (file->f_flags & O_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		access_flags |= SHFL_CF_ACCESS_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	params.create_flags |= access_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	params.info.attr.mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	err = vboxsf_create_at_dentry(file_dentry(file), &params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (err == 0 && params.handle == SHFL_HANDLE_NIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		err = (params.result == SHFL_FILE_EXISTS) ? -EEXIST : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	sf_handle = vboxsf_create_sf_handle(inode, params.handle, access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (IS_ERR(sf_handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		vboxsf_close(sbi->root, params.handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return PTR_ERR(sf_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	file->private_data = sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void vboxsf_handle_release(struct kref *refcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct vboxsf_handle *sf_handle =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		container_of(refcount, struct vboxsf_handle, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	vboxsf_close(sf_handle->root, sf_handle->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	kfree(sf_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void vboxsf_release_sf_handle(struct inode *inode, struct vboxsf_handle *sf_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct vboxsf_inode *sf_i = VBOXSF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	mutex_lock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	list_del(&sf_handle->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	mutex_unlock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	kref_put(&sf_handle->refcount, vboxsf_handle_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int vboxsf_file_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	 * When a file is closed on our (the guest) side, we want any subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * accesses done on the host side to see all changes done from our side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	filemap_write_and_wait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	vboxsf_release_sf_handle(inode, file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * Write back dirty pages now, because there may not be any suitable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * open files later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void vboxsf_vma_close(struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	filemap_write_and_wait(vma->vm_file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const struct vm_operations_struct vboxsf_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	.close		= vboxsf_vma_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	.fault		= filemap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	.map_pages	= filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int vboxsf_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	err = generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		vma->vm_ops = &vboxsf_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * Note that since we are accessing files on the host's filesystem, files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * may always be changed underneath us by the host!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * The vboxsf API between the guest and the host does not offer any functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * to deal with this. There is no inode-generation to check for changes, no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * events / callback on changes and no way to lock files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * To avoid returning stale data when a file gets *opened* on our (the guest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * side, we do a "stat" on the host side, then compare the mtime with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * last known mtime and invalidate the page-cache if they differ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * This is done from vboxsf_inode_revalidate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * When reads are done through the read_iter fop, it is possible to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * further cache revalidation then, there are 3 options to deal with this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * 1)  Rely solely on the revalidation done at open time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * 2)  Do another "stat" and compare mtime again. Unfortunately the vboxsf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *     host API does not allow stat on handles, so we would need to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  *     file->f_path.dentry and the stat will then fail if the file was unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  *     or renamed (and there is no thing like NFS' silly-rename). So we get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * 2a) "stat" and compare mtime, on stat failure invalidate the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * 2b) "stat" and compare mtime, on stat failure do nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * 3)  Simply always call invalidate_inode_pages2_range on the range of the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * Currently we are keeping things KISS and using option 1. this allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * directly using generic_file_read_iter without wrapping it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * This means that only data written on the host side before open() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * the guest side is guaranteed to be seen by the guest. If necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * we may provide other read-cache strategies in the future and make this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * configurable through a mount option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) const struct file_operations vboxsf_reg_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	.llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	.read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	.write_iter = generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	.mmap = vboxsf_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	.open = vboxsf_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	.release = vboxsf_file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.fsync = noop_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) const struct inode_operations vboxsf_reg_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.getattr = vboxsf_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.setattr = vboxsf_setattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int vboxsf_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct vboxsf_handle *sf_handle = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	loff_t off = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	u32 nread = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	buf = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	err = vboxsf_read(sf_handle->root, sf_handle->handle, off, &nread, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		memset(&buf[nread], 0, PAGE_SIZE - nread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static struct vboxsf_handle *vboxsf_get_write_handle(struct vboxsf_inode *sf_i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct vboxsf_handle *h, *sf_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	mutex_lock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	list_for_each_entry(h, &sf_i->handle_list, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (h->access_flags == SHFL_CF_ACCESS_WRITE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		    h->access_flags == SHFL_CF_ACCESS_READWRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			kref_get(&h->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			sf_handle = h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	mutex_unlock(&sf_i->handle_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	return sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int vboxsf_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct vboxsf_inode *sf_i = VBOXSF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct vboxsf_handle *sf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	loff_t off = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	loff_t size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	u32 nwrite = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (off + PAGE_SIZE > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		nwrite = size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	sf_handle = vboxsf_get_write_handle(sf_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!sf_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	buf = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	err = vboxsf_write(sf_handle->root, sf_handle->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			   off, &nwrite, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	kref_put(&sf_handle->refcount, vboxsf_handle_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		/* mtime changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		sf_i->force_restat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int vboxsf_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			    loff_t pos, unsigned int len, unsigned int copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			    struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct vboxsf_handle *sf_handle = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	unsigned int from = pos & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	u32 nwritten = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	/* zero the stale part of the page if we did a short copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (!PageUptodate(page) && copied < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		zero_user(page, from + copied, len - copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	buf = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	err = vboxsf_write(sf_handle->root, sf_handle->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			   pos, &nwritten, buf + from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		nwritten = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	/* mtime changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	VBOXSF_I(inode)->force_restat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (!PageUptodate(page) && nwritten == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	pos += nwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (pos > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		i_size_write(inode, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return nwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * Note simple_write_begin does not read the page from disk on partial writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * this is ok since vboxsf_write_end only writes the written parts of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * page and it does not call SetPageUptodate for partial writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) const struct address_space_operations vboxsf_reg_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	.readpage = vboxsf_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.writepage = vboxsf_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	.set_page_dirty = __set_page_dirty_nobuffers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.write_begin = simple_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	.write_end = vboxsf_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static const char *vboxsf_get_link(struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				   struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct shfl_string *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	char *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	path = vboxsf_path_from_dentry(sbi, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (IS_ERR(path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return ERR_CAST(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	link = kzalloc(PATH_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (!link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		__putname(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	err = vboxsf_readlink(sbi->root, path, PATH_MAX, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	__putname(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		kfree(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	set_delayed_call(done, kfree_link, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	return link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) const struct inode_operations vboxsf_lnk_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	.get_link = vboxsf_get_link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) };