VirtualBox

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

Last change on this file since 77819 was 77743, checked in by vboxsync, 6 years ago

linux/vboxsf: Fixed nls conversion issue (don't try convert the zero terminator, at least not on 2.6.8). Hacked around missing invalidate_mapping_pages symbol in kernels before 2.6.21. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/* $Id: vfsmod.h 77743 2019-03-17 03:43:09Z vboxsync $ */
2/** @file
3 * vboxsf - Linux Shared Folders VFS, internal header.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
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 LINUX_VERSION_CODE >= KERNEL_VERSION(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/*
83 * inode compatibility glue.
84 */
85#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
86
87DECLINLINE(loff_t) i_size_read(struct inode *pInode)
88{
89 AssertCompile(sizeof(loff_t) == sizeof(uint64_t));
90 return ASMAtomicReadU64((uint64_t volatile *)&pInode->i_size);
91}
92
93DECLINLINE(void) i_size_write(struct inode *pInode, loff_t cbNew)
94{
95 AssertCompile(sizeof(pInode->i_size) == sizeof(uint64_t));
96 ASMAtomicWriteU64((uint64_t volatile *)&pInode->i_size, cbNew);
97}
98
99#endif /* < 2.6.0 */
100
101#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
102DECLINLINE(void) set_nlink(struct inode *pInode, unsigned int cLinks)
103{
104 pInode->i_nlink = cLinks;
105}
106#endif
107
108
109/* global variables */
110extern VBGLSFCLIENT g_SfClient;
111extern spinlock_t g_SfHandleLock;
112
113extern struct inode_operations vbsf_dir_iops;
114extern struct inode_operations vbsf_lnk_iops;
115extern struct inode_operations vbsf_reg_iops;
116extern struct file_operations vbsf_dir_fops;
117extern struct file_operations vbsf_reg_fops;
118extern struct dentry_operations vbsf_dentry_ops;
119extern struct address_space_operations vbsf_reg_aops;
120
121
122/**
123 * VBox specific per-mount (shared folder) information.
124 */
125struct vbsf_super_info {
126 VBGLSFMAP map;
127 struct nls_table *nls;
128 /** Set if the NLS table is UTF-8. */
129 bool fNlsIsUtf8;
130 /** time-to-live value for direntry and inode info in jiffies.
131 * Zero == disabled. */
132 unsigned long ttl;
133 /** The mount option value for /proc/mounts. */
134 int ttl_msec;
135 int uid;
136 int gid;
137 int dmode;
138 int fmode;
139 int dmask;
140 int fmask;
141 /** Maximum number of pages to allow in an I/O buffer with the host.
142 * This applies to read and write operations. */
143 uint32_t cMaxIoPages;
144 /** Mount tag for VBoxService automounter. @since 6.0 */
145 char tag[32];
146#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
147 /** The backing device info structure. */
148 struct backing_dev_info bdi;
149#endif
150};
151
152/* Following casts are here to prevent assignment of void * to
153 pointers of arbitrary type */
154#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
155# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->u.generic_sbp)
156# define VBSF_SET_SUPER_INFO(sb, sf_g) do { (sb)->u.generic_sbp = sf_g; } while (0)
157#else
158# define VBSF_GET_SUPER_INFO(sb) ((struct vbsf_super_info *)(sb)->s_fs_info)
159# define VBSF_SET_SUPER_INFO(sb, sf_g) do { (sb)->s_fs_info = sf_g;} while (0)
160#endif
161
162
163/**
164 * For associating inodes with host handles.
165 *
166 * This is necessary for address_space_operations::vbsf_writepage and allows
167 * optimizing stat, lookups and other operations on open files and directories.
168 */
169struct vbsf_handle {
170 /** List entry (head vbsf_inode_info::HandleList). */
171 RTLISTNODE Entry;
172 /** Host file/whatever handle. */
173 SHFLHANDLE hHost;
174 /** VBSF_HANDLE_F_XXX */
175 uint32_t fFlags;
176 /** Reference counter.
177 * Close the handle and free the structure when it reaches zero. */
178 uint32_t volatile cRefs;
179#ifdef VBOX_STRICT
180 /** For strictness checks. */
181 struct vbsf_inode_info *pInodeInfo;
182#endif
183};
184
185/** @name VBSF_HANDLE_F_XXX - Handle summary flags (vbsf_handle::fFlags).
186 * @{ */
187#define VBSF_HANDLE_F_READ UINT32_C(0x00000001)
188#define VBSF_HANDLE_F_WRITE UINT32_C(0x00000002)
189#define VBSF_HANDLE_F_APPEND UINT32_C(0x00000004)
190#define VBSF_HANDLE_F_FILE UINT32_C(0x00000010)
191#define VBSF_HANDLE_F_DIR UINT32_C(0x00000020)
192#define VBSF_HANDLE_F_ON_LIST UINT32_C(0x00000080)
193#define VBSF_HANDLE_F_MAGIC_MASK UINT32_C(0xffffff00)
194#define VBSF_HANDLE_F_MAGIC UINT32_C(0x75030700) /**< Maurice Ravel (1875-03-07). */
195#define VBSF_HANDLE_F_MAGIC_DEAD UINT32_C(0x19371228)
196/** @} */
197
198
199/**
200 * VBox specific per-inode information.
201 */
202struct vbsf_inode_info {
203 /** Which file */
204 SHFLSTRING *path;
205 /** Some information was changed, update data on next revalidate */
206 bool force_restat;
207 /** The timestamp (jiffies) where the inode info was last updated. */
208 unsigned long ts_up_to_date;
209 /** The birth time. */
210 RTTIMESPEC BirthTime;
211
212 /** handle valid if a file was created with vbsf_create_worker until it will
213 * be opened with vbsf_reg_open()
214 * @todo r=bird: figure this one out... */
215 SHFLHANDLE handle;
216
217 /** List of open handles (struct vbsf_handle), protected by g_SfHandleLock. */
218 RTLISTANCHOR HandleList;
219#ifdef VBOX_STRICT
220 uint32_t u32Magic;
221# define SF_INODE_INFO_MAGIC UINT32_C(0x18620822) /**< Claude Debussy */
222# define SF_INODE_INFO_MAGIC_DEAD UINT32_C(0x19180325)
223#endif
224};
225
226#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) || defined(KERNEL_FC6)
227/* FC6 kernel 2.6.18, vanilla kernel 2.6.19+ */
228# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->i_private)
229# define VBSF_SET_INODE_INFO(i, sf_i) (i)->i_private = sf_i
230#else
231/* vanilla kernel up to 2.6.18 */
232# define VBSF_GET_INODE_INFO(i) ((struct vbsf_inode_info *) (i)->u.generic_ip)
233# define VBSF_SET_INODE_INFO(i, sf_i) (i)->u.generic_ip = sf_i
234#endif
235
236extern void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO info, struct vbsf_super_info *sf_g);
237extern void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
238 struct vbsf_super_info *sf_g, bool fInodeLocked);
239extern int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced, bool fInodeLocked);
240extern int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked);
241#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 18)
242# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
243extern int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int query_flags);
244# else
245extern int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat);
246# endif
247#else /* < 2.5.44 */
248extern int vbsf_inode_revalidate(struct dentry *dentry);
249#endif /* < 2.5.44 */
250extern int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr);
251
252
253extern void vbsf_handle_drop_chain(struct vbsf_inode_info *pInodeInfo);
254extern struct vbsf_handle *vbsf_handle_find(struct vbsf_inode_info *pInodeInfo, uint32_t fFlagsSet, uint32_t fFlagsClear);
255extern uint32_t vbsf_handle_release_slow(struct vbsf_handle *pHandle, struct vbsf_super_info *sf_g, const char *pszCaller);
256extern void vbsf_handle_append(struct vbsf_inode_info *pInodeInfo, struct vbsf_handle *pHandle);
257
258/**
259 * Releases a handle.
260 *
261 * @returns New reference count.
262 * @param pHandle The handle to release.
263 * @param sf_g The info structure for the shared folder associated
264 * with the handle.
265 * @param pszCaller The caller name (for logging failures).
266 */
267DECLINLINE(uint32_t) vbsf_handle_release(struct vbsf_handle *pHandle, struct vbsf_super_info *sf_g, const char *pszCaller)
268{
269 uint32_t cRefs;
270
271 Assert((pHandle->fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC);
272 Assert(pHandle->pInodeInfo);
273 Assert(pHandle->pInodeInfo && pHandle->pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
274
275 cRefs = ASMAtomicDecU32(&pHandle->cRefs);
276 Assert(cRefs < _64M);
277 if (cRefs)
278 return cRefs;
279 return vbsf_handle_release_slow(pHandle, sf_g, pszCaller);
280}
281
282
283/**
284 * VBox specific information for a regular file.
285 */
286struct vbsf_reg_info {
287 /** Handle tracking structure.
288 * @note Must be first! */
289 struct vbsf_handle Handle;
290};
291
292uint32_t vbsf_linux_oflags_to_vbox(unsigned fLnxOpen, uint32_t *pfHandle, const char *pszCaller);
293
294
295/**
296 * VBox specific information for an open directory.
297 */
298struct vbsf_dir_info {
299 /** Handle tracking structure.
300 * @note Must be first! */
301 struct vbsf_handle Handle;
302 /** Semaphore protecting everything below. */
303 struct semaphore Lock;
304 /** A magic number (VBSF_DIR_INFO_MAGIC). */
305 uint32_t u32Magic;
306 /** Size of the buffer for directory entries. */
307 uint32_t cbBuf;
308 /** Buffer for directory entries on the physical heap. */
309 PSHFLDIRINFO pBuf;
310 /** Number of valid bytes in the buffer. */
311 uint32_t cbValid;
312 /** Number of entries left in the buffer. */
313 uint32_t cEntriesLeft;
314 /** The position of the next entry. Incremented by one for each entry. */
315 loff_t offPos;
316 /** The next entry. */
317 PSHFLDIRINFO pEntry;
318 /** Set if there are no more files. */
319 bool fNoMoreFiles;
320};
321
322/** Magic number for vbsf_dir_info::u32Magic (Robert Anson Heinlein). */
323#define VBSF_DIR_INFO_MAGIC UINT32_C(0x19070707)
324/** Value of vbsf_dir_info::u32Magic when freed. */
325#define VBSF_DIR_INFO_MAGIC_DEAD UINT32_C(0x19880508)
326
327
328/**
329 * Sets the update-jiffies value for a dentry.
330 *
331 * This is used together with vbsf_super_info::ttl to reduce re-validation of
332 * dentry structures while walking.
333 *
334 * This used to be living in d_time, but since 4.9.0 that seems to have become
335 * unfashionable and d_fsdata is now used to for this purpose. We do this all
336 * the way back, since d_time seems only to have been used by the file system
337 * specific code (at least going back to 2.4.0).
338 */
339DECLINLINE(void) vbsf_dentry_set_update_jiffies(struct dentry *pDirEntry, unsigned long uToSet)
340{
341 /*SFLOG3(("vbsf_dentry_set_update_jiffies: %p: %lx -> %#lx\n", pDirEntry, (unsigned long)pDirEntry->d_fsdata, uToSet));*/
342 pDirEntry->d_fsdata = (void *)uToSet;
343}
344
345/**
346 * Get the update-jiffies value for a dentry.
347 */
348DECLINLINE(unsigned long) vbsf_dentry_get_update_jiffies(struct dentry *pDirEntry)
349{
350 return (unsigned long)pDirEntry->d_fsdata;
351}
352
353/**
354 * Increase the time-to-live of @a pDirEntry and all ancestors.
355 * @param pDirEntry The directory entry cache entry which ancestors
356 * we should increase the TTL for.
357 */
358DECLINLINE(void) vbsf_dentry_chain_increase_ttl(struct dentry *pDirEntry)
359{
360#ifdef VBOX_STRICT
361 struct super_block * const pSuper = pDirEntry->d_sb;
362#endif
363 unsigned long const uToSet = jiffies;
364 do {
365 Assert(pDirEntry->d_sb == pSuper);
366 vbsf_dentry_set_update_jiffies(pDirEntry, uToSet);
367 pDirEntry = pDirEntry->d_parent;
368 } while (!IS_ROOT(pDirEntry));
369}
370
371/**
372 * Increase the time-to-live of all ancestors.
373 * @param pDirEntry The directory entry cache entry which ancestors
374 * we should increase the TTL for.
375 */
376DECLINLINE(void) vbsf_dentry_chain_increase_parent_ttl(struct dentry *pDirEntry)
377{
378 Assert(!pDirEntry->d_parent || pDirEntry->d_parent->d_sb == pDirEntry->d_sb);
379 pDirEntry = pDirEntry->d_parent;
380 if (pDirEntry)
381 vbsf_dentry_chain_increase_ttl(pDirEntry);
382}
383
384/** Macro for getting the dentry for a struct file. */
385#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
386# define VBSF_GET_F_DENTRY(f) file_dentry(f)
387#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
388# define VBSF_GET_F_DENTRY(f) (f->f_path.dentry)
389#else
390# define VBSF_GET_F_DENTRY(f) (f->f_dentry)
391#endif
392
393extern int vbsf_stat(const char *caller, struct vbsf_super_info *sf_g, SHFLSTRING * path, PSHFLFSOBJINFO result, int ok_to_fail);
394extern int vbsf_path_from_dentry(const char *caller, struct vbsf_super_info *sf_g, struct vbsf_inode_info *sf_i,
395 struct dentry *dentry, SHFLSTRING ** result);
396extern int vbsf_nlscpy(struct vbsf_super_info *sf_g, char *name, size_t name_bound_len,
397 const unsigned char *utf8_name, size_t utf8_len);
398
399
400/**
401 * Converts Linux access permissions to VBox ones (mode & 0777).
402 *
403 * @note Currently identical.
404 * @sa sf_access_permissions_to_linux
405 */
406DECLINLINE(uint32_t) sf_access_permissions_to_vbox(int fAttr)
407{
408 /* Access bits should be the same: */
409 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
410 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
411 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
412 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
413 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
414 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
415 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
416 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
417 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
418
419 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
420}
421
422#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