1 /* 2 * linux/fs/nfs/file.c 3 * 4 * Copyright (C) 1992 Rick Sladkey 5 * 6 * Changes Copyright (C) 1994 by Florian La Roche 7 * - Do not copy data too often around in the kernel. 8 * - In nfs_file_read the return value of kmalloc wasn't checked. 9 * - Put in a better version of read look-ahead buffering. Original idea 10 * and implementation by Wai S Kok elekokws@ee.nus.sg. 11 * 12 * Expire cache on write to a file by Wai S Kok (Oct 1994). 13 * 14 * Total rewrite of read side for new NFS buffer cache.. Linus. 15 * 16 * nfs regular file handling functions 17 */ 18 19 #include <linux/time.h> 20 #include <linux/kernel.h> 21 #include <linux/errno.h> 22 #include <linux/fcntl.h> 23 #include <linux/stat.h> 24 #include <linux/nfs_fs.h> 25 #include <linux/nfs_mount.h> 26 #include <linux/mm.h> 27 #include <linux/slab.h> 28 #include <linux/pagemap.h> 29 #include <linux/smp_lock.h> 30 31 #include <asm/uaccess.h> 32 #include <asm/system.h> 33 34 #include "delegation.h" 35 36 #define NFSDBG_FACILITY NFSDBG_FILE 37 38 static int nfs_file_open(struct inode *, struct file *); 39 static int nfs_file_release(struct inode *, struct file *); 40 static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin); 41 static int nfs_file_mmap(struct file *, struct vm_area_struct *); 42 static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *); 43 static ssize_t nfs_file_read(struct kiocb *, char __user *, size_t, loff_t); 44 static ssize_t nfs_file_write(struct kiocb *, const char __user *, size_t, loff_t); 45 static int nfs_file_flush(struct file *); 46 static int nfs_fsync(struct file *, struct dentry *dentry, int datasync); 47 static int nfs_check_flags(int flags); 48 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 49 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 50 51 struct file_operations nfs_file_operations = { 52 .llseek = nfs_file_llseek, 53 .read = do_sync_read, 54 .write = do_sync_write, 55 .aio_read = nfs_file_read, 56 .aio_write = nfs_file_write, 57 .mmap = nfs_file_mmap, 58 .open = nfs_file_open, 59 .flush = nfs_file_flush, 60 .release = nfs_file_release, 61 .fsync = nfs_fsync, 62 .lock = nfs_lock, 63 .flock = nfs_flock, 64 .sendfile = nfs_file_sendfile, 65 .check_flags = nfs_check_flags, 66 }; 67 68 struct inode_operations nfs_file_inode_operations = { 69 .permission = nfs_permission, 70 .getattr = nfs_getattr, 71 .setattr = nfs_setattr, 72 }; 73 74 /* Hack for future NFS swap support */ 75 #ifndef IS_SWAPFILE 76 # define IS_SWAPFILE(inode) (0) 77 #endif 78 79 static int nfs_check_flags(int flags) 80 { 81 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 82 return -EINVAL; 83 84 return 0; 85 } 86 87 /* 88 * Open file 89 */ 90 static int 91 nfs_file_open(struct inode *inode, struct file *filp) 92 { 93 struct nfs_server *server = NFS_SERVER(inode); 94 int (*open)(struct inode *, struct file *); 95 int res; 96 97 res = nfs_check_flags(filp->f_flags); 98 if (res) 99 return res; 100 101 lock_kernel(); 102 /* Do NFSv4 open() call */ 103 if ((open = server->rpc_ops->file_open) != NULL) 104 res = open(inode, filp); 105 unlock_kernel(); 106 return res; 107 } 108 109 static int 110 nfs_file_release(struct inode *inode, struct file *filp) 111 { 112 /* Ensure that dirty pages are flushed out with the right creds */ 113 if (filp->f_mode & FMODE_WRITE) 114 filemap_fdatawrite(filp->f_mapping); 115 return NFS_PROTO(inode)->file_release(inode, filp); 116 } 117 118 /** 119 * nfs_revalidate_size - Revalidate the file size 120 * @inode - pointer to inode struct 121 * @file - pointer to struct file 122 * 123 * Revalidates the file length. This is basically a wrapper around 124 * nfs_revalidate_inode() that takes into account the fact that we may 125 * have cached writes (in which case we don't care about the server's 126 * idea of what the file length is), or O_DIRECT (in which case we 127 * shouldn't trust the cache). 128 */ 129 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 130 { 131 struct nfs_server *server = NFS_SERVER(inode); 132 struct nfs_inode *nfsi = NFS_I(inode); 133 134 if (server->flags & NFS_MOUNT_NOAC) 135 goto force_reval; 136 if (filp->f_flags & O_DIRECT) 137 goto force_reval; 138 if (nfsi->npages != 0) 139 return 0; 140 return nfs_revalidate_inode(server, inode); 141 force_reval: 142 return __nfs_revalidate_inode(server, inode); 143 } 144 145 static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 146 { 147 /* origin == SEEK_END => we must revalidate the cached file length */ 148 if (origin == 2) { 149 struct inode *inode = filp->f_mapping->host; 150 int retval = nfs_revalidate_file_size(inode, filp); 151 if (retval < 0) 152 return (loff_t)retval; 153 } 154 return remote_llseek(filp, offset, origin); 155 } 156 157 /* 158 * Flush all dirty pages, and check for write errors. 159 * 160 */ 161 static int 162 nfs_file_flush(struct file *file) 163 { 164 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 165 struct inode *inode = file->f_dentry->d_inode; 166 int status; 167 168 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 169 170 if ((file->f_mode & FMODE_WRITE) == 0) 171 return 0; 172 lock_kernel(); 173 /* Ensure that data+attribute caches are up to date after close() */ 174 status = nfs_wb_all(inode); 175 if (!status) { 176 status = ctx->error; 177 ctx->error = 0; 178 if (!status && !nfs_have_delegation(inode, FMODE_READ)) 179 __nfs_revalidate_inode(NFS_SERVER(inode), inode); 180 } 181 unlock_kernel(); 182 return status; 183 } 184 185 static ssize_t 186 nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos) 187 { 188 struct dentry * dentry = iocb->ki_filp->f_dentry; 189 struct inode * inode = dentry->d_inode; 190 ssize_t result; 191 192 #ifdef CONFIG_NFS_DIRECTIO 193 if (iocb->ki_filp->f_flags & O_DIRECT) 194 return nfs_file_direct_read(iocb, buf, count, pos); 195 #endif 196 197 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", 198 dentry->d_parent->d_name.name, dentry->d_name.name, 199 (unsigned long) count, (unsigned long) pos); 200 201 result = nfs_revalidate_inode(NFS_SERVER(inode), inode); 202 if (!result) 203 result = generic_file_aio_read(iocb, buf, count, pos); 204 return result; 205 } 206 207 static ssize_t 208 nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count, 209 read_actor_t actor, void *target) 210 { 211 struct dentry *dentry = filp->f_dentry; 212 struct inode *inode = dentry->d_inode; 213 ssize_t res; 214 215 dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n", 216 dentry->d_parent->d_name.name, dentry->d_name.name, 217 (unsigned long) count, (unsigned long long) *ppos); 218 219 res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 220 if (!res) 221 res = generic_file_sendfile(filp, ppos, count, actor, target); 222 return res; 223 } 224 225 static int 226 nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 227 { 228 struct dentry *dentry = file->f_dentry; 229 struct inode *inode = dentry->d_inode; 230 int status; 231 232 dfprintk(VFS, "nfs: mmap(%s/%s)\n", 233 dentry->d_parent->d_name.name, dentry->d_name.name); 234 235 status = nfs_revalidate_inode(NFS_SERVER(inode), inode); 236 if (!status) 237 status = generic_file_mmap(file, vma); 238 return status; 239 } 240 241 /* 242 * Flush any dirty pages for this process, and check for write errors. 243 * The return status from this call provides a reliable indication of 244 * whether any write errors occurred for this process. 245 */ 246 static int 247 nfs_fsync(struct file *file, struct dentry *dentry, int datasync) 248 { 249 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 250 struct inode *inode = dentry->d_inode; 251 int status; 252 253 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 254 255 lock_kernel(); 256 status = nfs_wb_all(inode); 257 if (!status) { 258 status = ctx->error; 259 ctx->error = 0; 260 } 261 unlock_kernel(); 262 return status; 263 } 264 265 /* 266 * This does the "real" work of the write. The generic routine has 267 * allocated the page, locked it, done all the page alignment stuff 268 * calculations etc. Now we should just copy the data from user 269 * space and write it back to the real medium.. 270 * 271 * If the writer ends up delaying the write, the writer needs to 272 * increment the page use counts until he is done with the page. 273 */ 274 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to) 275 { 276 return nfs_flush_incompatible(file, page); 277 } 278 279 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to) 280 { 281 long status; 282 283 lock_kernel(); 284 status = nfs_updatepage(file, page, offset, to-offset); 285 unlock_kernel(); 286 return status; 287 } 288 289 struct address_space_operations nfs_file_aops = { 290 .readpage = nfs_readpage, 291 .readpages = nfs_readpages, 292 .set_page_dirty = __set_page_dirty_nobuffers, 293 .writepage = nfs_writepage, 294 .writepages = nfs_writepages, 295 .prepare_write = nfs_prepare_write, 296 .commit_write = nfs_commit_write, 297 #ifdef CONFIG_NFS_DIRECTIO 298 .direct_IO = nfs_direct_IO, 299 #endif 300 }; 301 302 /* 303 * Write to a file (through the page cache). 304 */ 305 static ssize_t 306 nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) 307 { 308 struct dentry * dentry = iocb->ki_filp->f_dentry; 309 struct inode * inode = dentry->d_inode; 310 ssize_t result; 311 312 #ifdef CONFIG_NFS_DIRECTIO 313 if (iocb->ki_filp->f_flags & O_DIRECT) 314 return nfs_file_direct_write(iocb, buf, count, pos); 315 #endif 316 317 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n", 318 dentry->d_parent->d_name.name, dentry->d_name.name, 319 inode->i_ino, (unsigned long) count, (unsigned long) pos); 320 321 result = -EBUSY; 322 if (IS_SWAPFILE(inode)) 323 goto out_swapfile; 324 result = nfs_revalidate_inode(NFS_SERVER(inode), inode); 325 if (result) 326 goto out; 327 328 result = count; 329 if (!count) 330 goto out; 331 332 result = generic_file_aio_write(iocb, buf, count, pos); 333 out: 334 return result; 335 336 out_swapfile: 337 printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 338 goto out; 339 } 340 341 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 342 { 343 struct inode *inode = filp->f_mapping->host; 344 int status = 0; 345 346 lock_kernel(); 347 /* Use local locking if mounted with "-onolock" */ 348 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 349 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 350 else { 351 struct file_lock *cfl = posix_test_lock(filp, fl); 352 353 fl->fl_type = F_UNLCK; 354 if (cfl != NULL) 355 memcpy(fl, cfl, sizeof(*fl)); 356 } 357 unlock_kernel(); 358 return status; 359 } 360 361 static int do_vfs_lock(struct file *file, struct file_lock *fl) 362 { 363 int res = 0; 364 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 365 case FL_POSIX: 366 res = posix_lock_file_wait(file, fl); 367 break; 368 case FL_FLOCK: 369 res = flock_lock_file_wait(file, fl); 370 break; 371 default: 372 BUG(); 373 } 374 if (res < 0) 375 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", 376 __FUNCTION__); 377 return res; 378 } 379 380 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl) 381 { 382 struct inode *inode = filp->f_mapping->host; 383 sigset_t oldset; 384 int status; 385 386 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset); 387 /* 388 * Flush all pending writes before doing anything 389 * with locks.. 390 */ 391 filemap_fdatawrite(filp->f_mapping); 392 down(&inode->i_sem); 393 nfs_wb_all(inode); 394 up(&inode->i_sem); 395 filemap_fdatawait(filp->f_mapping); 396 397 /* NOTE: special case 398 * If we're signalled while cleaning up locks on process exit, we 399 * still need to complete the unlock. 400 */ 401 lock_kernel(); 402 /* Use local locking if mounted with "-onolock" */ 403 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 404 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 405 else 406 status = do_vfs_lock(filp, fl); 407 unlock_kernel(); 408 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset); 409 return status; 410 } 411 412 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl) 413 { 414 struct inode *inode = filp->f_mapping->host; 415 sigset_t oldset; 416 int status; 417 418 rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset); 419 /* 420 * Flush all pending writes before doing anything 421 * with locks.. 422 */ 423 status = filemap_fdatawrite(filp->f_mapping); 424 if (status == 0) { 425 down(&inode->i_sem); 426 status = nfs_wb_all(inode); 427 up(&inode->i_sem); 428 if (status == 0) 429 status = filemap_fdatawait(filp->f_mapping); 430 } 431 if (status < 0) 432 goto out; 433 434 lock_kernel(); 435 /* Use local locking if mounted with "-onolock" */ 436 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) { 437 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 438 /* If we were signalled we still need to ensure that 439 * we clean up any state on the server. We therefore 440 * record the lock call as having succeeded in order to 441 * ensure that locks_remove_posix() cleans it out when 442 * the process exits. 443 */ 444 if (status == -EINTR || status == -ERESTARTSYS) 445 do_vfs_lock(filp, fl); 446 } else 447 status = do_vfs_lock(filp, fl); 448 unlock_kernel(); 449 if (status < 0) 450 goto out; 451 /* 452 * Make sure we clear the cache whenever we try to get the lock. 453 * This makes locking act as a cache coherency point. 454 */ 455 filemap_fdatawrite(filp->f_mapping); 456 down(&inode->i_sem); 457 nfs_wb_all(inode); /* we may have slept */ 458 up(&inode->i_sem); 459 filemap_fdatawait(filp->f_mapping); 460 nfs_zap_caches(inode); 461 out: 462 rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset); 463 return status; 464 } 465 466 /* 467 * Lock a (portion of) a file 468 */ 469 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 470 { 471 struct inode * inode = filp->f_mapping->host; 472 473 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n", 474 inode->i_sb->s_id, inode->i_ino, 475 fl->fl_type, fl->fl_flags, 476 (long long)fl->fl_start, (long long)fl->fl_end); 477 478 if (!inode) 479 return -EINVAL; 480 481 /* No mandatory locks over NFS */ 482 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) 483 return -ENOLCK; 484 485 if (IS_GETLK(cmd)) 486 return do_getlk(filp, cmd, fl); 487 if (fl->fl_type == F_UNLCK) 488 return do_unlk(filp, cmd, fl); 489 return do_setlk(filp, cmd, fl); 490 } 491 492 /* 493 * Lock a (portion of) a file 494 */ 495 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 496 { 497 struct inode * inode = filp->f_mapping->host; 498 499 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n", 500 inode->i_sb->s_id, inode->i_ino, 501 fl->fl_type, fl->fl_flags); 502 503 if (!inode) 504 return -EINVAL; 505 506 /* 507 * No BSD flocks over NFS allowed. 508 * Note: we could try to fake a POSIX lock request here by 509 * using ((u32) filp | 0x80000000) or some such as the pid. 510 * Not sure whether that would be unique, though, or whether 511 * that would break in other places. 512 */ 513 if (!(fl->fl_flags & FL_FLOCK)) 514 return -ENOLCK; 515 516 /* We're simulating flock() locks using posix locks on the server */ 517 fl->fl_owner = (fl_owner_t)filp; 518 fl->fl_start = 0; 519 fl->fl_end = OFFSET_MAX; 520 521 if (fl->fl_type == F_UNLCK) 522 return do_unlk(filp, cmd, fl); 523 return do_setlk(filp, cmd, fl); 524 } 525