symlink.c 4.2 KB

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