file.c 14 KB

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