This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inode.c
executable file
·896 lines (775 loc) · 24.1 KB
/
inode.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
/* dazukofs: access control stackable filesystem
Copyright (C) 1997-2004 Erez Zadok
Copyright (C) 2001-2004 Stony Brook University
Copyright (C) 2004-2007 International Business Machines Corp.
Copyright (C) 2008-2010 John Ogness
Author: John Ogness <[email protected]>
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
of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/uaccess.h>
#include <linux/fs_stack.h>
#include <linux/slab.h>
#include "dazukofs_fs.h"
static struct inode_operations dazukofs_symlink_iops;
static struct inode_operations dazukofs_dir_iops;
static struct inode_operations dazukofs_main_iops;
static int dazukofs_inode_test(struct inode *inode,
void *candidate_lower_inode)
{
if (get_lower_inode(inode) ==
(struct inode *)candidate_lower_inode) {
return 1;
}
return 0;
}
static void dazukofs_init_inode(struct inode *inode, struct inode *lower_inode)
{
set_lower_inode(inode, lower_inode);
inode->i_ino = lower_inode->i_ino;
inode->i_version++;
inode->i_op = &dazukofs_main_iops;
inode->i_fop = &dazukofs_main_fops;
inode->i_mapping->a_ops = &dazukofs_aops;
}
static int dazukofs_inode_set(struct inode *inode, void *lower_inode)
{
dazukofs_init_inode(inode, (struct inode *)lower_inode);
return 0;
}
/**
* dazukofs_interpose - fill in new dentry, linking it to the lower dentry
* @lower_dentry: the corresponding lower dentry
* @denty: the new DazukoFS dentry
* @sb: super block of DazukoFS
* @already_hashed: flag to signify if "dentry" is already hashed
*
* Description: This is the key function which sets up all the hooks to
* give DazukoFS control.
*
* Returns 0 on success.
*/
int dazukofs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
struct super_block *sb, int already_hashed)
{
struct inode *inode;
struct inode *lower_inode = igrab(lower_dentry->d_inode);
if (!lower_inode)
return -ESTALE;
if (lower_inode->i_sb != get_lower_sb(sb)) {
iput(lower_inode);
return -EXDEV;
}
inode = iget5_locked(sb, (unsigned long)lower_inode,
dazukofs_inode_test, dazukofs_inode_set,
lower_inode);
if (!inode) {
iput(lower_inode);
return -EACCES;
}
if (inode->i_state & I_NEW) {
unlock_new_inode(inode);
/*
* This is a new node so we leave the lower_node "in use"
* and do not call iput().
*/
} else {
/*
* This is not a new node so we decrement the usage count.
*/
iput(lower_inode);
}
if (S_ISLNK(lower_inode->i_mode))
inode->i_op = &dazukofs_symlink_iops;
else if (S_ISDIR(lower_inode->i_mode))
inode->i_op = &dazukofs_dir_iops;
if (S_ISDIR(lower_inode->i_mode))
inode->i_fop = &dazukofs_dir_fops;
if (special_file(lower_inode->i_mode)) {
init_special_inode(inode, lower_inode->i_mode,
lower_inode->i_rdev);
}
dentry->d_op = &dazukofs_dops;
if (already_hashed)
d_add(dentry, inode);
else
d_instantiate(dentry, inode);
fsstack_copy_attr_all(inode, lower_inode);
fsstack_copy_inode_size(inode, lower_inode);
return 0;
}
/**
* Description: Called when the VFS needs to look up an inode in a parent
* directory. The name to look for is found in the dentry. This method
* must call d_add() to insert the found inode into the dentry. The
* "i_count" field in the inode structure should be incremented. If the
* named inode does not exist a NULL inode should be inserted into the
* dentry (this is called a negative dentry). Returning an error code
* from this routine must only be done on a real error, otherwise
* creating inodes with system calls like create(2), mknod(2), mkdir(2)
* and so on will fail. If you wish to overload the dentry methods then
* you should initialise the "d_dop" field in the dentry; this is a
* pointer to a struct "dentry_operations". This method is called with
* the directory inode semaphore held.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
static struct dentry* dazukofs_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags)
#else
static struct dentry *dazukofs_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
#endif
{
struct dentry *lower_dentry;
struct dentry *lower_dentry_parent;
struct vfsmount *lower_mnt;
int err = 0;
/* check for "." or ".." (they are not relevant here) */
if (dentry->d_name.len == 1) {
if (dentry->d_name.name[0] == '.') {
d_drop(dentry);
goto out;
}
} else if (dentry->d_name.len == 2) {
if (dentry->d_name.name[0] == '.' &&
dentry->d_name.name[1] == '.') {
d_drop(dentry);
goto out;
}
}
dentry->d_op = &dazukofs_dops;
lower_dentry_parent = get_lower_dentry(dentry->d_parent);
mutex_lock(&lower_dentry_parent->d_inode->i_mutex);
lower_dentry = lookup_one_len(dentry->d_name.name,
lower_dentry_parent,
dentry->d_name.len);
mutex_unlock(&lower_dentry_parent->d_inode->i_mutex);
if (IS_ERR(lower_dentry)) {
err = PTR_ERR(lower_dentry);
d_drop(dentry);
goto out;
}
BUG_ON(!lower_dentry->d_count);
set_dentry_private(dentry,
kmem_cache_zalloc(dazukofs_dentry_info_cachep,
GFP_KERNEL));
if (!get_dentry_private(dentry)) {
err = -ENOMEM;
goto out_dput;
}
fsstack_copy_attr_atime(dir, lower_dentry_parent->d_inode);
lower_mnt = mntget(get_lower_mnt(dentry->d_parent));
set_lower_dentry(dentry, lower_dentry, lower_mnt);
if (!lower_dentry->d_inode) {
/*
* We want to add because we could not find in lower.
*/
d_add(dentry, NULL);
goto out;
}
err = dazukofs_interpose(lower_dentry, dentry, dir->i_sb, 1);
if (err)
goto out_dput;
goto out;
out_dput:
dput(lower_dentry);
d_drop(dentry);
out:
return ERR_PTR(err);
}
/**
* Description: Called by the mknod(2) system call to create a device
* (char, block) inode or a named pipe (FIFO) or socket. Only required if
* you want to support creating these types of inodes. You will probably
* need to call d_instantiate() just as you would in the create() method.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
static int dazukofs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
dev_t dev)
#else
static int dazukofs_mknod(struct inode *dir, struct dentry *dentry, int mode,
dev_t dev)
#endif
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_mknod(lower_dentry_parent_inode, lower_dentry, mode, dev);
if (err)
goto out;
err = dazukofs_interpose(lower_dentry, dentry, dir->i_sb, 0);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
fsstack_copy_inode_size(dir, lower_dentry_parent_inode);
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the mkdir(2) system call. Only required if you
* want to support creating subdirectories. You will probably need to call
* d_instantiate() just as you would in the create() method.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
static int dazukofs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
#else
static int dazukofs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
#endif
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_mkdir(lower_dentry_parent_inode, lower_dentry, mode);
if (err)
goto out;
err = dazukofs_interpose(lower_dentry, dentry, dir->i_sb, 0);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
fsstack_copy_inode_size(dir, lower_dentry_parent_inode);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
set_nlink(dir, lower_dentry_parent_inode->i_nlink);
#else
dir->nlink = lower_dentry_parent_inode->i_nlink;
#endif
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the open(2) and creat(2) system calls. Only
* required if you want to support regular files. The dentry you get
* should not have an inode (i.e. it should be a negative dentry). Here
* you will probably call d_instantiate() with the dentry and the newly
* created inode.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
static int dazukofs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
#else
static int dazukofs_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *nd)
#endif
{
struct vfsmount *lower_mnt = get_lower_mnt(dentry);
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
struct vfsmount *vfsmount_save;
struct dentry *dentry_save;
vfsmount_save = nd->path.mnt;
dentry_save = nd->path.dentry;
nd->path.mnt = mntget(lower_mnt);
nd->path.dentry = dget(lower_dentry);
err = vfs_create(lower_dentry_parent_inode, lower_dentry, mode, nd);
#else
err = vfs_create(lower_dentry_parent_inode, lower_dentry, mode, excl);
#endif
mntput(lower_mnt);
dput(lower_dentry);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
nd->path.mnt = vfsmount_save;
nd->path.dentry = dentry_save;
#endif
if (err)
goto out;
err = dazukofs_interpose(lower_dentry, dentry, dir->i_sb, 0);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
fsstack_copy_inode_size(dir, lower_dentry_parent_inode);
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the symlink(2) system call. Only required if you
* want to support symlinks. You will probably need to call d_instantiate()
* just as you would in the create() method.
*/
static int dazukofs_symlink(struct inode *dir, struct dentry *dentry,
const char *symname)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_symlink(lower_dentry_parent_inode, lower_dentry, symname);
if (err)
goto out;
err = dazukofs_interpose(lower_dentry, dentry, dir->i_sb, 0);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
fsstack_copy_inode_size(dir, lower_dentry_parent_inode);
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the readlink(2) system call. Only required if
* you want to support reading symbolic links.
*/
static int dazukofs_readlink(struct dentry *dentry, char __user *buf,
int bufsiz)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *lower_dentry_inode = lower_dentry->d_inode;
int err;
if (!lower_dentry_inode) {
err = -ENOENT;
d_drop(dentry);
goto out;
}
if (!lower_dentry_inode->i_op->readlink) {
err = -EINVAL;
goto out;
}
err = lower_dentry_inode->i_op->readlink(lower_dentry, buf, bufsiz);
if (err)
goto out;
fsstack_copy_attr_times(dentry->d_inode, lower_dentry_inode);
out:
return err;
}
/**
* Description: Called by the VFS to follow a symbolic link to the inode
* it points to. Only required if you want to support symbolic links. This
* method returns a void pointer cookie that is passed to put_link().
*/
static void *dazukofs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
mm_segment_t fs_save;
int rc;
char *buf;
int len = PAGE_SIZE;
int err = 0;
/*
* Released in dazukofs_put_link(). Only release here on error.
*/
buf = kmalloc(len, GFP_KERNEL);
if (!buf) {
err = -ENOMEM;
goto out;
}
fs_save = get_fs();
set_fs(get_ds());
rc = dazukofs_readlink(dentry, (char __user *)buf, len);
set_fs(fs_save);
if (rc < 0) {
err = rc;
goto out_free;
}
buf[rc] = 0;
nd_set_link(nd, buf);
goto out;
out_free:
kfree(buf);
out:
return ERR_PTR(err);
}
/**
* Description: Called by the VFS to release resources allocated by
* follow_link(). The cookie returned by follow_link() is passed to this
* method as the last parameter. It is used by filesystems such as NFS
* where page cache is not stable (i.e. page that was installed when the
* symbolic link walk started might not be in the page cache at the end
* of the walk).
*/
static void dazukofs_put_link(struct dentry *dentry, struct nameidata *nd,
void *ptr)
{
/*
* Release the char* from dazukofs_follow_link().
*/
kfree(nd_get_link(nd));
}
/**
* Description: Called by the VFS to check for access rights on a
* POSIX-like filesystem.
*/
static int dazukofs_permission(struct inode *inode, int mask)
{
return inode_permission(get_lower_inode(inode), mask);
}
/**
* Description: Called by the VFS to get attributes for a file.
*/
static int dazukofs_getattr(struct vfsmount *mnt, struct dentry *dentry,
struct kstat *stat)
{
struct inode *inode = dentry->d_inode;
struct inode *lower_inode = get_lower_inode(inode);
struct dentry *lower_dentry;
struct vfsmount *lower_mnt;
int err;
if (!lower_inode->i_op->getattr) {
generic_fillattr(inode, stat);
return 0;
}
lower_mnt = mntget(get_lower_mnt(dentry));
lower_dentry = get_lower_dentry(dentry);
err = lower_inode->i_op->getattr(lower_mnt, lower_dentry, stat);
mntput(lower_mnt);
return err;
}
/**
* Description: Called by the VFS to set attributes for a file. This method
* is called by chmod(2) and related system calls.
*/
static int dazukofs_setattr(struct dentry *dentry, struct iattr *ia)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *inode = dentry->d_inode;
struct inode *lower_inode = get_lower_inode(inode);
int err;
/*
* mode change is for clearing setuid/setgid bits. Allow lower fs
* to interpret this in its own way.
*/
if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
ia->ia_valid &= ~ATTR_MODE;
mutex_lock(&lower_inode->i_mutex);
err = notify_change(lower_dentry, ia);
mutex_unlock(&lower_inode->i_mutex);
fsstack_copy_attr_all(inode, lower_inode);
fsstack_copy_inode_size(inode, lower_inode);
return err;
}
/**
* Description: Called by the VFS to set an extended attribute for a file.
* Extended attribute is a name:value pair associated with an inode. This
* method is called by setxattr(2) system call.
*/
static int dazukofs_setxattr(struct dentry *dentry, const char *name,
const void *value, size_t size, int flags)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *lower_dentry_inode = lower_dentry->d_inode;
int err;
if (!lower_dentry_inode) {
err = -ENOENT;
d_drop(dentry);
goto out;
}
if (!lower_dentry_inode->i_op->setxattr) {
err = -EOPNOTSUPP;
goto out;
}
mutex_lock(&lower_dentry_inode->i_mutex);
err = lower_dentry_inode->i_op->setxattr(lower_dentry, name, value,
size, flags);
mutex_unlock(&lower_dentry_inode->i_mutex);
fsstack_copy_attr_all(dentry->d_inode, lower_dentry_inode);
fsstack_copy_inode_size(dentry->d_inode, lower_dentry_inode);
out:
return err;
}
/**
* Description: Called by the VFS to retrieve the value of an extended
* attribute name. This method is called by getxattr(2) function call.
*/
static ssize_t dazukofs_getxattr(struct dentry *dentry, const char *name,
void *value, size_t size)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *lower_dentry_inode = lower_dentry->d_inode;
ssize_t err;
if (!lower_dentry_inode) {
err = -ENOENT;
d_drop(dentry);
goto out;
}
if (!lower_dentry_inode->i_op->getxattr) {
err = -EOPNOTSUPP;
goto out;
}
err = lower_dentry_inode->i_op->getxattr(lower_dentry, name,
value, size);
out:
return err;
}
/**
* Description: Called by the VFS to list all extended attributes for a
* given file. This method is called by listxattr(2) system call.
*/
static ssize_t dazukofs_listxattr(struct dentry *dentry, char *list,
size_t size)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *lower_dentry_inode = lower_dentry->d_inode;
int err;
if (!lower_dentry_inode) {
err = -ENOENT;
d_drop(dentry);
goto out;
}
if (!lower_dentry_inode->i_op->listxattr) {
err = -EOPNOTSUPP;
goto out;
}
err = lower_dentry_inode->i_op->listxattr(lower_dentry, list, size);
out:
return err;
}
/**
* Description: Called by the VFS to remove an extended attribute from a
* file. This method is called by removexattr(2) system call.
*/
static int dazukofs_removexattr(struct dentry *dentry, const char *name)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct inode *lower_dentry_inode = lower_dentry->d_inode;
int err;
if (!lower_dentry_inode) {
err = -ENOENT;
d_drop(dentry);
goto out;
}
if (!lower_dentry_inode->i_op->removexattr) {
err = -EOPNOTSUPP;
goto out;
}
mutex_lock(&lower_dentry_inode->i_mutex);
err = lower_dentry_inode->i_op->removexattr(lower_dentry, name);
mutex_unlock(&lower_dentry_inode->i_mutex);
out:
return err;
}
/**
* Description: Called by the link(2) system call. Only required if you want
* to support hard links. You will probably need to call d_instantiate()
* just as you would in the create() method.
*/
static int dazukofs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *new_dentry)
{
struct dentry *lower_old_dentry = get_lower_dentry(old_dentry);
struct dentry *lower_new_dentry = get_lower_dentry(new_dentry);
struct dentry *lower_dentry_parent = dget(lower_new_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_link(lower_old_dentry, lower_dentry_parent_inode,
lower_new_dentry);
if (err)
goto out;
err = dazukofs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
fsstack_copy_inode_size(dir, lower_dentry_parent_inode);
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the unlink(2) system call. Only required if you
* want to support deleting inodes.
*/
static int dazukofs_unlink(struct inode *dir, struct dentry *dentry)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_unlink(lower_dentry_parent_inode, lower_dentry);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
set_nlink(dentry->d_inode, get_lower_inode(dentry->d_inode)->i_nlink);
#else
dentry->d_inode->n_link = get_lower_inode(dentry->d_inode)->i_nlink;
#endif
fsstack_copy_attr_times(dentry->d_inode, dir);
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
return err;
}
/**
* Description: Called by the rmdir(2) system call. Only required if you
* want to support deleting subdirectories.
*/
static int dazukofs_rmdir(struct inode *dir, struct dentry *dentry)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
struct dentry *lower_dentry_parent = dget(lower_dentry->d_parent);
struct inode *lower_dentry_parent_inode = lower_dentry_parent->d_inode;
int err;
mutex_lock_nested(&(lower_dentry_parent_inode->i_mutex),
I_MUTEX_PARENT);
err = vfs_rmdir(lower_dentry_parent_inode, lower_dentry);
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dentry_parent_inode);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
set_nlink(dir, lower_dentry_parent_inode->i_nlink);
set_nlink(dentry->d_inode, get_lower_inode(dentry->d_inode)->i_nlink);
#else
dir->i_nlink = lower_dentry_parent_inode->i_nlink;
dentry->d_inode->i_nlink = get_lower_inode(dentry->d_inode)->i_nlink;
#endif
out:
mutex_unlock(&(lower_dentry_parent_inode->i_mutex));
dput(lower_dentry_parent);
if (!err)
d_drop(dentry);
return err;
}
/**
* Description: Called by the rename(2) system call to rename the object to
* have the parent and name given by the second inode and dentry.
*/
static int dazukofs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
struct dentry *lower_old_dentry = get_lower_dentry(old_dentry);
struct dentry *lower_new_dentry = get_lower_dentry(new_dentry);
struct dentry *lower_old_dentry_parent =
dget(lower_old_dentry->d_parent);
struct dentry *lower_new_dentry_parent =
dget(lower_new_dentry->d_parent);
struct inode *lower_old_dentry_parent_inode =
lower_old_dentry_parent->d_inode;
struct inode *lower_new_dentry_parent_inode =
lower_new_dentry_parent->d_inode;
int err = -ENOENT;
if (!lower_old_dentry_parent_inode) {
d_drop(old_dentry);
goto out;
}
if (!lower_new_dentry_parent_inode) {
d_drop(new_dentry);
goto out;
}
lock_rename(lower_old_dentry_parent, lower_new_dentry_parent);
err = vfs_rename(lower_old_dentry_parent_inode, lower_old_dentry,
lower_new_dentry_parent_inode, lower_new_dentry);
unlock_rename(lower_old_dentry_parent, lower_new_dentry_parent);
if (err)
goto out;
fsstack_copy_attr_all(new_dir, lower_new_dentry_parent_inode);
if (new_dir != old_dir)
fsstack_copy_attr_all(old_dir, lower_old_dentry_parent_inode);
out:
dput(lower_old_dentry_parent);
dput(lower_new_dentry_parent);
return err;
}
/**
* Unused operations:
* - create
* - lookup
* - link
* - unlink
* - symlink
* - mkdir
* - rmdir
* - mknod
* - rename
* - truncate
* - truncate_range
* - fallocate
*/
static struct inode_operations dazukofs_symlink_iops = {
.readlink = dazukofs_readlink,
.follow_link = dazukofs_follow_link,
.put_link = dazukofs_put_link,
.permission = dazukofs_permission,
.getattr = dazukofs_getattr,
.setattr = dazukofs_setattr,
.setxattr = dazukofs_setxattr,
.getxattr = dazukofs_getxattr,
.listxattr = dazukofs_listxattr,
.removexattr = dazukofs_removexattr,
};
/**
* Unused operations:
* - readlink
* - follow_link
* - put_link
* - truncate
* - truncate_range
* - fallocate
*/
static struct inode_operations dazukofs_dir_iops = {
.create = dazukofs_create,
.lookup = dazukofs_lookup,
.link = dazukofs_link,
.unlink = dazukofs_unlink,
.symlink = dazukofs_symlink,
.mkdir = dazukofs_mkdir,
.rmdir = dazukofs_rmdir,
.mknod = dazukofs_mknod,
.rename = dazukofs_rename,
.permission = dazukofs_permission,
.getattr = dazukofs_getattr,
.setattr = dazukofs_setattr,
.setxattr = dazukofs_setxattr,
.getxattr = dazukofs_getxattr,
.listxattr = dazukofs_listxattr,
.removexattr = dazukofs_removexattr,
};
/**
* Unused operations:
* - create
* - lookup
* - link
* - unlink
* - symlink
* - mkdir
* - rmdir
* - mknod
* - rename
* - readlink
* - follow_link
* - put_link
* - truncate
* - truncate_range
* - fallocate
*/
static struct inode_operations dazukofs_main_iops = {
.permission = dazukofs_permission,
.getattr = dazukofs_getattr,
.setattr = dazukofs_setattr,
.setxattr = dazukofs_setxattr,
.getxattr = dazukofs_getxattr,
.listxattr = dazukofs_listxattr,
.removexattr = dazukofs_removexattr,
};