nfs42proc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
  3. */
  4. #include <linux/fs.h>
  5. #include <linux/sunrpc/sched.h>
  6. #include <linux/nfs.h>
  7. #include <linux/nfs3.h>
  8. #include <linux/nfs4.h>
  9. #include <linux/nfs_xdr.h>
  10. #include <linux/nfs_fs.h>
  11. #include "nfs4_fs.h"
  12. #include "nfs42.h"
  13. static int nfs42_set_rw_stateid(nfs4_stateid *dst, struct file *file,
  14. fmode_t fmode)
  15. {
  16. struct nfs_open_context *open;
  17. struct nfs_lock_context *lock;
  18. int ret;
  19. open = get_nfs_open_context(nfs_file_open_context(file));
  20. lock = nfs_get_lock_context(open);
  21. if (IS_ERR(lock)) {
  22. put_nfs_open_context(open);
  23. return PTR_ERR(lock);
  24. }
  25. ret = nfs4_set_rw_stateid(dst, open, lock, fmode);
  26. nfs_put_lock_context(lock);
  27. put_nfs_open_context(open);
  28. return ret;
  29. }
  30. static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
  31. loff_t offset, loff_t len)
  32. {
  33. struct inode *inode = file_inode(filep);
  34. struct nfs_server *server = NFS_SERVER(inode);
  35. struct nfs42_falloc_args args = {
  36. .falloc_fh = NFS_FH(inode),
  37. .falloc_offset = offset,
  38. .falloc_length = len,
  39. .falloc_bitmask = server->cache_consistency_bitmask,
  40. };
  41. struct nfs42_falloc_res res = {
  42. .falloc_server = server,
  43. };
  44. int status;
  45. msg->rpc_argp = &args;
  46. msg->rpc_resp = &res;
  47. status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE);
  48. if (status)
  49. return status;
  50. res.falloc_fattr = nfs_alloc_fattr();
  51. if (!res.falloc_fattr)
  52. return -ENOMEM;
  53. status = nfs4_call_sync(server->client, server, msg,
  54. &args.seq_args, &res.seq_res, 0);
  55. if (status == 0)
  56. status = nfs_post_op_update_inode(inode, res.falloc_fattr);
  57. kfree(res.falloc_fattr);
  58. return status;
  59. }
  60. static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
  61. loff_t offset, loff_t len)
  62. {
  63. struct nfs_server *server = NFS_SERVER(file_inode(filep));
  64. struct nfs4_exception exception = { };
  65. int err;
  66. do {
  67. err = _nfs42_proc_fallocate(msg, filep, offset, len);
  68. if (err == -ENOTSUPP)
  69. return -EOPNOTSUPP;
  70. err = nfs4_handle_exception(server, err, &exception);
  71. } while (exception.retry);
  72. return err;
  73. }
  74. int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
  75. {
  76. struct rpc_message msg = {
  77. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
  78. };
  79. struct inode *inode = file_inode(filep);
  80. int err;
  81. if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
  82. return -EOPNOTSUPP;
  83. mutex_lock(&inode->i_mutex);
  84. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  85. if (err == -EOPNOTSUPP)
  86. NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
  87. mutex_unlock(&inode->i_mutex);
  88. return err;
  89. }
  90. int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
  91. {
  92. struct rpc_message msg = {
  93. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
  94. };
  95. struct inode *inode = file_inode(filep);
  96. int err;
  97. if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
  98. return -EOPNOTSUPP;
  99. nfs_wb_all(inode);
  100. mutex_lock(&inode->i_mutex);
  101. err = nfs42_proc_fallocate(&msg, filep, offset, len);
  102. if (err == 0)
  103. truncate_pagecache_range(inode, offset, (offset + len) -1);
  104. if (err == -EOPNOTSUPP)
  105. NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
  106. mutex_unlock(&inode->i_mutex);
  107. return err;
  108. }
  109. loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
  110. {
  111. struct inode *inode = file_inode(filep);
  112. struct nfs42_seek_args args = {
  113. .sa_fh = NFS_FH(inode),
  114. .sa_offset = offset,
  115. .sa_what = (whence == SEEK_HOLE) ?
  116. NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
  117. };
  118. struct nfs42_seek_res res;
  119. struct rpc_message msg = {
  120. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
  121. .rpc_argp = &args,
  122. .rpc_resp = &res,
  123. };
  124. struct nfs_server *server = NFS_SERVER(inode);
  125. int status;
  126. if (!nfs_server_capable(inode, NFS_CAP_SEEK))
  127. return -ENOTSUPP;
  128. status = nfs42_set_rw_stateid(&args.sa_stateid, filep, FMODE_READ);
  129. if (status)
  130. return status;
  131. nfs_wb_all(inode);
  132. status = nfs4_call_sync(server->client, server, &msg,
  133. &args.seq_args, &res.seq_res, 0);
  134. if (status == -ENOTSUPP)
  135. server->caps &= ~NFS_CAP_SEEK;
  136. if (status)
  137. return status;
  138. return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
  139. }