0001
0002
0003
0004
0005
0006
0007 #include <linux/fs.h>
0008 #include <linux/file.h>
0009 #include <linux/falloc.h>
0010 #include <linux/mount.h>
0011 #include <linux/nfs_fs.h>
0012 #include <linux/nfs_ssc.h>
0013 #include "delegation.h"
0014 #include "internal.h"
0015 #include "iostat.h"
0016 #include "fscache.h"
0017 #include "pnfs.h"
0018
0019 #include "nfstrace.h"
0020
0021 #ifdef CONFIG_NFS_V4_2
0022 #include "nfs42.h"
0023 #endif
0024
0025 #define NFSDBG_FACILITY NFSDBG_FILE
0026
0027 static int
0028 nfs4_file_open(struct inode *inode, struct file *filp)
0029 {
0030 struct nfs_open_context *ctx;
0031 struct dentry *dentry = file_dentry(filp);
0032 struct dentry *parent = NULL;
0033 struct inode *dir;
0034 unsigned openflags = filp->f_flags;
0035 fmode_t f_mode;
0036 struct iattr attr;
0037 int err;
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 dprintk("NFS: open file(%pd2)\n", dentry);
0049
0050 err = nfs_check_flags(openflags);
0051 if (err)
0052 return err;
0053
0054 f_mode = filp->f_mode;
0055 if ((openflags & O_ACCMODE) == 3)
0056 f_mode |= flags_to_mode(openflags);
0057
0058
0059 openflags &= ~(O_CREAT|O_EXCL);
0060
0061 parent = dget_parent(dentry);
0062 dir = d_inode(parent);
0063
0064 ctx = alloc_nfs_open_context(file_dentry(filp), f_mode, filp);
0065 err = PTR_ERR(ctx);
0066 if (IS_ERR(ctx))
0067 goto out;
0068
0069 attr.ia_valid = ATTR_OPEN;
0070 if (openflags & O_TRUNC) {
0071 attr.ia_valid |= ATTR_SIZE;
0072 attr.ia_size = 0;
0073 filemap_write_and_wait(inode->i_mapping);
0074 }
0075
0076 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
0077 if (IS_ERR(inode)) {
0078 err = PTR_ERR(inode);
0079 switch (err) {
0080 default:
0081 goto out_put_ctx;
0082 case -ENOENT:
0083 case -ESTALE:
0084 case -EISDIR:
0085 case -ENOTDIR:
0086 case -ELOOP:
0087 goto out_drop;
0088 }
0089 }
0090 if (inode != d_inode(dentry))
0091 goto out_drop;
0092
0093 nfs_file_set_open_context(filp, ctx);
0094 nfs_fscache_open_file(inode, filp);
0095 err = 0;
0096 filp->f_mode |= FMODE_CAN_ODIRECT;
0097
0098 out_put_ctx:
0099 put_nfs_open_context(ctx);
0100 out:
0101 dput(parent);
0102 return err;
0103
0104 out_drop:
0105 d_drop(dentry);
0106 err = -EOPENSTALE;
0107 goto out_put_ctx;
0108 }
0109
0110
0111
0112
0113 static int
0114 nfs4_file_flush(struct file *file, fl_owner_t id)
0115 {
0116 struct inode *inode = file_inode(file);
0117 errseq_t since;
0118
0119 dprintk("NFS: flush(%pD2)\n", file);
0120
0121 nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
0122 if ((file->f_mode & FMODE_WRITE) == 0)
0123 return 0;
0124
0125
0126
0127
0128
0129 if (!nfs4_delegation_flush_on_close(inode))
0130 return filemap_fdatawrite(file->f_mapping);
0131
0132
0133 since = filemap_sample_wb_err(file->f_mapping);
0134 nfs_wb_all(inode);
0135 return filemap_check_wb_err(file->f_mapping, since);
0136 }
0137
0138 #ifdef CONFIG_NFS_V4_2
0139 static ssize_t __nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
0140 struct file *file_out, loff_t pos_out,
0141 size_t count, unsigned int flags)
0142 {
0143 struct nfs42_copy_notify_res *cn_resp = NULL;
0144 struct nl4_server *nss = NULL;
0145 nfs4_stateid *cnrs = NULL;
0146 ssize_t ret;
0147 bool sync = false;
0148
0149
0150 if (file_in->f_op != &nfs4_file_operations)
0151 return -EXDEV;
0152 if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY) ||
0153 !nfs_server_capable(file_inode(file_in), NFS_CAP_COPY))
0154 return -EOPNOTSUPP;
0155 if (file_inode(file_in) == file_inode(file_out))
0156 return -EOPNOTSUPP;
0157
0158
0159
0160 if (count <= 2 * NFS_SERVER(file_inode(file_in))->rsize)
0161 sync = true;
0162 retry:
0163 if (!nfs42_files_from_same_server(file_in, file_out)) {
0164
0165
0166
0167
0168 if (sync)
0169 return -EOPNOTSUPP;
0170 cn_resp = kzalloc(sizeof(struct nfs42_copy_notify_res),
0171 GFP_KERNEL);
0172 if (unlikely(cn_resp == NULL))
0173 return -ENOMEM;
0174
0175 ret = nfs42_proc_copy_notify(file_in, file_out, cn_resp);
0176 if (ret) {
0177 ret = -EOPNOTSUPP;
0178 goto out;
0179 }
0180 nss = &cn_resp->cnr_src;
0181 cnrs = &cn_resp->cnr_stateid;
0182 }
0183 ret = nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count,
0184 nss, cnrs, sync);
0185 out:
0186 kfree(cn_resp);
0187
0188 if (ret == -EAGAIN)
0189 goto retry;
0190 return ret;
0191 }
0192
0193 static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
0194 struct file *file_out, loff_t pos_out,
0195 size_t count, unsigned int flags)
0196 {
0197 ssize_t ret;
0198
0199 ret = __nfs4_copy_file_range(file_in, pos_in, file_out, pos_out, count,
0200 flags);
0201 if (ret == -EOPNOTSUPP || ret == -EXDEV)
0202 ret = generic_copy_file_range(file_in, pos_in, file_out,
0203 pos_out, count, flags);
0204 return ret;
0205 }
0206
0207 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
0208 {
0209 loff_t ret;
0210
0211 switch (whence) {
0212 case SEEK_HOLE:
0213 case SEEK_DATA:
0214 ret = nfs42_proc_llseek(filep, offset, whence);
0215 if (ret != -EOPNOTSUPP)
0216 return ret;
0217 fallthrough;
0218 default:
0219 return nfs_file_llseek(filep, offset, whence);
0220 }
0221 }
0222
0223 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
0224 {
0225 struct inode *inode = file_inode(filep);
0226 long ret;
0227
0228 if (!S_ISREG(inode->i_mode))
0229 return -EOPNOTSUPP;
0230
0231 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
0232 return -EOPNOTSUPP;
0233
0234 ret = inode_newsize_ok(inode, offset + len);
0235 if (ret < 0)
0236 return ret;
0237
0238 if (mode & FALLOC_FL_PUNCH_HOLE)
0239 return nfs42_proc_deallocate(filep, offset, len);
0240 return nfs42_proc_allocate(filep, offset, len);
0241 }
0242
0243 static loff_t nfs42_remap_file_range(struct file *src_file, loff_t src_off,
0244 struct file *dst_file, loff_t dst_off, loff_t count,
0245 unsigned int remap_flags)
0246 {
0247 struct inode *dst_inode = file_inode(dst_file);
0248 struct nfs_server *server = NFS_SERVER(dst_inode);
0249 struct inode *src_inode = file_inode(src_file);
0250 unsigned int bs = server->clone_blksize;
0251 bool same_inode = false;
0252 int ret;
0253
0254
0255 if (remap_flags & REMAP_FILE_DEDUP)
0256 return -EOPNOTSUPP;
0257
0258 if (remap_flags & ~REMAP_FILE_ADVISORY)
0259 return -EINVAL;
0260
0261 if (IS_SWAPFILE(dst_inode) || IS_SWAPFILE(src_inode))
0262 return -ETXTBSY;
0263
0264
0265 ret = -EINVAL;
0266 if (bs) {
0267 if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
0268 goto out;
0269 if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
0270 goto out;
0271 }
0272
0273 if (src_inode == dst_inode)
0274 same_inode = true;
0275
0276
0277 if (same_inode) {
0278 inode_lock(src_inode);
0279 } else if (dst_inode < src_inode) {
0280 inode_lock_nested(dst_inode, I_MUTEX_PARENT);
0281 inode_lock_nested(src_inode, I_MUTEX_CHILD);
0282 } else {
0283 inode_lock_nested(src_inode, I_MUTEX_PARENT);
0284 inode_lock_nested(dst_inode, I_MUTEX_CHILD);
0285 }
0286
0287
0288
0289 ret = nfs_sync_inode(src_inode);
0290 if (ret)
0291 goto out_unlock;
0292 ret = nfs_sync_inode(dst_inode);
0293 if (ret)
0294 goto out_unlock;
0295
0296 ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
0297
0298
0299
0300 if (!ret)
0301 truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
0302
0303 out_unlock:
0304 if (same_inode) {
0305 inode_unlock(src_inode);
0306 } else if (dst_inode < src_inode) {
0307 inode_unlock(src_inode);
0308 inode_unlock(dst_inode);
0309 } else {
0310 inode_unlock(dst_inode);
0311 inode_unlock(src_inode);
0312 }
0313 out:
0314 return ret < 0 ? ret : count;
0315 }
0316
0317 static int read_name_gen = 1;
0318 #define SSC_READ_NAME_BODY "ssc_read_%d"
0319
0320 static struct file *__nfs42_ssc_open(struct vfsmount *ss_mnt,
0321 struct nfs_fh *src_fh, nfs4_stateid *stateid)
0322 {
0323 struct nfs_fattr *fattr = nfs_alloc_fattr();
0324 struct file *filep, *res;
0325 struct nfs_server *server;
0326 struct inode *r_ino = NULL;
0327 struct nfs_open_context *ctx;
0328 struct nfs4_state_owner *sp;
0329 char *read_name = NULL;
0330 int len, status = 0;
0331
0332 server = NFS_SB(ss_mnt->mnt_sb);
0333
0334 if (!fattr)
0335 return ERR_PTR(-ENOMEM);
0336
0337 status = nfs4_proc_getattr(server, src_fh, fattr, NULL);
0338 if (status < 0) {
0339 res = ERR_PTR(status);
0340 goto out;
0341 }
0342
0343 if (!S_ISREG(fattr->mode)) {
0344 res = ERR_PTR(-EBADF);
0345 goto out;
0346 }
0347
0348 res = ERR_PTR(-ENOMEM);
0349 len = strlen(SSC_READ_NAME_BODY) + 16;
0350 read_name = kzalloc(len, GFP_KERNEL);
0351 if (read_name == NULL)
0352 goto out;
0353 snprintf(read_name, len, SSC_READ_NAME_BODY, read_name_gen++);
0354
0355 r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);
0356 if (IS_ERR(r_ino)) {
0357 res = ERR_CAST(r_ino);
0358 goto out_free_name;
0359 }
0360
0361 filep = alloc_file_pseudo(r_ino, ss_mnt, read_name, O_RDONLY,
0362 r_ino->i_fop);
0363 if (IS_ERR(filep)) {
0364 res = ERR_CAST(filep);
0365 iput(r_ino);
0366 goto out_free_name;
0367 }
0368
0369 ctx = alloc_nfs_open_context(filep->f_path.dentry, filep->f_mode,
0370 filep);
0371 if (IS_ERR(ctx)) {
0372 res = ERR_CAST(ctx);
0373 goto out_filep;
0374 }
0375
0376 res = ERR_PTR(-EINVAL);
0377 sp = nfs4_get_state_owner(server, ctx->cred, GFP_KERNEL);
0378 if (sp == NULL)
0379 goto out_ctx;
0380
0381 ctx->state = nfs4_get_open_state(r_ino, sp);
0382 if (ctx->state == NULL)
0383 goto out_stateowner;
0384
0385 set_bit(NFS_SRV_SSC_COPY_STATE, &ctx->state->flags);
0386 memcpy(&ctx->state->open_stateid.other, &stateid->other,
0387 NFS4_STATEID_OTHER_SIZE);
0388 update_open_stateid(ctx->state, stateid, NULL, filep->f_mode);
0389 set_bit(NFS_OPEN_STATE, &ctx->state->flags);
0390
0391 nfs_file_set_open_context(filep, ctx);
0392 put_nfs_open_context(ctx);
0393
0394 file_ra_state_init(&filep->f_ra, filep->f_mapping->host->i_mapping);
0395 res = filep;
0396 out_free_name:
0397 kfree(read_name);
0398 out:
0399 nfs_free_fattr(fattr);
0400 return res;
0401 out_stateowner:
0402 nfs4_put_state_owner(sp);
0403 out_ctx:
0404 put_nfs_open_context(ctx);
0405 out_filep:
0406 fput(filep);
0407 goto out_free_name;
0408 }
0409
0410 static void __nfs42_ssc_close(struct file *filep)
0411 {
0412 struct nfs_open_context *ctx = nfs_file_open_context(filep);
0413
0414 ctx->state->flags = 0;
0415 }
0416
0417 static const struct nfs4_ssc_client_ops nfs4_ssc_clnt_ops_tbl = {
0418 .sco_open = __nfs42_ssc_open,
0419 .sco_close = __nfs42_ssc_close,
0420 };
0421
0422
0423
0424
0425
0426
0427
0428 void nfs42_ssc_register_ops(void)
0429 {
0430 nfs42_ssc_register(&nfs4_ssc_clnt_ops_tbl);
0431 }
0432
0433
0434
0435
0436
0437
0438
0439 void nfs42_ssc_unregister_ops(void)
0440 {
0441 nfs42_ssc_unregister(&nfs4_ssc_clnt_ops_tbl);
0442 }
0443 #endif
0444
0445 static int nfs4_setlease(struct file *file, long arg, struct file_lock **lease,
0446 void **priv)
0447 {
0448 return nfs4_proc_setlease(file, arg, lease, priv);
0449 }
0450
0451 const struct file_operations nfs4_file_operations = {
0452 .read_iter = nfs_file_read,
0453 .write_iter = nfs_file_write,
0454 .mmap = nfs_file_mmap,
0455 .open = nfs4_file_open,
0456 .flush = nfs4_file_flush,
0457 .release = nfs_file_release,
0458 .fsync = nfs_file_fsync,
0459 .lock = nfs_lock,
0460 .flock = nfs_flock,
0461 .splice_read = generic_file_splice_read,
0462 .splice_write = iter_file_splice_write,
0463 .check_flags = nfs_check_flags,
0464 .setlease = nfs4_setlease,
0465 #ifdef CONFIG_NFS_V4_2
0466 .copy_file_range = nfs4_copy_file_range,
0467 .llseek = nfs4_file_llseek,
0468 .fallocate = nfs42_fallocate,
0469 .remap_file_range = nfs42_remap_file_range,
0470 #else
0471 .llseek = nfs_file_llseek,
0472 #endif
0473 };