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