file.c 16 KB

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