blob: 0c3905e0542e8902f5d988a0fe89491a378cbe51 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mmap.c
3 *
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
6 *
7 */
8
9#include <linux/stat.h>
10#include <linux/time.h>
11#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/shm.h>
15#include <linux/errno.h>
16#include <linux/mman.h>
17#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/fcntl.h>
Ying Han456f9982011-05-26 16:25:38 -070019#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Al Viro32c419d2011-01-12 17:37:47 -050023#include "ncp_fs.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * Fill in the supplied page for mmap
Nick Piggind0217ac2007-07-19 01:47:03 -070027 * XXX: how are we excluding truncate/invalidate here? Maybe need to lock
28 * page?
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
Dave Jiang11bac802017-02-24 14:56:41 -080030static int ncp_file_mmap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Dave Jiang11bac802017-02-24 14:56:41 -080032 struct inode *inode = file_inode(vmf->vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 char *pg_addr;
34 unsigned int already_read;
35 unsigned int count;
36 int bufsize;
Nick Piggind0217ac2007-07-19 01:47:03 -070037 int pos; /* XXX: loff_t ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Nick Piggind0217ac2007-07-19 01:47:03 -070039 /*
40 * ncpfs has nothing against high pages as long
41 * as recvmsg and memset works on it
42 */
43 vmf->page = alloc_page(GFP_HIGHUSER);
44 if (!vmf->page)
45 return VM_FAULT_OOM;
46 pg_addr = kmap(vmf->page);
47 pos = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 count = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 /* what we can read in one go */
51 bufsize = NCP_SERVER(inode)->buffer_size;
52
53 already_read = 0;
54 if (ncp_make_open(inode, O_RDONLY) >= 0) {
55 while (already_read < count) {
56 int read_this_time;
57 int to_read;
58
59 to_read = bufsize - (pos % bufsize);
60
61 to_read = min_t(unsigned int, to_read, count - already_read);
62
63 if (ncp_read_kernel(NCP_SERVER(inode),
64 NCP_FINFO(inode)->file_handle,
65 pos, to_read,
66 pg_addr + already_read,
67 &read_this_time) != 0) {
68 read_this_time = 0;
69 }
70 pos += read_this_time;
71 already_read += read_this_time;
72
73 if (read_this_time < to_read) {
74 break;
75 }
76 }
77 ncp_inode_close(inode);
78
79 }
80
81 if (already_read < PAGE_SIZE)
82 memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
Nick Piggind0217ac2007-07-19 01:47:03 -070083 flush_dcache_page(vmf->page);
84 kunmap(vmf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /*
87 * If I understand ncp_read_kernel() properly, the above always
88 * fetches from the network, here the analogue of disk.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010089 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Christoph Lameterf8891e52006-06-30 01:55:45 -070091 count_vm_event(PGMAJFAULT);
Dave Jiang11bac802017-02-24 14:56:41 -080092 mem_cgroup_count_vm_event(vmf->vma->vm_mm, PGMAJFAULT);
Nick Piggind0217ac2007-07-19 01:47:03 -070093 return VM_FAULT_MAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040096static const struct vm_operations_struct ncp_file_mmap =
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Nick Piggin54cb8822007-07-19 01:46:59 -070098 .fault = ncp_file_mmap_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101
102/* This is used for a general mmap of a ncp file */
103int ncp_mmap(struct file *file, struct vm_area_struct *vma)
104{
Al Viro496ad9a2013-01-23 17:07:38 -0500105 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700107 ncp_dbg(1, "called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (!ncp_conn_valid(NCP_SERVER(inode)))
110 return -EIO;
111
112 /* only PAGE_COW or read-only supported now */
113 if (vma->vm_flags & VM_SHARED)
114 return -EINVAL;
115 /* we do not support files bigger than 4GB... We eventually
116 supports just 4GB... */
Libinef9f5152013-07-03 15:01:26 -0700117 if (vma_pages(vma) + vma->vm_pgoff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 > (1U << (32 - PAGE_SHIFT)))
119 return -EFBIG;
120
121 vma->vm_ops = &ncp_file_mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 file_accessed(file);
123 return 0;
124}