dir.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * fs/sysfs/dir.c - sysfs core and dir operation implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #undef DEBUG
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/module.h>
  16. #include <linux/kobject.h>
  17. #include <linux/namei.h>
  18. #include <linux/idr.h>
  19. #include <linux/completion.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/security.h>
  23. #include <linux/hash.h>
  24. #include "sysfs.h"
  25. DEFINE_MUTEX(sysfs_mutex);
  26. DEFINE_SPINLOCK(sysfs_assoc_lock);
  27. #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
  28. static DEFINE_SPINLOCK(sysfs_ino_lock);
  29. static DEFINE_IDA(sysfs_ino_ida);
  30. /**
  31. * sysfs_name_hash
  32. * @ns: Namespace tag to hash
  33. * @name: Null terminated string to hash
  34. *
  35. * Returns 31 bit hash of ns + name (so it fits in an off_t )
  36. */
  37. static unsigned int sysfs_name_hash(const void *ns, const char *name)
  38. {
  39. unsigned long hash = init_name_hash();
  40. unsigned int len = strlen(name);
  41. while (len--)
  42. hash = partial_name_hash(*name++, hash);
  43. hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
  44. hash &= 0x7fffffffU;
  45. /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
  46. if (hash < 1)
  47. hash += 2;
  48. if (hash >= INT_MAX)
  49. hash = INT_MAX - 1;
  50. return hash;
  51. }
  52. static int sysfs_name_compare(unsigned int hash, const void *ns,
  53. const char *name, const struct sysfs_dirent *sd)
  54. {
  55. if (hash != sd->s_hash)
  56. return hash - sd->s_hash;
  57. if (ns != sd->s_ns)
  58. return ns - sd->s_ns;
  59. return strcmp(name, sd->s_name);
  60. }
  61. static int sysfs_sd_compare(const struct sysfs_dirent *left,
  62. const struct sysfs_dirent *right)
  63. {
  64. return sysfs_name_compare(left->s_hash, left->s_ns, left->s_name,
  65. right);
  66. }
  67. /**
  68. * sysfs_link_sibling - link sysfs_dirent into sibling rbtree
  69. * @sd: sysfs_dirent of interest
  70. *
  71. * Link @sd into its sibling rbtree which starts from
  72. * sd->s_parent->s_dir.children.
  73. *
  74. * Locking:
  75. * mutex_lock(sysfs_mutex)
  76. *
  77. * RETURNS:
  78. * 0 on susccess -EEXIST on failure.
  79. */
  80. static int sysfs_link_sibling(struct sysfs_dirent *sd)
  81. {
  82. struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
  83. struct rb_node *parent = NULL;
  84. if (sysfs_type(sd) == SYSFS_DIR)
  85. sd->s_parent->s_dir.subdirs++;
  86. while (*node) {
  87. struct sysfs_dirent *pos;
  88. int result;
  89. pos = to_sysfs_dirent(*node);
  90. parent = *node;
  91. result = sysfs_sd_compare(sd, pos);
  92. if (result < 0)
  93. node = &pos->s_rb.rb_left;
  94. else if (result > 0)
  95. node = &pos->s_rb.rb_right;
  96. else
  97. return -EEXIST;
  98. }
  99. /* add new node and rebalance the tree */
  100. rb_link_node(&sd->s_rb, parent, node);
  101. rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
  102. return 0;
  103. }
  104. /**
  105. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
  106. * @sd: sysfs_dirent of interest
  107. *
  108. * Unlink @sd from its sibling rbtree which starts from
  109. * sd->s_parent->s_dir.children.
  110. *
  111. * Locking:
  112. * mutex_lock(sysfs_mutex)
  113. */
  114. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  115. {
  116. if (sysfs_type(sd) == SYSFS_DIR)
  117. sd->s_parent->s_dir.subdirs--;
  118. rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
  119. }
  120. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  121. /* Test for attributes that want to ignore lockdep for read-locking */
  122. static bool ignore_lockdep(struct sysfs_dirent *sd)
  123. {
  124. return sysfs_type(sd) == SYSFS_KOBJ_ATTR &&
  125. sd->s_attr.attr->ignore_lockdep;
  126. }
  127. #else
  128. static inline bool ignore_lockdep(struct sysfs_dirent *sd)
  129. {
  130. return true;
  131. }
  132. #endif
  133. /**
  134. * sysfs_get_active - get an active reference to sysfs_dirent
  135. * @sd: sysfs_dirent to get an active reference to
  136. *
  137. * Get an active reference of @sd. This function is noop if @sd
  138. * is NULL.
  139. *
  140. * RETURNS:
  141. * Pointer to @sd on success, NULL on failure.
  142. */
  143. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  144. {
  145. if (unlikely(!sd))
  146. return NULL;
  147. if (!atomic_inc_unless_negative(&sd->s_active))
  148. return NULL;
  149. if (likely(!ignore_lockdep(sd)))
  150. rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
  151. return sd;
  152. }
  153. /**
  154. * sysfs_put_active - put an active reference to sysfs_dirent
  155. * @sd: sysfs_dirent to put an active reference to
  156. *
  157. * Put an active reference to @sd. This function is noop if @sd
  158. * is NULL.
  159. */
  160. void sysfs_put_active(struct sysfs_dirent *sd)
  161. {
  162. int v;
  163. if (unlikely(!sd))
  164. return;
  165. if (likely(!ignore_lockdep(sd)))
  166. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  167. v = atomic_dec_return(&sd->s_active);
  168. if (likely(v != SD_DEACTIVATED_BIAS))
  169. return;
  170. /* atomic_dec_return() is a mb(), we'll always see the updated
  171. * sd->u.completion.
  172. */
  173. complete(sd->u.completion);
  174. }
  175. /**
  176. * sysfs_deactivate - deactivate sysfs_dirent
  177. * @sd: sysfs_dirent to deactivate
  178. *
  179. * Deny new active references and drain existing ones.
  180. */
  181. static void sysfs_deactivate(struct sysfs_dirent *sd)
  182. {
  183. DECLARE_COMPLETION_ONSTACK(wait);
  184. int v;
  185. BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
  186. if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
  187. return;
  188. sd->u.completion = (void *)&wait;
  189. rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
  190. /* atomic_add_return() is a mb(), put_active() will always see
  191. * the updated sd->u.completion.
  192. */
  193. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  194. if (v != SD_DEACTIVATED_BIAS) {
  195. lock_contended(&sd->dep_map, _RET_IP_);
  196. wait_for_completion(&wait);
  197. }
  198. lock_acquired(&sd->dep_map, _RET_IP_);
  199. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  200. }
  201. static int sysfs_alloc_ino(unsigned int *pino)
  202. {
  203. int ino, rc;
  204. retry:
  205. spin_lock(&sysfs_ino_lock);
  206. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  207. spin_unlock(&sysfs_ino_lock);
  208. if (rc == -EAGAIN) {
  209. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  210. goto retry;
  211. rc = -ENOMEM;
  212. }
  213. *pino = ino;
  214. return rc;
  215. }
  216. static void sysfs_free_ino(unsigned int ino)
  217. {
  218. spin_lock(&sysfs_ino_lock);
  219. ida_remove(&sysfs_ino_ida, ino);
  220. spin_unlock(&sysfs_ino_lock);
  221. }
  222. void release_sysfs_dirent(struct sysfs_dirent *sd)
  223. {
  224. struct sysfs_dirent *parent_sd;
  225. repeat:
  226. /* Moving/renaming is always done while holding reference.
  227. * sd->s_parent won't change beneath us.
  228. */
  229. parent_sd = sd->s_parent;
  230. WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
  231. "sysfs: free using entry: %s/%s\n",
  232. parent_sd ? parent_sd->s_name : "", sd->s_name);
  233. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  234. sysfs_put(sd->s_symlink.target_sd);
  235. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  236. kfree(sd->s_name);
  237. if (sd->s_iattr && sd->s_iattr->ia_secdata)
  238. security_release_secctx(sd->s_iattr->ia_secdata,
  239. sd->s_iattr->ia_secdata_len);
  240. kfree(sd->s_iattr);
  241. sysfs_free_ino(sd->s_ino);
  242. kmem_cache_free(sysfs_dir_cachep, sd);
  243. sd = parent_sd;
  244. if (sd && atomic_dec_and_test(&sd->s_count))
  245. goto repeat;
  246. }
  247. static int sysfs_dentry_delete(const struct dentry *dentry)
  248. {
  249. struct sysfs_dirent *sd = dentry->d_fsdata;
  250. return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
  251. }
  252. static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  253. {
  254. struct sysfs_dirent *sd;
  255. int type;
  256. if (flags & LOOKUP_RCU)
  257. return -ECHILD;
  258. sd = dentry->d_fsdata;
  259. mutex_lock(&sysfs_mutex);
  260. /* The sysfs dirent has been deleted */
  261. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  262. goto out_bad;
  263. /* The sysfs dirent has been moved? */
  264. if (dentry->d_parent->d_fsdata != sd->s_parent)
  265. goto out_bad;
  266. /* The sysfs dirent has been renamed */
  267. if (strcmp(dentry->d_name.name, sd->s_name) != 0)
  268. goto out_bad;
  269. /* The sysfs dirent has been moved to a different namespace */
  270. type = KOBJ_NS_TYPE_NONE;
  271. if (sd->s_parent) {
  272. type = sysfs_ns_type(sd->s_parent);
  273. if (type != KOBJ_NS_TYPE_NONE &&
  274. sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns)
  275. goto out_bad;
  276. }
  277. mutex_unlock(&sysfs_mutex);
  278. out_valid:
  279. return 1;
  280. out_bad:
  281. /* Remove the dentry from the dcache hashes.
  282. * If this is a deleted dentry we use d_drop instead of d_delete
  283. * so sysfs doesn't need to cope with negative dentries.
  284. *
  285. * If this is a dentry that has simply been renamed we
  286. * use d_drop to remove it from the dcache lookup on its
  287. * old parent. If this dentry persists later when a lookup
  288. * is performed at its new name the dentry will be readded
  289. * to the dcache hashes.
  290. */
  291. mutex_unlock(&sysfs_mutex);
  292. /* If we have submounts we must allow the vfs caches
  293. * to lie about the state of the filesystem to prevent
  294. * leaks and other nasty things.
  295. */
  296. if (check_submounts_and_drop(dentry) != 0)
  297. goto out_valid;
  298. return 0;
  299. }
  300. static void sysfs_dentry_release(struct dentry *dentry)
  301. {
  302. sysfs_put(dentry->d_fsdata);
  303. }
  304. const struct dentry_operations sysfs_dentry_ops = {
  305. .d_revalidate = sysfs_dentry_revalidate,
  306. .d_delete = sysfs_dentry_delete,
  307. .d_release = sysfs_dentry_release,
  308. };
  309. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  310. {
  311. char *dup_name = NULL;
  312. struct sysfs_dirent *sd;
  313. if (type & SYSFS_COPY_NAME) {
  314. name = dup_name = kstrdup(name, GFP_KERNEL);
  315. if (!name)
  316. return NULL;
  317. }
  318. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  319. if (!sd)
  320. goto err_out1;
  321. if (sysfs_alloc_ino(&sd->s_ino))
  322. goto err_out2;
  323. atomic_set(&sd->s_count, 1);
  324. atomic_set(&sd->s_active, 0);
  325. sd->s_name = name;
  326. sd->s_mode = mode;
  327. sd->s_flags = type | SYSFS_FLAG_REMOVED;
  328. return sd;
  329. err_out2:
  330. kmem_cache_free(sysfs_dir_cachep, sd);
  331. err_out1:
  332. kfree(dup_name);
  333. return NULL;
  334. }
  335. /**
  336. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  337. * @acxt: pointer to sysfs_addrm_cxt to be used
  338. * @parent_sd: parent sysfs_dirent
  339. *
  340. * This function is called when the caller is about to add or
  341. * remove sysfs_dirent under @parent_sd. This function acquires
  342. * sysfs_mutex. @acxt is used to keep and pass context to
  343. * other addrm functions.
  344. *
  345. * LOCKING:
  346. * Kernel thread context (may sleep). sysfs_mutex is locked on
  347. * return.
  348. */
  349. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  350. struct sysfs_dirent *parent_sd)
  351. {
  352. memset(acxt, 0, sizeof(*acxt));
  353. acxt->parent_sd = parent_sd;
  354. mutex_lock(&sysfs_mutex);
  355. }
  356. /**
  357. * __sysfs_add_one - add sysfs_dirent to parent without warning
  358. * @acxt: addrm context to use
  359. * @sd: sysfs_dirent to be added
  360. *
  361. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  362. * nlink of parent inode if @sd is a directory and link into the
  363. * children list of the parent.
  364. *
  365. * This function should be called between calls to
  366. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  367. * passed the same @acxt as passed to sysfs_addrm_start().
  368. *
  369. * LOCKING:
  370. * Determined by sysfs_addrm_start().
  371. *
  372. * RETURNS:
  373. * 0 on success, -EEXIST if entry with the given name already
  374. * exists.
  375. */
  376. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  377. {
  378. struct sysfs_inode_attrs *ps_iattr;
  379. int ret;
  380. if (!!sysfs_ns_type(acxt->parent_sd) != !!sd->s_ns) {
  381. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  382. sysfs_ns_type(acxt->parent_sd) ? "required" : "invalid",
  383. acxt->parent_sd->s_name, sd->s_name);
  384. return -EINVAL;
  385. }
  386. sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name);
  387. sd->s_parent = sysfs_get(acxt->parent_sd);
  388. ret = sysfs_link_sibling(sd);
  389. if (ret)
  390. return ret;
  391. /* Update timestamps on the parent */
  392. ps_iattr = acxt->parent_sd->s_iattr;
  393. if (ps_iattr) {
  394. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  395. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  396. }
  397. /* Mark the entry added into directory tree */
  398. sd->s_flags &= ~SYSFS_FLAG_REMOVED;
  399. return 0;
  400. }
  401. /**
  402. * sysfs_pathname - return full path to sysfs dirent
  403. * @sd: sysfs_dirent whose path we want
  404. * @path: caller allocated buffer of size PATH_MAX
  405. *
  406. * Gives the name "/" to the sysfs_root entry; any path returned
  407. * is relative to wherever sysfs is mounted.
  408. */
  409. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  410. {
  411. if (sd->s_parent) {
  412. sysfs_pathname(sd->s_parent, path);
  413. strlcat(path, "/", PATH_MAX);
  414. }
  415. strlcat(path, sd->s_name, PATH_MAX);
  416. return path;
  417. }
  418. /**
  419. * sysfs_add_one - add sysfs_dirent to parent
  420. * @acxt: addrm context to use
  421. * @sd: sysfs_dirent to be added
  422. *
  423. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  424. * nlink of parent inode if @sd is a directory and link into the
  425. * children list of the parent.
  426. *
  427. * This function should be called between calls to
  428. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  429. * passed the same @acxt as passed to sysfs_addrm_start().
  430. *
  431. * LOCKING:
  432. * Determined by sysfs_addrm_start().
  433. *
  434. * RETURNS:
  435. * 0 on success, -EEXIST if entry with the given name already
  436. * exists.
  437. */
  438. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  439. {
  440. int ret;
  441. ret = __sysfs_add_one(acxt, sd);
  442. if (ret == -EEXIST) {
  443. char *path = kzalloc(PATH_MAX, GFP_KERNEL);
  444. WARN(1, KERN_WARNING
  445. "sysfs: cannot create duplicate filename '%s'\n",
  446. (path == NULL) ? sd->s_name
  447. : (sysfs_pathname(acxt->parent_sd, path),
  448. strlcat(path, "/", PATH_MAX),
  449. strlcat(path, sd->s_name, PATH_MAX),
  450. path));
  451. kfree(path);
  452. }
  453. return ret;
  454. }
  455. /**
  456. * sysfs_remove_one - remove sysfs_dirent from parent
  457. * @acxt: addrm context to use
  458. * @sd: sysfs_dirent to be removed
  459. *
  460. * Mark @sd removed and drop nlink of parent inode if @sd is a
  461. * directory. @sd is unlinked from the children list.
  462. *
  463. * This function should be called between calls to
  464. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  465. * passed the same @acxt as passed to sysfs_addrm_start().
  466. *
  467. * LOCKING:
  468. * Determined by sysfs_addrm_start().
  469. */
  470. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  471. {
  472. struct sysfs_inode_attrs *ps_iattr;
  473. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  474. sysfs_unlink_sibling(sd);
  475. /* Update timestamps on the parent */
  476. ps_iattr = acxt->parent_sd->s_iattr;
  477. if (ps_iattr) {
  478. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  479. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  480. }
  481. sd->s_flags |= SYSFS_FLAG_REMOVED;
  482. sd->u.removed_list = acxt->removed;
  483. acxt->removed = sd;
  484. }
  485. /**
  486. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  487. * @acxt: addrm context to finish up
  488. *
  489. * Finish up sysfs_dirent add/remove. Resources acquired by
  490. * sysfs_addrm_start() are released and removed sysfs_dirents are
  491. * cleaned up.
  492. *
  493. * LOCKING:
  494. * sysfs_mutex is released.
  495. */
  496. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  497. {
  498. /* release resources acquired by sysfs_addrm_start() */
  499. mutex_unlock(&sysfs_mutex);
  500. /* kill removed sysfs_dirents */
  501. while (acxt->removed) {
  502. struct sysfs_dirent *sd = acxt->removed;
  503. acxt->removed = sd->u.removed_list;
  504. sysfs_deactivate(sd);
  505. unmap_bin_file(sd);
  506. sysfs_put(sd);
  507. }
  508. }
  509. /**
  510. * sysfs_find_dirent - find sysfs_dirent with the given name
  511. * @parent_sd: sysfs_dirent to search under
  512. * @name: name to look for
  513. *
  514. * Look for sysfs_dirent with name @name under @parent_sd.
  515. *
  516. * LOCKING:
  517. * mutex_lock(sysfs_mutex)
  518. *
  519. * RETURNS:
  520. * Pointer to sysfs_dirent if found, NULL if not.
  521. */
  522. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  523. const void *ns,
  524. const unsigned char *name)
  525. {
  526. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  527. unsigned int hash;
  528. if (!!sysfs_ns_type(parent_sd) != !!ns) {
  529. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  530. sysfs_ns_type(parent_sd) ? "required" : "invalid",
  531. parent_sd->s_name, name);
  532. return NULL;
  533. }
  534. hash = sysfs_name_hash(ns, name);
  535. while (node) {
  536. struct sysfs_dirent *sd;
  537. int result;
  538. sd = to_sysfs_dirent(node);
  539. result = sysfs_name_compare(hash, ns, name, sd);
  540. if (result < 0)
  541. node = node->rb_left;
  542. else if (result > 0)
  543. node = node->rb_right;
  544. else
  545. return sd;
  546. }
  547. return NULL;
  548. }
  549. /**
  550. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  551. * @parent_sd: sysfs_dirent to search under
  552. * @name: name to look for
  553. *
  554. * Look for sysfs_dirent with name @name under @parent_sd and get
  555. * it if found.
  556. *
  557. * LOCKING:
  558. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  559. *
  560. * RETURNS:
  561. * Pointer to sysfs_dirent if found, NULL if not.
  562. */
  563. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  564. const void *ns,
  565. const unsigned char *name)
  566. {
  567. struct sysfs_dirent *sd;
  568. mutex_lock(&sysfs_mutex);
  569. sd = sysfs_find_dirent(parent_sd, ns, name);
  570. sysfs_get(sd);
  571. mutex_unlock(&sysfs_mutex);
  572. return sd;
  573. }
  574. EXPORT_SYMBOL_GPL(sysfs_get_dirent);
  575. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  576. enum kobj_ns_type type, const void *ns, const char *name,
  577. struct sysfs_dirent **p_sd)
  578. {
  579. umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  580. struct sysfs_addrm_cxt acxt;
  581. struct sysfs_dirent *sd;
  582. int rc;
  583. /* allocate */
  584. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  585. if (!sd)
  586. return -ENOMEM;
  587. sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
  588. sd->s_ns = ns;
  589. sd->s_dir.kobj = kobj;
  590. /* link in */
  591. sysfs_addrm_start(&acxt, parent_sd);
  592. rc = sysfs_add_one(&acxt, sd);
  593. sysfs_addrm_finish(&acxt);
  594. if (rc == 0)
  595. *p_sd = sd;
  596. else
  597. sysfs_put(sd);
  598. return rc;
  599. }
  600. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  601. struct sysfs_dirent **p_sd)
  602. {
  603. return create_dir(kobj, kobj->sd,
  604. KOBJ_NS_TYPE_NONE, NULL, name, p_sd);
  605. }
  606. /**
  607. * sysfs_read_ns_type: return associated ns_type
  608. * @kobj: the kobject being queried
  609. *
  610. * Each kobject can be tagged with exactly one namespace type
  611. * (i.e. network or user). Return the ns_type associated with
  612. * this object if any
  613. */
  614. static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
  615. {
  616. const struct kobj_ns_type_operations *ops;
  617. enum kobj_ns_type type;
  618. ops = kobj_child_ns_ops(kobj);
  619. if (!ops)
  620. return KOBJ_NS_TYPE_NONE;
  621. type = ops->type;
  622. BUG_ON(type <= KOBJ_NS_TYPE_NONE);
  623. BUG_ON(type >= KOBJ_NS_TYPES);
  624. BUG_ON(!kobj_ns_type_registered(type));
  625. return type;
  626. }
  627. /**
  628. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  629. * @kobj: object we're creating directory for
  630. * @ns: the namespace tag to use
  631. */
  632. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  633. {
  634. enum kobj_ns_type type;
  635. struct sysfs_dirent *parent_sd, *sd;
  636. int error = 0;
  637. BUG_ON(!kobj);
  638. if (kobj->parent)
  639. parent_sd = kobj->parent->sd;
  640. else
  641. parent_sd = &sysfs_root;
  642. if (!parent_sd)
  643. return -ENOENT;
  644. type = sysfs_read_ns_type(kobj);
  645. error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd);
  646. if (!error)
  647. kobj->sd = sd;
  648. return error;
  649. }
  650. static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
  651. unsigned int flags)
  652. {
  653. struct dentry *ret = NULL;
  654. struct dentry *parent = dentry->d_parent;
  655. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  656. struct sysfs_dirent *sd;
  657. struct inode *inode;
  658. enum kobj_ns_type type;
  659. const void *ns;
  660. mutex_lock(&sysfs_mutex);
  661. type = sysfs_ns_type(parent_sd);
  662. ns = sysfs_info(dir->i_sb)->ns[type];
  663. sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name);
  664. /* no such entry */
  665. if (!sd) {
  666. ret = ERR_PTR(-ENOENT);
  667. goto out_unlock;
  668. }
  669. dentry->d_fsdata = sysfs_get(sd);
  670. /* attach dentry and inode */
  671. inode = sysfs_get_inode(dir->i_sb, sd);
  672. if (!inode) {
  673. ret = ERR_PTR(-ENOMEM);
  674. goto out_unlock;
  675. }
  676. /* instantiate and hash dentry */
  677. ret = d_materialise_unique(dentry, inode);
  678. out_unlock:
  679. mutex_unlock(&sysfs_mutex);
  680. return ret;
  681. }
  682. const struct inode_operations sysfs_dir_inode_operations = {
  683. .lookup = sysfs_lookup,
  684. .permission = sysfs_permission,
  685. .setattr = sysfs_setattr,
  686. .getattr = sysfs_getattr,
  687. .setxattr = sysfs_setxattr,
  688. };
  689. static void remove_dir(struct sysfs_dirent *sd)
  690. {
  691. struct sysfs_addrm_cxt acxt;
  692. sysfs_addrm_start(&acxt, sd->s_parent);
  693. sysfs_remove_one(&acxt, sd);
  694. sysfs_addrm_finish(&acxt);
  695. }
  696. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  697. {
  698. remove_dir(sd);
  699. }
  700. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  701. {
  702. struct sysfs_addrm_cxt acxt;
  703. struct rb_node *pos;
  704. if (!dir_sd)
  705. return;
  706. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  707. sysfs_addrm_start(&acxt, dir_sd);
  708. pos = rb_first(&dir_sd->s_dir.children);
  709. while (pos) {
  710. struct sysfs_dirent *sd = to_sysfs_dirent(pos);
  711. pos = rb_next(pos);
  712. if (sysfs_type(sd) != SYSFS_DIR)
  713. sysfs_remove_one(&acxt, sd);
  714. }
  715. sysfs_addrm_finish(&acxt);
  716. remove_dir(dir_sd);
  717. }
  718. /**
  719. * sysfs_remove_dir - remove an object's directory.
  720. * @kobj: object.
  721. *
  722. * The only thing special about this is that we remove any files in
  723. * the directory before we remove the directory, and we've inlined
  724. * what used to be sysfs_rmdir() below, instead of calling separately.
  725. */
  726. void sysfs_remove_dir(struct kobject *kobj)
  727. {
  728. struct sysfs_dirent *sd = kobj->sd;
  729. spin_lock(&sysfs_assoc_lock);
  730. kobj->sd = NULL;
  731. spin_unlock(&sysfs_assoc_lock);
  732. __sysfs_remove_dir(sd);
  733. }
  734. int sysfs_rename(struct sysfs_dirent *sd,
  735. struct sysfs_dirent *new_parent_sd, const void *new_ns,
  736. const char *new_name)
  737. {
  738. int error;
  739. mutex_lock(&sysfs_mutex);
  740. error = 0;
  741. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  742. (strcmp(sd->s_name, new_name) == 0))
  743. goto out; /* nothing to rename */
  744. error = -EEXIST;
  745. if (sysfs_find_dirent(new_parent_sd, new_ns, new_name))
  746. goto out;
  747. /* rename sysfs_dirent */
  748. if (strcmp(sd->s_name, new_name) != 0) {
  749. error = -ENOMEM;
  750. new_name = kstrdup(new_name, GFP_KERNEL);
  751. if (!new_name)
  752. goto out;
  753. kfree(sd->s_name);
  754. sd->s_name = new_name;
  755. }
  756. /*
  757. * Move to the appropriate place in the appropriate directories rbtree.
  758. */
  759. sysfs_unlink_sibling(sd);
  760. sysfs_get(new_parent_sd);
  761. sysfs_put(sd->s_parent);
  762. sd->s_ns = new_ns;
  763. sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name);
  764. sd->s_parent = new_parent_sd;
  765. sysfs_link_sibling(sd);
  766. error = 0;
  767. out:
  768. mutex_unlock(&sysfs_mutex);
  769. return error;
  770. }
  771. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  772. const void *new_ns)
  773. {
  774. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  775. return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name);
  776. }
  777. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  778. const void *new_ns)
  779. {
  780. struct sysfs_dirent *sd = kobj->sd;
  781. struct sysfs_dirent *new_parent_sd;
  782. BUG_ON(!sd->s_parent);
  783. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  784. new_parent_kobj->sd : &sysfs_root;
  785. return sysfs_rename(sd, new_parent_sd, new_ns, sd->s_name);
  786. }
  787. /* Relationship between s_mode and the DT_xxx types */
  788. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  789. {
  790. return (sd->s_mode >> 12) & 15;
  791. }
  792. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  793. {
  794. sysfs_put(filp->private_data);
  795. return 0;
  796. }
  797. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  798. struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
  799. {
  800. if (pos) {
  801. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  802. pos->s_parent == parent_sd &&
  803. hash == pos->s_hash;
  804. sysfs_put(pos);
  805. if (!valid)
  806. pos = NULL;
  807. }
  808. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  809. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  810. while (node) {
  811. pos = to_sysfs_dirent(node);
  812. if (hash < pos->s_hash)
  813. node = node->rb_left;
  814. else if (hash > pos->s_hash)
  815. node = node->rb_right;
  816. else
  817. break;
  818. }
  819. }
  820. /* Skip over entries in the wrong namespace */
  821. while (pos && pos->s_ns != ns) {
  822. struct rb_node *node = rb_next(&pos->s_rb);
  823. if (!node)
  824. pos = NULL;
  825. else
  826. pos = to_sysfs_dirent(node);
  827. }
  828. return pos;
  829. }
  830. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  831. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  832. {
  833. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  834. if (pos)
  835. do {
  836. struct rb_node *node = rb_next(&pos->s_rb);
  837. if (!node)
  838. pos = NULL;
  839. else
  840. pos = to_sysfs_dirent(node);
  841. } while (pos && pos->s_ns != ns);
  842. return pos;
  843. }
  844. static int sysfs_readdir(struct file *file, struct dir_context *ctx)
  845. {
  846. struct dentry *dentry = file->f_path.dentry;
  847. struct sysfs_dirent *parent_sd = dentry->d_fsdata;
  848. struct sysfs_dirent *pos = file->private_data;
  849. enum kobj_ns_type type;
  850. const void *ns;
  851. type = sysfs_ns_type(parent_sd);
  852. ns = sysfs_info(dentry->d_sb)->ns[type];
  853. if (!dir_emit_dots(file, ctx))
  854. return 0;
  855. mutex_lock(&sysfs_mutex);
  856. for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
  857. pos;
  858. pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
  859. const char *name = pos->s_name;
  860. unsigned int type = dt_type(pos);
  861. int len = strlen(name);
  862. ino_t ino = pos->s_ino;
  863. ctx->pos = pos->s_hash;
  864. file->private_data = sysfs_get(pos);
  865. mutex_unlock(&sysfs_mutex);
  866. if (!dir_emit(ctx, name, len, ino, type))
  867. return 0;
  868. mutex_lock(&sysfs_mutex);
  869. }
  870. mutex_unlock(&sysfs_mutex);
  871. file->private_data = NULL;
  872. ctx->pos = INT_MAX;
  873. return 0;
  874. }
  875. static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
  876. {
  877. struct inode *inode = file_inode(file);
  878. loff_t ret;
  879. mutex_lock(&inode->i_mutex);
  880. ret = generic_file_llseek(file, offset, whence);
  881. mutex_unlock(&inode->i_mutex);
  882. return ret;
  883. }
  884. const struct file_operations sysfs_dir_operations = {
  885. .read = generic_read_dir,
  886. .iterate = sysfs_readdir,
  887. .release = sysfs_dir_release,
  888. .llseek = sysfs_dir_llseek,
  889. };