GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / exportfs / expfs.c
1 /*
2  * Copyright (C) Neil Brown 2002
3  * Copyright (C) Christoph Hellwig 2007
4  *
5  * This file contains the code mapping from inodes to NFS file handles,
6  * and for mapping back from file handles to dentries.
7  *
8  * For details on why we do all the strange and hairy things in here
9  * take a look at Documentation/filesystems/nfs/Exporting.
10  */
11 #include <linux/exportfs.h>
12 #include <linux/fs.h>
13 #include <linux/file.h>
14 #include <linux/module.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/sched.h>
18 #include <linux/cred.h>
19
20 #define dprintk(fmt, args...) do{}while(0)
21
22
23 static int get_name(const struct path *path, char *name, struct dentry *child);
24
25
26 static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
27                 char *name, struct dentry *child)
28 {
29         const struct export_operations *nop = dir->d_sb->s_export_op;
30         struct path path = {.mnt = mnt, .dentry = dir};
31
32         if (nop->get_name)
33                 return nop->get_name(dir, name, child);
34         else
35                 return get_name(&path, name, child);
36 }
37
38 /*
39  * Check if the dentry or any of it's aliases is acceptable.
40  */
41 static struct dentry *
42 find_acceptable_alias(struct dentry *result,
43                 int (*acceptable)(void *context, struct dentry *dentry),
44                 void *context)
45 {
46         struct dentry *dentry, *toput = NULL;
47         struct inode *inode;
48
49         if (acceptable(context, result))
50                 return result;
51
52         inode = result->d_inode;
53         spin_lock(&inode->i_lock);
54         hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
55                 dget(dentry);
56                 spin_unlock(&inode->i_lock);
57                 if (toput)
58                         dput(toput);
59                 if (dentry != result && acceptable(context, dentry)) {
60                         dput(result);
61                         return dentry;
62                 }
63                 spin_lock(&inode->i_lock);
64                 toput = dentry;
65         }
66         spin_unlock(&inode->i_lock);
67
68         if (toput)
69                 dput(toput);
70         return NULL;
71 }
72
73 static bool dentry_connected(struct dentry *dentry)
74 {
75         dget(dentry);
76         while (dentry->d_flags & DCACHE_DISCONNECTED) {
77                 struct dentry *parent = dget_parent(dentry);
78
79                 dput(dentry);
80                 if (dentry == parent) {
81                         dput(parent);
82                         return false;
83                 }
84                 dentry = parent;
85         }
86         dput(dentry);
87         return true;
88 }
89
90 static void clear_disconnected(struct dentry *dentry)
91 {
92         dget(dentry);
93         while (dentry->d_flags & DCACHE_DISCONNECTED) {
94                 struct dentry *parent = dget_parent(dentry);
95
96                 WARN_ON_ONCE(IS_ROOT(dentry));
97
98                 spin_lock(&dentry->d_lock);
99                 dentry->d_flags &= ~DCACHE_DISCONNECTED;
100                 spin_unlock(&dentry->d_lock);
101
102                 dput(dentry);
103                 dentry = parent;
104         }
105         dput(dentry);
106 }
107
108 /*
109  * Reconnect a directory dentry with its parent.
110  *
111  * This can return a dentry, or NULL, or an error.
112  *
113  * In the first case the returned dentry is the parent of the given
114  * dentry, and may itself need to be reconnected to its parent.
115  *
116  * In the NULL case, a concurrent VFS operation has either renamed or
117  * removed this directory.  The concurrent operation has reconnected our
118  * dentry, so we no longer need to.
119  */
120 static struct dentry *reconnect_one(struct vfsmount *mnt,
121                 struct dentry *dentry, char *nbuf)
122 {
123         struct dentry *parent;
124         struct dentry *tmp;
125         int err;
126
127         parent = ERR_PTR(-EACCES);
128         inode_lock(dentry->d_inode);
129         if (mnt->mnt_sb->s_export_op->get_parent)
130                 parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
131         inode_unlock(dentry->d_inode);
132
133         if (IS_ERR(parent)) {
134                 dprintk("%s: get_parent of %ld failed, err %d\n",
135                         __func__, dentry->d_inode->i_ino, PTR_ERR(parent));
136                 return parent;
137         }
138
139         dprintk("%s: find name of %lu in %lu\n", __func__,
140                 dentry->d_inode->i_ino, parent->d_inode->i_ino);
141         err = exportfs_get_name(mnt, parent, nbuf, dentry);
142         if (err == -ENOENT)
143                 goto out_reconnected;
144         if (err)
145                 goto out_err;
146         dprintk("%s: found name: %s\n", __func__, nbuf);
147         tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
148         if (IS_ERR(tmp)) {
149                 dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
150                 err = PTR_ERR(tmp);
151                 goto out_err;
152         }
153         if (tmp != dentry) {
154                 /*
155                  * Somebody has renamed it since exportfs_get_name();
156                  * great, since it could've only been renamed if it
157                  * got looked up and thus connected, and it would
158                  * remain connected afterwards.  We are done.
159                  */
160                 dput(tmp);
161                 goto out_reconnected;
162         }
163         dput(tmp);
164         if (IS_ROOT(dentry)) {
165                 err = -ESTALE;
166                 goto out_err;
167         }
168         return parent;
169
170 out_err:
171         dput(parent);
172         return ERR_PTR(err);
173 out_reconnected:
174         dput(parent);
175         /*
176          * Someone must have renamed our entry into another parent, in
177          * which case it has been reconnected by the rename.
178          *
179          * Or someone removed it entirely, in which case filehandle
180          * lookup will succeed but the directory is now IS_DEAD and
181          * subsequent operations on it will fail.
182          *
183          * Alternatively, maybe there was no race at all, and the
184          * filesystem is just corrupt and gave us a parent that doesn't
185          * actually contain any entry pointing to this inode.  So,
186          * double check that this worked and return -ESTALE if not:
187          */
188         if (!dentry_connected(dentry))
189                 return ERR_PTR(-ESTALE);
190         return NULL;
191 }
192
193 /*
194  * Make sure target_dir is fully connected to the dentry tree.
195  *
196  * On successful return, DCACHE_DISCONNECTED will be cleared on
197  * target_dir, and target_dir->d_parent->...->d_parent will reach the
198  * root of the filesystem.
199  *
200  * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
201  * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
202  * set but already be connected.  In that case we'll verify the
203  * connection to root and then clear the flag.
204  *
205  * Note that target_dir could be removed by a concurrent operation.  In
206  * that case reconnect_path may still succeed with target_dir fully
207  * connected, but further operations using the filehandle will fail when
208  * necessary (due to S_DEAD being set on the directory).
209  */
210 static int
211 reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
212 {
213         struct dentry *dentry, *parent;
214
215         dentry = dget(target_dir);
216
217         while (dentry->d_flags & DCACHE_DISCONNECTED) {
218                 BUG_ON(dentry == mnt->mnt_sb->s_root);
219
220                 if (IS_ROOT(dentry))
221                         parent = reconnect_one(mnt, dentry, nbuf);
222                 else
223                         parent = dget_parent(dentry);
224
225                 if (!parent)
226                         break;
227                 dput(dentry);
228                 if (IS_ERR(parent))
229                         return PTR_ERR(parent);
230                 dentry = parent;
231         }
232         dput(dentry);
233         clear_disconnected(target_dir);
234         return 0;
235 }
236
237 struct getdents_callback {
238         struct dir_context ctx;
239         char *name;             /* name that was found. It already points to a
240                                    buffer NAME_MAX+1 is size */
241         u64 ino;                /* the inum we are looking for */
242         int found;              /* inode matched? */
243         int sequence;           /* sequence counter */
244 };
245
246 /*
247  * A rather strange filldir function to capture
248  * the name matching the specified inode number.
249  */
250 static int filldir_one(struct dir_context *ctx, const char *name, int len,
251                         loff_t pos, u64 ino, unsigned int d_type)
252 {
253         struct getdents_callback *buf =
254                 container_of(ctx, struct getdents_callback, ctx);
255         int result = 0;
256
257         buf->sequence++;
258         if (buf->ino == ino && len <= NAME_MAX) {
259                 memcpy(buf->name, name, len);
260                 buf->name[len] = '\0';
261                 buf->found = 1;
262                 result = -1;
263         }
264         return result;
265 }
266
267 /**
268  * get_name - default export_operations->get_name function
269  * @path:   the directory in which to find a name
270  * @name:   a pointer to a %NAME_MAX+1 char buffer to store the name
271  * @child:  the dentry for the child directory.
272  *
273  * calls readdir on the parent until it finds an entry with
274  * the same inode number as the child, and returns that.
275  */
276 static int get_name(const struct path *path, char *name, struct dentry *child)
277 {
278         const struct cred *cred = current_cred();
279         struct inode *dir = path->dentry->d_inode;
280         int error;
281         struct file *file;
282         struct kstat stat;
283         struct path child_path = {
284                 .mnt = path->mnt,
285                 .dentry = child,
286         };
287         struct getdents_callback buffer = {
288                 .ctx.actor = filldir_one,
289                 .name = name,
290         };
291
292         error = -ENOTDIR;
293         if (!dir || !S_ISDIR(dir->i_mode))
294                 goto out;
295         error = -EINVAL;
296         if (!dir->i_fop)
297                 goto out;
298         /*
299          * inode->i_ino is unsigned long, kstat->ino is u64, so the
300          * former would be insufficient on 32-bit hosts when the
301          * filesystem supports 64-bit inode numbers.  So we need to
302          * actually call ->getattr, not just read i_ino:
303          */
304         error = vfs_getattr_nosec(&child_path, &stat,
305                                   STATX_INO, AT_STATX_SYNC_AS_STAT);
306         if (error)
307                 return error;
308         buffer.ino = stat.ino;
309         /*
310          * Open the directory ...
311          */
312         file = dentry_open(path, O_RDONLY, cred);
313         error = PTR_ERR(file);
314         if (IS_ERR(file))
315                 goto out;
316
317         error = -EINVAL;
318         if (!file->f_op->iterate && !file->f_op->iterate_shared)
319                 goto out_close;
320
321         buffer.sequence = 0;
322         while (1) {
323                 int old_seq = buffer.sequence;
324
325                 error = iterate_dir(file, &buffer.ctx);
326                 if (buffer.found) {
327                         error = 0;
328                         break;
329                 }
330
331                 if (error < 0)
332                         break;
333
334                 error = -ENOENT;
335                 if (old_seq == buffer.sequence)
336                         break;
337         }
338
339 out_close:
340         fput(file);
341 out:
342         return error;
343 }
344
345 /**
346  * export_encode_fh - default export_operations->encode_fh function
347  * @inode:   the object to encode
348  * @fid:     where to store the file handle fragment
349  * @max_len: maximum length to store there
350  * @parent:  parent directory inode, if wanted
351  *
352  * This default encode_fh function assumes that the 32 inode number
353  * is suitable for locating an inode, and that the generation number
354  * can be used to check that it is still valid.  It places them in the
355  * filehandle fragment where export_decode_fh expects to find them.
356  */
357 static int export_encode_fh(struct inode *inode, struct fid *fid,
358                 int *max_len, struct inode *parent)
359 {
360         int len = *max_len;
361         int type = FILEID_INO32_GEN;
362
363         if (parent && (len < 4)) {
364                 *max_len = 4;
365                 return FILEID_INVALID;
366         } else if (len < 2) {
367                 *max_len = 2;
368                 return FILEID_INVALID;
369         }
370
371         len = 2;
372         fid->i32.ino = inode->i_ino;
373         fid->i32.gen = inode->i_generation;
374         if (parent) {
375                 fid->i32.parent_ino = parent->i_ino;
376                 fid->i32.parent_gen = parent->i_generation;
377                 len = 4;
378                 type = FILEID_INO32_GEN_PARENT;
379         }
380         *max_len = len;
381         return type;
382 }
383
384 int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
385                              int *max_len, struct inode *parent)
386 {
387         const struct export_operations *nop = inode->i_sb->s_export_op;
388
389         if (nop && nop->encode_fh)
390                 return nop->encode_fh(inode, fid->raw, max_len, parent);
391
392         return export_encode_fh(inode, fid, max_len, parent);
393 }
394 EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
395
396 int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
397                 int connectable)
398 {
399         int error;
400         struct dentry *p = NULL;
401         struct inode *inode = dentry->d_inode, *parent = NULL;
402
403         if (connectable && !S_ISDIR(inode->i_mode)) {
404                 p = dget_parent(dentry);
405                 /*
406                  * note that while p might've ceased to be our parent already,
407                  * it's still pinned by and still positive.
408                  */
409                 parent = p->d_inode;
410         }
411
412         error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
413         dput(p);
414
415         return error;
416 }
417 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
418
419 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
420                 int fh_len, int fileid_type,
421                 int (*acceptable)(void *, struct dentry *), void *context)
422 {
423         const struct export_operations *nop = mnt->mnt_sb->s_export_op;
424         struct dentry *result, *alias;
425         char nbuf[NAME_MAX+1];
426         int err;
427
428         /*
429          * Try to get any dentry for the given file handle from the filesystem.
430          */
431         if (!nop || !nop->fh_to_dentry)
432                 return ERR_PTR(-ESTALE);
433         result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
434         if (PTR_ERR(result) == -ENOMEM)
435                 return ERR_CAST(result);
436         if (IS_ERR_OR_NULL(result))
437                 return ERR_PTR(-ESTALE);
438
439         if (d_is_dir(result)) {
440                 /*
441                  * This request is for a directory.
442                  *
443                  * On the positive side there is only one dentry for each
444                  * directory inode.  On the negative side this implies that we
445                  * to ensure our dentry is connected all the way up to the
446                  * filesystem root.
447                  */
448                 if (result->d_flags & DCACHE_DISCONNECTED) {
449                         err = reconnect_path(mnt, result, nbuf);
450                         if (err)
451                                 goto err_result;
452                 }
453
454                 if (!acceptable(context, result)) {
455                         err = -EACCES;
456                         goto err_result;
457                 }
458
459                 return result;
460         } else {
461                 /*
462                  * It's not a directory.  Life is a little more complicated.
463                  */
464                 struct dentry *target_dir, *nresult;
465
466                 /*
467                  * See if either the dentry we just got from the filesystem
468                  * or any alias for it is acceptable.  This is always true
469                  * if this filesystem is exported without the subtreecheck
470                  * option.  If the filesystem is exported with the subtree
471                  * check option there's a fair chance we need to look at
472                  * the parent directory in the file handle and make sure
473                  * it's connected to the filesystem root.
474                  */
475                 alias = find_acceptable_alias(result, acceptable, context);
476                 if (alias)
477                         return alias;
478
479                 /*
480                  * Try to extract a dentry for the parent directory from the
481                  * file handle.  If this fails we'll have to give up.
482                  */
483                 err = -ESTALE;
484                 if (!nop->fh_to_parent)
485                         goto err_result;
486
487                 target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
488                                 fh_len, fileid_type);
489                 if (!target_dir)
490                         goto err_result;
491                 err = PTR_ERR(target_dir);
492                 if (IS_ERR(target_dir))
493                         goto err_result;
494
495                 /*
496                  * And as usual we need to make sure the parent directory is
497                  * connected to the filesystem root.  The VFS really doesn't
498                  * like disconnected directories..
499                  */
500                 err = reconnect_path(mnt, target_dir, nbuf);
501                 if (err) {
502                         dput(target_dir);
503                         goto err_result;
504                 }
505
506                 /*
507                  * Now that we've got both a well-connected parent and a
508                  * dentry for the inode we're after, make sure that our
509                  * inode is actually connected to the parent.
510                  */
511                 err = exportfs_get_name(mnt, target_dir, nbuf, result);
512                 if (err) {
513                         dput(target_dir);
514                         goto err_result;
515                 }
516
517                 inode_lock(target_dir->d_inode);
518                 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
519                 if (!IS_ERR(nresult)) {
520                         if (unlikely(nresult->d_inode != result->d_inode)) {
521                                 dput(nresult);
522                                 nresult = ERR_PTR(-ESTALE);
523                         }
524                 }
525                 inode_unlock(target_dir->d_inode);
526                 /*
527                  * At this point we are done with the parent, but it's pinned
528                  * by the child dentry anyway.
529                  */
530                 dput(target_dir);
531
532                 if (IS_ERR(nresult)) {
533                         err = PTR_ERR(nresult);
534                         goto err_result;
535                 }
536                 dput(result);
537                 result = nresult;
538
539                 /*
540                  * And finally make sure the dentry is actually acceptable
541                  * to NFSD.
542                  */
543                 alias = find_acceptable_alias(result, acceptable, context);
544                 if (!alias) {
545                         err = -EACCES;
546                         goto err_result;
547                 }
548
549                 return alias;
550         }
551
552  err_result:
553         dput(result);
554         if (err != -ENOMEM)
555                 err = -ESTALE;
556         return ERR_PTR(err);
557 }
558 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
559
560 MODULE_LICENSE("GPL");