file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_size - Revalidate the file size
  116. * @inode - pointer to inode struct
  117. * @file - pointer to struct file
  118. *
  119. * Revalidates the file length. This is basically a wrapper around
  120. * nfs_revalidate_inode() that takes into account the fact that we may
  121. * have cached writes (in which case we don't care about the server's
  122. * idea of what the file length is), or O_DIRECT (in which case we
  123. * shouldn't trust the cache).
  124. */
  125. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  126. {
  127. struct nfs_server *server = NFS_SERVER(inode);
  128. struct nfs_inode *nfsi = NFS_I(inode);
  129. if (server->flags & NFS_MOUNT_NOAC)
  130. goto force_reval;
  131. if (filp->f_flags & O_DIRECT)
  132. goto force_reval;
  133. if (nfsi->npages != 0)
  134. return 0;
  135. return nfs_revalidate_inode(server, inode);
  136. force_reval:
  137. return __nfs_revalidate_inode(server, inode);
  138. }
  139. static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
  140. {
  141. /* origin == SEEK_END => we must revalidate the cached file length */
  142. if (origin == 2) {
  143. struct inode *inode = filp->f_mapping->host;
  144. int retval = nfs_revalidate_file_size(inode, filp);
  145. if (retval < 0)
  146. return (loff_t)retval;
  147. }
  148. return remote_llseek(filp, offset, origin);
  149. }
  150. /*
  151. * Flush all dirty pages, and check for write errors.
  152. *
  153. */
  154. static int
  155. nfs_file_flush(struct file *file)
  156. {
  157. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  158. struct inode *inode = file->f_dentry->d_inode;
  159. int status;
  160. dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
  161. if ((file->f_mode & FMODE_WRITE) == 0)
  162. return 0;
  163. lock_kernel();
  164. /* Ensure that data+attribute caches are up to date after close() */
  165. status = nfs_wb_all(inode);
  166. if (!status) {
  167. status = ctx->error;
  168. ctx->error = 0;
  169. if (!status && !nfs_have_delegation(inode, FMODE_READ))
  170. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  171. }
  172. unlock_kernel();
  173. return status;
  174. }
  175. static ssize_t
  176. nfs_file_read(struct kiocb *iocb, char __user * buf, size_t count, loff_t pos)
  177. {
  178. struct dentry * dentry = iocb->ki_filp->f_dentry;
  179. struct inode * inode = dentry->d_inode;
  180. ssize_t result;
  181. #ifdef CONFIG_NFS_DIRECTIO
  182. if (iocb->ki_filp->f_flags & O_DIRECT)
  183. return nfs_file_direct_read(iocb, buf, count, pos);
  184. #endif
  185. dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
  186. dentry->d_parent->d_name.name, dentry->d_name.name,
  187. (unsigned long) count, (unsigned long) pos);
  188. result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  189. if (!result)
  190. result = generic_file_aio_read(iocb, buf, count, pos);
  191. return result;
  192. }
  193. static ssize_t
  194. nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count,
  195. read_actor_t actor, void *target)
  196. {
  197. struct dentry *dentry = filp->f_dentry;
  198. struct inode *inode = dentry->d_inode;
  199. ssize_t res;
  200. dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n",
  201. dentry->d_parent->d_name.name, dentry->d_name.name,
  202. (unsigned long) count, (unsigned long long) *ppos);
  203. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  204. if (!res)
  205. res = generic_file_sendfile(filp, ppos, count, actor, target);
  206. return res;
  207. }
  208. static int
  209. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  210. {
  211. struct dentry *dentry = file->f_dentry;
  212. struct inode *inode = dentry->d_inode;
  213. int status;
  214. dfprintk(VFS, "nfs: mmap(%s/%s)\n",
  215. dentry->d_parent->d_name.name, dentry->d_name.name);
  216. status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  217. if (!status)
  218. status = generic_file_mmap(file, vma);
  219. return status;
  220. }
  221. /*
  222. * Flush any dirty pages for this process, and check for write errors.
  223. * The return status from this call provides a reliable indication of
  224. * whether any write errors occurred for this process.
  225. */
  226. static int
  227. nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  228. {
  229. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  230. struct inode *inode = dentry->d_inode;
  231. int status;
  232. dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
  233. lock_kernel();
  234. status = nfs_wb_all(inode);
  235. if (!status) {
  236. status = ctx->error;
  237. ctx->error = 0;
  238. }
  239. unlock_kernel();
  240. return status;
  241. }
  242. /*
  243. * This does the "real" work of the write. The generic routine has
  244. * allocated the page, locked it, done all the page alignment stuff
  245. * calculations etc. Now we should just copy the data from user
  246. * space and write it back to the real medium..
  247. *
  248. * If the writer ends up delaying the write, the writer needs to
  249. * increment the page use counts until he is done with the page.
  250. */
  251. static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  252. {
  253. return nfs_flush_incompatible(file, page);
  254. }
  255. static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  256. {
  257. long status;
  258. lock_kernel();
  259. status = nfs_updatepage(file, page, offset, to-offset);
  260. unlock_kernel();
  261. return status;
  262. }
  263. struct address_space_operations nfs_file_aops = {
  264. .readpage = nfs_readpage,
  265. .readpages = nfs_readpages,
  266. .set_page_dirty = __set_page_dirty_nobuffers,
  267. .writepage = nfs_writepage,
  268. .writepages = nfs_writepages,
  269. .prepare_write = nfs_prepare_write,
  270. .commit_write = nfs_commit_write,
  271. #ifdef CONFIG_NFS_DIRECTIO
  272. .direct_IO = nfs_direct_IO,
  273. #endif
  274. };
  275. /*
  276. * Write to a file (through the page cache).
  277. */
  278. static ssize_t
  279. nfs_file_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
  280. {
  281. struct dentry * dentry = iocb->ki_filp->f_dentry;
  282. struct inode * inode = dentry->d_inode;
  283. ssize_t result;
  284. #ifdef CONFIG_NFS_DIRECTIO
  285. if (iocb->ki_filp->f_flags & O_DIRECT)
  286. return nfs_file_direct_write(iocb, buf, count, pos);
  287. #endif
  288. dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
  289. dentry->d_parent->d_name.name, dentry->d_name.name,
  290. inode->i_ino, (unsigned long) count, (unsigned long) pos);
  291. result = -EBUSY;
  292. if (IS_SWAPFILE(inode))
  293. goto out_swapfile;
  294. /*
  295. * O_APPEND implies that we must revalidate the file length.
  296. */
  297. if (iocb->ki_filp->f_flags & O_APPEND) {
  298. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  299. if (result)
  300. goto out;
  301. } else
  302. nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  303. result = count;
  304. if (!count)
  305. goto out;
  306. result = generic_file_aio_write(iocb, buf, count, pos);
  307. out:
  308. return result;
  309. out_swapfile:
  310. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  311. goto out;
  312. }
  313. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  314. {
  315. struct inode *inode = filp->f_mapping->host;
  316. int status = 0;
  317. lock_kernel();
  318. /* Use local locking if mounted with "-onolock" */
  319. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  320. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  321. else {
  322. struct file_lock *cfl = posix_test_lock(filp, fl);
  323. fl->fl_type = F_UNLCK;
  324. if (cfl != NULL)
  325. memcpy(fl, cfl, sizeof(*fl));
  326. }
  327. unlock_kernel();
  328. return status;
  329. }
  330. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  331. {
  332. int res = 0;
  333. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  334. case FL_POSIX:
  335. res = posix_lock_file_wait(file, fl);
  336. break;
  337. case FL_FLOCK:
  338. res = flock_lock_file_wait(file, fl);
  339. break;
  340. default:
  341. BUG();
  342. }
  343. if (res < 0)
  344. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  345. __FUNCTION__);
  346. return res;
  347. }
  348. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  349. {
  350. struct inode *inode = filp->f_mapping->host;
  351. sigset_t oldset;
  352. int status;
  353. rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
  354. /*
  355. * Flush all pending writes before doing anything
  356. * with locks..
  357. */
  358. filemap_fdatawrite(filp->f_mapping);
  359. down(&inode->i_sem);
  360. nfs_wb_all(inode);
  361. up(&inode->i_sem);
  362. filemap_fdatawait(filp->f_mapping);
  363. /* NOTE: special case
  364. * If we're signalled while cleaning up locks on process exit, we
  365. * still need to complete the unlock.
  366. */
  367. lock_kernel();
  368. /* Use local locking if mounted with "-onolock" */
  369. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  370. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  371. else
  372. status = do_vfs_lock(filp, fl);
  373. unlock_kernel();
  374. rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
  375. return status;
  376. }
  377. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  378. {
  379. struct inode *inode = filp->f_mapping->host;
  380. sigset_t oldset;
  381. int status;
  382. rpc_clnt_sigmask(NFS_CLIENT(inode), &oldset);
  383. /*
  384. * Flush all pending writes before doing anything
  385. * with locks..
  386. */
  387. status = filemap_fdatawrite(filp->f_mapping);
  388. if (status == 0) {
  389. down(&inode->i_sem);
  390. status = nfs_wb_all(inode);
  391. up(&inode->i_sem);
  392. if (status == 0)
  393. status = filemap_fdatawait(filp->f_mapping);
  394. }
  395. if (status < 0)
  396. goto out;
  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. /* If we were signalled we still need to ensure that
  402. * we clean up any state on the server. We therefore
  403. * record the lock call as having succeeded in order to
  404. * ensure that locks_remove_posix() cleans it out when
  405. * the process exits.
  406. */
  407. if (status == -EINTR || status == -ERESTARTSYS)
  408. do_vfs_lock(filp, fl);
  409. } else
  410. status = do_vfs_lock(filp, fl);
  411. unlock_kernel();
  412. if (status < 0)
  413. goto out;
  414. /*
  415. * Make sure we clear the cache whenever we try to get the lock.
  416. * This makes locking act as a cache coherency point.
  417. */
  418. filemap_fdatawrite(filp->f_mapping);
  419. down(&inode->i_sem);
  420. nfs_wb_all(inode); /* we may have slept */
  421. up(&inode->i_sem);
  422. filemap_fdatawait(filp->f_mapping);
  423. nfs_zap_caches(inode);
  424. out:
  425. rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
  426. return status;
  427. }
  428. /*
  429. * Lock a (portion of) a file
  430. */
  431. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  432. {
  433. struct inode * inode = filp->f_mapping->host;
  434. dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
  435. inode->i_sb->s_id, inode->i_ino,
  436. fl->fl_type, fl->fl_flags,
  437. (long long)fl->fl_start, (long long)fl->fl_end);
  438. if (!inode)
  439. return -EINVAL;
  440. /* No mandatory locks over NFS */
  441. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  442. return -ENOLCK;
  443. if (IS_GETLK(cmd))
  444. return do_getlk(filp, cmd, fl);
  445. if (fl->fl_type == F_UNLCK)
  446. return do_unlk(filp, cmd, fl);
  447. return do_setlk(filp, cmd, fl);
  448. }
  449. /*
  450. * Lock a (portion of) a file
  451. */
  452. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  453. {
  454. struct inode * inode = filp->f_mapping->host;
  455. dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
  456. inode->i_sb->s_id, inode->i_ino,
  457. fl->fl_type, fl->fl_flags);
  458. if (!inode)
  459. return -EINVAL;
  460. /*
  461. * No BSD flocks over NFS allowed.
  462. * Note: we could try to fake a POSIX lock request here by
  463. * using ((u32) filp | 0x80000000) or some such as the pid.
  464. * Not sure whether that would be unique, though, or whether
  465. * that would break in other places.
  466. */
  467. if (!(fl->fl_flags & FL_FLOCK))
  468. return -ENOLCK;
  469. /* We're simulating flock() locks using posix locks on the server */
  470. fl->fl_owner = (fl_owner_t)filp;
  471. fl->fl_start = 0;
  472. fl->fl_end = OFFSET_MAX;
  473. if (fl->fl_type == F_UNLCK)
  474. return do_unlk(filp, cmd, fl);
  475. return do_setlk(filp, cmd, fl);
  476. }