-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmvfs_linux_sops.c
1331 lines (1222 loc) · 38.3 KB
/
mvfs_linux_sops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1999, 2012 IBM Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Author: IBM Corporation
* This module is part of the IBM (R) Rational (R) ClearCase (R)
* Multi-version file system (MVFS).
* For support, please visit http://www.ibm.com/software/support
*/
/*
* superblock operations (plus some utilities) for MVFS
*/
#include "vnode_linux.h"
#include "mvfs_linux_shadow.h"
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38)
/* for noop_backing_dev_info */
#include <linux/backing-dev.h>
#endif
void
vnlayer_put_super(SUPER_T *super_p);
void
vnlayer_write_super(SUPER_T *super_p);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && !defined(SLES10SP2)
int
vnlayer_linux_statfs(
SUPER_T *super_p,
LINUX_STATFS_T *stat_p
);
#else
int
vnlayer_linux_statfs(
DENT_T *dent_p,
LINUX_STATFS_T *stat_p
);
#endif
int
vnlayer_remount_fs(
SUPER_T *super_p,
int *flags,
char *data
);
void
mvfs_clear_inode(
INODE_T *inode_p
);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
void
mvfs_evict_inode(struct inode *inode_p);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
void
mvfs_linux_umount_begin(SUPER_T *super_p);
#else
void
mvfs_linux_umount_begin(
struct vfsmount *mnt,
int flags);
#endif
int
vnlayer_fill_super(
SUPER_T *super_p,
void *data_p,
int silent
);
int
vnlayer_dentry_to_fh(
struct dentry *dent,
__u32 *fh,
int *lenp,
int need_parent
);
struct dentry *
vnlayer_get_dentry(
SUPER_T *sb,
void *fhbits
);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
struct dentry *
vnlayer_decode_fh(
SUPER_T *sb,
__u32 *fh,
int len, /* counted in units of 4-bytes */
int fhtype,
int (*acceptable)(void *context, struct dentry *de),
void *context
);
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) */
struct dentry * vnlayer_fh_to_dentry(
SUPER_T *sb,
struct fid *fh,
int len, /* counted in units of 4-bytes */
int fhtype
);
struct dentry * vnlayer_fh_to_parent(
SUPER_T *sb,
struct fid *fh,
int len, /* counted in units of 4-bytes */
int fhtype
);
#endif /* else LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) */
struct dentry *
vnlayer_get_parent(struct dentry *child);
static struct export_operations vnlayer_export_ops = {
.encode_fh = &vnlayer_dentry_to_fh,
.get_parent = &vnlayer_get_parent,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
.decode_fh = &vnlayer_decode_fh,
.get_dentry = &vnlayer_get_dentry,
#else
.fh_to_dentry = vnlayer_fh_to_dentry,
.fh_to_parent = vnlayer_fh_to_parent,
#endif
};
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
SUPER_T *
vnlayer_get_sb(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *raw_data
);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
int
vnlayer_get_sb(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *raw_data,
struct vfsmount *mnt
);
#else
struct dentry *
vnlayer_mount(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *data
);
#endif
void
vnlayer_kill_sb(SUPER_T *sbp);
INODE_T *
vnlayer_alloc_inode(SUPER_T *sbp);
void
vnlayer_destroy_inode(INODE_T *inode);
STATIC struct dentry *
vnlayer_find_dentry(VNODE_T *vp);
SB_OPS_T mvfs_super_ops = {
/* no read_inode */
/* no write_inode */
/* No put_inode. The work is done now in clear_inode */
/* no delete_inode (even before 2.6.36) */
.put_super = &vnlayer_put_super,
.write_super = &vnlayer_write_super,
.statfs = &vnlayer_linux_statfs,
.remount_fs = &vnlayer_remount_fs,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
.clear_inode = &mvfs_clear_inode,
#else
.evict_inode = mvfs_evict_inode,
#endif
/* inodes no longer have space for fs data (e.g. our vnode). */
.alloc_inode = &vnlayer_alloc_inode,
.destroy_inode = &vnlayer_destroy_inode,
.umount_begin = &mvfs_linux_umount_begin,
};
/* Define the filesytem type for loading
* Note that the flag is set to require a device for loading. Our
* module initialization routine will register /dev/mvfs for us to
* use. This is done so that we can control our own minor numbers.
*/
struct file_system_type mvfs_file_system =
{
.name = "mvfs",
.fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_REVAL_DOT,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
.get_sb = &vnlayer_get_sb,
#else
.mount = vnlayer_mount,
#endif
.kill_sb = &vnlayer_kill_sb,
.owner = THIS_MODULE,
};
void
vnlayer_put_super(struct super_block *super_p)
{
int err;
CRED_T *cred;
ASSERT_KERNEL_LOCKED();
ASSERT_SB_LOCKED(super_p);
cred = MDKI_GET_UCRED();
err = VFS_UNMOUNT(SBTOVFS(super_p), cred);
MDKI_CRFREE(cred);
if (err != 0) {
VFS_LOG(SBTOVFS(super_p), VFS_LOG_ERR,
"Linux file system interface doesn't let vnode/vfs reject unmounts: error %d\n", err);
}
return /* mdki_errno_unix_to_linux(err)*/;
}
void
vnlayer_write_super(struct super_block *super_p)
{
CRED_T *cred;
int err;
ASSERT_SB_LOCKED(super_p);
cred = MDKI_GET_UCRED();
err = VFS_SYNC(SBTOVFS(super_p), SBTOVFS(super_p), 0, cred);
MDKI_CRFREE(cred);
/* They rewrote sync_supers so that it won't proceed through their loop
* until the dirty bit is cleared.
*/
super_p->s_dirt = 0;
if (err != 0)
VFS_LOG(SBTOVFS(super_p), VFS_LOG_ERR, "%s: error %d syncing\n",
__func__, err);
return /* mdki_errno_unix_to_linux(err) */;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && !defined(SLES10SP2)
int
vnlayer_linux_statfs(
SUPER_T *super_p,
LINUX_STATFS_T *stat_p
)
#else
int
vnlayer_linux_statfs(
DENT_T *dent_p,
LINUX_STATFS_T *stat_p
)
#endif
{
int error;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && !defined(SLES10SP2)
error = VFS_STATVFS(SBTOVFS(super_p), stat_p);
#else
error = VFS_STATVFS(SBTOVFS(dent_p->d_sb), stat_p);
#endif
return (-error);
}
int
vnlayer_remount_fs(
struct super_block *super_p,
int *flags,
char *data
)
{
VFS_T *vfsp = SBTOVFS(super_p);
VFS_LOG(vfsp, VFS_LOG_ERR,
"Vnode/VFS does not support remounting of file systems (sb=%p)\n",
super_p);
return -EINVAL;
}
void
mvfs_clear_inode(struct inode *inode_p)
{
CALL_DATA_T cd;
ASSERT(MDKI_INOISOURS(inode_p));
if (MDKI_INOISMVFS(inode_p)) {
/* If we're an mnode-base vnode, do all this stuff ... */
VNODE_T *vp = ITOV(inode_p);
int error;
ASSERT(I_COUNT(inode_p) == 0);
ASSERT(inode_p->i_state & I_FREEING);
mdki_linux_init_call_data(&cd);
/*
* Do actual deactivation of the vnode/mnode
*/
error = VOP_INACTIVE(vp, &cd);
mdki_linux_destroy_call_data(&cd);
if (error)
MDKI_VFS_LOG(VFS_LOG_ERR, "mvfs_clear_inode: inactive error %d\n",
error);
} else if (MDKI_INOISCLRVN(inode_p)) {
/* cleartext vnode */
vnlayer_linux_free_clrvnode(ITOV(inode_p));
} else {
MDKI_TRACE(TRACE_INACTIVE,"no work: inode_p=%p vp=%p cnt=%d\n", inode_p,
ITOV(inode_p), I_COUNT(inode_p));
}
MDKI_TRACE(TRACE_INACTIVE,"inode_p=%p vp=%p cnt=%d\n", inode_p,
ITOV(inode_p), I_COUNT(inode_p));
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
void
mvfs_evict_inode(struct inode *inode_p)
{
mvfs_clear_inode(inode_p);
truncate_inode_pages(&inode_p->i_data, 0);
end_writeback(inode_p);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
#include <linux/lglock.h>
DECLARE_LGLOCK(vfsmount_lock);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
extern void
mvfs_linux_umount_begin(SUPER_T *super_p)
#else
extern void
mvfs_linux_umount_begin(
struct vfsmount * mnt,
int flags
)
#endif
{
VNODE_T *vp;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
struct vfsmount *mnt;
#else
/*
* Since 2.6.18 and before 2.6.27 we have mnt as a parameter.
* But we still need super_p.
*/
SUPER_T *super_p = mnt->mnt_sb;
#endif
int mount_count = 0;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)) && defined(CONFIG_SMP)
int cpu;
#endif
ASSERT(super_p != NULL);
ASSERT(super_p->s_root != NULL);
vp = ITOV(super_p->s_root->d_inode);
ASSERT(vp != NULL);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
mnt = VTOVFSMNT(vp);
#else
/* Check that the mountpoint passed in matches the one
* from the vp that we are going to clear. Skip it otherwise.
* We know from experience that this can happen when unmounting
* loopback (bind) mounts.
*/
if (mnt != VTOVFSMNT(vp))
return;
#endif
/* Note that there is no mechanism for restoring the mount pointer
* in the vnode if an error happens later on in the umount. This is
* the only callback into the mvfs during umount. So far this has not
* been a problem and if we don't do this here, the umount will never
* succeed because the Linux code expects the mnt_count to be 2.
* The count is 3 at this point from the initial allocation of the
* vfsmnt structure, the path_lookup call in this umount call and
* from when we placed the pointer in the vp.
*/
if (mnt == NULL) {
MDKI_VFS_LOG(VFS_LOG_ERR, "%s: mnt is NULL\n", __FUNCTION__);
return;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
mount_count = atomic_read(&mnt->mnt_count);
#else
# ifdef CONFIG_SMP
br_read_lock(vfsmount_lock);
for_each_possible_cpu(cpu) {
mount_count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
}
br_read_unlock(vfsmount_lock);
# else /* CONFIG_SMP */
mount_count = mnt->mnt_count;
# endif /* else CONFIG_SMP */
#endif /* else < KERNEL_VERSION(2,6,38) */
if (mount_count == 3) {
MDKI_MNTPUT(mnt);
SET_VTOVFSMNT(vp, NULL);
}
}
/* vnlayer_linux_mount
* This is a wrapper function to take the data passed in by the linux
* mount and massage it into a form that is usable by our common routines
*
* IN SUPER_T *super_p
* void *data_p
*
*/
extern int
vnlayer_linux_mount(
VFS_T *vfsp,
void *data_p
)
{
int err;
CRED_T *cred;
MVFS_CALLER_INFO_STRUCT ctx;
cred = MDKI_GET_UCRED();
BZERO(&ctx, sizeof(ctx));
/* VFS_MOUNT method must detect 32- or 64-bit caller, if necessary */
err = VFS_MOUNT(vfsp, NULL, NULL, vfsp->vfs_flag,
data_p, 0, cred, &ctx);
err = mdki_errno_unix_to_linux(err);
MDKI_CRFREE(cred);
return(err);
}
int
vnlayer_fill_super(
SUPER_T *super_p,
void *data_p,
int silent
)
{
INODE_T *ino_p;
VNODE_T *rootvp;
VATTR_T va;
VFS_T *vfsp;
int err = 0;
CALL_DATA_T cd;
ASSERT_KERNEL_LOCKED(); /* sys_mount() */
ASSERT_SB_MOUNT_LOCKED_W(super_p);
/* can't assert on mount_sem, we don't have access to it. */
if (vnlayer_vfs_opvec == NULL) {
MDKI_VFS_LOG(VFS_LOG_ERR,
"%s: VFS operation not set yet "
"(no file system module loaded?)\n", __func__);
err = -ENODATA;
goto return_NULL;
}
if (MDKI_INOISOURS(vnlayer_get_urdir_inode())) {
/* can't handle this case */
MDKI_VFS_LOG(VFS_LOG_ERR,
"%s: can't handle mounts inside setview.\n", __func__);
err = -EINVAL;
goto return_NULL;
}
/*
* The only fields we have coming in are s_type and s_flags.
*/
/* Verify this */
super_p->s_blocksize = MVFS_DEF_BLKSIZE;
super_p->s_blocksize_bits = MVFS_DEF_BLKSIZE_BITS;
super_p->s_maxbytes = MVFS_DEF_MAX_FILESIZE;
super_p->s_op = &mvfs_super_ops;
super_p->s_export_op = &vnlayer_export_ops;
super_p->dq_op = NULL;
super_p->s_magic = MVFS_SUPER_MAGIC;
/*
* XXX This module is currently restricted to one client file system
* type at a time, as registered via the vnlayer_vfs_opvec.
*/
vfsp = KMEM_ALLOC(sizeof(*vfsp), KM_SLEEP);
if (vfsp == NULL) {
MDKI_VFS_LOG(VFS_LOG_ERR, "%s failed: no memory\n", __func__);
SET_SBTOVFS(super_p, NULL);
err = -ENOMEM;
goto return_NULL;
}
BZERO(vfsp, sizeof(*vfsp));
SET_VFSTOSB(vfsp, super_p);
SET_SBTOVFS(super_p, vfsp);
vfsp->vfs_op = vnlayer_vfs_opvec;
/* XXX fill in more of vfsp (flag?) */
if (super_p->s_flags & MS_RDONLY)
vfsp->vfs_flag |= VFS_RDONLY;
if (super_p->s_flags & MS_NOSUID)
vfsp->vfs_flag |= VFS_NOSUID;
err = vnlayer_linux_mount(vfsp, data_p);
if (err) {
goto bailout;
}
/*
* Now create our dentry and set that up in the superblock. Get
* the inode from the vnode at the root of the file system, and
* attach it to a new dentry.
*/
mdki_linux_init_call_data(&cd);
err = VFS_ROOT(SBTOVFS(super_p), &rootvp);
if (err) {
err = mdki_errno_unix_to_linux(err);
(void) VFS_UNMOUNT(vfsp, MVFS_CD2CRED(&cd));
mdki_linux_destroy_call_data(&cd);
goto bailout;
}
ino_p = VTOI(rootvp);
#ifdef CONFIG_FS_POSIX_ACL
/* If the system supports ACLs, we set the flag in the superblock
* depending on the ability of the underlying filesystem
*/
if (vfsp->vfs_flag & VFS_POSIXACL) {
super_p->s_flags |= MS_POSIXACL;
}
#endif
/*
* Call getattr() to prime this inode with real attributes via the
* callback to mdki_linux_vattr_pullup()
*/
VATTR_NULL(&va);
/* ignore error code, we're committed */
(void) VOP_GETATTR(rootvp, &va, 0, &cd);
/* This will allocate a dentry with a name of /, which is
* what Linux uses in all filesystem roots. The dentry is
* also not put on the hash chains because Linux does not
* hash file system roots. It finds them through the super
* blocks.
*/
super_p->s_root = VNODE_D_ALLOC_ROOT(ino_p);
if (super_p->s_root) {
if (VFSTOSB(vnlayer_looproot_vp->v_vfsp) == super_p) {
/* loopback names are done with regular dentry ops */
MDKI_SET_DOPS(super_p->s_root, &vnode_dentry_ops);
} else {
/*
* setview names come in via VOB mounts, they're marked
* with setview dentry ops
*/
MDKI_SET_DOPS(super_p->s_root, &vnode_setview_dentry_ops);
}
super_p->s_root->d_fsdata = NULL;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
atomic_set(&super_p->s_root->d_count, 1);
#endif
/* d_alloc_root assumes that the caller will take care of
* bumping the inode count for the dentry. So we will oblige
*/
igrab(ino_p);
} else {
VN_RELE(rootvp);
(void) VFS_UNMOUNT(vfsp, MVFS_CD2CRED(&cd));
mdki_linux_destroy_call_data(&cd);
err = -ENOMEM;
goto bailout;
}
mdki_linux_destroy_call_data(&cd);
super_p->s_dirt = 1; /* we want to be called on
write_super/sync() */
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38)
/* write back is delegated to the undelying fs */
super_p->s_bdi = &noop_backing_dev_info;
#endif
/*
* release reference on rootvp--super block holds appropriate
* references now
*/
VN_RELE(rootvp);
return(0);
bailout:
MDKI_VFS_LOG(VFS_LOG_ERR,
"%s failed: error %d\n", __func__,
vnlayer_errno_linux_to_unix(err));
SET_SBTOVFS(super_p, NULL);
KMEM_FREE(vfsp, sizeof(*vfsp));
return_NULL:
return(err);
}
/* This function is a callback from sget. We will setup the device number
* to be our default major and minor = 0. We will set the real device number
* later in vnlayer_fill_super when it calls into the mvfs proper. The
* reason for not just making this the call to fill_super is that we are
* called with the sb_lock spinlock held so we can't pend and vnlayer_fill_super
* allocates memory.
*/
int vnlayer_set_sb(
struct super_block *sb,
void *data
)
{
sb->s_dev = MKDEV(mvfs_major, 0);
return 0;
}
/* The regular block device interface would try to obtain partition information
* for the block device in question. The mvfs registers a block device
* merely to establish a set of major device numbers. We have no physical
* device and no partitions so the get_sb_bdev() interface does not work
* for us. Get_sb_nodev() explicitly uses major device 0 so that is not
* an option for us. Instead we use the sget() interface which gives us the
* ability to get a superblock and handle the device issues ourselves while the
* kernel handles getting it placed on the appropriate lists.
*
* Since 2.6.18 they added vfsmount to the parameters and changed the
* return type to int, but then since 2.6.39 the .get_sb callback was replaced
* by .mount and the interface changed substantially. So, for the sake of
* clarity, the function has been rewritten.
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
SUPER_T *
vnlayer_get_sb(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *raw_data
)
#else
int
vnlayer_get_sb(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *raw_data,
struct vfsmount *mnt
)
#endif
{
SUPER_T *sb;
int err;
sb = sget(fs_type, NULL, vnlayer_set_sb, raw_data);
if (IS_ERR(sb)) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
return sb;
#else
return PTR_ERR(sb);
#endif
}
err = vnlayer_fill_super(sb, raw_data, 0);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
if (err != 0) {
generic_shutdown_super(sb);
sb = ERR_PTR(err);
}
return sb;
#else
if (err != 0) {
generic_shutdown_super(sb);
}
else {
/* We need to fill the mnt struct */
simple_set_mnt(mnt, sb);
}
return err;
#endif
}
#else
struct dentry *
vnlayer_mount(
struct file_system_type *fs_type,
int flags,
const char *dev_name,
void *data
)
{
SUPER_T *sb;
int err;
sb = sget(fs_type, NULL, vnlayer_set_sb, data);
if (IS_ERR(sb)) {
return (struct dentry *)(sb);
}
sb->s_flags = flags;
if (sb->s_root == NULL) {
err = vnlayer_fill_super(sb, data, 0);
if (err != 0) {
deactivate_locked_super(sb);
return ERR_PTR(err);
}
sb->s_flags |= MS_ACTIVE;
}
/* return a dget dentry */
return dget(sb->s_root);
}
#endif
/* generic_shutdown_super() will call our put_super function which
* will handle all of our specific cleanup.
*/
void
vnlayer_kill_sb(SUPER_T *sbp)
{
generic_shutdown_super(sbp);
}
INODE_T *
vnlayer_alloc_inode(SUPER_T *sbp)
{
vnlayer_vnode_t *vnlvp;
vnlvp = (vnlayer_vnode_t *)kmem_cache_alloc(vnlayer_vnode_cache, GFP_KERNEL);
if (vnlvp != NULL) {
/* The inode used to be initialized by the kernel slab allocator
** because we used it to allocate an inode directly (see init_once() in
** fs/inode.c). However, now we allocate our vnode with an inode
** inside of it so we have to do the initialization ourselves.
**
** The caller must fully initialize the associated vnode.
*/
inode_init_once(&vnlvp->vnl_inode);
return(&vnlvp->vnl_inode);
} else {
return(NULL);
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
static void
vnlayer_destroy_inode_callback(struct rcu_head *head)
{
struct inode *inode_p = container_of(head, struct inode, i_rcu);
INIT_LIST_HEAD(&inode_p->i_dentry);
ASSERT(I_COUNT(inode_p) == 0);
ASSERT(inode_p->i_state & I_FREEING);
kmem_cache_free(vnlayer_vnode_cache, (vnlayer_vnode_t *) ITOV(inode_p));
}
#endif
void
vnlayer_destroy_inode(INODE_T *inode)
{
ASSERT(I_COUNT(inode) == 0);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
kmem_cache_free(vnlayer_vnode_cache, (vnlayer_vnode_t *)ITOV(inode));
#else
call_rcu(&inode->i_rcu, vnlayer_destroy_inode_callback);
#endif
}
#ifndef roundup
#define roundup(val, upto) (( ((val) + (upto) - 1) / (upto)) * (upto))
#endif
/*
* NFS access to vnode file systems.
*
* We provide dentry_to_fh() and fh_to_dentry() methods so that the
* vnode-based file system can hook up its VOP_FID() and VFS_VGET()
* methods. The Linux NFS server calls these methods when encoding an
* object into a file handle to be passed to the client for future
* use, and when decoding a file handle and looking for the file
* system object it describes.
*
* VOP_FID() takes a vnode and provides a file ID (fid) that can later
* be presented (in a pair with a VFS pointer) to VFS_VGET() to
* reconstitute that vnode. In a Sun ONC-NFS style kernel, VOP_FID()
* is used twice per file handle, once for the exported directory and
* once for the object itself. In Linux, the NFS layer itself handles
* the export tree checking (depending on the status of
* NFSEXP_NOSUBTREECHECK), so the file system only needs to fill in
* the file handle with details for the object itself. We always
* provide both object and parent in the file handle to be sure that
* we don't end up short on file handle space in a future call that
* requires both.
*
* On a call from the NFS client, the Linux NFS layer finds a
* superblock pointer from the file handle passed by the NFS client,
* then calls the fh_to_dentry() method to get a dentry. Sun ONC-NFS
* kernels call VFS_VGET() on a vfsp, passing the FID portion of the
* file handle. In this layer, we unpack the file handle, determine
* whether the parent or the object is needed, and pass the info along
* to a VFS_VGET() call. Once that returns, we look for an attached
* dentry and use it, or fabricate a new one which NFS will attempt to
* reconnect to the namespace.
*/
int
vnlayer_dentry_to_fh(
struct dentry *dent,
__u32 *fh,
int *lenp,
int need_parent
)
{
int error;
int type;
int mylen;
MDKI_FID_T *lfidp = NULL;
MDKI_FID_T *parent_fidp = NULL;
mdki_boolean_t bailout_needed = TRUE; /* Assume we'll fail. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
SUPER_T *sbp;
#endif
/*
* We use the type byte (return value) to encode the FH length. Since we
* always include two FIDs of the same size, the type must be even, so
* that's how we "encode" the length of each FID (i.e. it is half the total
* length).
*
* Always include parent entry; this makes sure that we only work with NFS
* protocols that have enough room for our file handles. (Without this, we
* may return a directory file handle OK yet be unable to return a plain
* file handle.) Currently, we can just barely squeeze two standard
* 10-byte vnode FIDs into the NFS v2 file handle. The NFS v3 handle has
* plenty of room.
*/
ASSERT(ITOV(dent->d_inode));
error = VOP_FID(ITOV(dent->d_inode), &lfidp);
if (error != 0) {
ASSERT(lfidp == NULL);
goto bailout;
}
ASSERT(ITOV(dent->d_parent->d_inode));
error = VOP_FID(ITOV(dent->d_parent->d_inode), &parent_fidp);
if (error != 0) {
ASSERT(parent_fidp == NULL);
goto bailout;
}
/*
* Our encoding scheme can't tolerate different length FIDs
* (because otherwise the type wouldn't be guaranteed to be even).
*/
if (parent_fidp->fid_len != lfidp->fid_len) {
MDKI_VFS_LOG(VFS_LOG_ERR,
"%s: unbalanced parent/child fid lengths: %d, %d\n",
__func__, parent_fidp->fid_len, lfidp->fid_len);
goto bailout;
}
/*
* vnode layer needs to release the storage for a fid on
* Linux. The VOP_FID() function allocates its own fid in
* non-error cases. Other UNIX systems release this storage
* in the caller of VOP_FID, so we have to do it here. We
* copy the vnode-style fid into the caller-allocated space,
* then free our allocated version here.
*
* Remember: vnode lengths are counting bytes, Linux lengths count __u32
* units.
*/
type = parent_fidp->fid_len + lfidp->fid_len; /* Guaranteed even. */
mylen = roundup(type + MDKI_FID_EXTRA_SIZE, sizeof(*fh));
if (mylen == VNODE_NFS_FH_TYPE_RESERVED ||
mylen >= VNODE_NFS_FH_TYPE_ERROR)
{
MDKI_VFS_LOG(VFS_LOG_ESTALE,
"%s: required length %d out of range (%d,%d)\n",
__func__, mylen,
VNODE_NFS_FH_TYPE_RESERVED, VNODE_NFS_FH_TYPE_ERROR);
goto bailout;
}
if (((*lenp) * sizeof(*fh)) < mylen) {
MDKI_VFS_LOG(VFS_LOG_ESTALE,
"%s: need %d bytes for FH, have %d\n",
__func__, mylen, (int) (sizeof(*fh) * (*lenp)));
goto bailout;
}
/* Copy FIDs into file handle. */
*lenp = mylen / sizeof(*fh); /* No remainder because of roundup above. */
BZERO(fh, mylen); /* Zero whole fh to round up to __u32 boundary */
BCOPY(lfidp->fid_data, fh, lfidp->fid_len);
BCOPY(parent_fidp->fid_data, ((caddr_t)fh) + (type / 2),
parent_fidp->fid_len);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
/*
* For 64 bits OS, use a 32 bits hash of the SB pointer.
* For 32 bits OS, use the pointer itself.
*/
if (ITOV(dent->d_inode) == NULL ||
ITOV(dent->d_inode)->v_vfsmnt == NULL) {
MDKI_VFS_LOG(VFS_LOG_ESTALE,
"%s: %p is this a MVFS inode?\n",
__func__, dent->d_inode);
goto bailout;
} else {
sbp = ((struct vfsmount *)ITOV(dent->d_inode)->v_vfsmnt)->mnt_sb;
}
MDKI_FID_SET_SB_HASH(fh, type / 2, MDKI_FID_CALC_HASH(sbp));
#endif
bailout_needed = FALSE; /* We're home free now. */
if (bailout_needed) {
bailout:
type = VNODE_NFS_FH_TYPE_ERROR;
*lenp = 0;
}
#ifdef KMEMDEBUG
if (lfidp != NULL)
REAL_KMEM_FREE(lfidp, MDKI_FID_LEN(lfidp));
if (parent_fidp != NULL)
REAL_KMEM_FREE(parent_fidp, MDKI_FID_LEN(parent_fidp));
#else
if (lfidp != NULL)
KMEM_FREE(lfidp, MDKI_FID_LEN(lfidp));
if (parent_fidp != NULL)
KMEM_FREE(parent_fidp, MDKI_FID_LEN(parent_fidp));
#endif
return type;
}
STATIC int
vnlayer_unpack_fh(
__u32 *fh,
int len, /* counted in units of 4-bytes */
int fhtype,
int fidlen,
MDKI_FID_T *lfidp,
MDKI_FID_T *plfidp
)
{
if (len * sizeof(*fh) < fhtype) {
/*
* we put the size in the type on the way out;
* make sure we have enough data returning now
*/
MDKI_VFS_LOG(VFS_LOG_ESTALE,
"%s: FH doesn't have enough data for type:"
" has %d need %d\n",
__func__, (int)(len * sizeof(*fh)), fhtype);
return -EINVAL;
}
if ((fhtype & 1) != 0) {
/*
* The type/length must be even (there are two equal-sized
* halves in the payload). Somebody might be fabricating file
* handles?
*/
MDKI_VFS_LOG(VFS_LOG_ESTALE,
"%s: FH type (%d) not even\n", __func__, fhtype);
return -EINVAL;
}
if (lfidp != NULL) {
/* object */
lfidp->fid_len = fidlen;
BCOPY(fh, lfidp->fid_data, fidlen);
}
if (plfidp != NULL) {
/* parent */
plfidp->fid_len = fidlen;
BCOPY(((caddr_t)fh) + fidlen, plfidp->fid_data, fidlen);
}
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
#include <linux/sunrpc/svc.h>
#include <linux/nfsd/nfsd.h>
struct dentry *
vnlayer_decode_fh(
SUPER_T *sb,
__u32 *fh,