file.c 15 KB

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