symlink.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. struct sysfs_dirent * sd;
  45. int error;
  46. error = -ENOMEM;
  47. sl = kzalloc(sizeof(*sl), GFP_KERNEL);
  48. if (!sl)
  49. goto err_out;
  50. sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
  51. if (!sl->link_name)
  52. goto err_out;
  53. strcpy(sl->link_name, name);
  54. sl->target_kobj = kobject_get(target);
  55. sd = sysfs_new_dirent(sl, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
  56. if (!sd)
  57. goto err_out;
  58. sysfs_attach_dirent(sd, parent_sd, NULL);
  59. return 0;
  60. err_out:
  61. if (sl) {
  62. kobject_put(sl->target_kobj);
  63. kfree(sl->link_name);
  64. kfree(sl);
  65. }
  66. return error;
  67. }
  68. /**
  69. * sysfs_create_link - create symlink between two objects.
  70. * @kobj: object whose directory we're creating the link in.
  71. * @target: object we're pointing to.
  72. * @name: name of the symlink.
  73. */
  74. int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
  75. {
  76. struct dentry *dentry = NULL;
  77. int error = -EEXIST;
  78. BUG_ON(!name);
  79. if (!kobj) {
  80. if (sysfs_mount && sysfs_mount->mnt_sb)
  81. dentry = sysfs_mount->mnt_sb->s_root;
  82. } else
  83. dentry = kobj->dentry;
  84. if (!dentry)
  85. return -EFAULT;
  86. mutex_lock(&dentry->d_inode->i_mutex);
  87. if (!sysfs_dirent_exist(dentry->d_fsdata, name))
  88. error = sysfs_add_link(dentry, name, target);
  89. mutex_unlock(&dentry->d_inode->i_mutex);
  90. return error;
  91. }
  92. /**
  93. * sysfs_remove_link - remove symlink in object's directory.
  94. * @kobj: object we're acting for.
  95. * @name: name of the symlink to remove.
  96. */
  97. void sysfs_remove_link(struct kobject * kobj, const char * name)
  98. {
  99. sysfs_hash_and_remove(kobj->dentry,name);
  100. }
  101. static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
  102. char *path)
  103. {
  104. char * s;
  105. int depth, size;
  106. depth = object_depth(kobj);
  107. size = object_path_length(target) + depth * 3 - 1;
  108. if (size > PATH_MAX)
  109. return -ENAMETOOLONG;
  110. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  111. for (s = path; depth--; s += 3)
  112. strcpy(s,"../");
  113. fill_object_path(target, path, size);
  114. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  115. return 0;
  116. }
  117. static int sysfs_getlink(struct dentry *dentry, char * path)
  118. {
  119. struct kobject *kobj, *target_kobj;
  120. int error = 0;
  121. kobj = sysfs_get_kobject(dentry->d_parent);
  122. if (!kobj)
  123. return -EINVAL;
  124. target_kobj = sysfs_get_kobject(dentry);
  125. if (!target_kobj) {
  126. kobject_put(kobj);
  127. return -EINVAL;
  128. }
  129. down_read(&sysfs_rename_sem);
  130. error = sysfs_get_target_path(kobj, target_kobj, path);
  131. up_read(&sysfs_rename_sem);
  132. kobject_put(kobj);
  133. kobject_put(target_kobj);
  134. return error;
  135. }
  136. static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  137. {
  138. int error = -ENOMEM;
  139. unsigned long page = get_zeroed_page(GFP_KERNEL);
  140. if (page)
  141. error = sysfs_getlink(dentry, (char *) page);
  142. nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
  143. return NULL;
  144. }
  145. static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  146. {
  147. char *page = nd_get_link(nd);
  148. if (!IS_ERR(page))
  149. free_page((unsigned long)page);
  150. }
  151. const struct inode_operations sysfs_symlink_inode_operations = {
  152. .readlink = generic_readlink,
  153. .follow_link = sysfs_follow_link,
  154. .put_link = sysfs_put_link,
  155. };
  156. EXPORT_SYMBOL_GPL(sysfs_create_link);
  157. EXPORT_SYMBOL_GPL(sysfs_remove_link);