aboutsummaryrefslogtreecommitdiffstats
path: root/fs/exportfs/expfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/exportfs/expfs.c')
-rw-r--r--fs/exportfs/expfs.c267
1 files changed, 149 insertions, 118 deletions
diff --git a/fs/exportfs/expfs.c b/fs/exportfs/expfs.c
index a235f0016889..48a359dd286e 100644
--- a/fs/exportfs/expfs.c
+++ b/fs/exportfs/expfs.c
@@ -69,145 +69,162 @@ find_acceptable_alias(struct dentry *result,
69 return NULL; 69 return NULL;
70} 70}
71 71
72/* 72static bool dentry_connected(struct dentry *dentry)
73 * Find root of a disconnected subtree and return a reference to it.
74 */
75static struct dentry *
76find_disconnected_root(struct dentry *dentry)
77{ 73{
78 dget(dentry); 74 dget(dentry);
79 while (!IS_ROOT(dentry)) { 75 while (dentry->d_flags & DCACHE_DISCONNECTED) {
80 struct dentry *parent = dget_parent(dentry); 76 struct dentry *parent = dget_parent(dentry);
81 77
82 if (!(parent->d_flags & DCACHE_DISCONNECTED)) { 78 dput(dentry);
79 if (IS_ROOT(dentry)) {
83 dput(parent); 80 dput(parent);
84 break; 81 return false;
85 } 82 }
83 dentry = parent;
84 }
85 dput(dentry);
86 return true;
87}
88
89static void clear_disconnected(struct dentry *dentry)
90{
91 dget(dentry);
92 while (dentry->d_flags & DCACHE_DISCONNECTED) {
93 struct dentry *parent = dget_parent(dentry);
94
95 WARN_ON_ONCE(IS_ROOT(dentry));
96
97 spin_lock(&dentry->d_lock);
98 dentry->d_flags &= ~DCACHE_DISCONNECTED;
99 spin_unlock(&dentry->d_lock);
86 100
87 dput(dentry); 101 dput(dentry);
88 dentry = parent; 102 dentry = parent;
89 } 103 }
90 return dentry; 104 dput(dentry);
105}
106
107/*
108 * Reconnect a directory dentry with its parent.
109 *
110 * This can return a dentry, or NULL, or an error.
111 *
112 * In the first case the returned dentry is the parent of the given
113 * dentry, and may itself need to be reconnected to its parent.
114 *
115 * In the NULL case, a concurrent VFS operation has either renamed or
116 * removed this directory. The concurrent operation has reconnected our
117 * dentry, so we no longer need to.
118 */
119static struct dentry *reconnect_one(struct vfsmount *mnt,
120 struct dentry *dentry, char *nbuf)
121{
122 struct dentry *parent;
123 struct dentry *tmp;
124 int err;
125
126 parent = ERR_PTR(-EACCES);
127 mutex_lock(&dentry->d_inode->i_mutex);
128 if (mnt->mnt_sb->s_export_op->get_parent)
129 parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
130 mutex_unlock(&dentry->d_inode->i_mutex);
131
132 if (IS_ERR(parent)) {
133 dprintk("%s: get_parent of %ld failed, err %d\n",
134 __func__, dentry->d_inode->i_ino, PTR_ERR(parent));
135 return parent;
136 }
137
138 dprintk("%s: find name of %lu in %lu\n", __func__,
139 dentry->d_inode->i_ino, parent->d_inode->i_ino);
140 err = exportfs_get_name(mnt, parent, nbuf, dentry);
141 if (err == -ENOENT)
142 goto out_reconnected;
143 if (err)
144 goto out_err;
145 dprintk("%s: found name: %s\n", __func__, nbuf);
146 mutex_lock(&parent->d_inode->i_mutex);
147 tmp = lookup_one_len(nbuf, parent, strlen(nbuf));
148 mutex_unlock(&parent->d_inode->i_mutex);
149 if (IS_ERR(tmp)) {
150 dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
151 goto out_err;
152 }
153 if (tmp != dentry) {
154 dput(tmp);
155 goto out_reconnected;
156 }
157 dput(tmp);
158 if (IS_ROOT(dentry)) {
159 err = -ESTALE;
160 goto out_err;
161 }
162 return parent;
163
164out_err:
165 dput(parent);
166 return ERR_PTR(err);
167out_reconnected:
168 dput(parent);
169 /*
170 * Someone must have renamed our entry into another parent, in
171 * which case it has been reconnected by the rename.
172 *
173 * Or someone removed it entirely, in which case filehandle
174 * lookup will succeed but the directory is now IS_DEAD and
175 * subsequent operations on it will fail.
176 *
177 * Alternatively, maybe there was no race at all, and the
178 * filesystem is just corrupt and gave us a parent that doesn't
179 * actually contain any entry pointing to this inode. So,
180 * double check that this worked and return -ESTALE if not:
181 */
182 if (!dentry_connected(dentry))
183 return ERR_PTR(-ESTALE);
184 return NULL;
91} 185}
92 186
93/* 187/*
94 * Make sure target_dir is fully connected to the dentry tree. 188 * Make sure target_dir is fully connected to the dentry tree.
95 * 189 *
96 * It may already be, as the flag isn't always updated when connection happens. 190 * On successful return, DCACHE_DISCONNECTED will be cleared on
191 * target_dir, and target_dir->d_parent->...->d_parent will reach the
192 * root of the filesystem.
193 *
194 * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
195 * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
196 * set but already be connected. In that case we'll verify the
197 * connection to root and then clear the flag.
198 *
199 * Note that target_dir could be removed by a concurrent operation. In
200 * that case reconnect_path may still succeed with target_dir fully
201 * connected, but further operations using the filehandle will fail when
202 * necessary (due to S_DEAD being set on the directory).
97 */ 203 */
98static int 204static int
99reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf) 205reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
100{ 206{
101 int noprogress = 0; 207 struct dentry *dentry, *parent;
102 int err = -ESTALE;
103 208
104 /* 209 dentry = dget(target_dir);
105 * It is possible that a confused file system might not let us complete
106 * the path to the root. For example, if get_parent returns a directory
107 * in which we cannot find a name for the child. While this implies a
108 * very sick filesystem we don't want it to cause knfsd to spin. Hence
109 * the noprogress counter. If we go through the loop 10 times (2 is
110 * probably enough) without getting anywhere, we just give up
111 */
112 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
113 struct dentry *pd = find_disconnected_root(target_dir);
114
115 if (!IS_ROOT(pd)) {
116 /* must have found a connected parent - great */
117 spin_lock(&pd->d_lock);
118 pd->d_flags &= ~DCACHE_DISCONNECTED;
119 spin_unlock(&pd->d_lock);
120 noprogress = 0;
121 } else if (pd == mnt->mnt_sb->s_root) {
122 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
123 spin_lock(&pd->d_lock);
124 pd->d_flags &= ~DCACHE_DISCONNECTED;
125 spin_unlock(&pd->d_lock);
126 noprogress = 0;
127 } else {
128 /*
129 * We have hit the top of a disconnected path, try to
130 * find parent and connect.
131 *
132 * Racing with some other process renaming a directory
133 * isn't much of a problem here. If someone renames
134 * the directory, it will end up properly connected,
135 * which is what we want
136 *
137 * Getting the parent can't be supported generically,
138 * the locking is too icky.
139 *
140 * Instead we just return EACCES. If server reboots
141 * or inodes get flushed, you lose
142 */
143 struct dentry *ppd = ERR_PTR(-EACCES);
144 struct dentry *npd;
145
146 mutex_lock(&pd->d_inode->i_mutex);
147 if (mnt->mnt_sb->s_export_op->get_parent)
148 ppd = mnt->mnt_sb->s_export_op->get_parent(pd);
149 mutex_unlock(&pd->d_inode->i_mutex);
150
151 if (IS_ERR(ppd)) {
152 err = PTR_ERR(ppd);
153 dprintk("%s: get_parent of %ld failed, err %d\n",
154 __func__, pd->d_inode->i_ino, err);
155 dput(pd);
156 break;
157 }
158 210
159 dprintk("%s: find name of %lu in %lu\n", __func__, 211 while (dentry->d_flags & DCACHE_DISCONNECTED) {
160 pd->d_inode->i_ino, ppd->d_inode->i_ino); 212 BUG_ON(dentry == mnt->mnt_sb->s_root);
161 err = exportfs_get_name(mnt, ppd, nbuf, pd);
162 if (err) {
163 dput(ppd);
164 dput(pd);
165 if (err == -ENOENT)
166 /* some race between get_parent and
167 * get_name? just try again
168 */
169 continue;
170 break;
171 }
172 dprintk("%s: found name: %s\n", __func__, nbuf);
173 mutex_lock(&ppd->d_inode->i_mutex);
174 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
175 mutex_unlock(&ppd->d_inode->i_mutex);
176 if (IS_ERR(npd)) {
177 err = PTR_ERR(npd);
178 dprintk("%s: lookup failed: %d\n",
179 __func__, err);
180 dput(ppd);
181 dput(pd);
182 break;
183 }
184 /* we didn't really want npd, we really wanted
185 * a side-effect of the lookup.
186 * hopefully, npd == pd, though it isn't really
187 * a problem if it isn't
188 */
189 if (npd == pd)
190 noprogress = 0;
191 else
192 printk("%s: npd != pd\n", __func__);
193 dput(npd);
194 dput(ppd);
195 if (IS_ROOT(pd)) {
196 /* something went wrong, we have to give up */
197 dput(pd);
198 break;
199 }
200 }
201 dput(pd);
202 }
203 213
204 if (target_dir->d_flags & DCACHE_DISCONNECTED) { 214 if (IS_ROOT(dentry))
205 /* something went wrong - oh-well */ 215 parent = reconnect_one(mnt, dentry, nbuf);
206 if (!err) 216 else
207 err = -ESTALE; 217 parent = dget_parent(dentry);
208 return err;
209 }
210 218
219 if (!parent)
220 break;
221 dput(dentry);
222 if (IS_ERR(parent))
223 return PTR_ERR(parent);
224 dentry = parent;
225 }
226 dput(dentry);
227 clear_disconnected(target_dir);
211 return 0; 228 return 0;
212} 229}
213 230
@@ -215,7 +232,7 @@ struct getdents_callback {
215 struct dir_context ctx; 232 struct dir_context ctx;
216 char *name; /* name that was found. It already points to a 233 char *name; /* name that was found. It already points to a
217 buffer NAME_MAX+1 is size */ 234 buffer NAME_MAX+1 is size */
218 unsigned long ino; /* the inum we are looking for */ 235 u64 ino; /* the inum we are looking for */
219 int found; /* inode matched? */ 236 int found; /* inode matched? */
220 int sequence; /* sequence counter */ 237 int sequence; /* sequence counter */
221}; 238};
@@ -255,10 +272,14 @@ static int get_name(const struct path *path, char *name, struct dentry *child)
255 struct inode *dir = path->dentry->d_inode; 272 struct inode *dir = path->dentry->d_inode;
256 int error; 273 int error;
257 struct file *file; 274 struct file *file;
275 struct kstat stat;
276 struct path child_path = {
277 .mnt = path->mnt,
278 .dentry = child,
279 };
258 struct getdents_callback buffer = { 280 struct getdents_callback buffer = {
259 .ctx.actor = filldir_one, 281 .ctx.actor = filldir_one,
260 .name = name, 282 .name = name,
261 .ino = child->d_inode->i_ino
262 }; 283 };
263 284
264 error = -ENOTDIR; 285 error = -ENOTDIR;
@@ -268,6 +289,16 @@ static int get_name(const struct path *path, char *name, struct dentry *child)
268 if (!dir->i_fop) 289 if (!dir->i_fop)
269 goto out; 290 goto out;
270 /* 291 /*
292 * inode->i_ino is unsigned long, kstat->ino is u64, so the
293 * former would be insufficient on 32-bit hosts when the
294 * filesystem supports 64-bit inode numbers. So we need to
295 * actually call ->getattr, not just read i_ino:
296 */
297 error = vfs_getattr_nosec(&child_path, &stat);
298 if (error)
299 return error;
300 buffer.ino = stat.ino;
301 /*
271 * Open the directory ... 302 * Open the directory ...
272 */ 303 */
273 file = dentry_open(path, O_RDONLY, cred); 304 file = dentry_open(path, O_RDONLY, cred);