VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/dirops.c@ 78020

Last change on this file since 78020 was 77961, checked in by vboxsync, 6 years ago

linux/vboxsf: Don't set timestamps when changing the file size (open+O_TRUNC; ftrunctate), save a host call. Don't interpret setting mtime or changing the file size as a host file modification. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.5 KB
Line 
1/* $Id: dirops.c 77961 2019-03-30 01:46:38Z vboxsync $ */
2/** @file
3 * vboxsf - VBox Linux Shared Folders VFS, directory inode and file operations.
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
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include "vfsmod.h"
36#include <iprt/err.h>
37
38#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
39# define d_in_lookup(a_pDirEntry) (d_unhashed(a_pDirEntry))
40#endif
41
42
43
44/**
45 * Open a directory (implements file_operations::open).
46 *
47 * @returns 0 on success, negative errno otherwise.
48 * @param inode inode
49 * @param file file
50 */
51static int vbsf_dir_open(struct inode *inode, struct file *file)
52{
53 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
54 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
55 struct dentry *dentry = VBSF_GET_F_DENTRY(file);
56 struct vbsf_dir_info *sf_d;
57 int rc;
58
59 SFLOGFLOW(("vbsf_dir_open: inode=%p file=%p %s\n", inode, file, sf_i && sf_i->path ? sf_i->path->String.ach : NULL));
60 AssertReturn(pSuperInfo, -EINVAL);
61 AssertReturn(sf_i, -EINVAL);
62 AssertReturn(!file->private_data, 0);
63
64 /*
65 * Allocate and initialize our directory info structure.
66 * We delay buffer allocation until vbsf_getdent is actually used.
67 */
68 sf_d = kmalloc(sizeof(*sf_d), GFP_KERNEL);
69 if (sf_d) {
70 VBOXSFCREATEREQ *pReq;
71 RT_ZERO(*sf_d);
72 sf_d->u32Magic = VBSF_DIR_INFO_MAGIC;
73 sema_init(&sf_d->Lock, 1);
74
75 /*
76 * Try open the directory.
77 */
78 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath.String) + sf_i->path->u16Size);
79 if (pReq) {
80 memcpy(&pReq->StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
81 RT_ZERO(pReq->CreateParms);
82 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
83 pReq->CreateParms.CreateFlags = SHFL_CF_DIRECTORY
84 | SHFL_CF_ACT_OPEN_IF_EXISTS
85 | SHFL_CF_ACT_FAIL_IF_NEW
86 | SHFL_CF_ACCESS_READ;
87
88 LogFunc(("calling VbglR0SfHostReqCreate on folder %s, flags %#x\n",
89 sf_i->path->String.utf8, pReq->CreateParms.CreateFlags));
90 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, pReq);
91 if (RT_SUCCESS(rc)) {
92 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
93 Assert(pReq->CreateParms.Handle != SHFL_HANDLE_NIL);
94
95 /*
96 * Update the inode info with fresh stats and increase the TTL for the
97 * dentry cache chain that got us here.
98 */
99 vbsf_update_inode(inode, sf_i, &pReq->CreateParms.Info, pSuperInfo,
100 true /*fLocked*/ /** @todo inode locking */, 0 /*fSetAttrs*/);
101 vbsf_dentry_chain_increase_ttl(dentry);
102
103 sf_d->Handle.hHost = pReq->CreateParms.Handle;
104 sf_d->Handle.cRefs = 1;
105 sf_d->Handle.fFlags = VBSF_HANDLE_F_READ | VBSF_HANDLE_F_DIR | VBSF_HANDLE_F_MAGIC;
106 vbsf_handle_append(sf_i, &sf_d->Handle);
107
108 file->private_data = sf_d;
109 VbglR0PhysHeapFree(pReq);
110 SFLOGFLOW(("vbsf_dir_open(%p,%p): returns 0; hHost=%#llx\n", inode, file, sf_d->Handle.hHost));
111 return 0;
112
113 }
114 Assert(pReq->CreateParms.Handle == SHFL_HANDLE_NIL);
115
116 /*
117 * Directory does not exist, so we probably got some invalid
118 * dir cache and inode info.
119 */
120 /** @todo do more to invalidate dentry and inode here. */
121 vbsf_dentry_invalidate_ttl(dentry);
122 sf_i->force_restat = true;
123 rc = -ENOENT;
124 } else
125 rc = -EPERM;
126 VbglR0PhysHeapFree(pReq);
127 } else {
128 LogRelMaxFunc(64, ("failed to allocate %zu bytes for '%s'\n",
129 RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath.String) + sf_i->path->u16Size, sf_i->path->String.ach));
130 rc = -ENOMEM;
131 }
132 sf_d->u32Magic = VBSF_DIR_INFO_MAGIC_DEAD;
133 kfree(sf_d);
134 } else
135 rc = -ENOMEM;
136 SFLOGFLOW(("vbsf_dir_open(%p,%p): returns %d\n", inode, file, rc));
137 return rc;
138}
139
140
141/**
142 * This is called when reference count of [file] goes to zero. Notify
143 * the host that it can free whatever is associated with this directory
144 * and deallocate our own internal buffers
145 *
146 * @param inode inode
147 * @param file file
148 * returns 0 on success, Linux error code otherwise
149 */
150static int vbsf_dir_release(struct inode *inode, struct file *file)
151{
152 struct vbsf_dir_info *sf_d = (struct vbsf_dir_info *)file->private_data;
153
154 SFLOGFLOW(("vbsf_dir_release(%p,%p): sf_d=%p hHost=%#llx\n", inode, file, sf_d, sf_d ? sf_d->Handle.hHost : SHFL_HANDLE_NIL));
155
156 if (sf_d) {
157 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
158
159 /* Invalidate the non-handle part. */
160 sf_d->u32Magic = VBSF_DIR_INFO_MAGIC_DEAD;
161 sf_d->cEntriesLeft = 0;
162 sf_d->cbValid = 0;
163 sf_d->pEntry = NULL;
164 sf_d->fNoMoreFiles = false;
165 if (sf_d->pBuf) {
166 kfree(sf_d->pBuf);
167 sf_d->pBuf = NULL;
168 }
169
170 /* Closes the handle and frees the structure when the last reference is released. */
171 vbsf_handle_release(&sf_d->Handle, pSuperInfo, "vbsf_dir_release");
172 }
173
174 return 0;
175}
176
177
178/**
179 * Translate RTFMODE into DT_xxx (in conjunction to rtDirType()).
180 * returns d_type
181 * @param fMode file mode
182 */
183DECLINLINE(int) vbsf_get_d_type(RTFMODE fMode)
184{
185 switch (fMode & RTFS_TYPE_MASK) {
186 case RTFS_TYPE_FIFO: return DT_FIFO;
187 case RTFS_TYPE_DEV_CHAR: return DT_CHR;
188 case RTFS_TYPE_DIRECTORY: return DT_DIR;
189 case RTFS_TYPE_DEV_BLOCK: return DT_BLK;
190 case RTFS_TYPE_FILE: return DT_REG;
191 case RTFS_TYPE_SYMLINK: return DT_LNK;
192 case RTFS_TYPE_SOCKET: return DT_SOCK;
193 case RTFS_TYPE_WHITEOUT: return DT_WHT;
194 }
195 return DT_UNKNOWN;
196}
197
198
199/**
200 * Refills the buffer with more entries.
201 *
202 * @returns 0 on success, negative errno on error,
203 */
204static int vbsf_dir_read_more(struct vbsf_dir_info *sf_d, struct vbsf_super_info *pSuperInfo, bool fRestart)
205{
206 int rc;
207 VBOXSFLISTDIRREQ *pReq;
208
209 /*
210 * Don't call the host again if we've reached the end of the
211 * directory entries already.
212 */
213 if (sf_d->fNoMoreFiles) {
214 if (!fRestart) {
215 SFLOGFLOW(("vbsf_dir_read_more: no more files\n"));
216 return 0;
217 }
218 sf_d->fNoMoreFiles = false;
219 }
220
221 /*
222 * Make sure we've got some kind of buffers.
223 */
224 if (sf_d->pBuf) {
225 /* Likely, except for the first time. */
226 } else {
227 sf_d->pBuf = (PSHFLDIRINFO)kmalloc(pSuperInfo->cbDirBuf, GFP_KERNEL);
228 if (sf_d->pBuf)
229 sf_d->cbBuf = pSuperInfo->cbDirBuf;
230 else {
231 sf_d->pBuf = (PSHFLDIRINFO)kmalloc(_4K, GFP_KERNEL);
232 if (!sf_d->pBuf) {
233 LogRelMax(10, ("vbsf_dir_read_more: Failed to allocate buffer!\n"));
234 return -ENOMEM;
235 }
236 sf_d->cbBuf = _4K;
237 }
238 }
239
240 /*
241 * Allocate a request buffer.
242 */
243 pReq = (VBOXSFLISTDIRREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
244 if (pReq) {
245 rc = VbglR0SfHostReqListDirContig2x(pSuperInfo->map.root, pReq, sf_d->Handle.hHost, NULL, NIL_RTGCPHYS64,
246 fRestart ? SHFL_LIST_RESTART : SHFL_LIST_NONE,
247 sf_d->pBuf, virt_to_phys(sf_d->pBuf), sf_d->cbBuf);
248 if (RT_SUCCESS(rc)) {
249 sf_d->pEntry = sf_d->pBuf;
250 sf_d->cbValid = pReq->Parms.cb32Buffer.u.value32;
251 sf_d->cEntriesLeft = pReq->Parms.c32Entries.u.value32;
252 sf_d->fNoMoreFiles = pReq->Parms.f32More.u.value32 == 0;
253 } else {
254 sf_d->pEntry = sf_d->pBuf;
255 sf_d->cbValid = 0;
256 sf_d->cEntriesLeft = 0;
257 if (rc == VERR_NO_MORE_FILES) {
258 sf_d->fNoMoreFiles = true;
259 rc = 0;
260 } else {
261 /* In theory we could end up here with a buffer overflow, but
262 with a 4KB minimum buffer size that's very unlikely with the
263 typical filename length of today's file systems (2019). */
264 LogRelMax(16, ("vbsf_dir_read_more: VbglR0SfHostReqListDirContig2x -> %Rrc\n", rc));
265 rc = -EPROTO;
266 }
267 }
268 VbglR0PhysHeapFree(pReq);
269 } else
270 rc = -ENOMEM;
271 SFLOGFLOW(("vbsf_dir_read_more: returns %d; cbValid=%#x cEntriesLeft=%#x fNoMoreFiles=%d\n",
272 rc, sf_d->cbValid, sf_d->cEntriesLeft, sf_d->fNoMoreFiles));
273 return rc;
274}
275
276
277/**
278 * Helper function for when we need to convert the name, avoids wasting stack in
279 * the UTF-8 code path.
280 */
281DECL_NO_INLINE(static, bool) vbsf_dir_emit_nls(
282# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
283 struct dir_context *ctx,
284# else
285 void *opaque, filldir_t filldir, loff_t offPos,
286# endif
287 const char *pszSrcName, uint16_t cchSrcName, ino_t d_ino, int d_type,
288 struct vbsf_super_info *pSuperInfo)
289{
290 char szDstName[NAME_MAX];
291 int rc = vbsf_nlscpy(pSuperInfo, szDstName, sizeof(szDstName), pszSrcName, cchSrcName);
292 if (rc == 0) {
293#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
294 return dir_emit(ctx, szDstName, strlen(szDstName), d_ino, d_type);
295#else
296 return filldir(opaque, szDstName, strlen(szDstName), offPos, d_ino, d_type) == 0;
297#endif
298 }
299
300 /* Assuming this is a buffer overflow issue, just silently skip it. */
301 SFLOGFLOW(("vbsf_dir_emit_nls: vbsf_nlscopy failed with %d for '%s'\n", rc, pszSrcName));
302 return true;
303}
304
305
306/**
307 * This is called when vfs wants to populate internal buffers with
308 * directory [dir]s contents. [opaque] is an argument to the
309 * [filldir]. [filldir] magically modifies it's argument - [opaque]
310 * and takes following additional arguments (which i in turn get from
311 * the host via vbsf_getdent):
312 *
313 * name : name of the entry (i must also supply it's length huh?)
314 * type : type of the entry (FILE | DIR | etc) (i ellect to use DT_UNKNOWN)
315 * pos : position/index of the entry
316 * ino : inode number of the entry (i fake those)
317 *
318 * [dir] contains:
319 * f_pos : cursor into the directory listing
320 * private_data : mean of communication with the host side
321 *
322 * Extract elements from the directory listing (incrementing f_pos
323 * along the way) and feed them to [filldir] until:
324 *
325 * a. there are no more entries (i.e. vbsf_getdent set done to 1)
326 * b. failure to compute fake inode number
327 * c. filldir returns an error (see comment on that)
328 */
329#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
330static int vbsf_dir_iterate(struct file *dir, struct dir_context *ctx)
331#else
332static int vbsf_dir_read(struct file *dir, void *opaque, filldir_t filldir)
333#endif
334{
335#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
336 loff_t offPos = ctx->pos;
337#else
338 loff_t offPos = dir->f_pos;
339#endif
340 struct vbsf_dir_info *sf_d = (struct vbsf_dir_info *)dir->private_data;
341 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(VBSF_GET_F_DENTRY(dir)->d_sb);
342 int rc;
343
344 /*
345 * Lock the directory info structures.
346 */
347 if (RT_LIKELY(down_interruptible(&sf_d->Lock) == 0)) {
348 /* likely */
349 } else
350 return -ERESTARTSYS;
351
352 /*
353 * Any seek performed in the mean time?
354 */
355 if (offPos == sf_d->offPos) {
356 /* likely */
357 } else {
358 /* Restart the search if iPos is lower than the current buffer position. */
359 loff_t offCurEntry = sf_d->offPos;
360 if (offPos < offCurEntry) {
361 rc = vbsf_dir_read_more(sf_d, pSuperInfo, true /*fRestart*/);
362 if (rc == 0)
363 offCurEntry = 0;
364 else {
365 up(&sf_d->Lock);
366 return rc;
367 }
368 }
369
370 /* Skip ahead to offPos. */
371 while (offCurEntry < offPos) {
372 uint32_t cEntriesLeft = sf_d->cEntriesLeft;
373 if ((uint64_t)(offPos - offCurEntry) >= cEntriesLeft) {
374 /* Skip the current buffer and read the next: */
375 offCurEntry += cEntriesLeft;
376 sf_d->offPos = offCurEntry;
377 sf_d->cEntriesLeft = 0;
378 rc = vbsf_dir_read_more(sf_d, pSuperInfo, false /*fRestart*/);
379 if (rc != 0 || sf_d->cEntriesLeft == 0) {
380 up(&sf_d->Lock);
381 return rc;
382 }
383 } else {
384 do
385 {
386 PSHFLDIRINFO pEntry = sf_d->pEntry;
387 pEntry = (PSHFLDIRINFO)&pEntry->name.String.utf8[pEntry->name.u16Length];
388 AssertLogRelBreakStmt( cEntriesLeft == 1
389 || (uintptr_t)pEntry - (uintptr_t)sf_d->pBuf
390 <= sf_d->cbValid - RT_UOFFSETOF(SHFLDIRINFO, name.String),
391 sf_d->cEntriesLeft = 0);
392 sf_d->cEntriesLeft = --cEntriesLeft;
393 sf_d->offPos = ++offCurEntry;
394 } while (offPos < sf_d->offPos);
395 }
396 }
397 }
398
399 /*
400 * Handle '.' and '..' specially so we get the inode numbers right.
401 * We'll skip any '.' or '..' returned by the host (included in pos,
402 * however, to simplify the above skipping code).
403 */
404 if (offPos < 2) {
405#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
406 if (offPos == 0) {
407 if (dir_emit_dot(dir, ctx))
408 dir->f_pos = ctx->pos = sf_d->offPos = offPos = 1;
409 else {
410 up(&sf_d->Lock);
411 return 0;
412 }
413 }
414 if (offPos == 1) {
415 if (dir_emit_dotdot(dir, ctx))
416 dir->f_pos = ctx->pos = sf_d->offPos = offPos = 2;
417 else {
418 up(&sf_d->Lock);
419 return 0;
420 }
421 }
422#else
423 if (offPos == 0) {
424 rc = filldir(opaque, ".", 1, 0, VBSF_GET_F_DENTRY(dir)->d_inode->i_ino, DT_DIR);
425 if (!rc)
426 dir->f_pos = sf_d->offPos = offPos = 1;
427 else {
428 up(&sf_d->Lock);
429 return 0;
430 }
431 }
432 if (offPos == 1) {
433# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 5)
434 rc = filldir(opaque, "..", 2, 1, parent_ino(VBSF_GET_F_DENTRY(dir)), DT_DIR);
435# else
436 rc = filldir(opaque, "..", 2, 1, VBSF_GET_F_DENTRY(dir)->d_parent->d_inode->i_ino, DT_DIR);
437# endif
438 if (!rc)
439 dir->f_pos = sf_d->offPos = offPos = 2;
440 else {
441 up(&sf_d->Lock);
442 return 0;
443 }
444 }
445#endif
446 }
447
448 /*
449 * Produce stuff.
450 */
451 Assert(offPos == sf_d->offPos);
452 for (;;) {
453 PSHFLDIRINFO pBuf;
454 PSHFLDIRINFO pEntry;
455
456 /*
457 * Do we need to read more?
458 */
459 uint32_t cbValid = sf_d->cbValid;
460 uint32_t cEntriesLeft = sf_d->cEntriesLeft;
461 if (!cEntriesLeft) {
462 rc = vbsf_dir_read_more(sf_d, pSuperInfo, false /*fRestart*/);
463 if (rc == 0) {
464 cEntriesLeft = sf_d->cEntriesLeft;
465 if (!cEntriesLeft) {
466 up(&sf_d->Lock);
467 return 0;
468 }
469 cbValid = sf_d->cbValid;
470 } else {
471 up(&sf_d->Lock);
472 return rc;
473 }
474 }
475
476 /*
477 * Feed entries to the caller.
478 */
479 pBuf = sf_d->pBuf;
480 pEntry = sf_d->pEntry;
481 do {
482 /*
483 * Validate the entry in case the host is messing with us.
484 * We're ASSUMING the host gives us a zero terminated string (UTF-8) here.
485 */
486 uintptr_t const offEntryInBuf = (uintptr_t)pEntry - (uintptr_t)pBuf;
487 uint16_t cbSrcName;
488 uint16_t cchSrcName;
489 AssertLogRelMsgBreak(offEntryInBuf + RT_UOFFSETOF(SHFLDIRINFO, name.String) <= cbValid,
490 ("%#llx + %#x vs %#x\n", offEntryInBuf, RT_UOFFSETOF(SHFLDIRINFO, name.String), cbValid));
491 cbSrcName = pEntry->name.u16Size;
492 cchSrcName = pEntry->name.u16Length;
493 AssertLogRelBreak(offEntryInBuf + RT_UOFFSETOF(SHFLDIRINFO, name.String) + cbSrcName <= cbValid);
494 AssertLogRelBreak(cchSrcName < cbSrcName);
495 AssertLogRelBreak(pEntry->name.String.ach[cchSrcName] == '\0');
496
497 /*
498 * Filter out '.' and '..' entires.
499 */
500 if ( cchSrcName > 2
501 || pEntry->name.String.ach[0] != '.'
502 || ( cchSrcName == 2
503 && pEntry->name.String.ach[1] != '.')) {
504 int const d_type = vbsf_get_d_type(pEntry->Info.Attr.fMode);
505 ino_t const d_ino = (ino_t)offPos + 0xbeef; /* very fake */
506 bool fContinue;
507 if (pSuperInfo->fNlsIsUtf8) {
508#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
509 fContinue = dir_emit(ctx, pEntry->name.String.ach, cchSrcName, d_ino, d_type);
510#else
511 fContinue = filldir(opaque, pEntry->name.String.ach, cchSrcName, offPos, d_ino, d_type) == 0;
512#endif
513 } else {
514#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
515 fContinue = vbsf_dir_emit_nls(ctx, pEntry->name.String.ach, cchSrcName, d_ino, d_type, pSuperInfo);
516#else
517 fContinue = vbsf_dir_emit_nls(opaque, filldir, offPos, pEntry->name.String.ach, cchSrcName,
518 d_ino, d_type, pSuperInfo);
519#endif
520 }
521 if (fContinue) {
522 /* likely */
523 } else {
524 sf_d->cEntriesLeft = cEntriesLeft;
525 sf_d->pEntry = pEntry;
526 sf_d->offPos = offPos;
527 up(&sf_d->Lock);
528 return 0;
529 }
530 }
531
532 /*
533 * Advance to the next entry.
534 */
535 pEntry = (PSHFLDIRINFO)((uintptr_t)pEntry + RT_UOFFSETOF(SHFLDIRINFO, name.String) + cbSrcName);
536 offPos += 1;
537 dir->f_pos = offPos;
538#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
539 ctx->pos = offPos;
540#endif
541 cEntriesLeft -= 1;
542 } while (cEntriesLeft > 0);
543
544 /* Done with all available entries. */
545 sf_d->offPos = offPos + cEntriesLeft;
546 sf_d->pEntry = pBuf;
547 sf_d->cEntriesLeft = 0;
548 }
549}
550
551
552/**
553 * Directory file operations.
554 */
555struct file_operations vbsf_dir_fops = {
556 .open = vbsf_dir_open,
557#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
558 .iterate_shared = vbsf_dir_iterate,
559#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
560 .iterate = vbsf_dir_iterate,
561#else
562 .readdir = vbsf_dir_read,
563#endif
564 .release = vbsf_dir_release,
565 .read = generic_read_dir,
566#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
567 .llseek = generic_file_llseek
568#endif
569};
570
571
572
573/*********************************************************************************************************************************
574* Directory Inode Operations *
575*********************************************************************************************************************************/
576
577/**
578 * Worker for vbsf_inode_lookup(), vbsf_create_worker() and
579 * vbsf_inode_instantiate().
580 */
581static struct inode *vbsf_create_inode(struct inode *parent, struct dentry *dentry, PSHFLSTRING path,
582 PSHFLFSOBJINFO pObjInfo, struct vbsf_super_info *pSuperInfo, bool fInstantiate)
583{
584 /*
585 * Allocate memory for our additional inode info and create an inode.
586 */
587 struct vbsf_inode_info *sf_new_i = (struct vbsf_inode_info *)kmalloc(sizeof(*sf_new_i), GFP_KERNEL);
588 if (sf_new_i) {
589 ino_t iNodeNo = iunique(parent->i_sb, 16);
590#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
591 struct inode *pInode = iget_locked(parent->i_sb, iNodeNo);
592#else
593 struct inode *pInode = iget(parent->i_sb, iNodeNo);
594#endif
595 if (pInode) {
596 /*
597 * Initialize the two structures.
598 */
599#ifdef VBOX_STRICT
600 sf_new_i->u32Magic = SF_INODE_INFO_MAGIC;
601#endif
602 sf_new_i->path = path;
603 sf_new_i->force_restat = false;
604 sf_new_i->ts_up_to_date = jiffies;
605 RTListInit(&sf_new_i->HandleList);
606 sf_new_i->handle = SHFL_HANDLE_NIL;
607
608 VBSF_SET_INODE_INFO(pInode, sf_new_i);
609 vbsf_init_inode(pInode, sf_new_i, pObjInfo, pSuperInfo);
610
611 /*
612 * Before we unlock the new inode, we may need to call d_instantiate.
613 */
614 if (fInstantiate)
615 d_instantiate(dentry, pInode);
616#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 25)
617 unlock_new_inode(pInode);
618#endif
619 return pInode;
620
621 }
622 LogFunc(("iget failed\n"));
623 kfree(sf_new_i);
624 } else
625 LogRelFunc(("could not allocate memory for new inode info\n"));
626 return NULL;
627}
628
629
630/** Helper for vbsf_create_worker() and vbsf_inode_lookup() that wraps
631 * d_add() and setting d_op. */
632DECLINLINE(void) vbsf_d_add_inode(struct dentry *dentry, struct inode *pNewInode)
633{
634#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
635 Assert(dentry->d_op == &vbsf_dentry_ops); /* (taken from the superblock) */
636#else
637 dentry->d_op = &vbsf_dentry_ops;
638#endif
639 d_add(dentry, pNewInode);
640}
641
642
643/**
644 * This is called when vfs failed to locate dentry in the cache. The
645 * job of this function is to allocate inode and link it to dentry.
646 * [dentry] contains the name to be looked in the [parent] directory.
647 * Failure to locate the name is not a "hard" error, in this case NULL
648 * inode is added to [dentry] and vfs should proceed trying to create
649 * the entry via other means. NULL(or "positive" pointer) ought to be
650 * returned in case of success and "negative" pointer on error
651 */
652static struct dentry *vbsf_inode_lookup(struct inode *parent, struct dentry *dentry
653#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
654 , unsigned int flags
655#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
656 , struct nameidata *nd
657#endif
658 )
659{
660 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(parent->i_sb);
661 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(parent);
662 SHFLSTRING *path;
663 struct dentry *dret;
664 int rc;
665
666#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
667 SFLOGFLOW(("vbsf_inode_lookup: parent=%p dentry=%p flags=%#x\n", parent, dentry, flags));
668#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
669 SFLOGFLOW(("vbsf_inode_lookup: parent=%p dentry=%p nd=%p{.flags=%#x}\n", parent, dentry, nd, nd ? nd->flags : 0));
670#else
671 SFLOGFLOW(("vbsf_inode_lookup: parent=%p dentry=%p\n", parent, dentry));
672#endif
673
674 Assert(pSuperInfo);
675 Assert(sf_i && sf_i->u32Magic == SF_INODE_INFO_MAGIC);
676
677 /*
678 * Build the path. We'll associate the path with dret's inode on success.
679 */
680 rc = vbsf_path_from_dentry(pSuperInfo, sf_i, dentry, &path, __func__);
681 if (rc == 0) {
682 /*
683 * Do a lookup on the host side.
684 */
685 VBOXSFCREATEREQ *pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + path->u16Size);
686 if (pReq) {
687 struct inode *pInode = NULL;
688
689 RT_ZERO(*pReq);
690 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
691 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
692 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
693
694 LogFunc(("Calling VbglR0SfHostReqCreate on %s\n", path->String.utf8));
695 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, pReq);
696 if (RT_SUCCESS(rc)) {
697 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
698 /*
699 * Create an inode for the result. Since this also confirms
700 * the existence of all parent dentries, we increase their TTL.
701 */
702 pInode = vbsf_create_inode(parent, dentry, path, &pReq->CreateParms.Info, pSuperInfo, false /*fInstantiate*/);
703 if (rc == 0) {
704 path = NULL; /* given to the inode */
705 dret = dentry;
706 } else
707 dret = (struct dentry *)ERR_PTR(-ENOMEM);
708 vbsf_dentry_chain_increase_parent_ttl(dentry);
709 } else if ( pReq->CreateParms.Result == SHFL_FILE_NOT_FOUND
710 || pReq->CreateParms.Result == SHFL_PATH_NOT_FOUND /*this probably should happen*/) {
711 dret = dentry;
712 } else {
713 AssertMsgFailed(("%d\n", pReq->CreateParms.Result));
714 dret = (struct dentry *)ERR_PTR(-EPROTO);
715 }
716 } else if (rc == VERR_INVALID_NAME) {
717 dret = dentry; /* this can happen for names like 'foo*' on a Windows host */
718 } else {
719 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc\n", path->String.utf8, rc));
720 dret = (struct dentry *)ERR_PTR(-EPROTO);
721 }
722 VbglR0PhysHeapFree(pReq);
723
724 /*
725 * When dret is set to dentry we got something to insert,
726 * though it may be negative (pInode == NULL).
727 */
728 if (dret == dentry) {
729 vbsf_dentry_set_update_jiffies(dentry, jiffies);
730 vbsf_d_add_inode(dentry, pInode);
731 dret = NULL;
732 }
733 } else
734 dret = (struct dentry *)ERR_PTR(-ENOMEM);
735 if (path)
736 kfree(path);
737 } else
738 dret = (struct dentry *)ERR_PTR(rc);
739 return dret;
740}
741
742
743/**
744 * This should allocate memory for vbsf_inode_info, compute a unique inode
745 * number, get an inode from vfs, initialize inode info, instantiate
746 * dentry.
747 *
748 * @param parent inode entry of the directory
749 * @param dentry directory cache entry
750 * @param path path name. Consumed on success.
751 * @param info file information
752 * @param handle handle
753 * @returns 0 on success, Linux error code otherwise
754 */
755static int vbsf_inode_instantiate(struct inode *parent, struct dentry *dentry, PSHFLSTRING path,
756 PSHFLFSOBJINFO info, SHFLHANDLE handle)
757{
758 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(parent->i_sb);
759 struct inode *pInode = vbsf_create_inode(parent, dentry, path, info, pSuperInfo, true /*fInstantiate*/);
760 if (pInode) {
761 /* Store this handle if we leave the handle open. */
762 struct vbsf_inode_info *sf_new_i = VBSF_GET_INODE_INFO(pInode);
763 sf_new_i->handle = handle;
764 return 0;
765 }
766 return -ENOMEM;
767}
768
769
770/**
771 * Create a new regular file / directory.
772 *
773 * @param parent inode of the directory
774 * @param dentry directory cache entry
775 * @param mode file mode
776 * @param fCreateFlags SHFL_CF_XXX.
777 * @param fStashHandle Whether the resulting handle should be stashed in
778 * the inode for a subsequent open call.
779 * @param fDoLookup Whether we're doing a lookup and need to d_add the
780 * inode we create to dentry.
781 * @param phHostFile Where to return the handle to the create file/dir.
782 * @param pfCreated Where to indicate whether the file/dir was created
783 * or not. Optional.
784 * @returns 0 on success, Linux error code otherwise
785 */
786static int vbsf_create_worker(struct inode *parent, struct dentry *dentry, umode_t mode, uint32_t fCreateFlags,
787 bool fStashHandle, bool fDoLookup, SHFLHANDLE *phHostFile, bool *pfCreated)
788
789{
790#ifdef SFLOG_ENABLED
791 const char * const pszPrefix = S_ISDIR(mode) ? "vbsf_create_worker/dir:" : "vbsf_create_worker/file:";
792#endif
793 struct vbsf_inode_info *sf_parent_i = VBSF_GET_INODE_INFO(parent);
794 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(parent->i_sb);
795 PSHFLSTRING path;
796 int rc;
797
798 AssertReturn(sf_parent_i, -EINVAL);
799 AssertReturn(pSuperInfo, -EINVAL);
800
801 /*
802 * Build a path. We'll donate this to the inode on success.
803 */
804 rc = vbsf_path_from_dentry(pSuperInfo, sf_parent_i, dentry, &path, __func__);
805 if (rc == 0) {
806 /*
807 * Allocate, initialize and issue the SHFL_CREATE request.
808 */
809 /** @todo combine with vbsf_path_from_dentry? */
810 union CreateAuxReq
811 {
812 VBOXSFCREATEREQ Create;
813 VBOXSFCLOSEREQ Close;
814 } *pReq = (union CreateAuxReq *)VbglR0PhysHeapAlloc(RT_UOFFSETOF(VBOXSFCREATEREQ, StrPath.String) + path->u16Size);
815 if (pReq) {
816 memcpy(&pReq->Create.StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
817 RT_ZERO(pReq->Create.CreateParms);
818 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
819 pReq->Create.CreateParms.CreateFlags = fCreateFlags;
820 pReq->Create.CreateParms.Info.Attr.fMode = (S_ISDIR(mode) ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE)
821 | sf_access_permissions_to_vbox(mode);
822 pReq->Create.CreateParms.Info.Attr.enmAdditional = RTFSOBJATTRADD_NOTHING;
823
824 SFLOGFLOW(("%s calling VbglR0SfHostReqCreate(%s, %#x)\n", pszPrefix, path->String.ach, pReq->Create.CreateParms.CreateFlags));
825 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, &pReq->Create);
826 if (RT_SUCCESS(rc)) {
827 SFLOGFLOW(("%s VbglR0SfHostReqCreate returned %Rrc Result=%d Handle=%#llx\n",
828 pszPrefix, rc, pReq->Create.CreateParms.Result, pReq->Create.CreateParms.Handle));
829
830 /*
831 * Work the dentry cache and inode restatting.
832 */
833 if ( pReq->Create.CreateParms.Result == SHFL_FILE_CREATED
834 || pReq->Create.CreateParms.Result == SHFL_FILE_REPLACED) {
835 vbsf_dentry_chain_increase_parent_ttl(dentry);
836 sf_parent_i->force_restat = 1;
837 } else if ( pReq->Create.CreateParms.Result == SHFL_FILE_EXISTS
838 || pReq->Create.CreateParms.Result == SHFL_FILE_NOT_FOUND)
839 vbsf_dentry_chain_increase_parent_ttl(dentry);
840
841 /*
842 * If we got a handle back, we're good. Create an inode for it and return.
843 */
844 if (pReq->Create.CreateParms.Handle != SHFL_HANDLE_NIL) {
845 struct inode *pNewInode = vbsf_create_inode(parent, dentry, path, &pReq->Create.CreateParms.Info, pSuperInfo,
846 !fDoLookup /*fInstantiate*/);
847 if (pNewInode) {
848 struct vbsf_inode_info *sf_new_i = VBSF_GET_INODE_INFO(pNewInode);
849 if (phHostFile) {
850 *phHostFile = pReq->Create.CreateParms.Handle;
851 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
852 } else if (fStashHandle) {
853 sf_new_i->handle = pReq->Create.CreateParms.Handle;
854 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
855 }
856 if (fDoLookup)
857 vbsf_d_add_inode(dentry, pNewInode);
858 path = NULL;
859 } else {
860 SFLOGFLOW(("%s vbsf_create_inode failed: -ENOMEM (path %s)\n", pszPrefix, rc, path->String.ach));
861 rc = -ENOMEM;
862 }
863 } else if (pReq->Create.CreateParms.Result == SHFL_FILE_EXISTS) {
864 /*
865 * For atomic_open (at least), we should create an inode and
866 * convert the dentry from a negative to a positive one.
867 */
868 SFLOGFLOW(("%s SHFL_FILE_EXISTS for %s\n", pszPrefix, sf_parent_i->path->String.ach));
869 if (fDoLookup) {
870 struct inode *pNewInode = vbsf_create_inode(parent, dentry, path, &pReq->Create.CreateParms.Info,
871 pSuperInfo, false /*fInstantiate*/);
872 if (pNewInode)
873 vbsf_d_add_inode(dentry, pNewInode);
874 path = NULL;
875 }
876 rc = -EEXIST;
877 } else if (pReq->Create.CreateParms.Result == SHFL_FILE_NOT_FOUND) {
878 SFLOGFLOW(("%s SHFL_FILE_NOT_FOUND for %s\n", pszPrefix, sf_parent_i->path->String.ach));
879 rc = -ENOENT;
880 } else if (pReq->Create.CreateParms.Result == SHFL_PATH_NOT_FOUND) {
881 SFLOGFLOW(("%s SHFL_PATH_NOT_FOUND for %s\n", pszPrefix, sf_parent_i->path->String.ach));
882 rc = -ENOENT;
883 } else {
884 AssertMsgFailed(("result=%d creating '%s'\n", pReq->Create.CreateParms.Result, sf_parent_i->path->String.ach));
885 rc = -EPERM;
886 }
887 } else {
888 int const vrc = rc;
889 rc = -RTErrConvertToErrno(vrc);
890 SFLOGFLOW(("%s SHFL_FN_CREATE(%s) failed vrc=%Rrc rc=%d\n", pszPrefix, path->String.ach, vrc, rc));
891 }
892
893 /* Cleanups. */
894 if (pReq->Create.CreateParms.Handle != SHFL_HANDLE_NIL) {
895 AssertCompile(RTASSERT_OFFSET_OF(VBOXSFCREATEREQ, CreateParms.Handle) > sizeof(VBOXSFCLOSEREQ)); /* no aliasing issues */
896 int rc2 = VbglR0SfHostReqClose(pSuperInfo->map.root, &pReq->Close, pReq->Create.CreateParms.Handle);
897 if (RT_FAILURE(rc2))
898 SFLOGFLOW(("%s VbglR0SfHostReqCloseSimple failed rc=%Rrc\n", pszPrefix, rc2));
899 }
900 VbglR0PhysHeapFree(pReq);
901 } else
902 rc = -ENOMEM;
903 if (path)
904 kfree(path);
905 }
906 return rc;
907}
908
909
910#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
911/**
912 * More atomic way of handling creation.
913 *
914 * Older kernels would first to a lookup that created the file, followed by
915 * an open call. We've got this horrid vbsf_inode_info::handle member because
916 * of that approach. The call combines the lookup and open.
917 */
918static int vbsf_inode_atomic_open(struct inode *pDirInode, struct dentry *dentry, struct file *file, unsigned fOpen,
919 umode_t fMode
920# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0)
921 , int *opened
922# endif
923 )
924{
925 SFLOGFLOW(("vbsf_inode_atomic_open: pDirInode=%p dentry=%p file=%p fOpen=%#x, fMode=%#x\n", pDirInode, dentry, file, fOpen, fMode));
926 int rc;
927
928 /* Code assumes negative dentry. */
929 Assert(dentry->d_inode == NULL);
930
931 /** @todo see if we can do this for non-create calls too, as it may save us a
932 * host call to revalidate the dentry. (Can't see anyone else doing
933 * this, so playing it safe for now.) */
934 if (fOpen & O_CREAT) {
935 /*
936 * Prepare our file info structure.
937 */
938 struct vbsf_reg_info *sf_r = kmalloc(sizeof(*sf_r), GFP_KERNEL);
939 if (sf_r) {
940 bool fCreated = false;
941 uint32_t fCreateFlags;
942
943 RTListInit(&sf_r->Handle.Entry);
944 sf_r->Handle.cRefs = 1;
945 sf_r->Handle.fFlags = !(fOpen & O_DIRECTORY)
946 ? VBSF_HANDLE_F_FILE | VBSF_HANDLE_F_MAGIC
947 : VBSF_HANDLE_F_DIR | VBSF_HANDLE_F_MAGIC;
948 sf_r->Handle.hHost = SHFL_HANDLE_NIL;
949
950 /*
951 * Try create it.
952 */
953 /* vbsf_create_worker uses the type from fMode, so match it up to O_DIRECTORY. */
954 AssertMsg(!(fMode & S_IFMT) || (fMode & S_IFMT) == (fOpen & O_DIRECTORY ? S_IFDIR : S_IFREG), ("0%o\n", fMode));
955 if (!(fOpen & O_DIRECTORY))
956 fMode = (fMode & ~S_IFMT) | S_IFREG;
957 else
958 fMode = (fMode & ~S_IFMT) | S_IFDIR;
959
960 fCreateFlags = vbsf_linux_oflags_to_vbox(fOpen, &sf_r->Handle.fFlags, __FUNCTION__);
961
962 rc = vbsf_create_worker(pDirInode, dentry, fMode, fCreateFlags, false /*fStashHandle*/, true /*fDoLookup*/,
963 &sf_r->Handle.hHost, &fCreated);
964 if (rc == 0) {
965 struct inode *inode = dentry->d_inode;
966 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
967
968 /*
969 * Set FMODE_CREATED according to the action taken by SHFL_CREATE
970 * and call finish_open() to do the remaining open() work.
971 */
972# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
973 if (fCreated)
974 file->f_mode |= FMODE_CREATED;
975 rc = finish_open(file, dentry, generic_file_open);
976# else
977 if (fCreated)
978 *opened |= FILE_CREATED;
979 rc = finish_open(file, dentry, generic_file_open, opened);
980# endif
981 if (rc == 0) {
982 /*
983 * Now that the file is fully opened, associate sf_r with it
984 * and link the handle to the inode.
985 */
986 vbsf_handle_append(sf_i, &sf_r->Handle);
987 file->private_data = sf_r;
988 SFLOGFLOW(("vbsf_inode_atomic_open: create succeeded; hHost=%#llx path='%s'\n",
989 rc, sf_r->Handle.hHost, sf_i->path->String.ach));
990 sf_r = NULL; /* don't free it */
991 } else {
992 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(pDirInode->i_sb);
993 SFLOGFLOW(("vbsf_inode_atomic_open: finish_open failed: %d (path='%s'\n", rc, sf_i->path->String.ach));
994 VbglR0SfHostReqCloseSimple(pSuperInfo->map.root, sf_r->Handle.hHost);
995 sf_r->Handle.hHost = SHFL_HANDLE_NIL;
996 }
997 } else
998 SFLOGFLOW(("vbsf_inode_atomic_open: vbsf_create_worker failed: %d\n", rc));
999 if (sf_r)
1000 kfree(sf_r);
1001 } else {
1002 LogRelMaxFunc(64, ("could not allocate reg info\n"));
1003 rc = -ENOMEM;
1004 }
1005 }
1006 /*
1007 * Not creating anything.
1008 * Do we need to do a lookup or should we just fail?
1009 */
1010 else if (d_in_lookup(dentry)) {
1011 struct dentry *pResult = vbsf_inode_lookup(pDirInode, dentry, 0 /*fFlags*/);
1012 if (!IS_ERR(pResult))
1013 rc = finish_no_open(file, pResult);
1014 else
1015 rc = PTR_ERR(pResult);
1016 SFLOGFLOW(("vbsf_inode_atomic_open: open -> %d (%p)\n", rc, pResult));
1017 } else {
1018 SFLOGFLOW(("vbsf_inode_atomic_open: open -> -ENOENT\n"));
1019 rc = -ENOENT;
1020 }
1021 return rc;
1022}
1023#endif /* 3.6.0 */
1024
1025
1026/**
1027 * Create a new regular file.
1028 *
1029 * @param parent inode of the directory
1030 * @param dentry directory cache entry
1031 * @param mode file mode
1032 * @param excl Possible O_EXCL...
1033 * @returns 0 on success, Linux error code otherwise
1034 */
1035#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) || defined(DOXYGEN_RUNNING)
1036static int vbsf_inode_create(struct inode *parent, struct dentry *dentry, umode_t mode, bool excl)
1037#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
1038static int vbsf_inode_create(struct inode *parent, struct dentry *dentry, umode_t mode, struct nameidata *nd)
1039#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
1040static int vbsf_inode_create(struct inode *parent, struct dentry *dentry, int mode, struct nameidata *nd)
1041#else
1042static int vbsf_inode_create(struct inode *parent, struct dentry *dentry, int mode)
1043#endif
1044{
1045 uint32_t fCreateFlags = SHFL_CF_ACT_CREATE_IF_NEW
1046 | SHFL_CF_ACT_FAIL_IF_EXISTS
1047 | SHFL_CF_ACCESS_READWRITE;
1048#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
1049 /* Clear the RD flag if write-only access requested. Otherwise assume we
1050 need write access to create stuff. */
1051 if (!(nd->intent.open.flags & 1) ) {
1052 fCreateFlags &= SHFL_CF_ACCESS_READWRITE;
1053 fCreateFlags |= SHFL_CF_ACCESS_WRITE;
1054 }
1055 /* (file since 2.6.15) */
1056#endif
1057 TRACE();
1058 AssertMsg(!(mode & S_IFMT) || (mode & S_IFMT) == S_IFREG, ("0%o\n", mode));
1059 return vbsf_create_worker(parent, dentry, (mode & ~S_IFMT) | S_IFREG, fCreateFlags,
1060 true /*fStashHandle*/, false /*fDoLookup*/, NULL /*phHandle*/, NULL /*fCreated*/);
1061}
1062
1063
1064/**
1065 * Create a new directory.
1066 *
1067 * @param parent inode of the directory
1068 * @param dentry directory cache entry
1069 * @param mode file mode
1070 * @returns 0 on success, Linux error code otherwise
1071 */
1072#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
1073static int vbsf_inode_mkdir(struct inode *parent, struct dentry *dentry, umode_t mode)
1074#else
1075static int vbsf_inode_mkdir(struct inode *parent, struct dentry *dentry, int mode)
1076#endif
1077{
1078 TRACE();
1079 AssertMsg(!(mode & S_IFMT) || (mode & S_IFMT) == S_IFDIR, ("0%o\n", mode));
1080 return vbsf_create_worker(parent, dentry, (mode & ~S_IFMT) | S_IFDIR,
1081 SHFL_CF_ACT_CREATE_IF_NEW
1082 | SHFL_CF_ACT_FAIL_IF_EXISTS
1083 | SHFL_CF_ACCESS_READWRITE
1084 | SHFL_CF_DIRECTORY,
1085 false /*fStashHandle*/, false /*fDoLookup*/, NULL /*phHandle*/, NULL /*fCreated*/);
1086}
1087
1088
1089/**
1090 * Remove a regular file / directory.
1091 *
1092 * @param parent inode of the directory
1093 * @param dentry directory cache entry
1094 * @param fDirectory true if directory, false otherwise
1095 * @returns 0 on success, Linux error code otherwise
1096 */
1097static int vbsf_unlink_worker(struct inode *parent, struct dentry *dentry, int fDirectory)
1098{
1099 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(parent->i_sb);
1100 struct vbsf_inode_info *sf_parent_i = VBSF_GET_INODE_INFO(parent);
1101 SHFLSTRING *path;
1102 int rc;
1103
1104 TRACE();
1105
1106 rc = vbsf_path_from_dentry(pSuperInfo, sf_parent_i, dentry, &path, __func__);
1107 if (!rc) {
1108 VBOXSFREMOVEREQ *pReq = (VBOXSFREMOVEREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF(VBOXSFREMOVEREQ, StrPath.String)
1109 + path->u16Size);
1110 if (pReq) {
1111 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
1112 uint32_t fFlags = fDirectory ? SHFL_REMOVE_DIR : SHFL_REMOVE_FILE;
1113 if (dentry->d_inode && ((dentry->d_inode->i_mode & S_IFLNK) == S_IFLNK))
1114 fFlags |= SHFL_REMOVE_SYMLINK;
1115
1116 rc = VbglR0SfHostReqRemove(pSuperInfo->map.root, pReq, fFlags);
1117
1118 if (dentry->d_inode) {
1119 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(dentry->d_inode);
1120 sf_i->force_restat = true;
1121 }
1122
1123 if (RT_SUCCESS(rc)) {
1124 sf_parent_i->force_restat = true; /* directory access/change time changed */
1125 rc = 0;
1126 } else if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND) {
1127 /* Probably deleted on the host while the guest had it cached, so don't complain: */
1128 LogFunc(("(%d): VbglR0SfRemove(%s) failed rc=%Rrc; calling d_drop on %p\n",
1129 fDirectory, path->String.ach, rc, dentry));
1130 sf_parent_i->force_restat = true;
1131 d_drop(dentry);
1132 rc = 0;
1133 } else {
1134 LogFunc(("(%d): VbglR0SfRemove(%s) failed rc=%Rrc\n", fDirectory, path->String.ach, rc));
1135 rc = -RTErrConvertToErrno(rc);
1136 }
1137 VbglR0PhysHeapFree(pReq);
1138 } else
1139 rc = -ENOMEM;
1140 kfree(path);
1141 }
1142 return rc;
1143}
1144
1145
1146/**
1147 * Remove a regular file.
1148 *
1149 * @param parent inode of the directory
1150 * @param dentry directory cache entry
1151 * @returns 0 on success, Linux error code otherwise
1152 */
1153static int vbsf_inode_unlink(struct inode *parent, struct dentry *dentry)
1154{
1155 TRACE();
1156 return vbsf_unlink_worker(parent, dentry, false /*fDirectory*/);
1157}
1158
1159
1160/**
1161 * Remove a directory.
1162 *
1163 * @param parent inode of the directory
1164 * @param dentry directory cache entry
1165 * @returns 0 on success, Linux error code otherwise
1166 */
1167static int vbsf_inode_rmdir(struct inode *parent, struct dentry *dentry)
1168{
1169 TRACE();
1170 return vbsf_unlink_worker(parent, dentry, true /*fDirectory*/);
1171}
1172
1173
1174/**
1175 * Rename a regular file / directory.
1176 *
1177 * @param old_parent inode of the old parent directory
1178 * @param old_dentry old directory cache entry
1179 * @param new_parent inode of the new parent directory
1180 * @param new_dentry new directory cache entry
1181 * @param flags flags
1182 * @returns 0 on success, Linux error code otherwise
1183 */
1184static int vbsf_inode_rename(struct inode *old_parent, struct dentry *old_dentry,
1185 struct inode *new_parent, struct dentry *new_dentry, unsigned flags)
1186{
1187 /*
1188 * Deal with flags.
1189 */
1190 int rc;
1191 uint32_t fRename = (old_dentry->d_inode->i_mode & S_IFDIR ? SHFL_RENAME_DIR : SHFL_RENAME_FILE)
1192 | SHFL_RENAME_REPLACE_IF_EXISTS;
1193#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
1194 if (!(flags & ~RENAME_NOREPLACE)) {
1195 if (flags & RENAME_NOREPLACE)
1196 fRename &= ~SHFL_RENAME_REPLACE_IF_EXISTS;
1197#endif
1198 /*
1199 * Check that they are on the same mount.
1200 */
1201 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(old_parent->i_sb);
1202 if (pSuperInfo == VBSF_GET_SUPER_INFO(new_parent->i_sb)) {
1203 /*
1204 * Build the new path.
1205 */
1206 struct vbsf_inode_info *sf_new_parent_i = VBSF_GET_INODE_INFO(new_parent);
1207 PSHFLSTRING pNewPath;
1208 rc = vbsf_path_from_dentry(pSuperInfo, sf_new_parent_i, new_dentry, &pNewPath, __func__);
1209 if (rc == 0) {
1210 /*
1211 * Create and issue the rename request.
1212 */
1213 VBOXSFRENAMEWITHSRCBUFREQ *pReq;
1214 pReq = (VBOXSFRENAMEWITHSRCBUFREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF(VBOXSFRENAMEWITHSRCBUFREQ, StrDstPath.String)
1215 + pNewPath->u16Size);
1216 if (pReq) {
1217 struct vbsf_inode_info *sf_file_i = VBSF_GET_INODE_INFO(old_dentry->d_inode);
1218 PSHFLSTRING pOldPath = sf_file_i->path;
1219
1220 memcpy(&pReq->StrDstPath, pNewPath, SHFLSTRING_HEADER_SIZE + pNewPath->u16Size);
1221 rc = VbglR0SfHostReqRenameWithSrcContig(pSuperInfo->map.root, pReq, pOldPath, virt_to_phys(pOldPath), fRename);
1222 VbglR0PhysHeapFree(pReq);
1223 if (RT_SUCCESS(rc)) {
1224 /*
1225 * On success we replace the path in the inode and trigger
1226 * restatting of both parent directories.
1227 */
1228 struct vbsf_inode_info *sf_old_parent_i = VBSF_GET_INODE_INFO(old_parent);
1229 SFLOGFLOW(("vbsf_inode_rename: %s -> %s (%#x)\n", pOldPath->String.ach, pNewPath->String.ach, fRename));
1230
1231 sf_file_i->path = pNewPath;
1232 kfree(pOldPath);
1233 pNewPath = NULL;
1234
1235 sf_new_parent_i->force_restat = 1;
1236 sf_old_parent_i->force_restat = 1;
1237
1238 vbsf_dentry_chain_increase_parent_ttl(old_dentry);
1239 vbsf_dentry_chain_increase_parent_ttl(new_dentry);
1240
1241 rc = 0;
1242 } else {
1243 SFLOGFLOW(("vbsf_inode_rename: VbglR0SfHostReqRenameWithSrcContig(%s,%s,%#x) failed -> %d\n",
1244 pOldPath->String.ach, pNewPath->String.ach, fRename, rc));
1245 if (rc == VERR_IS_A_DIRECTORY || rc == VERR_IS_A_FILE)
1246 vbsf_dentry_invalidate_ttl(old_dentry);
1247 rc = -RTErrConvertToErrno(rc);
1248 }
1249 } else {
1250 SFLOGFLOW(("vbsf_inode_rename: failed to allocate request (%#x bytes)\n",
1251 RT_UOFFSETOF(VBOXSFRENAMEWITHSRCBUFREQ, StrDstPath.String) + pNewPath->u16Size));
1252 rc = -ENOMEM;
1253 }
1254 if (pNewPath)
1255 kfree(pNewPath);
1256 } else
1257 SFLOGFLOW(("vbsf_inode_rename: vbsf_path_from_dentry failed: %d\n", rc));
1258 } else {
1259 SFLOGFLOW(("vbsf_inode_rename: rename with different roots (%#x vs %#x)\n",
1260 pSuperInfo->map.root, VBSF_GET_SUPER_INFO(new_parent->i_sb)->map.root));
1261 rc = -EXDEV;
1262 }
1263#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
1264 } else {
1265 SFLOGFLOW(("vbsf_inode_rename: Unsupported flags: %#x\n", flags));
1266 rc = -EINVAL;
1267 }
1268#else
1269 RT_NOREF(flags);
1270#endif
1271 return rc;
1272}
1273
1274
1275#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
1276/**
1277 * The traditional rename interface without any flags.
1278 */
1279static int vbsf_inode_rename_no_flags(struct inode *old_parent, struct dentry *old_dentry,
1280 struct inode *new_parent, struct dentry *new_dentry)
1281{
1282 return vbsf_inode_rename(old_parent, old_dentry, new_parent, new_dentry, 0);
1283}
1284#endif
1285
1286
1287/**
1288 * Create a symbolic link.
1289 */
1290static int vbsf_inode_symlink(struct inode *parent, struct dentry *dentry, const char *target)
1291{
1292 /*
1293 * Turn the target into a string (contiguous physcial memory).
1294 */
1295 /** @todo we can save a kmalloc here if we switch to embedding the target rather
1296 * than the symlink path into the request. Will require more NLS helpers. */
1297 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(parent->i_sb);
1298 PSHFLSTRING pTarget = NULL;
1299 int rc = vbsf_nls_to_shflstring(pSuperInfo, target, &pTarget);
1300 if (rc == 0) {
1301 /*
1302 * Create a full path for the symlink name.
1303 */
1304 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(parent);
1305 PSHFLSTRING pPath = NULL;
1306 rc = vbsf_path_from_dentry(pSuperInfo, sf_i, dentry, &pPath, __func__);
1307 if (rc == 0) {
1308 /*
1309 * Create the request and issue it.
1310 */
1311 uint32_t const cbReq = RT_UOFFSETOF(VBOXSFCREATESYMLINKREQ, StrSymlinkPath.String) + pPath->u16Size;
1312 VBOXSFCREATESYMLINKREQ *pReq = (VBOXSFCREATESYMLINKREQ *)VbglR0PhysHeapAlloc(cbReq);
1313 if (pReq) {
1314 RT_ZERO(*pReq);
1315 memcpy(&pReq->StrSymlinkPath, pPath, SHFLSTRING_HEADER_SIZE + pPath->u16Size);
1316
1317 rc = VbglR0SfHostReqCreateSymlinkContig(pSuperInfo->map.root, pTarget, virt_to_phys(pTarget), pReq);
1318 if (RT_SUCCESS(rc)) {
1319 sf_i->force_restat = 1;
1320
1321 /*
1322 * Instantiate a new inode for the symlink.
1323 */
1324 rc = vbsf_inode_instantiate(parent, dentry, pPath, &pReq->ObjInfo, SHFL_HANDLE_NIL);
1325 if (rc == 0) {
1326 SFLOGFLOW(("vbsf_inode_symlink: Successfully created '%s' -> '%s'\n", pPath->String.ach, pTarget->String.ach));
1327 pPath = NULL; /* consumed by inode */
1328 vbsf_dentry_chain_increase_ttl(dentry);
1329 } else {
1330 SFLOGFLOW(("vbsf_inode_symlink: Failed to create inode for '%s': %d\n", pPath->String.ach, rc));
1331 vbsf_dentry_chain_increase_parent_ttl(dentry);
1332 vbsf_dentry_invalidate_ttl(dentry);
1333 }
1334 } else {
1335 int const vrc = rc;
1336 if (vrc == VERR_WRITE_PROTECT)
1337 rc = -EPERM; /* EPERM: Symlink creation not supported according to the linux manpage as of 2017-09-15.
1338 "VBoxInternal2/SharedFoldersEnableSymlinksCreate/<share>" is not 1. */
1339 else
1340 rc = -RTErrConvertToErrno(vrc);
1341 SFLOGFLOW(("vbsf_inode_symlink: VbglR0SfHostReqCreateSymlinkContig failed for '%s' -> '%s': %Rrc (-> %d)\n",
1342 pPath->String.ach, pTarget->String.ach, vrc, rc));
1343 }
1344 VbglR0PhysHeapFree(pReq);
1345 } else {
1346 SFLOGFLOW(("vbsf_inode_symlink: failed to allocate %u phys heap for the request!\n", cbReq));
1347 rc = -ENOMEM;
1348 }
1349 if (pPath)
1350 kfree(pPath);
1351 }
1352 kfree(pTarget);
1353 }
1354 return rc;
1355}
1356
1357
1358/**
1359 * Directory inode operations.
1360 */
1361struct inode_operations vbsf_dir_iops = {
1362 .lookup = vbsf_inode_lookup,
1363#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
1364 .atomic_open = vbsf_inode_atomic_open,
1365#endif
1366 .create = vbsf_inode_create,
1367 .symlink = vbsf_inode_symlink,
1368 .mkdir = vbsf_inode_mkdir,
1369 .rmdir = vbsf_inode_rmdir,
1370 .unlink = vbsf_inode_unlink,
1371#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
1372 .rename = vbsf_inode_rename,
1373#else
1374 .rename = vbsf_inode_rename_no_flags,
1375# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
1376 .rename2 = vbsf_inode_rename,
1377# endif
1378#endif
1379#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 18)
1380 .getattr = vbsf_inode_getattr,
1381#else
1382 .revalidate = vbsf_inode_revalidate,
1383#endif
1384 .setattr = vbsf_inode_setattr,
1385};
1386
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