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.
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>
16 #include <linux/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/smp_lock.h>
20 #include <asm/uaccess.h>
21 #include <asm/system.h>
23 #include <linux/smbno.h>
24 #include <linux/smb_fs.h>
26 #include "smb_debug.h"
30 smb_fsync(struct file *file, struct dentry * dentry, int datasync)
32 struct smb_sb_info *server = server_from_dentry(dentry);
35 VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry));
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.
42 * Note: this function requires all pages to have been written already
43 * (should be ok with writepage_sync)
45 smb_lock_server(server);
46 result = smb_proc_flush(server, dentry->d_inode->u.smbfs_i.fileid);
47 smb_unlock_server(server);
52 * Read a page synchronously.
55 smb_readpage_sync(struct dentry *dentry, struct page *page)
57 char *buffer = kmap(page);
58 unsigned long offset = page->index << PAGE_CACHE_SHIFT;
59 int rsize = smb_get_rsize(server_from_dentry(dentry));
60 int count = PAGE_SIZE;
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);
68 PARANOIA("%s/%s open failed, error=%d\n",
69 DENTRY_PATH(dentry), result);
77 result = smb_proc_read(dentry->d_inode, offset, rsize, buffer);
84 dentry->d_inode->i_atime = CURRENT_TIME;
89 memset(buffer, 0, count);
90 flush_dcache_page(page);
91 SetPageUptodate(page);
101 * We are called with the page locked and we unlock it when done.
104 smb_readpage(struct file *file, struct page *page)
107 struct dentry *dentry = file->f_dentry;
110 error = smb_readpage_sync(dentry, page);
116 * Write a page synchronously.
117 * Offset is the data offset within the page.
120 smb_writepage_sync(struct inode *inode, struct page *page,
121 unsigned long offset, unsigned int count)
123 char *buffer = kmap(page) + offset;
124 int wsize = smb_get_wsize(server_from_inode(inode));
125 int result, written = 0;
127 offset += page->index << PAGE_CACHE_SHIFT;
128 VERBOSE("file ino=%ld, fileid=%d, count=%d@%ld, wsize=%d\n",
129 inode->i_ino, inode->u.smbfs_i.fileid, count, offset, wsize);
135 result = smb_proc_write(inode, offset, wsize, buffer);
137 PARANOIA("failed write, wsize=%d, result=%d\n",
141 /* N.B. what if result < wsize?? */
142 #ifdef SMBFS_PARANOIA
144 PARANOIA("short write, wsize=%d, result=%d\n",
152 * Update the inode now rather than waiting for a refresh.
154 inode->i_mtime = inode->i_atime = CURRENT_TIME;
155 inode->u.smbfs_i.flags |= SMB_F_LOCALWRITE;
156 if (offset > inode->i_size)
157 inode->i_size = offset;
161 return written ? written : result;
165 * Write a page to the server. This will be used for NFS swapping only
166 * (for now), and we currently do this synchronously only.
168 * We are called with the page locked and we unlock it when done.
171 smb_writepage(struct page *page)
173 struct address_space *mapping = page->mapping;
175 unsigned long end_index;
176 unsigned offset = PAGE_CACHE_SIZE;
181 inode = mapping->host;
185 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
188 if (page->index < end_index)
190 /* things got complicated... */
191 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
192 /* OK, are we completely out? */
193 if (page->index >= end_index+1 || !offset)
197 err = smb_writepage_sync(inode, page, 0, offset);
198 SetPageUptodate(page);
205 smb_updatepage(struct file *file, struct page *page, unsigned long offset,
208 struct dentry *dentry = file->f_dentry;
210 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
211 count, (page->index << PAGE_CACHE_SHIFT)+offset);
213 return smb_writepage_sync(dentry->d_inode, page, offset, count);
217 smb_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
219 struct dentry * dentry = file->f_dentry;
222 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
223 (unsigned long) count, (unsigned long) *ppos);
225 status = smb_revalidate_inode(dentry);
227 PARANOIA("%s/%s validation failed, error=%Zd\n",
228 DENTRY_PATH(dentry), status);
232 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
233 (long)dentry->d_inode->i_size,
234 dentry->d_inode->i_flags, dentry->d_inode->i_atime);
236 status = generic_file_read(file, buf, count, ppos);
242 smb_file_mmap(struct file * file, struct vm_area_struct * vma)
244 struct dentry * dentry = file->f_dentry;
247 VERBOSE("file %s/%s, address %lu - %lu\n",
248 DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
250 status = smb_revalidate_inode(dentry);
252 PARANOIA("%s/%s validation failed, error=%d\n",
253 DENTRY_PATH(dentry), status);
256 status = generic_file_mmap(file, vma);
262 * This does the "real" work of the write. The generic routine has
263 * allocated the page, locked it, done all the page alignment stuff
264 * calculations etc. Now we should just copy the data from user
265 * space and write it back to the real medium..
267 * If the writer ends up delaying the write, the writer needs to
268 * increment the page use counts until he is done with the page.
270 static int smb_prepare_write(struct file *file, struct page *page,
271 unsigned offset, unsigned to)
276 static int smb_commit_write(struct file *file, struct page *page,
277 unsigned offset, unsigned to)
283 status = smb_updatepage(file, page, offset, to-offset);
288 struct address_space_operations smb_file_aops = {
289 readpage: smb_readpage,
290 writepage: smb_writepage,
291 prepare_write: smb_prepare_write,
292 commit_write: smb_commit_write
296 * Write to a file (through the page cache).
299 smb_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
301 struct dentry * dentry = file->f_dentry;
304 VERBOSE("file %s/%s, count=%lu@%lu\n",
306 (unsigned long) count, (unsigned long) *ppos);
308 result = smb_revalidate_inode(dentry);
310 PARANOIA("%s/%s validation failed, error=%Zd\n",
311 DENTRY_PATH(dentry), result);
315 result = smb_open(dentry, SMB_O_WRONLY);
320 result = generic_file_write(file, buf, count, ppos);
321 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
322 (long) file->f_pos, (long) dentry->d_inode->i_size,
323 dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
330 smb_file_open(struct inode *inode, struct file * file)
333 struct dentry *dentry = file->f_dentry;
334 int smb_mode = (file->f_mode & O_ACCMODE) - 1;
337 result = smb_open(dentry, smb_mode);
340 inode->u.smbfs_i.openers++;
347 smb_file_release(struct inode *inode, struct file * file)
350 if (!--inode->u.smbfs_i.openers) {
351 /* We must flush any dirty pages now as we won't be able to
352 write anything after close. mmap can trigger this.
353 "openers" should perhaps include mmap'ers ... */
354 filemap_fdatasync(inode->i_mapping);
355 filemap_fdatawait(inode->i_mapping);
363 * Check whether the required access is compatible with
364 * an inode's permission. SMB doesn't recognize superuser
365 * privileges, so we need our own check for this.
368 smb_file_permission(struct inode *inode, int mask)
370 int mode = inode->i_mode;
373 VERBOSE("mode=%x, mask=%x\n", mode, mask);
375 /* Look at user permissions */
377 if ((mode & 7 & mask) != mask)
382 struct file_operations smb_file_operations =
384 llseek: generic_file_llseek,
386 write: smb_file_write,
390 release: smb_file_release,
394 struct inode_operations smb_file_inode_operations =
396 permission: smb_file_permission,
397 revalidate: smb_revalidate_inode,
398 setattr: smb_notify_change,