[MIPS] Delete duplicate definitions of break codes.
[linux-2.6/mini2440.git] / fs / smbfs / file.c
blobb4fcfa8b55a149d3e16e995cab6cae0342fb3bf5
1 /*
2 * file.c
4 * Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
10 #include <linux/time.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 #include <linux/net.h>
21 #include <asm/uaccess.h>
22 #include <asm/system.h>
24 #include <linux/smbno.h>
25 #include <linux/smb_fs.h>
27 #include "smb_debug.h"
28 #include "proto.h"
30 static int
31 smb_fsync(struct file *file, struct dentry * dentry, int datasync)
33 struct smb_sb_info *server = server_from_dentry(dentry);
34 int result;
36 VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry));
39 * The VFS will writepage() all dirty pages for us, but we
40 * should send a SMBflush to the server, letting it know that
41 * we want things synchronized with actual storage.
43 * Note: this function requires all pages to have been written already
44 * (should be ok with writepage_sync)
46 result = smb_proc_flush(server, SMB_I(dentry->d_inode)->fileid);
47 return result;
51 * Read a page synchronously.
53 static int
54 smb_readpage_sync(struct dentry *dentry, struct page *page)
56 char *buffer = kmap(page);
57 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
58 struct smb_sb_info *server = server_from_dentry(dentry);
59 unsigned int rsize = smb_get_rsize(server);
60 int count = PAGE_SIZE;
61 int result;
63 VERBOSE("file %s/%s, count=%d@%Ld, rsize=%d\n",
64 DENTRY_PATH(dentry), count, offset, rsize);
66 result = smb_open(dentry, SMB_O_RDONLY);
67 if (result < 0)
68 goto io_error;
70 do {
71 if (count < rsize)
72 rsize = count;
74 result = server->ops->read(dentry->d_inode,offset,rsize,buffer);
75 if (result < 0)
76 goto io_error;
78 count -= result;
79 offset += result;
80 buffer += result;
81 dentry->d_inode->i_atime =
82 current_fs_time(dentry->d_inode->i_sb);
83 if (result < rsize)
84 break;
85 } while (count);
87 memset(buffer, 0, count);
88 flush_dcache_page(page);
89 SetPageUptodate(page);
90 result = 0;
92 io_error:
93 kunmap(page);
94 unlock_page(page);
95 return result;
99 * We are called with the page locked and we unlock it when done.
101 static int
102 smb_readpage(struct file *file, struct page *page)
104 int error;
105 struct dentry *dentry = file->f_dentry;
107 page_cache_get(page);
108 error = smb_readpage_sync(dentry, page);
109 page_cache_release(page);
110 return error;
114 * Write a page synchronously.
115 * Offset is the data offset within the page.
117 static int
118 smb_writepage_sync(struct inode *inode, struct page *page,
119 unsigned long pageoffset, unsigned int count)
121 loff_t offset;
122 char *buffer = kmap(page) + pageoffset;
123 struct smb_sb_info *server = server_from_inode(inode);
124 unsigned int wsize = smb_get_wsize(server);
125 int ret = 0;
127 offset = ((loff_t)page->index << PAGE_CACHE_SHIFT) + pageoffset;
128 VERBOSE("file ino=%ld, fileid=%d, count=%d@%Ld, wsize=%d\n",
129 inode->i_ino, SMB_I(inode)->fileid, count, offset, wsize);
131 do {
132 int write_ret;
134 if (count < wsize)
135 wsize = count;
137 write_ret = server->ops->write(inode, offset, wsize, buffer);
138 if (write_ret < 0) {
139 PARANOIA("failed write, wsize=%d, write_ret=%d\n",
140 wsize, write_ret);
141 ret = write_ret;
142 break;
144 /* N.B. what if result < wsize?? */
145 #ifdef SMBFS_PARANOIA
146 if (write_ret < wsize)
147 PARANOIA("short write, wsize=%d, write_ret=%d\n",
148 wsize, write_ret);
149 #endif
150 buffer += wsize;
151 offset += wsize;
152 count -= wsize;
154 * Update the inode now rather than waiting for a refresh.
156 inode->i_mtime = inode->i_atime = current_fs_time(inode->i_sb);
157 SMB_I(inode)->flags |= SMB_F_LOCALWRITE;
158 if (offset > inode->i_size)
159 inode->i_size = offset;
160 } while (count);
162 kunmap(page);
163 return ret;
167 * Write a page to the server. This will be used for NFS swapping only
168 * (for now), and we currently do this synchronously only.
170 * We are called with the page locked and we unlock it when done.
172 static int
173 smb_writepage(struct page *page, struct writeback_control *wbc)
175 struct address_space *mapping = page->mapping;
176 struct inode *inode;
177 unsigned long end_index;
178 unsigned offset = PAGE_CACHE_SIZE;
179 int err;
181 if (!mapping)
182 BUG();
183 inode = mapping->host;
184 if (!inode)
185 BUG();
187 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
189 /* easy case */
190 if (page->index < end_index)
191 goto do_it;
192 /* things got complicated... */
193 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
194 /* OK, are we completely out? */
195 if (page->index >= end_index+1 || !offset)
196 return 0; /* truncated - don't care */
197 do_it:
198 page_cache_get(page);
199 err = smb_writepage_sync(inode, page, 0, offset);
200 SetPageUptodate(page);
201 unlock_page(page);
202 page_cache_release(page);
203 return err;
206 static int
207 smb_updatepage(struct file *file, struct page *page, unsigned long offset,
208 unsigned int count)
210 struct dentry *dentry = file->f_dentry;
212 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
213 count, (page->index << PAGE_CACHE_SHIFT)+offset);
215 return smb_writepage_sync(dentry->d_inode, page, offset, count);
218 static ssize_t
219 smb_file_read(struct file * file, char __user * buf, size_t count, loff_t *ppos)
221 struct dentry * dentry = file->f_dentry;
222 ssize_t status;
224 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
225 (unsigned long) count, (unsigned long) *ppos);
227 status = smb_revalidate_inode(dentry);
228 if (status) {
229 PARANOIA("%s/%s validation failed, error=%Zd\n",
230 DENTRY_PATH(dentry), status);
231 goto out;
234 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
235 (long)dentry->d_inode->i_size,
236 dentry->d_inode->i_flags, dentry->d_inode->i_atime);
238 status = generic_file_read(file, buf, count, ppos);
239 out:
240 return status;
243 static int
244 smb_file_mmap(struct file * file, struct vm_area_struct * vma)
246 struct dentry * dentry = file->f_dentry;
247 int status;
249 VERBOSE("file %s/%s, address %lu - %lu\n",
250 DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
252 status = smb_revalidate_inode(dentry);
253 if (status) {
254 PARANOIA("%s/%s validation failed, error=%d\n",
255 DENTRY_PATH(dentry), status);
256 goto out;
258 status = generic_file_mmap(file, vma);
259 out:
260 return status;
263 static ssize_t
264 smb_file_sendfile(struct file *file, loff_t *ppos,
265 size_t count, read_actor_t actor, void *target)
267 struct dentry *dentry = file->f_dentry;
268 ssize_t status;
270 VERBOSE("file %s/%s, pos=%Ld, count=%d\n",
271 DENTRY_PATH(dentry), *ppos, count);
273 status = smb_revalidate_inode(dentry);
274 if (status) {
275 PARANOIA("%s/%s validation failed, error=%Zd\n",
276 DENTRY_PATH(dentry), status);
277 goto out;
279 status = generic_file_sendfile(file, ppos, count, actor, target);
280 out:
281 return status;
285 * This does the "real" work of the write. The generic routine has
286 * allocated the page, locked it, done all the page alignment stuff
287 * calculations etc. Now we should just copy the data from user
288 * space and write it back to the real medium..
290 * If the writer ends up delaying the write, the writer needs to
291 * increment the page use counts until he is done with the page.
293 static int smb_prepare_write(struct file *file, struct page *page,
294 unsigned offset, unsigned to)
296 return 0;
299 static int smb_commit_write(struct file *file, struct page *page,
300 unsigned offset, unsigned to)
302 int status;
304 status = -EFAULT;
305 lock_kernel();
306 status = smb_updatepage(file, page, offset, to-offset);
307 unlock_kernel();
308 return status;
311 struct address_space_operations smb_file_aops = {
312 .readpage = smb_readpage,
313 .writepage = smb_writepage,
314 .prepare_write = smb_prepare_write,
315 .commit_write = smb_commit_write
319 * Write to a file (through the page cache).
321 static ssize_t
322 smb_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
324 struct dentry * dentry = file->f_dentry;
325 ssize_t result;
327 VERBOSE("file %s/%s, count=%lu@%lu\n",
328 DENTRY_PATH(dentry),
329 (unsigned long) count, (unsigned long) *ppos);
331 result = smb_revalidate_inode(dentry);
332 if (result) {
333 PARANOIA("%s/%s validation failed, error=%Zd\n",
334 DENTRY_PATH(dentry), result);
335 goto out;
338 result = smb_open(dentry, SMB_O_WRONLY);
339 if (result)
340 goto out;
342 if (count > 0) {
343 result = generic_file_write(file, buf, count, ppos);
344 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
345 (long) file->f_pos, (long) dentry->d_inode->i_size,
346 dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
348 out:
349 return result;
352 static int
353 smb_file_open(struct inode *inode, struct file * file)
355 int result;
356 struct dentry *dentry = file->f_dentry;
357 int smb_mode = (file->f_mode & O_ACCMODE) - 1;
359 lock_kernel();
360 result = smb_open(dentry, smb_mode);
361 if (result)
362 goto out;
363 SMB_I(inode)->openers++;
364 out:
365 unlock_kernel();
366 return result;
369 static int
370 smb_file_release(struct inode *inode, struct file * file)
372 lock_kernel();
373 if (!--SMB_I(inode)->openers) {
374 /* We must flush any dirty pages now as we won't be able to
375 write anything after close. mmap can trigger this.
376 "openers" should perhaps include mmap'ers ... */
377 filemap_fdatawrite(inode->i_mapping);
378 filemap_fdatawait(inode->i_mapping);
379 smb_close(inode);
381 unlock_kernel();
382 return 0;
386 * Check whether the required access is compatible with
387 * an inode's permission. SMB doesn't recognize superuser
388 * privileges, so we need our own check for this.
390 static int
391 smb_file_permission(struct inode *inode, int mask, struct nameidata *nd)
393 int mode = inode->i_mode;
394 int error = 0;
396 VERBOSE("mode=%x, mask=%x\n", mode, mask);
398 /* Look at user permissions */
399 mode >>= 6;
400 if ((mode & 7 & mask) != mask)
401 error = -EACCES;
402 return error;
405 struct file_operations smb_file_operations =
407 .llseek = remote_llseek,
408 .read = smb_file_read,
409 .write = smb_file_write,
410 .ioctl = smb_ioctl,
411 .mmap = smb_file_mmap,
412 .open = smb_file_open,
413 .release = smb_file_release,
414 .fsync = smb_fsync,
415 .sendfile = smb_file_sendfile,
418 struct inode_operations smb_file_inode_operations =
420 .permission = smb_file_permission,
421 .getattr = smb_getattr,
422 .setattr = smb_notify_change,