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/malloc.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"
29 smb_fsync(struct file
*file
, struct dentry
* dentry
, int datasync
)
31 VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry
));
36 * Read a page synchronously.
39 smb_readpage_sync(struct dentry
*dentry
, struct page
*page
)
41 char *buffer
= page_address(page
);
42 unsigned long offset
= page
->index
<< PAGE_CACHE_SHIFT
;
43 int rsize
= smb_get_rsize(server_from_dentry(dentry
));
44 int count
= PAGE_SIZE
;
47 VERBOSE("file %s/%s, count=%d@%ld, rsize=%d\n",
48 DENTRY_PATH(dentry
), count
, offset
, rsize
);
50 result
= smb_open(dentry
, SMB_O_RDONLY
);
52 PARANOIA("%s/%s open failed, error=%d\n",
53 DENTRY_PATH(dentry
), result
);
61 result
= smb_proc_read(dentry
->d_inode
, offset
, rsize
, buffer
);
68 dentry
->d_inode
->i_atime
= CURRENT_TIME
;
73 memset(buffer
, 0, count
);
74 flush_dcache_page(page
);
75 SetPageUptodate(page
);
84 * We are called with the page locked and we unlock it when done.
87 smb_readpage(struct file
*file
, struct page
*page
)
90 struct dentry
*dentry
= file
->f_dentry
;
92 DEBUG1("readpage %p\n", page_address(page
));
95 error
= smb_readpage_sync(dentry
, page
);
101 * Write a page synchronously.
102 * Offset is the data offset within the page.
105 smb_writepage_sync(struct inode
*inode
, struct page
*page
,
106 unsigned long offset
, unsigned int count
)
108 u8
*buffer
= page_address(page
) + offset
;
109 int wsize
= smb_get_wsize(server_from_inode(inode
));
110 int result
, written
= 0;
112 offset
+= page
->index
<< PAGE_CACHE_SHIFT
;
113 VERBOSE("file ino=%ld, fileid=%d, count=%d@%ld, wsize=%d\n",
114 inode
->i_ino
, inode
->u
.smbfs_i
.fileid
, count
, offset
, wsize
);
120 result
= smb_proc_write(inode
, offset
, wsize
, buffer
);
122 PARANOIA("failed write, wsize=%d, result=%d\n",
126 /* N.B. what if result < wsize?? */
127 #ifdef SMBFS_PARANOIA
129 PARANOIA("short write, wsize=%d, result=%d\n",
137 * Update the inode now rather than waiting for a refresh.
139 inode
->i_mtime
= inode
->i_atime
= CURRENT_TIME
;
140 if (offset
> inode
->i_size
)
141 inode
->i_size
= offset
;
142 inode
->u
.smbfs_i
.cache_valid
|= SMB_F_LOCALWRITE
;
144 return written
? written
: result
;
148 * Write a page to the server. This will be used for NFS swapping only
149 * (for now), and we currently do this synchronously only.
151 * We are called with the page locked and we unlock it when done.
154 smb_writepage(struct page
*page
)
156 struct address_space
*mapping
= page
->mapping
;
158 unsigned long end_index
;
159 unsigned offset
= PAGE_CACHE_SIZE
;
164 inode
= (struct inode
*)mapping
->host
;
168 end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
171 if (page
->index
< end_index
)
173 /* things got complicated... */
174 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
175 /* OK, are we completely out? */
176 if (page
->index
>= end_index
+1 || !offset
)
180 err
= smb_writepage_sync(inode
, page
, 0, offset
);
181 SetPageUptodate(page
);
188 smb_updatepage(struct file
*file
, struct page
*page
, unsigned long offset
,
191 struct dentry
*dentry
= file
->f_dentry
;
193 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry
),
194 count
, (page
->index
<< PAGE_CACHE_SHIFT
)+offset
);
196 return smb_writepage_sync(dentry
->d_inode
, page
, offset
, count
);
200 smb_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
202 struct dentry
* dentry
= file
->f_dentry
;
205 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry
),
206 (unsigned long) count
, (unsigned long) *ppos
);
208 status
= smb_revalidate_inode(dentry
);
211 PARANOIA("%s/%s validation failed, error=%Zd\n",
212 DENTRY_PATH(dentry
), status
);
216 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
217 (long)dentry
->d_inode
->i_size
,
218 dentry
->d_inode
->i_flags
, dentry
->d_inode
->i_atime
);
220 status
= generic_file_read(file
, buf
, count
, ppos
);
226 smb_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
228 struct dentry
* dentry
= file
->f_dentry
;
231 VERBOSE("file %s/%s, address %lu - %lu\n",
232 DENTRY_PATH(dentry
), vma
->vm_start
, vma
->vm_end
);
234 status
= smb_revalidate_inode(dentry
);
237 PARANOIA("%s/%s validation failed, error=%d\n",
238 DENTRY_PATH(dentry
), status
);
241 status
= generic_file_mmap(file
, vma
);
247 * This does the "real" work of the write. The generic routine has
248 * allocated the page, locked it, done all the page alignment stuff
249 * calculations etc. Now we should just copy the data from user
250 * space and write it back to the real medium..
252 * If the writer ends up delaying the write, the writer needs to
253 * increment the page use counts until he is done with the page.
255 static int smb_prepare_write(struct file
*file
, struct page
*page
,
256 unsigned offset
, unsigned to
)
262 static int smb_commit_write(struct file
*file
, struct page
*page
,
263 unsigned offset
, unsigned to
)
269 status
= smb_updatepage(file
, page
, offset
, to
-offset
);
275 struct address_space_operations smb_file_aops
= {
276 readpage
: smb_readpage
,
277 writepage
: smb_writepage
,
278 prepare_write
: smb_prepare_write
,
279 commit_write
: smb_commit_write
283 * Write to a file (through the page cache).
286 smb_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
288 struct dentry
* dentry
= file
->f_dentry
;
291 VERBOSE("file %s/%s, count=%lu@%lu\n",
293 (unsigned long) count
, (unsigned long) *ppos
);
295 result
= smb_revalidate_inode(dentry
);
298 PARANOIA("%s/%s validation failed, error=%Zd\n",
299 DENTRY_PATH(dentry
), result
);
303 result
= smb_open(dentry
, SMB_O_WRONLY
);
309 result
= generic_file_write(file
, buf
, count
, ppos
);
310 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
311 (long) file
->f_pos
, (long) dentry
->d_inode
->i_size
,
312 dentry
->d_inode
->i_mtime
, dentry
->d_inode
->i_atime
);
319 smb_file_open(struct inode
*inode
, struct file
* file
)
322 struct dentry
*dentry
= file
->f_dentry
;
323 int smb_mode
= (file
->f_mode
& O_ACCMODE
) - 1;
326 result
= smb_open(dentry
, smb_mode
);
329 inode
->u
.smbfs_i
.openers
++;
336 smb_file_release(struct inode
*inode
, struct file
* file
)
339 if (!--inode
->u
.smbfs_i
.openers
)
346 * Check whether the required access is compatible with
347 * an inode's permission. SMB doesn't recognize superuser
348 * privileges, so we need our own check for this.
351 smb_file_permission(struct inode
*inode
, int mask
)
353 int mode
= inode
->i_mode
;
356 VERBOSE("mode=%x, mask=%x\n", mode
, mask
);
358 /* Look at user permissions */
360 if ((mode
& 7 & mask
) != mask
)
365 struct file_operations smb_file_operations
=
368 write
: smb_file_write
,
372 release
: smb_file_release
,
376 struct inode_operations smb_file_inode_operations
=
378 permission
: smb_file_permission
,
379 revalidate
: smb_revalidate_inode
,
380 setattr
: smb_notify_change
,