· 6 years ago · Nov 07, 2019, 05:32 AM
1// Interrupt - 4 -s has Create and Delete system calls
2
3////////////////////////////////////////////////////////////////////////////////
4// Both calls search inode table so it's better to combine
5
6[PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 13] = SP;
7
8alias userSP R1;
9userSP = SP;
10
11alias ret R2;
12SP = [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 11]*512 - 1;
13
14alias SyscallNumber R3;
15SyscallNumber = [[PTBR + 2 * ((userSP - 5)/ 512)] * 512 + ((userSP - 5) % 512)];
16
17////////////////// file name (string) is arg1 for both calls ---------------------
18
19alias filename R4;
20filename = [[PTBR + 2 * ((userSP - 4)/ 512)] * 512 + ((userSP - 4) % 512)];
21
22////////////////// finding file in inode table -----------------------------------
23
24alias fileindex R5;
25alias flag R6;
26alias freeentry R7;
27
28flag = 0; // not found flag
29fileindex = 0; // inode table (page 59) INODE_TABLE
30freeentry = -1;
31
32while (fileindex < MAX_FILE_NUM) do
33 if ([INODE_TABLE + fileindex * 16 + 1] == -1 && freeentry == -1) then // get a free entry in the inode table
34 freeentry = fileindex;
35 endif;
36 if ([INODE_TABLE + fileindex * 16 + 1] == filename) then // found the file
37 flag = 1;
38 // break; // TODO - this could be an error
39 endif;
40 fileindex = fileindex + 1;
41endwhile;
42
43// Return address ----------------------------------------------------------------
44//////////////// retAdd = [PTBR + 2 * ((userSP - 1) / 512)]*512 + ((userSP - 1) % 512);
45
46////////////////////////////////////////////////////////////////////////////////
47// Create System call
48// The Create operation takes as input a filename.
49// If the file already exists, then the system call returns 0 (success).
50// Otherwise,
51// -> creates an empty file by that name,
52// -> sets the file type to DATA, file size to 0,
53// -> userid to that of the process (from the process table) and
54// -> permission as given in the input in the Inode Table.
55// also creates a root entry for that file.
56
57// Arguments: Filename (String), Permission (0 - exclusive/1 - open-access)
58
59// Return Value:
60// 0 Success/File already exists
61// -1 No free inode table entry
62
63if (SyscallNumber == 1) then
64
65 [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 9] = 1;
66 alias permission R8;
67 permission = [[PTBR + 2 * ((userSP - 3)/ 512)] * 512 + ((userSP - 3) % 512)];
68
69 //----------------------------
70
71 if (flag == 1) then // file present in disk
72 ret = 0;
73 goto lb2;
74 endif;
75
76 if (freeentry == -1) then
77 ret = -1; // max # of files reached
78 goto lb2;
79 endif;
80
81 // a file's inode entry & root file entry have same index ----------------------
82
83 R11 = INODE_TABLE + freeentry * 16; // inode table :- 16 words/entry
84 R12 = ROOT_FILE + freeentry * 8; // root file :- 8 words/entry
85
86 // INODE TABLE ENTRIES ---------------------
87 // file type, name, size -------------------
88
89 [R11 + 0] = DATA;
90 [R11 + 1] = filename;
91 [R11 + 2] = 0;
92 [R11 + 3] = [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 3]; // user id of process
93 [R11 + 4] = permission;
94
95 // 0 blocks allocated to file --------------
96
97 [R11 + 8] = -1;
98 [R11 + 9] = -1;
99 [R11 + 10] = -1;
100 [R11 + 11] = -1;
101
102 // ROOT FILE ENTRIES ----------------------
103 // file name, size, type, username, permissn [ all entries basically ]
104
105 [R12 + 0] = filename;
106 [R12 + 1] = 0;
107 [R12 + 2] = DATA;
108 [R12 + 3] = [USER_TABLE + 2*[R11 + 3]]; // TODO - this could be an error (user name)
109 [R12 + 4] = permission;
110
111 // Success --------------------------------
112
113 ret = 0; //Return value
114 goto lb2;
115
116endif;
117
118////////////////////////////////////////////////////////////////////////////////
119// Delete System Call
120
121// Arguments: Filename (String)
122
123// Return Value:
124// 0 Success/File does not exist
125// -1 Permission denied
126// -2 File is open
127
128// The Delete operation takes as input a filename and deletes it.
129// It returns an error if
130// -> any instance of the file is open in the system or
131// -> if the file is not a DATA file.
132// Delete command fails also if the file to be deleted does not belong to the current user
133// and it has exclusive permissions.
134// Otherwise,
135// -> it deletes the root entry for the file name,
136// -> invalidates the Inode Table entry for the file,
137// -> releases the disk blocks allocated to the file and returns 0.
138
139if (SyscallNumber == 4) then //delete
140
141 // set mode flag to syscall num -----------------------
142
143 [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 9] = 4;
144
145 // if file not in inode -------------------------------
146
147 if (flag == 0) then
148 print("File not found");
149 ret = 0;
150 goto lb2;
151 endif;
152
153 //-----------------------------------------------------
154
155 R11 = INODE_TABLE + fileindex * 16;
156 R12 = ROOT_FILE + fileindex * 8;
157
158 if ([R11 + 0] != DATA) then // if not data file
159 ret = -1;
160 goto lb2;
161 endif;
162
163 //----------------------------------------------------
164 // if file exclusive,
165 // if current user != root (uid 1) AND not owner of the file, error!
166
167 if ([R11 + 4] == EXCLUSIVE) then // if exclusive
168 if ([PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 3] != 1 && [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 3] != [R11 + 3]) then //root abstract here correct it later TODO
169 ret = -1;
170 goto lb2;
171 endif;
172 endif;
173
174 //----------------------------------------------------
175 // Acquire inode
176
177 multipush(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12);
178 R1 = 4;
179 R2 = fileindex; // inode index
180 R3 = [SYSTEM_STATUS_TABLE + 1]; // pid
181 call MOD_0;
182 multipop(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12);
183
184 //---------------------------------------------------
185
186 if(R0 != -1) then // lock accquired
187
188 // if file open, cannot be deleted --------------
189
190 if ([FILE_STATUS_TABLE + fileindex * 4 + 1] != -1) then // file open now
191 print("In Use");
192 ret = -2;
193 goto lb1;
194
195 else
196 // for each disk block allocated, if in buffer & dirty, set dirty = 0
197 // call release_block to free the block
198
199 alias counter R15;
200 alias iter R13;
201 counter = 8;
202
203 while (counter < 12) do // checking data block from inode table
204
205 if ([R11 + counter] != -1) then
206 iter = 0;
207
208 // if block in buffer AND dirty bit set --------------------
209
210 while (iter < 4) do // checking the 4 buffer pages
211 if ([BUFFER_TABLE + iter * 4 + 0] == [R11 + counter] && [BUFFER_TABLE + iter * 4 + 1] == 1) then
212 [BUFFER_TABLE + iter * 4 + 1] = 0; //reset the dirty bit
213 endif;
214
215 iter = iter + 1;
216 endwhile;
217
218 //-------------------------------------------------------------
219 // free the disk block
220
221 multipush(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R15);
222 R1 = 4; // release block
223 R2 = [R11 + counter];
224 R3 = [SYSTEM_STATUS_TABLE + 1];
225 call MOD_2;
226 multipop(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R15);
227
228 endif;
229
230 [R11 + counter] = -1; // disk block freed, mark in inode table
231 counter = counter + 1;
232 endwhile;
233
234 //--------------------------------------------------------------------
235
236 [R11 + 1] = -1; // invalidate inode entry
237 [R11 + 2] = -1;
238
239 [R12 + 0] = -1; // invalidate root entry
240 [R12 + 1] = -1;
241 [R12 + 2] = -1; // TODO added later
242
243 print("Deleted");
244 ret = 0; // success
245 goto lb1;
246 endif;
247
248 lb1:
249 // Release_inode ---------------------------------------------------
250
251 multipop(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R15);
252 R1 = 5; // release inode
253 R2 = fileindex; // inode index
254 R3 = [SYSTEM_STATUS_TABLE + 1]; // pid
255 call MOD_0;
256 multipop(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R15);
257
258 //------------------------------------------------------------------
259 endif;
260endif;
261
262////////////////////////////////////////////////////////////////////////////////
263// Common :- mode flag to 0, set return value, user mode -----------------------
264
265lb2:
266 // physical add where to return val------------------------------------------
267
268 alias physicalAddrRetVal R10;
269 physicalAddrRetVal = ([PTBR + 2 * ((userSP - 1) / 512)] * 512) + ((userSP - 1) % 512);
270 [physicalAddrRetVal] = ret;
271
272 // user mode ----------------------------------------------------------------
273
274 SP = [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 13];
275 [PROCESS_TABLE + ([SYSTEM_STATUS_TABLE + 1] * 16) + 9] = 0;
276 ireturn;
277
278
279////////////////////////////////////////////////////////////////////////////////