This file is indexed.

/usr/src/open-vm-tools-10.0.7/vmhgfs/file.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
/*********************************************************
 * Copyright (C) 2006-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 *********************************************************/

/*
 * file.c --
 *
 * File operations for the filesystem portion of the vmhgfs driver.
 */

/* Must come before any kernel header file. */
#include "driver-config.h"

#include <linux/errno.h>
#include <linux/module.h>
#include <linux/signal.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
#include <linux/uio.h> /* iov_iter_count */
#endif
#include "compat_cred.h"
#include "compat_fs.h"
#include "compat_kernel.h"
#include "compat_slab.h"

/* Must be after compat_fs.h */
#if defined VMW_USE_AIO
#include <linux/aio.h>
#endif

#include "cpName.h"
#include "hgfsProto.h"
#include "module.h"
#include "request.h"
#include "hgfsUtil.h"
#include "fsutil.h"
#include "vm_assert.h"
#include "vm_basic_types.h"

/*
 * Before Linux 2.6.33 only O_DSYNC semantics were implemented, but using
 * the O_SYNC flag.  We continue to use the existing numerical value
 * for O_DSYNC semantics now, but using the correct symbolic name for it.
 * This new value is used to request true Posix O_SYNC semantics.  It is
 * defined in this strange way to make sure applications compiled against
 * new headers get at least O_DSYNC semantics on older kernels.
 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
#define HGFS_FILECTL_SYNC(flags)            ((flags) & O_DSYNC)
#else
#define HGFS_FILECTL_SYNC(flags)            ((flags) & O_SYNC)
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
typedef struct iov_iter *hgfs_iov;
#define HGFS_IOV_TO_COUNT(iov, nr_segs)                   (iov_iter_count(iov))
#define HGFS_IOV_TO_SEGS(iov, nr_segs)                    (0)
#define HGFS_IOCB_TO_POS(iocb, pos)                       (iocb->ki_pos)
#else
typedef const struct iovec *hgfs_iov;
#define HGFS_IOV_TO_COUNT(iov, nr_segs)                   (iov_length(iov, nr_segs))
#define HGFS_IOV_TO_SEGS(iov, nr_segs)                    (nr_segs)
#define HGFS_IOCB_TO_POS(iocb, pos)                       (pos)
#endif

/* Private functions. */
static int HgfsPackOpenRequest(struct inode *inode,
                               struct file *file,
                               HgfsOp opUsed,
                               HgfsReq *req);
static int HgfsUnpackOpenReply(HgfsReq *req,
                               HgfsOp opUsed,
                               HgfsHandle *file,
                               HgfsLockType *lock);

/* HGFS file operations for files. */
static int HgfsOpen(struct inode *inode,
                    struct file *file);
#if defined VMW_USE_AIO
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static ssize_t HgfsFileRead(struct kiocb *iocb,
                            struct iov_iter *to);
static ssize_t HgfsFileWrite(struct kiocb *iocb,
                            struct iov_iter *from);
#else // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static ssize_t HgfsFileRead(struct kiocb *iocb,
                            const struct iovec *iov,
                            unsigned long numSegs,
                            loff_t offset);
static ssize_t HgfsFileWrite(struct kiocb *iocb,
                             const struct iovec *iov,
                             unsigned long numSegs,
                             loff_t offset);
#endif // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
#else
static ssize_t HgfsRead(struct file *file,
                        char __user *buf,
                        size_t count,
                        loff_t *offset);
static ssize_t HgfsWrite(struct file *file,
                         const char __user *buf,
                         size_t count,
                         loff_t *offset);
#endif

static loff_t HgfsSeek(struct file *file,
                       loff_t  offset,
                       int origin);
static int HgfsFlush(struct file *file
#if !defined VMW_FLUSH_HAS_1_ARG
                     ,fl_owner_t id
#endif
                     );

#if !defined VMW_FSYNC_31
static int HgfsDoFsync(struct inode *inode);
#endif

static int HgfsFsync(struct file *file,
#if defined VMW_FSYNC_OLD
                     struct dentry *dentry,
#elif defined VMW_FSYNC_31
                     loff_t start,
                     loff_t end,
#endif
                     int datasync);
static int HgfsMmap(struct file *file,
                    struct vm_area_struct *vma);
static int HgfsRelease(struct inode *inode,
                       struct file *file);

#ifndef VMW_SENDFILE_NONE
#if defined VMW_SENDFILE_OLD
static ssize_t HgfsSendfile(struct file *file,
                            loff_t *offset,
                            size_t count,
                            read_actor_t actor,
                            void __user *target);
#else /* defined VMW_SENDFILE_NEW */
static ssize_t HgfsSendfile(struct file *file,
                            loff_t *offset,
                            size_t count,
                            read_actor_t actor,
                            void *target);
#endif
#endif
#ifdef VMW_SPLICE_READ
static ssize_t HgfsSpliceRead(struct file *file,
                              loff_t *offset,
                              struct pipe_inode_info *pipe,
                              size_t len,
                              unsigned int flags);
#endif

/* HGFS file operations structure for files. */
struct file_operations HgfsFileFileOperations = {
   .owner      = THIS_MODULE,
   .open       = HgfsOpen,
   .llseek     = HgfsSeek,
   .flush      = HgfsFlush,
#if defined VMW_USE_AIO
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
   /* Fallback to async counterpart, check kernel source read_write.c */
   .read       = NULL,
   .write      = NULL,
   .read_iter  = HgfsFileRead,
   .write_iter = HgfsFileWrite,
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
   .read       = new_sync_read,
   .write      = new_sync_write,
   .read_iter  = HgfsFileRead,
   .write_iter = HgfsFileWrite,
#else // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
   .read       = do_sync_read,
   .write      = do_sync_write,
   .aio_read   = HgfsFileRead,
   .aio_write  = HgfsFileWrite,
#endif // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
#else
   .read       = HgfsRead,
   .write      = HgfsWrite,
#endif
   .fsync      = HgfsFsync,
   .mmap       = HgfsMmap,
   .release    = HgfsRelease,
#ifndef VMW_SENDFILE_NONE
   .sendfile   = HgfsSendfile,
#endif
#ifdef VMW_SPLICE_READ
   .splice_read = HgfsSpliceRead,
#endif
};

/* File open mask. */
#define HGFS_FILE_OPEN_MASK (HGFS_OPEN_VALID_MODE | \
                             HGFS_OPEN_VALID_FLAGS | \
                             HGFS_OPEN_VALID_SPECIAL_PERMS | \
			     HGFS_OPEN_VALID_OWNER_PERMS | \
			     HGFS_OPEN_VALID_GROUP_PERMS | \
			     HGFS_OPEN_VALID_OTHER_PERMS | \
			     HGFS_OPEN_VALID_FILE_NAME | \
			     HGFS_OPEN_VALID_SERVER_LOCK)


/*
 * Private functions.
 */

/*
 *----------------------------------------------------------------------
 *
 * HgfsPackOpenRequest --
 *
 *    Setup the Open request, depending on the op version.
 *
 * Results:
 *    Returns zero on success, or negative error on failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static int
HgfsPackOpenRequest(struct inode *inode, // IN: Inode of the file to open
                    struct file *file,   // IN: File pointer for this open
                    HgfsOp opUsed,       // IN: Op to use
                    HgfsReq *req)        // IN/OUT: Packet to write into
{
   char *name;
   uint32 *nameLength;
   size_t requestSize;
   int result;

   ASSERT(inode);
   ASSERT(file);
   ASSERT(req);

   switch (opUsed) {
    case HGFS_OP_OPEN_V3: {
      HgfsRequest *requestHeader;
      HgfsRequestOpenV3 *requestV3;

      requestHeader = (HgfsRequest *)HGFS_REQ_PAYLOAD(req);
      requestHeader->op = opUsed;
      requestHeader->id = req->id;

      requestV3 = (HgfsRequestOpenV3 *)HGFS_REQ_PAYLOAD_V3(req);
      requestSize = HGFS_REQ_PAYLOAD_SIZE_V3(requestV3);

      /* We'll use these later. */
      name = requestV3->fileName.name;
      nameLength = &requestV3->fileName.length;

      requestV3->mask = HGFS_FILE_OPEN_MASK;

      /* Linux clients need case-sensitive lookups. */
      requestV3->fileName.flags = 0;
      requestV3->fileName.caseType = HGFS_FILE_NAME_CASE_SENSITIVE;
      requestV3->fileName.fid = HGFS_INVALID_HANDLE;

      /* Set mode. */
      result = HgfsGetOpenMode(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open mode\n"));
         return -EINVAL;
      }
      requestV3->mode = result;

      /* Set flags. */
      result = HgfsGetOpenFlags(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open flags\n"));
         return -EINVAL;
      }
      requestV3->flags = result;

      LOG(4, (KERN_DEBUG "VMware hgfs: %s: mode file %o inode %o -> user %o\n",
              __func__, file->f_mode, inode->i_mode, (inode->i_mode & S_IRWXU) >> 6));
      /* Set permissions. */
      requestV3->specialPerms = (inode->i_mode & (S_ISUID | S_ISGID | S_ISVTX))
                                >> 9;
      requestV3->ownerPerms = (inode->i_mode & S_IRWXU) >> 6;
      requestV3->groupPerms = (inode->i_mode & S_IRWXG) >> 3;
      requestV3->otherPerms = (inode->i_mode & S_IRWXO);

      /* XXX: Request no lock for now. */
      requestV3->desiredLock = HGFS_LOCK_NONE;

      requestV3->reserved1 = 0;
      requestV3->reserved2 = 0;
      break;
   }

   case HGFS_OP_OPEN_V2: {
      HgfsRequestOpenV2 *requestV2;

      requestV2 = (HgfsRequestOpenV2 *)(HGFS_REQ_PAYLOAD(req));
      requestV2->header.op = opUsed;
      requestV2->header.id = req->id;

      /* We'll use these later. */
      name = requestV2->fileName.name;
      nameLength = &requestV2->fileName.length;
      requestSize = sizeof *requestV2;

      requestV2->mask = HGFS_FILE_OPEN_MASK;

      /* Set mode. */
      result = HgfsGetOpenMode(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open mode\n"));
         return -EINVAL;
      }
      requestV2->mode = result;

      /* Set flags. */
      result = HgfsGetOpenFlags(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open flags\n"));
         return -EINVAL;
      }
      requestV2->flags = result;

      /* Set permissions. */
      requestV2->specialPerms = (inode->i_mode & (S_ISUID | S_ISGID | S_ISVTX))
                                >> 9;
      requestV2->ownerPerms = (inode->i_mode & S_IRWXU) >> 6;
      requestV2->groupPerms = (inode->i_mode & S_IRWXG) >> 3;
      requestV2->otherPerms = (inode->i_mode & S_IRWXO);

      /* XXX: Request no lock for now. */
      requestV2->desiredLock = HGFS_LOCK_NONE;
      break;
   }
   case HGFS_OP_OPEN: {
      HgfsRequestOpen *request;

      request = (HgfsRequestOpen *)(HGFS_REQ_PAYLOAD(req));
      request->header.op = opUsed;
      request->header.id = req->id;

      /* We'll use these later. */
      name = request->fileName.name;
      nameLength = &request->fileName.length;
      requestSize = sizeof *request;

      /* Set mode. */
      result = HgfsGetOpenMode(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open mode\n"));
         return -EINVAL;
      }
      request->mode = result;

      /* Set flags. */
      result = HgfsGetOpenFlags(file->f_flags);
      if (result < 0) {
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: failed to get "
                 "open flags\n"));
         return -EINVAL;
      }
      request->flags = result;

      /* Set permissions. */
      request->permissions = (inode->i_mode & S_IRWXU) >> 6;
      break;
   }
   default:
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: unexpected "
              "OP type encountered\n"));
      return -EPROTO;
   }

   /* Build full name to send to server. */
   if (HgfsBuildPath(name,
                     req->bufferSize - (requestSize - 1),
                     file->f_dentry) < 0) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: build path "
              "failed\n"));
      return -EINVAL;
   }
   LOG(6, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: opening \"%s\", "
           "flags %o, create perms %o\n", name,
           file->f_flags, file->f_mode));

   /* Convert to CP name. */
   result = CPName_ConvertTo(name,
                             req->bufferSize - (requestSize - 1),
                             name);
   if (result < 0) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsPackOpenRequest: CP conversion "
              "failed\n"));
      return -EINVAL;
   }

   *nameLength = (uint32) result;
   req->payloadSize = requestSize + result;

   return 0;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsUnpackOpenReply --
 *
 *    Get interesting fields out of the Open reply, depending on the op
 *    version.
 *
 * Results:
 *    Returns zero on success, or negative error on failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static int
HgfsUnpackOpenReply(HgfsReq *req,          // IN: Packet with reply inside
                    HgfsOp opUsed,         // IN: What request op did we send
                    HgfsHandle *file,      // OUT: Handle in reply packet
                    HgfsLockType *lock)    // OUT: The server lock we got
{
   HgfsReplyOpenV3 *replyV3;
   HgfsReplyOpenV2 *replyV2;
   HgfsReplyOpen *replyV1;
   size_t replySize;

   ASSERT(req);
   ASSERT(file);
   ASSERT(lock);

   switch (opUsed) {
   case HGFS_OP_OPEN_V3:
      replyV3 = (HgfsReplyOpenV3 *)HGFS_REP_PAYLOAD_V3(req);
      replySize = HGFS_REP_PAYLOAD_SIZE_V3(replyV3);
      *file = replyV3->file;
      *lock = replyV3->acquiredLock;
      break;
   case HGFS_OP_OPEN_V2:
      replyV2 = (HgfsReplyOpenV2 *)(HGFS_REQ_PAYLOAD(req));
      replySize = sizeof *replyV2;
      *file = replyV2->file;
      *lock = replyV2->acquiredLock;
      break;
   case HGFS_OP_OPEN:
      replyV1 = (HgfsReplyOpen *)(HGFS_REQ_PAYLOAD(req));
      replySize = sizeof *replyV1;
      *file = replyV1->file;
      *lock = HGFS_LOCK_NONE;
      break;
   default:

      /* This really shouldn't happen since we set opUsed ourselves. */
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsUnpackOpenReply: unexpected "
              "OP type encountered\n"));
      ASSERT(FALSE);
      return -EPROTO;
   }

   if (req->payloadSize != replySize) {
      /*
       * The reply to Open is a fixed size. So the size of the payload
       * really ought to match the expected size of an HgfsReplyOpen[V2].
       */
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsUnpackOpenReply: wrong packet "
              "size\n"));
      return -EPROTO;
   }
   return 0;
}


/*
 * HGFS file operations for files.
 */

/*
 *----------------------------------------------------------------------
 *
 * HgfsOpen --
 *
 *    Called whenever a process opens a file in our filesystem.
 *
 *    We send an "Open" request to the server with the name stored in
 *    this file's inode. If the Open succeeds, we store the filehandle
 *    sent by the server in the file struct so it can be accessed by
 *    read/write/close.
 *
 * Results:
 *    Returns zero if on success, error on failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static int
HgfsOpen(struct inode *inode,  // IN: Inode of the file to open
         struct file *file)    // IN: File pointer for this open
{
   HgfsReq *req;
   HgfsOp opUsed;
   HgfsStatus replyStatus;
   HgfsHandle replyFile;
   HgfsLockType replyLock;
   HgfsInodeInfo *iinfo;
   int result = 0;

   ASSERT(inode);
   ASSERT(inode->i_sb);
   ASSERT(file);
   ASSERT(file->f_dentry);
   ASSERT(file->f_dentry->d_inode);

   iinfo = INODE_GET_II_P(inode);

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s)\n",
           __func__, file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name));

   req = HgfsGetNewRequest();
   if (!req) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: out of memory while "
              "getting new request\n"));
      result = -ENOMEM;
      goto out;
   }

  retry:
   /*
    * Set up pointers using the proper struct This lets us check the
    * version exactly once and use the pointers later.
    */

   opUsed = hgfsVersionOpen;
   result = HgfsPackOpenRequest(inode, file, opUsed, req);
   if (result != 0) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: error packing request\n"));
      goto out;
   }

   /* Send the request and process the reply. */
   result = HgfsSendRequest(req);
   if (result == 0) {
      /* Get the reply and check return status. */
      replyStatus = HgfsReplyStatus(req);
      result = HgfsStatusConvertToLinux(replyStatus);

      switch (result) {
      case 0:
         iinfo->createdAndUnopened = FALSE;
         LOG(10, (KERN_DEBUG "VMware hgfs: HgfsOpen: old hostFileId = "
                  "%"FMT64"u\n", iinfo->hostFileId));
         /*
          * Invalidate the hostFileId as we need to retrieve it from
          * the server.
          */
         iinfo->hostFileId = 0;
         result = HgfsUnpackOpenReply(req, opUsed, &replyFile, &replyLock);
         if (result != 0) {
            break;
         }
         result = HgfsCreateFileInfo(file, replyFile);
         if (result != 0) {
            break;
         }
         LOG(6, (KERN_DEBUG "VMware hgfs: HgfsOpen: set handle to %u\n",
                 replyFile));

         /*
          * HgfsCreate faked all of the inode's attributes, so by the time
          * we're done in HgfsOpen, we need to make sure that the attributes
          * in the inode are real. The following is only necessary when
          * O_CREAT is set, otherwise we got here after HgfsLookup (which sent
          * a getattr to the server and got the real attributes).
          *
          * In particular, we'd like to at least try and set the inode's
          * uid/gid to match the caller's. We don't expect this to work,
          * because Windows servers will ignore it, and Linux servers running
          * as non-root won't be able to change it, but we're forward thinking
          * people.
          *
          * Either way, we force a revalidate following the setattr so that
          * we'll get the actual uid/gid from the server.
          */
         if (file->f_flags & O_CREAT) {
            struct dentry *dparent;
            struct inode *iparent;

            /*
             * This is not the root of our file system so there should always
             * be a parent.
             */
            ASSERT(file->f_dentry->d_parent);

            /*
             * Here we obtain a reference on the parent to make sure it doesn't
             * go away.  This might not be necessary, since the existence of
             * a child (which we hold a reference to in this call) should
             * account for a reference in the parent, but it's safe to do so.
             * Overly cautious and safe is better than risky and broken.
             *
             * XXX Note that this and a handful of other hacks wouldn't be
             * necessary if we actually created the file in our create
             * implementation (where references and locks are properly held).
             * We could do this if we were willing to give up support for
             * O_EXCL on 2.4 kernels.
             */
            dparent = dget(file->f_dentry->d_parent);
            iparent = dparent->d_inode;

            HgfsSetUidGid(iparent, file->f_dentry,
                          current_fsuid(), current_fsgid());

            dput(dparent);
         }
         break;

      case -EPROTO:
         /* Retry with older version(s). Set globally. */
         if (opUsed == HGFS_OP_OPEN_V3) {
            LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: Version 3 not "
                    "supported. Falling back to version 2.\n"));
            hgfsVersionOpen = HGFS_OP_OPEN_V2;
            goto retry;
         }

         /* Retry with Version 1 of Open. Set globally. */
         if (opUsed == HGFS_OP_OPEN_V2) {
            LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: Version 2 not "
                    "supported. Falling back to version 1.\n"));
            hgfsVersionOpen = HGFS_OP_OPEN;
            goto retry;
         }

         /* Fallthrough. */
      default:
         break;
      }
   } else if (result == -EIO) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: timed out\n"));
   } else if (result == -EPROTO) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: server "
              "returned error: %d\n", result));
   } else {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsOpen: unknown error: "
              "%d\n", result));
   }
out:
   HgfsFreeRequest(req);

   /*
    * If the open failed (for any reason) and we tried to open a newly created
    * file, we must ensure that the next operation on this inode triggers a
    * revalidate to the server. This is because the file wasn't created on the
    * server, yet we currently believe that it was, because we created a fake
    * inode with a hashed dentry for it in HgfsCreate. We will continue to
    * believe this until the dentry's ttl expires, which will cause a
    * revalidate to the server that will reveal the truth. So in order to find
    * the truth as soon as possible, we'll reset the dentry's last revalidate
    * time now to force a revalidate the next time someone uses the dentry.
    *
    * We're using our own flag to track this case because using O_CREAT isn't
    * good enough: HgfsOpen will be called with O_CREAT even if the file exists
    * on the server, and if that's the case, there's no need to revalidate.
    *
    * XXX: Note that this will need to be reworked if/when we support hard
    * links, because multiple dentries will point to the same inode, and
    * forcing a revalidate on one will not force it on any others.
    */
   if (result != 0 && iinfo->createdAndUnopened == TRUE) {
      HgfsDentryAgeForce(file->f_dentry);
   }
   return result;
}


#if defined VMW_USE_AIO
/*
 *----------------------------------------------------------------------
 *
 * HgfsGenericFileRead --
 *
 *    Called when the kernel initiates an asynchronous read from a file in
 *    our filesystem. Our function is just a thin wrapper around
 *    system generic read function.
 *
 *
 * Results:
 *    Returns the number of bytes read on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static ssize_t
HgfsGenericFileRead(struct kiocb *iocb,          // IN: I/O control block
                    hgfs_iov iov,                // IN: Array of I/O vectors
                    unsigned long iovSegs,       // IN: Count of I/O vectors
                    loff_t pos)                  // IN: Position at which to read
{
   ssize_t result;

   LOG(8, (KERN_DEBUG "VMware hgfs: %s(%lu@%Ld)\n",
           __func__, (unsigned long)HGFS_IOV_TO_COUNT(iov, iovSegs),
           (long long) pos));

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
   result = generic_file_read_iter(iocb, iov);
#else
   result = generic_file_aio_read(iocb, iov, iovSegs, pos);
#endif

   LOG(8, (KERN_DEBUG "VMware hgfs: %s return %"FMTSZ"d\n",
           __func__, result));
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsFileRead --
 *
 *    Called when the kernel initiates an asynchronous read to a file in
 *    our filesystem. Our function is just a thin wrapper around
 *    generic_file_aio_read() that tries to validate the dentry first.
 *
 * Results:
 *    Returns the number of bytes read on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static ssize_t
HgfsFileRead(struct kiocb *iocb,      // IN:  I/O control block
             struct iov_iter *iov)    // OUT: Array of I/O buffers
#else // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static ssize_t
HgfsFileRead(struct kiocb *iocb,      // IN:  I/O control block
             const struct iovec *iov, // OUT: Array of I/O buffers
             unsigned long numSegs,   // IN:  Number of buffers
             loff_t offset)           // IN:  Offset at which to read
#endif // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
{
   ssize_t result;
   struct dentry *readDentry;
   loff_t pos;
   unsigned long iovSegs;

   ASSERT(iocb);
   ASSERT(iocb->ki_filp);
   ASSERT(iocb->ki_filp->f_dentry);
   ASSERT(iov);

   pos = HGFS_IOCB_TO_POS(iocb, offset);
   iovSegs = HGFS_IOV_TO_SEGS(iov, numSegs);

   readDentry = iocb->ki_filp->f_dentry;

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s)\n",
           __func__, readDentry->d_parent->d_name.name,
           readDentry->d_name.name));

   result = HgfsRevalidate(readDentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: %s: invalid dentry\n", __func__));
      goto out;
   }

   result = HgfsGenericFileRead(iocb, iov, iovSegs, pos);

out:
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsGenericFileWrite --
 *
 *    Called when the kernel initiates an asynchronous write to a file in
 *    our filesystem. Our function is just a thin wrapper around
 *    system generic write function.
 *
 *
 * Results:
 *    Returns the number of bytes written on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static ssize_t
HgfsGenericFileWrite(struct kiocb *iocb,          // IN: I/O control block
                     hgfs_iov iov,                // IN: Array of I/O vectors
                     unsigned long iovSegs,       // IN: Count of I/O vectors
                     loff_t pos)                  // IN: Position at which to write
{
   ssize_t result;

   LOG(8, (KERN_DEBUG "VMware hgfs: %s(%lu@%Ld)\n",
           __func__, (unsigned long)HGFS_IOV_TO_COUNT(iov, iovSegs),
           (long long) pos));

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
   result = generic_file_write_iter(iocb, iov);
#else
   result = generic_file_aio_write(iocb, iov, iovSegs, pos);
#endif

   LOG(8, (KERN_DEBUG "VMware hgfs: %s return %"FMTSZ"d\n",
           __func__, result));
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsFileWrite --
 *
 *    Called when the kernel initiates an asynchronous write to a file in
 *    our filesystem. Our function is just a thin wrapper around
 *    generic_file_aio_write() that tries to validate the dentry first.
 *
 *    Note that files opened with O_SYNC (or superblocks mounted with
 *    "sync") are synchronously written to by the VFS.
 *
 * Results:
 *    Returns the number of bytes written on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
ssize_t
HgfsFileWrite(struct kiocb *iocb,     // IN:  I/O control block
              struct iov_iter *iov)   // IN:  Array of I/O buffers
#else // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static ssize_t
HgfsFileWrite(struct kiocb *iocb,      // IN:  I/O control block
              const struct iovec *iov, // IN:  Array of I/O buffers
              unsigned long numSegs,   // IN:  Number of buffers
              loff_t offset)           // IN:  Offset at which to write
#endif // LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
{
   ssize_t result;
   struct dentry *writeDentry;
   HgfsInodeInfo *iinfo;
   loff_t pos;
   unsigned long iovSegs;

   ASSERT(iocb);
   ASSERT(iocb->ki_filp);
   ASSERT(iocb->ki_filp->f_dentry);
   ASSERT(iov);

   pos = HGFS_IOCB_TO_POS(iocb, offset);
   iovSegs = HGFS_IOV_TO_SEGS(iov, numSegs);

   writeDentry = iocb->ki_filp->f_dentry;
   iinfo = INODE_GET_II_P(writeDentry->d_inode);

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s)\n",
          __func__, writeDentry->d_parent->d_name.name,
          writeDentry->d_name.name));


   spin_lock(&writeDentry->d_inode->i_lock);
   /*
    * Guard against dentry revalidation invalidating the inode underneath us.
    *
    * Data is being written and may have valid data in a page in the cache.
    * This action prevents any invalidating of the inode when a flushing of
    * cache data occurs prior to syncing the file with the server's attributes.
    * The flushing of cache data would empty our in memory write pages list and
    * would cause the inode modified write time to be updated and so the inode
    * would also be invalidated.
    */
   iinfo->numWbPages++;
   spin_unlock(&writeDentry->d_inode->i_lock);

   result = HgfsRevalidate(writeDentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: %s: invalid dentry\n", __func__));
      goto out;
   }

   result = HgfsGenericFileWrite(iocb, iov, iovSegs, pos);

   if (result >= 0) {
      if (IS_SYNC(writeDentry->d_inode) ||
          HGFS_FILECTL_SYNC(iocb->ki_filp->f_flags)) {
         int error;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
         error = vfs_fsync(iocb->ki_filp, 0);
#else
         error = HgfsDoFsync(writeDentry->d_inode);
#endif
         if (error < 0) {
            result = error;
         }
      }
   }

out:
   spin_lock(&writeDentry->d_inode->i_lock);
   iinfo->numWbPages--;
   spin_unlock(&writeDentry->d_inode->i_lock);
   return result;
}


#else
/*
 *----------------------------------------------------------------------
 *
 * HgfsRead --
 *
 *    Called whenever a process reads from a file in our filesystem. Our
 *    function is just a thin wrapper around generic_read_file() that
 *    tries to validate the dentry first.
 *
 * Results:
 *    Returns the number of bytes read on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static ssize_t
HgfsRead(struct file *file,  // IN:  File to read from
         char __user *buf,   // OUT: User buffer to copy data into
         size_t count,       // IN:  Number of bytes to read
         loff_t *offset)     // IN:  Offset at which to read
{
   int result;

   ASSERT(file);
   ASSERT(file->f_dentry);
   ASSERT(buf);
   ASSERT(offset);

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s,%Zu@%lld)\n",
           __func__, file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name, count, (long long) *offset));

   result = HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRead: invalid dentry\n"));
      goto out;
   }

   result = generic_file_read(file, buf, count, offset);
  out:
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsWrite --
 *
 *    Called whenever a process writes to a file in our filesystem. Our
 *    function is just a thin wrapper around generic_write_file() that
 *    tries to validate the dentry first.
 *
 *    Note that files opened with O_SYNC (or superblocks mounted with
 *    "sync") are synchronously written to by the VFS.
 *
 * Results:
 *    Returns the number of bytes written on success, or an error on
 *    failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static ssize_t
HgfsWrite(struct file *file,      // IN: File to write to
          const char __user *buf, // IN: User buffer where the data is
          size_t count,           // IN: Number of bytes to write
          loff_t *offset)         // IN: Offset to begin writing at
{
   int result;

   ASSERT(file);
   ASSERT(file->f_dentry);
   ASSERT(file->f_dentry->d_inode);
   ASSERT(buf);
   ASSERT(offset);

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s,%Zu@%lld)\n",
           __func__, file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name, count, (long long) *offset));

   result = HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsWrite: invalid dentry\n"));
      goto out;
   }

   result = generic_file_write(file, buf, count, offset);
  out:
   return result;
}
#endif

/*
 *----------------------------------------------------------------------
 *
 * HgfsSeek --
 *
 *    Called whenever a process moves the file pointer for a file in our
 *    filesystem. Our function is just a thin wrapper around
 *    generic_file_llseek() that tries to validate the dentry first.
 *
 * Results:
 *    Returns the new position of the file pointer on success,
 *    or a negative error on failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static loff_t
HgfsSeek(struct file *file,  // IN:  File to seek
         loff_t offset,      // IN:  Number of bytes to seek
         int origin)         // IN:  Position to seek from

{
   loff_t result = -1;

   ASSERT(file);
   ASSERT(file->f_dentry);

   LOG(6, (KERN_DEBUG "VMware hgfs: %s(%s/%s, %u, %lld, %d)\n",
           __func__,
            file->f_dentry->d_parent->d_name.name,
            file->f_dentry->d_name.name,
            FILE_GET_FI_P(file)->handle, offset, origin));

   result = (loff_t) HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(6, (KERN_DEBUG "VMware hgfs: %s: invalid dentry\n", __func__));
      goto out;
   }

   result = generic_file_llseek(file, offset, origin);

  out:
   return result;
}


#if !defined VMW_FSYNC_31
/*
 *----------------------------------------------------------------------
 *
 * HgfsDoFsync --
 *
 *    Helper for HgfsFlush() and HgfsFsync().
 *
 *    The hgfs protocol doesn't support fsync explicityly yet.
 *    So for now, we flush all the pages to presumably honor the
 *    intent of an app calling fsync() which is to get the
 *    data onto persistent storage. As things stand now we're at
 *    the whim of the hgfs server code running on the host to fsync or
 *    not if and when it pleases.
 *
 *
 * Results:
 *    Returns zero on success. Otherwise an error.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static int
HgfsDoFsync(struct inode *inode)            // IN: File we operate on
{
   int ret;

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%"FMT64"u)\n",
            __func__,  INODE_GET_II_P(inode)->hostFileId));

   ret = compat_filemap_write_and_wait(inode->i_mapping);

   LOG(4, (KERN_DEBUG "VMware hgfs: %s: returns %d\n",
           __func__, ret));

   return ret;
}
#endif


/*
 *----------------------------------------------------------------------
 *
 * HgfsFlush --
 *
 *    Called when user process calls fflush() on an hgfs file.
 *    Flush all dirty pages and check for write errors.
 *
 *
 * Results:
 *    Returns zero on success. (Currently always succeeds).
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static int
HgfsFlush(struct file *file                        // IN: file to flush
#if !defined VMW_FLUSH_HAS_1_ARG
          ,fl_owner_t id                           // IN: id not used
#endif
         )
{
   int ret = 0;

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s)\n",
            __func__, file->f_dentry->d_parent->d_name.name,
            file->f_dentry->d_name.name));

   if ((file->f_mode & FMODE_WRITE) == 0) {
      goto exit;
   }


   /* Flush writes to the server and return any errors */
   LOG(6, (KERN_DEBUG "VMware hgfs: %s: calling vfs_sync ... \n",
           __func__));
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
   ret = vfs_fsync(file, 0);
#else
   ret = HgfsDoFsync(file->f_dentry->d_inode);
#endif

exit:
   LOG(4, (KERN_DEBUG "VMware hgfs: %s: returns %d\n",
           __func__, ret));
   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsFsync --
 *
 *    Called when user process calls fsync() on hgfs file.
 *
 *    The hgfs protocol doesn't support fsync explicitly yet,
 *    so for now, we flush all the pages to presumably honor the
 *    intent of an app calling fsync() which is to get the
 *    data onto persistent storage, and as things stand now we're at
 *    the whim of the hgfs server code running on the host to fsync or
 *    not if and when it pleases.
 *
 * Results:
 *    Returns zero on success. (Currently always succeeds).
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static int
HgfsFsync(struct file *file,            // IN: File we operate on
#if defined VMW_FSYNC_OLD
          struct dentry *dentry,        // IN: Dentry for this file
#elif defined VMW_FSYNC_31
          loff_t start,                 // IN: start of range to sync
          loff_t end,                   // IN: end of range to sync
#endif
          int datasync)                 // IN: fdatasync or fsync
{
   int ret = 0;
   loff_t startRange;
   loff_t endRange;
   struct inode *inode;

#if defined VMW_FSYNC_31
   startRange = start;
   endRange = end;
#else
   startRange = 0;
   endRange = MAX_INT64;
#endif

   LOG(4, (KERN_DEBUG "VMware hgfs: %s(%s/%s, %lld, %lld, %d)\n",
           __func__,
           file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name,
           startRange, endRange,
           datasync));

   /* Flush writes to the server and return any errors */
   inode = file->f_dentry->d_inode;
#if defined VMW_FSYNC_31
   ret = filemap_write_and_wait_range(inode->i_mapping, startRange, endRange);
#else
   ret = HgfsDoFsync(inode);
#endif

   LOG(4, (KERN_DEBUG "VMware hgfs: %s: written pages  %lld, %lld returns %d)\n",
           __func__, startRange, endRange, ret));
   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsMmap --
 *
 *    Called when user process calls mmap() on hgfs file. This is a very
 *    thin wrapper function- we simply attempt to revalidate the
 *    dentry prior to calling generic_file_mmap().
 *
 * Results:
 *    Returns zero on success.
 *    Returns negative error value on failure
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static int
HgfsMmap(struct file *file,            // IN: File we operate on
         struct vm_area_struct *vma)   // IN/OUT: VM area information
{
   int result;

   ASSERT(file);
   ASSERT(vma);
   ASSERT(file->f_dentry);

   LOG(6, (KERN_DEBUG "VMware hgfs: %s(%s/%s)\n",
           __func__,
           file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name));

   result = HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: %s: invalid dentry\n", __func__));
      goto out;
   }

   result = generic_file_mmap(file, vma);
  out:
   return result;
}


/*
 *----------------------------------------------------------------------
 *
 * HgfsRelease --
 *
 *    Called when the last user of a file closes it, i.e. when the
 *    file's f_count becomes zero.
 *
 * Results:
 *    Returns zero on success, or an error on failure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

static int
HgfsRelease(struct inode *inode,  // IN: Inode that this file points to
            struct file *file)    // IN: File that is getting released
{
   HgfsReq *req;
   HgfsHandle handle;
   HgfsOp opUsed;
   HgfsStatus replyStatus;
   int result = 0;

   ASSERT(inode);
   ASSERT(file);
   ASSERT(file->f_dentry);
   ASSERT(file->f_dentry->d_sb);

   handle = FILE_GET_FI_P(file)->handle;
   LOG(6, (KERN_DEBUG "VMware hgfs: %s(%s/%s, %u)\n",
           __func__,
           file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name,
           handle));

   /*
    * This may be our last open handle to an inode, so we should flush our
    * dirty pages before closing it.
    */
   compat_filemap_write_and_wait(inode->i_mapping);

   HgfsReleaseFileInfo(file);

   req = HgfsGetNewRequest();
   if (!req) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: out of memory while "
              "getting new request\n"));
      result = -ENOMEM;
      goto out;
   }

 retry:
   opUsed = hgfsVersionClose;
   if (opUsed == HGFS_OP_CLOSE_V3) {
      HgfsRequest *header;
      HgfsRequestCloseV3 *request;

      header = (HgfsRequest *)(HGFS_REQ_PAYLOAD(req));
      header->id = req->id;
      header->op = opUsed;

      request = (HgfsRequestCloseV3 *)(HGFS_REQ_PAYLOAD_V3(req));
      request->file = handle;
      request->reserved = 0;
      req->payloadSize = HGFS_REQ_PAYLOAD_SIZE_V3(request);
   } else {
      HgfsRequestClose *request;

      request = (HgfsRequestClose *)(HGFS_REQ_PAYLOAD(req));
      request->header.id = req->id;
      request->header.op = opUsed;
      request->file = handle;
      req->payloadSize = sizeof *request;
   }

   /* Send the request and process the reply. */
   result = HgfsSendRequest(req);
   if (result == 0) {
      /* Get the reply. */
      replyStatus = HgfsReplyStatus(req);
      result = HgfsStatusConvertToLinux(replyStatus);

      switch (result) {
      case 0:
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: released handle %u\n",
                 handle));
         break;
      case -EPROTO:
         /* Retry with older version(s). Set globally. */
         if (opUsed == HGFS_OP_CLOSE_V3) {
            LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: Version 3 not "
                    "supported. Falling back to version 1.\n"));
            hgfsVersionClose = HGFS_OP_CLOSE;
            goto retry;
         }
         break;
      default:
         LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: failed handle %u\n",
                 handle));
         break;
      }
   } else if (result == -EIO) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: timed out\n"));
   } else if (result == -EPROTO) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: server "
              "returned error: %d\n", result));
   } else {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsRelease: unknown error: "
              "%d\n", result));
   }

out:
   HgfsFreeRequest(req);
   return result;
}


#ifndef VMW_SENDFILE_NONE
/*
 *-----------------------------------------------------------------------------
 *
 * HgfsSendfile --
 *
 *    sendfile() wrapper for HGFS. Note that this is for sending a file
 *    from HGFS to another filesystem (or socket). To use HGFS as the
 *    destination file in a call to sendfile(), we must implement sendpage()
 *    as well.
 *
 *    Like mmap(), we're just interested in validating the dentry and then
 *    calling into generic_file_sendfile().
 *
 * Results:
 *    Returns number of bytes written on success, or an error on failure.
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

#if defined VMW_SENDFILE_OLD
static ssize_t
HgfsSendfile(struct file *file,    // IN: File to read from
             loff_t *offset,       // IN/OUT: Where to start reading
             size_t count,         // IN: How much to read
             read_actor_t actor,   // IN: Routine to send a page of data
             void __user *target)  // IN: Destination file/socket
#elif defined VMW_SENDFILE_NEW
static ssize_t
HgfsSendfile(struct file *file,    // IN: File to read from
             loff_t *offset,       // IN/OUT: Where to start reading
             size_t count,         // IN: How much to read
             read_actor_t actor,   // IN: Routine to send a page of data
             void *target)         // IN: Destination file/socket
#endif
{
   ssize_t result;

   ASSERT(file);
   ASSERT(file->f_dentry);
   ASSERT(target);
   ASSERT(offset);
   ASSERT(actor);

   LOG(6, (KERN_DEBUG "VMware hgfs: HgfsSendfile: was called\n"));

   result = HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: HgfsSendfile: invalid dentry\n"));
      goto out;
   }

   result = generic_file_sendfile (file, offset, count, actor, target);
  out:
   return result;

}
#endif


#ifdef VMW_SPLICE_READ
/*
 *-----------------------------------------------------------------------------
 *
 * HgfsSpliceRead --
 *
 *    splice_read() wrapper for HGFS. Note that this is for sending a file
 *    from HGFS to another filesystem (or socket). To use HGFS as the
 *    destination file in a call to splice, we must implement splice_write()
 *    as well.
 *
 *    Like mmap(), we're just interested in validating the dentry and then
 *    calling into generic_file_splice_read().
 *
 * Results:
 *    Returns number of bytes written on success, or an error on failure.
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

static ssize_t
HgfsSpliceRead(struct file *file,            // IN: File to read from
               loff_t *offset,               // IN/OUT: Where to start reading
               struct pipe_inode_info *pipe, // IN: Pipe where to write data
               size_t len,                   // IN: How much to read
               unsigned int flags)           // IN: Various flags
{
   ssize_t result;

   ASSERT(file);
   ASSERT(file->f_dentry);

   LOG(6, (KERN_DEBUG "VMware hgfs: %s(%s/%s, %lu@%Lu)\n",
           __func__,
           file->f_dentry->d_parent->d_name.name,
           file->f_dentry->d_name.name,
           (unsigned long) len, (unsigned long long) *offset));

   result = HgfsRevalidate(file->f_dentry);
   if (result) {
      LOG(4, (KERN_DEBUG "VMware hgfs: %s: invalid dentry\n", __func__));
      goto out;
   }

   result = generic_file_splice_read(file, offset, pipe, len, flags);
out:
   return result;

}
#endif