| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <error.h> |
|---|
| 20 | #include <fs/kfs.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | #include <keobject.h> |
|---|
| 23 | #include <malloc.h> |
|---|
| 24 | #include <module.h> |
|---|
| 25 | #include <panic.h> |
|---|
| 26 | #include <print.h> |
|---|
| 27 | #include <fs/devfs.h> |
|---|
| 28 | #include <fs/icfs.h> |
|---|
| 29 | |
|---|
| 30 | void KeObjectInit(struct KeObject* object, struct KeSet* parent) |
|---|
| 31 | { |
|---|
| 32 | object->refs = 1; |
|---|
| 33 | object->dir = NULL; |
|---|
| 34 | object->parent = parent; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | SYMBOL_EXPORT(KeObjectInit); |
|---|
| 38 | |
|---|
| 39 | void KeObjectDetach(struct KeObject* object) |
|---|
| 40 | { |
|---|
| 41 | if (object->dir) |
|---|
| 42 | IcFsRemoveDir(object->dir); |
|---|
| 43 | |
|---|
| 44 | if (object->parent) |
|---|
| 45 | ListRemove(&object->next); |
|---|
| 46 | |
|---|
| 47 | MemFree(object->name); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | SYMBOL_EXPORT(KeObjectDetach); |
|---|
| 51 | |
|---|
| 52 | void KeObjectFree(struct KeObject* object) |
|---|
| 53 | { |
|---|
| 54 | struct KeSet* parent = object->parent; |
|---|
| 55 | |
|---|
| 56 | KeObjectDetach(object); |
|---|
| 57 | |
|---|
| 58 | if (parent && parent->type && parent->type->release) |
|---|
| 59 | parent->type->release(object); |
|---|
| 60 | else |
|---|
| 61 | KernelPanic("No type to free from\n"); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | SYMBOL_EXPORT(KeObjectFree); |
|---|
| 65 | |
|---|
| 66 | int KeObjectAttach(struct KeObject* object, const char* name, ...) |
|---|
| 67 | { |
|---|
| 68 | VaList vaList; |
|---|
| 69 | int err; |
|---|
| 70 | |
|---|
| 71 | VaStart(vaList, name); |
|---|
| 72 | |
|---|
| 73 | err = KeObjectVaAttach(object, name, vaList); |
|---|
| 74 | |
|---|
| 75 | VaEnd(vaList); |
|---|
| 76 | |
|---|
| 77 | return err; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | int KeObjectVaAttach(struct KeObject* object, const char* name, VaList vaList) |
|---|
| 81 | { |
|---|
| 82 | if (!object) |
|---|
| 83 | return -EFAULT; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | if (object->dir) |
|---|
| 89 | return -EEXIST; |
|---|
| 90 | |
|---|
| 91 | object->name = vasprintf(name, vaList); |
|---|
| 92 | |
|---|
| 93 | if (!object->name) |
|---|
| 94 | return -EFAULT; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | if (object->parent && KeSetFind(object->parent, object->name)) |
|---|
| 98 | return -EEXIST; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | object->dir=IcFsCreateDir(KeObjGetParentDir(object), object->name); |
|---|
| 105 | |
|---|
| 106 | if (!object->dir) |
|---|
| 107 | return -EIO; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | KeSetAdd(object->parent, object); |
|---|
| 111 | |
|---|
| 112 | return 0; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | int KeObjUnbindFs(struct KeObject* object) |
|---|
| 116 | { |
|---|
| 117 | |
|---|
| 118 | return 0; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | struct KeObject* KeSetFind(struct KeSet* set, const char* name) |
|---|
| 122 | { |
|---|
| 123 | struct KeObject* curr; |
|---|
| 124 | static int i = 0; |
|---|
| 125 | |
|---|
| 126 | SpinLock(&set->lock); |
|---|
| 127 | |
|---|
| 128 | ListForEachEntry(curr, &set->head, next) |
|---|
| 129 | { |
|---|
| 130 | const char* objName = KeObjGetName(curr); |
|---|
| 131 | |
|---|
| 132 | if (!objName) |
|---|
| 133 | continue; |
|---|
| 134 | |
|---|
| 135 | if (!strcmp(name, objName)) |
|---|
| 136 | goto out; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | curr = NULL; |
|---|
| 140 | |
|---|
| 141 | out: |
|---|
| 142 | SpinUnlock(&set->lock); |
|---|
| 143 | return curr; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void KeSetInit(struct KeSet* set, struct KeSet* parent, struct KeObjType* type) |
|---|
| 147 | { |
|---|
| 148 | INIT_LIST_HEAD(&set->head); |
|---|
| 149 | KeObjectInit(&set->object, parent); |
|---|
| 150 | |
|---|
| 151 | set->devDir = NULL; |
|---|
| 152 | set->type = type; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | SYMBOL_EXPORT(KeSetInit); |
|---|
| 156 | |
|---|
| 157 | int KeSetAttach(struct KeSet* set, const char* name, ...) |
|---|
| 158 | { |
|---|
| 159 | if (!set) |
|---|
| 160 | return -EFAULT; |
|---|
| 161 | |
|---|
| 162 | return KeObjectAttach(&set->object, name); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | SYMBOL_EXPORT(KeSetAttach); |
|---|