file.c 15 KB

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