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.

218 volume VI os16<br />

Script e sorgenti del kernel 219<br />

«<br />

«<br />

680074 | }<br />

680075 | //<br />

680076 | inode_free (inode);<br />

680077 | return (0);<br />

680078 | }<br />

680079 | }<br />

680080 | //<br />

680081 | // Save inode to disk and return.<br />

680082 | //<br />

680083 | return (inode_save (inode));<br />

680084 |}<br />

104.4.26 kernel/fs/inode_reference.c<br />

Si veda la sezione 103.3.25.<br />

690001 |#include <br />

690002 |#include <br />

690003 |#include <br />

690004 |//----------------------------------------------------------------------<br />

690005 |inode_t *<br />

690006 |inode_reference (dev_t device, ino_t ino)<br />

690007 |{<br />

690008 | int s; // Slot index.<br />

690009 | sb_t *sb_table = sb_reference (0);<br />

690010 | //<br />

690011 | // If device is zero, and inode is zero, a reference to the whole<br />

690012 | // table is returned.<br />

690013 | //<br />

690014 | if (device == 0 && ino == 0)<br />

690015 | {<br />

690016 | return (inode_table);<br />

690017 | }<br />

690018 | //<br />

690019 | // If device is ((dev_t) -1) and the inode is ((ino_t) -1), a<br />

690020 | // reference to a free inode slot is returned.<br />

690021 | //<br />

690022 | if (device == (dev_t) -1 && ino == ((ino_t) -1))<br />

690023 | {<br />

690024 | for (s = 0; s < INODE_MAX_SLOTS; s++)<br />

690025 | {<br />

690026 | if (inode_table[s].references == 0)<br />

690027 | {<br />

690028 | return (&inode_table[s]);<br />

690029 | }<br />

690030 | }<br />

690031 | return (NULL);<br />

690032 | }<br />

690033 | //<br />

690034 | // If device is zero and the inode is 1, a reference to the root<br />

690035 | // directory inode is returned.<br />

690036 | //<br />

690037 | if (device == 0 && ino == 1)<br />

690038 | {<br />

690039 | //<br />

690040 | // The super block table is to be scanned.<br />

690041 | //<br />

690042 | for (device = 0, s = 0; s < SB_MAX_SLOTS; s++)<br />

690043 | {<br />

690044 | if (sb_table[s].device != 0 &&<br />

690045 | sb_table[s].inode_mounted_on == NULL)<br />

690046 | {<br />

690047 | device = sb_table[s].device;<br />

690048 | break;<br />

690049 | }<br />

690050 | }<br />

690051 | if (device == 0)<br />

690052 | {<br />

690053 | errset (E_CANNOT_FIND_ROOT_DEVICE);<br />

690054 | return (NULL);<br />

690055 | }<br />

690056 | //<br />

690057 | // Scan the inode table to find inode 1 and the same device.<br />

690058 | //<br />

690059 | for (s = 0; s < INODE_MAX_SLOTS; s++)<br />

690060 | {<br />

690061 | if (inode_table[s].sb->device == device &&<br />

690062 | inode_table[s].ino == 1)<br />

690063 | {<br />

690064 | return (&inode_table[s]);<br />

690065 | }<br />

690066 | }<br />

690067 | //<br />

690068 | // Cannot find a root file system inode.<br />

690069 | //<br />

690070 | errset (E_CANNOT_FIND_ROOT_INODE);<br />

690071 | return (NULL);<br />

690072 | }<br />

690073 | //<br />

690074 | // A device and an inode number were selected: find the inode<br />

690075 | // associated to it.<br />

690076 | //<br />

690077 | for (s = 0; s < INODE_MAX_SLOTS; s++)<br />

690078 | {<br />

690079 | if (inode_table[s].sb->device == device &&<br />

690080 | inode_table[s].ino == ino)<br />

690081 | {<br />

690082 | return (&inode_table[s]);<br />

690083 | }<br />

690084 | }<br />

690085 | //<br />

690086 | // The inode was not found.<br />

690087 | //<br />

690088 | return (NULL);<br />

690089 |}<br />

104.4.27 kernel/fs/inode_save.c<br />

Si veda la sezione 103.3.26.<br />

700001 |#include <br />

700002 |#include <br />

700003 |#include <br />

700004 |#include <br />

700005 |//----------------------------------------------------------------------<br />

700006 |int<br />

700007 |inode_save (inode_t *inode)<br />

700008 |{<br />

700009 | size_t size;<br />

700010 | unsigned long int start;<br />

700011 | ssize_t n;<br />

700012 | //<br />

700013 | // Check for valid argument.<br />

700014 | //<br />

700015 | if (inode == NULL)<br />

700016 | {<br />

700017 | errset (EINVAL); // Invalid argument.<br />

700018 | return (-1);<br />

700019 | }<br />

700020 | //<br />

700021 | // If the inode number is zero, no file system is involved!<br />

700022 | //<br />

700023 | if (inode->ino == 0)<br />

700024 | {<br />

700025 | return (0);<br />

700026 | }<br />

700027 | //<br />

700028 | // Save the super block to disk.<br />

700029 | //<br />

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

700031 | //<br />

700032 | // Save the inode to disk.<br />

700033 | //<br />

700034 | if (inode->changed)<br />

700035 | {<br />

700036 | size = offsetof (inode_t, sb);<br />

700037 | //<br />

700038 | // Calculating start position for write.<br />

700039 | //<br />

700040 | // [1] Boot block.<br />

700041 | // [2] Super block.<br />

700042 | // [3] Inode bit map.<br />

700043 | // [4] Zone bit map.<br />

700044 | // [5] Previous inodes: consider that the inode zero is<br />

700045 | // present in the inode map, but not in the inode<br />

700046 | // table.<br />

700047 | //<br />

700048 | start = 1024; // [1]<br />

700049 | start += 1024; // [2]<br />

700050 | start += (inode->sb->map_inode_blocks * 1024); // [3]<br />

700051 | start += (inode->sb->map_zone_blocks * 1024); // [4]<br />

700052 | start += ((inode->ino -1) * size); // [5]<br />

700053 | //<br />

700054 | // Write the inode.<br />

700055 | //<br />

700056 | n = dev_io ((pid_t) -1, inode->sb->device, DEV_WRITE, start,<br />

700057 | inode, size, NULL);<br />

700058 | //<br />

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

700060 | }<br />

700061 | return (0);<br />

700062 |}<br />

104.4.28 kernel/fs/inode_stdio_dev_make.c<br />

Si veda la sezione 103.3.27.<br />

710001 |#include <br />

710002 |#include <br />

710003 |#include <br />

710004 |//----------------------------------------------------------------------<br />

710005 |inode_t *<br />

710006 |inode_stdio_dev_make (dev_t device, mode_t mode)<br />

710007 |{<br />

710008 | inode_t *inode;<br />

710009 | //<br />

710010 | // Check for arguments.<br />

710011 | //<br />

710012 | if (mode == 0 || device == 0)<br />

710013 | {<br />

710014 | errset (EINVAL); // Invalid argument.<br />

710015 | return (NULL);<br />

710016 | }<br />

710017 | //<br />

710018 | // Find a free inode.<br />

710019 | //<br />

710020 | inode = inode_reference ((dev_t) -1, (ino_t) -1);<br />

710021 | if (inode == NULL)<br />

710022 | {<br />

710023 | //<br />

710024 | // No free slot available.<br />

710025 | //<br />

710026 | errset (ENFILE); // Too many files open in system.<br />

710027 | return (NULL);<br />

710028 | }<br />

710029 | //<br />

710030 | // Put data inside the inode. Please note that ‘inode->ino’ must be<br />

710031 | // zero, because it is necessary to recognize it as an internal<br />

710032 | // inode with no file system. Otherwise, with a value different than<br />

710033 | // zero, ‘inode_put()’ will try to remove it. [*]<br />

710034 | //<br />

710035 | inode->mode = mode;<br />

710036 | inode->uid = 0;<br />

710037 | inode->gid = 0;<br />

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

710039 | inode->time = k_time (NULL);<br />

710040 | inode->links = 0;<br />

710041 | inode->direct[0] = device;<br />

710042 | inode->direct[1] = 0;<br />

710043 | inode->direct[2] = 0;<br />

710044 | inode->direct[3] = 0;<br />

710045 | inode->direct[4] = 0;<br />

710046 | inode->direct[5] = 0;<br />

710047 | inode->direct[6] = 0;<br />

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

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

710050 | inode->sb_attached = NULL;<br />

710051 | inode->sb = 0;<br />

710052 | inode->ino = 0; // Must be zero. [*]<br />

710053 | inode->blkcnt = 0;<br />

«

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

Saved successfully!

Ooh no, something went wrong!