file.c 13 KB

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