This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chris_ospfsmod.c
1742 lines (1444 loc) · 52 KB
/
chris_ospfsmod.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
#include <linux/autoconf.h>
#include <linux/version.h>
#ifndef EXPORT_SYMTAB
# define EXPORT_SYMTAB
#endif
#include <linux/module.h>
#include <linux/moduleparam.h>
#include "ospfs.h"
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/sched.h>
/****************************************************************************
* ospfsmod
*
* This is the OSPFS module! It contains both library code for your use,
* and exercises where you must add code.
*
****************************************************************************/
/* Define eprintk() to be a version of printk(), which prints messages to
* the console.
* (If working on a real Linux machine, change KERN_NOTICE to KERN_ALERT or
* KERN_EMERG so that you are sure to see the messages. By default, the
* kernel does not print all messages to the console. Levels like KERN_ALERT
* and KERN_EMERG will make sure that you will see messages.) */
#define eprintk(format, ...) printk(KERN_NOTICE format, ## __VA_ARGS__)
// The actual disk data is just an array of raw memory.
// The initial array is defined in fsimg.c, based on your 'base' directory.
extern uint8_t ospfs_data[];
extern uint32_t ospfs_length;
// A pointer to the superblock; see ospfs.h for details on the struct.
static ospfs_super_t * const ospfs_super =
(ospfs_super_t *) &ospfs_data[OSPFS_BLKSIZE];
static int change_size(ospfs_inode_t *oi, uint32_t want_size);
static ospfs_direntry_t *find_direntry(ospfs_inode_t *dir_oi, const char *name, int namelen);
/*****************************************************************************
* FILE SYSTEM OPERATIONS STRUCTURES
*
* Linux filesystems are based around three interrelated structures.
*
* These are:
*
* 1. THE LINUX SUPERBLOCK. This structure represents the whole file system.
* Example members include the root directory and the number of blocks
* on the disk.
* 2. LINUX INODES. Each file and directory in the file system corresponds
* to an inode. Inode operations include "mkdir" and "create" (add to
* directory).
* 3. LINUX FILES. Corresponds to an open file or directory. Operations
* include "read", "write", and "readdir".
*
* When Linux wants to perform some file system operation,
* it calls a function pointer provided by the file system type.
* (Thus, Linux file systems are object oriented!)
*
* These function pointers are grouped into structures called "operations"
* structures.
*
* The initial portion of the file declares all the operations structures we
* need to support ospfsmod: one for the superblock, several for different
* kinds of inodes and files. There are separate inode_operations and
* file_operations structures for OSPFS directories and for regular OSPFS
* files. The structures are actually defined near the bottom of this file.
*/
// Basic file system type structure
// (links into Linux's list of file systems it supports)
static struct file_system_type ospfs_fs_type;
// Inode and file operations for regular files
static struct inode_operations ospfs_reg_inode_ops;
static struct file_operations ospfs_reg_file_ops;
// Inode and file operations for directories
static struct inode_operations ospfs_dir_inode_ops;
static struct file_operations ospfs_dir_file_ops;
// Inode operations for symbolic links
static struct inode_operations ospfs_symlink_inode_ops;
// Other required operations
static struct dentry_operations ospfs_dentry_ops;
static struct super_operations ospfs_superblock_ops;
/*****************************************************************************
* BITVECTOR OPERATIONS
*
* OSPFS uses a free bitmap to keep track of free blocks.
* These bitvector operations, which set, clear, and test individual bits
* in a bitmap, may be useful.
*/
// bitvector_set -- Set 'i'th bit of 'vector' to 1.
static inline void
bitvector_set(void *vector, int i)
{
((uint32_t *) vector) [i / 32] |= (1 << (i % 32));
}
// bitvector_clear -- Set 'i'th bit of 'vector' to 0.
static inline void
bitvector_clear(void *vector, int i)
{
((uint32_t *) vector) [i / 32] &= ~(1 << (i % 32));
}
// bitvector_test -- Return the value of the 'i'th bit of 'vector'.
static inline int
bitvector_test(const void *vector, int i)
{
return (((const uint32_t *) vector) [i / 32] & (1 << (i % 32))) != 0;
}
/*****************************************************************************
* OSPFS HELPER FUNCTIONS
*/
// ospfs_size2nblocks(size)
// Returns the number of blocks required to hold 'size' bytes of data.
//
// Input: size -- file size
// Returns: a number of blocks
uint32_t
ospfs_size2nblocks(uint32_t size)
{
return (size + OSPFS_BLKSIZE - 1) / OSPFS_BLKSIZE;
}
// ospfs_block(blockno)
// Use this function to load a block's contents from "disk".
//
// Input: blockno -- block number
// Returns: a pointer to that block's data
static void *
ospfs_block(uint32_t blockno)
{
return &ospfs_data[blockno * OSPFS_BLKSIZE];
}
// ospfs_inode(ino)
// Use this function to load a 'ospfs_inode' structure from "disk".
//
// Input: ino -- inode number
// Returns: a pointer to the corresponding ospfs_inode structure
static inline ospfs_inode_t *
ospfs_inode(ino_t ino)
{
ospfs_inode_t *oi;
if (ino >= ospfs_super->os_ninodes)
return 0;
oi = ospfs_block(ospfs_super->os_firstinob);
return &oi[ino];
}
// ospfs_inode_blockno(oi, offset)
// Use this function to look up the blocks that are part of a file's
// contents.
//
// Inputs: oi -- pointer to a OSPFS inode
// offset -- byte offset into that inode
// Returns: the block number of the block that contains the 'offset'th byte
// of the file
static inline uint32_t
ospfs_inode_blockno(ospfs_inode_t *oi, uint32_t offset)
{
uint32_t blockno = offset / OSPFS_BLKSIZE;
if (offset >= oi->oi_size || oi->oi_ftype == OSPFS_FTYPE_SYMLINK)
return 0;
else if (blockno >= OSPFS_NDIRECT + OSPFS_NINDIRECT) {
uint32_t blockoff = blockno - (OSPFS_NDIRECT + OSPFS_NINDIRECT);
uint32_t *indirect2_block = ospfs_block(oi->oi_indirect2);
uint32_t *indirect_block = ospfs_block(indirect2_block[blockoff / OSPFS_NINDIRECT]);
return indirect_block[blockoff % OSPFS_NINDIRECT];
} else if (blockno >= OSPFS_NDIRECT) {
uint32_t *indirect_block = ospfs_block(oi->oi_indirect);
return indirect_block[blockno - OSPFS_NDIRECT];
} else
return oi->oi_direct[blockno];
}
// ospfs_inode_data(oi, offset)
// Use this function to load part of inode's data from "disk",
// where 'offset' is relative to the first byte of inode data.
//
// Inputs: oi -- pointer to a OSPFS inode
// offset -- byte offset into 'oi's data contents
// Returns: a pointer to the 'offset'th byte of 'oi's data contents
//
// Be careful: the returned pointer is only valid within a single block.
// This function is a simple combination of 'ospfs_inode_blockno'
// and 'ospfs_block'.
static inline void *
ospfs_inode_data(ospfs_inode_t *oi, uint32_t offset)
{
uint32_t blockno = ospfs_inode_blockno(oi, offset);
return (uint8_t *) ospfs_block(blockno) + (offset % OSPFS_BLKSIZE);
}
/*****************************************************************************
* LOW-LEVEL FILE SYSTEM FUNCTIONS
* There are no exercises in this section, and you don't need to understand
* the code.
*/
// ospfs_mk_linux_inode(sb, ino)
// Linux's in-memory 'struct inode' structure represents disk
// objects (files and directories). Many file systems have their own
// notion of inodes on disk, and for such file systems, Linux's
// 'struct inode's are like a cache of on-disk inodes.
//
// This function takes an inode number for the OSPFS and constructs
// and returns the corresponding Linux 'struct inode'.
//
// Inputs: sb -- the relevant Linux super_block structure (one per mount)
// ino -- OSPFS inode number
// Returns: 'struct inode'
static struct inode *
ospfs_mk_linux_inode(struct super_block *sb, ino_t ino)
{
ospfs_inode_t *oi = ospfs_inode(ino);
struct inode *inode;
if (!oi)
return 0;
if (!(inode = new_inode(sb)))
return 0;
inode->i_ino = ino;
// Make it look like everything was created by root.
inode->i_uid = inode->i_gid = 0;
inode->i_size = oi->oi_size;
if (oi->oi_ftype == OSPFS_FTYPE_REG) {
// Make an inode for a regular file.
inode->i_mode = oi->oi_mode | S_IFREG;
inode->i_op = &ospfs_reg_inode_ops;
inode->i_fop = &ospfs_reg_file_ops;
inode->i_nlink = oi->oi_nlink;
} else if (oi->oi_ftype == OSPFS_FTYPE_DIR) {
// Make an inode for a directory.
inode->i_mode = oi->oi_mode | S_IFDIR;
inode->i_op = &ospfs_dir_inode_ops;
inode->i_fop = &ospfs_dir_file_ops;
inode->i_nlink = oi->oi_nlink + 1 /* dot-dot */;
} else if (oi->oi_ftype == OSPFS_FTYPE_SYMLINK) {
// Make an inode for a symbolic link.
inode->i_mode = S_IRUSR | S_IRGRP | S_IROTH
| S_IWUSR | S_IWGRP | S_IWOTH
| S_IXUSR | S_IXGRP | S_IXOTH | S_IFLNK;
inode->i_op = &ospfs_symlink_inode_ops;
inode->i_nlink = oi->oi_nlink;
} else
panic("OSPFS: unknown inode type!");
// Access and modification times are now.
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
return inode;
}
// ospfs_fill_super, ospfs_get_sb
// These functions are called by Linux when the user mounts a version of
// the OSPFS onto some directory. They help construct a Linux
// 'struct super_block' for that file system.
static int
ospfs_fill_super(struct super_block *sb, void *data, int flags)
{
struct inode *root_inode;
sb->s_blocksize = OSPFS_BLKSIZE;
sb->s_blocksize_bits = OSPFS_BLKSIZE_BITS;
sb->s_magic = OSPFS_MAGIC;
sb->s_op = &ospfs_superblock_ops;
if (!(root_inode = ospfs_mk_linux_inode(sb, OSPFS_ROOT_INO))
|| !(sb->s_root = d_alloc_root(root_inode))) {
iput(root_inode);
sb->s_dev = 0;
return -ENOMEM;
}
return 0;
}
static int
ospfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data, struct vfsmount *mount)
{
return get_sb_single(fs_type, flags, data, ospfs_fill_super, mount);
}
// ospfs_delete_dentry
// Another bookkeeping function.
static int
ospfs_delete_dentry(struct dentry *dentry)
{
return 1;
}
/*****************************************************************************
* DIRECTORY OPERATIONS
*
* EXERCISE: Finish 'ospfs_dir_readdir' and 'ospfs_symlink'.
*/
// ospfs_dir_lookup(dir, dentry, ignore)
// This function implements the "lookup" directory operation, which
// looks up a named entry.
//
// We have written this function for you.
//
// Input: dir -- The Linux 'struct inode' for the directory.
// You can extract the corresponding 'ospfs_inode_t'
// by calling 'ospfs_inode' with the relevant inode number.
// dentry -- The name of the entry being looked up.
// Effect: Looks up the entry named 'dentry'. If found, attaches the
// entry's 'struct inode' to the 'dentry'. If not found, returns
// a "negative dentry", which has no inode attachment.
static struct dentry *
ospfs_dir_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *ignore)
{
// Find the OSPFS inode corresponding to 'dir'
ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
struct inode *entry_inode = NULL;
int entry_off;
// Make sure filename is not too long
if (dentry->d_name.len > OSPFS_MAXNAMELEN)
return (struct dentry *) ERR_PTR(-ENAMETOOLONG);
// Mark with our operations
dentry->d_op = &ospfs_dentry_ops;
// Search through the directory block
for (entry_off = 0; entry_off < dir_oi->oi_size;
entry_off += OSPFS_DIRENTRY_SIZE) {
// Find the OSPFS inode for the entry
ospfs_direntry_t *od = ospfs_inode_data(dir_oi, entry_off);
// Set 'entry_inode' if we find the file we are looking for
if (od->od_ino > 0
&& strlen(od->od_name) == dentry->d_name.len
&& memcmp(od->od_name, dentry->d_name.name, dentry->d_name.len) == 0) {
entry_inode = ospfs_mk_linux_inode(dir->i_sb, od->od_ino);
if (!entry_inode)
return (struct dentry *) ERR_PTR(-EINVAL);
break;
}
}
// We return a dentry whether or not the file existed.
// The file exists if and only if 'entry_inode != NULL'.
// If the file doesn't exist, the dentry is called a "negative dentry".
// d_splice_alias() attaches the inode to the dentry.
// If it returns a new dentry, we need to set its operations.
if ((dentry = d_splice_alias(entry_inode, dentry)))
dentry->d_op = &ospfs_dentry_ops;
return dentry;
}
// ospfs_dir_readdir(filp, dirent, filldir)
// This function is called when the kernel reads the contents of a directory
// (i.e. when file_operations.readdir is called for the inode).
//
// Inputs: filp -- The 'struct file' structure correspoding to
// the open directory.
// The most important member is 'filp->f_pos', the
// File POSition. This remembers how far into the
// directory we are, so if the user calls 'readdir'
// twice, we don't forget our position.
// This function must update 'filp->f_pos'.
// dirent -- Used to pass to 'filldir'.
// filldir -- A pointer to a callback function.
// This function should call 'filldir' once for each
// directory entry, passing it six arguments:
// (1) 'dirent'.
// (2) The directory entry's name.
// (3) The length of the directory entry's name.
// (4) The 'f_pos' value corresponding to the directory entry.
// (5) The directory entry's inode number.
// (6) DT_REG, for regular files; DT_DIR, for subdirectories;
// or DT_LNK, for symbolic links.
// This function should stop returning directory
// entries either when the directory is complete, or
// when 'filldir' returns < 0, whichever comes first.
//
// Returns: 1 at end of directory, 0 if filldir returns < 0 before the end
// of the directory, and -(error number) on error.
//
// EXERCISE: Finish implementing this function.
static int
ospfs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
struct inode *dir_inode = filp->f_dentry->d_inode;
ospfs_inode_t *dir_oi = ospfs_inode(dir_inode->i_ino);
uint32_t f_pos = filp->f_pos;
int r = 0; /* Error return value, if any */
int ok_so_far = 0; /* Return value from 'filldir' */
// f_pos is an offset into the directory's data, plus two.
// The "plus two" is to account for "." and "..".
if (r == 0 && f_pos == 0) {
ok_so_far = filldir(dirent, ".", 1, f_pos, dir_inode->i_ino, DT_DIR);
if (ok_so_far >= 0)
f_pos++;
}
if (r == 0 && ok_so_far >= 0 && f_pos == 1) {
ok_so_far = filldir(dirent, "..", 2, f_pos, filp->f_dentry->d_parent->d_inode->i_ino, DT_DIR);
if (ok_so_far >= 0)
f_pos++;
}
// actual entries
while (r == 0 && ok_so_far >= 0 && f_pos >= 2)
{
ospfs_direntry_t *od;
ospfs_inode_t *entry_oi;
/* If at the end of the directory, set 'r' to 1 and exit
* the loop. For now we do this all the time. */
if (f_pos > (dir_oi->oi_size * OSPFS_DIRENTRY_SIZE))
{
r = 1;
break;
}
/* Get a pointer to the next entry (od) in the directory.
* The file system interprets the contents of a
* directory-file as a sequence of ospfs_direntry structures.
* You will find 'f_pos' and 'ospfs_inode_data' useful.
*
* Then use the fields of that file to fill in the directory
* entry. To figure out whether a file is a regular file or
* another directory, use 'ospfs_inode' to get the directory
* entry's corresponding inode, and check out its 'oi_ftype'
* member.
*
* Make sure you ignore blank directory entries! (Which have
* an inode number of 0.)
*
* If the current entry is successfully read (the call to
* filldir returns >= 0), or the current entry is skipped,
* your function should advance f_pos by the proper amount to
* advance to the next directory entry. */
od = ospfs_inode_data(dir_oi, f_pos * OSPFS_DIRENTRY_SIZE);
entry_oi = ospfs_inode(od->od_ino);
if (entry_oi != 0)
{
switch (entry_oi->oi_ftype) {
case OSPFS_FTYPE_REG:
{
ok_so_far = filldir(dirent, od->od_name, strlen(od->od_name), f_pos, od->od_ino, DT_REG);
break;
}
case OSPFS_FTYPE_DIR:
{
ok_so_far = filldir(dirent, od->od_name, strlen(od->od_name), f_pos, od->od_ino, DT_DIR);
break;
}
case OSPFS_FTYPE_SYMLINK:
{
ok_so_far = filldir(dirent, od->od_name, strlen(od->od_name), f_pos, od->od_ino, DT_LNK);
break;
}
default:
{
ok_so_far = -1;
continue;
}
}
}
f_pos++;
}
// Save the file position and return!
filp->f_pos = f_pos;
return r;
}
// ospfs_unlink(dirino, dentry)
// This function is called to remove a file.
//
// Inputs: dirino -- You may ignore this.
// dentry -- The 'struct dentry' structure, which contains the inode
// the directory entry points to and the directory entry's
// directory.
//
// Returns: 0 if success and -ENOENT on entry not found.
//
// EXERCISE: Make sure that deleting symbolic links works correctly.
static int
ospfs_unlink(struct inode *dirino, struct dentry *dentry)
{
ospfs_inode_t *oi = ospfs_inode(dentry->d_inode->i_ino);
ospfs_inode_t *dir_oi = ospfs_inode(dentry->d_parent->d_inode->i_ino);
int entry_off;
ospfs_direntry_t *od;
od = NULL; // silence compiler warning; entry_off indicates when !od
for (entry_off = 0; entry_off < dir_oi->oi_size;
entry_off += OSPFS_DIRENTRY_SIZE) {
od = ospfs_inode_data(dir_oi, entry_off);
if (od->od_ino > 0
&& strlen(od->od_name) == dentry->d_name.len
&& memcmp(od->od_name, dentry->d_name.name, dentry->d_name.len) == 0)
break;
}
if (entry_off == dir_oi->oi_size) {
printk("<1>ospfs_unlink should not fail!\n");
return -ENOENT;
}
od->od_ino = 0;
oi->oi_nlink--;
return 0;
}
/*****************************************************************************
* FREE-BLOCK BITMAP OPERATIONS
*
* EXERCISE: Implement these functions.
*/
// allocate_block()
// Use this function to allocate a block.
//
// Inputs: none
// Returns: block number of the allocated block,
// or 0 if the disk is full
//
// This function searches the free-block bitmap, which starts at Block 2, for
// a free block, allocates it (by marking it non-free), and returns the block
// number to the caller. The block itself is not touched.
//
// Note: A value of 0 for a bit indicates the corresponding block is
// allocated; a value of 1 indicates the corresponding block is free.
//
// You can use the functions bitvector_set(), bitvector_clear(), and
// bitvector_test() to do bit operations on the map.
// COMPLETE
static uint32_t
allocate_block(void)
{
void *bitmap = ospfs_block(OSPFS_FREEMAP_BLK);
uint32_t i;
for (i = 2; i < ospfs_super->os_nblocks; i++)
{
if (bitvector_test(bitmap, i))
{
bitvector_clear(bitmap, i);
return i;
}
}
//returns 0 if no free block is found.
return 0;
}
// free_block(blockno)
// Use this function to free an allocated block.
//
// Inputs: blockno -- the block number to be freed
// Returns: none
//
// This function should mark the named block as free in the free-block
// bitmap. (You might want to program defensively and make sure the block
// number isn't obviously bogus: the boot sector, superblock, free-block
// bitmap, and inode blocks must never be freed. But this is not required.)
// COMPLETE
static void
free_block(uint32_t blockno)
{
uint32_t lowest_block = ospfs_super->os_firstinob + ospfs_super->os_ninodes;
if (blockno < lowest_block || blockno >= ospfs_super->os_nblocks)
{
return;
}
else
{
void *bitmap = ospfs_block(OSPFS_FREEMAP_BLK);
bitvector_set(bitmap, blockno);
return;
}
}
/*****************************************************************************
* FILE OPERATIONS
*
* EXERCISE: Finish off change_size, read, and write.
*
* The find_*, add_block, and remove_block functions are only there to support
* the change_size function. If you prefer to code change_size a different
* way, then you may not need these functions.
*
*/
// The following functions are used in our code to unpack a block number into
// its consituent pieces: the doubly indirect block number (if any), the
// indirect block number (which might be one of many in the doubly indirect
// block), and the direct block number (which might be one of many in an
// indirect block). We use these functions in our implementation of
// change_size.
//int32_t indir2_index(uint32_t b)
// Returns the doubly-indirect block index for file block b.
//
//Inputs: b -- the zero-based index of the file block (e.g., 0 for the first
// block, 1 for the second, etc.)
//Returns: 0 if block index 'b' requires using the doubly indirect
// block, -1 if it does not.
//EXERCISE: COMPLETE
static int32_t
indir2_index(uint32_t b)
{
if (b >= OSPFS_NDIRECT + OSPFS_NINDIRECT)
return 0;
else
return -1;
}
// int32_t indir_index(uint32_t b)
// Returns the indirect block index for file block b.
//
// Inputs: b -- the zero-based index of the file block
// Returns: -1 if b is one of the file's direct blocks;
// 0 if b is located under the file's first indirect block;
// otherwise, the offset of the relevant indirect block within
// the doubly indirect block.
//
// EXERCISE: Fill in this function.
// COMPLETE
static int32_t
indir_index(uint32_t b)
{
if (b < OSPFS_NDIRECT)
return -1;
else if (indir2_index(b) == -1)
{
return 0;
} else
return (b - OSPFS_NDIRECT - OSPFS_NINDIRECT) / OSPFS_NINDIRECT;
}
// int32_t indir_index(uint32_t b)
// Returns the indirect block index for file block b.
//
// Inputs: b -- the zero-based index of the file block
// Returns: the index of block b in the relevant indirect block or the direct
// block array.
//
// EXERCISE: COMPLETE
static int32_t
direct_index(uint32_t b)
{
if (b < OSPFS_NDIRECT)
return b;
else if (b < OSPFS_NDIRECT + OSPFS_NINDIRECT)
return b - OSPFS_NDIRECT;
else
return (b - OSPFS_NDIRECT) % OSPFS_NINDIRECT;
}
// add_block(ospfs_inode_t *oi)
// Adds a single data block to a file, adding indirect and
// doubly-indirect blocks if necessary. (Helper function for
// change_size).
//
// Inputs: oi -- pointer to the file we want to grow
// Returns: 0 if successful, < 0 on error. Specifically:
// -ENOSPC if you are unable to allocate a block
// due to the disk being full or
// -EIO for any other error.
// If the function is successful, then oi->oi_size
// should be set to the maximum file size in bytes that could
// fit in oi's data blocks. If the function returns an error,
// then oi->oi_size should remain unchanged. Any newly
// allocated blocks should be erased (set to zero).
//
// EXERCISE: Finish off this function.
//
// Remember that allocating a new data block may require allocating
// as many as three disk blocks, depending on whether a new indirect
// block and/or a new indirect^2 block is required. If the function
// fails with -ENOSPC or -EIO, then you need to make sure that you
// free any indirect (or indirect^2) blocks you may have allocated!
//
// Also, make sure you:
// 1) zero out any new blocks that you allocate
// 2) store the disk block number of any newly allocated block
// in the appropriate place in the inode or one of the
// indirect blocks.
// 3) update the oi->oi_size field
static int
add_block(ospfs_inode_t *oi)
{
// current number of blocks in file
uint32_t n = ospfs_size2nblocks(oi->oi_size);
// keep track of allocations to free in case of -ENOSPC
uint32_t *allocated[2] = { 0, 0 };
// EXERCISE: Your code here
return -EIO; // Replace this line
}
// remove_block(ospfs_inode_t *oi)
// Removes a single data block from the end of a file, freeing
// any indirect and indirect^2 blocks that are no
// longer needed. (Helper function for change_size)
//
// Inputs: oi -- pointer to the file we want to shrink
// Returns: 0 if successful, < 0 on error.
// If the function is successful, then oi->oi_size
// should be set to the maximum file size that could
// fit in oi's blocks. If the function returns -EIO (for
// instance if an indirect block that should be there isn't),
// then oi->oi_size should remain unchanged.
//
// EXERCISE: Finish off this function.
//
// Remember that you must free any indirect and doubly-indirect blocks
// that are no longer necessary after shrinking the file. Removing a
// single data block could result in as many as 3 disk blocks being
// deallocated. Also, if you free a block, make sure that
// you set the block pointer to 0. Don't leave pointers to
// deallocated blocks laying around!
static int
remove_block(ospfs_inode_t *oi)
{
// current number of blocks in file
uint32_t n = ospfs_size2nblocks(oi->oi_size);
//EXERCISE: Your code here
return -EIO; // Replace this line
}
// change_size(oi, want_size)
// Use this function to change a file's size, allocating and freeing
// blocks as necessary.
//
// Inputs: oi -- pointer to the file whose size we're changing
// want_size -- the requested size in bytes
// Returns: 0 on success, < 0 on error. In particular:
// -ENOSPC: if there are no free blocks available
// -EIO: an I/O error -- for example an indirect block should
// exist, but doesn't
// If the function succeeds, the file's oi_size member should be
// changed to want_size, with blocks allocated as appropriate.
// Any newly-allocated blocks should be erased (set to 0).
// If there is an -ENOSPC error when growing a file,
// the file size and allocated blocks should not change from their
// original values!!!
// (However, if there is an -EIO error, do not worry too much about
// restoring the file.)
//
// If want_size has the same number of blocks as the current file, life
// is good -- the function is pretty easy. But the function might have
// to add or remove blocks.
//
// If you need to grow the file, then do so by adding one block at a time
// using the add_block function you coded above. If one of these additions
// fails with -ENOSPC, you must shrink the file back to its original size!
//
// If you need to shrink the file, remove blocks from the end of
// the file one at a time using the remove_block function you coded above.
//
// Also: Don't forget to change the size field in the metadata of the file.
// (The value that the final add_block or remove_block set it to
// is probably not correct).
//
// EXERCISE: Finish off this function.
static int
change_size(ospfs_inode_t *oi, uint32_t new_size)
{
uint32_t old_size = oi->oi_size;
int r = 0;
while (ospfs_size2nblocks(oi->oi_size) < ospfs_size2nblocks(new_size)) {
// EXERCISE: Your code here
return -EIO; // Replace this line
}
while (ospfs_size2nblocks(oi->oi_size) > ospfs_size2nblocks(new_size)) {
//EXERCISE: Your code here
return -EIO; // Replace this line
}
// EXERCISE: Make sure you update necessary file meta data
// and return the proper value.
return -EIO; // Replace this line
}
/////STOLEN CODE START///////////////
// static int
// add_block(ospfs_inode_t *oi)
// {
// // current number of blocks in file
// uint32_t n = ospfs_size2nblocks(oi->oi_size);
// // keep track of allocations to free in case of -ENOSPC
// uint32_t allocated[2] = { 0, 0 };
// // used later to store respective block nums
// uint32_t direct_block_num, indirect_block_num;
// // if we are already maxed out on space
// if(n == OSPFS_MAXFILEBLKS)
// return -ENOSPC;
// // if size2nblocks returns negative for num blocks
// else if(n < 0)
// return -EIO;
// // Direct Block Case
// else if(n < OSPFS_NDIRECT) {
// // allocate a block
// direct_block_num = allocate_block();
// // if no free block was found
// if(direct_block_num == 0)
// return -ENOSPC;
// // zero out allocated block
// memset(ospfs_block(direct_block_num), 0, OSPFS_BLKSIZE);
// // add the block number to inode
// oi->oi_direct[n] = direct_block_num;
// }
// // Indirect Block Case
// else if(n < (OSPFS_NDIRECT + OSPFS_NINDIRECT) ) {
// // if no indirect block allocated already
// if(oi->oi_indirect == 0) {
// // allocate a block
// allocated[0] = allocate_block();
// // if no free block was found available
// if(allocated[0] == 0)
// return -ENOSPC;
// // zero out allocated block
// memset(ospfs_block(allocated[0]), 0, OSPFS_BLKSIZE);
// // set inode indirect block
// oi->oi_indirect = allocated[0];
// }
// // allocate a direct block
// direct_block_num = allocate_block();
// // if there was no available free block
// if(direct_block_num == 0) {
// // if we allocated a indirect block, free it
// if(allocated[0] != 0) {
// free_block(allocated[0]);
// oi->oi_indirect = 0;
// }
// return -ENOSPC;
// }
// // zero out allocated block
// memset(ospfs_block(direct_block_num), 0, OSPFS_BLKSIZE);
// // add direct block to indirect block
// ((uint32_t*) ospfs_block(oi->oi_indirect))[direct_index(n)] = direct_block_num;
// }
// // Doubly Indirect Block Case
// else if(n < OSPFS_MAXFILEBLKS) {
// // if no doubly indirect block allocated already
// if(oi->oi_indirect2 == 0) {
// // allocate a block
// allocated[0] = allocate_block();
// // if we couldn't find a free block
// if(allocated[0] == 0)
// return -ENOSPC;
// // zero out allocated block
// memset(ospfs_block(allocated[0]), 0, OSPFS_BLKSIZE);
// // set as doubly indirect block
// oi->oi_indirect2 = allocated[0];
// }
// // set indirect block to one of the entires in doubly indirect block
// indirect_block_num = ((uint32_t*) ospfs_block(oi->oi_indirect2))[indir_index(n)];
// // allocate new indirect block if not already allocated
// if(indirect_block_num == 0) {
// // allocate an indir block
// allocated[1] = allocate_block();
// // if we couldn't find a free block
// if(allocated[1] == 0) {
// // if we already allocated a block free it
// if(allocated[0] != 0)
// free_block(allocated[0]);
// return -ENOSPC;
// }
// // zero out allocated block
// memset(ospfs_block(allocated[1]), 0, OSPFS_BLKSIZE);
// // set indirect block
// indirect_block_num = allocated[1];
// }
// // allocate direct block
// direct_block_num = allocate_block();
// // if could not find free block
// if(direct_block_num == 0) {
// // if we allocated a doubly indirect block, free it
// if(allocated[0] != 0) {
// free_block(allocated[0]);
// oi->oi_indirect2 = 0;
// }
// // if we allocated a indirect block, free it
// if(allocated[1] != 0){
// free_block(allocated[1]);
// }
// return -ENOSPC;
// }
// // zero out allocated block
// memset(ospfs_block(direct_block_num), 0, OSPFS_BLKSIZE);
// // set direct block to one of the entires of indirect block
// ((uint32_t*) ospfs_block(indirect_block_num))[direct_index(n)] = direct_block_num;
// }
// // Block Num out of Range
// else
// return -ENOSPC;