2
* Copyright (C) 2007-2008 Junjiro Okajima
4
* This program, aufs is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
* $Id: export.c,v 1.4 2008/05/04 23:52:08 sfjro Exp $
25
#include <linux/exportfs.h>
29
#if BITS_PER_LONG == 32
37
static ino_t decode_ino(__u32 *a)
42
#if BITS_PER_LONG == 64
48
static void encode_ino(__u32 *a, ino_t ino)
54
#if BITS_PER_LONG == 64
63
#if BITS_PER_LONG == 64
64
/* support 64bit inode number */
81
Fh_dir_ino = Fh_dir_ino1,
85
static int au_test_anon(struct dentry *dentry)
87
return !!(dentry->d_flags & DCACHE_DISCONNECTED);
90
/* ---------------------------------------------------------------------- */
92
static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
95
struct dentry *dentry, *parent;
98
LKTRTrace("i%lu, diri%lu\n", ino, dir_ino);
101
inode = ilookup(sb, ino);
102
if (unlikely(!inode))
105
dentry = ERR_PTR(-ESTALE);
106
if (unlikely(is_bad_inode(inode)))
110
if (!S_ISDIR(inode->i_mode)) {
112
spin_lock(&dcache_lock);
113
list_for_each_entry(d, &inode->i_dentry, d_alias)
115
&& d->d_parent->d_inode->i_ino == dir_ino) {
116
dentry = dget_locked(d);
119
spin_unlock(&dcache_lock);
121
dentry = d_find_alias(inode);
122
if (dentry && !au_test_anon(dentry)) {
124
parent = dget_parent(dentry);
125
same_ino = (parent->d_inode->i_ino == dir_ino);
128
goto out_iput; /* success */
138
AuTraceErrPtr(dentry);
142
/* ---------------------------------------------------------------------- */
144
struct find_name_by_ino {
152
find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
153
u64 ino, unsigned int d_type)
155
struct find_name_by_ino *a = arg;
161
memcpy(a->name, name, namelen);
162
a->namelen = namelen;
167
static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
170
struct dentry *dentry, *parent;
172
struct find_name_by_ino arg;
176
LKTRTrace("i%lu, diri%lu\n", ino, dir_ino);
179
dir = ilookup(sb, dir_ino);
183
dentry = ERR_PTR(-ESTALE);
184
if (unlikely(is_bad_inode(dir)))
188
parent = d_find_alias(dir);
190
if (unlikely(au_test_anon(parent))) {
197
file = dentry_open(parent, au_mntcache_get(sb), au_dir_roflags);
198
dentry = (void *)file;
202
dentry = ERR_PTR(-ENOMEM);
203
arg.name = __getname();
204
if (unlikely(!arg.name))
212
err = vfsub_readdir(file, find_name_by_ino, &arg, /*dlgt*/0);
213
} while (!err && !arg.found && arg.called);
214
dentry = ERR_PTR(err);
216
/* do not call au_lkup_one(), nor dlgt */
217
mutex_lock(&dir->i_mutex);
218
dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
219
mutex_unlock(&dir->i_mutex);
220
AuTraceErrPtr(dentry);
230
AuTraceErrPtr(dentry);
234
/* ---------------------------------------------------------------------- */
237
int found, called, len;
242
static int append_name(void *arg, const char *name, int len, loff_t pos,
243
u64 ino, unsigned int d_type)
245
struct append_name *a = arg;
252
AuDebugOn(len == 1 && *name == '.');
253
AuDebugOn(len == 2 && name[0] == '.' && name[1] == '.');
254
a->len = strlen(a->h_path);
255
memmove(a->h_path - len - 1, a->h_path, a->len);
256
a->h_path -= len + 1;
257
p = a->h_path + a->len;
259
memcpy(p, name, len);
265
static int h_acceptable(void *expv, struct dentry *dentry)
270
static char *au_build_path(struct super_block *sb, __u32 *fh, char *path,
271
struct vfsmount *h_mnt, struct dentry *h_root,
272
struct dentry *h_parent)
277
struct append_name arg;
278
struct path dm_path = {
285
arg.h_path = d_path(&dm_path, path, PATH_MAX);
287
if (unlikely(!arg.h_path || IS_ERR(arg.h_path)))
290
len = strlen(arg.h_path);
291
dm_path.dentry = h_parent;
292
arg.h_path = d_path(&dm_path, path, PATH_MAX);
294
if (unlikely(!arg.h_path || IS_ERR(arg.h_path)))
296
LKTRTrace("%s\n", arg.h_path);
299
LKTRTrace("%p, %s, %ld\n",
300
arg.h_path, arg.h_path, (long)(arg.h_path - path));
302
/* cf. fs/exportfs/expfs.c */
303
h_file = dentry_open(dget(h_parent), mntget(h_mnt), au_dir_roflags);
304
ret = (void *)h_file;
309
arg.h_ino = decode_ino(fh + Fh_h_ino);
312
err = vfsub_readdir(h_file, append_name, &arg, /*dlgt*/0);
313
} while (!err && !arg.found && arg.called);
314
LKTRTrace("%p, %s, %d\n", arg.h_path, arg.h_path, arg.len);
320
dm_path.mnt = au_mntcache_get(sb);
321
dm_path.dentry = sb->s_root;
322
ret = d_path(&dm_path, path, PATH_MAX - arg.len);
324
if (unlikely(!ret || IS_ERR(ret)))
326
ret[strlen(ret)] = '/';
327
LKTRTrace("%s\n", ret);
334
static struct dentry *
335
decode_by_path(struct super_block *sb, aufs_bindex_t bindex, __u32 *fh,
336
int fh_len, void *context)
338
struct dentry *dentry, *h_parent, *root, *h_root;
339
struct super_block *h_sb;
341
struct vfsmount *h_mnt;
344
struct au_branch *br;
346
LKTRTrace("b%d\n", bindex);
349
br = au_sbr(sb, bindex);
352
h_sb = h_mnt->mnt_sb;
353
LKTRTrace("%s, h_decode_fh\n", au_sbtype(h_sb));
354
/* in linux-2.6.24, it takes struct fid * as file handle */
355
//todo: call lower fh_to_dentry()? fh_to_parent()?
356
h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
357
fh_len - Fh_tail, fh[Fh_h_type],
358
h_acceptable, /*context*/NULL);
360
if (unlikely(!h_parent || IS_ERR(h_parent))) {
361
AuWarn1("%s decode_fh failed\n", au_sbtype(h_sb));
365
if (unlikely(au_test_anon(h_parent))) {
366
AuWarn1("%s decode_fh returned a disconnected dentry\n",
372
dentry = ERR_PTR(-ENOMEM);
374
if (unlikely(!path)) {
380
di_read_lock_parent(root, !AuLock_IR);
381
h_root = au_h_dptr(root, bindex);
382
di_read_unlock(root, !AuLock_IR);
383
p = au_build_path(sb, fh, path, h_mnt, h_root, h_parent);
388
err = vfsub_path_lookup(p, LOOKUP_FOLLOW, &nd);
389
dentry = ERR_PTR(err);
391
dentry = dget(nd.path.dentry);
392
if (unlikely(au_test_anon(dentry))) {
394
dentry = ERR_PTR(-ESTALE);
403
AuTraceErrPtr(dentry);
407
/* ---------------------------------------------------------------------- */
409
static noinline_for_stack struct dentry *
410
aufs_decode_fh(struct super_block *sb, __u32 *fh, int fh_len, int fh_type,
411
int (*acceptable)(void *context, struct dentry *de),
414
struct dentry *dentry;
416
aufs_bindex_t bindex, br_id;
417
struct inode *inode, *h_inode;
421
LKTRTrace("%d, fh{i%u, br_id %u, sigen %u, hi%u}\n",
422
fh_type, fh[Fh_ino], fh[Fh_br_id], fh[Fh_sigen],
424
AuDebugOn(fh_len < Fh_tail);
426
si_read_lock(sb, AuLock_FLUSH);
429
/* branch id may be wrapped around */
430
dentry = ERR_PTR(-ESTALE);
431
br_id = fh[Fh_br_id];
432
sigen = fh[Fh_sigen];
433
bindex = au_br_index(sb, br_id);
434
if (unlikely(bindex < 0 || sigen + AUFS_BRANCH_MAX <= au_sigen(sb)))
437
/* is this inode still cached? */
438
ino = decode_ino(fh + Fh_ino);
439
dir_ino = decode_ino(fh + Fh_dir_ino);
440
dentry = decode_by_ino(sb, ino, dir_ino);
446
/* is the parent dir cached? */
447
dentry = decode_by_dir_ino(sb, ino, dir_ino);
454
dentry = decode_by_path(sb, bindex, fh, fh_len, context);
457
if (unlikely(!dentry))
459
if (unlikely(dentry->d_inode->i_ino != ino))
463
inode = dentry->d_inode;
465
ii_read_lock_child(inode);
466
if (au_ibstart(inode) <= bindex && bindex <= au_ibend(inode))
467
h_inode = au_h_iptr(inode, bindex);
468
ii_read_unlock(inode);
470
&& h_inode->i_generation == fh[Fh_h_igen]
471
&& acceptable(context, dentry))
472
goto out; /* success */
476
dentry = ERR_PTR(-ESTALE);
480
AuTraceErrPtr(dentry);
485
static struct dentry *
486
aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
489
return aufs_decode_fh(sb, fid->raw, fh_len, fh_type, h_acceptable,
494
/* support subtreecheck option */
495
static struct dentry *au_fh_to_parent(struct super_block *sb, struct fid *fid,
496
int fh_len, int fh_type)
501
/* ---------------------------------------------------------------------- */
503
static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
507
struct super_block *sb, *h_sb;
508
struct inode *inode, *h_inode, *dir;
509
aufs_bindex_t bindex;
511
struct dentry *parent, *h_parent;
514
BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
515
LKTRTrace("%.*s, max %d, conn %d\n",
516
AuDLNPair(dentry), *max_len, connectable);
517
AuDebugOn(au_test_anon(dentry));
518
inode = dentry->d_inode;
520
parent = dget_parent(dentry);
521
AuDebugOn(au_test_anon(parent));
524
if (unlikely(*max_len <= Fh_tail)) {
525
AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
530
si_read_lock(sb, AuLock_FLUSH);
531
di_read_lock_child(dentry, AuLock_IR);
532
di_read_lock_parent(parent, AuLock_IR);
533
#ifdef CONFIG_AUFS_DEBUG
534
if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
535
AuWarn1("NFS-exporting requires xino\n");
537
if (unlikely(au_opt_test(au_mntflags(sb), UDBA_INOTIFY)))
538
AuWarn1("udba=inotify is not recommended when exporting\n");
543
bindex = au_dbstart(dentry);
544
h_sb = au_sbr_sb(sb, bindex);
545
if (unlikely(!h_sb->s_export_op)) {
546
AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
550
#if 0 //def CONFIG_AUFS_ROBR
551
if (unlikely(au_test_aufs(h_sb))) {
552
AuErr1("aufs branch is not supported\n");
557
fh[Fh_br_id] = au_sbr_id(sb, bindex);
558
fh[Fh_sigen] = au_sigen(sb);
559
encode_ino(fh + Fh_ino, inode->i_ino);
560
dir = parent->d_inode;
561
encode_ino(fh + Fh_dir_ino, dir->i_ino);
562
h_inode = au_h_dptr(dentry, bindex)->d_inode;
563
encode_ino(fh + Fh_h_ino, h_inode->i_ino);
564
fh[Fh_h_igen] = h_inode->i_generation;
567
//LKTRTrace("Fh_tail %d, max_len %d\n", Fh_tail, *max_len);
568
h_parent = au_h_dptr(parent, bindex);
569
AuDebugOn(au_test_anon(h_parent));
570
/* in linux-2.6.24, it takes struct fid * as file handle */
571
fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
572
max_len, connectable);
578
AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
581
di_read_unlock(parent, AuLock_IR);
582
aufs_read_unlock(dentry, AuLock_IR);
587
if (unlikely(err < 0))
592
/* ---------------------------------------------------------------------- */
594
struct export_operations aufs_export_op = {
595
.fh_to_dentry = aufs_fh_to_dentry,
596
.encode_fh = aufs_encode_fh