symlink.c 4.1 KB

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