VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/vfsmod.h@ 98869

Last change on this file since 98869 was 98869, checked in by vboxsync, 23 months ago

Additions: Linux: Shared Folders: Introduce initial support for kernel 6.3.x series, bugref:10381.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: vfsmod.h 98869 2023-03-07 17:24:50Z vboxsync $ */
2/** @file
3 * vboxsf - Linux Shared Folders VFS, internal header.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#ifndef GA_INCLUDED_SRC_linux_sharedfolders_vfsmod_h
32#define GA_INCLUDED_SRC_linux_sharedfolders_vfsmod_h
33#ifndef RT_WITHOUT_PRAGMA_ONCE
34# pragma once
35#endif
36
37#if 0 /* Enables strict checks. */
38# define RT_STRICT
39# define VBOX_STRICT
40#endif
41
42#define LOG_GROUP LOG_GROUP_SHARED_FOLDERS
43#include "the-linux-kernel.h"
44#include <iprt/list.h>
45#include <iprt/asm.h>
46#include <VBox/log.h>
47
48#if RTLNX_VER_MIN(2,6,0)
49# include <linux/backing-dev.h>
50#endif
51
52#include <VBox/VBoxGuestLibSharedFolders.h>
53#include <VBox/VBoxGuestLibSharedFoldersInline.h>
54#include <iprt/asm.h>
55#include "vbsfmount.h"
56
57
58/*
59 * Logging wrappers.
60 */
61#if 1
62# define TRACE() LogFunc(("tracepoint\n"))
63# define SFLOG(aArgs) Log(aArgs)
64# define SFLOGFLOW(aArgs) LogFlow(aArgs)
65# define SFLOG2(aArgs) Log2(aArgs)
66# define SFLOG3(aArgs) Log3(aArgs)
67# define SFLOGRELBOTH(aArgs) LogRel(aArgs)
68# ifdef LOG_ENABLED
69# define SFLOG_ENABLED 1
70# endif
71#else
72# define TRACE() RTLogBackdoorPrintf("%s: tracepoint\n", __FUNCTION__)
73# define SFLOG(aArgs) RTLogBackdoorPrintf aArgs
74# define SFLOGFLOW(aArgs) RTLogBackdoorPrintf aArgs
75# define SFLOG2(aArgs) RTLogBackdoorPrintf aArgs
76# define SFLOG3(aArgs) RTLogBackdoorPrintf aArgs
77# define SFLOG_ENABLED 1
78# define SFLOGRELBOTH(aArgs) do { RTLogBackdoorPrintf aArgs; printk aArgs; } while (0)
79#endif
80
81
82/* Simmilar workaround for CONFIG_FORTIFY_SOURCE kernel config option as we have for host drivers.
83 * In Linux 5.18-rc1, memcpy became a wrapper which does fortify checks
84 * before triggering __underlying_memcpy() call. We do not pass these checks in some places so
85 * bypass them for now. */
86#if RTLNX_VER_MIN(5,18,0) && !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
87# define VBOX_LINUX_MEMCPY __underlying_memcpy
88#else
89#define VBOX_LINUX_MEMCPY memcpy
90#endif
91
92
93/*
94 * inode compatibility glue.
95 */
96#if RTLNX_VER_MAX(2,6,0)
97
98DECLINLINE(loff_t) i_size_read(struct inode *pInode)
99{
100 AssertCompile(sizeof(loff_t) == sizeof(uint64_t));
101 return ASMAtomicReadU64((uint64_t volatile *)&pInode->i_size);
102}
103
104DECLINLINE(void) i_size_write(struct inode *pInode, loff_t cbNew)
105{
106 AssertCompile(sizeof(pInode->i_size) == sizeof(uint64_t));
107 ASMAtomicWriteU64((uint64_t volatile *)&pInode->i_size, cbNew);
108}
109
110#endif /* < 2.6.0 */
111
112#if RTLNX_VER_MAX(3,2,0) && !RTLNX_RHEL_MIN(6, 10)
113DECLINLINE(void) set_nlink(struct inode *pInode, unsigned int cLinks)
114{
115 pInode->i_nlink = cLinks;
116}
117#endif
118
119
120/* global variables */
121extern VBGLSFCLIENT g_SfClient;
122extern spinlock_t g_SfHandleLock;
123extern uint32_t g_uSfLastFunction;
124extern uint64_t g_fSfFeatures;
125
126extern struct inode_operations vbsf_dir_iops;
127extern struct inode_operations vbsf_lnk_iops;
128extern struct inode_operations vbsf_reg_iops;
129extern struct file_operations vbsf_dir_fops;
130extern struct file_operations vbsf_reg_fops;
131extern struct dentry_operations vbsf_dentry_ops;
132extern struct address_space_operations vbsf_reg_aops;
133
134
135/**
136 * VBox specific per-mount (shared folder) information.
137 */
138struct vbsf_super_info {
139 VBGLSFMAP map;
140 struct nls_table *nls;
141 /** Set if the NLS table is UTF-8. */
142 bool fNlsIsUtf8;
143 int uid;
144 int gid;
145 int dmode;
146 int fmode;
147 int dmask;
148 int fmask;
149 /** Maximum number of pages to allow in an I/O buffer with the host.
150 * This applies to read and write operations. */
151 uint32_t cMaxIoPages;
152 /** The default directory buffer size. */
153 uint32_t cbDirBuf;
154 /** The time to live for directory entries in jiffies, zero if disabled. */
155 uint32_t cJiffiesDirCacheTTL;
156 /** The time to live for inode information in jiffies, zero if disabled. */
157 uint32_t cJiffiesInodeTTL;
158 /** The cache and coherency mode. */
159 enum vbsf_cache_mode enmCacheMode;
160 /** Mount tag for VBoxService automounter. @since 6.0 */
161 char szTag[32];
162#if RTLNX_VER_RANGE(2,6,0, 4,12,0)
163 /** The backing device info structure. */
164 struct backing_dev_info bdi;
165#endif
166 /** The mount option value for /proc/mounts. */
167 int32_t msTTL;
168 /** The time to live for directory entries in milliseconds, for /proc/mounts. */
169 int32_t msDirCacheTTL;
170 /** The time to live for inode information in milliseconds, for /proc/mounts. */
171 int32_t msInodeTTL;
172#if RTLNX_VER_RANGE(4,0,0, 4,2,0)
173 /** 4.0 and 4.1 are missing noop_backing_dev_info export, so take down the
174 * initial value so we can restore it in vbsf_done_backing_dev(). (paranoia) */
175 struct backing_dev_info *bdi_org;
176#endif
177};
178
179/* Following casts are here to prevent assignment of void * to
180 pointers of arbitrary type */
181#if RTLNX_VER_MAX(2,6,0)
182# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->u.generic_sbp)
183# define VBSF_SET_SUPER_INFO(sb, a_pSuperInfo) do { (sb)->u.generic_sbp = a_pSuperInfo; } while (0)
184#else
185# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->s_fs_info)
186# define VBSF_SET_SUPER_INFO(sb, a_pSuperInfo) do { (sb)->s_fs_info = a_pSuperInfo;} while (0)
187#endif
188
189
190/**
191 * For associating inodes with host handles.
192 *
193 * This is necessary for address_space_operations::vbsf_writepage and allows
194 * optimizing stat, lookups and other operations on open files and directories.
195 */
196struct vbsf_handle {
197 /** List entry (head vbsf_inode_info::HandleList). */
198 RTLISTNODE Entry;
199 /** Host file/whatever handle. */
200 SHFLHANDLE hHost;
201 /** VBSF_HANDLE_F_XXX */
202 uint32_t fFlags;
203 /** Reference counter.
204 * Close the handle and free the structure when it reaches zero. */
205 uint32_t volatile cRefs;
206#ifdef VBOX_STRICT
207 /** For strictness checks. */
208 struct vbsf_inode_info *pInodeInfo;
209#endif
210};
211
212/** @name VBSF_HANDLE_F_XXX - Handle summary flags (vbsf_handle::fFlags).
213 * @{ */
214#define VBSF_HANDLE_F_READ UINT32_C(0x00000001)
215#define VBSF_HANDLE_F_WRITE UINT32_C(0x00000002)
216#define VBSF_HANDLE_F_APPEND UINT32_C(0x00000004)
217#define VBSF_HANDLE_F_FILE UINT32_C(0x00000010)
218#define VBSF_HANDLE_F_DIR UINT32_C(0x00000020)
219#define VBSF_HANDLE_F_ON_LIST UINT32_C(0x00000080)
220#define VBSF_HANDLE_F_MAGIC_MASK UINT32_C(0xffffff00)
221#define VBSF_HANDLE_F_MAGIC UINT32_C(0x75030700) /**< Maurice Ravel (1875-03-07). */
222#define VBSF_HANDLE_F_MAGIC_DEAD UINT32_C(0x19371228)
223/** @} */
224
225
226/**
227 * VBox specific per-inode information.
228 */
229struct vbsf_inode_info {
230 /** Which file */
231 SHFLSTRING *path;
232 /** Some information was changed, update data on next revalidate */
233 bool force_restat;
234 /** The timestamp (jiffies) where the inode info was last updated. */
235 unsigned long ts_up_to_date;
236 /** The birth time. */
237 RTTIMESPEC BirthTime;
238
239 /** @name Host modification detection stats.
240 * @{ */
241 /** The raw modification time, for mapping invalidation purposes. */
242 RTTIMESPEC ModificationTime;
243 /** Copy of ModificationTime from the last time we wrote to the the file. */
244 RTTIMESPEC ModificationTimeAtOurLastWrite;
245 /** @} */
246
247 /** handle valid if a file was created with vbsf_create_worker until it will
248 * be opened with vbsf_reg_open()
249 * @todo r=bird: figure this one out... */
250 SHFLHANDLE handle;
251
252 /** List of open handles (struct vbsf_handle), protected by g_SfHandleLock. */
253 RTLISTANCHOR HandleList;
254#ifdef VBOX_STRICT
255 uint32_t u32Magic;
256# define SF_INODE_INFO_MAGIC UINT32_C(0x18620822) /**< Claude Debussy */
257# define SF_INODE_INFO_MAGIC_DEAD UINT32_C(0x19180325)
258#endif
259};
260
261#if RTLNX_VER_MIN(2,6,19) || defined(KERNEL_FC6)
262/* FC6 kernel 2.6.18, vanilla kernel 2.6.19+ */
263# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->i_private)
264# define VBSF_SET_INODE_INFO(i, sf_i) (i)->i_private = sf_i
265#else
266/* vanilla kernel up to 2.6.18 */
267# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->u.generic_ip)
268# define VBSF_SET_INODE_INFO(i, sf_i) (i)->u.generic_ip = sf_i
269#endif
270
271extern void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO info,
272 struct vbsf_super_info *pSuperInfo);
273extern void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
274 struct vbsf_super_info *pSuperInfo, bool fInodeLocked, unsigned fSetAttrs);
275extern int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced, bool fInodeLocked);
276extern int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked);
277#if RTLNX_VER_MIN(2,5,18)
278# if RTLNX_VER_MIN(6,3,0)
279extern int vbsf_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
280 struct kstat *kstat, u32 request_mask, unsigned int query_flags);
281# elif RTLNX_VER_MIN(5,12,0)
282extern int vbsf_inode_getattr(struct user_namespace *ns, const struct path *path,
283 struct kstat *kstat, u32 request_mask, unsigned int query_flags);
284# elif RTLNX_VER_MIN(4,11,0)
285extern int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int query_flags);
286# else
287extern int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat);
288# endif
289#else /* < 2.5.44 */
290extern int vbsf_inode_revalidate(struct dentry *dentry);
291#endif /* < 2.5.44 */
292#if RTLNX_VER_MIN(6,3,0)
293extern int vbsf_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *iattr);
294#elif RTLNX_VER_MIN(5,12,0)
295extern int vbsf_inode_setattr(struct user_namespace *ns, struct dentry *dentry, struct iattr *iattr);
296#else
297extern int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr);
298#endif
299
300
301extern void vbsf_handle_drop_chain(struct vbsf_inode_info *pInodeInfo);
302extern struct vbsf_handle *vbsf_handle_find(struct vbsf_inode_info *pInodeInfo, uint32_t fFlagsSet, uint32_t fFlagsClear);
303extern uint32_t vbsf_handle_release_slow(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo,
304 const char *pszCaller);
305extern void vbsf_handle_append(struct vbsf_inode_info *pInodeInfo, struct vbsf_handle *pHandle);
306
307/**
308 * Releases a handle.
309 *
310 * @returns New reference count.
311 * @param pHandle The handle to release.
312 * @param pSuperInfo The info structure for the shared folder associated
313 * with the handle.
314 * @param pszCaller The caller name (for logging failures).
315 */
316DECLINLINE(uint32_t) vbsf_handle_release(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo, const char *pszCaller)
317{
318 uint32_t cRefs;
319
320 Assert((pHandle->fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC);
321 Assert(pHandle->pInodeInfo);
322 Assert(pHandle->pInodeInfo && pHandle->pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
323
324 cRefs = ASMAtomicDecU32(&pHandle->cRefs);
325 Assert(cRefs < _64M);
326 if (cRefs)
327 return cRefs;
328 return vbsf_handle_release_slow(pHandle, pSuperInfo, pszCaller);
329}
330
331
332/**
333 * VBox specific information for a regular file.
334 */
335struct vbsf_reg_info {
336 /** Handle tracking structure.
337 * @note Must be first! */
338 struct vbsf_handle Handle;
339};
340
341uint32_t vbsf_linux_oflags_to_vbox(unsigned fLnxOpen, uint32_t *pfHandle, const char *pszCaller);
342
343
344/**
345 * VBox specific information for an open directory.
346 */
347struct vbsf_dir_info {
348 /** Handle tracking structure.
349 * @note Must be first! */
350 struct vbsf_handle Handle;
351 /** Semaphore protecting everything below. */
352 struct semaphore Lock;
353 /** A magic number (VBSF_DIR_INFO_MAGIC). */
354 uint32_t u32Magic;
355 /** Size of the buffer for directory entries. */
356 uint32_t cbBuf;
357 /** Buffer for directory entries on the physical heap. */
358 PSHFLDIRINFO pBuf;
359 /** Number of valid bytes in the buffer. */
360 uint32_t cbValid;
361 /** Number of entries left in the buffer. */
362 uint32_t cEntriesLeft;
363 /** The position of the next entry. Incremented by one for each entry. */
364 loff_t offPos;
365 /** The next entry. */
366 PSHFLDIRINFO pEntry;
367 /** Set if there are no more files. */
368 bool fNoMoreFiles;
369};
370
371/** Magic number for vbsf_dir_info::u32Magic (Robert Anson Heinlein). */
372#define VBSF_DIR_INFO_MAGIC UINT32_C(0x19070707)
373/** Value of vbsf_dir_info::u32Magic when freed. */
374#define VBSF_DIR_INFO_MAGIC_DEAD UINT32_C(0x19880508)
375
376
377/**
378 * Sets the update-jiffies value for a dentry.
379 *
380 * This is used together with vbsf_super_info::cJiffiesDirCacheTTL to reduce
381 * re-validation of dentry structures while walking.
382 *
383 * This used to be living in d_time, but since 4.9.0 that seems to have become
384 * unfashionable and d_fsdata is now used to for this purpose. We do this all
385 * the way back, since d_time seems only to have been used by the file system
386 * specific code (at least going back to 2.4.0).
387 */
388DECLINLINE(void) vbsf_dentry_set_update_jiffies(struct dentry *pDirEntry, unsigned long uToSet)
389{
390 /*SFLOG3(("vbsf_dentry_set_update_jiffies: %p: %lx -> %#lx\n", pDirEntry, (unsigned long)pDirEntry->d_fsdata, uToSet));*/
391 pDirEntry->d_fsdata = (void *)uToSet;
392}
393
394/**
395 * Get the update-jiffies value for a dentry.
396 */
397DECLINLINE(unsigned long) vbsf_dentry_get_update_jiffies(struct dentry *pDirEntry)
398{
399 return (unsigned long)pDirEntry->d_fsdata;
400}
401
402/**
403 * Invalidates the update TTL for the given directory entry so that it is
404 * revalidate the next time it is used.
405 * @param pDirEntry The directory entry cache entry to invalidate.
406 */
407DECLINLINE(void) vbsf_dentry_invalidate_ttl(struct dentry *pDirEntry)
408{
409 vbsf_dentry_set_update_jiffies(pDirEntry, jiffies - INT32_MAX / 2);
410}
411
412/**
413 * Increase the time-to-live of @a pDirEntry and all ancestors.
414 * @param pDirEntry The directory entry cache entry which ancestors
415 * we should increase the TTL for.
416 */
417DECLINLINE(void) vbsf_dentry_chain_increase_ttl(struct dentry *pDirEntry)
418{
419#ifdef VBOX_STRICT
420 struct super_block * const pSuper = pDirEntry->d_sb;
421#endif
422 unsigned long const uToSet = jiffies;
423 do {
424 Assert(pDirEntry->d_sb == pSuper);
425 vbsf_dentry_set_update_jiffies(pDirEntry, uToSet);
426 pDirEntry = pDirEntry->d_parent;
427 } while (!IS_ROOT(pDirEntry));
428}
429
430/**
431 * Increase the time-to-live of all ancestors.
432 * @param pDirEntry The directory entry cache entry which ancestors
433 * we should increase the TTL for.
434 */
435DECLINLINE(void) vbsf_dentry_chain_increase_parent_ttl(struct dentry *pDirEntry)
436{
437 Assert(!pDirEntry->d_parent || pDirEntry->d_parent->d_sb == pDirEntry->d_sb);
438 pDirEntry = pDirEntry->d_parent;
439 if (pDirEntry)
440 vbsf_dentry_chain_increase_ttl(pDirEntry);
441}
442
443/** Macro for getting the dentry for a struct file. */
444#if RTLNX_VER_MIN(4,6,0)
445# define VBSF_GET_F_DENTRY(f) file_dentry(f)
446#elif RTLNX_VER_MIN(2,6,20)
447# define VBSF_GET_F_DENTRY(f) (f->f_path.dentry)
448#else
449# define VBSF_GET_F_DENTRY(f) (f->f_dentry)
450#endif
451
452/**
453 * Macro for checking if the 'data' argument passed in via mount(2) was supplied
454 * by the mount.vboxsf command line utility as a page of data containing the
455 * vbsf_mount_info_new structure.
456 */
457#define VBSF_IS_MOUNT_VBOXSF_DATA(data) \
458 (((struct vbsf_mount_info_new *)data)->nullchar == '\0' && \
459 ((struct vbsf_mount_info_new *)data)->signature[0] == VBSF_MOUNT_SIGNATURE_BYTE_0 && \
460 ((struct vbsf_mount_info_new *)data)->signature[1] == VBSF_MOUNT_SIGNATURE_BYTE_1 && \
461 ((struct vbsf_mount_info_new *)data)->signature[2] == VBSF_MOUNT_SIGNATURE_BYTE_2)
462
463extern int vbsf_stat(const char *caller, struct vbsf_super_info *pSuperInfo, SHFLSTRING * path, PSHFLFSOBJINFO result,
464 int ok_to_fail);
465extern int vbsf_path_from_dentry(struct vbsf_super_info *pSuperInfo, struct vbsf_inode_info *sf_i, struct dentry *dentry,
466 SHFLSTRING ** result, const char *caller);
467extern int vbsf_nlscpy(struct vbsf_super_info *pSuperInfo, char *name, size_t name_bound_len,
468 const unsigned char *utf8_name, size_t utf8_len);
469extern int vbsf_nls_to_shflstring(struct vbsf_super_info *pSuperInfo, const char *pszNls, PSHFLSTRING *ppString);
470
471
472/**
473 * Converts Linux access permissions to VBox ones (mode & 0777).
474 *
475 * @note Currently identical.
476 * @sa sf_access_permissions_to_linux
477 */
478DECLINLINE(uint32_t) sf_access_permissions_to_vbox(int fAttr)
479{
480 /* Access bits should be the same: */
481 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
482 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
483 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
484 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
485 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
486 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
487 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
488 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
489 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
490
491 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
492}
493
494#endif /* !GA_INCLUDED_SRC_linux_sharedfolders_vfsmod_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette