mm/memunmap: don't access uninitialized memmap in memunmap_pages()
[linux-2.6-block.git] / fs / nfs / namespace.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
55a97593
TM
2/*
3 * linux/fs/nfs/namespace.c
4 *
5 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
54ceac45 6 * - Modified by David Howells <dhowells@redhat.com>
55a97593
TM
7 *
8 * NFS namespace
9 */
10
ddda8e0a 11#include <linux/module.h>
55a97593 12#include <linux/dcache.h>
5a0e3ad6 13#include <linux/gfp.h>
55a97593
TM
14#include <linux/mount.h>
15#include <linux/namei.h>
16#include <linux/nfs_fs.h>
17#include <linux/string.h>
18#include <linux/sunrpc/clnt.h>
19#include <linux/vfs.h>
7ebb9315 20#include <linux/sunrpc/gss_api.h>
f7b422b1 21#include "internal.h"
55a97593
TM
22
23#define NFSDBG_FACILITY NFSDBG_VFS
24
65f27f38 25static void nfs_expire_automounts(struct work_struct *work);
f7b422b1 26
a3dab293 27static LIST_HEAD(nfs_automount_list);
65f27f38 28static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
51d8fa6a
TM
29int nfs_mountpoint_expiry_timeout = 500 * HZ;
30
f7b422b1
DH
31/*
32 * nfs_path - reconstruct the path given an arbitrary dentry
b514f872 33 * @base - used to return pointer to the end of devname part of path
f7b422b1
DH
34 * @dentry - pointer to dentry
35 * @buffer - result buffer
36 * @buflen - length of buffer
97a54868 37 * @flags - options (see below)
f7b422b1 38 *
b514f872
AV
39 * Helper function for constructing the server pathname
40 * by arbitrary hashed dentry.
f7b422b1
DH
41 *
42 * This is mainly for use in figuring out the path on the
b514f872
AV
43 * server side when automounting on top of an existing partition
44 * and in generating /proc/mounts and friends.
97a54868
BH
45 *
46 * Supported flags:
47 * NFS_PATH_CANONICAL: ensure there is exactly one slash after
48 * the original device (export) name
49 * (if unset, the original name is returned verbatim)
f7b422b1 50 */
97a54868
BH
51char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
52 unsigned flags)
f7b422b1 53{
949854d0 54 char *end;
f7b422b1 55 int namelen;
949854d0 56 unsigned seq;
b514f872 57 const char *base;
f7b422b1 58
949854d0
NP
59rename_retry:
60 end = buffer+buflen;
f7b422b1
DH
61 *--end = '\0';
62 buflen--;
949854d0
NP
63
64 seq = read_seqbegin(&rename_lock);
65 rcu_read_lock();
b514f872
AV
66 while (1) {
67 spin_lock(&dentry->d_lock);
68 if (IS_ROOT(dentry))
69 break;
f7b422b1
DH
70 namelen = dentry->d_name.len;
71 buflen -= namelen + 1;
72 if (buflen < 0)
ce510193 73 goto Elong_unlock;
f7b422b1
DH
74 end -= namelen;
75 memcpy(end, dentry->d_name.name, namelen);
76 *--end = '/';
b514f872 77 spin_unlock(&dentry->d_lock);
f7b422b1
DH
78 dentry = dentry->d_parent;
79 }
b514f872
AV
80 if (read_seqretry(&rename_lock, seq)) {
81 spin_unlock(&dentry->d_lock);
82 rcu_read_unlock();
949854d0 83 goto rename_retry;
b514f872 84 }
97a54868 85 if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
b514f872
AV
86 if (--buflen < 0) {
87 spin_unlock(&dentry->d_lock);
88 rcu_read_unlock();
0b75b35c 89 goto Elong;
b514f872 90 }
0b75b35c
TM
91 *--end = '/';
92 }
b514f872
AV
93 *p = end;
94 base = dentry->d_fsdata;
95 if (!base) {
96 spin_unlock(&dentry->d_lock);
97 rcu_read_unlock();
98 WARN_ON(1);
99 return end;
100 }
f7b422b1 101 namelen = strlen(base);
86a6c211 102 if (*end == '/') {
97a54868
BH
103 /* Strip off excess slashes in base string */
104 while (namelen > 0 && base[namelen - 1] == '/')
105 namelen--;
106 }
f7b422b1 107 buflen -= namelen;
b514f872 108 if (buflen < 0) {
1c34092a 109 spin_unlock(&dentry->d_lock);
b514f872 110 rcu_read_unlock();
f7b422b1 111 goto Elong;
b514f872 112 }
f7b422b1
DH
113 end -= namelen;
114 memcpy(end, base, namelen);
b514f872
AV
115 spin_unlock(&dentry->d_lock);
116 rcu_read_unlock();
f7b422b1 117 return end;
ce510193 118Elong_unlock:
1c34092a 119 spin_unlock(&dentry->d_lock);
949854d0
NP
120 rcu_read_unlock();
121 if (read_seqretry(&rename_lock, seq))
122 goto rename_retry;
f7b422b1
DH
123Elong:
124 return ERR_PTR(-ENAMETOOLONG);
125}
89d77c8f 126EXPORT_SYMBOL_GPL(nfs_path);
f7b422b1 127
55a97593 128/*
36d43a43
DH
129 * nfs_d_automount - Handle crossing a mountpoint on the server
130 * @path - The mountpoint
55a97593
TM
131 *
132 * When we encounter a mountpoint on the server, we want to set up
133 * a mountpoint on the client too, to prevent inode numbers from
134 * colliding, and to allow "df" to work properly.
135 * On NFSv4, we also want to allow for the fact that different
136 * filesystems may be migrated to different servers in a failover
137 * situation, and that different filesystems may want to use
138 * different security flavours.
139 */
36d43a43 140struct vfsmount *nfs_d_automount(struct path *path)
55a97593
TM
141{
142 struct vfsmount *mnt;
2b0143b5 143 struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
a4d7f168
TM
144 struct nfs_fh *fh = NULL;
145 struct nfs_fattr *fattr = NULL;
55a97593 146
36d43a43 147 if (IS_ROOT(path->dentry))
e36d48e9 148 return ERR_PTR(-ESTALE);
44d5759d 149
36d43a43 150 mnt = ERR_PTR(-ENOMEM);
a4d7f168
TM
151 fh = nfs_alloc_fhandle();
152 fattr = nfs_alloc_fattr();
153 if (fh == NULL || fattr == NULL)
36d43a43 154 goto out;
a4d7f168 155
281cad46 156 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
55a97593 157 if (IS_ERR(mnt))
36d43a43 158 goto out;
55a97593 159
ea5b778a
DH
160 mntget(mnt); /* prevent immediate expiration */
161 mnt_set_expiry(mnt, &nfs_automount_list);
162 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
36d43a43 163
55a97593 164out:
a4d7f168
TM
165 nfs_free_fattr(fattr);
166 nfs_free_fhandle(fh);
36d43a43 167 return mnt;
55a97593
TM
168}
169
ab225417 170static int
a528d35e
DH
171nfs_namespace_getattr(const struct path *path, struct kstat *stat,
172 u32 request_mask, unsigned int query_flags)
ab225417 173{
a528d35e
DH
174 if (NFS_FH(d_inode(path->dentry))->size != 0)
175 return nfs_getattr(path, stat, request_mask, query_flags);
176 generic_fillattr(d_inode(path->dentry), stat);
ab225417
TM
177 return 0;
178}
179
180static int
181nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
182{
2b0143b5 183 if (NFS_FH(d_inode(dentry))->size != 0)
ab225417
TM
184 return nfs_setattr(dentry, attr);
185 return -EACCES;
186}
187
92e1d5be 188const struct inode_operations nfs_mountpoint_inode_operations = {
55a97593 189 .getattr = nfs_getattr,
ab225417 190 .setattr = nfs_setattr,
55a97593 191};
51d8fa6a 192
92e1d5be 193const struct inode_operations nfs_referral_inode_operations = {
ab225417
TM
194 .getattr = nfs_namespace_getattr,
195 .setattr = nfs_namespace_setattr,
6b97fd3d
MN
196};
197
65f27f38 198static void nfs_expire_automounts(struct work_struct *work)
51d8fa6a 199{
65f27f38 200 struct list_head *list = &nfs_automount_list;
51d8fa6a
TM
201
202 mark_mounts_for_expiry(list);
203 if (!list_empty(list))
204 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
205}
206
207void nfs_release_automount_timer(void)
208{
3d39c691 209 if (list_empty(&nfs_automount_list))
560aef74 210 cancel_delayed_work(&nfs_automount_task);
51d8fa6a 211}
f7b422b1
DH
212
213/*
214 * Clone a mountpoint of the appropriate type
215 */
509de811
DH
216static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
217 const char *devname,
f7b422b1
DH
218 struct nfs_clone_mount *mountdata)
219{
93faccbb 220 return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
f7b422b1
DH
221}
222
223/**
224 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
302fad7b
TM
225 * @dentry: parent directory
226 * @fh: filehandle for new root dentry
227 * @fattr: attributes for new root inode
228 * @authflavor: security flavor to use when performing the mount
f7b422b1
DH
229 *
230 */
281cad46
BS
231struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
232 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
f7b422b1
DH
233{
234 struct nfs_clone_mount mountdata = {
f8ad9c4b 235 .sb = dentry->d_sb,
f7b422b1
DH
236 .dentry = dentry,
237 .fh = fh,
238 .fattr = fattr,
7ebb9315 239 .authflavor = authflavor,
f7b422b1 240 };
e36d48e9 241 struct vfsmount *mnt;
f7b422b1
DH
242 char *page = (char *) __get_free_page(GFP_USER);
243 char *devname;
244
f7b422b1 245 if (page == NULL)
e36d48e9
AS
246 return ERR_PTR(-ENOMEM);
247
b514f872 248 devname = nfs_devname(dentry, page, PAGE_SIZE);
f7b422b1 249 if (IS_ERR(devname))
fe3b81b4 250 mnt = ERR_CAST(devname);
e36d48e9
AS
251 else
252 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
54ceac45 253
e36d48e9 254 free_page((unsigned long)page);
f7b422b1
DH
255 return mnt;
256}
89d77c8f 257EXPORT_SYMBOL_GPL(nfs_do_submount);
281cad46
BS
258
259struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
260 struct nfs_fh *fh, struct nfs_fattr *fattr)
261{
262 int err;
263 struct dentry *parent = dget_parent(dentry);
264
265 /* Look it up again to get its attributes */
2b0143b5 266 err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
281cad46
BS
267 dput(parent);
268 if (err != 0)
269 return ERR_PTR(err);
270
271 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
272}
ddda8e0a 273EXPORT_SYMBOL_GPL(nfs_submount);