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