file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. #define NFSDBG_FACILITY NFSDBG_FILE
  33. static int nfs_file_open(struct inode *, struct file *);
  34. static int nfs_file_release(struct inode *, struct file *);
  35. static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
  36. static int nfs_file_mmap(struct file *, struct vm_area_struct *);
  37. static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *);
  38. static ssize_t nfs_file_read(struct kiocb *, char __user *, size_t, loff_t);
  39. static ssize_t nfs_file_write(struct kiocb *, const char __user *, size_t, loff_t);
  40. static int nfs_file_flush(struct file *);
  41. static int nfs_fsync(struct file *, struct dentry *dentry, int datasync);
  42. static int nfs_check_flags(int flags);
  43. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
  44. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
  45. struct file_operations nfs_file_operations = {
  46. .llseek = nfs_file_llseek,
  47. .read = do_sync_read,
  48. .write = do_sync_write,
  49. .aio_read = nfs_file_read,
  50. .aio_write = nfs_file_write,
  51. .mmap = nfs_file_mmap,
  52. .open = nfs_file_open,
  53. .flush = nfs_file_flush,
  54. .release = nfs_file_release,
  55. .fsync = nfs_fsync,
  56. .lock = nfs_lock,
  57. .flock = nfs_flock,
  58. .sendfile = nfs_file_sendfile,
  59. .check_flags = nfs_check_flags,
  60. };
  61. struct inode_operations nfs_file_inode_operations = {
  62. .permission = nfs_permission,
  63. .getattr = nfs_getattr,
  64. .setattr = nfs_setattr,
  65. };
  66. #ifdef CONFIG_NFS_V3
  67. struct inode_operations nfs3_file_inode_operations = {
  68. .permission = nfs_permission,
  69. .getattr = nfs_getattr,
  70. .setattr = nfs_setattr,
  71. .listxattr = nfs3_listxattr,
  72. .getxattr = nfs3_getxattr,
  73. .setxattr = nfs3_setxattr,
  74. .removexattr = nfs3_removexattr,
  75. };
  76. #endif /* CONFIG_NFS_v3 */
  77. /* Hack for future NFS swap support */
  78. #ifndef IS_SWAPFILE
  79. # define IS_SWAPFILE(inode) (0)
  80. #endif
  81. static int nfs_check_flags(int flags)
  82. {
  83. if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  84. return -EINVAL;
  85. return 0;
  86. }
  87. /*
  88. * Open file
  89. */
  90. static int
  91. nfs_file_open(struct inode *inode, struct file *filp)
  92. {
  93. struct nfs_server *server = NFS_SERVER(inode);
  94. int (*open)(struct inode *, struct file *);
  95. int res;
  96. res = nfs_check_flags(filp->f_flags);
  97. if (res)
  98. return res;
  99. lock_kernel();
  100. /* Do NFSv4 open() call */
  101. if ((open = server->rpc_ops->file_open) != NULL)
  102. res = 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. return NFS_PROTO(inode)->file_release(inode, filp);
  113. }
  114. /**
  115. * nfs_revalidate_file - Revalidate the page cache & related metadata
  116. * @inode - pointer to inode struct
  117. * @file - pointer to file
  118. */
  119. static int nfs_revalidate_file(struct inode *inode, struct file *filp)
  120. {
  121. struct nfs_inode *nfsi = NFS_I(inode);
  122. int retval = 0;
  123. if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) || 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. lock_kernel();
  179. /* Ensure that data+attribute caches are up to date after close() */
  180. status = nfs_wb_all(inode);
  181. if (!status) {
  182. status = ctx->error;
  183. ctx->error = 0;
  184. if (!status && !nfs_have_delegation(inode, FMODE_READ))
  185. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  186. }
  187. unlock_kernel();
  188. return status;
  189. }
  190. static ssize_t
  191. nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
  192. {
  193. struct dentry * dentry = iocb->ki_filp->f_dentry;
  194. struct inode * inode = dentry->d_inode;
  195. ssize_t result;
  196. #ifdef CONFIG_NFS_DIRECTIO
  197. if (iocb->ki_filp->f_flags & O_DIRECT)
  198. return nfs_file_direct_read(iocb, buf, count, pos);
  199. #endif
  200. dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
  201. dentry->d_parent->d_name.name, dentry->d_name.name,
  202. (unsigned long) count, (unsigned long) pos);
  203. result = nfs_revalidate_file(inode, iocb->ki_filp);
  204. if (!result)
  205. result = generic_file_aio_read(iocb, buf, count, pos);
  206. return result;
  207. }
  208. static ssize_t
  209. nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
  210. read_actor_t actor, void *target)
  211. {
  212. struct dentry *dentry = filp->f_dentry;
  213. struct inode *inode = dentry->d_inode;
  214. ssize_t res;
  215. dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
  216. dentry->d_parent->d_name.name, dentry->d_name.name,
  217. (unsigned long) count, (unsigned long long) *ppos);
  218. res = nfs_revalidate_file(inode, filp);
  219. if (!res)
  220. res = generic_file_sendfile(filp, ppos, count, actor, target);
  221. return res;
  222. }
  223. static int
  224. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  225. {
  226. struct dentry *dentry = file->f_dentry;
  227. struct inode *inode = dentry->d_inode;
  228. int status;
  229. dfprintk(VFS, "nfs: mmap(%s/%s)\n",
  230. dentry->d_parent->d_name.name, dentry->d_name.name);
  231. status = nfs_revalidate_file(inode, file);
  232. if (!status)
  233. status = generic_file_mmap(file, vma);
  234. return status;
  235. }
  236. /*
  237. * Flush any dirty pages for this process, and check for write errors.
  238. * The return status from this call provides a reliable indication of
  239. * whether any write errors occurred for this process.
  240. */
  241. static int
  242. nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  243. {
  244. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  245. struct inode *inode = dentry->d_inode;
  246. int status;
  247. dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
  248. lock_kernel();
  249. status = nfs_wb_all(inode);
  250. if (!status) {
  251. status = ctx->error;
  252. ctx->error = 0;
  253. }
  254. unlock_kernel();
  255. return status;
  256. }
  257. /*
  258. * This does the "real" work of the write. The generic routine has
  259. * allocated the page, locked it, done all the page alignment stuff
  260. * calculations etc. Now we should just copy the data from user
  261. * space and write it back to the real medium..
  262. *
  263. * If the writer ends up delaying the write, the writer needs to
  264. * increment the page use counts until he is done with the page.
  265. */
  266. static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  267. {
  268. return nfs_flush_incompatible(file, page);
  269. }
  270. static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  271. {
  272. long status;
  273. lock_kernel();
  274. status = nfs_updatepage(file, page, offset, to-offset);
  275. unlock_kernel();
  276. return status;
  277. }
  278. struct address_space_operations nfs_file_aops = {
  279. .readpage = nfs_readpage,
  280. .readpages = nfs_readpages,
  281. .set_page_dirty = __set_page_dirty_nobuffers,
  282. .writepage = nfs_writepage,
  283. .writepages = nfs_writepages,
  284. .prepare_write = nfs_prepare_write,
  285. .commit_write = nfs_commit_write,
  286. #ifdef CONFIG_NFS_DIRECTIO
  287. .direct_IO = nfs_direct_IO,
  288. #endif
  289. };
  290. /*
  291. * Write to a file (through the page cache).
  292. */
  293. static ssize_t
  294. nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
  295. {
  296. struct dentry * dentry = iocb->ki_filp->f_dentry;
  297. struct inode * inode = dentry->d_inode;
  298. ssize_t result;
  299. #ifdef CONFIG_NFS_DIRECTIO
  300. if (iocb->ki_filp->f_flags & O_DIRECT)
  301. return nfs_file_direct_write(iocb, buf, count, pos);
  302. #endif
  303. dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
  304. dentry->d_parent->d_name.name, dentry->d_name.name,
  305. inode->i_ino, (unsigned long) count, (unsigned long) pos);
  306. result = -EBUSY;
  307. if (IS_SWAPFILE(inode))
  308. goto out_swapfile;
  309. /*
  310. * O_APPEND implies that we must revalidate the file length.
  311. */
  312. if (iocb->ki_filp->f_flags & O_APPEND) {
  313. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  314. if (result)
  315. goto out;
  316. }
  317. nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  318. result = count;
  319. if (!count)
  320. goto out;
  321. result = generic_file_aio_write(iocb, buf, count, pos);
  322. out:
  323. return result;
  324. out_swapfile:
  325. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  326. goto out;
  327. }
  328. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  329. {
  330. struct inode *inode = filp->f_mapping->host;
  331. int status = 0;
  332. lock_kernel();
  333. /* Use local locking if mounted with "-onolock" */
  334. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  335. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  336. else {
  337. struct file_lock *cfl = posix_test_lock(filp, fl);
  338. fl->fl_type = F_UNLCK;
  339. if (cfl != NULL)
  340. memcpy(fl, cfl, sizeof(*fl));
  341. }
  342. unlock_kernel();
  343. return status;
  344. }
  345. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  346. {
  347. int res = 0;
  348. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  349. case FL_POSIX:
  350. res = posix_lock_file_wait(file, fl);
  351. break;
  352. case FL_FLOCK:
  353. res = flock_lock_file_wait(file, fl);
  354. break;
  355. default:
  356. BUG();
  357. }
  358. if (res < 0)
  359. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  360. __FUNCTION__);
  361. return res;
  362. }
  363. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  364. {
  365. struct inode *inode = filp->f_mapping->host;
  366. sigset_t oldset;
  367. int status;
  368. rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
  369. /*
  370. * Flush all pending writes before doing anything
  371. * with locks..
  372. */
  373. filemap_fdatawrite(filp->f_mapping);
  374. down(&inode->i_sem);
  375. nfs_wb_all(inode);
  376. up(&inode->i_sem);
  377. filemap_fdatawait(filp->f_mapping);
  378. /* NOTE: special case
  379. * If we're signalled while cleaning up locks on process exit, we
  380. * still need to complete the unlock.
  381. */
  382. lock_kernel();
  383. /* Use local locking if mounted with "-onolock" */
  384. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  385. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  386. else
  387. status = do_vfs_lock(filp, fl);
  388. unlock_kernel();
  389. rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
  390. return status;
  391. }
  392. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  393. {
  394. struct inode *inode = filp->f_mapping->host;
  395. sigset_t oldset;
  396. int status;
  397. rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
  398. /*
  399. * Flush all pending writes before doing anything
  400. * with locks..
  401. */
  402. status = filemap_fdatawrite(filp->f_mapping);
  403. if (status == 0) {
  404. down(&inode->i_sem);
  405. status = nfs_wb_all(inode);
  406. up(&inode->i_sem);
  407. if (status == 0)
  408. status = filemap_fdatawait(filp->f_mapping);
  409. }
  410. if (status < 0)
  411. goto out;
  412. lock_kernel();
  413. /* Use local locking if mounted with "-onolock" */
  414. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
  415. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  416. /* If we were signalled we still need to ensure that
  417. * we clean up any state on the server. We therefore
  418. * record the lock call as having succeeded in order to
  419. * ensure that locks_remove_posix() cleans it out when
  420. * the process exits.
  421. */
  422. if (status == -EINTR || status == -ERESTARTSYS)
  423. do_vfs_lock(filp, fl);
  424. } else
  425. status = do_vfs_lock(filp, fl);
  426. unlock_kernel();
  427. if (status < 0)
  428. goto out;
  429. /*
  430. * Make sure we clear the cache whenever we try to get the lock.
  431. * This makes locking act as a cache coherency point.
  432. */
  433. filemap_fdatawrite(filp->f_mapping);
  434. down(&inode->i_sem);
  435. nfs_wb_all(inode); /* we may have slept */
  436. up(&inode->i_sem);
  437. filemap_fdatawait(filp->f_mapping);
  438. nfs_zap_caches(inode);
  439. out:
  440. rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
  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. if (!inode)
  454. return -EINVAL;
  455. /* No mandatory locks over NFS */
  456. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  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. struct inode * inode = filp->f_mapping->host;
  470. dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
  471. inode->i_sb->s_id, inode->i_ino,
  472. fl->fl_type, fl->fl_flags);
  473. if (!inode)
  474. return -EINVAL;
  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. }