symlink.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * symlink.c - operations for sysfs symlinks.
  3. */
  4. #include <linux/fs.h>
  5. #include <linux/mount.h>
  6. #include <linux/module.h>
  7. #include <linux/kobject.h>
  8. #include <linux/namei.h>
  9. #include <asm/semaphore.h>
  10. #include "sysfs.h"
  11. static int object_depth(struct kobject * kobj)
  12. {
  13. struct kobject * p = kobj;
  14. int depth = 0;
  15. do { depth++; } while ((p = p->parent));
  16. return depth;
  17. }
  18. static int object_path_length(struct kobject * kobj)
  19. {
  20. struct kobject * p = kobj;
  21. int length = 1;
  22. do {
  23. length += strlen(kobject_name(p)) + 1;
  24. p = p->parent;
  25. } while (p);
  26. return length;
  27. }
  28. static void fill_object_path(struct kobject * kobj, char * buffer, int length)
  29. {
  30. struct kobject * p;
  31. --length;
  32. for (p = kobj; p; p = p->parent) {
  33. int cur = strlen(kobject_name(p));
  34. /* back up enough to print this bus id with '/' */
  35. length -= cur;
  36. strncpy(buffer + length,kobject_name(p),cur);
  37. *(buffer + --length) = '/';
  38. }
  39. }
  40. static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
  41. {
  42. struct sysfs_dirent * parent_sd = parent->d_fsdata;
  43. struct sysfs_symlink * sl;
  44. int error = 0;
  45. error = -ENOMEM;
  46. sl = kmalloc(sizeof(*sl), GFP_KERNEL);
  47. if (!sl)
  48. goto exit1;
  49. sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
  50. if (!sl->link_name)
  51. goto exit2;
  52. strcpy(sl->link_name, name);
  53. sl->target_kobj = kobject_get(target);
  54. error = sysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
  55. SYSFS_KOBJ_LINK);
  56. if (!error)
  57. return 0;
  58. kobject_put(target);
  59. kfree(sl->link_name);
  60. exit2:
  61. kfree(sl);
  62. exit1:
  63. return error;
  64. }
  65. /**
  66. * sysfs_create_link - create symlink between two objects.
  67. * @kobj: object whose directory we're creating the link in.
  68. * @target: object we're pointing to.
  69. * @name: name of the symlink.
  70. */
  71. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  72. {
  73. struct dentry *dentry = NULL;
  74. int error = -EEXIST;
  75. BUG_ON(!name);
  76. if (!kobj) {
  77. if (sysfs_mount && sysfs_mount->mnt_sb)
  78. dentry = sysfs_mount->mnt_sb->s_root;
  79. } else
  80. dentry = kobj->dentry;
  81. if (!dentry)
  82. return -EFAULT;
  83. mutex_lock(&dentry->d_inode->i_mutex);
  84. if (!sysfs_dirent_exist(dentry->d_fsdata, name))
  85. error = sysfs_add_link(dentry, name, target);
  86. mutex_unlock(&dentry->d_inode->i_mutex);
  87. return error;
  88. }
  89. /**
  90. * sysfs_remove_link - remove symlink in object's directory.
  91. * @kobj: object we're acting for.
  92. * @name: name of the symlink to remove.
  93. */
  94. void sysfs_remove_link(struct kobject * kobj, const char * name)
  95. {
  96. sysfs_hash_and_remove(kobj->dentry,name);
  97. }
  98. static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
  99. char *path)
  100. {
  101. char * s;
  102. int depth, size;
  103. depth = object_depth(kobj);
  104. size = object_path_length(target) + depth * 3 - 1;
  105. if (size > PATH_MAX)
  106. return -ENAMETOOLONG;
  107. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  108. for (s = path; depth--; s += 3)
  109. strcpy(s,"../");
  110. fill_object_path(target, path, size);
  111. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  112. return 0;
  113. }
  114. static int sysfs_getlink(struct dentry *dentry, char * path)
  115. {
  116. struct kobject *kobj, *target_kobj;
  117. int error = 0;
  118. kobj = sysfs_get_kobject(dentry->d_parent);
  119. if (!kobj)
  120. return -EINVAL;
  121. target_kobj = sysfs_get_kobject(dentry);
  122. if (!target_kobj) {
  123. kobject_put(kobj);
  124. return -EINVAL;
  125. }
  126. down_read(&sysfs_rename_sem);
  127. error = sysfs_get_target_path(kobj, target_kobj, path);
  128. up_read(&sysfs_rename_sem);
  129. kobject_put(kobj);
  130. kobject_put(target_kobj);
  131. return error;
  132. }
  133. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  134. {
  135. int error = -ENOMEM;
  136. unsigned long page = get_zeroed_page(GFP_KERNEL);
  137. if (page)
  138. error = sysfs_getlink(dentry, (char *) page);
  139. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  140. return NULL;
  141. }
  142. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  143. {
  144. char *page = nd_get_link(nd);
  145. if (!IS_ERR(page))
  146. free_page((unsigned long)page);
  147. }
  148. const struct inode_operations sysfs_symlink_inode_operations = {
  149. .readlink = generic_readlink,
  150. .follow_link = sysfs_follow_link,
  151. .put_link = sysfs_put_link,
  152. };
  153. EXPORT_SYMBOL_GPL(sysfs_create_link);
  154. EXPORT_SYMBOL_GPL(sysfs_remove_link);