Advertisement
iocoder

mount.c

Nov 27th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. /*
  2.       +----------------------------------------------------------+
  3.       | +------------------------------------------------------+ |
  4.       | |  Quafios Kernel 1.0.1.                   | |
  5.       | |  Filesystem: Mount Point Management.         | |
  6.       | +------------------------------------------------------+ |
  7.       +----------------------------------------------------------+
  8. */
  9.  
  10. // This file is part of Quafios 1.0.1 source code.
  11. // Copyright (C) 2012  Mostafa Abd El-Aziz Mohamed.
  12.  
  13. // This program is free software: you can redistribute it and/or modify
  14. // it under the terms of the GNU General Public License as published by
  15. // the Free Software Foundation, either version 3 of the License, or
  16. // (at your option) any later version.
  17.  
  18. // This program is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. // GNU General Public License for more details.
  22.  
  23. // You should have received a copy of the GNU General Public License
  24. // along with Quafios.  If not, see <http://www.gnu.org/licenses/>.
  25.  
  26. // Visit http://www.quafios.com/ for contact information.
  27.  
  28. int isMountPoint(String path) {
  29.     // check whether path is mountpoint or not.
  30.     mountpoint_t *i;
  31.     for (i = mountlist.first; i != (mountpoint_t *) NULLPTR &&
  32.             isSubPath(path, i->path) != 0; i = i->next);
  33.     return (i == (mountpoint_t *) NULLPTR ? 0 : 1);
  34. }
  35.  
  36. unsigned int subMountPoints(String path) {
  37.     // Get count of sub mountpoints from "path".
  38.     unsigned int res = 0;
  39.     for (mountpoint_t *i = mountlist.first; i != (mountpoint_t *) NULLPTR; i = i->next)
  40.         if (isSubPath(i->path, path) == 1) res++;
  41.     return res;
  42. }
  43.  
  44. mountpoint_t *getMountPoint(String path) {
  45.     // Get mountpoint of path, i.e, the filesystem to which path belongs.
  46.     // if path is itself mountpoint, return the mountpoint_t that represents itself, else
  47.     // return the mountpoint_t that represents the filesystem it belongs to.
  48.     mountpoint_t *i;
  49.     for (i = mountlist.first; i != (mountpoint_t *) NULLPTR &&
  50.                     isSubPath(path, i->path) == -1; i = i->next);
  51.     // it is guaranteed that first i not meeting the condition is the result.
  52.     return i;
  53. }
  54.  
  55. int mount(char *devfile, char *mntpoint, char *fstype, unsigned int flags, char *data) {
  56.  
  57.     /* Mount System Call */
  58.  
  59.     unsigned int fsid, inst, error = 0;
  60.  
  61.     // Allocate Structures:
  62.     // ======================
  63.     String mntpoint_path = reduce_path(toString(mntpoint));
  64.     mountpoint_t *mntpoint_struct = (mountpoint_t *) kmalloc(sizeof(mountpoint_t));
  65.     if ((mntpoint_path.s == (char *) NULLPTR) ||
  66.          (mntpoint_struct == (mountpoint_t *) NULLPTR)) error = ENOMEM;
  67.  
  68.     // "path" must be directory:
  69.     // ===========================
  70.     if ((!error)) {
  71.         // TODO: stat.
  72.     }
  73.  
  74.     // "path" shouldn't be mountpoint:
  75.     // ================================
  76.     if ((!error) && isMountPoint(mntpoint_path))
  77.         error = EBUSY;
  78.  
  79.     // "path" shouldn't have sub mountpoints:
  80.     // =======================================
  81.     if ((!error) && subMountPoints(mntpoint_path))
  82.         error = EBUSY;
  83.  
  84.     // "fstype" should be proper:
  85.     // ============================
  86.     if (!error) {
  87.         // Search for a filesystem of that type:
  88.         for (fsid = 0; fsid < FSD_COUNT && strcmp(fstype, fsdrivers[fsid].alias); fsid++);
  89.  
  90.         if (fsid == FSD_COUNT)
  91.             // filesystem not found!
  92.             error = ENODEV;
  93.     }
  94.  
  95.     // Create instance of the filesystem:
  96.     // ===================================
  97.     if (!error) {
  98.         error = fsd_send_command(fsid, 0, FSC_MKINST,
  99.                     &((mkinst_data) {devfile,mntpoint_path,data,&inst}));
  100.     }
  101.  
  102.     // Set mountpoint structure:
  103.     // =============================
  104.     if (!error) {
  105.         mntpoint_struct->path = mntpoint_path;
  106.         mntpoint_struct->fsid = fsid;
  107.         mntpoint_struct->inst = inst;
  108.  
  109.         linkedlist_add((linkedlist *) &mountlist, (linknode *) mntpoint_struct);
  110.     }
  111.  
  112.     // Return:
  113.     // ========
  114.     if (error) {
  115.         // unallocate structures if there is error...
  116.         if (mntpoint_path.s != (char *) NULLPTR) kfree(mntpoint_path.s);
  117.         if (mntpoint_struct != (mountpoint_t *) NULLPTR) kfree(mntpoint_struct);
  118.     }
  119.     return error;
  120. }
  121.  
  122. int umount(char *target) {
  123.  
  124.     unsigned int error = 0;
  125.     mountpoint_t *mntpoint;
  126.  
  127.     // Allocate Structures:
  128.     // ======================
  129.     String target_path = reduce_path(toString(target));
  130.     if (target_path.s == (char *) NULLPTR) return ENOMEM;
  131.  
  132.     // Check if target must be mountpoint:
  133.     // ====================================
  134.     if (!error && !isMountPoint(target_path)) {
  135.         printk("'%s' is not a mountpoint\n", target_path.s);
  136.         error = EINVAL;
  137.     }
  138.  
  139.     // Check if target contains other mountpoints:
  140.     // ============================================
  141.     if (!error && subMountPoints(target_path)) {
  142.         error = EBUSY;
  143.         printk("'%s' has sub mountpoints\n", target_path.s);
  144.     }
  145.  
  146.     // Get Mount Point Structure:
  147.     // ==========================
  148.     if (!error) {
  149.         mntpoint = getMountPoint(target_path);
  150.     }
  151.  
  152.     // Remove filesystem Driver instance:
  153.     // ====================================
  154.     if (!error) {
  155.         error = fsd_send_command(mntpoint->fsid, mntpoint->inst, FSC_RMINST, 0);
  156.     }
  157.  
  158.     // Remove mountpoint structure:
  159.     // =============================
  160.     if (!error) {
  161.         linkedlist_aremove((linkedlist *) &mountlist, (linknode *) mntpoint);
  162.         kfree(mntpoint->path.s);
  163.         kfree(mntpoint);
  164.     }
  165.  
  166.     // Return:
  167.     // ========
  168.     kfree(target_path.s);
  169.     return error;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement