Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file.c |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke |
| 5 | * Copyright (C) 1997 by Volker Lendecke |
| 6 | * |
| 7 | * Please add a note about your changes to smbfs in the ChangeLog file. |
| 8 | */ |
| 9 | |
| 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> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/smp_lock.h> |
| 19 | #include <linux/net.h> |
| 20 | |
| 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/system.h> |
| 23 | |
| 24 | #include <linux/smbno.h> |
| 25 | #include <linux/smb_fs.h> |
| 26 | |
| 27 | #include "smb_debug.h" |
| 28 | #include "proto.h" |
| 29 | |
| 30 | static int |
| 31 | smb_fsync(struct file *file, struct dentry * dentry, int datasync) |
| 32 | { |
| 33 | struct smb_sb_info *server = server_from_dentry(dentry); |
| 34 | int result; |
| 35 | |
| 36 | VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry)); |
| 37 | |
| 38 | /* |
| 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. |
| 42 | * |
| 43 | * Note: this function requires all pages to have been written already |
| 44 | * (should be ok with writepage_sync) |
| 45 | */ |
| 46 | result = smb_proc_flush(server, SMB_I(dentry->d_inode)->fileid); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Read a page synchronously. |
| 52 | */ |
| 53 | static int |
| 54 | smb_readpage_sync(struct dentry *dentry, struct page *page) |
| 55 | { |
| 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; |
| 61 | int result; |
| 62 | |
| 63 | VERBOSE("file %s/%s, count=%d@%Ld, rsize=%d\n", |
| 64 | DENTRY_PATH(dentry), count, offset, rsize); |
| 65 | |
| 66 | result = smb_open(dentry, SMB_O_RDONLY); |
| 67 | if (result < 0) |
| 68 | goto io_error; |
| 69 | |
| 70 | do { |
| 71 | if (count < rsize) |
| 72 | rsize = count; |
| 73 | |
| 74 | result = server->ops->read(dentry->d_inode,offset,rsize,buffer); |
| 75 | if (result < 0) |
| 76 | goto io_error; |
| 77 | |
| 78 | count -= result; |
| 79 | offset += result; |
| 80 | buffer += result; |
| 81 | dentry->d_inode->i_atime = |
| 82 | current_fs_time(dentry->d_inode->i_sb); |
| 83 | if (result < rsize) |
| 84 | break; |
| 85 | } while (count); |
| 86 | |
| 87 | memset(buffer, 0, count); |
| 88 | flush_dcache_page(page); |
| 89 | SetPageUptodate(page); |
| 90 | result = 0; |
| 91 | |
| 92 | io_error: |
| 93 | kunmap(page); |
| 94 | unlock_page(page); |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * We are called with the page locked and we unlock it when done. |
| 100 | */ |
| 101 | static int |
| 102 | smb_readpage(struct file *file, struct page *page) |
| 103 | { |
| 104 | int error; |
| 105 | struct dentry *dentry = file->f_dentry; |
| 106 | |
| 107 | page_cache_get(page); |
| 108 | error = smb_readpage_sync(dentry, page); |
| 109 | page_cache_release(page); |
| 110 | return error; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Write a page synchronously. |
| 115 | * Offset is the data offset within the page. |
| 116 | */ |
| 117 | static int |
| 118 | smb_writepage_sync(struct inode *inode, struct page *page, |
| 119 | unsigned long pageoffset, unsigned int count) |
| 120 | { |
| 121 | loff_t offset; |
| 122 | char *buffer = kmap(page) + pageoffset; |
| 123 | struct smb_sb_info *server = server_from_inode(inode); |
| 124 | unsigned int wsize = smb_get_wsize(server); |
| 125 | int ret = 0; |
| 126 | |
| 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); |
| 130 | |
| 131 | do { |
| 132 | int write_ret; |
| 133 | |
| 134 | if (count < wsize) |
| 135 | wsize = count; |
| 136 | |
| 137 | write_ret = server->ops->write(inode, offset, wsize, buffer); |
| 138 | if (write_ret < 0) { |
| 139 | PARANOIA("failed write, wsize=%d, write_ret=%d\n", |
| 140 | wsize, write_ret); |
| 141 | ret = write_ret; |
| 142 | break; |
| 143 | } |
| 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", |
| 148 | wsize, write_ret); |
| 149 | #endif |
| 150 | buffer += wsize; |
| 151 | offset += wsize; |
| 152 | count -= wsize; |
| 153 | /* |
| 154 | * Update the inode now rather than waiting for a refresh. |
| 155 | */ |
| 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; |
| 160 | } while (count); |
| 161 | |
| 162 | kunmap(page); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | /* |
| 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. |
| 169 | * |
| 170 | * We are called with the page locked and we unlock it when done. |
| 171 | */ |
| 172 | static int |
| 173 | smb_writepage(struct page *page, struct writeback_control *wbc) |
| 174 | { |
| 175 | struct address_space *mapping = page->mapping; |
| 176 | struct inode *inode; |
| 177 | unsigned long end_index; |
| 178 | unsigned offset = PAGE_CACHE_SIZE; |
| 179 | int err; |
| 180 | |
Eric Sesterhenn | 5df0d31 | 2006-04-01 01:16:26 +0200 | [diff] [blame] | 181 | BUG_ON(!mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | inode = mapping->host; |
Eric Sesterhenn | 5df0d31 | 2006-04-01 01:16:26 +0200 | [diff] [blame] | 183 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | end_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 186 | |
| 187 | /* easy case */ |
| 188 | if (page->index < end_index) |
| 189 | goto do_it; |
| 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 */ |
| 195 | do_it: |
| 196 | page_cache_get(page); |
| 197 | err = smb_writepage_sync(inode, page, 0, offset); |
| 198 | SetPageUptodate(page); |
| 199 | unlock_page(page); |
| 200 | page_cache_release(page); |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | smb_updatepage(struct file *file, struct page *page, unsigned long offset, |
| 206 | unsigned int count) |
| 207 | { |
| 208 | struct dentry *dentry = file->f_dentry; |
| 209 | |
Andrew Morton | 54b21a7 | 2006-01-08 01:03:05 -0800 | [diff] [blame] | 210 | DEBUG1("(%s/%s %d@%lld)\n", DENTRY_PATH(dentry), count, |
| 211 | ((unsigned long long)page->index << PAGE_CACHE_SHIFT) + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | return smb_writepage_sync(dentry->d_inode, page, offset, count); |
| 214 | } |
| 215 | |
| 216 | static ssize_t |
| 217 | smb_file_read(struct file * file, char __user * buf, size_t count, loff_t *ppos) |
| 218 | { |
| 219 | struct dentry * dentry = file->f_dentry; |
| 220 | ssize_t status; |
| 221 | |
| 222 | VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry), |
| 223 | (unsigned long) count, (unsigned long) *ppos); |
| 224 | |
| 225 | status = smb_revalidate_inode(dentry); |
| 226 | if (status) { |
| 227 | PARANOIA("%s/%s validation failed, error=%Zd\n", |
| 228 | DENTRY_PATH(dentry), status); |
| 229 | goto out; |
| 230 | } |
| 231 | |
| 232 | VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n", |
| 233 | (long)dentry->d_inode->i_size, |
| 234 | dentry->d_inode->i_flags, dentry->d_inode->i_atime); |
| 235 | |
| 236 | status = generic_file_read(file, buf, count, ppos); |
| 237 | out: |
| 238 | return status; |
| 239 | } |
| 240 | |
| 241 | static int |
| 242 | smb_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 243 | { |
| 244 | struct dentry * dentry = file->f_dentry; |
| 245 | int status; |
| 246 | |
| 247 | VERBOSE("file %s/%s, address %lu - %lu\n", |
| 248 | DENTRY_PATH(dentry), vma->vm_start, vma->vm_end); |
| 249 | |
| 250 | status = smb_revalidate_inode(dentry); |
| 251 | if (status) { |
| 252 | PARANOIA("%s/%s validation failed, error=%d\n", |
| 253 | DENTRY_PATH(dentry), status); |
| 254 | goto out; |
| 255 | } |
| 256 | status = generic_file_mmap(file, vma); |
| 257 | out: |
| 258 | return status; |
| 259 | } |
| 260 | |
| 261 | static ssize_t |
| 262 | smb_file_sendfile(struct file *file, loff_t *ppos, |
| 263 | size_t count, read_actor_t actor, void *target) |
| 264 | { |
| 265 | struct dentry *dentry = file->f_dentry; |
| 266 | ssize_t status; |
| 267 | |
| 268 | VERBOSE("file %s/%s, pos=%Ld, count=%d\n", |
| 269 | DENTRY_PATH(dentry), *ppos, count); |
| 270 | |
| 271 | status = smb_revalidate_inode(dentry); |
| 272 | if (status) { |
| 273 | PARANOIA("%s/%s validation failed, error=%Zd\n", |
| 274 | DENTRY_PATH(dentry), status); |
| 275 | goto out; |
| 276 | } |
| 277 | status = generic_file_sendfile(file, ppos, count, actor, target); |
| 278 | out: |
| 279 | return status; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * This does the "real" work of the write. The generic routine has |
| 284 | * allocated the page, locked it, done all the page alignment stuff |
| 285 | * calculations etc. Now we should just copy the data from user |
| 286 | * space and write it back to the real medium.. |
| 287 | * |
| 288 | * If the writer ends up delaying the write, the writer needs to |
| 289 | * increment the page use counts until he is done with the page. |
| 290 | */ |
| 291 | static int smb_prepare_write(struct file *file, struct page *page, |
| 292 | unsigned offset, unsigned to) |
| 293 | { |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int smb_commit_write(struct file *file, struct page *page, |
| 298 | unsigned offset, unsigned to) |
| 299 | { |
| 300 | int status; |
| 301 | |
| 302 | status = -EFAULT; |
| 303 | lock_kernel(); |
| 304 | status = smb_updatepage(file, page, offset, to-offset); |
| 305 | unlock_kernel(); |
| 306 | return status; |
| 307 | } |
| 308 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 309 | const struct address_space_operations smb_file_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | .readpage = smb_readpage, |
| 311 | .writepage = smb_writepage, |
| 312 | .prepare_write = smb_prepare_write, |
| 313 | .commit_write = smb_commit_write |
| 314 | }; |
| 315 | |
| 316 | /* |
| 317 | * Write to a file (through the page cache). |
| 318 | */ |
| 319 | static ssize_t |
| 320 | smb_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 321 | { |
| 322 | struct dentry * dentry = file->f_dentry; |
| 323 | ssize_t result; |
| 324 | |
| 325 | VERBOSE("file %s/%s, count=%lu@%lu\n", |
| 326 | DENTRY_PATH(dentry), |
| 327 | (unsigned long) count, (unsigned long) *ppos); |
| 328 | |
| 329 | result = smb_revalidate_inode(dentry); |
| 330 | if (result) { |
| 331 | PARANOIA("%s/%s validation failed, error=%Zd\n", |
| 332 | DENTRY_PATH(dentry), result); |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | result = smb_open(dentry, SMB_O_WRONLY); |
| 337 | if (result) |
| 338 | goto out; |
| 339 | |
| 340 | if (count > 0) { |
| 341 | result = generic_file_write(file, buf, count, ppos); |
| 342 | VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n", |
| 343 | (long) file->f_pos, (long) dentry->d_inode->i_size, |
| 344 | dentry->d_inode->i_mtime, dentry->d_inode->i_atime); |
| 345 | } |
| 346 | out: |
| 347 | return result; |
| 348 | } |
| 349 | |
| 350 | static int |
| 351 | smb_file_open(struct inode *inode, struct file * file) |
| 352 | { |
| 353 | int result; |
| 354 | struct dentry *dentry = file->f_dentry; |
| 355 | int smb_mode = (file->f_mode & O_ACCMODE) - 1; |
| 356 | |
| 357 | lock_kernel(); |
| 358 | result = smb_open(dentry, smb_mode); |
| 359 | if (result) |
| 360 | goto out; |
| 361 | SMB_I(inode)->openers++; |
| 362 | out: |
| 363 | unlock_kernel(); |
| 364 | return result; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | smb_file_release(struct inode *inode, struct file * file) |
| 369 | { |
| 370 | lock_kernel(); |
| 371 | if (!--SMB_I(inode)->openers) { |
| 372 | /* We must flush any dirty pages now as we won't be able to |
| 373 | write anything after close. mmap can trigger this. |
| 374 | "openers" should perhaps include mmap'ers ... */ |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 375 | filemap_write_and_wait(inode->i_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | smb_close(inode); |
| 377 | } |
| 378 | unlock_kernel(); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Check whether the required access is compatible with |
| 384 | * an inode's permission. SMB doesn't recognize superuser |
| 385 | * privileges, so we need our own check for this. |
| 386 | */ |
| 387 | static int |
| 388 | smb_file_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 389 | { |
| 390 | int mode = inode->i_mode; |
| 391 | int error = 0; |
| 392 | |
| 393 | VERBOSE("mode=%x, mask=%x\n", mode, mask); |
| 394 | |
| 395 | /* Look at user permissions */ |
| 396 | mode >>= 6; |
| 397 | if ((mode & 7 & mask) != mask) |
| 398 | error = -EACCES; |
| 399 | return error; |
| 400 | } |
| 401 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 402 | const struct file_operations smb_file_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | .llseek = remote_llseek, |
| 405 | .read = smb_file_read, |
| 406 | .write = smb_file_write, |
| 407 | .ioctl = smb_ioctl, |
| 408 | .mmap = smb_file_mmap, |
| 409 | .open = smb_file_open, |
| 410 | .release = smb_file_release, |
| 411 | .fsync = smb_fsync, |
| 412 | .sendfile = smb_file_sendfile, |
| 413 | }; |
| 414 | |
| 415 | struct inode_operations smb_file_inode_operations = |
| 416 | { |
| 417 | .permission = smb_file_permission, |
| 418 | .getattr = smb_getattr, |
| 419 | .setattr = smb_notify_change, |
| 420 | }; |