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