file.c 15 KB

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