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
);
53 PARANOIA("%s/%s open failed, error=%d\n",
54 DENTRY_PATH(dentry
), result
);
62 result
= smb_proc_read(dentry
, offset
, rsize
, buffer
);
69 dentry
->d_inode
->i_atime
= CURRENT_TIME
;
74 memset(buffer
, 0, count
);
75 flush_dcache_page(page
);
76 SetPageUptodate(page
);
85 * We are called with the page locked and the caller unlocks.
88 smb_readpage(struct file
*file
, struct page
*page
)
91 struct dentry
*dentry
= file
->f_dentry
;
93 DEBUG1("readpage %p\n", page_address(page
));
96 error
= smb_readpage_sync(dentry
, page
);
102 * Write a page synchronously.
103 * Offset is the data offset within the page.
106 smb_writepage_sync(struct dentry
*dentry
, struct page
*page
,
107 unsigned long offset
, unsigned int count
)
109 struct inode
*inode
= dentry
->d_inode
;
110 u8
*buffer
= page_address(page
) + offset
;
111 int wsize
= smb_get_wsize(server_from_dentry(dentry
));
112 int result
, written
= 0;
114 offset
+= page
->index
<< PAGE_CACHE_SHIFT
;
115 VERBOSE("file %s/%s, count=%d@%ld, wsize=%d\n",
116 DENTRY_PATH(dentry
), count
, offset
, wsize
);
122 result
= smb_proc_write(dentry
, offset
, wsize
, buffer
);
125 /* N.B. what if result < wsize?? */
126 #ifdef SMBFS_PARANOIA
128 PARANOIA("short write, wsize=%d, result=%d\n",
136 * Update the inode now rather than waiting for a refresh.
138 inode
->i_mtime
= inode
->i_atime
= CURRENT_TIME
;
139 if (offset
> inode
->i_size
)
140 inode
->i_size
= offset
;
141 inode
->u
.smbfs_i
.cache_valid
|= SMB_F_LOCALWRITE
;
143 return written
? written
: result
;
147 * Write a page to the server. This will be used for NFS swapping only
148 * (for now), and we currently do this synchronously only.
150 * We are called with the page locked and the caller unlocks.
153 smb_writepage(struct file
*file
, struct page
*page
)
155 struct dentry
*dentry
= file
->f_dentry
;
156 struct inode
*inode
= dentry
->d_inode
;
157 unsigned long end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
158 unsigned offset
= PAGE_CACHE_SIZE
;
162 if (page
->index
< end_index
)
164 /* things got complicated... */
165 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
166 /* OK, are we completely out? */
167 if (page
->index
>= end_index
+1 || !offset
)
171 err
= smb_writepage_sync(dentry
, page
, 0, offset
);
172 SetPageUptodate(page
);
178 smb_updatepage(struct file
*file
, struct page
*page
, unsigned long offset
,
181 struct dentry
*dentry
= file
->f_dentry
;
183 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry
),
184 count
, (page
->index
<< PAGE_CACHE_SHIFT
)+offset
);
186 return smb_writepage_sync(dentry
, page
, offset
, count
);
190 smb_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
192 struct dentry
* dentry
= file
->f_dentry
;
195 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry
),
196 (unsigned long) count
, (unsigned long) *ppos
);
198 status
= smb_revalidate_inode(dentry
);
201 PARANOIA("%s/%s validation failed, error=%Zd\n",
202 DENTRY_PATH(dentry
), status
);
206 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
207 (long)dentry
->d_inode
->i_size
,
208 dentry
->d_inode
->i_flags
, dentry
->d_inode
->i_atime
);
210 status
= generic_file_read(file
, buf
, count
, ppos
);
216 smb_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
218 struct dentry
* dentry
= file
->f_dentry
;
221 VERBOSE("file %s/%s, address %lu - %lu\n",
222 DENTRY_PATH(dentry
), vma
->vm_start
, vma
->vm_end
);
224 status
= smb_revalidate_inode(dentry
);
227 PARANOIA("%s/%s validation failed, error=%d\n",
228 DENTRY_PATH(dentry
), status
);
231 status
= generic_file_mmap(file
, vma
);
237 * This does the "real" work of the write. The generic routine has
238 * allocated the page, locked it, done all the page alignment stuff
239 * calculations etc. Now we should just copy the data from user
240 * space and write it back to the real medium..
242 * If the writer ends up delaying the write, the writer needs to
243 * increment the page use counts until he is done with the page.
245 static int smb_prepare_write(struct file
*file
, struct page
*page
,
246 unsigned offset
, unsigned to
)
252 static int smb_commit_write(struct file
*file
, struct page
*page
,
253 unsigned offset
, unsigned to
)
259 status
= smb_updatepage(file
, page
, offset
, to
-offset
);
265 struct address_space_operations smb_file_aops
= {
266 readpage
: smb_readpage
,
267 writepage
: smb_writepage
,
268 prepare_write
: smb_prepare_write
,
269 commit_write
: smb_commit_write
273 * Write to a file (through the page cache).
276 smb_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
278 struct dentry
* dentry
= file
->f_dentry
;
281 VERBOSE("file %s/%s, count=%lu@%lu\n",
283 (unsigned long) count
, (unsigned long) *ppos
);
285 result
= smb_revalidate_inode(dentry
);
288 PARANOIA("%s/%s validation failed, error=%Zd\n",
289 DENTRY_PATH(dentry
), result
);
293 result
= smb_open(dentry
, SMB_O_WRONLY
);
299 result
= generic_file_write(file
, buf
, count
, ppos
);
300 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
301 (long) file
->f_pos
, (long) dentry
->d_inode
->i_size
,
302 dentry
->d_inode
->i_mtime
, dentry
->d_inode
->i_atime
);
309 smb_file_open(struct inode
*inode
, struct file
* file
)
312 inode
->u
.smbfs_i
.openers
++;
318 smb_file_release(struct inode
*inode
, struct file
* file
)
321 if (!--inode
->u
.smbfs_i
.openers
)
328 * Check whether the required access is compatible with
329 * an inode's permission. SMB doesn't recognize superuser
330 * privileges, so we need our own check for this.
333 smb_file_permission(struct inode
*inode
, int mask
)
335 int mode
= inode
->i_mode
;
338 VERBOSE("mode=%x, mask=%x\n", mode
, mask
);
340 /* Look at user permissions */
342 if ((mode
& 7 & mask
) != mask
)
347 struct file_operations smb_file_operations
=
350 write
: smb_file_write
,
354 release
: smb_file_release
,
358 struct inode_operations smb_file_inode_operations
=
360 permission
: smb_file_permission
,
361 revalidate
: smb_revalidate_inode
,
362 setattr
: smb_notify_change
,