dir.c 26 KB

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