xref: /openbmc/linux/fs/afs/mntpt.c (revision e3d786a3)
1 /* mountpoint management
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/pagemap.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/gfp.h>
20 #include "internal.h"
21 
22 
23 static struct dentry *afs_mntpt_lookup(struct inode *dir,
24 				       struct dentry *dentry,
25 				       unsigned int flags);
26 static int afs_mntpt_open(struct inode *inode, struct file *file);
27 static void afs_mntpt_expiry_timed_out(struct work_struct *work);
28 
29 const struct file_operations afs_mntpt_file_operations = {
30 	.open		= afs_mntpt_open,
31 	.llseek		= noop_llseek,
32 };
33 
34 const struct inode_operations afs_mntpt_inode_operations = {
35 	.lookup		= afs_mntpt_lookup,
36 	.readlink	= page_readlink,
37 	.getattr	= afs_getattr,
38 	.listxattr	= afs_listxattr,
39 };
40 
41 const struct inode_operations afs_autocell_inode_operations = {
42 	.getattr	= afs_getattr,
43 };
44 
45 static LIST_HEAD(afs_vfsmounts);
46 static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
47 
48 static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
49 
50 /*
51  * no valid lookup procedure on this sort of dir
52  */
53 static struct dentry *afs_mntpt_lookup(struct inode *dir,
54 				       struct dentry *dentry,
55 				       unsigned int flags)
56 {
57 	_enter("%p,%p{%pd2}", dir, dentry, dentry);
58 	return ERR_PTR(-EREMOTE);
59 }
60 
61 /*
62  * no valid open procedure on this sort of dir
63  */
64 static int afs_mntpt_open(struct inode *inode, struct file *file)
65 {
66 	_enter("%p,%p{%pD2}", inode, file, file);
67 	return -EREMOTE;
68 }
69 
70 /*
71  * create a vfsmount to be automounted
72  */
73 static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
74 {
75 	struct afs_super_info *as;
76 	struct vfsmount *mnt;
77 	struct afs_vnode *vnode;
78 	struct page *page;
79 	char *devname, *options;
80 	bool rwpath = false;
81 	int ret;
82 
83 	_enter("{%pd}", mntpt);
84 
85 	BUG_ON(!d_inode(mntpt));
86 
87 	ret = -ENOMEM;
88 	devname = (char *) get_zeroed_page(GFP_KERNEL);
89 	if (!devname)
90 		goto error_no_devname;
91 
92 	options = (char *) get_zeroed_page(GFP_KERNEL);
93 	if (!options)
94 		goto error_no_options;
95 
96 	vnode = AFS_FS_I(d_inode(mntpt));
97 	if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
98 		/* if the directory is a pseudo directory, use the d_name */
99 		static const char afs_root_cell[] = ":root.cell.";
100 		unsigned size = mntpt->d_name.len;
101 
102 		ret = -ENOENT;
103 		if (size < 2 || size > AFS_MAXCELLNAME)
104 			goto error_no_page;
105 
106 		if (mntpt->d_name.name[0] == '.') {
107 			devname[0] = '%';
108 			memcpy(devname + 1, mntpt->d_name.name + 1, size - 1);
109 			memcpy(devname + size, afs_root_cell,
110 			       sizeof(afs_root_cell));
111 			rwpath = true;
112 		} else {
113 			devname[0] = '#';
114 			memcpy(devname + 1, mntpt->d_name.name, size);
115 			memcpy(devname + size + 1, afs_root_cell,
116 			       sizeof(afs_root_cell));
117 		}
118 	} else {
119 		/* read the contents of the AFS special symlink */
120 		loff_t size = i_size_read(d_inode(mntpt));
121 		char *buf;
122 
123 		ret = -EINVAL;
124 		if (size > PAGE_SIZE - 1)
125 			goto error_no_page;
126 
127 		page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
128 		if (IS_ERR(page)) {
129 			ret = PTR_ERR(page);
130 			goto error_no_page;
131 		}
132 
133 		if (PageError(page)) {
134 			ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
135 			goto error;
136 		}
137 
138 		buf = kmap_atomic(page);
139 		memcpy(devname, buf, size);
140 		kunmap_atomic(buf);
141 		put_page(page);
142 		page = NULL;
143 	}
144 
145 	/* work out what options we want */
146 	as = AFS_FS_S(mntpt->d_sb);
147 	if (as->cell) {
148 		memcpy(options, "cell=", 5);
149 		strcpy(options + 5, as->cell->name);
150 		if ((as->volume && as->volume->type == AFSVL_RWVOL) || rwpath)
151 			strcat(options, ",rwpath");
152 	}
153 
154 	/* try and do the mount */
155 	_debug("--- attempting mount %s -o %s ---", devname, options);
156 	mnt = vfs_submount(mntpt, &afs_fs_type, devname, options);
157 	_debug("--- mount result %p ---", mnt);
158 
159 	free_page((unsigned long) devname);
160 	free_page((unsigned long) options);
161 	_leave(" = %p", mnt);
162 	return mnt;
163 
164 error:
165 	put_page(page);
166 error_no_page:
167 	free_page((unsigned long) options);
168 error_no_options:
169 	free_page((unsigned long) devname);
170 error_no_devname:
171 	_leave(" = %d", ret);
172 	return ERR_PTR(ret);
173 }
174 
175 /*
176  * handle an automount point
177  */
178 struct vfsmount *afs_d_automount(struct path *path)
179 {
180 	struct vfsmount *newmnt;
181 
182 	_enter("{%pd}", path->dentry);
183 
184 	newmnt = afs_mntpt_do_automount(path->dentry);
185 	if (IS_ERR(newmnt))
186 		return newmnt;
187 
188 	mntget(newmnt); /* prevent immediate expiration */
189 	mnt_set_expiry(newmnt, &afs_vfsmounts);
190 	queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
191 			   afs_mntpt_expiry_timeout * HZ);
192 	_leave(" = %p", newmnt);
193 	return newmnt;
194 }
195 
196 /*
197  * handle mountpoint expiry timer going off
198  */
199 static void afs_mntpt_expiry_timed_out(struct work_struct *work)
200 {
201 	_enter("");
202 
203 	if (!list_empty(&afs_vfsmounts)) {
204 		mark_mounts_for_expiry(&afs_vfsmounts);
205 		queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
206 				   afs_mntpt_expiry_timeout * HZ);
207 	}
208 
209 	_leave("");
210 }
211 
212 /*
213  * kill the AFS mountpoint timer if it's still running
214  */
215 void afs_mntpt_kill_timer(void)
216 {
217 	_enter("");
218 
219 	ASSERT(list_empty(&afs_vfsmounts));
220 	cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
221 }
222