dir.c 24 KB

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