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/time.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fcntl.h>
14 #include <linux/stat.h>
16 #include <linux/pagemap.h>
17 #include <linux/smp_lock.h>
18 #include <linux/net.h>
19 #include <linux/aio.h>
21 #include <asm/uaccess.h>
22 #include <asm/system.h>
24 #include <linux/smbno.h>
25 #include <linux/smb_fs.h>
27 #include "smb_debug.h"
31 smb_fsync(struct file
*file
, struct dentry
* dentry
, int datasync
)
33 struct smb_sb_info
*server
= server_from_dentry(dentry
);
36 VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry
));
39 * The VFS will writepage() all dirty pages for us, but we
40 * should send a SMBflush to the server, letting it know that
41 * we want things synchronized with actual storage.
43 * Note: this function requires all pages to have been written already
44 * (should be ok with writepage_sync)
46 result
= smb_proc_flush(server
, SMB_I(dentry
->d_inode
)->fileid
);
51 * Read a page synchronously.
54 smb_readpage_sync(struct dentry
*dentry
, struct page
*page
)
56 char *buffer
= kmap(page
);
57 loff_t offset
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
58 struct smb_sb_info
*server
= server_from_dentry(dentry
);
59 unsigned int rsize
= smb_get_rsize(server
);
60 int count
= PAGE_SIZE
;
63 VERBOSE("file %s/%s, count=%d@%Ld, rsize=%d\n",
64 DENTRY_PATH(dentry
), count
, offset
, rsize
);
66 result
= smb_open(dentry
, SMB_O_RDONLY
);
74 result
= server
->ops
->read(dentry
->d_inode
,offset
,rsize
,buffer
);
81 dentry
->d_inode
->i_atime
=
82 current_fs_time(dentry
->d_inode
->i_sb
);
87 memset(buffer
, 0, count
);
88 flush_dcache_page(page
);
89 SetPageUptodate(page
);
99 * We are called with the page locked and we unlock it when done.
102 smb_readpage(struct file
*file
, struct page
*page
)
105 struct dentry
*dentry
= file
->f_path
.dentry
;
107 page_cache_get(page
);
108 error
= smb_readpage_sync(dentry
, page
);
109 page_cache_release(page
);
114 * Write a page synchronously.
115 * Offset is the data offset within the page.
118 smb_writepage_sync(struct inode
*inode
, struct page
*page
,
119 unsigned long pageoffset
, unsigned int count
)
122 char *buffer
= kmap(page
) + pageoffset
;
123 struct smb_sb_info
*server
= server_from_inode(inode
);
124 unsigned int wsize
= smb_get_wsize(server
);
127 offset
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + pageoffset
;
128 VERBOSE("file ino=%ld, fileid=%d, count=%d@%Ld, wsize=%d\n",
129 inode
->i_ino
, SMB_I(inode
)->fileid
, count
, offset
, wsize
);
137 write_ret
= server
->ops
->write(inode
, offset
, wsize
, buffer
);
139 PARANOIA("failed write, wsize=%d, write_ret=%d\n",
144 /* N.B. what if result < wsize?? */
145 #ifdef SMBFS_PARANOIA
146 if (write_ret
< wsize
)
147 PARANOIA("short write, wsize=%d, write_ret=%d\n",
154 * Update the inode now rather than waiting for a refresh.
156 inode
->i_mtime
= inode
->i_atime
= current_fs_time(inode
->i_sb
);
157 SMB_I(inode
)->flags
|= SMB_F_LOCALWRITE
;
158 if (offset
> inode
->i_size
)
159 inode
->i_size
= offset
;
167 * Write a page to the server. This will be used for NFS swapping only
168 * (for now), and we currently do this synchronously only.
170 * We are called with the page locked and we unlock it when done.
173 smb_writepage(struct page
*page
, struct writeback_control
*wbc
)
175 struct address_space
*mapping
= page
->mapping
;
177 unsigned long end_index
;
178 unsigned offset
= PAGE_CACHE_SIZE
;
182 inode
= mapping
->host
;
185 end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
188 if (page
->index
< end_index
)
190 /* things got complicated... */
191 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
192 /* OK, are we completely out? */
193 if (page
->index
>= end_index
+1 || !offset
)
194 return 0; /* truncated - don't care */
196 page_cache_get(page
);
197 err
= smb_writepage_sync(inode
, page
, 0, offset
);
198 SetPageUptodate(page
);
200 page_cache_release(page
);
205 smb_updatepage(struct file
*file
, struct page
*page
, unsigned long offset
,
208 struct dentry
*dentry
= file
->f_path
.dentry
;
210 DEBUG1("(%s/%s %d@%lld)\n", DENTRY_PATH(dentry
), count
,
211 ((unsigned long long)page
->index
<< PAGE_CACHE_SHIFT
) + offset
);
213 return smb_writepage_sync(dentry
->d_inode
, page
, offset
, count
);
217 smb_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
218 unsigned long nr_segs
, loff_t pos
)
220 struct file
* file
= iocb
->ki_filp
;
221 struct dentry
* dentry
= file
->f_path
.dentry
;
224 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry
),
225 (unsigned long) iocb
->ki_left
, (unsigned long) pos
);
227 status
= smb_revalidate_inode(dentry
);
229 PARANOIA("%s/%s validation failed, error=%Zd\n",
230 DENTRY_PATH(dentry
), status
);
234 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
235 (long)dentry
->d_inode
->i_size
,
236 dentry
->d_inode
->i_flags
, dentry
->d_inode
->i_atime
.tv_sec
);
238 status
= generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
244 smb_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
246 struct dentry
* dentry
= file
->f_path
.dentry
;
249 VERBOSE("file %s/%s, address %lu - %lu\n",
250 DENTRY_PATH(dentry
), vma
->vm_start
, vma
->vm_end
);
252 status
= smb_revalidate_inode(dentry
);
254 PARANOIA("%s/%s validation failed, error=%d\n",
255 DENTRY_PATH(dentry
), status
);
258 status
= generic_file_mmap(file
, vma
);
264 smb_file_splice_read(struct file
*file
, loff_t
*ppos
,
265 struct pipe_inode_info
*pipe
, size_t count
,
268 struct dentry
*dentry
= file
->f_path
.dentry
;
271 VERBOSE("file %s/%s, pos=%Ld, count=%lu\n",
272 DENTRY_PATH(dentry
), *ppos
, count
);
274 status
= smb_revalidate_inode(dentry
);
276 PARANOIA("%s/%s validation failed, error=%Zd\n",
277 DENTRY_PATH(dentry
), status
);
280 status
= generic_file_splice_read(file
, ppos
, pipe
, count
, flags
);
286 * This does the "real" work of the write. The generic routine has
287 * allocated the page, locked it, done all the page alignment stuff
288 * calculations etc. Now we should just copy the data from user
289 * space and write it back to the real medium..
291 * If the writer ends up delaying the write, the writer needs to
292 * increment the page use counts until he is done with the page.
294 static int smb_write_begin(struct file
*file
, struct address_space
*mapping
,
295 loff_t pos
, unsigned len
, unsigned flags
,
296 struct page
**pagep
, void **fsdata
)
298 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
299 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
305 static int smb_write_end(struct file
*file
, struct address_space
*mapping
,
306 loff_t pos
, unsigned len
, unsigned copied
,
307 struct page
*page
, void *fsdata
)
310 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
313 status
= smb_updatepage(file
, page
, offset
, copied
);
317 if (!PageUptodate(page
) && copied
== PAGE_CACHE_SIZE
)
318 SetPageUptodate(page
);
323 page_cache_release(page
);
328 const struct address_space_operations smb_file_aops
= {
329 .readpage
= smb_readpage
,
330 .writepage
= smb_writepage
,
331 .write_begin
= smb_write_begin
,
332 .write_end
= smb_write_end
,
336 * Write to a file (through the page cache).
339 smb_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
340 unsigned long nr_segs
, loff_t pos
)
342 struct file
* file
= iocb
->ki_filp
;
343 struct dentry
* dentry
= file
->f_path
.dentry
;
346 VERBOSE("file %s/%s, count=%lu@%lu\n",
348 (unsigned long) iocb
->ki_left
, (unsigned long) pos
);
350 result
= smb_revalidate_inode(dentry
);
352 PARANOIA("%s/%s validation failed, error=%Zd\n",
353 DENTRY_PATH(dentry
), result
);
357 result
= smb_open(dentry
, SMB_O_WRONLY
);
361 if (iocb
->ki_left
> 0) {
362 result
= generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
363 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
364 (long) file
->f_pos
, (long) dentry
->d_inode
->i_size
,
365 dentry
->d_inode
->i_mtime
.tv_sec
,
366 dentry
->d_inode
->i_atime
.tv_sec
);
373 smb_file_open(struct inode
*inode
, struct file
* file
)
376 struct dentry
*dentry
= file
->f_path
.dentry
;
377 int smb_mode
= (file
->f_mode
& O_ACCMODE
) - 1;
380 result
= smb_open(dentry
, smb_mode
);
383 SMB_I(inode
)->openers
++;
390 smb_file_release(struct inode
*inode
, struct file
* file
)
393 if (!--SMB_I(inode
)->openers
) {
394 /* We must flush any dirty pages now as we won't be able to
395 write anything after close. mmap can trigger this.
396 "openers" should perhaps include mmap'ers ... */
397 filemap_write_and_wait(inode
->i_mapping
);
405 * Check whether the required access is compatible with
406 * an inode's permission. SMB doesn't recognize superuser
407 * privileges, so we need our own check for this.
410 smb_file_permission(struct inode
*inode
, int mask
)
412 int mode
= inode
->i_mode
;
415 VERBOSE("mode=%x, mask=%x\n", mode
, mask
);
417 /* Look at user permissions */
419 if (mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
))
424 static loff_t
smb_remote_llseek(struct file
*file
, loff_t offset
, int origin
)
428 ret
= generic_file_llseek_unlocked(file
, offset
, origin
);
433 const struct file_operations smb_file_operations
=
435 .llseek
= smb_remote_llseek
,
436 .read
= do_sync_read
,
437 .aio_read
= smb_file_aio_read
,
438 .write
= do_sync_write
,
439 .aio_write
= smb_file_aio_write
,
440 .unlocked_ioctl
= smb_ioctl
,
441 .mmap
= smb_file_mmap
,
442 .open
= smb_file_open
,
443 .release
= smb_file_release
,
445 .splice_read
= smb_file_splice_read
,
448 const struct inode_operations smb_file_inode_operations
=
450 .permission
= smb_file_permission
,
451 .getattr
= smb_getattr
,
452 .setattr
= smb_notify_change
,