file.c 15 KB

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