forked from Ninals-GitHub/Learning-Ext2-Filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme2fs_dir.c
1119 lines (944 loc) · 28.8 KB
/
me2fs_dir.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
/********************************************************************************
File : me2fs_dir.c
Description : Directory Operations of my ext2 file system
*********************************************************************************/
#include <linux/buffer_head.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include "me2fs.h"
#include "me2fs_util.h"
#include "me2fs_inode.h"
#include "me2fs_dir.h"
/*
==================================================================================
Prototype Statement
==================================================================================
*/
/*
---------------------------------------------------------------------------------
File Operations for Directory
---------------------------------------------------------------------------------
*/
static int me2fsReadDir( struct file *file, struct dir_context *ctx );
/*
---------------------------------------------------------------------------------
Helper Functions for Directory
---------------------------------------------------------------------------------
*/
static inline unsigned long getDirNumPages( struct inode *inode );
static struct page*
me2fsGetDirPageCache( struct inode *inode, unsigned long index );
static inline void me2fsPutDirPageCache( struct page *page );
static unsigned long
me2fsGetPageLastByte( struct inode *inode, unsigned long page_nr );
static inline int
me2fsStrncmp( int len, const char* const name, struct ext2_dir_entry *dent );
static inline void
setDirEntryType( struct ext2_dir_entry *dent, struct inode *inode );
static inline int
prepareWriteBlock( struct page *page, loff_t pos, unsigned long len );
static int commitBlockWrite( struct page *page, loff_t pos, unsigned long len );
/*
==================================================================================
DEFINES
==================================================================================
*/
/*
==================================================================================
Management
==================================================================================
*/
const struct file_operations me2fs_dir_operations =
{
.llseek = generic_file_llseek,
.read = generic_read_dir,
.iterate = me2fsReadDir,
//.open = me2fsOpen,
.fsync = generic_file_fsync,
};
//#define S_SHIFT 16
#define S_SHIFT 12
static unsigned char me2fs_type_by_mode[ S_IFMT >> S_SHIFT ] =
{
[ S_IFREG >> S_SHIFT ] = EXT2_FT_REG_FILE,
[ S_IFDIR >> S_SHIFT ] = EXT2_FT_DIR,
[ S_IFCHR >> S_SHIFT ] = EXT2_FT_CHRDEV,
[ S_IFBLK >> S_SHIFT ] = EXT2_FT_BLKDEV,
[ S_IFIFO >> S_SHIFT ] = EXT2_FT_FIFO,
[ S_IFSOCK >> S_SHIFT ] = EXT2_FT_SOCK,
[ S_IFLNK >> S_SHIFT ] = EXT2_FT_SYMLINK,
};
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Open Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsGetInoByName
Input :struct inode *dir
< vfs inode of directory >
struct qstr *child
< query name which is child of the directory >
Output :void
Return :ino_t
< inode number >
Description :lookup by name in directory and return its inode number
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
ino_t me2fsGetInoByName( struct inode *dir, struct qstr *child )
{
struct ext2_dir_entry *dent;
struct page *page;
ino_t ino;
dent = me2fsFindDirEntry( dir, child, &page );
if( dent )
{
ino = le32_to_cpu( dent->inode );
me2fsPutDirPageCache( page );
}
else
{
ino = 0;
}
return( ino );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsFindDirEntry
Input :struct inode *dir
< vfs inode of directory >
struct qstr *child
< query;child of directory >
struct page **res_page
< output >
Output :struct page **res_page
< the entry was found in >
Return :struct ext2_dir_entry *
< found entry >
Description :find an entry in the directory, ouput the page in which the
entry was found( **res_page ) and return entry.
page is returned mapped and unlocked. the entry is guaranteed
to be valid.
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
struct ext2_dir_entry*
me2fsFindDirEntry( struct inode *dir,
struct qstr *child,
struct page **res_page )
{
struct page *page;
struct ext2_dir_entry *dent;
unsigned long rec_len;
unsigned long page_index;
const char *name = child->name;
int namelen;
namelen = child->len;
rec_len = ME2FS_DIR_REC_LEN( namelen );
for( page_index = 0 ;
page_index < getDirNumPages( dir ) ;
page_index++ )
{
char *start;
char *end;
page = ( struct page* )me2fsGetDirPageCache( dir, page_index );
if( IS_ERR( page ) )
{
ME2FS_ERROR( "<ME2FS>%s:bad page [%lu\n]", __func__, page_index );
goto not_found;
}
start = ( char* )page_address( ( const struct page* )page );
end = start
+ me2fsGetPageLastByte( dir, page_index )
- rec_len;
dent = ( struct ext2_dir_entry* )start;
while( ( char* )dent <= end )
{
unsigned long d_rec_len;
if( dent->rec_len == 0 )
{
ME2FS_ERROR( "<ME2FS>%s:zero-length directoryentry\n", __func__ );
me2fsPutDirPageCache( page );
goto not_found;
}
if( me2fsStrncmp( namelen, name, dent ) )
{
goto found;
}
/* ---------------------------------------------------------------- */
/* go to next entry */
/* ---------------------------------------------------------------- */
d_rec_len = le16_to_cpu( dent->rec_len );
dent = ( struct ext2_dir_entry* )( ( char* )dent + d_rec_len );
}
me2fsPutDirPageCache( page );
}
not_found:
return( NULL );
found:
*res_page = page;
return( dent );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsMakeEmpty
Input :struct inode *inode
< vfs inode >
struct inode *parent
< parent inode >
Output :void
Return :int
< result >
Description :make empty entry
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsMakeEmpty( struct inode *inode, struct inode *parent )
{
struct page *page;
struct ext2_dir_entry *dent;
unsigned long block_size;
int err;
void *start;
/* -------------------------------------------------------------------------*/
/* find or create a page for index 0 */
/* -------------------------------------------------------------------------*/
if( !( page = grab_cache_page( inode->i_mapping, 0 ) ) )
{
return( -ENOMEM );
}
/* get block size in file system */
block_size = inode->i_sb->s_blocksize;
if( ( err = prepareWriteBlock( page, 0, block_size ) ) )
{
/* failed to prepare */
unlock_page( page );
page_cache_release( page );
return( err );
}
start = kmap_atomic( page );
memset( start, 0, block_size );
/* -------------------------------------------------------------------------*/
/* make dot */
/* -------------------------------------------------------------------------*/
dent = ( struct ext2_dir_entry* )start;
dent->name_len = 1;
dent->rec_len = cpu_to_le16( ME2FS_DIR_REC_LEN( dent->name_len ) );
memcpy( dent->name, ".\0\0", 4 );
dent->inode = cpu_to_le32( inode->i_ino );
setDirEntryType( dent, inode );
/* -------------------------------------------------------------------------*/
/* make dot dot */
/* -------------------------------------------------------------------------*/
dent = ( struct ext2_dir_entry* )( start + ME2FS_DIR_REC_LEN( 1 ) );
dent->name_len = 2;
dent->rec_len = cpu_to_le16( block_size - ME2FS_DIR_REC_LEN( 1 ) );
dent->inode = cpu_to_le32( parent->i_ino );
memcpy( dent->name, "..\0", 4 );
setDirEntryType( dent, inode );
kunmap_atomic( start );
/* -------------------------------------------------------------------------*/
/* commit write block of empty contents */
/* -------------------------------------------------------------------------*/
err = commitBlockWrite( page, 0, block_size );
page_cache_release( page );
return( err );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsAddLink
Input :struct dentry *dentry
< vfs dentry >
struct inode *inode
< vfs inode >
Output :void
Return :void
Description :add link
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsAddLink( struct dentry *dentry, struct inode *inode )
{
struct inode *dir;
struct page *page;
struct ext2_dir_entry *dent;
const char *link_name = dentry->d_name.name;
int link_name_len;
unsigned long block_size;
unsigned long link_rec_len;
unsigned short rec_len;
unsigned short name_len;
unsigned long page_index;
loff_t pos;
int err;
dir = dentry->d_parent->d_inode;
link_name_len = dentry->d_name.len;
link_rec_len = ME2FS_DIR_REC_LEN( link_name_len );
block_size = dir->i_sb->s_blocksize;
/* ------------------------------------------------------------------------ */
/* find entry space in the directory */
/* ------------------------------------------------------------------------ */
for( page_index = 0 ;
page_index <= getDirNumPages( dir ) ;
page_index++ )
{
char *start;
char *end;
char *dir_end;
page = ( struct page* )me2fsGetDirPageCache( dir, page_index );
err = PTR_ERR( page );
if( IS_ERR( page ) )
{
ME2FS_ERROR( "<ME2FS>%s:bad page [%lu\n]", __func__, page_index );
return( err );
}
lock_page( page );
start = ( char* )page_address( ( const struct page* )page );
dir_end = start
+ me2fsGetPageLastByte( dir, page_index );
dent = ( struct ext2_dir_entry* )start;
end = start + PAGE_CACHE_SIZE - link_rec_len;
/* -------------------------------------------------------------------- */
/* find entry space in the page cache of the directory */
/* -------------------------------------------------------------------- */
while( ( char* )dent <= end )
{
if( ( char* )dent == dir_end )
{
/* ------------------------------------------------------------ */
/* reach i_size */
/* ------------------------------------------------------------ */
name_len = 0;
rec_len = block_size;
dent->rec_len = cpu_to_le16( rec_len );
dent->inode = 0;
goto got_it;
}
/* ---------------------------------------------------------------- */
/* invalid entry */
/* ---------------------------------------------------------------- */
if( !dent->rec_len )
{
ME2FS_ERROR( "<ME2FS>%s:zero-length directory entry\n", __func__ );
err = -EIO;
goto out_unlock;
}
/* ---------------------------------------------------------------- */
/* the entry already exists */
/* ---------------------------------------------------------------- */
if( me2fsStrncmp( link_name_len, link_name, dent ) )
{
err = -EEXIST;
goto out_unlock;
}
name_len = ME2FS_DIR_REC_LEN( dent->name_len );
rec_len = le16_to_cpu( dent->rec_len );
/* ---------------------------------------------------------------- */
/* found empty entry */
/* ---------------------------------------------------------------- */
if( !dent->inode && ( link_rec_len <= rec_len ) )
{
goto got_it;
}
/* ---------------------------------------------------------------- */
/* detected deteleted entry */
/* ---------------------------------------------------------------- */
if( ( name_len + link_rec_len ) <= rec_len )
{
goto got_it;
}
dent = ( struct ext2_dir_entry* )( ( char* )dent + rec_len );
}
unlock_page( page );
me2fsPutDirPageCache( page );
}
DBGPRINT( "<ME2FS>%s:failed\n", __func__ );
return( -EINVAL );
got_it:
pos = page_offset( page )
+ ( ( char* )dent - ( char* )page_address( page ) );
if( ( err = prepareWriteBlock( page, pos, rec_len ) ) )
{
goto out_unlock;
}
/* ------------------------------------------------------------------------ */
/* insert deteleted entry space */
/* ------------------------------------------------------------------------ */
if( dent->inode )
{
struct ext2_dir_entry *cur_dent;
/* here, name_len = rec_len of deleted entry */
cur_dent = ( struct ext2_dir_entry* )( ( char* )dent + name_len );
cur_dent->rec_len = cpu_to_le16( rec_len - name_len );
dent->rec_len = cpu_to_le16( name_len );
dent = cur_dent;
}
/* ------------------------------------------------------------------------ */
/* finally link the inode to parent directory */
/* ------------------------------------------------------------------------ */
dent->name_len = link_name_len;
memcpy( dent->name, link_name, link_name_len );
dent->inode = cpu_to_le32( inode->i_ino );
setDirEntryType( dent, inode );
err = commitBlockWrite( page, pos, rec_len );
dir->i_mtime = CURRENT_TIME_SEC;
dir->i_ctime = dir->i_mtime;
ME2FS_I( dir )->i_flags &= ~EXT2_BTREE_FL;
mark_inode_dirty( dir );
me2fsPutDirPageCache( page );
return( err );
out_unlock:
unlock_page( page );
me2fsPutDirPageCache( page );
return( err );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsIsEmptyDir
Input :struct inode *inode
< vfs inode >
Output :void
Return :int
< 0:false 1:true >
Description :check if the directory is empty or not
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsIsEmptyDir( struct inode *inode )
{
struct page *page;
unsigned long i;
page = NULL;
for( i = 0 ; i < getDirNumPages( inode ) ; i++ )
{
char *start;
char *end;
struct ext2_dir_entry *dent;
page = me2fsGetDirPageCache( inode, i );
if( IS_ERR( page ) )
{
continue;
}
start = page_address( page );
dent = ( struct ext2_dir_entry* )start;
end = start + ( me2fsGetPageLastByte( inode, i )
- ME2FS_DIR_REC_LEN( 1 ) );
while( ( char* )dent <= end )
{
if( dent->rec_len == 0 )
{
ME2FS_ERROR( "<ME2FS>%s:zero-length directory entry\n",
__func__ );
}
if( dent->inode != 0 )
{
if( dent->name[ 0 ] != '.' )
{
goto not_empty;
}
if( 2 < dent->name_len )
{
goto not_empty;
}
if( dent->name_len < 2 )
{
if( dent->inode != cpu_to_le32( inode->i_ino ) )
{
goto not_empty;
}
}
else if( dent->name[ 1 ] != '.' )
{
goto not_empty;
}
/* goto next entry */
dent = ( struct ext2_dir_entry* )
( ( char* )dent + le16_to_cpu( dent->rec_len ) );
}
me2fsPutDirPageCache( page );
}
}
return( 1 );
not_empty:
me2fsPutDirPageCache( page );
return( 0 );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsDeleteDirEntry
Input :struct ext2_dir_entry *dir
< directory entry to delete >
struct page *page
< page cache the directory belongs to >
Output :void
Return :void
Description :delete a directory entry
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
int me2fsDeleteDirEntry( struct ext2_dir_entry *dir, struct page *page )
{
struct inode *inode;
char *start;
unsigned from;
unsigned to;
loff_t pos;
struct ext2_dir_entry *pde;
struct ext2_dir_entry *dent;
int err;
inode = page->mapping->host;
start = page_address( page );
from = ( ( char* )dir - start ) & ~( inode->i_sb->s_blocksize - 1 );
to = ( ( char* )dir - start ) + le16_to_cpu( dir->rec_len );
pde = NULL;
dent = ( struct ext2_dir_entry* )( start + from );
while( ( char* )dent < ( char* )dir )
{
if( dent->rec_len == 0 )
{
ME2FS_ERROR( "<ME2FS>%s:zero-length directory entry\n",
__func__ );
err = -EIO;
goto out;
}
pde = dent;
dent = ( struct ext2_dir_entry* )( ( char* )dent
+ le16_to_cpu( dent->rec_len ) );
}
if( pde )
{
from = ( char* )pde - start;
}
pos = page_offset( page ) + from;
lock_page( page );
err = prepareWriteBlock( page, pos, to - from );
if( pde )
{
pde->rec_len = le16_to_cpu( to - from );
}
dir->inode = 0;
err = commitBlockWrite( page, pos, to - from );
inode->i_mtime = CURRENT_TIME_SEC;
inode->i_ctime = inode->i_mtime;
ME2FS_I( inode )->i_flags &= ~EXT2_BTREE_FL;
mark_inode_dirty( inode );
out:
me2fsPutDirPageCache( page );
return( err );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsGetDotDotEntry
Input :struct inode *dir
< vfs inode of directory >
struct page **p
< page to which dot dot entry belongs to >
Output :struct page **p
< page to which dot dot entry belongs to >
Return :struct ext2_dir_entry*
< dot dot entry >
Description :get dot dot entry
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
struct ext2_dir_entry*
me2fsGetDotDotEntry( struct inode *dir, struct page **p )
{
struct page *page;
struct ext2_dir_entry *dent;
page = me2fsGetDirPageCache( dir, 0 );
dent = NULL;
if( !IS_ERR( page ) )
{
struct ext2_dir_entry *cur_dent;
cur_dent = ( struct ext2_dir_entry* )page_address( page );
dent = ( struct ext2_dir_entry* )( ( char* )cur_dent
+ le16_to_cpu( cur_dent->rec_len ) );
*p = page;
}
return( dent );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsSetLink
Input :struct inode *dir
< vfs inode >
struct ext2_dir_entry *dent
< directory entry to set link >
struct page *page
< directory page cache >
struct inode *inode
< vfs inode to be linked >
int update_times
< flag to update times >
Output :void
Return :void
Description :set link
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
void me2fsSetLink( struct inode *dir,
struct ext2_dir_entry *dent,
struct page *page,
struct inode *inode,
int update_times )
{
loff_t pos;
unsigned len;
int err;
pos = page_offset( page ) + ( ( char* )dent - ( char* )page_address( page ) );
len = le16_to_cpu( dent->rec_len );
lock_page( page );
err = prepareWriteBlock( page, pos, len );
dent->inode = cpu_to_le32( inode->i_ino );
setDirEntryType( dent, inode );
err = commitBlockWrite( page, pos, len );
me2fsPutDirPageCache( page );
if( update_times )
{
dir->i_mtime = CURRENT_TIME_SEC;
dir->i_ctime = dir->i_mtime;
}
ME2FS_I( dir )->i_flags &= ~EXT2_BTREE_FL;
mark_inode_dirty( dir );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :void
Input :void
Output :void
Return :void
Description :void
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Local Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
---------------------------------------------------------------------------------
File Operations for Directory
---------------------------------------------------------------------------------
*/
/*
==================================================================================
Function :me2fsReadDir
Input :struct file *file
< vfs file object >
struct dir_context *ctx
< context of reading directory >
Output :void
Return :void
Description :void
==================================================================================
*/
static int me2fsReadDir( struct file *file, struct dir_context *ctx )
{
struct super_block *sb;
struct inode *inode;
unsigned long offset;
unsigned long page_index;
unsigned char ftype_table[ EXT2_FT_MAX ] =
{
[ EXT2_FT_UNKNOWN ] = DT_UNKNOWN,
[ EXT2_FT_REG_FILE ] = DT_REG,
[ EXT2_FT_DIR ] = DT_DIR,
[ EXT2_FT_CHRDEV ] = DT_CHR,
[ EXT2_FT_BLKDEV ] = DT_BLK,
[ EXT2_FT_FIFO ] = DT_FIFO,
[ EXT2_FT_SOCK ] = DT_SOCK,
[ EXT2_FT_SYMLINK ] = DT_LNK,
};
inode = file_inode( file );
/* ------------------------------------------------------------------------ */
/* whether position exceeds last of mininum dir entry or not */
/* ------------------------------------------------------------------------ */
if( ( inode->i_size - ( 1 + 8 + 3 ) ) < ctx->pos )
{
/* there is no more entry in the directory */
return( 0 );
}
sb = inode->i_sb;
offset = ctx->pos & ~PAGE_CACHE_MASK;
for( page_index = ctx->pos >> PAGE_CACHE_SHIFT ;
page_index < getDirNumPages( inode ) ;
page_index++ )
{
struct page *page;
struct ext2_dir_entry *dent;
char *start;
char *end;
page = ( struct page* )me2fsGetDirPageCache( inode, page_index );
if( IS_ERR( page ) )
{
ME2FS_ERROR( "<ME2FS>bad page in %lu\n", inode->i_ino );
ctx->pos += PAGE_CACHE_SIZE - offset;
return( PTR_ERR( page ) );
}
start = ( char* )page_address( ( const struct page* )page );
end = start
+ me2fsGetPageLastByte( inode, page_index )
- ( 1 + 8 + 3 );
dent = ( struct ext2_dir_entry* )( start + offset );
while( ( char* )dent <= end )
{
unsigned long rec_len;
unsigned char ftype_indx;
if( !dent->rec_len )
{
ME2FS_ERROR( "<ME2FS>error:zero-length directory entry\n" );
me2fsPutDirPageCache( page );
return( -EIO );
}
if( dent->inode )
{
if( EXT2_FT_MAX < dent->file_type )
{
ftype_indx = DT_UNKNOWN;
}
else
{
ftype_indx = dent->file_type;
}
if( !( dir_emit( ctx, dent->name, dent->name_len,
le32_to_cpu( dent->inode ),
ftype_table[ ftype_indx ] ) ) )
{
break;
}
}
/* ---------------------------------------------------------------- */
/* go to next entry */
/* ---------------------------------------------------------------- */
rec_len = le16_to_cpu( dent->rec_len );
ctx->pos+= rec_len;
dent = ( struct ext2_dir_entry* )( ( char* )dent + rec_len );
}
me2fsPutDirPageCache( page );
}
return( 0 );
}
/*
---------------------------------------------------------------------------------
Helper Functions for Directory
---------------------------------------------------------------------------------
*/
/*
==================================================================================
Function :getDirNumPages
Input :struct inode *inode
< vfs inode of a directory >
Output :void
Return :unsigned long
< number of pages to be cached for directory >
Description :get number of pages to be cached for directory
==================================================================================
*/
static inline unsigned long getDirNumPages( struct inode *inode )
{
return( ( inode->i_size + PAGE_CACHE_SIZE - 1 ) >> PAGE_CACHE_SHIFT );
}
/*
==================================================================================
Function :me2fsGetDirPageCache
Input :struct inode *inode
< vfs inode of directory >
unsigned long index
< index of page cache >
Output :void
Return :struct page*
< page to which logical block attributes >
Description :get page cache of the directory
==================================================================================
*/
static struct page*
me2fsGetDirPageCache( struct inode *inode, unsigned long index )
{
struct page *page;
/* ------------------------------------------------------------------------ */
/* read blocks from device and map them */
/* ------------------------------------------------------------------------ */
/* find get a page from mapping if there is not allocated then alloc to it */
/* and fill it. if there is cached page then return it */
page = read_mapping_page( inode->i_mapping, index, NULL );
if( !IS_ERR( page ) )
{
kmap( page );
/* Actually linux ext2 module verifies page at here */
if( PageError( page ) )
{
me2fsPutDirPageCache( page );
return( ERR_PTR( -EIO ) );
}
}
return( page );
}
/*
==================================================================================
Function :me2fsPutDirPageCache
Input :struct page *page
< page to put >
Output :void
Return :void
Description :put page
==================================================================================
*/
static inline void me2fsPutDirPageCache( struct page *page )
{
kunmap( page );
page_cache_release( page );
}
/*
==================================================================================
Function :me2fsGetPageLastByte
Input :struct inode *inode
< vfs inode >
unsigned long page_nr
< page number >
Output :void
Return :unsigned long
< last byte in that page >
Description :return the offset int page `page_nr' of the last valid
byte in that page, plus one.
==================================================================================
*/
static unsigned long
me2fsGetPageLastByte( struct inode *inode, unsigned long page_nr )
{
unsigned long last_byte;
last_byte = inode->i_size - ( page_nr << PAGE_CACHE_SHIFT );
if( last_byte > PAGE_CACHE_SIZE )
{
return( PAGE_CACHE_SIZE );
}
return( last_byte );
}
/*
==================================================================================
Function :me2fsStrncmp
Input :int len
< length of name >
const char* const name
< name of comparison >
struct ext2_dir_entry *dent
< directory entry to be compared >
Output :void
Return :void
Description :compare name and name of direcotry entry
< 0 : mismatch, 1 : match >
==================================================================================
*/
static inline int
me2fsStrncmp( int len, const char* const name, struct ext2_dir_entry *dent )
{
if( len != dent->name_len )
{
return( 0 );
}
if( !dent->inode )
{
return( 0 );
}
return( !memcmp( name, dent->name, len ) );
}
/*