nfs4file.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/falloc.h>
  8. #include <linux/nfs_fs.h>
  9. #include "internal.h"
  10. #include "fscache.h"
  11. #include "pnfs.h"
  12. #ifdef CONFIG_NFS_V4_2
  13. #include "nfs42.h"
  14. #endif
  15. #define NFSDBG_FACILITY NFSDBG_FILE
  16. static int
  17. nfs4_file_open(struct inode *inode, struct file *filp)
  18. {
  19. struct nfs_open_context *ctx;
  20. struct dentry *dentry = filp->f_path.dentry;
  21. struct dentry *parent = NULL;
  22. struct inode *dir;
  23. unsigned openflags = filp->f_flags;
  24. struct iattr attr;
  25. int opened = 0;
  26. int err;
  27. /*
  28. * If no cached dentry exists or if it's negative, NFSv4 handled the
  29. * opens in ->lookup() or ->create().
  30. *
  31. * We only get this far for a cached positive dentry. We skipped
  32. * revalidation, so handle it here by dropping the dentry and returning
  33. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  34. */
  35. dprintk("NFS: open file(%pd2)\n", dentry);
  36. if ((openflags & O_ACCMODE) == 3)
  37. openflags--;
  38. /* We can't create new files here */
  39. openflags &= ~(O_CREAT|O_EXCL);
  40. parent = dget_parent(dentry);
  41. dir = parent->d_inode;
  42. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  43. err = PTR_ERR(ctx);
  44. if (IS_ERR(ctx))
  45. goto out;
  46. attr.ia_valid = ATTR_OPEN;
  47. if (openflags & O_TRUNC) {
  48. attr.ia_valid |= ATTR_SIZE;
  49. attr.ia_size = 0;
  50. nfs_wb_all(inode);
  51. }
  52. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
  53. if (IS_ERR(inode)) {
  54. err = PTR_ERR(inode);
  55. switch (err) {
  56. case -EPERM:
  57. case -EACCES:
  58. case -EDQUOT:
  59. case -ENOSPC:
  60. case -EROFS:
  61. goto out_put_ctx;
  62. default:
  63. goto out_drop;
  64. }
  65. }
  66. if (inode != dentry->d_inode)
  67. goto out_drop;
  68. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  69. nfs_file_set_open_context(filp, ctx);
  70. nfs_fscache_open_file(inode, filp);
  71. err = 0;
  72. out_put_ctx:
  73. put_nfs_open_context(ctx);
  74. out:
  75. dput(parent);
  76. return err;
  77. out_drop:
  78. d_drop(dentry);
  79. err = -EOPENSTALE;
  80. goto out_put_ctx;
  81. }
  82. static int
  83. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  84. {
  85. int ret;
  86. struct inode *inode = file_inode(file);
  87. do {
  88. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  89. if (ret != 0)
  90. break;
  91. mutex_lock(&inode->i_mutex);
  92. ret = nfs_file_fsync_commit(file, start, end, datasync);
  93. if (!ret)
  94. ret = pnfs_layoutcommit_inode(inode, true);
  95. mutex_unlock(&inode->i_mutex);
  96. /*
  97. * If nfs_file_fsync_commit detected a server reboot, then
  98. * resend all dirty pages that might have been covered by
  99. * the NFS_CONTEXT_RESEND_WRITES flag
  100. */
  101. start = 0;
  102. end = LLONG_MAX;
  103. } while (ret == -EAGAIN);
  104. return ret;
  105. }
  106. #ifdef CONFIG_NFS_V4_2
  107. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  108. {
  109. loff_t ret;
  110. switch (whence) {
  111. case SEEK_HOLE:
  112. case SEEK_DATA:
  113. ret = nfs42_proc_llseek(filep, offset, whence);
  114. if (ret != -ENOTSUPP)
  115. return ret;
  116. default:
  117. return nfs_file_llseek(filep, offset, whence);
  118. }
  119. }
  120. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  121. {
  122. struct inode *inode = file_inode(filep);
  123. long ret;
  124. if (!S_ISREG(inode->i_mode))
  125. return -EOPNOTSUPP;
  126. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  127. return -EOPNOTSUPP;
  128. ret = inode_newsize_ok(inode, offset + len);
  129. if (ret < 0)
  130. return ret;
  131. mutex_lock(&inode->i_mutex);
  132. if (mode & FALLOC_FL_PUNCH_HOLE)
  133. ret = nfs42_proc_deallocate(filep, offset, len);
  134. else
  135. ret = nfs42_proc_allocate(filep, offset, len);
  136. mutex_unlock(&inode->i_mutex);
  137. nfs_zap_caches(inode);
  138. return ret;
  139. }
  140. #endif /* CONFIG_NFS_V4_2 */
  141. const struct file_operations nfs4_file_operations = {
  142. #ifdef CONFIG_NFS_V4_2
  143. .llseek = nfs4_file_llseek,
  144. #else
  145. .llseek = nfs_file_llseek,
  146. #endif
  147. .read = new_sync_read,
  148. .write = new_sync_write,
  149. .read_iter = nfs_file_read,
  150. .write_iter = nfs_file_write,
  151. .mmap = nfs_file_mmap,
  152. .open = nfs4_file_open,
  153. .flush = nfs_file_flush,
  154. .release = nfs_file_release,
  155. .fsync = nfs4_file_fsync,
  156. .lock = nfs_lock,
  157. .flock = nfs_flock,
  158. .splice_read = nfs_file_splice_read,
  159. .splice_write = iter_file_splice_write,
  160. #ifdef CONFIG_NFS_V4_2
  161. .fallocate = nfs42_fallocate,
  162. #endif /* CONFIG_NFS_V4_2 */
  163. .check_flags = nfs_check_flags,
  164. .setlease = simple_nosetlease,
  165. };