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.

220 volume VI os16<br />

Script e sorgenti del kernel 221<br />

«<br />

«<br />

710054 | inode->references = 1;<br />

710055 | inode->changed = 0;<br />

710056 | //<br />

710057 | // Add all access permissions.<br />

710058 | //<br />

710059 | inode->mode |= (S_IRWXU|S_IRWXG|S_IRWXO);<br />

710060 | //<br />

710061 | // Return the inode pointer.<br />

710062 | //<br />

710063 | return (inode);<br />

710064 |}<br />

104.4.29 kernel/fs/inode_table.c<br />

Si veda la sezione 103.3.25.<br />

720001 |#include <br />

720002 |//----------------------------------------------------------------------<br />

720003 |inode_t inode_table[INODE_MAX_SLOTS];<br />

104.4.30 kernel/fs/inode_truncate.c<br />

Si veda la sezione 103.3.28.<br />

730001 |#include <br />

730002 |#include <br />

730003 |#include <br />

730004 |//----------------------------------------------------------------------<br />

730005 |int<br />

730006 |inode_truncate (inode_t *inode)<br />

730007 |{<br />

730008 | unsigned int indirect_zones;<br />

730009 | zno_t zone_table1[INODE_MAX_INDIRECT_ZONES];<br />

730010 | zno_t zone_table2[INODE_MAX_INDIRECT_ZONES];<br />

730011 | unsigned int i; // Direct index.<br />

730012 | unsigned int i0; // Single indirect index.<br />

730013 | unsigned int i1; // Double indirect first index.<br />

730014 | unsigned int i2; // Double indirect second index.<br />

730015 | int status; // ‘zone_read()’ return value.<br />

730016 | //<br />

730017 | // Calculate how many indirect zone numbers are stored inside<br />

730018 | // a zone: it depends on the zone size.<br />

730019 | //<br />

730020 | indirect_zones = inode->sb->blksize / 2;<br />

730021 | //<br />

730022 | // Scan and release direct zones. Errors are ignored.<br />

730023 | //<br />

730024 | for (i = 0; i < 7; i++)<br />

730025 | {<br />

730026 | zone_free (inode->sb, inode->direct[i]);<br />

730027 | inode->direct[i] = 0;<br />

730028 | }<br />

730029 | //<br />

730030 | // Scan single indirect zones, if present.<br />

730031 | //<br />

730032 | if (inode->blkcnt > 7 && inode->indirect1 != 0)<br />

730033 | {<br />

730034 | //<br />

730035 | // There is a single indirect table to load. Errors are<br />

730036 | // almost ignored.<br />

730037 | //<br />

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

730039 | if (status == 0)<br />

730040 | {<br />

730041 | //<br />

730042 | // Scan the table and remove zones.<br />

730043 | //<br />

730044 | for (i0 = 0; i0 < indirect_zones; i0++)<br />

730045 | {<br />

730046 | zone_free (inode->sb, zone_table1[i0]);<br />

730047 | }<br />

730048 | }<br />

730049 | //<br />

730050 | // Remove indirect table too.<br />

730051 | //<br />

730052 | zone_free (inode->sb, inode->indirect1);<br />

730053 | //<br />

730054 | // Clear single indirect reference inside the inode.<br />

730055 | //<br />

730056 | inode->indirect1 = 0;<br />

730057 | }<br />

730058 | //<br />

730059 | // Scan double indirect zones, if present.<br />

730060 | //<br />

730061 | if ( inode->blkcnt > (7+indirect_zones)<br />

730062 | && inode->indirect2 != 0)<br />

730063 | {<br />

730064 | //<br />

730065 | // There is a double indirect table to load. Errors are<br />

730066 | // almost ignored.<br />

730067 | //<br />

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

730069 | if (status == 0)<br />

730070 | {<br />

730071 | //<br />

730072 | // Scan the table and get second level indirection.<br />

730073 | //<br />

730074 | for (i1 = 0; i1 < indirect_zones; i1++)<br />

730075 | {<br />

730076 | if ((inode->blkcnt > (7+indirect_zones+indirect_zones*i1))<br />

730077 | && zone_table1[i1] != 0)<br />

730078 | {<br />

730079 | //<br />

730080 | // There is a second level table to load.<br />

730081 | //<br />

730082 | status = zone_read (inode->sb, zone_table1[i1],<br />

730083 | zone_table2);<br />

730084 | if (status == 0)<br />

730085 | {<br />

730086 | //<br />

730087 | // Release zones.<br />

730088 | //<br />

730089 | for (i2 = 0;<br />

730090 | i2 < indirect_zones &&<br />

730091 | (inode->blkcnt > (7+indirect_zones+indirect_zones*i1+i2));<br />

730092 | i2++)<br />

730093 | {<br />

730094 | zone_free (inode->sb, zone_table2[i2]);<br />

730095 | }<br />

730096 | //<br />

730097 | // Remove second level indirect table.<br />

730098 | //<br />

730099 | zone_free (inode->sb, zone_table1[i1]);<br />

730100 | }<br />

730101 | }<br />

730102 | }<br />

730103 | //<br />

730104 | // Remove first level indirect table.<br />

730105 | //<br />

730106 | zone_free (inode->sb, inode->indirect2);<br />

730107 | }<br />

730108 | //<br />

730109 | // Clear single indirect reference inside the inode.<br />

730110 | //<br />

730111 | inode->indirect2 = 0;<br />

730112 | }<br />

730113 | //<br />

730114 | // Update super block and inode data.<br />

730115 | //<br />

730116 | sb_save (inode->sb);<br />

730117 | inode->size = 0;<br />

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

730119 | inode_save (inode);<br />

730120 | //<br />

730121 | // Successful return.<br />

730122 | //<br />

730123 | return (0);<br />

730124 |}<br />

104.4.31 kernel/fs/inode_zone.c<br />

Si veda la sezione 103.3.29.<br />

740001 |#include <br />

740002 |#include <br />

740003 |#include <br />

740004 |//----------------------------------------------------------------------<br />

740005 |zno_t<br />

740006 |inode_zone (inode_t *inode, zno_t fzone, int write)<br />

740007 |{<br />

740008 | unsigned int indirect_zones;<br />

740009 | unsigned int allocated_zone;<br />

740010 | zno_t zone_table[INODE_MAX_INDIRECT_ZONES];<br />

740011 | char buffer[SB_MAX_ZONE_SIZE];<br />

740012 | unsigned int i0; // Single indirect index.<br />

740013 | unsigned int i1; // Double indirect first index.<br />

740014 | unsigned int i2; // Double indirect second index.<br />

740015 | int status;<br />

740016 | zno_t zone_second; // Second level table zone.<br />

740017 | //<br />

740018 | // Calculate how many indirect zone numbers are stored inside<br />

740019 | // a zone: it depends on the zone size.<br />

740020 | //<br />

740021 | indirect_zones = inode->sb->blksize / 2;<br />

740022 | //<br />

740023 | // Convert file-zone number into a zone number.<br />

740024 | //<br />

740025 | if (fzone < 7)<br />

740026 | {<br />

740027 | //<br />

740028 | // 0 sb);<br />

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

740047 | {<br />

740048 | //<br />

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

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

740051 | //<br />

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

740053 | }<br />

740054 | //<br />

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

740056 | //<br />

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

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

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

740060 | {<br />

740061 | //<br />

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

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

740064 | //<br />

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

740066 | }<br />

740067 | //<br />

740068 | // The zone is allocated and cleared: save the inode.<br />

740069 | //<br />

740070 | inode->direct[fzone] = allocated_zone;<br />

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

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

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

740074 | {<br />

740075 | //<br />

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

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

«

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

Saved successfully!

Ooh no, something went wrong!