blob: a5c5cf2ff0073980eb32058414da0d16ffdba064 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * mmap.c
4 *
5 * Copyright (C) 1995, 1996 by Volker Lendecke
6 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7 *
8 */
9
10#include <linux/stat.h>
11#include <linux/time.h>
12#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/shm.h>
16#include <linux/errno.h>
17#include <linux/mman.h>
18#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/fcntl.h>
Ying Han456f9982011-05-26 16:25:38 -070020#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Al Viro32c419d2011-01-12 17:37:47 -050024#include "ncp_fs.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
27 * Fill in the supplied page for mmap
Nick Piggind0217ac2007-07-19 01:47:03 -070028 * XXX: how are we excluding truncate/invalidate here? Maybe need to lock
29 * page?
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
Dave Jiang11bac802017-02-24 14:56:41 -080031static int ncp_file_mmap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Dave Jiang11bac802017-02-24 14:56:41 -080033 struct inode *inode = file_inode(vmf->vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 char *pg_addr;
35 unsigned int already_read;
36 unsigned int count;
37 int bufsize;
Nick Piggind0217ac2007-07-19 01:47:03 -070038 int pos; /* XXX: loff_t ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Nick Piggind0217ac2007-07-19 01:47:03 -070040 /*
41 * ncpfs has nothing against high pages as long
42 * as recvmsg and memset works on it
43 */
44 vmf->page = alloc_page(GFP_HIGHUSER);
45 if (!vmf->page)
46 return VM_FAULT_OOM;
47 pg_addr = kmap(vmf->page);
48 pos = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 count = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* what we can read in one go */
52 bufsize = NCP_SERVER(inode)->buffer_size;
53
54 already_read = 0;
55 if (ncp_make_open(inode, O_RDONLY) >= 0) {
56 while (already_read < count) {
57 int read_this_time;
58 int to_read;
59
60 to_read = bufsize - (pos % bufsize);
61
62 to_read = min_t(unsigned int, to_read, count - already_read);
63
64 if (ncp_read_kernel(NCP_SERVER(inode),
65 NCP_FINFO(inode)->file_handle,
66 pos, to_read,
67 pg_addr + already_read,
68 &read_this_time) != 0) {
69 read_this_time = 0;
70 }
71 pos += read_this_time;
72 already_read += read_this_time;
73
74 if (read_this_time < to_read) {
75 break;
76 }
77 }
78 ncp_inode_close(inode);
79
80 }
81
82 if (already_read < PAGE_SIZE)
83 memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
Nick Piggind0217ac2007-07-19 01:47:03 -070084 flush_dcache_page(vmf->page);
85 kunmap(vmf->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * If I understand ncp_read_kernel() properly, the above always
89 * fetches from the network, here the analogue of disk.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010090 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Christoph Lameterf8891e52006-06-30 01:55:45 -070092 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -070093 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Nick Piggind0217ac2007-07-19 01:47:03 -070094 return VM_FAULT_MAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040097static const struct vm_operations_struct ncp_file_mmap =
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Nick Piggin54cb8822007-07-19 01:46:59 -070099 .fault = ncp_file_mmap_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102
103/* This is used for a general mmap of a ncp file */
104int ncp_mmap(struct file *file, struct vm_area_struct *vma)
105{
Al Viro496ad9a2013-01-23 17:07:38 -0500106 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700108 ncp_dbg(1, "called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (!ncp_conn_valid(NCP_SERVER(inode)))
111 return -EIO;
112
113 /* only PAGE_COW or read-only supported now */
114 if (vma->vm_flags & VM_SHARED)
115 return -EINVAL;
116 /* we do not support files bigger than 4GB... We eventually
117 supports just 4GB... */
Libinef9f5152013-07-03 15:01:26 -0700118 if (vma_pages(vma) + vma->vm_pgoff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 > (1U << (32 - PAGE_SHIFT)))
120 return -EFBIG;
121
122 vma->vm_ops = &ncp_file_mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 file_accessed(file);
124 return 0;
125}