1 /*
2  *  file.c
3  *
4  *  Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke
5  *  Copyright (C) 1997 by Volker Lendecke
6  *
7  *  Please add a note about your changes to smbfs in the ChangeLog file.
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fcntl.h>
14 #include <linux/stat.h>
15 #include <linux/mm.h>
16 #include <linux/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/smp_lock.h>
19 
20 #include <asm/uaccess.h>
21 #include <asm/system.h>
22 
23 #include <linux/smbno.h>
24 #include <linux/smb_fs.h>
25 
26 #include "smb_debug.h"
27 #include "proto.h"
28 
29 static int
smb_fsync(struct file * file,struct dentry * dentry,int datasync)30 smb_fsync(struct file *file, struct dentry * dentry, int datasync)
31 {
32 	struct smb_sb_info *server = server_from_dentry(dentry);
33 	int result;
34 
35 	VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry));
36 
37 	/*
38 	 * The VFS will writepage() all dirty pages for us, but we
39 	 * should send a SMBflush to the server, letting it know that
40 	 * we want things synchronized with actual storage.
41 	 *
42 	 * Note: this function requires all pages to have been written already
43 	 *       (should be ok with writepage_sync)
44 	 */
45 	smb_lock_server(server);
46 	result = smb_proc_flush(server, dentry->d_inode->u.smbfs_i.fileid);
47 	smb_unlock_server(server);
48 	return result;
49 }
50 
51 /*
52  * Read a page synchronously.
53  */
54 static int
smb_readpage_sync(struct dentry * dentry,struct page * page)55 smb_readpage_sync(struct dentry *dentry, struct page *page)
56 {
57 	char *buffer = kmap(page);
58 	loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
59 	struct smb_sb_info *server = server_from_dentry(dentry);
60 	unsigned int rsize = smb_get_rsize(server);
61 	int count = PAGE_SIZE;
62 	int result;
63 
64 	VERBOSE("file %s/%s, count=%d@%ld, rsize=%d\n",
65 		DENTRY_PATH(dentry), count, offset, rsize);
66 
67 	result = smb_open(dentry, SMB_O_RDONLY);
68 	if (result < 0) {
69 		PARANOIA("%s/%s open failed, error=%d\n",
70 			 DENTRY_PATH(dentry), result);
71 		goto io_error;
72 	}
73 
74 	do {
75 		if (count < rsize)
76 			rsize = count;
77 
78 		result = server->ops->read(dentry->d_inode,offset,rsize,buffer);
79 		if (result < 0)
80 			goto io_error;
81 
82 		count -= result;
83 		offset += result;
84 		buffer += result;
85 		dentry->d_inode->i_atime = CURRENT_TIME;
86 		if (result < rsize)
87 			break;
88 	} while (count);
89 
90 	memset(buffer, 0, count);
91 	flush_dcache_page(page);
92 	SetPageUptodate(page);
93 	result = 0;
94 
95 io_error:
96 	kunmap(page);
97 	UnlockPage(page);
98 	return result;
99 }
100 
101 /*
102  * We are called with the page locked and we unlock it when done.
103  */
104 static int
smb_readpage(struct file * file,struct page * page)105 smb_readpage(struct file *file, struct page *page)
106 {
107 	int		error;
108 	struct dentry  *dentry = file->f_dentry;
109 
110 	get_page(page);
111 	error = smb_readpage_sync(dentry, page);
112 	put_page(page);
113 	return error;
114 }
115 
116 /*
117  * Write a page synchronously.
118  * Offset is the data offset within the page.
119  */
120 static int
smb_writepage_sync(struct inode * inode,struct page * page,unsigned long pageoffset,unsigned int count)121 smb_writepage_sync(struct inode *inode, struct page *page,
122 		   unsigned long pageoffset, unsigned int count)
123 {
124 	loff_t offset;
125 	char *buffer = kmap(page) + pageoffset;
126 	struct smb_sb_info *server = server_from_inode(inode);
127 	unsigned int wsize = smb_get_wsize(server);
128 	int result, written = 0;
129 
130 	offset = ((loff_t)page->index << PAGE_CACHE_SHIFT) + pageoffset;
131 	VERBOSE("file ino=%ld, fileid=%d, count=%d@%Ld, wsize=%d\n",
132 		inode->i_ino, inode->u.smbfs_i.fileid, count, offset, wsize);
133 
134 	do {
135 		if (count < wsize)
136 			wsize = count;
137 
138 		result = server->ops->write(inode, offset, wsize, buffer);
139 		if (result < 0) {
140 			PARANOIA("failed write, wsize=%d, result=%d\n",
141 				 wsize, result);
142 			break;
143 		}
144 		/* N.B. what if result < wsize?? */
145 #ifdef SMBFS_PARANOIA
146 		if (result < wsize)
147 			PARANOIA("short write, wsize=%d, result=%d\n",
148 				 wsize, result);
149 #endif
150 		buffer += wsize;
151 		offset += wsize;
152 		written += wsize;
153 		count -= wsize;
154 		/*
155 		 * Update the inode now rather than waiting for a refresh.
156 		 */
157 		inode->i_mtime = inode->i_atime = CURRENT_TIME;
158 		inode->u.smbfs_i.flags |= SMB_F_LOCALWRITE;
159 		if (offset > inode->i_size)
160 			inode->i_size = offset;
161 	} while (count);
162 
163 	kunmap(page);
164 	return written ? written : result;
165 }
166 
167 /*
168  * Write a page to the server. This will be used for NFS swapping only
169  * (for now), and we currently do this synchronously only.
170  *
171  * We are called with the page locked and we unlock it when done.
172  */
173 static int
smb_writepage(struct page * page)174 smb_writepage(struct page *page)
175 {
176 	struct address_space *mapping = page->mapping;
177 	struct inode *inode;
178 	unsigned long end_index;
179 	unsigned offset = PAGE_CACHE_SIZE;
180 	int err;
181 
182 	if (!mapping)
183 		BUG();
184 	inode = mapping->host;
185 	if (!inode)
186 		BUG();
187 
188 	end_index = inode->i_size >> PAGE_CACHE_SHIFT;
189 
190 	/* easy case */
191 	if (page->index < end_index)
192 		goto do_it;
193 	/* things got complicated... */
194 	offset = inode->i_size & (PAGE_CACHE_SIZE-1);
195 	/* OK, are we completely out? */
196 	if (page->index >= end_index+1 || !offset)
197 		return -EIO;
198 do_it:
199 	get_page(page);
200 	err = smb_writepage_sync(inode, page, 0, offset);
201 	SetPageUptodate(page);
202 	UnlockPage(page);
203 	put_page(page);
204 	return err;
205 }
206 
207 static int
smb_updatepage(struct file * file,struct page * page,unsigned long offset,unsigned int count)208 smb_updatepage(struct file *file, struct page *page, unsigned long offset,
209 	       unsigned int count)
210 {
211 	struct dentry *dentry = file->f_dentry;
212 
213 	DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
214 	       count, (page->index << PAGE_CACHE_SHIFT)+offset);
215 
216 	return smb_writepage_sync(dentry->d_inode, page, offset, count);
217 }
218 
219 static ssize_t
smb_file_read(struct file * file,char * buf,size_t count,loff_t * ppos)220 smb_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
221 {
222 	struct dentry * dentry = file->f_dentry;
223 	ssize_t	status;
224 
225 	VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
226 		(unsigned long) count, (unsigned long) *ppos);
227 
228 	status = smb_revalidate_inode(dentry);
229 	if (status) {
230 		PARANOIA("%s/%s validation failed, error=%Zd\n",
231 			 DENTRY_PATH(dentry), status);
232 		goto out;
233 	}
234 
235 	VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
236 		(long)dentry->d_inode->i_size,
237 		dentry->d_inode->i_flags, dentry->d_inode->i_atime);
238 
239 	status = generic_file_read(file, buf, count, ppos);
240 out:
241 	return status;
242 }
243 
244 static int
smb_file_mmap(struct file * file,struct vm_area_struct * vma)245 smb_file_mmap(struct file * file, struct vm_area_struct * vma)
246 {
247 	struct dentry * dentry = file->f_dentry;
248 	int	status;
249 
250 	VERBOSE("file %s/%s, address %lu - %lu\n",
251 		DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
252 
253 	status = smb_revalidate_inode(dentry);
254 	if (status) {
255 		PARANOIA("%s/%s validation failed, error=%d\n",
256 			 DENTRY_PATH(dentry), status);
257 		goto out;
258 	}
259 	status = generic_file_mmap(file, vma);
260 out:
261 	return status;
262 }
263 
264 /*
265  * This does the "real" work of the write. The generic routine has
266  * allocated the page, locked it, done all the page alignment stuff
267  * calculations etc. Now we should just copy the data from user
268  * space and write it back to the real medium..
269  *
270  * If the writer ends up delaying the write, the writer needs to
271  * increment the page use counts until he is done with the page.
272  */
smb_prepare_write(struct file * file,struct page * page,unsigned offset,unsigned to)273 static int smb_prepare_write(struct file *file, struct page *page,
274 			     unsigned offset, unsigned to)
275 {
276 	return 0;
277 }
278 
smb_commit_write(struct file * file,struct page * page,unsigned offset,unsigned to)279 static int smb_commit_write(struct file *file, struct page *page,
280 			    unsigned offset, unsigned to)
281 {
282 	int status;
283 
284 	status = -EFAULT;
285 	lock_kernel();
286 	status = smb_updatepage(file, page, offset, to-offset);
287 	unlock_kernel();
288 	return status;
289 }
290 
291 struct address_space_operations smb_file_aops = {
292 	readpage: smb_readpage,
293 	writepage: smb_writepage,
294 	prepare_write: smb_prepare_write,
295 	commit_write: smb_commit_write
296 };
297 
298 /*
299  * Write to a file (through the page cache).
300  */
301 static ssize_t
smb_file_write(struct file * file,const char * buf,size_t count,loff_t * ppos)302 smb_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
303 {
304 	struct dentry * dentry = file->f_dentry;
305 	ssize_t	result;
306 
307 	VERBOSE("file %s/%s, count=%lu@%lu\n",
308 		DENTRY_PATH(dentry),
309 		(unsigned long) count, (unsigned long) *ppos);
310 
311 	result = smb_revalidate_inode(dentry);
312 	if (result) {
313 		PARANOIA("%s/%s validation failed, error=%Zd\n",
314 			 DENTRY_PATH(dentry), result);
315 		goto out;
316 	}
317 
318 	result = smb_open(dentry, SMB_O_WRONLY);
319 	if (result)
320 		goto out;
321 
322 	if (count > 0) {
323 		result = generic_file_write(file, buf, count, ppos);
324 		VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
325 			(long) file->f_pos, (long) dentry->d_inode->i_size,
326 			dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
327 	}
328 out:
329 	return result;
330 }
331 
332 static int
smb_file_open(struct inode * inode,struct file * file)333 smb_file_open(struct inode *inode, struct file * file)
334 {
335 	int result;
336 	struct dentry *dentry = file->f_dentry;
337 	int smb_mode = (file->f_mode & O_ACCMODE) - 1;
338 
339 	lock_kernel();
340 	result = smb_open(dentry, smb_mode);
341 	if (result)
342 		goto out;
343 	inode->u.smbfs_i.openers++;
344 out:
345 	unlock_kernel();
346 	return 0;
347 }
348 
349 static int
smb_file_release(struct inode * inode,struct file * file)350 smb_file_release(struct inode *inode, struct file * file)
351 {
352 	lock_kernel();
353 	if (!--inode->u.smbfs_i.openers) {
354 		/* We must flush any dirty pages now as we won't be able to
355 		   write anything after close. mmap can trigger this.
356 		   "openers" should perhaps include mmap'ers ... */
357 		filemap_fdatasync(inode->i_mapping);
358 		filemap_fdatawait(inode->i_mapping);
359 		smb_close(inode);
360 	}
361 	unlock_kernel();
362 	return 0;
363 }
364 
365 /*
366  * Check whether the required access is compatible with
367  * an inode's permission. SMB doesn't recognize superuser
368  * privileges, so we need our own check for this.
369  */
370 static int
smb_file_permission(struct inode * inode,int mask)371 smb_file_permission(struct inode *inode, int mask)
372 {
373 	int mode = inode->i_mode;
374 	int error = 0;
375 
376 	VERBOSE("mode=%x, mask=%x\n", mode, mask);
377 
378 	/* Look at user permissions */
379 	mode >>= 6;
380 	if ((mode & 7 & mask) != mask)
381 		error = -EACCES;
382 	return error;
383 }
384 
385 struct file_operations smb_file_operations =
386 {
387 	llseek:		generic_file_llseek,
388 	read:		smb_file_read,
389 	write:		smb_file_write,
390 	ioctl:		smb_ioctl,
391 	mmap:		smb_file_mmap,
392 	open:		smb_file_open,
393 	release:	smb_file_release,
394 	fsync:		smb_fsync,
395 };
396 
397 struct inode_operations smb_file_inode_operations =
398 {
399 	permission:	smb_file_permission,
400 	revalidate:	smb_revalidate_inode,
401 	setattr:	smb_notify_change,
402 };
403