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 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;
64 VERBOSE("file %s/%s, count=%d@%ld, rsize=%d\n",
65 DENTRY_PATH(dentry), count, offset, rsize);
67 result = smb_open(dentry, SMB_O_RDONLY);
69 PARANOIA("%s/%s open failed, error=%d\n",
70 DENTRY_PATH(dentry), result);
78 result = server->ops->read(dentry->d_inode,offset,rsize,buffer);
85 dentry->d_inode->i_atime = CURRENT_TIME;
90 memset(buffer, 0, count);
91 flush_dcache_page(page);
92 SetPageUptodate(page);
102 * We are called with the page locked and we unlock it when done.
105 smb_readpage(struct file *file, struct page *page)
108 struct dentry *dentry = file->f_dentry;
111 error = smb_readpage_sync(dentry, page);
117 * Write a page synchronously.
118 * Offset is the data offset within the page.
121 smb_writepage_sync(struct inode *inode, struct page *page,
122 unsigned long pageoffset, unsigned int count)
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;
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);
138 result = server->ops->write(inode, offset, wsize, buffer);
140 PARANOIA("failed write, wsize=%d, result=%d\n",
144 /* N.B. what if result < wsize?? */
145 #ifdef SMBFS_PARANOIA
147 PARANOIA("short write, wsize=%d, result=%d\n",
155 * Update the inode now rather than waiting for a refresh.
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;
164 return written ? written : result;
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.
171 * We are called with the page locked and we unlock it when done.
174 smb_writepage(struct page *page)
176 struct address_space *mapping = page->mapping;
178 unsigned long end_index;
179 unsigned offset = PAGE_CACHE_SIZE;
184 inode = mapping->host;
188 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
191 if (page->index < end_index)
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)
200 err = smb_writepage_sync(inode, page, 0, offset);
201 SetPageUptodate(page);
208 smb_updatepage(struct file *file, struct page *page, unsigned long offset,
211 struct dentry *dentry = file->f_dentry;
213 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
214 count, (page->index << PAGE_CACHE_SHIFT)+offset);
216 return smb_writepage_sync(dentry->d_inode, page, offset, count);
220 smb_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
222 struct dentry * dentry = file->f_dentry;
225 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
226 (unsigned long) count, (unsigned long) *ppos);
228 status = smb_revalidate_inode(dentry);
230 PARANOIA("%s/%s validation failed, error=%Zd\n",
231 DENTRY_PATH(dentry), status);
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);
239 status = generic_file_read(file, buf, count, ppos);
245 smb_file_mmap(struct file * file, struct vm_area_struct * vma)
247 struct dentry * dentry = file->f_dentry;
250 VERBOSE("file %s/%s, address %lu - %lu\n",
251 DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
253 status = smb_revalidate_inode(dentry);
255 PARANOIA("%s/%s validation failed, error=%d\n",
256 DENTRY_PATH(dentry), status);
259 status = generic_file_mmap(file, vma);
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..
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.
273 static int smb_prepare_write(struct file *file, struct page *page,
274 unsigned offset, unsigned to)
279 static int smb_commit_write(struct file *file, struct page *page,
280 unsigned offset, unsigned to)
286 status = smb_updatepage(file, page, offset, to-offset);
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
299 * Write to a file (through the page cache).
302 smb_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
304 struct dentry * dentry = file->f_dentry;
307 VERBOSE("file %s/%s, count=%lu@%lu\n",
309 (unsigned long) count, (unsigned long) *ppos);
311 result = smb_revalidate_inode(dentry);
313 PARANOIA("%s/%s validation failed, error=%Zd\n",
314 DENTRY_PATH(dentry), result);
318 result = smb_open(dentry, SMB_O_WRONLY);
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);
333 smb_file_open(struct inode *inode, struct file * file)
336 struct dentry *dentry = file->f_dentry;
337 int smb_mode = (file->f_mode & O_ACCMODE) - 1;
340 result = smb_open(dentry, smb_mode);
343 inode->u.smbfs_i.openers++;
350 smb_file_release(struct inode *inode, struct file * file)
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);
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.
371 smb_file_permission(struct inode *inode, int mask)
373 int mode = inode->i_mode;
376 VERBOSE("mode=%x, mask=%x\n", mode, mask);
378 /* Look at user permissions */
380 if ((mode & 7 & mask) != mask)
385 struct file_operations smb_file_operations =
387 llseek: generic_file_llseek,
389 write: smb_file_write,
393 release: smb_file_release,
397 struct inode_operations smb_file_inode_operations =
399 permission: smb_file_permission,
400 revalidate: smb_revalidate_inode,
401 setattr: smb_notify_change,