24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

222 volume VI os16<br />

Script e sorgenti del kernel 223<br />

740078 | //<br />

740079 | return ((zno_t) -1);<br />

740080 | }<br />

740081 | }<br />

740082 | //<br />

740083 | // The zone is there: return it.<br />

740084 | //<br />

740085 | return (inode->direct[fzone]);<br />

740086 | }<br />

740087 | if (fzone < 7 + indirect_zones)<br />

740088 | {<br />

740089 | //<br />

740090 | // 7 sb);<br />

740107 | if (allocated_zone == 0)<br />

740108 | {<br />

740109 | //<br />

740110 | // Cannot allocate the zone for the indirection table:<br />

740111 | // this is an error and the ‘errno’ value is produced<br />

740112 | // by ‘zone_alloc()’.<br />

740113 | //<br />

740114 | return ((zno_t) -1);<br />

740115 | }<br />

740116 | //<br />

740117 | // The zone for the indirection table is allocated:<br />

740118 | // clear the zone and save.<br />

740119 | //<br />

740120 | memset (buffer, 0, SB_MAX_ZONE_SIZE);<br />

740121 | status = zone_write (inode->sb, allocated_zone, buffer);<br />

740122 | if (status < 0)<br />

740123 | {<br />

740124 | //<br />

740125 | // Cannot overwrite the zone. The variable ‘errno’ is<br />

740126 | // set by ‘zone_write()’.<br />

740127 | //<br />

740128 | return ((zno_t) -1);<br />

740129 | }<br />

740130 | //<br />

740131 | // The indirection table zone is allocated and cleared:<br />

740132 | // save the inode.<br />

740133 | //<br />

740134 | inode->indirect1 = allocated_zone;<br />

740135 | inode->changed = 1;<br />

740136 | status = inode_save (inode);<br />

740137 | if (status != 0)<br />

740138 | {<br />

740139 | //<br />

740140 | // Cannot save the inode. This is an error and the value<br />

740141 | // for ‘errno’ is produced by ‘inode_save()’.<br />

740142 | //<br />

740143 | return ((zno_t) -1);<br />

740144 | }<br />

740145 | }<br />

740146 | //<br />

740147 | // An indirect table is present inside the file system:<br />

740148 | // load it.<br />

740149 | //<br />

740150 | status = zone_read (inode->sb, inode->indirect1, zone_table);<br />

740151 | if (status != 0)<br />

740152 | {<br />

740153 | //<br />

740154 | // Cannot load the indirect table. This is an error and the<br />

740155 | // value for ‘errno’ is assigned by function ‘zone_read()’.<br />

740156 | //<br />

740157 | return ((zno_t) -1);<br />

740158 | }<br />

740159 | //<br />

740160 | // The indirect table was read. Calculate the index inside<br />

740161 | // the table, for the requested zone.<br />

740162 | //<br />

740163 | i0 = (fzone - 7);<br />

740164 | //<br />

740165 | // Check if the zone is to be allocated.<br />

740166 | //<br />

740167 | if (zone_table[i0] == 0)<br />

740168 | {<br />

740169 | //<br />

740170 | // There is not such zone, but it is not an error.<br />

740171 | //<br />

740172 | if (!write)<br />

740173 | {<br />

740174 | return ((zno_t) 0);<br />

740175 | }<br />

740176 | //<br />

740177 | // The zone must be allocated.<br />

740178 | //<br />

740179 | allocated_zone = zone_alloc (inode->sb);<br />

740180 | if (allocated_zone == 0)<br />

740181 | {<br />

740182 | //<br />

740183 | // There is no space for the zone allocation. The<br />

740184 | // variable ‘errno’ is already updated by<br />

740185 | // ‘zone_alloc()’.<br />

740186 | //<br />

740187 | return ((zno_t) -1);<br />

740188 | }<br />

740189 | //<br />

740190 | // The zone is allocated: clear the zone and save.<br />

740191 | //<br />

740192 | memset (buffer, 0, SB_MAX_ZONE_SIZE);<br />

740193 | status = zone_write (inode->sb, allocated_zone, buffer);<br />

740194 | if (status < 0)<br />

740195 | {<br />

740196 | //<br />

740197 | // Cannot overwrite the zone. The variable ‘errno’ is<br />

740198 | // set by ‘zone_write()’.<br />

740199 | //<br />

740200 | return ((zno_t) -1);<br />

740201 | }<br />

740202 | //<br />

740203 | // The zone is allocated and cleared: update the indirect<br />

740204 | // zone table an save it. The inode is not modified,<br />

740205 | // because the indirect table is outside.<br />

740206 | //<br />

740207 | zone_table[i0] = allocated_zone;<br />

740208 | status = zone_write (inode->sb, inode->indirect1, zone_table);<br />

740209 | if (status != 0)<br />

740210 | {<br />

740211 | //<br />

740212 | // Cannot save the zone. The variable ‘errno’ is already<br />

740213 | // set by ‘zone_write()’.<br />

740214 | //<br />

740215 | return ((zno_t) -1);<br />

740216 | }<br />

740217 | }<br />

740218 | //<br />

740219 | // The zone is allocated.<br />

740220 | //<br />

740221 | return (zone_table[i0]);<br />

740222 | }<br />

740223 | else<br />

740224 | {<br />

740225 | //<br />

740226 | // (7 + indirect_zones) indirect2 == 0)<br />

740232 | {<br />

740233 | //<br />

740234 | // There is not such zone, but it is not an error.<br />

740235 | //<br />

740236 | if (!write)<br />

740237 | {<br />

740238 | return ((zno_t) 0);<br />

740239 | }<br />

740240 | //<br />

740241 | // The first level of second indirection must be<br />

740242 | // initialized.<br />

740243 | //<br />

740244 | allocated_zone = zone_alloc (inode->sb);<br />

740245 | if (allocated_zone == 0)<br />

740246 | {<br />

740247 | //<br />

740248 | // Cannot allocate the zone. The variable ‘errno’ is<br />

740249 | // set by ‘zone_alloc()’.<br />

740250 | //<br />

740251 | return ((zno_t) -1);<br />

740252 | }<br />

740253 | //<br />

740254 | // The zone for the indirection table is allocated:<br />

740255 | // clear the zone and save.<br />

740256 | //<br />

740257 | memset (buffer, 0, SB_MAX_ZONE_SIZE);<br />

740258 | status = zone_write (inode->sb, allocated_zone, buffer);<br />

740259 | if (status < 0)<br />

740260 | {<br />

740261 | //<br />

740262 | // Cannot overwrite the zone. The variable ‘errno’ is<br />

740263 | // set by ‘zone_write()’.<br />

740264 | //<br />

740265 | return ((zno_t) -1);<br />

740266 | }<br />

740267 | //<br />

740268 | // The zone for the indirection table is allocated and<br />

740269 | // cleared: save the inode.<br />

740270 | //<br />

740271 | inode->indirect2 = allocated_zone;<br />

740272 | inode->changed = 1;<br />

740273 | status = inode_save (inode);<br />

740274 | if (status != 0)<br />

740275 | {<br />

740276 | //<br />

740277 | // Cannot save the inode. The variable ‘errno’ is<br />

740278 | // set by ‘inode_save()’.<br />

740279 | //<br />

740280 | return ((zno_t) -1);<br />

740281 | }<br />

740282 | }<br />

740283 | //<br />

740284 | // The first level of second indirection is present:<br />

740285 | // Read the second indirect table.<br />

740286 | //<br />

740287 | status = zone_read (inode->sb, inode->indirect2, zone_table);<br />

740288 | if (status != 0)<br />

740289 | {<br />

740290 | //<br />

740291 | // Cannot read the second indirect table. The variable<br />

740292 | // ‘errno’ is set by ‘zone_read()’.<br />

740293 | //<br />

740294 | return ((zno_t) -1);<br />

740295 | }<br />

740296 | //<br />

740297 | // The first double indirect table was read: calculate<br />

740298 | // indexes inside first and second level of table.<br />

740299 | //<br />

740300 | fzone -= 7;<br />

740301 | fzone -= indirect_zones;<br />

740302 | i1 = fzone / indirect_zones;<br />

740303 | i2 = fzone % indirect_zones;<br />

740304 | //<br />

740305 | // Verify to have a second level.<br />

740306 | //<br />

740307 | if (zone_table[i1] == 0)<br />

740308 | {<br />

740309 | //

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!