VirtualBox

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

Last change on this file since 99425 was 99425, checked in by vboxsync, 20 months ago

linux/vboxsf: s/VBOX_LINUX_MEMCPY/VBSF_UNFORTIFIED_MEMCPY/g + explanation why and when to use. [scm fix] bugref:10209 ticketref:21410

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
Line 
1/* $Id: vfsmod.h 99425 2023-04-17 18:05:10Z 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/** Similar workaround for CONFIG_FORTIFY_SOURCE kernel config option as we
83 * have in the host drivers.
84 *
85 * In Linux 5.18-rc1, memcpy became a wrapper which does fortify checks before
86 * making a call to __underlying_memcpy(). There are a number of places where
87 * we trigger the "field-spanning write" fortify check, typically when copying
88 * to SHFLSTRING structure members as these are actually of variable length but
89 * we don't (cannot with gcc) use RT_FLEXIBLE_ARRAY_NESTED.
90 *
91 * Use this when copying to structures or members with a variable length member.
92 *
93 * @see @ticketref{21410}, @bugref{10209}
94 */
95#if RTLNX_VER_MIN(5,18,0) && !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
96# define VBSF_UNFORTIFIED_MEMCPY __underlying_memcpy
97#else
98# define VBSF_UNFORTIFIED_MEMCPY memcpy
99#endif
100
101
102/*
103 * inode compatibility glue.
104 */
105#if RTLNX_VER_MAX(2,6,0)
106
107DECLINLINE(loff_t) i_size_read(struct inode *pInode)
108{
109 AssertCompile(sizeof(loff_t) == sizeof(uint64_t));
110 return ASMAtomicReadU64((uint64_t volatile *)&pInode->i_size);
111}
112
113DECLINLINE(void) i_size_write(struct inode *pInode, loff_t cbNew)
114{
115 AssertCompile(sizeof(pInode->i_size) == sizeof(uint64_t));
116 ASMAtomicWriteU64((uint64_t volatile *)&pInode->i_size, cbNew);
117}
118
119#endif /* < 2.6.0 */
120
121#if RTLNX_VER_MAX(3,2,0) && !RTLNX_RHEL_MIN(6, 10)
122DECLINLINE(void) set_nlink(struct inode *pInode, unsigned int cLinks)
123{
124 pInode->i_nlink = cLinks;
125}
126#endif
127
128
129/* global variables */
130extern VBGLSFCLIENT g_SfClient;
131extern spinlock_t g_SfHandleLock;
132extern uint32_t g_uSfLastFunction;
133extern uint64_t g_fSfFeatures;
134
135extern struct inode_operations vbsf_dir_iops;
136extern struct inode_operations vbsf_lnk_iops;
137extern struct inode_operations vbsf_reg_iops;
138extern struct file_operations vbsf_dir_fops;
139extern struct file_operations vbsf_reg_fops;
140extern struct dentry_operations vbsf_dentry_ops;
141extern struct address_space_operations vbsf_reg_aops;
142
143
144/**
145 * VBox specific per-mount (shared folder) information.
146 */
147struct vbsf_super_info {
148 VBGLSFMAP map;
149 struct nls_table *nls;
150 /** Set if the NLS table is UTF-8. */
151 bool fNlsIsUtf8;
152 int uid;
153 int gid;
154 int dmode;
155 int fmode;
156 int dmask;
157 int fmask;
158 /** Maximum number of pages to allow in an I/O buffer with the host.
159 * This applies to read and write operations. */
160 uint32_t cMaxIoPages;
161 /** The default directory buffer size. */
162 uint32_t cbDirBuf;
163 /** The time to live for directory entries in jiffies, zero if disabled. */
164 uint32_t cJiffiesDirCacheTTL;
165 /** The time to live for inode information in jiffies, zero if disabled. */
166 uint32_t cJiffiesInodeTTL;
167 /** The cache and coherency mode. */
168 enum vbsf_cache_mode enmCacheMode;
169 /** Mount tag for VBoxService automounter. @since 6.0 */
170 char szTag[32];
171#if RTLNX_VER_RANGE(2,6,0, 4,12,0)
172 /** The backing device info structure. */
173 struct backing_dev_info bdi;
174#endif
175 /** The mount option value for /proc/mounts. */
176 int32_t msTTL;
177 /** The time to live for directory entries in milliseconds, for /proc/mounts. */
178 int32_t msDirCacheTTL;
179 /** The time to live for inode information in milliseconds, for /proc/mounts. */
180 int32_t msInodeTTL;
181#if RTLNX_VER_RANGE(4,0,0, 4,2,0)
182 /** 4.0 and 4.1 are missing noop_backing_dev_info export, so take down the
183 * initial value so we can restore it in vbsf_done_backing_dev(). (paranoia) */
184 struct backing_dev_info *bdi_org;
185#endif
186};
187
188/* Following casts are here to prevent assignment of void * to
189 pointers of arbitrary type */
190#if RTLNX_VER_MAX(2,6,0)
191# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->u.generic_sbp)
192# define VBSF_SET_SUPER_INFO(sb, a_pSuperInfo) do { (sb)->u.generic_sbp = a_pSuperInfo; } while (0)
193#else
194# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->s_fs_info)
195# define VBSF_SET_SUPER_INFO(sb, a_pSuperInfo) do { (sb)->s_fs_info = a_pSuperInfo;} while (0)
196#endif
197
198
199/**
200 * For associating inodes with host handles.
201 *
202 * This is necessary for address_space_operations::vbsf_writepage and allows
203 * optimizing stat, lookups and other operations on open files and directories.
204 */
205struct vbsf_handle {
206 /** List entry (head vbsf_inode_info::HandleList). */
207 RTLISTNODE Entry;
208 /** Host file/whatever handle. */
209 SHFLHANDLE hHost;
210 /** VBSF_HANDLE_F_XXX */
211 uint32_t fFlags;
212 /** Reference counter.
213 * Close the handle and free the structure when it reaches zero. */
214 uint32_t volatile cRefs;
215#ifdef VBOX_STRICT
216 /** For strictness checks. */
217 struct vbsf_inode_info *pInodeInfo;
218#endif
219};
220
221/** @name VBSF_HANDLE_F_XXX - Handle summary flags (vbsf_handle::fFlags).
222 * @{ */
223#define VBSF_HANDLE_F_READ UINT32_C(0x00000001)
224#define VBSF_HANDLE_F_WRITE UINT32_C(0x00000002)
225#define VBSF_HANDLE_F_APPEND UINT32_C(0x00000004)
226#define VBSF_HANDLE_F_FILE UINT32_C(0x00000010)
227#define VBSF_HANDLE_F_DIR UINT32_C(0x00000020)
228#define VBSF_HANDLE_F_ON_LIST UINT32_C(0x00000080)
229#define VBSF_HANDLE_F_MAGIC_MASK UINT32_C(0xffffff00)
230#define VBSF_HANDLE_F_MAGIC UINT32_C(0x75030700) /**< Maurice Ravel (1875-03-07). */
231#define VBSF_HANDLE_F_MAGIC_DEAD UINT32_C(0x19371228)
232/** @} */
233
234
235/**
236 * VBox specific per-inode information.
237 */
238struct vbsf_inode_info {
239 /** Which file */
240 SHFLSTRING *path;
241 /** Some information was changed, update data on next revalidate */
242 bool force_restat;
243 /** The timestamp (jiffies) where the inode info was last updated. */
244 unsigned long ts_up_to_date;
245 /** The birth time. */
246 RTTIMESPEC BirthTime;
247
248 /** @name Host modification detection stats.
249 * @{ */
250 /** The raw modification time, for mapping invalidation purposes. */
251 RTTIMESPEC ModificationTime;
252 /** Copy of ModificationTime from the last time we wrote to the the file. */
253 RTTIMESPEC ModificationTimeAtOurLastWrite;
254 /** @} */
255
256 /** handle valid if a file was created with vbsf_create_worker until it will
257 * be opened with vbsf_reg_open()
258 * @todo r=bird: figure this one out... */
259 SHFLHANDLE handle;
260
261 /** List of open handles (struct vbsf_handle), protected by g_SfHandleLock. */
262 RTLISTANCHOR HandleList;
263#ifdef VBOX_STRICT
264 uint32_t u32Magic;
265# define SF_INODE_INFO_MAGIC UINT32_C(0x18620822) /**< Claude Debussy */
266# define SF_INODE_INFO_MAGIC_DEAD UINT32_C(0x19180325)
267#endif
268};
269
270#if RTLNX_VER_MIN(2,6,19) || defined(KERNEL_FC6)
271/* FC6 kernel 2.6.18, vanilla kernel 2.6.19+ */
272# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->i_private)
273# define VBSF_SET_INODE_INFO(i, sf_i) (i)->i_private = sf_i
274#else
275/* vanilla kernel up to 2.6.18 */
276# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->u.generic_ip)
277# define VBSF_SET_INODE_INFO(i, sf_i) (i)->u.generic_ip = sf_i
278#endif
279
280extern void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO info,
281 struct vbsf_super_info *pSuperInfo);
282extern void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
283 struct vbsf_super_info *pSuperInfo, bool fInodeLocked, unsigned fSetAttrs);
284extern int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced, bool fInodeLocked);
285extern int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked);
286#if RTLNX_VER_MIN(2,5,18)
287# if RTLNX_VER_MIN(6,3,0)
288extern int vbsf_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
289 struct kstat *kstat, u32 request_mask, unsigned int query_flags);
290# elif RTLNX_VER_MIN(5,12,0)
291extern int vbsf_inode_getattr(struct user_namespace *ns, const struct path *path,
292 struct kstat *kstat, u32 request_mask, unsigned int query_flags);
293# elif RTLNX_VER_MIN(4,11,0)
294extern int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int query_flags);
295# else
296extern int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat);
297# endif
298#else /* < 2.5.44 */
299extern int vbsf_inode_revalidate(struct dentry *dentry);
300#endif /* < 2.5.44 */
301#if RTLNX_VER_MIN(6,3,0)
302extern int vbsf_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *iattr);
303#elif RTLNX_VER_MIN(5,12,0)
304extern int vbsf_inode_setattr(struct user_namespace *ns, struct dentry *dentry, struct iattr *iattr);
305#else
306extern int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr);
307#endif
308
309
310extern void vbsf_handle_drop_chain(struct vbsf_inode_info *pInodeInfo);
311extern struct vbsf_handle *vbsf_handle_find(struct vbsf_inode_info *pInodeInfo, uint32_t fFlagsSet, uint32_t fFlagsClear);
312extern uint32_t vbsf_handle_release_slow(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo,
313 const char *pszCaller);
314extern void vbsf_handle_append(struct vbsf_inode_info *pInodeInfo, struct vbsf_handle *pHandle);
315
316/**
317 * Releases a handle.
318 *
319 * @returns New reference count.
320 * @param pHandle The handle to release.
321 * @param pSuperInfo The info structure for the shared folder associated
322 * with the handle.
323 * @param pszCaller The caller name (for logging failures).
324 */
325DECLINLINE(uint32_t) vbsf_handle_release(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo, const char *pszCaller)
326{
327 uint32_t cRefs;
328
329 Assert((pHandle->fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC);
330 Assert(pHandle->pInodeInfo);
331 Assert(pHandle->pInodeInfo && pHandle->pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
332
333 cRefs = ASMAtomicDecU32(&pHandle->cRefs);
334 Assert(cRefs < _64M);
335 if (cRefs)
336 return cRefs;
337 return vbsf_handle_release_slow(pHandle, pSuperInfo, pszCaller);
338}
339
340
341/**
342 * VBox specific information for a regular file.
343 */
344struct vbsf_reg_info {
345 /** Handle tracking structure.
346 * @note Must be first! */
347 struct vbsf_handle Handle;
348};
349
350uint32_t vbsf_linux_oflags_to_vbox(unsigned fLnxOpen, uint32_t *pfHandle, const char *pszCaller);
351
352
353/**
354 * VBox specific information for an open directory.
355 */
356struct vbsf_dir_info {
357 /** Handle tracking structure.
358 * @note Must be first! */
359 struct vbsf_handle Handle;
360 /** Semaphore protecting everything below. */
361 struct semaphore Lock;
362 /** A magic number (VBSF_DIR_INFO_MAGIC). */
363 uint32_t u32Magic;
364 /** Size of the buffer for directory entries. */
365 uint32_t cbBuf;
366 /** Buffer for directory entries on the physical heap. */
367 PSHFLDIRINFO pBuf;
368 /** Number of valid bytes in the buffer. */
369 uint32_t cbValid;
370 /** Number of entries left in the buffer. */
371 uint32_t cEntriesLeft;
372 /** The position of the next entry. Incremented by one for each entry. */
373 loff_t offPos;
374 /** The next entry. */
375 PSHFLDIRINFO pEntry;
376 /** Set if there are no more files. */
377 bool fNoMoreFiles;
378};
379
380/** Magic number for vbsf_dir_info::u32Magic (Robert Anson Heinlein). */
381#define VBSF_DIR_INFO_MAGIC UINT32_C(0x19070707)
382/** Value of vbsf_dir_info::u32Magic when freed. */
383#define VBSF_DIR_INFO_MAGIC_DEAD UINT32_C(0x19880508)
384
385
386/**
387 * Sets the update-jiffies value for a dentry.
388 *
389 * This is used together with vbsf_super_info::cJiffiesDirCacheTTL to reduce
390 * re-validation of dentry structures while walking.
391 *
392 * This used to be living in d_time, but since 4.9.0 that seems to have become
393 * unfashionable and d_fsdata is now used to for this purpose. We do this all
394 * the way back, since d_time seems only to have been used by the file system
395 * specific code (at least going back to 2.4.0).
396 */
397DECLINLINE(void) vbsf_dentry_set_update_jiffies(struct dentry *pDirEntry, unsigned long uToSet)
398{
399 /*SFLOG3(("vbsf_dentry_set_update_jiffies: %p: %lx -> %#lx\n", pDirEntry, (unsigned long)pDirEntry->d_fsdata, uToSet));*/
400 pDirEntry->d_fsdata = (void *)uToSet;
401}
402
403/**
404 * Get the update-jiffies value for a dentry.
405 */
406DECLINLINE(unsigned long) vbsf_dentry_get_update_jiffies(struct dentry *pDirEntry)
407{
408 return (unsigned long)pDirEntry->d_fsdata;
409}
410
411/**
412 * Invalidates the update TTL for the given directory entry so that it is
413 * revalidate the next time it is used.
414 * @param pDirEntry The directory entry cache entry to invalidate.
415 */
416DECLINLINE(void) vbsf_dentry_invalidate_ttl(struct dentry *pDirEntry)
417{
418 vbsf_dentry_set_update_jiffies(pDirEntry, jiffies - INT32_MAX / 2);
419}
420
421/**
422 * Increase the time-to-live of @a pDirEntry and all ancestors.
423 * @param pDirEntry The directory entry cache entry which ancestors
424 * we should increase the TTL for.
425 */
426DECLINLINE(void) vbsf_dentry_chain_increase_ttl(struct dentry *pDirEntry)
427{
428#ifdef VBOX_STRICT
429 struct super_block * const pSuper = pDirEntry->d_sb;
430#endif
431 unsigned long const uToSet = jiffies;
432 do {
433 Assert(pDirEntry->d_sb == pSuper);
434 vbsf_dentry_set_update_jiffies(pDirEntry, uToSet);
435 pDirEntry = pDirEntry->d_parent;
436 } while (!IS_ROOT(pDirEntry));
437}
438
439/**
440 * Increase the time-to-live of all ancestors.
441 * @param pDirEntry The directory entry cache entry which ancestors
442 * we should increase the TTL for.
443 */
444DECLINLINE(void) vbsf_dentry_chain_increase_parent_ttl(struct dentry *pDirEntry)
445{
446 Assert(!pDirEntry->d_parent || pDirEntry->d_parent->d_sb == pDirEntry->d_sb);
447 pDirEntry = pDirEntry->d_parent;
448 if (pDirEntry)
449 vbsf_dentry_chain_increase_ttl(pDirEntry);
450}
451
452/** Macro for getting the dentry for a struct file. */
453#if RTLNX_VER_MIN(4,6,0)
454# define VBSF_GET_F_DENTRY(f) file_dentry(f)
455#elif RTLNX_VER_MIN(2,6,20)
456# define VBSF_GET_F_DENTRY(f) (f->f_path.dentry)
457#else
458# define VBSF_GET_F_DENTRY(f) (f->f_dentry)
459#endif
460
461/**
462 * Macro for checking if the 'data' argument passed in via mount(2) was supplied
463 * by the mount.vboxsf command line utility as a page of data containing the
464 * vbsf_mount_info_new structure.
465 */
466#define VBSF_IS_MOUNT_VBOXSF_DATA(data) \
467 (((struct vbsf_mount_info_new *)data)->nullchar == '\0' && \
468 ((struct vbsf_mount_info_new *)data)->signature[0] == VBSF_MOUNT_SIGNATURE_BYTE_0 && \
469 ((struct vbsf_mount_info_new *)data)->signature[1] == VBSF_MOUNT_SIGNATURE_BYTE_1 && \
470 ((struct vbsf_mount_info_new *)data)->signature[2] == VBSF_MOUNT_SIGNATURE_BYTE_2)
471
472extern int vbsf_stat(const char *caller, struct vbsf_super_info *pSuperInfo, SHFLSTRING * path, PSHFLFSOBJINFO result,
473 int ok_to_fail);
474extern int vbsf_path_from_dentry(struct vbsf_super_info *pSuperInfo, struct vbsf_inode_info *sf_i, struct dentry *dentry,
475 SHFLSTRING ** result, const char *caller);
476extern int vbsf_nlscpy(struct vbsf_super_info *pSuperInfo, char *name, size_t name_bound_len,
477 const unsigned char *utf8_name, size_t utf8_len);
478extern int vbsf_nls_to_shflstring(struct vbsf_super_info *pSuperInfo, const char *pszNls, PSHFLSTRING *ppString);
479
480
481/**
482 * Converts Linux access permissions to VBox ones (mode & 0777).
483 *
484 * @note Currently identical.
485 * @sa sf_access_permissions_to_linux
486 */
487DECLINLINE(uint32_t) sf_access_permissions_to_vbox(int fAttr)
488{
489 /* Access bits should be the same: */
490 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
491 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
492 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
493 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
494 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
495 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
496 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
497 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
498 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
499
500 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
501}
502
503#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