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>
18 #include <asm/uaccess.h>
19 #include <asm/system.h>
21 #include <linux/smbno.h>
22 #include <linux/smb_fs.h>
24 #define SMBFS_PARANOIA 1
25 /* #define SMBFS_DEBUG_VERBOSE 1 */
26 /* #define pr_debug printk */
35 smb_unlock_page(struct page
*page
)
37 clear_bit(PG_locked
, &page
->flags
);
42 smb_fsync(struct file
*file
, struct dentry
* dentry
)
44 #ifdef SMBFS_DEBUG_VERBOSE
45 printk("smb_fsync: sync file %s/%s\n",
46 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
52 * Read a page synchronously.
55 smb_readpage_sync(struct dentry
*dentry
, struct page
*page
)
57 char *buffer
= (char *) page_address(page
);
58 unsigned long offset
= page
->offset
;
59 int rsize
= smb_get_rsize(server_from_dentry(dentry
));
60 int count
= PAGE_SIZE
;
63 clear_bit(PG_error
, &page
->flags
);
65 #ifdef SMBFS_DEBUG_VERBOSE
66 printk("smb_readpage_sync: file %s/%s, count=%d@%ld, rsize=%d\n",
67 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, count
, offset
, rsize
);
69 result
= smb_open(dentry
, SMB_O_RDONLY
);
73 printk("smb_readpage_sync: %s/%s open failed, error=%d\n",
74 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, result
);
83 result
= smb_proc_read(dentry
, offset
, rsize
, buffer
);
90 dentry
->d_inode
->i_atime
= CURRENT_TIME
;
95 memset(buffer
, 0, count
);
96 set_bit(PG_uptodate
, &page
->flags
);
100 smb_unlock_page(page
);
105 smb_readpage(struct file
*file
, struct page
*page
)
107 struct dentry
*dentry
= file
->f_dentry
;
110 pr_debug("SMB: smb_readpage %08lx\n", page_address(page
));
111 #ifdef SMBFS_PARANOIA
112 if (test_bit(PG_locked
, &page
->flags
))
113 printk("smb_readpage: page already locked!\n");
115 set_bit(PG_locked
, &page
->flags
);
116 atomic_inc(&page
->count
);
117 error
= smb_readpage_sync(dentry
, page
);
118 free_page(page_address(page
));
123 * Write a page synchronously.
124 * Offset is the data offset within the page.
127 smb_writepage_sync(struct dentry
*dentry
, struct page
*page
,
128 unsigned long offset
, unsigned int count
)
130 struct inode
*inode
= dentry
->d_inode
;
131 u8
*buffer
= (u8
*) page_address(page
) + offset
;
132 int wsize
= smb_get_wsize(server_from_dentry(dentry
));
133 int result
, written
= 0;
135 offset
+= page
->offset
;
136 #ifdef SMBFS_DEBUG_VERBOSE
137 printk("smb_writepage_sync: file %s/%s, count=%d@%ld, wsize=%d\n",
138 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, count
, offset
, wsize
);
145 result
= smb_proc_write(dentry
, offset
, wsize
, buffer
);
148 /* N.B. what if result < wsize?? */
149 #ifdef SMBFS_PARANOIA
151 printk("smb_writepage_sync: short write, wsize=%d, result=%d\n", wsize
, result
);
158 * Update the inode now rather than waiting for a refresh.
160 inode
->i_mtime
= inode
->i_atime
= CURRENT_TIME
;
161 if (offset
> inode
->i_size
)
162 inode
->i_size
= offset
;
163 inode
->u
.smbfs_i
.cache_valid
|= SMB_F_LOCALWRITE
;
167 smb_unlock_page(page
);
168 return written
? written
: result
;
171 /* Must mark the page invalid after I/O error */
172 clear_bit(PG_uptodate
, &page
->flags
);
177 * Write a page to the server. This will be used for NFS swapping only
178 * (for now), and we currently do this synchronously only.
181 smb_writepage(struct file
*file
, struct page
*page
)
183 struct dentry
*dentry
= file
->f_dentry
;
186 #ifdef SMBFS_PARANOIA
187 if (test_bit(PG_locked
, &page
->flags
))
188 printk("smb_writepage: page already locked!\n");
190 set_bit(PG_locked
, &page
->flags
);
191 atomic_inc(&page
->count
);
192 result
= smb_writepage_sync(dentry
, page
, 0, PAGE_SIZE
);
193 free_page(page_address(page
));
198 smb_updatepage(struct file
*file
, struct page
*page
, const char *buffer
,
199 unsigned long offset
, unsigned int count
, int sync
)
201 struct dentry
*dentry
= file
->f_dentry
;
202 unsigned long page_addr
= page_address(page
);
205 pr_debug("SMBFS: smb_updatepage(%s/%s %d@%ld, sync=%d)\n",
206 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
207 count
, page
->offset
+offset
, sync
);
209 #ifdef SMBFS_PARANOIA
210 if (test_bit(PG_locked
, &page
->flags
))
211 printk("smb_updatepage: page already locked!\n");
213 set_bit(PG_locked
, &page
->flags
);
214 atomic_inc(&page
->count
);
216 if (copy_from_user((char *) page_addr
+ offset
, buffer
, count
))
218 result
= smb_writepage_sync(dentry
, page
, offset
, count
);
220 free_page(page_addr
);
224 #ifdef SMBFS_PARANOIA
225 printk("smb_updatepage: fault at addr=%lu, offset=%lu, buffer=%p\n",
226 page_addr
, offset
, buffer
);
229 clear_bit(PG_uptodate
, &page
->flags
);
230 smb_unlock_page(page
);
235 smb_file_read(struct file
* file
, char * buf
, size_t count
, loff_t
*ppos
)
237 struct dentry
* dentry
= file
->f_dentry
;
240 #ifdef SMBFS_DEBUG_VERBOSE
241 printk("smb_file_read: file %s/%s, count=%lu@%lu\n",
242 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
243 (unsigned long) count
, (unsigned long) *ppos
);
246 status
= smb_revalidate_inode(dentry
);
249 #ifdef SMBFS_PARANOIA
250 printk("smb_file_read: %s/%s validation failed, error=%d\n",
251 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, status
);
256 #ifdef SMBFS_DEBUG_VERBOSE
257 printk("smb_file_read: before read, size=%ld, pages=%ld, flags=%x, atime=%ld\n",
258 dentry
->d_inode
->i_size
, dentry
->d_inode
->i_nrpages
, dentry
->d_inode
->i_flags
,
259 dentry
->d_inode
->i_atime
);
261 status
= generic_file_read(file
, buf
, count
, ppos
);
267 smb_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
269 struct dentry
* dentry
= file
->f_dentry
;
272 #ifdef SMBFS_DEBUG_VERBOSE
273 printk("smb_file_mmap: file %s/%s, address %lu - %lu\n",
274 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, vma
->vm_start
, vma
->vm_end
);
277 status
= smb_revalidate_inode(dentry
);
280 #ifdef SMBFS_PARANOIA
281 printk("smb_file_mmap: %s/%s validation failed, error=%d\n",
282 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, status
);
286 status
= generic_file_mmap(file
, vma
);
292 * Write to a file (through the page cache).
295 smb_file_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
297 struct dentry
* dentry
= file
->f_dentry
;
300 #ifdef SMBFS_DEBUG_VERBOSE
301 printk("smb_file_write: file %s/%s, count=%lu@%lu, pages=%ld\n",
302 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
303 (unsigned long) count
, (unsigned long) *ppos
, dentry
->d_inode
->i_nrpages
);
306 result
= smb_revalidate_inode(dentry
);
309 #ifdef SMBFS_PARANOIA
310 printk("smb_file_write: %s/%s validation failed, error=%d\n",
311 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, result
);
316 result
= smb_open(dentry
, SMB_O_WRONLY
);
322 result
= generic_file_write(file
, buf
, count
, ppos
);
323 #ifdef SMBFS_DEBUG_VERBOSE
324 printk("smb_file_write: pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
325 (long) file
->f_pos
, dentry
->d_inode
->i_size
, dentry
->d_inode
->i_mtime
,
326 dentry
->d_inode
->i_atime
);
334 smb_file_open(struct inode
*inode
, struct file
* file
)
336 #ifdef SMBFS_DEBUG_VERBOSE
337 printk("smb_file_open: opening %s/%s, d_count=%d\n",
338 file
->f_dentry
->d_parent
->d_name
.name
, file
->f_dentry
->d_name
.name
,
339 file
->f_dentry
->d_count
);
345 smb_file_release(struct inode
*inode
, struct file
* file
)
347 struct dentry
* dentry
= file
->f_dentry
;
349 #ifdef SMBFS_DEBUG_VERBOSE
350 printk("smb_file_release: closing %s/%s, d_count=%d\n",
351 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, dentry
->d_count
);
354 if (dentry
->d_count
== 1)
362 * Check whether the required access is compatible with
363 * an inode's permission. SMB doesn't recognize superuser
364 * privileges, so we need our own check for this.
367 smb_file_permission(struct inode
*inode
, int mask
)
369 int mode
= inode
->i_mode
;
372 #ifdef SMBFS_DEBUG_VERBOSE
373 printk("smb_file_permission: mode=%x, mask=%x\n", mode
, mask
);
375 /* Look at user permissions */
377 if ((mode
& 7 & mask
) != mask
)
382 static struct file_operations smb_file_operations
=
384 NULL
, /* lseek - default */
385 smb_file_read
, /* read */
386 smb_file_write
, /* write */
387 NULL
, /* readdir - bad */
388 NULL
, /* poll - default */
389 smb_ioctl
, /* ioctl */
390 smb_file_mmap
, /* mmap(struct file*, struct vm_area_struct*) */
391 smb_file_open
, /* open(struct inode*, struct file*) */
393 smb_file_release
, /* release(struct inode*, struct file*) */
394 smb_fsync
, /* fsync(struct file*, struct dentry*) */
395 NULL
, /* fasync(struct file*, int) */
396 NULL
, /* check_media_change(kdev_t dev) */
397 NULL
, /* revalidate(kdev_t dev) */
398 NULL
/* lock(struct file*, int, struct file_lock*) */
401 struct inode_operations smb_file_inode_operations
=
403 &smb_file_operations
, /* default file operations */
414 NULL
, /* follow_link */
415 smb_readpage
, /* readpage */
416 smb_writepage
, /* writepage */
419 smb_file_permission
, /* permission */
421 smb_updatepage
, /* updatepage */
422 smb_revalidate_inode
, /* revalidate */