VirtualBox

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

Last change on this file was 103011, checked in by vboxsync, 3 months ago

iprt/asm.h,*: Split out the ASMMem* and related stuff into a separate header, asm-mem.h, so that we can get the RT_ASM_PAGE_SIZE stuff out of the way. [build fixes]

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

© 2023 Oracle
ContactPrivacy policyTerms of Use