+ if (buf != MAP_FAILED)
+ munmap(buf, page_size);
+ if (fd != -1)
+ close(fd);
+ return rc;
+}
+
+static int mmap_tst9(char *mnt)
+{
+ char fname[256];
+ char *buf = MAP_FAILED;
+ int fd = -1;
+ int rc = 0;
+
+ if (snprintf(fname, 256, "%s/mmap_tst9", mnt) >= 256) {
+ fprintf(stderr, "dir name too long\n");
+ rc = -ENAMETOOLONG;
+ goto out;
+ }
+ if (unlink(fname) == -1 && errno != ENOENT) {
+ perror("unlink");
+ rc = -errno;
+ goto out;
+ }
+ fd = open(fname, O_RDWR | O_CREAT, 0644);
+ if (fd == -1) {
+ perror("open");
+ rc = -errno;
+ goto out;
+ }
+ buf = mmap(NULL, page_size * 2,
+ PROT_READ, MAP_PRIVATE, fd, (loff_t)(-10 * page_size));
+ if (buf == MAP_FAILED) {
+ perror("mmap");
+ rc = -errno;
+ goto out;
+ }
+ rc = write(STDOUT_FILENO, buf, 2 * page_size);
+ if (rc != -1) {
+ fprintf(stderr, "write succeded with %d instead of failing\n",
+ rc);
+ rc = -EINVAL;
+ goto out;
+ } else if (errno != EFAULT) {
+ fprintf(stderr, "write failed with %d instead of EFAULT(%d)\n",
+ errno, EFAULT);
+ rc = -errno;
+ goto out;
+ }
+ rc = 0;
+out:
+ if (buf != MAP_FAILED)
+ munmap(buf, page_size * 2);
+ if (fd != -1)
+ close(fd);
+ return rc;
+}
+
+static int mmap_tst10(char *mnt)
+{
+ char *buf = MAP_FAILED;
+ char *buffer[256];
+ struct stat st1, st2;
+ long off;
+ int fd = -1;
+ int rc = 0;
+
+ /* cancel unused locks */
+ rc = cancel_lru_locks("osc");
+ if (rc)
+ goto out;
+
+ fd = open(mmap_sanity, O_RDONLY);
+ if (fd == -1) {
+ perror("open");
+ rc = -errno;
+ goto out;
+ }
+
+ if (fstat(fd, &st1) != 0) {
+ perror("fstat");
+ rc = -errno;
+ goto out;
+ }
+
+ sleep(1);
+
+ buf = mmap(NULL, st1.st_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_NORESERVE, fd, 0);
+ if (buf == MAP_FAILED) {
+ perror("mmap");
+ rc = -errno;
+ goto out;
+ }
+
+ /* read some data */
+ for (off = 0; off + 256 <= st1.st_size; off += 256)
+ memcpy(buffer, off + buf, 256);
+
+ if (fstat(fd, &st2) != 0) {
+ perror("fstat");
+ rc = -errno;
+ goto out;
+ }
+
+ if (st1.st_mtime != st2.st_mtime) {
+ fprintf(stderr, "before mmap read mtime(%ld) != after mtime=(%ld)\n",
+ st1.st_mtime, st2.st_mtime);
+ rc = -EINVAL;
+ goto out;
+ }
+out:
+ if (buf != MAP_FAILED)
+ munmap(buf, st1.st_size);
+ if (fd != -1)
+ close(fd);
+ return rc;