Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
ec26815a | 2 | /* mountpoint management |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
1da177e4 LT |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/init.h> | |
1da177e4 LT |
11 | #include <linux/fs.h> |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/mount.h> | |
14 | #include <linux/namei.h> | |
5a0e3ad6 | 15 | #include <linux/gfp.h> |
13fcc683 | 16 | #include <linux/fs_context.h> |
1da177e4 LT |
17 | #include "internal.h" |
18 | ||
19 | ||
20 | static struct dentry *afs_mntpt_lookup(struct inode *dir, | |
21 | struct dentry *dentry, | |
00cd8dd3 | 22 | unsigned int flags); |
1da177e4 | 23 | static int afs_mntpt_open(struct inode *inode, struct file *file); |
08e0e7c8 | 24 | static void afs_mntpt_expiry_timed_out(struct work_struct *work); |
1da177e4 | 25 | |
4b6f5d20 | 26 | const struct file_operations afs_mntpt_file_operations = { |
1da177e4 | 27 | .open = afs_mntpt_open, |
6038f373 | 28 | .llseek = noop_llseek, |
1da177e4 LT |
29 | }; |
30 | ||
754661f1 | 31 | const struct inode_operations afs_mntpt_inode_operations = { |
1da177e4 | 32 | .lookup = afs_mntpt_lookup, |
1da177e4 | 33 | .readlink = page_readlink, |
416351f2 | 34 | .getattr = afs_getattr, |
d3e3b7ea | 35 | .listxattr = afs_listxattr, |
1da177e4 LT |
36 | }; |
37 | ||
bec5eb61 | 38 | const struct inode_operations afs_autocell_inode_operations = { |
bec5eb61 | 39 | .getattr = afs_getattr, |
40 | }; | |
41 | ||
1da177e4 | 42 | static LIST_HEAD(afs_vfsmounts); |
08e0e7c8 | 43 | static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out); |
1da177e4 | 44 | |
c1206a2c | 45 | static unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
1da177e4 | 46 | |
c99c2171 DH |
47 | static const char afs_root_volume[] = "root.cell"; |
48 | ||
1da177e4 LT |
49 | /* |
50 | * no valid lookup procedure on this sort of dir | |
51 | */ | |
52 | static struct dentry *afs_mntpt_lookup(struct inode *dir, | |
53 | struct dentry *dentry, | |
00cd8dd3 | 54 | unsigned int flags) |
1da177e4 | 55 | { |
a455589f | 56 | _enter("%p,%p{%pd2}", dir, dentry, dentry); |
1da177e4 | 57 | return ERR_PTR(-EREMOTE); |
ec26815a | 58 | } |
1da177e4 | 59 | |
1da177e4 LT |
60 | /* |
61 | * no valid open procedure on this sort of dir | |
62 | */ | |
63 | static int afs_mntpt_open(struct inode *inode, struct file *file) | |
64 | { | |
a455589f | 65 | _enter("%p,%p{%pD2}", inode, file, file); |
1da177e4 | 66 | return -EREMOTE; |
ec26815a | 67 | } |
1da177e4 | 68 | |
1da177e4 | 69 | /* |
c99c2171 | 70 | * Set the parameters for the proposed superblock. |
1da177e4 | 71 | */ |
c99c2171 | 72 | static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt) |
1da177e4 | 73 | { |
c99c2171 DH |
74 | struct afs_fs_context *ctx = fc->fs_private; |
75 | struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb); | |
76 | struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt)); | |
77 | struct afs_cell *cell; | |
78 | const char *p; | |
1da177e4 LT |
79 | int ret; |
80 | ||
c99c2171 DH |
81 | if (fc->net_ns != src_as->net_ns) { |
82 | put_net(fc->net_ns); | |
83 | fc->net_ns = get_net(src_as->net_ns); | |
84 | } | |
1da177e4 | 85 | |
c99c2171 DH |
86 | if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) { |
87 | ctx->type = AFSVL_RWVOL; | |
88 | ctx->force = true; | |
89 | } | |
90 | if (ctx->cell) { | |
91 | afs_put_cell(ctx->net, ctx->cell); | |
92 | ctx->cell = NULL; | |
93 | } | |
bec5eb61 | 94 | if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) { |
95 | /* if the directory is a pseudo directory, use the d_name */ | |
bec5eb61 | 96 | unsigned size = mntpt->d_name.len; |
97 | ||
c99c2171 DH |
98 | if (size < 2) |
99 | return -ENOENT; | |
bec5eb61 | 100 | |
c99c2171 | 101 | p = mntpt->d_name.name; |
bec5eb61 | 102 | if (mntpt->d_name.name[0] == '.') { |
c99c2171 DH |
103 | size--; |
104 | p++; | |
105 | ctx->type = AFSVL_RWVOL; | |
106 | ctx->force = true; | |
bec5eb61 | 107 | } |
c99c2171 DH |
108 | if (size > AFS_MAXCELLNAME) |
109 | return -ENAMETOOLONG; | |
110 | ||
111 | cell = afs_lookup_cell(ctx->net, p, size, NULL, false); | |
112 | if (IS_ERR(cell)) { | |
113 | pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt); | |
114 | return PTR_ERR(cell); | |
115 | } | |
116 | ctx->cell = cell; | |
117 | ||
118 | ctx->volname = afs_root_volume; | |
119 | ctx->volnamesz = sizeof(afs_root_volume) - 1; | |
bec5eb61 | 120 | } else { |
121 | /* read the contents of the AFS special symlink */ | |
c99c2171 | 122 | struct page *page; |
2b0143b5 | 123 | loff_t size = i_size_read(d_inode(mntpt)); |
bec5eb61 | 124 | char *buf; |
125 | ||
c99c2171 DH |
126 | if (src_as->cell) |
127 | ctx->cell = afs_get_cell(src_as->cell); | |
128 | ||
158d5833 | 129 | if (size < 2 || size > PAGE_SIZE - 1) |
c99c2171 | 130 | return -EINVAL; |
bec5eb61 | 131 | |
2b0143b5 | 132 | page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL); |
c99c2171 DH |
133 | if (IS_ERR(page)) |
134 | return PTR_ERR(page); | |
bec5eb61 | 135 | |
f51375cd DH |
136 | if (PageError(page)) { |
137 | ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt); | |
c99c2171 DH |
138 | put_page(page); |
139 | return ret; | |
f51375cd | 140 | } |
bec5eb61 | 141 | |
c99c2171 | 142 | buf = kmap(page); |
158d5833 DH |
143 | ret = -EINVAL; |
144 | if (buf[size - 1] == '.') | |
145 | ret = vfs_parse_fs_string(fc, "source", buf, size - 1); | |
c99c2171 | 146 | kunmap(page); |
09cbfeaf | 147 | put_page(page); |
c99c2171 DH |
148 | if (ret < 0) |
149 | return ret; | |
1da177e4 LT |
150 | } |
151 | ||
c99c2171 DH |
152 | return 0; |
153 | } | |
1da177e4 | 154 | |
c99c2171 DH |
155 | /* |
156 | * create a vfsmount to be automounted | |
157 | */ | |
158 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) | |
159 | { | |
160 | struct fs_context *fc; | |
161 | struct vfsmount *mnt; | |
162 | int ret; | |
1da177e4 | 163 | |
c99c2171 | 164 | BUG_ON(!d_inode(mntpt)); |
1da177e4 | 165 | |
c99c2171 DH |
166 | fc = fs_context_for_submount(&afs_fs_type, mntpt); |
167 | if (IS_ERR(fc)) | |
168 | return ERR_CAST(fc); | |
169 | ||
170 | ret = afs_mntpt_set_params(fc, mntpt); | |
171 | if (!ret) | |
172 | mnt = fc_mount(fc); | |
173 | else | |
174 | mnt = ERR_PTR(ret); | |
175 | ||
176 | put_fs_context(fc); | |
177 | return mnt; | |
ec26815a | 178 | } |
1da177e4 | 179 | |
1da177e4 | 180 | /* |
d18610b0 | 181 | * handle an automount point |
1da177e4 | 182 | */ |
d18610b0 | 183 | struct vfsmount *afs_d_automount(struct path *path) |
1da177e4 LT |
184 | { |
185 | struct vfsmount *newmnt; | |
1da177e4 | 186 | |
a455589f | 187 | _enter("{%pd}", path->dentry); |
08e0e7c8 | 188 | |
d18610b0 DH |
189 | newmnt = afs_mntpt_do_automount(path->dentry); |
190 | if (IS_ERR(newmnt)) | |
191 | return newmnt; | |
1da177e4 | 192 | |
ea5b778a DH |
193 | mntget(newmnt); /* prevent immediate expiration */ |
194 | mnt_set_expiry(newmnt, &afs_vfsmounts); | |
195 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, | |
196 | afs_mntpt_expiry_timeout * HZ); | |
5ffc2836 | 197 | _leave(" = %p", newmnt); |
ea5b778a | 198 | return newmnt; |
ec26815a | 199 | } |
1da177e4 | 200 | |
1da177e4 LT |
201 | /* |
202 | * handle mountpoint expiry timer going off | |
203 | */ | |
08e0e7c8 | 204 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
1da177e4 | 205 | { |
08e0e7c8 DH |
206 | _enter(""); |
207 | ||
208 | if (!list_empty(&afs_vfsmounts)) { | |
209 | mark_mounts_for_expiry(&afs_vfsmounts); | |
0ad53eee TH |
210 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
211 | afs_mntpt_expiry_timeout * HZ); | |
08e0e7c8 DH |
212 | } |
213 | ||
214 | _leave(""); | |
215 | } | |
1da177e4 | 216 | |
08e0e7c8 DH |
217 | /* |
218 | * kill the AFS mountpoint timer if it's still running | |
219 | */ | |
220 | void afs_mntpt_kill_timer(void) | |
221 | { | |
222 | _enter(""); | |
1da177e4 | 223 | |
08e0e7c8 | 224 | ASSERT(list_empty(&afs_vfsmounts)); |
0ad53eee | 225 | cancel_delayed_work_sync(&afs_mntpt_expiry_timer); |
08e0e7c8 | 226 | } |