VirtualBox

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

Last change on this file since 98278 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1/* $Id: vfsmod.h 98103 2023-01-17 14:15:46Z 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(5,12,0)
279extern int vbsf_inode_getattr(struct user_namespace *ns, const struct path *path,
280 struct kstat *kstat, u32 request_mask, unsigned int query_flags);
281# elif RTLNX_VER_MIN(4,11,0)
282extern int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int query_flags);
283# else
284extern int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat);
285# endif
286#else /* < 2.5.44 */
287extern int vbsf_inode_revalidate(struct dentry *dentry);
288#endif /* < 2.5.44 */
289#if RTLNX_VER_MIN(5,12,0)
290extern int vbsf_inode_setattr(struct user_namespace *ns, struct dentry *dentry, struct iattr *iattr);
291#else
292extern int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr);
293#endif
294
295
296extern void vbsf_handle_drop_chain(struct vbsf_inode_info *pInodeInfo);
297extern struct vbsf_handle *vbsf_handle_find(struct vbsf_inode_info *pInodeInfo, uint32_t fFlagsSet, uint32_t fFlagsClear);
298extern uint32_t vbsf_handle_release_slow(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo,
299 const char *pszCaller);
300extern void vbsf_handle_append(struct vbsf_inode_info *pInodeInfo, struct vbsf_handle *pHandle);
301
302/**
303 * Releases a handle.
304 *
305 * @returns New reference count.
306 * @param pHandle The handle to release.
307 * @param pSuperInfo The info structure for the shared folder associated
308 * with the handle.
309 * @param pszCaller The caller name (for logging failures).
310 */
311DECLINLINE(uint32_t) vbsf_handle_release(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo, const char *pszCaller)
312{
313 uint32_t cRefs;
314
315 Assert((pHandle->fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC);
316 Assert(pHandle->pInodeInfo);
317 Assert(pHandle->pInodeInfo && pHandle->pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
318
319 cRefs = ASMAtomicDecU32(&pHandle->cRefs);
320 Assert(cRefs < _64M);
321 if (cRefs)
322 return cRefs;
323 return vbsf_handle_release_slow(pHandle, pSuperInfo, pszCaller);
324}
325
326
327/**
328 * VBox specific information for a regular file.
329 */
330struct vbsf_reg_info {
331 /** Handle tracking structure.
332 * @note Must be first! */
333 struct vbsf_handle Handle;
334};
335
336uint32_t vbsf_linux_oflags_to_vbox(unsigned fLnxOpen, uint32_t *pfHandle, const char *pszCaller);
337
338
339/**
340 * VBox specific information for an open directory.
341 */
342struct vbsf_dir_info {
343 /** Handle tracking structure.
344 * @note Must be first! */
345 struct vbsf_handle Handle;
346 /** Semaphore protecting everything below. */
347 struct semaphore Lock;
348 /** A magic number (VBSF_DIR_INFO_MAGIC). */
349 uint32_t u32Magic;
350 /** Size of the buffer for directory entries. */
351 uint32_t cbBuf;
352 /** Buffer for directory entries on the physical heap. */
353 PSHFLDIRINFO pBuf;
354 /** Number of valid bytes in the buffer. */
355 uint32_t cbValid;
356 /** Number of entries left in the buffer. */
357 uint32_t cEntriesLeft;
358 /** The position of the next entry. Incremented by one for each entry. */
359 loff_t offPos;
360 /** The next entry. */
361 PSHFLDIRINFO pEntry;
362 /** Set if there are no more files. */
363 bool fNoMoreFiles;
364};
365
366/** Magic number for vbsf_dir_info::u32Magic (Robert Anson Heinlein). */
367#define VBSF_DIR_INFO_MAGIC UINT32_C(0x19070707)
368/** Value of vbsf_dir_info::u32Magic when freed. */
369#define VBSF_DIR_INFO_MAGIC_DEAD UINT32_C(0x19880508)
370
371
372/**
373 * Sets the update-jiffies value for a dentry.
374 *
375 * This is used together with vbsf_super_info::cJiffiesDirCacheTTL to reduce
376 * re-validation of dentry structures while walking.
377 *
378 * This used to be living in d_time, but since 4.9.0 that seems to have become
379 * unfashionable and d_fsdata is now used to for this purpose. We do this all
380 * the way back, since d_time seems only to have been used by the file system
381 * specific code (at least going back to 2.4.0).
382 */
383DECLINLINE(void) vbsf_dentry_set_update_jiffies(struct dentry *pDirEntry, unsigned long uToSet)
384{
385 /*SFLOG3(("vbsf_dentry_set_update_jiffies: %p: %lx -> %#lx\n", pDirEntry, (unsigned long)pDirEntry->d_fsdata, uToSet));*/
386 pDirEntry->d_fsdata = (void *)uToSet;
387}
388
389/**
390 * Get the update-jiffies value for a dentry.
391 */
392DECLINLINE(unsigned long) vbsf_dentry_get_update_jiffies(struct dentry *pDirEntry)
393{
394 return (unsigned long)pDirEntry->d_fsdata;
395}
396
397/**
398 * Invalidates the update TTL for the given directory entry so that it is
399 * revalidate the next time it is used.
400 * @param pDirEntry The directory entry cache entry to invalidate.
401 */
402DECLINLINE(void) vbsf_dentry_invalidate_ttl(struct dentry *pDirEntry)
403{
404 vbsf_dentry_set_update_jiffies(pDirEntry, jiffies - INT32_MAX / 2);
405}
406
407/**
408 * Increase the time-to-live of @a pDirEntry and all ancestors.
409 * @param pDirEntry The directory entry cache entry which ancestors
410 * we should increase the TTL for.
411 */
412DECLINLINE(void) vbsf_dentry_chain_increase_ttl(struct dentry *pDirEntry)
413{
414#ifdef VBOX_STRICT
415 struct super_block * const pSuper = pDirEntry->d_sb;
416#endif
417 unsigned long const uToSet = jiffies;
418 do {
419 Assert(pDirEntry->d_sb == pSuper);
420 vbsf_dentry_set_update_jiffies(pDirEntry, uToSet);
421 pDirEntry = pDirEntry->d_parent;
422 } while (!IS_ROOT(pDirEntry));
423}
424
425/**
426 * Increase the time-to-live of all ancestors.
427 * @param pDirEntry The directory entry cache entry which ancestors
428 * we should increase the TTL for.
429 */
430DECLINLINE(void) vbsf_dentry_chain_increase_parent_ttl(struct dentry *pDirEntry)
431{
432 Assert(!pDirEntry->d_parent || pDirEntry->d_parent->d_sb == pDirEntry->d_sb);
433 pDirEntry = pDirEntry->d_parent;
434 if (pDirEntry)
435 vbsf_dentry_chain_increase_ttl(pDirEntry);
436}
437
438/** Macro for getting the dentry for a struct file. */
439#if RTLNX_VER_MIN(4,6,0)
440# define VBSF_GET_F_DENTRY(f) file_dentry(f)
441#elif RTLNX_VER_MIN(2,6,20)
442# define VBSF_GET_F_DENTRY(f) (f->f_path.dentry)
443#else
444# define VBSF_GET_F_DENTRY(f) (f->f_dentry)
445#endif
446
447/**
448 * Macro for checking if the 'data' argument passed in via mount(2) was supplied
449 * by the mount.vboxsf command line utility as a page of data containing the
450 * vbsf_mount_info_new structure.
451 */
452#define VBSF_IS_MOUNT_VBOXSF_DATA(data) \
453 (((struct vbsf_mount_info_new *)data)->nullchar == '\0' && \
454 ((struct vbsf_mount_info_new *)data)->signature[0] == VBSF_MOUNT_SIGNATURE_BYTE_0 && \
455 ((struct vbsf_mount_info_new *)data)->signature[1] == VBSF_MOUNT_SIGNATURE_BYTE_1 && \
456 ((struct vbsf_mount_info_new *)data)->signature[2] == VBSF_MOUNT_SIGNATURE_BYTE_2)
457
458extern int vbsf_stat(const char *caller, struct vbsf_super_info *pSuperInfo, SHFLSTRING * path, PSHFLFSOBJINFO result,
459 int ok_to_fail);
460extern int vbsf_path_from_dentry(struct vbsf_super_info *pSuperInfo, struct vbsf_inode_info *sf_i, struct dentry *dentry,
461 SHFLSTRING ** result, const char *caller);
462extern int vbsf_nlscpy(struct vbsf_super_info *pSuperInfo, char *name, size_t name_bound_len,
463 const unsigned char *utf8_name, size_t utf8_len);
464extern int vbsf_nls_to_shflstring(struct vbsf_super_info *pSuperInfo, const char *pszNls, PSHFLSTRING *ppString);
465
466
467/**
468 * Converts Linux access permissions to VBox ones (mode & 0777).
469 *
470 * @note Currently identical.
471 * @sa sf_access_permissions_to_linux
472 */
473DECLINLINE(uint32_t) sf_access_permissions_to_vbox(int fAttr)
474{
475 /* Access bits should be the same: */
476 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
477 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
478 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
479 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
480 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
481 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
482 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
483 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
484 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
485
486 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
487}
488
489#endif /* !GA_INCLUDED_SRC_linux_sharedfolders_vfsmod_h */
Note: See TracBrowser for help on using the repository browser.

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