4 * Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/fcntl.h>
13 #include <linux/stat.h>
15 #include <linux/malloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/smp_lock.h>
19 #include <asm/uaccess.h>
20 #include <asm/system.h>
22 #include <linux/smbno.h>
23 #include <linux/smb_fs.h>
25 #define SMBFS_PARANOIA 1
26 /* #define SMBFS_DEBUG_VERBOSE 1 */
27 /* #define pr_debug printk */
36 smb_fsync(struct file
*file
, struct dentry
* dentry
)
38 #ifdef SMBFS_DEBUG_VERBOSE
39 printk("smb_fsync: sync file %s/%s\n",
40 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
46 * Read a page synchronously.
49 smb_readpage_sync(struct dentry
*dentry
, struct page
*page
)
51 char *buffer
= (char *) page_address(page
);
52 unsigned long offset
= page
->index
<< PAGE_CACHE_SHIFT
;
53 int rsize
= smb_get_rsize(server_from_dentry(dentry
));
54 int count
= PAGE_SIZE
;
57 /* We can't replace this with ClearPageError. why? is it a problem?
58 fs/buffer.c:brw_page does the same. */
59 /* clear_bit(PG_error, &page->flags); */
61 #ifdef SMBFS_DEBUG_VERBOSE
62 printk("smb_readpage_sync: file %s/%s, count=%d@%ld, rsize=%d\n",
63 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, count
, offset
, rsize
);
65 result
= smb_open(dentry
, SMB_O_RDONLY
);
69 printk("smb_readpage_sync: %s/%s open failed, error=%d\n",
70 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, result
);
79 result
= smb_proc_read(dentry
, offset
, rsize
, buffer
);
86 dentry
->d_inode
->i_atime
= CURRENT_TIME
;
91 memset(buffer
, 0, count
);
92 SetPageUptodate(page
);
101 smb_readpage(struct dentry
*dentry
, struct page
*page
)
105 pr_debug("SMB: smb_readpage %08lx\n", page_address(page
));
106 #ifdef SMBFS_PARANOIA
107 if (!PageLocked(page
))
108 printk("smb_readpage: page not already locked!\n");
112 error
= smb_readpage_sync(dentry
, page
);
118 * Write a page synchronously.
119 * Offset is the data offset within the page.
122 smb_writepage_sync(struct dentry
*dentry
, struct page
*page
,
123 unsigned long offset
, unsigned int count
)
125 struct inode
*inode
= dentry
->d_inode
;
126 u8
*buffer
= (u8
*) page_address(page
) + offset
;
127 int wsize
= smb_get_wsize(server_from_dentry(dentry
));
128 int result
, written
= 0;
130 offset
+= page
->index
<< PAGE_CACHE_SHIFT
;
131 #ifdef SMBFS_DEBUG_VERBOSE
132 printk("smb_writepage_sync: file %s/%s, count=%d@%ld, wsize=%d\n",
133 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, count
, offset
, wsize
);
140 result
= smb_proc_write(dentry
, offset
, wsize
, buffer
);
143 /* N.B. what if result < wsize?? */
144 #ifdef SMBFS_PARANOIA
146 printk("smb_writepage_sync: short write, wsize=%d, result=%d\n", wsize
, result
);
153 * Update the inode now rather than waiting for a refresh.
155 inode
->i_mtime
= inode
->i_atime
= CURRENT_TIME
;
156 if (offset
> inode
->i_size
)
157 inode
->i_size
= offset
;
158 inode
->u
.smbfs_i
.cache_valid
|= SMB_F_LOCALWRITE
;
160 return written
? written
: result
;
164 * Write a page to the server. This will be used for NFS swapping only
165 * (for now), and we currently do this synchronously only.
167 * We are called with the page locked and the caller unlocks.
170 smb_writepage(struct dentry
*dentry
, struct page
*page
)
174 #ifdef SMBFS_PARANOIA
175 if (!PageLocked(page
))
176 printk("smb_writepage: page not already locked!\n");
179 result
= smb_writepage_sync(dentry
, page
, 0, PAGE_SIZE
);
180 SetPageUptodate(page
);
186 smb_updatepage(struct file
*file
, struct page
*page
, unsigned long offset
, unsigned int count
)
188 struct dentry
*dentry
= file
->f_dentry
;
190 pr_debug("SMBFS: smb_updatepage(%s/%s %d@%ld)\n",
191 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
192 count
, (page
->index
<< PAGE_CACHE_SHIFT
)+offset
);
194 return smb_writepage_sync(dentry
, page
, offset
, count
);
198 smb_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
200 struct dentry
* dentry
= file
->f_dentry
;
203 #ifdef SMBFS_DEBUG_VERBOSE
204 printk("smb_file_read: file %s/%s, count=%lu@%lu\n",
205 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
206 (unsigned long) count
, (unsigned long) *ppos
);
209 status
= smb_revalidate_inode(dentry
);
212 #ifdef SMBFS_PARANOIA
213 printk("smb_file_read: %s/%s validation failed, error=%d\n",
214 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, status
);
219 #ifdef SMBFS_DEBUG_VERBOSE
220 printk("smb_file_read: before read, size=%ld, pages=%ld, flags=%x, atime=%ld\n",
221 dentry
->d_inode
->i_size
, dentry
->d_inode
->i_nrpages
, dentry
->d_inode
->i_flags
,
222 dentry
->d_inode
->i_atime
);
224 status
= generic_file_read(file
, buf
, count
, ppos
);
230 smb_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
232 struct dentry
* dentry
= file
->f_dentry
;
235 #ifdef SMBFS_DEBUG_VERBOSE
236 printk("smb_file_mmap: file %s/%s, address %lu - %lu\n",
237 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, vma
->vm_start
, vma
->vm_end
);
240 status
= smb_revalidate_inode(dentry
);
243 #ifdef SMBFS_PARANOIA
244 printk("smb_file_mmap: %s/%s validation failed, error=%d\n",
245 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, status
);
249 status
= generic_file_mmap(file
, vma
);
255 * This does the "real" work of the write. The generic routine has
256 * allocated the page, locked it, done all the page alignment stuff
257 * calculations etc. Now we should just copy the data from user
258 * space and write it back to the real medium..
260 * If the writer ends up delaying the write, the writer needs to
261 * increment the page use counts until he is done with the page.
263 static int smb_write_one_page(struct file
*file
, struct page
*page
, unsigned long offset
, unsigned long bytes
, const char * buf
)
267 bytes
-= copy_from_user((u8
*)page_address(page
) + offset
, buf
, bytes
);
271 status
= smb_updatepage(file
, page
, offset
, bytes
);
278 * Write to a file (through the page cache).
281 smb_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
283 struct dentry
* dentry
= file
->f_dentry
;
286 #ifdef SMBFS_DEBUG_VERBOSE
287 printk("smb_file_write: file %s/%s, count=%lu@%lu, pages=%ld\n",
288 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
289 (unsigned long) count
, (unsigned long) *ppos
, dentry
->d_inode
->i_nrpages
);
292 result
= smb_revalidate_inode(dentry
);
295 #ifdef SMBFS_PARANOIA
296 printk("smb_file_write: %s/%s validation failed, error=%d\n",
297 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, result
);
302 result
= smb_open(dentry
, SMB_O_WRONLY
);
308 result
= generic_file_write(file
, buf
, count
, ppos
, smb_write_one_page
);
309 #ifdef SMBFS_DEBUG_VERBOSE
310 printk("smb_file_write: pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
311 (long) file
->f_pos
, dentry
->d_inode
->i_size
, dentry
->d_inode
->i_mtime
,
312 dentry
->d_inode
->i_atime
);
320 smb_file_open(struct inode
*inode
, struct file
* file
)
322 #ifdef SMBFS_DEBUG_VERBOSE
323 printk("smb_file_open: opening %s/%s, d_count=%d\n",
324 file
->f_dentry
->d_parent
->d_name
.name
, file
->f_dentry
->d_name
.name
,
325 file
->f_dentry
->d_count
);
331 smb_file_release(struct inode
*inode
, struct file
* file
)
333 struct dentry
* dentry
= file
->f_dentry
;
335 #ifdef SMBFS_DEBUG_VERBOSE
336 printk("smb_file_release: closing %s/%s, d_count=%d\n",
337 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
340 if (dentry
->d_count
== 1)
348 * Check whether the required access is compatible with
349 * an inode's permission. SMB doesn't recognize superuser
350 * privileges, so we need our own check for this.
353 smb_file_permission(struct inode
*inode
, int mask
)
355 int mode
= inode
->i_mode
;
358 #ifdef SMBFS_DEBUG_VERBOSE
359 printk("smb_file_permission: mode=%x, mask=%x\n", mode
, mask
);
361 /* Look at user permissions */
363 if ((mode
& 7 & mask
) != mask
)
368 static struct file_operations smb_file_operations
=
370 NULL
, /* lseek - default */
371 smb_file_read
, /* read */
372 smb_file_write
, /* write */
373 NULL
, /* readdir - bad */
374 NULL
, /* poll - default */
375 smb_ioctl
, /* ioctl */
376 smb_file_mmap
, /* mmap(struct file*, struct vm_area_struct*) */
377 smb_file_open
, /* open(struct inode*, struct file*) */
379 smb_file_release
, /* release(struct inode*, struct file*) */
380 smb_fsync
, /* fsync(struct file*, struct dentry*) */
381 NULL
, /* fasync(struct file*, int) */
382 NULL
, /* check_media_change(kdev_t dev) */
383 NULL
, /* revalidate(kdev_t dev) */
384 NULL
/* lock(struct file*, int, struct file_lock*) */
387 struct inode_operations smb_file_inode_operations
=
389 &smb_file_operations
, /* default file operations */
400 NULL
, /* follow_link */
401 NULL
, /* get_block */
402 smb_readpage
, /* readpage */
403 smb_writepage
, /* writepage */
405 smb_file_permission
, /* permission */
406 smb_revalidate_inode
, /* revalidate */