blob: 99c2b9dfbb1046bdb99b4bcf3c806858c9e7cae9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations used by nfsd. Some of these have been ripped from
4 * other parts of the kernel because they weren't exported, others
5 * are partial duplicates with added or changed functionality.
6 *
7 * Note that several functions dget() the dentry upon which they want
8 * to act, most notably those that create directory entries. Response
9 * dentry's are dput()'d if necessary in the release callback.
10 * So if you notice code paths that apparently fail to dput() the
11 * dentry, don't worry--they have been taken care of.
12 *
13 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14 * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/file.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020019#include <linux/splice.h>
Anna Schumaker95d871f2014-11-07 14:44:26 -050020#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020027#include <linux/jhash.h>
28#include <linux/ima.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Ben Myersf5019122010-02-17 14:05:11 -060031#include <linux/exportfs.h>
32#include <linux/writeback.h>
David Quigley18032ca2013-05-02 13:19:10 -040033#include <linux/security.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020034
35#ifdef CONFIG_NFSD_V3
36#include "xdr3.h"
37#endif /* CONFIG_NFSD_V3 */
38
Christoph Hellwig5be196e2006-01-09 20:51:55 -080039#ifdef CONFIG_NFSD_V4
Christoph Hellwigffa01602015-12-03 12:59:52 +010040#include "../internal.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050041#include "acl.h"
42#include "idmap.h"
Trond Myklebusta2f4c3f2021-12-18 20:38:00 -050043#include "xdr4.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#endif /* CONFIG_NFSD_V4 */
45
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046#include "nfsd.h"
47#include "vfs.h"
Jeff Laytonb4935232019-08-18 14:18:49 -040048#include "filecache.h"
Jeff Layton6e8b50d2015-11-17 06:52:23 -050049#include "trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define NFSDDBG_FACILITY NFSDDBG_FILEOP
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * Called from nfsd_lookup and encode_dirent. Check if we have crossed
55 * a mount point.
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080056 * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * or nfs_ok having possibly changed *dpp and *expp
58 */
59int
60nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
61 struct svc_export **expp)
62{
63 struct svc_export *exp = *expp, *exp2 = NULL;
64 struct dentry *dentry = *dpp;
Al Viro91c9fa82009-04-18 02:42:05 -040065 struct path path = {.mnt = mntget(exp->ex_path.mnt),
66 .dentry = dget(dentry)};
Al Viro6264d692006-10-19 23:28:58 -070067 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Al Viro7cc90cc2011-03-18 09:04:20 -040069 err = follow_down(&path);
David Howellscc53ce52011-01-14 18:45:26 +000070 if (err < 0)
71 goto out;
NeilBrown99bbf6e2017-03-15 12:40:44 +110072 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
73 nfsd_mountpoint(dentry, exp) == 2) {
74 /* This is only a mountpoint in some other namespace */
75 path_put(&path);
76 goto out;
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Al Viro91c9fa82009-04-18 02:42:05 -040079 exp2 = rqst_exp_get_by_name(rqstp, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (IS_ERR(exp2)) {
J. Bruce Fields3b6cee7bc2009-10-25 21:18:19 -040081 err = PTR_ERR(exp2);
82 /*
83 * We normally allow NFS clients to continue
84 * "underneath" a mountpoint that is not exported.
85 * The exception is V4ROOT, where no traversal is ever
86 * allowed without an explicit export of the new
87 * directory.
88 */
89 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
90 err = 0;
Al Viro91c9fa82009-04-18 02:42:05 -040091 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 goto out;
93 }
Steve Dickson3c394dd2009-09-09 15:02:40 -040094 if (nfsd_v4client(rqstp) ||
95 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* successfully crossed mount point */
Al Viro1644ccc2009-04-18 02:32:31 -040097 /*
Al Viro91c9fa82009-04-18 02:42:05 -040098 * This is subtle: path.dentry is *not* on path.mnt
99 * at this point. The only reason we are safe is that
100 * original mnt is pinned down by exp, so we should
101 * put path *before* putting exp
Al Viro1644ccc2009-04-18 02:32:31 -0400102 */
Al Viro91c9fa82009-04-18 02:42:05 -0400103 *dpp = path.dentry;
104 path.dentry = dentry;
Al Viro1644ccc2009-04-18 02:32:31 -0400105 *expp = exp2;
Al Viro91c9fa82009-04-18 02:42:05 -0400106 exp2 = exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Al Viro91c9fa82009-04-18 02:42:05 -0400108 path_put(&path);
109 exp_put(exp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110out:
111 return err;
112}
113
J. Bruce Fields289ede42009-09-26 20:32:24 -0400114static void follow_to_parent(struct path *path)
115{
116 struct dentry *dp;
117
118 while (path->dentry == path->mnt->mnt_root && follow_up(path))
119 ;
120 dp = dget_parent(path->dentry);
121 dput(path->dentry);
122 path->dentry = dp;
123}
124
125static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
126{
127 struct svc_export *exp2;
128 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
129 .dentry = dget(dparent)};
130
131 follow_to_parent(&path);
132
133 exp2 = rqst_exp_parent(rqstp, &path);
134 if (PTR_ERR(exp2) == -ENOENT) {
135 *dentryp = dget(dparent);
136 } else if (IS_ERR(exp2)) {
137 path_put(&path);
138 return PTR_ERR(exp2);
139 } else {
140 *dentryp = dget(path.dentry);
141 exp_put(*exp);
142 *exp = exp2;
143 }
144 path_put(&path);
145 return 0;
146}
147
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400148/*
149 * For nfsd purposes, we treat V4ROOT exports as though there was an
150 * export at *every* directory.
NeilBrown99bbf6e2017-03-15 12:40:44 +1100151 * We return:
152 * '1' if this dentry *must* be an export point,
153 * '2' if it might be, if there is really a mount here, and
154 * '0' if there is no chance of an export point here.
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400155 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -0400156int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400157{
NeilBrown99bbf6e2017-03-15 12:40:44 +1100158 if (!d_inode(dentry))
159 return 0;
160 if (exp->ex_flags & NFSEXP_V4ROOT)
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400161 return 1;
Trond Myklebust11fcee022011-09-12 19:37:26 -0400162 if (nfsd4_is_junction(dentry))
163 return 1;
NeilBrown99bbf6e2017-03-15 12:40:44 +1100164 if (d_mountpoint(dentry))
165 /*
166 * Might only be a mountpoint in a different namespace,
167 * but we need to check.
168 */
169 return 2;
170 return 0;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400171}
172
Al Viro6264d692006-10-19 23:28:58 -0700173__be32
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700174nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400175 const char *name, unsigned int len,
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700176 struct svc_export **exp_ret, struct dentry **dentry_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct svc_export *exp;
179 struct dentry *dparent;
180 struct dentry *dentry;
Al Viro6264d692006-10-19 23:28:58 -0700181 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dparent = fhp->fh_dentry;
Kinglong Meebf18f162014-06-10 22:06:44 +0800186 exp = exp_get(fhp->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Lookup the name, but don't follow links */
189 if (isdotent(name, len)) {
190 if (len==1)
191 dentry = dget(dparent);
Jan Blunck54775492008-02-14 19:38:39 -0800192 else if (dparent != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dentry = dget_parent(dparent);
J. Bruce Fieldsfed83812009-09-26 16:53:01 -0400194 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dentry = dget(dparent); /* .. == . just like at / */
196 else {
197 /* checking mountpoint crossing is very different when stepping up */
J. Bruce Fields289ede42009-09-26 20:32:24 -0400198 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
199 if (host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 } else {
J. Bruce Fields43357232014-01-24 18:04:40 -0500203 /*
204 * In the nfsd4_open() case, this may be held across
205 * subsequent open and delegation acquisition which may
206 * need to take the child's i_mutex:
207 */
208 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 dentry = lookup_one_len(name, dparent, len);
Al Viro6264d692006-10-19 23:28:58 -0700210 host_err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (IS_ERR(dentry))
212 goto out_nfserr;
J. Bruce Fields82ead7f2009-10-25 21:33:15 -0400213 if (nfsd_mountpoint(dentry, exp)) {
NeilBrownbbddca82016-01-07 16:08:20 -0500214 /*
215 * We don't need the i_mutex after all. It's
216 * still possible we could open this (regular
217 * files can be mountpoints too), but the
218 * i_mutex is just there to prevent renames of
219 * something that we might be about to delegate,
220 * and a mountpoint won't be renamed:
221 */
222 fh_unlock(fhp);
Al Viro6264d692006-10-19 23:28:58 -0700223 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dput(dentry);
225 goto out_nfserr;
226 }
227 }
228 }
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700229 *dentry_ret = dentry;
230 *exp_ret = exp;
231 return 0;
232
233out_nfserr:
234 exp_put(exp);
235 return nfserrno(host_err);
236}
237
238/*
239 * Look up one component of a pathname.
240 * N.B. After this call _both_ fhp and resfh need an fh_put
241 *
242 * If the lookup would cross a mountpoint, and the mounted filesystem
243 * is exported to the client with NFSEXP_NOHIDE, then the lookup is
244 * accepted as it stands and the mounted directory is
245 * returned. Otherwise the covered directory is returned.
246 * NOTE: this mountpoint crossing is not supported properly by all
247 * clients and is explicitly disallowed for NFSv3
NeilBrownef5825e2021-09-02 11:14:47 +1000248 * NeilBrown <neilb@cse.unsw.edu.au>
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700249 */
250__be32
251nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
Chuck Lever5a022fc2007-11-01 16:57:09 -0400252 unsigned int len, struct svc_fh *resfh)
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700253{
254 struct svc_export *exp;
255 struct dentry *dentry;
256 __be32 err;
257
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400258 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
259 if (err)
260 return err;
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700261 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
262 if (err)
263 return err;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700264 err = check_nfsd_access(exp, rqstp);
265 if (err)
266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
268 * Note: we compose the file handle now, but as the
269 * dentry may be negative, it may need to be updated.
270 */
271 err = fh_compose(resfh, exp, dentry, fhp);
David Howells2b0143b2015-03-17 22:25:59 +0000272 if (!err && d_really_is_negative(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 err = nfserr_noent;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700274out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 exp_put(exp);
277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Ben Myersf5019122010-02-17 14:05:11 -0600280/*
281 * Commit metadata changes to stable storage.
282 */
283static int
Trond Myklebust57f64032019-12-18 14:57:23 -0500284commit_inode_metadata(struct inode *inode)
Ben Myersf5019122010-02-17 14:05:11 -0600285{
Ben Myersf5019122010-02-17 14:05:11 -0600286 const struct export_operations *export_ops = inode->i_sb->s_export_op;
Ben Myersf5019122010-02-17 14:05:11 -0600287
Christoph Hellwigc37650162010-10-06 10:48:20 +0200288 if (export_ops->commit_metadata)
289 return export_ops->commit_metadata(inode);
290 return sync_inode_metadata(inode, 1);
Ben Myersf5019122010-02-17 14:05:11 -0600291}
J. Bruce Fields6c0a6542007-07-17 04:04:47 -0700292
Trond Myklebust57f64032019-12-18 14:57:23 -0500293static int
294commit_metadata(struct svc_fh *fhp)
295{
296 struct inode *inode = d_inode(fhp->fh_dentry);
297
298 if (!EX_ISSYNC(fhp->fh_export))
299 return 0;
300 return commit_inode_metadata(inode);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800304 * Go over the attributes and take care of the small differences between
305 * NFS semantics and what Linux expects.
306 */
307static void
308nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
309{
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800310 /* sanitize the mode change */
311 if (iap->ia_valid & ATTR_MODE) {
312 iap->ia_mode &= S_IALLUGO;
313 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
314 }
315
316 /* Revoke setuid/setgid on chown */
317 if (!S_ISDIR(inode->i_mode) &&
Stanislav Kholmanskikhc4fa6d72013-12-11 14:16:36 +0400318 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800319 iap->ia_valid |= ATTR_KILL_PRIV;
320 if (iap->ia_valid & ATTR_MODE) {
321 /* we're setting mode too, just clear the s*id bits */
322 iap->ia_mode &= ~S_ISUID;
323 if (iap->ia_mode & S_IXGRP)
324 iap->ia_mode &= ~S_ISGID;
325 } else {
326 /* set ATTR_KILL_* bits and let VFS handle it */
327 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
328 }
329 }
330}
331
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500332static __be32
333nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
334 struct iattr *iap)
335{
336 struct inode *inode = d_inode(fhp->fh_dentry);
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500337
338 if (iap->ia_size < inode->i_size) {
339 __be32 err;
340
341 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
342 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
343 if (err)
344 return err;
345 }
Jeff Laytonf7e33bd2021-08-19 14:56:38 -0400346 return nfserrno(get_write_access(inode));
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500347}
348
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800349/*
350 * Set various file attributes. After this call fhp needs an fh_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Al Viro6264d692006-10-19 23:28:58 -0700352__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
Arnd Bergmann2a1aa482019-11-03 17:50:18 +0100354 int check_guard, time64_t guardtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct dentry *dentry;
357 struct inode *inode;
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200358 int accmode = NFSD_MAY_SATTR;
Al Viro175a4eb2011-07-26 03:30:54 -0400359 umode_t ftype = 0;
Al Viro6264d692006-10-19 23:28:58 -0700360 __be32 err;
361 int host_err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500362 bool get_write_count;
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500363 bool size_change = (iap->ia_valid & ATTR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
zhengbin255fbca2018-11-30 16:04:25 +0800365 if (iap->ia_valid & ATTR_SIZE) {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200366 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ftype = S_IFREG;
zhengbin255fbca2018-11-30 16:04:25 +0800368 }
369
370 /*
371 * If utimes(2) and friends are called with times not NULL, we should
372 * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
Geert Uytterhoevene977cc82019-05-27 14:21:32 +0200373 * will return EACCES, when the caller's effective UID does not match
zhengbin255fbca2018-11-30 16:04:25 +0800374 * the owner of the file, and the caller is not privileged. In this
375 * situation, we should return EPERM(notify_change will return this).
376 */
377 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
378 accmode |= NFSD_MAY_OWNER_OVERRIDE;
379 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
380 accmode |= NFSD_MAY_WRITE;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500383 /* Callers that do fh_verify should do the fh_want_write: */
384 get_write_count = !fhp->fh_dentry;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Get inode */
387 err = fh_verify(rqstp, fhp, ftype, accmode);
NeilBrown15b7a1b2005-11-07 01:00:23 -0800388 if (err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500389 return err;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500390 if (get_write_count) {
391 host_err = fh_want_write(fhp);
392 if (host_err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500393 goto out;
J. Bruce Fields9f67f182014-02-24 14:59:47 -0500394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000397 inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrown15b7a1b2005-11-07 01:00:23 -0800399 /* Ignore any mode updates on symlinks */
400 if (S_ISLNK(inode->i_mode))
401 iap->ia_valid &= ~ATTR_MODE;
402
403 if (!iap->ia_valid)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500404 return 0;
NeilBrown15b7a1b2005-11-07 01:00:23 -0800405
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800406 nfsd_sanitize_attrs(inode, iap);
407
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500408 if (check_guard && guardtime != inode->i_ctime.tv_sec)
409 return nfserr_notsync;
410
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000411 /*
Christoph Hellwig818e5a22013-11-18 05:07:30 -0800412 * The size case is special, it changes the file in addition to the
Christoph Hellwig783112f2017-02-20 07:21:33 +0100413 * attributes, and file systems don't expect it to be mixed with
414 * "random" attribute changes. We thus split out the size change
415 * into a separate call to ->setattr, and do the rest as a separate
416 * setattr call.
Christoph Hellwig9c85fca2007-03-07 15:26:25 +0000417 */
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500418 if (size_change) {
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500419 err = nfsd_get_write_access(rqstp, fhp, iap);
420 if (err)
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500421 return err;
Christoph Hellwig783112f2017-02-20 07:21:33 +0100422 }
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700423
Christoph Hellwig783112f2017-02-20 07:21:33 +0100424 fh_lock(fhp);
425 if (size_change) {
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700426 /*
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500427 * RFC5661, Section 18.30.4:
428 * Changing the size of a file with SETATTR indirectly
429 * changes the time_modify and change attributes.
430 *
431 * (and similar for the older RFCs)
Christoph Hellwigf0c63122014-09-07 12:15:52 -0700432 */
Christoph Hellwig783112f2017-02-20 07:21:33 +0100433 struct iattr size_attr = {
434 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
435 .ia_size = iap->ia_size,
436 };
437
Christian Brauner2f221d62021-01-21 14:19:26 +0100438 host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
Christoph Hellwig783112f2017-02-20 07:21:33 +0100439 if (host_err)
440 goto out_unlock;
441 iap->ia_valid &= ~ATTR_SIZE;
442
443 /*
444 * Avoid the additional setattr call below if the only other
445 * attribute that the client sends is the mtime, as we update
446 * it as part of the size change above.
447 */
448 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
449 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 iap->ia_valid |= ATTR_CTIME;
Christian Brauner2f221d62021-01-21 14:19:26 +0100453 host_err = notify_change(&init_user_ns, dentry, iap, NULL);
Christoph Hellwig987da472013-11-18 05:07:47 -0800454
Christoph Hellwig783112f2017-02-20 07:21:33 +0100455out_unlock:
456 fh_unlock(fhp);
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500457 if (size_change)
458 put_write_access(inode);
J. Bruce Fields0839ffb2017-02-09 14:20:42 -0500459out:
Christoph Hellwig758e99f2017-02-20 17:04:42 -0500460 if (!host_err)
461 host_err = commit_metadata(fhp);
462 return nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800465#if defined(CONFIG_NFSD_V4)
Chuck Lever9b4146e2012-01-04 16:26:43 -0500466/*
467 * NFS junction information is stored in an extended attribute.
468 */
469#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
470
471/**
472 * nfsd4_is_junction - Test if an object could be an NFS junction
473 *
474 * @dentry: object to test
475 *
476 * Returns 1 if "dentry" appears to contain NFS junction information.
477 * Otherwise 0 is returned.
478 */
Trond Myklebust11fcee022011-09-12 19:37:26 -0400479int nfsd4_is_junction(struct dentry *dentry)
480{
David Howells2b0143b2015-03-17 22:25:59 +0000481 struct inode *inode = d_inode(dentry);
Trond Myklebust11fcee022011-09-12 19:37:26 -0400482
483 if (inode == NULL)
484 return 0;
485 if (inode->i_mode & S_IXUGO)
486 return 0;
487 if (!(inode->i_mode & S_ISVTX))
488 return 0;
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +0100489 if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
490 NULL, 0) <= 0)
Trond Myklebust11fcee022011-09-12 19:37:26 -0400491 return 0;
492 return 1;
493}
David Quigley18032ca2013-05-02 13:19:10 -0400494#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
495__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
496 struct xdr_netobj *label)
497{
498 __be32 error;
499 int host_error;
500 struct dentry *dentry;
501
502 error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
503 if (error)
504 return error;
505
506 dentry = fhp->fh_dentry;
507
Al Viro59551022016-01-22 15:40:57 -0500508 inode_lock(d_inode(dentry));
David Quigley18032ca2013-05-02 13:19:10 -0400509 host_error = security_inode_setsecctx(dentry, label->data, label->len);
Al Viro59551022016-01-22 15:40:57 -0500510 inode_unlock(d_inode(dentry));
David Quigley18032ca2013-05-02 13:19:10 -0400511 return nfserrno(host_error);
512}
513#else
514__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
515 struct xdr_netobj *label)
516{
517 return nfserr_notsupp;
518}
519#endif
520
Trond Myklebusta2f4c3f2021-12-18 20:38:00 -0500521static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
522{
523 return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
524}
525
526__be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
527 struct nfsd_file *nf_src, u64 src_pos,
528 struct nfsd_file *nf_dst, u64 dst_pos,
529 u64 count, bool sync)
Christoph Hellwigffa01602015-12-03 12:59:52 +0100530{
Trond Myklebustb66ae6d2020-01-06 13:40:32 -0500531 struct file *src = nf_src->nf_file;
532 struct file *dst = nf_dst->nf_file;
Trond Myklebust555dbf12021-12-18 20:38:01 -0500533 errseq_t since;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100534 loff_t cloned;
Trond Myklebust1b28d752020-01-06 13:40:33 -0500535 __be32 ret = 0;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100536
Trond Myklebust555dbf12021-12-18 20:38:01 -0500537 since = READ_ONCE(dst->f_wb_err);
Darrick J. Wong452ce652018-10-30 10:41:56 +1100538 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
Trond Myklebust1b28d752020-01-06 13:40:33 -0500539 if (cloned < 0) {
540 ret = nfserrno(cloned);
541 goto out_err;
542 }
543 if (count && cloned != count) {
544 ret = nfserrno(-EINVAL);
545 goto out_err;
546 }
Trond Myklebusta25e37262019-11-27 17:05:51 -0500547 if (sync) {
548 loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
549 int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
Trond Myklebust57f64032019-12-18 14:57:23 -0500550
551 if (!status)
Trond Myklebust555dbf12021-12-18 20:38:01 -0500552 status = filemap_check_wb_err(dst->f_mapping, since);
553 if (!status)
Trond Myklebust57f64032019-12-18 14:57:23 -0500554 status = commit_inode_metadata(file_inode(src));
Trond Myklebust1b28d752020-01-06 13:40:33 -0500555 if (status < 0) {
Chuck Lever75acacb2021-12-28 14:27:56 -0500556 struct nfsd_net *nn = net_generic(nf_dst->nf_net,
557 nfsd_net_id);
558
Trond Myklebusta2f4c3f2021-12-18 20:38:00 -0500559 trace_nfsd_clone_file_range_err(rqstp,
560 &nfsd4_get_cstate(rqstp)->save_fh,
561 src_pos,
562 &nfsd4_get_cstate(rqstp)->current_fh,
563 dst_pos,
564 count, status);
Chuck Lever75acacb2021-12-28 14:27:56 -0500565 nfsd_reset_write_verifier(nn);
566 trace_nfsd_writeverf_reset(nn, rqstp, status);
Trond Myklebust1b28d752020-01-06 13:40:33 -0500567 ret = nfserrno(status);
568 }
Trond Myklebusta25e37262019-11-27 17:05:51 -0500569 }
Trond Myklebust1b28d752020-01-06 13:40:33 -0500570out_err:
Trond Myklebust1b28d752020-01-06 13:40:33 -0500571 return ret;
Christoph Hellwigffa01602015-12-03 12:59:52 +0100572}
573
Anna Schumaker29ae7f92016-09-07 15:57:30 -0400574ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
575 u64 dst_pos, u64 count)
576{
577
578 /*
579 * Limit copy to 4MB to prevent indefinitely blocking an nfsd
580 * thread and client rpc slot. The choice of 4MB is somewhat
581 * arbitrary. We might instead base this on r/wsize, or make it
582 * tunable, or use a time instead of a byte limit, or implement
583 * asynchronous copy. In theory a client could also recognize a
584 * limit like this and pipeline multiple COPY requests.
585 */
586 count = min_t(u64, count, 1 << 22);
587 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
588}
589
Anna Schumaker95d871f2014-11-07 14:44:26 -0500590__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
591 struct file *file, loff_t offset, loff_t len,
592 int flags)
593{
Anna Schumaker95d871f2014-11-07 14:44:26 -0500594 int error;
595
596 if (!S_ISREG(file_inode(file)->i_mode))
597 return nfserr_inval;
598
Anna Schumaker95d871f2014-11-07 14:44:26 -0500599 error = vfs_fallocate(file, flags, offset, len);
600 if (!error)
601 error = commit_metadata(fhp);
602
603 return nfserrno(error);
604}
J. Bruce Fields6a85d6c2010-07-06 12:39:12 -0400605#endif /* defined(CONFIG_NFSD_V4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607#ifdef CONFIG_NFSD_V3
608/*
609 * Check server access rights to a file system object
610 */
611struct accessmap {
612 u32 access;
613 int how;
614};
615static struct accessmap nfs3_regaccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200616 { NFS3_ACCESS_READ, NFSD_MAY_READ },
617 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
618 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
619 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000621#ifdef CONFIG_NFSD_V4
622 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
623 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
624 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
625#endif
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 { 0, 0 }
628};
629
630static struct accessmap nfs3_diraccess[] = {
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200631 { NFS3_ACCESS_READ, NFSD_MAY_READ },
632 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
633 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
634 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
635 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000637#ifdef CONFIG_NFSD_V4
638 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
639 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
640 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
641#endif
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 { 0, 0 }
644};
645
646static struct accessmap nfs3_anyaccess[] = {
647 /* Some clients - Solaris 2.6 at least, make an access call
648 * to the server to check for access for things like /dev/null
649 * (which really, the server doesn't care about). So
650 * We provide simple access checking for them, looking
651 * mainly at mode bits, and we make sure to ignore read-only
652 * filesystem checks
653 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200654 { NFS3_ACCESS_READ, NFSD_MAY_READ },
655 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
656 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
657 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 { 0, 0 }
660};
661
Al Viro6264d692006-10-19 23:28:58 -0700662__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
664{
665 struct accessmap *map;
666 struct svc_export *export;
667 struct dentry *dentry;
668 u32 query, result = 0, sresult = 0;
Al Viro6264d692006-10-19 23:28:58 -0700669 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200671 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (error)
673 goto out;
674
675 export = fhp->fh_export;
676 dentry = fhp->fh_dentry;
677
David Howellse36cb0b2015-01-29 12:02:35 +0000678 if (d_is_reg(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 map = nfs3_regaccess;
David Howellse36cb0b2015-01-29 12:02:35 +0000680 else if (d_is_dir(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 map = nfs3_diraccess;
682 else
683 map = nfs3_anyaccess;
684
685
686 query = *access;
687 for (; map->access; map++) {
688 if (map->access & query) {
Al Viro6264d692006-10-19 23:28:58 -0700689 __be32 err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 sresult |= map->access;
692
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700693 err2 = nfsd_permission(rqstp, export, dentry, map->how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 switch (err2) {
695 case nfs_ok:
696 result |= map->access;
697 break;
698
699 /* the following error codes just mean the access was not allowed,
700 * rather than an error occurred */
701 case nfserr_rofs:
702 case nfserr_acces:
703 case nfserr_perm:
704 /* simply don't "or" in the access bit. */
705 break;
706 default:
707 error = err2;
708 goto out;
709 }
710 }
711 }
712 *access = result;
713 if (supported)
714 *supported = sresult;
715
716 out:
717 return error;
718}
719#endif /* CONFIG_NFSD_V3 */
720
Jeff Layton65294c12019-08-18 14:18:48 -0400721int nfsd_open_break_lease(struct inode *inode, int access)
J. Bruce Fields105f4622011-06-07 11:50:23 -0400722{
723 unsigned int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
J. Bruce Fields105f4622011-06-07 11:50:23 -0400725 if (access & NFSD_MAY_NOT_BREAK_LEASE)
726 return 0;
727 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
728 return break_lease(inode, mode | O_NONBLOCK);
729}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * Open an existing file or directory.
Bernd Schubert999448a2012-03-18 22:44:49 -0400733 * The may_flags argument indicates the type of open (read/write/lock)
734 * and additional flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * N.B. After this call fhp needs an fh_put
736 */
Jeff Layton65294c12019-08-18 14:18:48 -0400737static __be32
738__nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
Bernd Schubert999448a2012-03-18 22:44:49 -0400739 int may_flags, struct file **filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Al Viro765927b2012-06-26 21:58:53 +0400741 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct inode *inode;
Kinglong Mee8519f992014-09-03 08:14:06 +0800743 struct file *file;
Al Viro6264d692006-10-19 23:28:58 -0700744 int flags = O_RDONLY|O_LARGEFILE;
745 __be32 err;
Jeff Layton91885252010-03-19 08:06:28 -0400746 int host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Al Viro765927b2012-06-26 21:58:53 +0400748 path.mnt = fhp->fh_export->ex_path.mnt;
749 path.dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000750 inode = d_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 err = nfserr_perm;
Bernd Schubert999448a2012-03-18 22:44:49 -0400753 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (!inode->i_fop)
757 goto out;
758
Bernd Schubert999448a2012-03-18 22:44:49 -0400759 host_err = nfsd_open_break_lease(inode, may_flags);
Al Viro6264d692006-10-19 23:28:58 -0700760 if (host_err) /* NOMEM or WOULDBLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto out_nfserr;
762
Bernd Schubert999448a2012-03-18 22:44:49 -0400763 if (may_flags & NFSD_MAY_WRITE) {
764 if (may_flags & NFSD_MAY_READ)
J. Bruce Fields9ecb6a02006-06-30 01:56:17 -0700765 flags = O_RDWR|O_LARGEFILE;
766 else
767 flags = O_WRONLY|O_LARGEFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Bernd Schubert999448a2012-03-18 22:44:49 -0400769
Kinglong Mee8519f992014-09-03 08:14:06 +0800770 file = dentry_open(&path, flags, current_cred());
771 if (IS_ERR(file)) {
772 host_err = PTR_ERR(file);
773 goto out_nfserr;
Bernd Schubert06effdb2012-03-18 22:44:50 -0400774 }
775
Al Viro6035a272018-06-08 13:40:10 -0400776 host_err = ima_file_check(file, may_flags);
Kinglong Mee8519f992014-09-03 08:14:06 +0800777 if (host_err) {
Christoph Hellwigfd891452015-04-28 15:41:16 +0200778 fput(file);
Kinglong Mee8519f992014-09-03 08:14:06 +0800779 goto out_nfserr;
780 }
781
782 if (may_flags & NFSD_MAY_64BIT_COOKIE)
783 file->f_mode |= FMODE_64BITHASH;
784 else
785 file->f_mode |= FMODE_32BITHASH;
786
787 *filp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -0700789 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790out:
Jeff Layton65294c12019-08-18 14:18:48 -0400791 return err;
792}
793
794__be32
795nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
796 int may_flags, struct file **filp)
797{
798 __be32 err;
Jeff Layton12bcbd42021-12-18 20:37:56 -0500799 bool retried = false;
Jeff Layton65294c12019-08-18 14:18:48 -0400800
801 validate_process_creds();
802 /*
803 * If we get here, then the client has already done an "open",
804 * and (hopefully) checked permission - so allow OWNER_OVERRIDE
805 * in case a chmod has now revoked permission.
806 *
807 * Arguably we should also allow the owner override for
808 * directories, but we never have and it doesn't seem to have
809 * caused anyone a problem. If we were to change this, note
810 * also that our filldir callbacks would need a variant of
811 * lookup_one_len that doesn't check permissions.
812 */
813 if (type == S_IFREG)
814 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
Jeff Layton12bcbd42021-12-18 20:37:56 -0500815retry:
Jeff Layton65294c12019-08-18 14:18:48 -0400816 err = fh_verify(rqstp, fhp, type, may_flags);
Jeff Layton12bcbd42021-12-18 20:37:56 -0500817 if (!err) {
Jeff Layton65294c12019-08-18 14:18:48 -0400818 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
Jeff Layton12bcbd42021-12-18 20:37:56 -0500819 if (err == nfserr_stale && !retried) {
820 retried = true;
821 fh_put(fhp);
822 goto retry;
823 }
824 }
David Howellse0e81732009-09-02 09:13:40 +0100825 validate_process_creds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return err;
827}
828
Jeff Layton65294c12019-08-18 14:18:48 -0400829__be32
830nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
831 int may_flags, struct file **filp)
832{
833 __be32 err;
834
835 validate_process_creds();
836 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
837 validate_process_creds();
838 return err;
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*
Jens Axboecf8208d2007-06-12 21:22:14 +0200842 * Grab and keep cached pages associated with a file in the svc_rqst
843 * so that they can be passed to the network sendmsg/sendpage routines
844 * directly. They will be released after the sending has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
846static int
Jens Axboecf8208d2007-06-12 21:22:14 +0200847nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
848 struct splice_desc *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Jens Axboecf8208d2007-06-12 21:22:14 +0200850 struct svc_rqst *rqstp = sd->u.data;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500851 struct page **pp = rqstp->rq_next_page;
Jens Axboecf8208d2007-06-12 21:22:14 +0200852 struct page *page = buf->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (rqstp->rq_res.page_len == 0) {
Chuck Lever496d83cf2021-06-28 17:24:27 -0400855 svc_rqst_replace_page(rqstp, page);
Jens Axboecf8208d2007-06-12 21:22:14 +0200856 rqstp->rq_res.page_base = buf->offset;
NeilBrown44524352006-10-04 02:15:46 -0700857 } else if (page != pp[-1]) {
Chuck Lever496d83cf2021-06-28 17:24:27 -0400858 svc_rqst_replace_page(rqstp, page);
Chuck Leverc7e0b782021-06-28 16:34:20 -0400859 }
860 rqstp->rq_res.page_len += sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Chuck Leverc7e0b782021-06-28 16:34:20 -0400862 return sd->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Jens Axboecf8208d2007-06-12 21:22:14 +0200865static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
866 struct splice_desc *sd)
867{
868 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
869}
870
Trond Myklebust83a63072019-08-26 13:03:11 -0400871static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
872 size_t expected)
873{
874 if (expected != 0 && len == 0)
875 return 1;
876 if (offset+len >= i_size_read(file_inode(file)))
877 return 1;
878 return 0;
879}
880
Chuck Lever87c59422018-03-28 13:29:11 -0400881static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
882 struct file *file, loff_t offset,
Trond Myklebust83a63072019-08-26 13:03:11 -0400883 unsigned long *count, u32 *eof, ssize_t host_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Al Viro6264d692006-10-19 23:28:58 -0700885 if (host_err >= 0) {
Amir Goldstein20ad8562021-01-06 09:52:36 +0200886 nfsd_stats_io_read_add(fhp->fh_export, host_err);
Trond Myklebust83a63072019-08-26 13:03:11 -0400887 *eof = nfsd_eof_on_read(file, offset, host_err, *count);
Al Viro6264d692006-10-19 23:28:58 -0700888 *count = host_err;
Eric Paris2a12a9d2009-12-17 21:24:21 -0500889 fsnotify_access(file);
Chuck Lever87c59422018-03-28 13:29:11 -0400890 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400891 return 0;
Chuck Lever87c59422018-03-28 13:29:11 -0400892 } else {
893 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400894 return nfserrno(host_err);
Chuck Lever87c59422018-03-28 13:29:11 -0400895 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400896}
897
Chuck Lever87c59422018-03-28 13:29:11 -0400898__be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -0400899 struct file *file, loff_t offset, unsigned long *count,
900 u32 *eof)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400901{
902 struct splice_desc sd = {
903 .len = 0,
904 .total_len = *count,
905 .pos = offset,
906 .u.data = rqstp,
907 };
Trond Myklebust83a63072019-08-26 13:03:11 -0400908 ssize_t host_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400909
Chuck Lever87c59422018-03-28 13:29:11 -0400910 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400911 rqstp->rq_next_page = rqstp->rq_respages + 1;
912 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
Trond Myklebust83a63072019-08-26 13:03:11 -0400913 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400914}
915
Chuck Lever87c59422018-03-28 13:29:11 -0400916__be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
917 struct file *file, loff_t offset,
Trond Myklebust83a63072019-08-26 13:03:11 -0400918 struct kvec *vec, int vlen, unsigned long *count,
919 u32 *eof)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400920{
Christoph Hellwig73da8522017-05-27 11:16:53 +0300921 struct iov_iter iter;
Trond Myklebust83a63072019-08-26 13:03:11 -0400922 loff_t ppos = offset;
923 ssize_t host_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -0400924
Chuck Lever87c59422018-03-28 13:29:11 -0400925 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
David Howellsaa563d72018-10-20 00:57:56 +0100926 iov_iter_kvec(&iter, READ, vec, vlen, *count);
Trond Myklebust83a63072019-08-26 13:03:11 -0400927 host_err = vfs_iter_read(file, &iter, &ppos, 0);
928 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700931/*
932 * Gathered writes: If another process is currently writing to the file,
933 * there's a high chance this is another nfsd (triggered by a bulk write
934 * from a client's biod). Rather than syncing the file with each write
935 * request, we sleep for 10 msec.
936 *
937 * I don't know if this roughly approximates C. Juszak's idea of
938 * gathered writes, but it's a nice and simple solution (IMHO), and it
939 * seems to work:-)
940 *
941 * Note: we do this only in the NFSv2 case, since v3 and higher have a
942 * better tool (separate unstable writes and commits) for solving this
943 * problem.
944 */
945static int wait_for_concurrent_writes(struct file *file)
946{
Al Viro496ad9a2013-01-23 17:07:38 -0500947 struct inode *inode = file_inode(file);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700948 static ino_t last_ino;
949 static dev_t last_dev;
950 int err = 0;
951
952 if (atomic_read(&inode->i_writecount) > 1
953 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
954 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
955 msleep(10);
956 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
957 }
958
959 if (inode->i_state & I_DIRTY) {
960 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100961 err = vfs_fsync(file, 0);
J. Bruce Fieldsd911df72009-06-15 16:03:53 -0700962 }
963 last_ino = inode->i_ino;
964 last_dev = inode->i_sb->s_dev;
965 return err;
966}
967
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200968__be32
Trond Myklebust16f8f892020-01-06 13:40:29 -0500969nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 loff_t offset, struct kvec *vec, int vlen,
Trond Myklebust19e06632020-01-06 13:40:37 -0500971 unsigned long *cnt, int stable,
972 __be32 *verf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Chuck Leverfb7622c2021-12-28 12:41:32 -0500974 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebust16f8f892020-01-06 13:40:29 -0500975 struct file *file = nf->nf_file;
Trond Myklebust01cbf382020-11-30 17:03:19 -0500976 struct super_block *sb = file_inode(file)->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct svc_export *exp;
Christoph Hellwig73da8522017-05-27 11:16:53 +0300978 struct iov_iter iter;
Trond Myklebust555dbf12021-12-18 20:38:01 -0500979 errseq_t since;
Chuck Leverd890be12018-03-27 10:53:27 -0400980 __be32 nfserr;
Al Viro6264d692006-10-19 23:28:58 -0700981 int host_err;
Trond Myklebust48e03bc2009-06-05 12:35:15 -0400982 int use_wgather;
Kent Overstreete49dbbf2013-03-22 11:18:24 -0700983 loff_t pos = offset;
Trond Myklebust01cbf382020-11-30 17:03:19 -0500984 unsigned long exp_op_flags = 0;
NeilBrown86584522014-05-12 11:22:47 +1000985 unsigned int pflags = current->flags;
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200986 rwf_t flags = 0;
Trond Myklebust01cbf382020-11-30 17:03:19 -0500987 bool restore_flags = false;
NeilBrown86584522014-05-12 11:22:47 +1000988
Chuck Leverd890be12018-03-27 10:53:27 -0400989 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
990
Trond Myklebust01cbf382020-11-30 17:03:19 -0500991 if (sb->s_export_op)
992 exp_op_flags = sb->s_export_op->flags;
993
994 if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
995 !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
NeilBrown86584522014-05-12 11:22:47 +1000996 /*
NeilBrowna37b0712020-06-01 21:48:18 -0700997 * We want throttling in balance_dirty_pages()
998 * and shrink_inactive_list() to only consider
999 * the backingdev we are writing to, so that nfs to
NeilBrown86584522014-05-12 11:22:47 +10001000 * localhost doesn't cause nfsd to lock up due to all
1001 * the client's dirty pages or its congested queue.
1002 */
NeilBrowna37b0712020-06-01 21:48:18 -07001003 current->flags |= PF_LOCAL_THROTTLE;
Trond Myklebust01cbf382020-11-30 17:03:19 -05001004 restore_flags = true;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Kinglong Mee865d50b2016-12-31 21:00:21 +08001007 exp = fhp->fh_export;
Trond Myklebust48e03bc2009-06-05 12:35:15 -04001008 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!EX_ISSYNC(exp))
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001011 stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Christoph Hellwig24368aa2016-04-07 08:52:04 -07001013 if (stable && !use_wgather)
1014 flags |= RWF_SYNC;
1015
David Howellsaa563d72018-10-20 00:57:56 +01001016 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
Trond Myklebust555dbf12021-12-18 20:38:01 -05001017 since = READ_ONCE(file->f_wb_err);
Chuck Lever33388b32021-12-28 14:19:41 -05001018 if (verf)
Chuck Lever3988a572021-12-30 10:22:05 -05001019 nfsd_copy_write_verifier(verf, nn);
Chuck Lever33388b32021-12-28 14:19:41 -05001020 host_err = vfs_iter_write(file, &iter, &pos, flags);
Trond Myklebust7bf94c62020-01-06 13:40:31 -05001021 if (host_err < 0) {
Chuck Lever3988a572021-12-30 10:22:05 -05001022 nfsd_reset_write_verifier(nn);
Chuck Lever75acacb2021-12-28 14:27:56 -05001023 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001024 goto out_nfserr;
Trond Myklebust7bf94c62020-01-06 13:40:31 -05001025 }
Trond Myklebust09a80f22019-12-17 12:33:33 -05001026 *cnt = host_err;
Amir Goldstein20ad8562021-01-06 09:52:36 +02001027 nfsd_stats_io_write_add(exp, *cnt);
Eric Paris2a12a9d2009-12-17 21:24:21 -05001028 fsnotify_modify(file);
Trond Myklebust555dbf12021-12-18 20:38:01 -05001029 host_err = filemap_check_wb_err(file->f_mapping, since);
1030 if (host_err < 0)
1031 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001033 if (stable && use_wgather) {
Christoph Hellwig24368aa2016-04-07 08:52:04 -07001034 host_err = wait_for_concurrent_writes(file);
Chuck Lever75acacb2021-12-28 14:27:56 -05001035 if (host_err < 0) {
Chuck Lever3988a572021-12-30 10:22:05 -05001036 nfsd_reset_write_verifier(nn);
Chuck Lever75acacb2021-12-28 14:27:56 -05001037 trace_nfsd_writeverf_reset(nn, rqstp, host_err);
1038 }
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
J. Bruce Fieldse4636d52009-06-15 19:07:13 -07001041out_nfserr:
Chuck Leverd890be12018-03-27 10:53:27 -04001042 if (host_err >= 0) {
1043 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1044 nfserr = nfs_ok;
1045 } else {
1046 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1047 nfserr = nfserrno(host_err);
1048 }
Trond Myklebust01cbf382020-11-30 17:03:19 -05001049 if (restore_flags)
NeilBrowna37b0712020-06-01 21:48:18 -07001050 current_restore_flags(pflags, PF_LOCAL_THROTTLE);
Chuck Leverd890be12018-03-27 10:53:27 -04001051 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001054/*
1055 * Read data from a file. count must contain the requested read count
1056 * on entry. On return, *count contains the number of bytes actually read.
1057 * N.B. After this call fhp needs an fh_put
1058 */
1059__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04001060 loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1061 u32 *eof)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001062{
Jeff Layton48cd7b52019-08-18 14:18:50 -04001063 struct nfsd_file *nf;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001064 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001065 __be32 err;
1066
Chuck Leverf394b622018-03-27 10:53:11 -04001067 trace_nfsd_read_start(rqstp, fhp, offset, *count);
Jeff Layton48cd7b52019-08-18 14:18:50 -04001068 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001069 if (err)
1070 return err;
1071
Jeff Layton48cd7b52019-08-18 14:18:50 -04001072 file = nf->nf_file;
Christoph Hellwiga4058c52017-05-27 11:16:54 +03001073 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
Trond Myklebust83a63072019-08-26 13:03:11 -04001074 err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
Christoph Hellwiga4058c52017-05-27 11:16:54 +03001075 else
Trond Myklebust83a63072019-08-26 13:03:11 -04001076 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001077
Jeff Layton48cd7b52019-08-18 14:18:50 -04001078 nfsd_file_put(nf);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04001079
Chuck Leverf394b622018-03-27 10:53:11 -04001080 trace_nfsd_read_done(rqstp, fhp, offset, *count);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001081
J. Bruce Fieldsfa0a2122010-07-27 16:48:54 -04001082 return err;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/*
1086 * Write data to a file.
1087 * The stable flag requests synchronous writes.
1088 * N.B. After this call fhp needs an fh_put
1089 */
Al Viro6264d692006-10-19 23:28:58 -07001090__be32
Kinglong Mee52e380e2016-12-31 21:00:13 +08001091nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
Trond Myklebust19e06632020-01-06 13:40:37 -05001092 struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1093 __be32 *verf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Jeff Laytonb4935232019-08-18 14:18:49 -04001095 struct nfsd_file *nf;
1096 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Chuck Leverf394b622018-03-27 10:53:11 -04001098 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
Jeff Layton6e8b50d2015-11-17 06:52:23 -05001099
Jeff Laytonb4935232019-08-18 14:18:49 -04001100 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
Kinglong Mee52e380e2016-12-31 21:00:13 +08001101 if (err)
1102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Trond Myklebust16f8f892020-01-06 13:40:29 -05001104 err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
Trond Myklebust19e06632020-01-06 13:40:37 -05001105 vlen, cnt, stable, verf);
Jeff Laytonb4935232019-08-18 14:18:49 -04001106 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107out:
Chuck Leverf394b622018-03-27 10:53:11 -04001108 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return err;
1110}
1111
1112#ifdef CONFIG_NFSD_V3
1113/*
1114 * Commit all pending writes to stable storage.
Trond Myklebustaa696a62010-01-29 16:44:25 -05001115 *
1116 * Note: we only guarantee that data that lies within the range specified
1117 * by the 'offset' and 'count' parameters will be synced.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 *
1119 * Unfortunately we cannot lock the file to make sure we return full WCC
1120 * data to the client, as locking happens lower down in the filesystem.
1121 */
Al Viro6264d692006-10-19 23:28:58 -07001122__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
Trond Myklebust524ff1a2020-01-06 13:40:36 -05001124 loff_t offset, unsigned long count, __be32 *verf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Chuck Lever2c445a02021-12-28 14:26:03 -05001126 struct nfsd_net *nn;
Jeff Layton5920afa2019-08-18 14:18:51 -04001127 struct nfsd_file *nf;
1128 loff_t end = LLONG_MAX;
1129 __be32 err = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Trond Myklebustaa696a62010-01-29 16:44:25 -05001131 if (offset < 0)
1132 goto out;
1133 if (count != 0) {
1134 end = offset + (loff_t)count - 1;
1135 if (end < offset)
1136 goto out;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jeff Layton5920afa2019-08-18 14:18:51 -04001139 err = nfsd_file_acquire(rqstp, fhp,
1140 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001141 if (err)
Trond Myklebustaa696a62010-01-29 16:44:25 -05001142 goto out;
Chuck Lever2c445a02021-12-28 14:26:03 -05001143 nn = net_generic(nf->nf_net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (EX_ISSYNC(fhp->fh_export)) {
Trond Myklebust555dbf12021-12-18 20:38:01 -05001145 errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
1146 int err2;
Trond Myklebustaa696a62010-01-29 16:44:25 -05001147
Trond Myklebust555dbf12021-12-18 20:38:01 -05001148 err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001149 switch (err2) {
1150 case 0:
Chuck Lever3988a572021-12-30 10:22:05 -05001151 nfsd_copy_write_verifier(verf, nn);
Trond Myklebust555dbf12021-12-18 20:38:01 -05001152 err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
1153 since);
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001154 break;
1155 case -EINVAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 err = nfserr_notsupp;
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001157 break;
1158 default:
Chuck Lever3988a572021-12-30 10:22:05 -05001159 nfsd_reset_write_verifier(nn);
Chuck Lever75acacb2021-12-28 14:27:56 -05001160 trace_nfsd_writeverf_reset(nn, rqstp, err2);
Trond Myklebustbbf2f092019-09-02 13:02:58 -04001161 }
Trond Myklebust555dbf12021-12-18 20:38:01 -05001162 err = nfserrno(err2);
Trond Myklebust524ff1a2020-01-06 13:40:36 -05001163 } else
Chuck Lever3988a572021-12-30 10:22:05 -05001164 nfsd_copy_write_verifier(verf, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jeff Layton5920afa2019-08-18 14:18:51 -04001166 nfsd_file_put(nf);
Trond Myklebustaa696a62010-01-29 16:44:25 -05001167out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return err;
1169}
1170#endif /* CONFIG_NFSD_V3 */
1171
Adrian Bunkf2b0dee2008-02-13 23:30:26 +02001172static __be32
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001173nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1174 struct iattr *iap)
1175{
1176 /*
1177 * Mode has already been set earlier in create:
1178 */
1179 iap->ia_valid &= ~ATTR_MODE;
1180 /*
1181 * Setting uid/gid works only for root. Irix appears to
1182 * send along the gid on create when it tries to implement
1183 * setgid directories via NFS:
1184 */
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001185 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001186 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1187 if (iap->ia_valid)
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01001188 return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001189 /* Callers expect file metadata to be committed here */
Jeff Layton722b6202014-07-03 07:54:19 -04001190 return nfserrno(commit_metadata(resfhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001191}
1192
wengang wang4ac35c22009-02-10 11:27:51 +08001193/* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1194 * setting size to 0 may fail for some specific file systems by the permission
1195 * checking which requires WRITE permission but the mode is 000.
1196 * we ignore the resizing(to 0) on the just new created file, since the size is
1197 * 0 after file created.
1198 *
1199 * call this only after vfs_create() is called.
1200 * */
1201static void
1202nfsd_check_ignore_resizing(struct iattr *iap)
1203{
1204 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1205 iap->ia_valid &= ~ATTR_SIZE;
1206}
1207
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001208/* The parent directory should already be locked: */
Al Viro6264d692006-10-19 23:28:58 -07001209__be32
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001210nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 char *fname, int flen, struct iattr *iap,
1212 int type, dev_t rdev, struct svc_fh *resfhp)
1213{
Dan Carpenter2b118852016-08-03 22:05:00 +03001214 struct dentry *dentry, *dchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001216 __be32 err;
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001217 __be32 err2;
Al Viro6264d692006-10-19 23:28:58 -07001218 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001221 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001223 dchild = dget(resfhp->fh_dentry);
1224 if (!fhp->fh_locked) {
1225 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
Al Viroa6a9f182013-09-16 10:57:01 -04001226 dentry);
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001227 err = nfserr_io;
1228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Oleg Drokin7eed34f2016-07-14 23:20:22 -04001231 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1232 if (err)
1233 goto out;
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!(iap->ia_valid & ATTR_MODE))
1236 iap->ia_mode = 0;
1237 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1238
J. Bruce Fields22cf8412020-06-16 16:43:18 -04001239 if (!IS_POSIXACL(dirp))
1240 iap->ia_mode &= ~current_umask();
1241
J. Bruce Fields088406b2006-11-08 17:44:59 -08001242 err = 0;
Jan Kara4a55c102012-06-12 16:20:33 +02001243 host_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 switch (type) {
1245 case S_IFREG:
Christian Brauner6521f892021-01-21 14:19:33 +01001246 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
wengang wang4ac35c22009-02-10 11:27:51 +08001247 if (!host_err)
1248 nfsd_check_ignore_resizing(iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 break;
1250 case S_IFDIR:
Christian Brauner6521f892021-01-21 14:19:33 +01001251 host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
Al Viro3819bb02018-05-11 17:03:19 -04001252 if (!host_err && unlikely(d_unhashed(dchild))) {
1253 struct dentry *d;
1254 d = lookup_one_len(dchild->d_name.name,
1255 dchild->d_parent,
1256 dchild->d_name.len);
1257 if (IS_ERR(d)) {
1258 host_err = PTR_ERR(d);
1259 break;
1260 }
1261 if (unlikely(d_is_negative(d))) {
1262 dput(d);
1263 err = nfserr_serverfault;
1264 goto out;
1265 }
1266 dput(resfhp->fh_dentry);
1267 resfhp->fh_dentry = dget(d);
1268 err = fh_update(resfhp);
1269 dput(dchild);
1270 dchild = d;
1271 if (err)
1272 goto out;
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275 case S_IFCHR:
1276 case S_IFBLK:
1277 case S_IFIFO:
1278 case S_IFSOCK:
Christian Brauner6521f892021-01-21 14:19:33 +01001279 host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1280 iap->ia_mode, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
J. Bruce Fields714232742016-07-22 12:03:46 -04001282 default:
1283 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1284 type);
1285 host_err = -EINVAL;
Dave Hansen463c3192008-02-15 14:37:57 -08001286 }
Jan Kara4a55c102012-06-12 16:20:33 +02001287 if (host_err < 0)
Dave Hansen463c3192008-02-15 14:37:57 -08001288 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Ben Myersf5019122010-02-17 14:05:11 -06001290 err = nfsd_create_setattr(rqstp, resfhp, iap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Ben Myersf5019122010-02-17 14:05:11 -06001292 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001293 * nfsd_create_setattr already committed the child. Transactional
1294 * filesystems had a chance to commit changes for both parent and
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001295 * child simultaneously making the following commit_metadata a
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001296 * noop.
Ben Myersf5019122010-02-17 14:05:11 -06001297 */
1298 err2 = nfserrno(commit_metadata(fhp));
J. Bruce Fields5c002b32007-11-30 16:55:23 -05001299 if (err2)
1300 err = err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Update the file handle to get the new inode info.
1303 */
1304 if (!err)
1305 err = fh_update(resfhp);
1306out:
Dan Carpenter2b118852016-08-03 22:05:00 +03001307 dput(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return err;
1309
1310out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001311 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto out;
1313}
1314
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001315/*
1316 * Create a filesystem object (regular, directory, special).
1317 * Note that the parent directory is left locked.
1318 *
1319 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1320 */
1321__be32
1322nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1323 char *fname, int flen, struct iattr *iap,
1324 int type, dev_t rdev, struct svc_fh *resfhp)
1325{
1326 struct dentry *dentry, *dchild = NULL;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001327 __be32 err;
1328 int host_err;
1329
1330 if (isdotent(fname, flen))
1331 return nfserr_exist;
1332
J. Bruce Fieldsfa081392016-07-21 16:00:12 -04001333 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001334 if (err)
1335 return err;
1336
1337 dentry = fhp->fh_dentry;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001338
1339 host_err = fh_want_write(fhp);
1340 if (host_err)
1341 return nfserrno(host_err);
1342
1343 fh_lock_nested(fhp, I_MUTEX_PARENT);
1344 dchild = lookup_one_len(fname, dentry, flen);
1345 host_err = PTR_ERR(dchild);
1346 if (IS_ERR(dchild))
1347 return nfserrno(host_err);
1348 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
Josef Bacik502aa0a2016-08-10 14:46:27 -04001349 /*
1350 * We unconditionally drop our ref to dchild as fh_compose will have
1351 * already grabbed its own ref for it.
1352 */
1353 dput(dchild);
1354 if (err)
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001355 return err;
J. Bruce Fieldsb44061d2016-07-20 16:16:06 -04001356 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1357 rdev, resfhp);
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_NFSD_V3
Mi Jinlongac6721a2011-04-20 17:06:25 +08001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/*
Mi Jinlongac6721a2011-04-20 17:06:25 +08001363 * NFSv3 and NFSv4 version of nfsd_create
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Al Viro6264d692006-10-19 23:28:58 -07001365__be32
Mi Jinlongac6721a2011-04-20 17:06:25 +08001366do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 char *fname, int flen, struct iattr *iap,
1368 struct svc_fh *resfhp, int createmode, u32 *verifier,
J. Bruce Fields856121b2011-10-13 11:37:11 -04001369 bool *truncp, bool *created)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct dentry *dentry, *dchild = NULL;
1372 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001373 __be32 err;
1374 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 __u32 v_mtime=0, v_atime=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 err = nfserr_perm;
1378 if (!flen)
1379 goto out;
1380 err = nfserr_exist;
1381 if (isdotent(fname, flen))
1382 goto out;
1383 if (!(iap->ia_valid & ATTR_MODE))
1384 iap->ia_mode = 0;
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001385 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (err)
1387 goto out;
1388
1389 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001390 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jan Kara4a55c102012-06-12 16:20:33 +02001392 host_err = fh_want_write(fhp);
1393 if (host_err)
1394 goto out_nfserr;
1395
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001396 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
1399 * Compose the response file handle.
1400 */
1401 dchild = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001402 host_err = PTR_ERR(dchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (IS_ERR(dchild))
1404 goto out_nfserr;
1405
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001406 /* If file doesn't exist, check for permissions to create one */
David Howells2b0143b2015-03-17 22:25:59 +00001407 if (d_really_is_negative(dchild)) {
Sachin Prabhu1574dff2011-04-20 13:09:35 +01001408 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1409 if (err)
1410 goto out;
1411 }
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1414 if (err)
1415 goto out;
1416
Mi Jinlongac6721a2011-04-20 17:06:25 +08001417 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001418 /* solaris7 gets confused (bugid 4218508) if these have
J. Bruce Fields2336d692021-10-15 14:42:11 -04001419 * the high bit set, as do xfs filesystems without the
1420 * "bigtime" feature. So just clear the high bits. If this is
Jeff Layton749997e2007-07-31 00:37:51 -07001421 * ever changed to use different attrs for storing the
1422 * verifier, then do_open_lookup() will also need to be fixed
1423 * accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
1425 v_mtime = verifier[0]&0x7fffffff;
1426 v_atime = verifier[1]&0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
David Howells2b0143b2015-03-17 22:25:59 +00001429 if (d_really_is_positive(dchild)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 err = 0;
1431
1432 switch (createmode) {
1433 case NFS3_CREATE_UNCHECKED:
David Howellse36cb0b2015-01-29 12:02:35 +00001434 if (! d_is_reg(dchild))
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -04001435 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 else if (truncp) {
1437 /* in nfsv4, we need to treat this case a little
1438 * differently. we don't want to truncate the
1439 * file now; this would be wrong if the OPEN
1440 * fails for some other reason. furthermore,
1441 * if the size is nonzero, we should ignore it
1442 * according to spec!
1443 */
1444 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1445 }
1446 else {
1447 iap->ia_valid &= ATTR_SIZE;
1448 goto set_attr;
1449 }
1450 break;
1451 case NFS3_CREATE_EXCLUSIVE:
David Howells2b0143b2015-03-17 22:25:59 +00001452 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1453 && d_inode(dchild)->i_atime.tv_sec == v_atime
1454 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001455 if (created)
zhengbin384a7cca2019-12-25 11:19:34 +08001456 *created = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
Neil Brown7007c902012-12-07 15:40:55 -05001458 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001459 fallthrough;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001460 case NFS4_CREATE_EXCLUSIVE4_1:
David Howells2b0143b2015-03-17 22:25:59 +00001461 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1462 && d_inode(dchild)->i_atime.tv_sec == v_atime
1463 && d_inode(dchild)->i_size == 0 ) {
Neil Brown7007c902012-12-07 15:40:55 -05001464 if (created)
zhengbin384a7cca2019-12-25 11:19:34 +08001465 *created = true;
Mi Jinlongac6721a2011-04-20 17:06:25 +08001466 goto set_attr;
Neil Brown7007c902012-12-07 15:40:55 -05001467 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001468 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 case NFS3_CREATE_GUARDED:
1470 err = nfserr_exist;
1471 }
Al Virobad0dcf2011-11-23 12:03:18 -05001472 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto out;
1474 }
1475
J. Bruce Fields22cf8412020-06-16 16:43:18 -04001476 if (!IS_POSIXACL(dirp))
1477 iap->ia_mode &= ~current_umask();
1478
Christian Brauner6521f892021-01-21 14:19:33 +01001479 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
Dave Hansen463c3192008-02-15 14:37:57 -08001480 if (host_err < 0) {
Al Virobad0dcf2011-11-23 12:03:18 -05001481 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out_nfserr;
Dave Hansen463c3192008-02-15 14:37:57 -08001483 }
J. Bruce Fields81ac95c2006-11-08 17:44:40 -08001484 if (created)
zhengbin384a7cca2019-12-25 11:19:34 +08001485 *created = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
wengang wang4ac35c22009-02-10 11:27:51 +08001487 nfsd_check_ignore_resizing(iap);
1488
Mi Jinlongac6721a2011-04-20 17:06:25 +08001489 if (nfsd_create_is_exclusive(createmode)) {
Peter Staubachc3978522007-01-26 00:57:00 -08001490 /* Cram the verifier into atime/mtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
Peter Staubachc3978522007-01-26 00:57:00 -08001492 | ATTR_MTIME_SET|ATTR_ATIME_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* XXX someone who knows this better please fix it for nsec */
1494 iap->ia_mtime.tv_sec = v_mtime;
1495 iap->ia_atime.tv_sec = v_atime;
1496 iap->ia_mtime.tv_nsec = 0;
1497 iap->ia_atime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 set_attr:
Ben Myersf5019122010-02-17 14:05:11 -06001501 err = nfsd_create_setattr(rqstp, resfhp, iap);
1502
1503 /*
Trond Myklebust0f3a24b2014-07-01 18:27:53 -04001504 * nfsd_create_setattr already committed the child
1505 * (and possibly also the parent).
Ben Myersf5019122010-02-17 14:05:11 -06001506 */
1507 if (!err)
1508 err = nfserrno(commit_metadata(fhp));
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001509
1510 /*
1511 * Update the filehandle to get the new inode info.
1512 */
1513 if (!err)
1514 err = fh_update(resfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 out:
1517 fh_unlock(fhp);
1518 if (dchild && !IS_ERR(dchild))
1519 dput(dchild);
Jan Kara4a55c102012-06-12 16:20:33 +02001520 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return err;
1522
1523 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001524 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 goto out;
1526}
1527#endif /* CONFIG_NFSD_V3 */
1528
1529/*
1530 * Read a symlink. On entry, *lenp must contain the maximum path length that
1531 * fits into the buffer. On return, it contains the true length.
1532 * N.B. After this call fhp needs an fh_put
1533 */
Al Viro6264d692006-10-19 23:28:58 -07001534__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1536{
Al Viro6264d692006-10-19 23:28:58 -07001537 __be32 err;
Al Viro4d7edbc2017-05-27 16:11:23 -04001538 const char *link;
Al Viro68ac1232012-03-15 08:21:57 -04001539 struct path path;
Al Viro4d7edbc2017-05-27 16:11:23 -04001540 DEFINE_DELAYED_CALL(done);
1541 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001543 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
Al Viro4d7edbc2017-05-27 16:11:23 -04001544 if (unlikely(err))
1545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Al Viro68ac1232012-03-15 08:21:57 -04001547 path.mnt = fhp->fh_export->ex_path.mnt;
1548 path.dentry = fhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Al Viro4d7edbc2017-05-27 16:11:23 -04001550 if (unlikely(!d_is_symlink(path.dentry)))
1551 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Al Viro68ac1232012-03-15 08:21:57 -04001553 touch_atime(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Al Viro4d7edbc2017-05-27 16:11:23 -04001555 link = vfs_get_link(path.dentry, &done);
1556 if (IS_ERR(link))
1557 return nfserrno(PTR_ERR(link));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Al Viro4d7edbc2017-05-27 16:11:23 -04001559 len = strlen(link);
1560 if (len < *lenp)
1561 *lenp = len;
1562 memcpy(buf, link, *lenp);
1563 do_delayed_call(&done);
1564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * Create a symlink and look up its inode
1569 * N.B. After this call _both_ fhp and resfhp need an fh_put
1570 */
Al Viro6264d692006-10-19 23:28:58 -07001571__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1573 char *fname, int flen,
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001574 char *path,
Kinglong Mee1e444f52014-07-01 17:48:02 +08001575 struct svc_fh *resfhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct dentry *dentry, *dnew;
Al Viro6264d692006-10-19 23:28:58 -07001578 __be32 err, cerr;
1579 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 err = nfserr_noent;
J. Bruce Fields52ee0432014-06-20 11:52:21 -04001582 if (!flen || path[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto out;
1584 err = nfserr_exist;
1585 if (isdotent(fname, flen))
1586 goto out;
1587
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001588 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err)
1590 goto out;
Jan Kara4a55c102012-06-12 16:20:33 +02001591
1592 host_err = fh_want_write(fhp);
1593 if (host_err)
1594 goto out_nfserr;
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 fh_lock(fhp);
1597 dentry = fhp->fh_dentry;
1598 dnew = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001599 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (IS_ERR(dnew))
1601 goto out_nfserr;
1602
Christian Brauner6521f892021-01-21 14:19:33 +01001603 host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
Al Viro6264d692006-10-19 23:28:58 -07001604 err = nfserrno(host_err);
J. Bruce Fieldseeeadbb2021-05-14 18:21:37 -04001605 fh_unlock(fhp);
Ben Myersf5019122010-02-17 14:05:11 -06001606 if (!err)
1607 err = nfserrno(commit_metadata(fhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Al Virobad0dcf2011-11-23 12:03:18 -05001609 fh_drop_write(fhp);
Dave Hansen75c3f292008-02-15 14:37:45 -08001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1612 dput(dnew);
1613 if (err==0) err = cerr;
1614out:
1615 return err;
1616
1617out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001618 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto out;
1620}
1621
1622/*
1623 * Create a hardlink
1624 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1625 */
Al Viro6264d692006-10-19 23:28:58 -07001626__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1628 char *name, int len, struct svc_fh *tfhp)
1629{
1630 struct dentry *ddir, *dnew, *dold;
J. Bruce Fields55b13352010-07-19 16:38:24 -04001631 struct inode *dirp;
Al Viro6264d692006-10-19 23:28:58 -07001632 __be32 err;
1633 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001635 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (err)
1637 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001638 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (err)
1640 goto out;
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001641 err = nfserr_isdir;
David Howellse36cb0b2015-01-29 12:02:35 +00001642 if (d_is_dir(tfhp->fh_dentry))
J. Bruce Fields7d818a72011-08-15 16:59:55 -04001643 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 err = nfserr_perm;
1645 if (!len)
1646 goto out;
1647 err = nfserr_exist;
1648 if (isdotent(name, len))
1649 goto out;
1650
Jan Kara4a55c102012-06-12 16:20:33 +02001651 host_err = fh_want_write(tfhp);
1652 if (host_err) {
1653 err = nfserrno(host_err);
1654 goto out;
1655 }
1656
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001657 fh_lock_nested(ffhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ddir = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001659 dirp = d_inode(ddir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 dnew = lookup_one_len(name, ddir, len);
Al Viro6264d692006-10-19 23:28:58 -07001662 host_err = PTR_ERR(dnew);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (IS_ERR(dnew))
1664 goto out_nfserr;
1665
1666 dold = tfhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
J. Bruce Fields4795bb32011-01-11 13:55:46 -05001668 err = nfserr_noent;
David Howells2b0143b2015-03-17 22:25:59 +00001669 if (d_really_is_negative(dold))
Jan Kara4a55c102012-06-12 16:20:33 +02001670 goto out_dput;
Christian Brauner6521f892021-01-21 14:19:33 +01001671 host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
J. Bruce Fieldseeeadbb2021-05-14 18:21:37 -04001672 fh_unlock(ffhp);
Al Viro6264d692006-10-19 23:28:58 -07001673 if (!host_err) {
Ben Myersf5019122010-02-17 14:05:11 -06001674 err = nfserrno(commit_metadata(ffhp));
1675 if (!err)
1676 err = nfserrno(commit_metadata(tfhp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 } else {
Al Viro6264d692006-10-19 23:28:58 -07001678 if (host_err == -EXDEV && rqstp->rq_vers == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 err = nfserr_acces;
1680 else
Al Viro6264d692006-10-19 23:28:58 -07001681 err = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Dave Hansen75c3f292008-02-15 14:37:45 -08001683out_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 dput(dnew);
David M. Richter270d56e2006-06-30 01:56:15 -07001685out_unlock:
1686 fh_unlock(ffhp);
Jan Kara4a55c102012-06-12 16:20:33 +02001687 fh_drop_write(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688out:
1689 return err;
1690
1691out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001692 err = nfserrno(host_err);
David M. Richter270d56e2006-06-30 01:56:15 -07001693 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Jeff Layton7775ec52019-08-18 14:18:57 -04001696static void
1697nfsd_close_cached_files(struct dentry *dentry)
1698{
1699 struct inode *inode = d_inode(dentry);
1700
1701 if (inode && S_ISREG(inode->i_mode))
1702 nfsd_file_close_inode_sync(inode);
1703}
1704
1705static bool
1706nfsd_has_cached_files(struct dentry *dentry)
1707{
1708 bool ret = false;
1709 struct inode *inode = d_inode(dentry);
1710
1711 if (inode && S_ISREG(inode->i_mode))
1712 ret = nfsd_file_is_cached(inode);
1713 return ret;
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716/*
1717 * Rename a file
1718 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1719 */
Al Viro6264d692006-10-19 23:28:58 -07001720__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1722 struct svc_fh *tfhp, char *tname, int tlen)
1723{
1724 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1725 struct inode *fdir, *tdir;
Al Viro6264d692006-10-19 23:28:58 -07001726 __be32 err;
1727 int host_err;
Jeff Layton7f84b482020-11-30 17:03:16 -05001728 bool close_cached = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001730 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (err)
1732 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001733 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (err)
1735 goto out;
1736
1737 fdentry = ffhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001738 fdir = d_inode(fdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 tdentry = tfhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001741 tdir = d_inode(tdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 err = nfserr_perm;
1744 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1745 goto out;
1746
Jeff Layton7775ec52019-08-18 14:18:57 -04001747retry:
Jan Kara4a55c102012-06-12 16:20:33 +02001748 host_err = fh_want_write(ffhp);
1749 if (host_err) {
1750 err = nfserrno(host_err);
1751 goto out;
1752 }
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* cannot use fh_lock as we need deadlock protective ordering
1755 * so do it by hand */
1756 trap = lock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001757 ffhp->fh_locked = tfhp->fh_locked = true;
Chuck Leverfcb5e3f2021-12-24 14:36:49 -05001758 fh_fill_pre_attrs(ffhp);
1759 fh_fill_pre_attrs(tfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 odentry = lookup_one_len(fname, fdentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001762 host_err = PTR_ERR(odentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (IS_ERR(odentry))
1764 goto out_nfserr;
1765
Al Viro6264d692006-10-19 23:28:58 -07001766 host_err = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001767 if (d_really_is_negative(odentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001769 host_err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (odentry == trap)
1771 goto out_dput_old;
1772
1773 ndentry = lookup_one_len(tname, tdentry, tlen);
Al Viro6264d692006-10-19 23:28:58 -07001774 host_err = PTR_ERR(ndentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (IS_ERR(ndentry))
1776 goto out_dput_old;
Al Viro6264d692006-10-19 23:28:58 -07001777 host_err = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (ndentry == trap)
1779 goto out_dput_new;
1780
Dave Hansen9079b1e2008-02-15 14:37:49 -08001781 host_err = -EXDEV;
1782 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1783 goto out_dput_new;
J. Bruce Fieldsaa387d6c2013-04-15 16:03:46 -04001784 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1785 goto out_dput_new;
Dave Hansen9079b1e2008-02-15 14:37:49 -08001786
Jeff Layton7f84b482020-11-30 17:03:16 -05001787 if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1788 nfsd_has_cached_files(ndentry)) {
1789 close_cached = true;
Jeff Layton7775ec52019-08-18 14:18:57 -04001790 goto out_dput_old;
1791 } else {
Christian Brauner9fe61452021-01-21 14:19:32 +01001792 struct renamedata rd = {
Christian Brauner6521f892021-01-21 14:19:33 +01001793 .old_mnt_userns = &init_user_ns,
Christian Brauner9fe61452021-01-21 14:19:32 +01001794 .old_dir = fdir,
1795 .old_dentry = odentry,
Christian Brauner6521f892021-01-21 14:19:33 +01001796 .new_mnt_userns = &init_user_ns,
Christian Brauner9fe61452021-01-21 14:19:32 +01001797 .new_dir = tdir,
1798 .new_dentry = ndentry,
1799 };
1800 host_err = vfs_rename(&rd);
Jeff Layton7775ec52019-08-18 14:18:57 -04001801 if (!host_err) {
1802 host_err = commit_metadata(tfhp);
1803 if (!host_err)
1804 host_err = commit_metadata(ffhp);
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 out_dput_new:
1808 dput(ndentry);
1809 out_dput_old:
1810 dput(odentry);
1811 out_nfserr:
Al Viro6264d692006-10-19 23:28:58 -07001812 err = nfserrno(host_err);
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001813 /*
1814 * We cannot rely on fh_unlock on the two filehandles,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * as that would do the wrong thing if the two directories
J. Bruce Fieldsfbb74a32014-03-28 16:43:17 -04001816 * were the same, so again we do it by hand.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Jeff Layton7f84b482020-11-30 17:03:16 -05001818 if (!close_cached) {
Chuck Leverfcb5e3f2021-12-24 14:36:49 -05001819 fh_fill_post_attrs(ffhp);
1820 fh_fill_post_attrs(tfhp);
Jeff Layton7775ec52019-08-18 14:18:57 -04001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 unlock_rename(tdentry, fdentry);
Jeff Laytonaaf91ec2015-09-17 08:28:39 -04001823 ffhp->fh_locked = tfhp->fh_locked = false;
Jan Kara4a55c102012-06-12 16:20:33 +02001824 fh_drop_write(ffhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Jeff Layton7775ec52019-08-18 14:18:57 -04001826 /*
1827 * If the target dentry has cached open files, then we need to try to
1828 * close them prior to doing the rename. Flushing delayed fput
1829 * shouldn't be done with locks held however, so we delay it until this
1830 * point and then reattempt the whole shebang.
1831 */
Jeff Layton7f84b482020-11-30 17:03:16 -05001832 if (close_cached) {
1833 close_cached = false;
Jeff Layton7775ec52019-08-18 14:18:57 -04001834 nfsd_close_cached_files(ndentry);
1835 dput(ndentry);
1836 goto retry;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838out:
1839 return err;
1840}
1841
1842/*
1843 * Unlink a file or directory
1844 * N.B. After this call fhp needs an fh_put
1845 */
Al Viro6264d692006-10-19 23:28:58 -07001846__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1848 char *fname, int flen)
1849{
1850 struct dentry *dentry, *rdentry;
1851 struct inode *dirp;
Yu Hsiang Huange5d74a22021-05-14 11:58:29 +08001852 struct inode *rinode;
Al Viro6264d692006-10-19 23:28:58 -07001853 __be32 err;
1854 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 err = nfserr_acces;
1857 if (!flen || isdotent(fname, flen))
1858 goto out;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001859 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (err)
1861 goto out;
1862
Jan Kara4a55c102012-06-12 16:20:33 +02001863 host_err = fh_want_write(fhp);
1864 if (host_err)
1865 goto out_nfserr;
1866
Peter Zijlstra12fd3522006-10-02 02:18:03 -07001867 fh_lock_nested(fhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001869 dirp = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 rdentry = lookup_one_len(fname, dentry, flen);
Al Viro6264d692006-10-19 23:28:58 -07001872 host_err = PTR_ERR(rdentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (IS_ERR(rdentry))
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001874 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
David Howells2b0143b2015-03-17 22:25:59 +00001876 if (d_really_is_negative(rdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 dput(rdentry);
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001878 host_err = -ENOENT;
1879 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Yu Hsiang Huange5d74a22021-05-14 11:58:29 +08001881 rinode = d_inode(rdentry);
1882 ihold(rinode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 if (!type)
David Howells2b0143b2015-03-17 22:25:59 +00001885 type = d_inode(rdentry)->i_mode & S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jeff Layton7775ec52019-08-18 14:18:57 -04001887 if (type != S_IFDIR) {
Jeff Layton7f84b482020-11-30 17:03:16 -05001888 if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1889 nfsd_close_cached_files(rdentry);
Christian Brauner6521f892021-01-21 14:19:33 +01001890 host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
Jeff Layton7775ec52019-08-18 14:18:57 -04001891 } else {
Christian Brauner6521f892021-01-21 14:19:33 +01001892 host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
Jeff Layton7775ec52019-08-18 14:18:57 -04001893 }
1894
J. Bruce Fieldseeeadbb2021-05-14 18:21:37 -04001895 fh_unlock(fhp);
J. Bruce Fields541ce982011-01-14 20:00:02 -05001896 if (!host_err)
1897 host_err = commit_metadata(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 dput(rdentry);
Yu Hsiang Huange5d74a22021-05-14 11:58:29 +08001899 iput(rinode); /* truncate the inode here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
J. Bruce Fields0ca0c9d2019-04-12 16:26:30 -04001901out_drop_write:
1902 fh_drop_write(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903out_nfserr:
NeilBrown466e16f2019-11-28 13:56:43 +11001904 if (host_err == -EBUSY) {
1905 /* name is mounted-on. There is no perfect
1906 * error status.
1907 */
1908 if (nfsd_v4client(rqstp))
1909 err = nfserr_file_open;
1910 else
1911 err = nfserr_acces;
1912 } else {
1913 err = nfserrno(host_err);
1914 }
YAMAMOTO Takashif193fba2006-01-18 17:43:13 -08001915out:
1916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
1919/*
David Woodhouse14f7dd62008-07-31 20:29:12 +01001920 * We do this buffering because we must not call back into the file
1921 * system's ->lookup() method from the filldir callback. That may well
1922 * deadlock a number of file systems.
1923 *
1924 * This is based heavily on the implementation of same in XFS.
1925 */
1926struct buffered_dirent {
1927 u64 ino;
1928 loff_t offset;
1929 int namlen;
1930 unsigned int d_type;
1931 char name[];
1932};
1933
1934struct readdir_data {
Al Viro5c0ba4e2013-05-15 13:52:59 -04001935 struct dir_context ctx;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001936 char *dirent;
1937 size_t used;
Al Viro53c9c5c2008-08-24 07:29:52 -04001938 int full;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001939};
1940
Miklos Szerediac7576f2014-10-30 17:37:34 +01001941static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1942 int namlen, loff_t offset, u64 ino,
1943 unsigned int d_type)
David Woodhouse2628b762008-07-31 17:16:51 +01001944{
Miklos Szerediac7576f2014-10-30 17:37:34 +01001945 struct readdir_data *buf =
1946 container_of(ctx, struct readdir_data, ctx);
David Woodhouse14f7dd62008-07-31 20:29:12 +01001947 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1948 unsigned int reclen;
1949
1950 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
Al Viro53c9c5c2008-08-24 07:29:52 -04001951 if (buf->used + reclen > PAGE_SIZE) {
1952 buf->full = 1;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001953 return -EINVAL;
Al Viro53c9c5c2008-08-24 07:29:52 -04001954 }
David Woodhouse14f7dd62008-07-31 20:29:12 +01001955
1956 de->namlen = namlen;
1957 de->offset = offset;
1958 de->ino = ino;
1959 de->d_type = d_type;
1960 memcpy(de->name, name, namlen);
1961 buf->used += reclen;
1962
1963 return 0;
1964}
1965
Chuck Lever6019ce072021-03-05 13:57:40 -05001966static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1967 nfsd_filldir_t func, struct readdir_cd *cdp,
1968 loff_t *offsetp)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001969{
David Woodhouse14f7dd62008-07-31 20:29:12 +01001970 struct buffered_dirent *de;
David Woodhouse2628b762008-07-31 17:16:51 +01001971 int host_err;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001972 int size;
1973 loff_t offset;
Al Viroac6614b2013-05-22 22:22:04 -04001974 struct readdir_data buf = {
1975 .ctx.actor = nfsd_buffered_filldir,
1976 .dirent = (void *)__get_free_page(GFP_KERNEL)
1977 };
David Woodhouse2628b762008-07-31 17:16:51 +01001978
David Woodhouse14f7dd62008-07-31 20:29:12 +01001979 if (!buf.dirent)
David Woodhouse2f9092e2009-04-20 23:18:37 +01001980 return nfserrno(-ENOMEM);
David Woodhouse2628b762008-07-31 17:16:51 +01001981
David Woodhouse14f7dd62008-07-31 20:29:12 +01001982 offset = *offsetp;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001983
1984 while (1) {
1985 unsigned int reclen;
1986
Doug Nazarb726e922008-11-05 06:16:28 -05001987 cdp->err = nfserr_eof; /* will be cleared on successful read */
David Woodhouse14f7dd62008-07-31 20:29:12 +01001988 buf.used = 0;
Al Viro53c9c5c2008-08-24 07:29:52 -04001989 buf.full = 0;
David Woodhouse14f7dd62008-07-31 20:29:12 +01001990
Al Viro5c0ba4e2013-05-15 13:52:59 -04001991 host_err = iterate_dir(file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -04001992 if (buf.full)
1993 host_err = 0;
1994
1995 if (host_err < 0)
David Woodhouse14f7dd62008-07-31 20:29:12 +01001996 break;
1997
1998 size = buf.used;
1999
2000 if (!size)
2001 break;
2002
David Woodhouse14f7dd62008-07-31 20:29:12 +01002003 de = (struct buffered_dirent *)buf.dirent;
2004 while (size > 0) {
2005 offset = de->offset;
2006
2007 if (func(cdp, de->name, de->namlen, de->offset,
2008 de->ino, de->d_type))
David Woodhouse2f9092e2009-04-20 23:18:37 +01002009 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002010
2011 if (cdp->err != nfs_ok)
David Woodhouse2f9092e2009-04-20 23:18:37 +01002012 break;
David Woodhouse14f7dd62008-07-31 20:29:12 +01002013
Chuck Lever6019ce072021-03-05 13:57:40 -05002014 trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
2015
David Woodhouse14f7dd62008-07-31 20:29:12 +01002016 reclen = ALIGN(sizeof(*de) + de->namlen,
2017 sizeof(u64));
2018 size -= reclen;
2019 de = (struct buffered_dirent *)((char *)de + reclen);
2020 }
David Woodhouse2f9092e2009-04-20 23:18:37 +01002021 if (size > 0) /* We bailed out early */
2022 break;
2023
David Woodhousec002a6c2008-08-17 17:21:18 +01002024 offset = vfs_llseek(file, 0, SEEK_CUR);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002025 }
2026
David Woodhouse14f7dd62008-07-31 20:29:12 +01002027 free_page((unsigned long)(buf.dirent));
David Woodhouse2628b762008-07-31 17:16:51 +01002028
2029 if (host_err)
2030 return nfserrno(host_err);
David Woodhouse14f7dd62008-07-31 20:29:12 +01002031
2032 *offsetp = offset;
2033 return cdp->err;
David Woodhouse2628b762008-07-31 17:16:51 +01002034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
2037 * Read entries from a directory.
2038 * The NFSv3/4 verifier we ignore for now.
2039 */
Al Viro6264d692006-10-19 23:28:58 -07002040__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
Miklos Szerediac7576f2014-10-30 17:37:34 +01002042 struct readdir_cd *cdp, nfsd_filldir_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Al Viro6264d692006-10-19 23:28:58 -07002044 __be32 err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct file *file;
2046 loff_t offset = *offsetp;
Bernd Schubert06effdb2012-03-18 22:44:50 -04002047 int may_flags = NFSD_MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Bernd Schubert06effdb2012-03-18 22:44:50 -04002049 /* NFSv2 only supports 32 bit cookies */
2050 if (rqstp->rq_vers > 2)
2051 may_flags |= NFSD_MAY_64BIT_COOKIE;
2052
2053 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (err)
2055 goto out;
2056
Jeff Laytonb108fe62012-04-25 15:30:00 -04002057 offset = vfs_llseek(file, offset, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (offset < 0) {
2059 err = nfserrno((int)offset);
2060 goto out_close;
2061 }
2062
Chuck Lever6019ce072021-03-05 13:57:40 -05002063 err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (err == nfserr_eof || err == nfserr_toosmall)
2066 err = nfs_ok; /* can still be found in ->err */
2067out_close:
Christoph Hellwigfd891452015-04-28 15:41:16 +02002068 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069out:
2070 return err;
2071}
2072
2073/*
2074 * Get file system stats
2075 * N.B. After this call fhp needs an fh_put
2076 */
Al Viro6264d692006-10-19 23:28:58 -07002077__be32
J. Bruce Fields04716e62008-08-07 13:00:20 -04002078nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002080 __be32 err;
2081
2082 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
Takashi Iwaif6360ef2010-08-13 15:53:49 +02002083 if (!err) {
2084 struct path path = {
2085 .mnt = fhp->fh_export->ex_path.mnt,
2086 .dentry = fhp->fh_dentry,
2087 };
2088 if (vfs_statfs(&path, stat))
2089 err = nfserr_io;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return err;
2092}
2093
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002094static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002095{
J. Bruce Fieldsc7d51402007-07-19 01:49:20 -07002096 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
J. Bruce Fieldse22841c2007-07-19 01:49:20 -07002097}
2098
Frank van der Linden32119442020-06-23 22:39:23 +00002099#ifdef CONFIG_NFSD_V4
2100/*
2101 * Helper function to translate error numbers. In the case of xattr operations,
2102 * some error codes need to be translated outside of the standard translations.
2103 *
2104 * ENODATA needs to be translated to nfserr_noxattr.
2105 * E2BIG to nfserr_xattr2big.
2106 *
2107 * Additionally, vfs_listxattr can return -ERANGE. This means that the
2108 * file has too many extended attributes to retrieve inside an
2109 * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
2110 * filesystems will allow the adding of extended attributes until they hit
2111 * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
2112 * So, at that point, the attributes are present and valid, but can't
2113 * be retrieved using listxattr, since the upper level xattr code enforces
2114 * the XATTR_LIST_MAX limit.
2115 *
2116 * This bug means that we need to deal with listxattr returning -ERANGE. The
2117 * best mapping is to return TOOSMALL.
2118 */
2119static __be32
2120nfsd_xattr_errno(int err)
2121{
2122 switch (err) {
2123 case -ENODATA:
2124 return nfserr_noxattr;
2125 case -E2BIG:
2126 return nfserr_xattr2big;
2127 case -ERANGE:
2128 return nfserr_toosmall;
2129 }
2130 return nfserrno(err);
2131}
2132
2133/*
2134 * Retrieve the specified user extended attribute. To avoid always
2135 * having to allocate the maximum size (since we are not getting
2136 * a maximum size from the RPC), do a probe + alloc. Hold a reader
2137 * lock on i_rwsem to prevent the extended attribute from changing
2138 * size while we're doing this.
2139 */
2140__be32
2141nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2142 void **bufp, int *lenp)
2143{
2144 ssize_t len;
2145 __be32 err;
2146 char *buf;
2147 struct inode *inode;
2148 struct dentry *dentry;
2149
2150 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2151 if (err)
2152 return err;
2153
2154 err = nfs_ok;
2155 dentry = fhp->fh_dentry;
2156 inode = d_inode(dentry);
2157
2158 inode_lock_shared(inode);
2159
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01002160 len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
Frank van der Linden32119442020-06-23 22:39:23 +00002161
2162 /*
2163 * Zero-length attribute, just return.
2164 */
2165 if (len == 0) {
2166 *bufp = NULL;
2167 *lenp = 0;
2168 goto out;
2169 }
2170
2171 if (len < 0) {
2172 err = nfsd_xattr_errno(len);
2173 goto out;
2174 }
2175
2176 if (len > *lenp) {
2177 err = nfserr_toosmall;
2178 goto out;
2179 }
2180
2181 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2182 if (buf == NULL) {
2183 err = nfserr_jukebox;
2184 goto out;
2185 }
2186
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01002187 len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
Frank van der Linden32119442020-06-23 22:39:23 +00002188 if (len <= 0) {
2189 kvfree(buf);
2190 buf = NULL;
2191 err = nfsd_xattr_errno(len);
2192 }
2193
2194 *lenp = len;
2195 *bufp = buf;
2196
2197out:
2198 inode_unlock_shared(inode);
2199
2200 return err;
2201}
2202
2203/*
2204 * Retrieve the xattr names. Since we can't know how many are
2205 * user extended attributes, we must get all attributes here,
2206 * and have the XDR encode filter out the "user." ones.
2207 *
2208 * While this could always just allocate an XATTR_LIST_MAX
2209 * buffer, that's a waste, so do a probe + allocate. To
2210 * avoid any changes between the probe and allocate, wrap
2211 * this in inode_lock.
2212 */
2213__be32
2214nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2215 int *lenp)
2216{
2217 ssize_t len;
2218 __be32 err;
2219 char *buf;
2220 struct inode *inode;
2221 struct dentry *dentry;
2222
2223 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2224 if (err)
2225 return err;
2226
2227 dentry = fhp->fh_dentry;
2228 inode = d_inode(dentry);
2229 *lenp = 0;
2230
2231 inode_lock_shared(inode);
2232
2233 len = vfs_listxattr(dentry, NULL, 0);
2234 if (len <= 0) {
2235 err = nfsd_xattr_errno(len);
2236 goto out;
2237 }
2238
2239 if (len > XATTR_LIST_MAX) {
2240 err = nfserr_xattr2big;
2241 goto out;
2242 }
2243
2244 /*
2245 * We're holding i_rwsem - use GFP_NOFS.
2246 */
2247 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2248 if (buf == NULL) {
2249 err = nfserr_jukebox;
2250 goto out;
2251 }
2252
2253 len = vfs_listxattr(dentry, buf, len);
2254 if (len <= 0) {
2255 kvfree(buf);
2256 err = nfsd_xattr_errno(len);
2257 goto out;
2258 }
2259
2260 *lenp = len;
2261 *bufp = buf;
2262
2263 err = nfs_ok;
2264out:
2265 inode_unlock_shared(inode);
2266
2267 return err;
2268}
2269
2270/*
2271 * Removexattr and setxattr need to call fh_lock to both lock the inode
2272 * and set the change attribute. Since the top-level vfs_removexattr
2273 * and vfs_setxattr calls already do their own inode_lock calls, call
2274 * the _locked variant. Pass in a NULL pointer for delegated_inode,
2275 * and let the client deal with NFS4ERR_DELAY (same as with e.g.
2276 * setattr and remove).
2277 */
2278__be32
2279nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2280{
Chuck Lever82372842020-09-11 14:47:48 -04002281 __be32 err;
2282 int ret;
Frank van der Linden32119442020-06-23 22:39:23 +00002283
2284 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2285 if (err)
2286 return err;
2287
2288 ret = fh_want_write(fhp);
2289 if (ret)
2290 return nfserrno(ret);
2291
2292 fh_lock(fhp);
2293
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01002294 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2295 name, NULL);
Frank van der Linden32119442020-06-23 22:39:23 +00002296
2297 fh_unlock(fhp);
2298 fh_drop_write(fhp);
2299
2300 return nfsd_xattr_errno(ret);
2301}
2302
2303__be32
2304nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2305 void *buf, u32 len, u32 flags)
2306{
Chuck Lever82372842020-09-11 14:47:48 -04002307 __be32 err;
2308 int ret;
Frank van der Linden32119442020-06-23 22:39:23 +00002309
2310 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2311 if (err)
2312 return err;
2313
2314 ret = fh_want_write(fhp);
2315 if (ret)
2316 return nfserrno(ret);
2317 fh_lock(fhp);
2318
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01002319 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2320 len, flags, NULL);
Frank van der Linden32119442020-06-23 22:39:23 +00002321
2322 fh_unlock(fhp);
2323 fh_drop_write(fhp);
2324
2325 return nfsd_xattr_errno(ret);
2326}
2327#endif
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329/*
2330 * Check for a user's access permissions to this inode.
2331 */
Al Viro6264d692006-10-19 23:28:58 -07002332__be32
J. Bruce Fields0ec757d2007-07-17 04:04:48 -07002333nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2334 struct dentry *dentry, int acc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
David Howells2b0143b2015-03-17 22:25:59 +00002336 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 int err;
2338
J. Bruce Fieldsaea93392011-04-10 10:35:12 -04002339 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
2341#if 0
2342 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2343 acc,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002344 (acc & NFSD_MAY_READ)? " read" : "",
2345 (acc & NFSD_MAY_WRITE)? " write" : "",
2346 (acc & NFSD_MAY_EXEC)? " exec" : "",
2347 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2348 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2349 (acc & NFSD_MAY_LOCK)? " lock" : "",
2350 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 inode->i_mode,
2352 IS_IMMUTABLE(inode)? " immut" : "",
2353 IS_APPEND(inode)? " append" : "",
Dave Hansen2c463e92008-02-15 14:37:56 -08002354 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 dprintk(" owner %d/%d user %d/%d\n",
David Howells5cc0a842008-11-14 10:38:58 +11002356 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357#endif
2358
2359 /* Normally we reject any write/sattr etc access on a read-only file
2360 * system. But if it is IRIX doing check on write-access for a
2361 * device special file, we ignore rofs.
2362 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002363 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2364 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
Dave Hansen2c463e92008-02-15 14:37:56 -08002365 if (exp_rdonly(rqstp, exp) ||
2366 __mnt_is_readonly(exp->ex_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return nfserr_rofs;
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002368 if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return nfserr_perm;
2370 }
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002371 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return nfserr_perm;
2373
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002374 if (acc & NFSD_MAY_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* If we cannot rely on authentication in NLM requests,
2376 * just allow locks, otherwise require read permission, or
2377 * ownership
2378 */
2379 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2380 return 0;
2381 else
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002382 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 /*
2385 * The file owner always gets access permission for accesses that
2386 * would normally be checked at open time. This is to make
2387 * file access work even when the client has done a fchmod(fd, 0).
2388 *
2389 * However, `cp foo bar' should fail nevertheless when bar is
2390 * readonly. A sensible way to do this might be to reject all
2391 * attempts to truncate a read-only file, because a creat() call
2392 * always implies file truncation.
2393 * ... but this isn't really fair. A process may reasonably call
2394 * ftruncate on an open file descriptor on a file with perm 000.
2395 * We must trust the client to do permission checking - using "ACCESS"
2396 * with NFSv3.
2397 */
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002398 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002399 uid_eq(inode->i_uid, current_fsuid()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return 0;
2401
Miklos Szeredi8837abc2008-06-16 13:20:29 +02002402 /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
Christian Brauner47291ba2021-01-21 14:19:24 +01002403 err = inode_permission(&init_user_ns, inode,
2404 acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 /* Allow read access to binaries even when mode 111 */
2407 if (err == -EACCES && S_ISREG(inode->i_mode) &&
J. Bruce Fieldsa0432262011-08-25 10:48:39 -04002408 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2409 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
Christian Brauner47291ba2021-01-21 14:19:24 +01002410 err = inode_permission(&init_user_ns, inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 return err? nfserrno(err) : 0;
2413}