Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- +----------------------------------------------------------+
- | +------------------------------------------------------+ |
- | | Quafios Kernel 1.0.1. | |
- | | Filesystem: Mount Point Management. | |
- | +------------------------------------------------------+ |
- +----------------------------------------------------------+
- */
- // This file is part of Quafios 1.0.1 source code.
- // Copyright (C) 2012 Mostafa Abd El-Aziz Mohamed.
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with Quafios. If not, see <http://www.gnu.org/licenses/>.
- // Visit http://www.quafios.com/ for contact information.
- int isMountPoint(String path) {
- // check whether path is mountpoint or not.
- mountpoint_t *i;
- for (i = mountlist.first; i != (mountpoint_t *) NULLPTR &&
- isSubPath(path, i->path) != 0; i = i->next);
- return (i == (mountpoint_t *) NULLPTR ? 0 : 1);
- }
- unsigned int subMountPoints(String path) {
- // Get count of sub mountpoints from "path".
- unsigned int res = 0;
- for (mountpoint_t *i = mountlist.first; i != (mountpoint_t *) NULLPTR; i = i->next)
- if (isSubPath(i->path, path) == 1) res++;
- return res;
- }
- mountpoint_t *getMountPoint(String path) {
- // Get mountpoint of path, i.e, the filesystem to which path belongs.
- // if path is itself mountpoint, return the mountpoint_t that represents itself, else
- // return the mountpoint_t that represents the filesystem it belongs to.
- mountpoint_t *i;
- for (i = mountlist.first; i != (mountpoint_t *) NULLPTR &&
- isSubPath(path, i->path) == -1; i = i->next);
- // it is guaranteed that first i not meeting the condition is the result.
- return i;
- }
- int mount(char *devfile, char *mntpoint, char *fstype, unsigned int flags, char *data) {
- /* Mount System Call */
- unsigned int fsid, inst, error = 0;
- // Allocate Structures:
- // ======================
- String mntpoint_path = reduce_path(toString(mntpoint));
- mountpoint_t *mntpoint_struct = (mountpoint_t *) kmalloc(sizeof(mountpoint_t));
- if ((mntpoint_path.s == (char *) NULLPTR) ||
- (mntpoint_struct == (mountpoint_t *) NULLPTR)) error = ENOMEM;
- // "path" must be directory:
- // ===========================
- if ((!error)) {
- // TODO: stat.
- }
- // "path" shouldn't be mountpoint:
- // ================================
- if ((!error) && isMountPoint(mntpoint_path))
- error = EBUSY;
- // "path" shouldn't have sub mountpoints:
- // =======================================
- if ((!error) && subMountPoints(mntpoint_path))
- error = EBUSY;
- // "fstype" should be proper:
- // ============================
- if (!error) {
- // Search for a filesystem of that type:
- for (fsid = 0; fsid < FSD_COUNT && strcmp(fstype, fsdrivers[fsid].alias); fsid++);
- if (fsid == FSD_COUNT)
- // filesystem not found!
- error = ENODEV;
- }
- // Create instance of the filesystem:
- // ===================================
- if (!error) {
- error = fsd_send_command(fsid, 0, FSC_MKINST,
- &((mkinst_data) {devfile,mntpoint_path,data,&inst}));
- }
- // Set mountpoint structure:
- // =============================
- if (!error) {
- mntpoint_struct->path = mntpoint_path;
- mntpoint_struct->fsid = fsid;
- mntpoint_struct->inst = inst;
- linkedlist_add((linkedlist *) &mountlist, (linknode *) mntpoint_struct);
- }
- // Return:
- // ========
- if (error) {
- // unallocate structures if there is error...
- if (mntpoint_path.s != (char *) NULLPTR) kfree(mntpoint_path.s);
- if (mntpoint_struct != (mountpoint_t *) NULLPTR) kfree(mntpoint_struct);
- }
- return error;
- }
- int umount(char *target) {
- unsigned int error = 0;
- mountpoint_t *mntpoint;
- // Allocate Structures:
- // ======================
- String target_path = reduce_path(toString(target));
- if (target_path.s == (char *) NULLPTR) return ENOMEM;
- // Check if target must be mountpoint:
- // ====================================
- if (!error && !isMountPoint(target_path)) {
- printk("'%s' is not a mountpoint\n", target_path.s);
- error = EINVAL;
- }
- // Check if target contains other mountpoints:
- // ============================================
- if (!error && subMountPoints(target_path)) {
- error = EBUSY;
- printk("'%s' has sub mountpoints\n", target_path.s);
- }
- // Get Mount Point Structure:
- // ==========================
- if (!error) {
- mntpoint = getMountPoint(target_path);
- }
- // Remove filesystem Driver instance:
- // ====================================
- if (!error) {
- error = fsd_send_command(mntpoint->fsid, mntpoint->inst, FSC_RMINST, 0);
- }
- // Remove mountpoint structure:
- // =============================
- if (!error) {
- linkedlist_aremove((linkedlist *) &mountlist, (linknode *) mntpoint);
- kfree(mntpoint->path.s);
- kfree(mntpoint);
- }
- // Return:
- // ========
- kfree(target_path.s);
- return error;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement